Wed, 19 Apr 2000 22:10:28 +0200
trunk: changeset 8
Moved include/*.h to include/libtu/
--- a/Makefile Wed Apr 19 22:03:51 2000 +0200 +++ b/Makefile Wed Apr 19 22:10:28 2000 +0200 @@ -7,6 +7,8 @@ ###################################### +INCLUDES += -I./include + OBJS= misc.o output.o util.o optparser.o parser.o tokenizer.o LIBDIR=$(PREFIX)/lib @@ -33,5 +35,5 @@ $(INSTALL) -d $(LIBDIR) $(INSTALL) -d $(INCDIR) $(INSTALL) -m $(DATA_MODE) libtu.a $(LIBDIR) - $(INSTALL) -m $(DATA_MODE) include/*.h $(INCDIR) + $(INSTALL) -m $(DATA_MODE) include/libtu/*.h $(INCDIR)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/libtu/misc.h Wed Apr 19 22:10:28 2000 +0200 @@ -0,0 +1,41 @@ +/* + * libtu/misc.h + * + * Copyright (c) Tuomo Valkonen 1999-2000. + * + * This file is distributed under the terms of the "Artistic License". + * See the included file LICENSE for details. + */ + +#ifndef __LIBTU_MISC_H +#define __LIBTU_MISC_H + +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> +#include "types.h" + +#define TR(X) X +#define DUMMY_TR(X) X + +#define ALLOC(X) (X*)malloczero(sizeof(X)) +#define ALLOC_N(X, N) (X*)malloczero(sizeof(X)*(N)) +#define REALLOC_N(PTR, X, S, N) (X*)remalloczero(PTR, sizeof(X)*(S), sizeof(X)*(N)) + +#define FREE(X) ({if(X!=NULL) free(X);}) + +extern void* malloczero(size_t size); +extern void* remalloczero(void *ptr, size_t oldsize, size_t newsize); + +extern char* scopy(const char *p); +extern char* scat(const char *p1, const char *p2); +extern char* scatn(const char *p1, ssize_t n1, const char *p2, ssize_t n2); +extern char* scat3(const char *p1, const char *p2, const char *p3); + +extern const char* simple_basename(const char *name); + +/* I dislike fread and fwrite... */ +extern bool readf(FILE *fd, void *buf, size_t n); +extern bool writef(FILE *fd, const void *buf, size_t n); + +#endif /* __LIBTU_MISC_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/libtu/optparser.h Wed Apr 19 22:10:28 2000 +0200 @@ -0,0 +1,68 @@ +/* + * libtu/optparser.h + * + * Copyright (c) Tuomo Valkonen 1999-2000. + * + * This file is distributed under the terms of the "Artistic License". + * See the included file LICENSE for details. + */ + +#ifndef __LIBTU_OPTPARSER_H +#define __LIBTU_OPTPARSER_H + +#include "types.h" + + +#define OPT_ID(X) ((X)|0x10000) +#define OPT_ID_RESERVED(X) ((X)|0x20000) + +/* OPTP_CHAIN is the normal behavior, i.e. single-letter options can be + * "chained" together: 'lr -lR'. Use for normal command line programs. + * OPTP_MIDLONG allows '-display foo' -like args but disables chaining + * of single-letter options. X programs should probably use this. + * OPTP_IMMEDIATE allows immediate arguments (-I/usr/include) (and disables + * chaining and midlong options). + * OPTP_NO_DASH is the same as OPTP_CHAIN but allows the dash to be omitted + * for 'tar xzf foo' -like behavior. + * Long '--foo=bar' options are supported in all of the modes. + */ + +enum{ + OPTP_CHAIN=0, + OPTP_MIDLONG=1, + OPTP_IMMEDIATE=2, + OPTP_NO_DASH=3 +}; + +enum{ + OPT_ARG=1, /* option has an argument */ + OPT_OPT_ARG=3 /* option may have an argument */ +}; + + +typedef struct{ + int optid; + const char *longopt; + int flags; +} OptParserOpt; + + +enum{ + OPT_ID_END=0, + OPT_ID_ARGUMENT=1, + + E_OPT_INVALID_OPTION=-1, + E_OPT_INVALID_CHAIN_OPTION=-2, + E_OPT_SYNTAX_ERROR=-3, + E_OPT_MISSING_ARGUMENT=-4, + E_OPT_UNEXPECTED_ARGUMENT=-5 +}; + + +extern void optparser_init(int argc, char *const argv[], int mode, + const OptParserOpt *opts); +extern int optparser_get_opt(); +extern const char* optparser_get_arg(); +extern void optparser_print_error(); + +#endif /* __LIBTU_OPTPARSER_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/libtu/output.h Wed Apr 19 22:10:28 2000 +0200 @@ -0,0 +1,50 @@ +/* + * libtu/output.h + * + * Copyright (c) Tuomo Valkonen 1999-2000. + * + * This file is distributed under the terms of the "Artistic License". + * See the included file LICENSE for details. + */ + +#ifndef __LIBTU_OUTPUT_H +#define __LIBTU_OUTPUT_H + +#include <stdarg.h> + +#include "types.h" + + +extern void verbose(const char *p, ...); +extern void verbose_v(const char *p, va_list args); +extern void verbose_enable(bool enable); +extern int verbose_indent(int depth); + +extern void warn_progname_enable(bool enable); + +extern void die(const char *p, ...); +extern void die_v(const char *p, va_list args); + +extern void die_obj(const char *obj, const char *p, ...); +extern void die_obj_v(const char *obj, const char *p, va_list args); +extern void die_obj_line(const char *obj, int line, const char *p, ...); +extern void die_obj_line_v(const char *obj, int line, const char *p, va_list args); + +extern void die_err(); +extern void die_err_obj(const char *obj); +extern void die_err_obj_line(const char *obj, int line); + + +extern void warn(const char *p, ...); +extern void warn_v(const char *p, va_list args); + +extern void warn_obj(const char *obj, const char *p, ...); +extern void warn_obj_v(const char *obj, const char *p, va_list args); +extern void warn_obj_line(const char *obj, int line, const char *p, ...); +extern void warn_obj_line_v(const char *obj, int line, const char *p, va_list args); + +extern void warn_err(); +extern void warn_err_obj(const char *obj); +extern void warn_err_obj_line(const char *obj, int line); + +#endif /* __LIBTU_OUTPUT_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/libtu/parser.h Wed Apr 19 22:10:28 2000 +0200 @@ -0,0 +1,45 @@ +/* + * libtu/parser.h + * + * Copyright (c) Tuomo Valkonen 1999-2000. + * + * This file is distributed under the terms of the "Artistic License". + * See the included file LICENSE for details. + */ + +#ifndef __LIBTU_PARSER_H +#define __LIBTU_PARSER_H + +#include "tokenizer.h" + +/* + * format: + * l = long + * d = double + * i = identifier + * s = string + * c = char + * . = 1 times any ("l.d") + * * = 0 or more times any (must be the last, "sd*") + * ? = optional ("?c") + * : = conditional (":c:s") + * + = 1 or more times last (most be the last, "l+") + * special entries: + * + * "#end" call this handler at the end of section. + * "#cancel" call this handler when recovering from error + */ + +typedef struct _ConfOpt{ + const char *optname; + const char *argfmt; + bool (*fn)(Tokenizer *tokz, int n, Token *toks); + struct _ConfOpt *opts; +} ConfOpt; + + +extern bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options); +extern bool parse_config(const char *fname, const ConfOpt *options, int flags); +extern bool parse_config_file(FILE *file, const ConfOpt *options, int flags); + +#endif /* __LIBTU_PARSER_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/libtu/tokenizer.h Wed Apr 19 22:10:28 2000 +0200 @@ -0,0 +1,191 @@ +/* + * libtu/tokenizer.h + * + * Copyright (c) Tuomo Valkonen 1999-2000. + * + * This file is distributed under the terms of the "Artistic License". + * See the included file LICENSE for details. + */ + +#ifndef __LIBTU_TOKENIZER_H +#define __LIBTU_TOKENIZER_H + +#include <stdio.h> +#include "types.h" + + +#define TOK_SET_LONG(TOK, VAL) {(TOK)->type=TOK_LONG; (TOK)->u.lval=VAL;} +#define TOK_SET_DOUBLE(TOK, VAL) {(TOK)->type=TOK_DOUBLE; (TOK)->u.dval=VAL;} +#define TOK_SET_CHAR(TOK, VAL) {(TOK)->type=TOK_CHAR; (TOK)->u.cval=VAL;} +#define TOK_SET_STRING(TOK, VAL) {(TOK)->type=TOK_STRING; (TOK)->u.sval=VAL;} +#define TOK_SET_IDENT(TOK, VAL) {(TOK)->type=TOK_IDENT; (TOK)->u.sval=VAL;} +#define TOK_SET_COMMENT(TOK, VAL) {(TOK)->type=TOK_COMMENT; (TOK)->u.sval=VAL;} +#define TOK_SET_OP(TOK, VAL) {(TOK)->type=TOK_OP; (TOK)->u.opval=VAL;} + +#define TOK_TYPE(TOK) ((TOK)->type) +#define TOK_LONG_VAL(TOK) ((TOK)->u.lval) +#define TOK_DOUBLE_VAL(TOK) ((TOK)->u.dval) +#define TOK_CHAR_VAL(TOK) ((TOK)->u.cval) +#define TOK_STRING_VAL(TOK) ((TOK)->u.sval) +#define TOK_IDENT_VAL(TOK) ((TOK)->u.sval) +#define TOK_COMMENT_VAL(TOK) ((TOK)->u.sval) +#define TOK_OP_VAL(TOK) ((TOK)->u.opval) + +#define TOK_IS_INVALID(TOK) ((TOK)->type==TOK_INVALID) +#define TOK_IS_LONG(TOK) ((TOK)->type==TOK_LONG) +#define TOK_IS_DOUBLE(TOK) ((TOK)->type==TOK_DOUBLE) +#define TOK_IS_CHAR(TOK) ((TOK)->type==TOK_CHAR) +#define TOK_IS_STRING(TOK) ((TOK)->type==TOK_STRING) +#define TOK_IS_IDENT(TOK) ((TOK)->type==TOK_IDENT) +#define TOK_IS_COMMENT(TOK) ((TOK)->type==TOK_COMMENT) +#define TOK_IS_OP(TOK) ((TOK)->type==TOK_OP) + +#define TOK_OP_IS(TOK, OP) ((TOK)->type==TOK_OP && (TOK)->u.opval==(OP)) + +#define TOK_TAKE_STRING_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) +#define TOK_TAKE_IDENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) +#define TOK_TAKE_COMMENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) + + +enum{ + TOK_INVALID=0, + TOK_LONG, + TOK_DOUBLE, + TOK_CHAR, + TOK_STRING, + TOK_IDENT, + TOK_COMMENT, + TOK_OP +}; + + +enum{ +#define OP2(X,Y) ((X)|((Y)<<8)) +#define OP3(X,Y,Z) ((X)|((Y)<<8)|((Z)<<16)) + + OP_L_PAR= '(', OP_R_PAR= ')', OP_L_BRK= '[', OP_R_BRK= ']', + OP_L_BRC= '{', OP_R_BRC= '}', OP_COMMA= ',', OP_SCOLON= ';', + + OP_PLUS= '+', OP_MINUS= '-', OP_MUL= '*', OP_DIV= '/', + OP_MOD= '%', OP_POW= '^', OP_OR= '|', OP_AND= '&', + /*OP_NOT= '~',*/ OP_NOT= '!', OP_ASGN= '=', OP_LT= '<', + OP_GT= '>', OP_DOT= '.', OP_COLON= ':', OP_QMARK= '?', + OP_AT= '@', + OP_NEXTLINE='\n',OP_EOF= -1, + + OP_INC= OP2('+','+'), OP_DEC= OP2('-','-'), + OP_LSHIFT= OP2('<','<'), OP_RSHIFT= OP2('>','>'), + OP_AS_INC= OP2('+','='), OP_AS_DEC= OP2('-','='), + OP_AS_MUL= OP2('*','='), OP_AS_DIV= OP2('/','='), + OP_AS_MOD= OP2('%','='), OP_AS_POW= OP2('^','='), + +/* AS_OR= OP2('|','='), AS_AND= OP2('&','='), */ + OP_EQ= OP2('=','='), OP_NE= OP2('!','='), + OP_LE= OP2('<','='), OP_GE= OP2('>','=') + +/* L_AND= OP2('&','&'), L_OR= OP2('|','|'), + L_XOR= OP2('^','^'), */ + +/* AsLShift= OP3('<','<','='), + AsRShift= OP3('>','>','='), */ + +#undef OP2 +#undef OP3 +}; + + +typedef struct{ + int type; + int line; + union{ + long lval; + double dval; + char cval; + char *sval; + int opval; + } u; +} Token; + +#define TOK_INIT {0, 0, {0}} + + +extern void tok_free(Token*tok); +extern void tok_init(Token*tok); + + +/* */ + + +enum{ + TOKZ_IGNORE_NEXTLINE=0x1, + TOKZ_READ_COMMENTS=0x2, + TOKZ_PARSER_INDENT_MODE=0x04, + TOKZ_ERROR_TOLERANT=0x8 +}; + + +enum{ + E_TOKZ_UNEXPECTED_EOF=1, + E_TOKZ_UNEXPECTED_EOL, + E_TOKZ_EOL_EXPECTED, + E_TOKZ_INVALID_CHAR, + E_TOKZ_TOOBIG, + E_TOKZ_NUMFMT, + E_TOKZ_NUM_JUNK, + E_TOKZ_NOTINT, + E_TOKZ_RANGE, + E_TOKZ_MULTICHAR, + + E_TOKZ_TOKEN_LIMIT, + E_TOKZ_UNKNOWN_OPTION, + E_TOKZ_SYNTAX, + E_TOKZ_INVALID_ARGUMENT, + E_TOKZ_EOS_EXPECTED, + E_TOKZ_TOO_FEW_ARGS, + E_TOKZ_TOO_MANY_ARGS, + E_TOKZ_MAX_NEST, + E_TOKZ_IDENTIFIER_EXPECTED, + + E_TOKZ_LBRACE_EXPECTED +}; + + +struct _ConfOpt; + +typedef struct _Tokenizer_FInfo{ + FILE *file; + char *name; + int line; + int ungetc; + Token ungettok; +} Tokenizer_FInfo; + +typedef struct _Tokenizer{ + FILE *file; + char *name; + int line; + int ungetc; + Token ungettok; + + int flags; + const struct _ConfOpt **optstack; + int nest_lvl; + void *user_data; + + int filestack_n; + Tokenizer_FInfo *filestack; +} Tokenizer; + + +extern Tokenizer *tokz_open(const char *fname); +extern Tokenizer *tokz_open_file(FILE *file); +extern void tokz_close(Tokenizer *tokz); +extern bool tokz_get_token(Tokenizer *tokz, Token *tok); +extern void tokz_unget_token(Tokenizer *tokz, Token *tok); +extern void tokz_warn_error(const Tokenizer *tokz, int line, int e); + +extern bool tokz_pushf(Tokenizer *tokz, const char *fname); +extern bool tokz_pushf_file(Tokenizer *tokz, FILE *file); +extern bool tokz_popf(Tokenizer *tokz); + +#endif /* __LIBTU_TOKENIZER_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/libtu/types.h Wed Apr 19 22:10:28 2000 +0200 @@ -0,0 +1,80 @@ +/* + * libtu/types.h + * + * Copyright (c) Tuomo Valkonen 1999-2000. + * + * This file is distributed under the terms of the "Artistic License". + * See the included file LICENSE for details. + */ + +#ifndef __LIBTU_TYPES_H +#define __LIBTU_TYPES_H + +#include <sys/types.h> + +#ifndef TRUE + #define TRUE 1 +#endif + +#ifndef FALSE + #define FALSE 0 +#endif + +#ifndef NULL + #define NULL ((void*)0) +#endif + +#ifndef LIBTU_TYPEDEF_UXXX + + /* All systems seem to define these whichever way they want to + * despite -D_*_SOURCE etc. so there is no easy way to know whether + * they can be typedef'd or not. Unless you want to go through using + * autoconf or similar methods. ==> Just stick to #define. :-( + */ + + #ifndef uchar + #define uchar unsigned char + #endif + #ifndef ushort + #define ushort unsigned short + #endif + #ifndef uint + #define uint unsigned int + #endif + #ifndef ulong + #define ulong unsigned long + #endif + +#else /* LIBTU_TYPEDEF_UXXX */ + + #ifndef uchar + typedef unsigned char uchar; + #endif + #ifndef ushort + typedef unsigned short ushort; + #endif + #ifndef uint + typedef unsigned int uint; + #endif + #ifndef ulong + typedef unsigned long ulong; + #endif + +#endif /* LIBTU_TYPEDEF_UXXX */ + + +#ifndef LIBTU_TYPEDEF_BOOL + + #ifndef bool + #define bool int + #endif + +#else /* LIBTU_TYPEDEF_BOOL */ + + #ifndef bool + typedef int bool; + #endif + +#endif /* LIBTU_TYPEDEF_BOOL */ + +#endif /* __LIBTU_TYPES_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/libtu/util.h Wed Apr 19 22:10:28 2000 +0200 @@ -0,0 +1,40 @@ +/* + * libtu/util.h + * + * Copyright (c) Tuomo Valkonen 1999-2000. + * + * This file is distributed under the terms of the "Artistic License". + * See the included file LICENSE for details. + */ + +#ifndef __LIBTU_UTIL_H +#define __LIBTU_UTIL_H + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include "types.h" + + +typedef struct{ + const char *name; + const char *version; + const char *authors; + const char *license; + const char *usage; +} ProgInfo; + + +extern void libtu_init_argv0(const char *argv0, const ProgInfo *info); +extern void libtu_init(int *argc, char *argv[], const ProgInfo *info); + +extern const char *prog_execname(); +extern const ProgInfo *prog_info(); +extern const char *prog_name(); +extern const char *prog_version(); +extern const char *prog_authors(); +extern const char *prog_license(); +extern const char *prog_usage(); + +#endif /* __LIBTU_UTIL_H */
--- a/include/misc.h Wed Apr 19 22:03:51 2000 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -/* - * libtu/misc.h - * - * Copyright (c) Tuomo Valkonen 1999-2000. - * - * This file is distributed under the terms of the "Artistic License". - * See the included file LICENSE for details. - */ - -#ifndef __LIBTU_MISC_H -#define __LIBTU_MISC_H - -#include <stdlib.h> -#include <stdio.h> -#include <assert.h> -#include "types.h" - -#define TR(X) X -#define DUMMY_TR(X) X - -#define ALLOC(X) (X*)malloczero(sizeof(X)) -#define ALLOC_N(X, N) (X*)malloczero(sizeof(X)*(N)) -#define REALLOC_N(PTR, X, S, N) (X*)remalloczero(PTR, sizeof(X)*(S), sizeof(X)*(N)) - -#define FREE(X) ({if(X!=NULL) free(X);}) - -extern void* malloczero(size_t size); -extern void* remalloczero(void *ptr, size_t oldsize, size_t newsize); - -extern char* scopy(const char *p); -extern char* scat(const char *p1, const char *p2); -extern char* scatn(const char *p1, ssize_t n1, const char *p2, ssize_t n2); -extern char* scat3(const char *p1, const char *p2, const char *p3); - -extern const char* simple_basename(const char *name); - -/* I dislike fread and fwrite... */ -extern bool readf(FILE *fd, void *buf, size_t n); -extern bool writef(FILE *fd, const void *buf, size_t n); - -#endif /* __LIBTU_MISC_H */
--- a/include/optparser.h Wed Apr 19 22:03:51 2000 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* - * libtu/optparser.h - * - * Copyright (c) Tuomo Valkonen 1999-2000. - * - * This file is distributed under the terms of the "Artistic License". - * See the included file LICENSE for details. - */ - -#ifndef __LIBTU_OPTPARSER_H -#define __LIBTU_OPTPARSER_H - -#include "types.h" - - -#define OPT_ID(X) ((X)|0x10000) -#define OPT_ID_RESERVED(X) ((X)|0x20000) - -/* OPTP_CHAIN is the normal behavior, i.e. single-letter options can be - * "chained" together: 'lr -lR'. Use for normal command line programs. - * OPTP_MIDLONG allows '-display foo' -like args but disables chaining - * of single-letter options. X programs should probably use this. - * OPTP_IMMEDIATE allows immediate arguments (-I/usr/include) (and disables - * chaining and midlong options). - * OPTP_NO_DASH is the same as OPTP_CHAIN but allows the dash to be omitted - * for 'tar xzf foo' -like behavior. - * Long '--foo=bar' options are supported in all of the modes. - */ - -enum{ - OPTP_CHAIN=0, - OPTP_MIDLONG=1, - OPTP_IMMEDIATE=2, - OPTP_NO_DASH=3 -}; - -enum{ - OPT_ARG=1, /* option has an argument */ - OPT_OPT_ARG=3 /* option may have an argument */ -}; - - -typedef struct{ - int optid; - const char *longopt; - int flags; -} OptParserOpt; - - -enum{ - OPT_ID_END=0, - OPT_ID_ARGUMENT=1, - - E_OPT_INVALID_OPTION=-1, - E_OPT_INVALID_CHAIN_OPTION=-2, - E_OPT_SYNTAX_ERROR=-3, - E_OPT_MISSING_ARGUMENT=-4, - E_OPT_UNEXPECTED_ARGUMENT=-5 -}; - - -extern void optparser_init(int argc, char *const argv[], int mode, - const OptParserOpt *opts); -extern int optparser_get_opt(); -extern const char* optparser_get_arg(); -extern void optparser_print_error(); - -#endif /* __LIBTU_OPTPARSER_H */
--- a/include/output.h Wed Apr 19 22:03:51 2000 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -/* - * libtu/output.h - * - * Copyright (c) Tuomo Valkonen 1999-2000. - * - * This file is distributed under the terms of the "Artistic License". - * See the included file LICENSE for details. - */ - -#ifndef __LIBTU_OUTPUT_H -#define __LIBTU_OUTPUT_H - -#include <stdarg.h> - -#include "types.h" - - -extern void verbose(const char *p, ...); -extern void verbose_v(const char *p, va_list args); -extern void verbose_enable(bool enable); -extern int verbose_indent(int depth); - -extern void warn_progname_enable(bool enable); - -extern void die(const char *p, ...); -extern void die_v(const char *p, va_list args); - -extern void die_obj(const char *obj, const char *p, ...); -extern void die_obj_v(const char *obj, const char *p, va_list args); -extern void die_obj_line(const char *obj, int line, const char *p, ...); -extern void die_obj_line_v(const char *obj, int line, const char *p, va_list args); - -extern void die_err(); -extern void die_err_obj(const char *obj); -extern void die_err_obj_line(const char *obj, int line); - - -extern void warn(const char *p, ...); -extern void warn_v(const char *p, va_list args); - -extern void warn_obj(const char *obj, const char *p, ...); -extern void warn_obj_v(const char *obj, const char *p, va_list args); -extern void warn_obj_line(const char *obj, int line, const char *p, ...); -extern void warn_obj_line_v(const char *obj, int line, const char *p, va_list args); - -extern void warn_err(); -extern void warn_err_obj(const char *obj); -extern void warn_err_obj_line(const char *obj, int line); - -#endif /* __LIBTU_OUTPUT_H */
--- a/include/parser.h Wed Apr 19 22:03:51 2000 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -/* - * libtu/parser.h - * - * Copyright (c) Tuomo Valkonen 1999-2000. - * - * This file is distributed under the terms of the "Artistic License". - * See the included file LICENSE for details. - */ - -#ifndef __LIBTU_PARSER_H -#define __LIBTU_PARSER_H - -#include "tokenizer.h" - -/* - * format: - * l = long - * d = double - * i = identifier - * s = string - * c = char - * . = 1 times any ("l.d") - * * = 0 or more times any (must be the last, "sd*") - * ? = optional ("?c") - * : = conditional (":c:s") - * + = 1 or more times last (most be the last, "l+") - * special entries: - * - * "#end" call this handler at the end of section. - * "#cancel" call this handler when recovering from error - */ - -typedef struct _ConfOpt{ - const char *optname; - const char *argfmt; - bool (*fn)(Tokenizer *tokz, int n, Token *toks); - struct _ConfOpt *opts; -} ConfOpt; - - -extern bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options); -extern bool parse_config(const char *fname, const ConfOpt *options, int flags); -extern bool parse_config_file(FILE *file, const ConfOpt *options, int flags); - -#endif /* __LIBTU_PARSER_H */
--- a/include/tokenizer.h Wed Apr 19 22:03:51 2000 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,191 +0,0 @@ -/* - * libtu/tokenizer.h - * - * Copyright (c) Tuomo Valkonen 1999-2000. - * - * This file is distributed under the terms of the "Artistic License". - * See the included file LICENSE for details. - */ - -#ifndef __LIBTU_TOKENIZER_H -#define __LIBTU_TOKENIZER_H - -#include <stdio.h> -#include "types.h" - - -#define TOK_SET_LONG(TOK, VAL) {(TOK)->type=TOK_LONG; (TOK)->u.lval=VAL;} -#define TOK_SET_DOUBLE(TOK, VAL) {(TOK)->type=TOK_DOUBLE; (TOK)->u.dval=VAL;} -#define TOK_SET_CHAR(TOK, VAL) {(TOK)->type=TOK_CHAR; (TOK)->u.cval=VAL;} -#define TOK_SET_STRING(TOK, VAL) {(TOK)->type=TOK_STRING; (TOK)->u.sval=VAL;} -#define TOK_SET_IDENT(TOK, VAL) {(TOK)->type=TOK_IDENT; (TOK)->u.sval=VAL;} -#define TOK_SET_COMMENT(TOK, VAL) {(TOK)->type=TOK_COMMENT; (TOK)->u.sval=VAL;} -#define TOK_SET_OP(TOK, VAL) {(TOK)->type=TOK_OP; (TOK)->u.opval=VAL;} - -#define TOK_TYPE(TOK) ((TOK)->type) -#define TOK_LONG_VAL(TOK) ((TOK)->u.lval) -#define TOK_DOUBLE_VAL(TOK) ((TOK)->u.dval) -#define TOK_CHAR_VAL(TOK) ((TOK)->u.cval) -#define TOK_STRING_VAL(TOK) ((TOK)->u.sval) -#define TOK_IDENT_VAL(TOK) ((TOK)->u.sval) -#define TOK_COMMENT_VAL(TOK) ((TOK)->u.sval) -#define TOK_OP_VAL(TOK) ((TOK)->u.opval) - -#define TOK_IS_INVALID(TOK) ((TOK)->type==TOK_INVALID) -#define TOK_IS_LONG(TOK) ((TOK)->type==TOK_LONG) -#define TOK_IS_DOUBLE(TOK) ((TOK)->type==TOK_DOUBLE) -#define TOK_IS_CHAR(TOK) ((TOK)->type==TOK_CHAR) -#define TOK_IS_STRING(TOK) ((TOK)->type==TOK_STRING) -#define TOK_IS_IDENT(TOK) ((TOK)->type==TOK_IDENT) -#define TOK_IS_COMMENT(TOK) ((TOK)->type==TOK_COMMENT) -#define TOK_IS_OP(TOK) ((TOK)->type==TOK_OP) - -#define TOK_OP_IS(TOK, OP) ((TOK)->type==TOK_OP && (TOK)->u.opval==(OP)) - -#define TOK_TAKE_STRING_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) -#define TOK_TAKE_IDENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) -#define TOK_TAKE_COMMENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) - - -enum{ - TOK_INVALID=0, - TOK_LONG, - TOK_DOUBLE, - TOK_CHAR, - TOK_STRING, - TOK_IDENT, - TOK_COMMENT, - TOK_OP -}; - - -enum{ -#define OP2(X,Y) ((X)|((Y)<<8)) -#define OP3(X,Y,Z) ((X)|((Y)<<8)|((Z)<<16)) - - OP_L_PAR= '(', OP_R_PAR= ')', OP_L_BRK= '[', OP_R_BRK= ']', - OP_L_BRC= '{', OP_R_BRC= '}', OP_COMMA= ',', OP_SCOLON= ';', - - OP_PLUS= '+', OP_MINUS= '-', OP_MUL= '*', OP_DIV= '/', - OP_MOD= '%', OP_POW= '^', OP_OR= '|', OP_AND= '&', - /*OP_NOT= '~',*/ OP_NOT= '!', OP_ASGN= '=', OP_LT= '<', - OP_GT= '>', OP_DOT= '.', OP_COLON= ':', OP_QMARK= '?', - OP_AT= '@', - OP_NEXTLINE='\n',OP_EOF= -1, - - OP_INC= OP2('+','+'), OP_DEC= OP2('-','-'), - OP_LSHIFT= OP2('<','<'), OP_RSHIFT= OP2('>','>'), - OP_AS_INC= OP2('+','='), OP_AS_DEC= OP2('-','='), - OP_AS_MUL= OP2('*','='), OP_AS_DIV= OP2('/','='), - OP_AS_MOD= OP2('%','='), OP_AS_POW= OP2('^','='), - -/* AS_OR= OP2('|','='), AS_AND= OP2('&','='), */ - OP_EQ= OP2('=','='), OP_NE= OP2('!','='), - OP_LE= OP2('<','='), OP_GE= OP2('>','=') - -/* L_AND= OP2('&','&'), L_OR= OP2('|','|'), - L_XOR= OP2('^','^'), */ - -/* AsLShift= OP3('<','<','='), - AsRShift= OP3('>','>','='), */ - -#undef OP2 -#undef OP3 -}; - - -typedef struct{ - int type; - int line; - union{ - long lval; - double dval; - char cval; - char *sval; - int opval; - } u; -} Token; - -#define TOK_INIT {0, 0, {0}} - - -extern void tok_free(Token*tok); -extern void tok_init(Token*tok); - - -/* */ - - -enum{ - TOKZ_IGNORE_NEXTLINE=0x1, - TOKZ_READ_COMMENTS=0x2, - TOKZ_PARSER_INDENT_MODE=0x04, - TOKZ_ERROR_TOLERANT=0x8 -}; - - -enum{ - E_TOKZ_UNEXPECTED_EOF=1, - E_TOKZ_UNEXPECTED_EOL, - E_TOKZ_EOL_EXPECTED, - E_TOKZ_INVALID_CHAR, - E_TOKZ_TOOBIG, - E_TOKZ_NUMFMT, - E_TOKZ_NUM_JUNK, - E_TOKZ_NOTINT, - E_TOKZ_RANGE, - E_TOKZ_MULTICHAR, - - E_TOKZ_TOKEN_LIMIT, - E_TOKZ_UNKNOWN_OPTION, - E_TOKZ_SYNTAX, - E_TOKZ_INVALID_ARGUMENT, - E_TOKZ_EOS_EXPECTED, - E_TOKZ_TOO_FEW_ARGS, - E_TOKZ_TOO_MANY_ARGS, - E_TOKZ_MAX_NEST, - E_TOKZ_IDENTIFIER_EXPECTED, - - E_TOKZ_LBRACE_EXPECTED -}; - - -struct _ConfOpt; - -typedef struct _Tokenizer_FInfo{ - FILE *file; - char *name; - int line; - int ungetc; - Token ungettok; -} Tokenizer_FInfo; - -typedef struct _Tokenizer{ - FILE *file; - char *name; - int line; - int ungetc; - Token ungettok; - - int flags; - const struct _ConfOpt **optstack; - int nest_lvl; - void *user_data; - - int filestack_n; - Tokenizer_FInfo *filestack; -} Tokenizer; - - -extern Tokenizer *tokz_open(const char *fname); -extern Tokenizer *tokz_open_file(FILE *file); -extern void tokz_close(Tokenizer *tokz); -extern bool tokz_get_token(Tokenizer *tokz, Token *tok); -extern void tokz_unget_token(Tokenizer *tokz, Token *tok); -extern void tokz_warn_error(const Tokenizer *tokz, int line, int e); - -extern bool tokz_pushf(Tokenizer *tokz, const char *fname); -extern bool tokz_pushf_file(Tokenizer *tokz, FILE *file); -extern bool tokz_popf(Tokenizer *tokz); - -#endif /* __LIBTU_TOKENIZER_H */
--- a/include/types.h Wed Apr 19 22:03:51 2000 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* - * libtu/types.h - * - * Copyright (c) Tuomo Valkonen 1999-2000. - * - * This file is distributed under the terms of the "Artistic License". - * See the included file LICENSE for details. - */ - -#ifndef __LIBTU_TYPES_H -#define __LIBTU_TYPES_H - -#include <sys/types.h> - -#ifndef TRUE - #define TRUE 1 -#endif - -#ifndef FALSE - #define FALSE 0 -#endif - -#ifndef NULL - #define NULL ((void*)0) -#endif - -#ifndef LIBTU_TYPEDEF_UXXX - - /* All systems seem to define these whichever way they want to - * despite -D_*_SOURCE etc. so there is no easy way to know whether - * they can be typedef'd or not. Unless you want to go through using - * autoconf or similar methods. ==> Just stick to #define. :-( - */ - - #ifndef uchar - #define uchar unsigned char - #endif - #ifndef ushort - #define ushort unsigned short - #endif - #ifndef uint - #define uint unsigned int - #endif - #ifndef ulong - #define ulong unsigned long - #endif - -#else /* LIBTU_TYPEDEF_UXXX */ - - #ifndef uchar - typedef unsigned char uchar; - #endif - #ifndef ushort - typedef unsigned short ushort; - #endif - #ifndef uint - typedef unsigned int uint; - #endif - #ifndef ulong - typedef unsigned long ulong; - #endif - -#endif /* LIBTU_TYPEDEF_UXXX */ - - -#ifndef LIBTU_TYPEDEF_BOOL - - #ifndef bool - #define bool int - #endif - -#else /* LIBTU_TYPEDEF_BOOL */ - - #ifndef bool - typedef int bool; - #endif - -#endif /* LIBTU_TYPEDEF_BOOL */ - -#endif /* __LIBTU_TYPES_H */
--- a/include/util.h Wed Apr 19 22:03:51 2000 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/* - * libtu/util.h - * - * Copyright (c) Tuomo Valkonen 1999-2000. - * - * This file is distributed under the terms of the "Artistic License". - * See the included file LICENSE for details. - */ - -#ifndef __LIBTU_UTIL_H -#define __LIBTU_UTIL_H - -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> - -#include "types.h" - - -typedef struct{ - const char *name; - const char *version; - const char *authors; - const char *license; - const char *usage; -} ProgInfo; - - -extern void libtu_init_argv0(const char *argv0, const ProgInfo *info); -extern void libtu_init(int *argc, char *argv[], const ProgInfo *info); - -extern const char *prog_execname(); -extern const ProgInfo *prog_info(); -extern const char *prog_name(); -extern const char *prog_version(); -extern const char *prog_authors(); -extern const char *prog_license(); -extern const char *prog_usage(); - -#endif /* __LIBTU_UTIL_H */
--- a/misc.c Wed Apr 19 22:03:51 2000 +0200 +++ b/misc.c Wed Apr 19 22:10:28 2000 +0200 @@ -11,7 +11,7 @@ #include <stdio.h> #include <string.h> -#include "include/misc.h" +#include <libtu/misc.h> void *malloczero(size_t size)
--- a/optparser.c Wed Apr 19 22:03:51 2000 +0200 +++ b/optparser.c Wed Apr 19 22:10:28 2000 +0200 @@ -10,10 +10,10 @@ #include <string.h> #include <stdlib.h> -#include "include/util.h" -#include "include/misc.h" -#include "include/optparser.h" -#include "include/output.h" +#include <libtu/util.h> +#include <libtu/misc.h> +#include <libtu/optparser.h> +#include <libtu/output.h> #define O_ARGS(o) (o->flags&OPT_OPT_ARG) @@ -61,17 +61,6 @@ } -/*static bool valid_chain(const char *p, const OptParserOpt *o) -{ - while(*p!='\0'){ - if(!find_chain_opt(*p, o)) - return FALSE; - p++; - } - return TRUE; -}*/ - - static bool is_option(const char *p) { if(p==NULL) @@ -138,10 +127,6 @@ o_args_left=o_left; RET(OPT_ID_ARGUMENT); } - /*argptr=strchr(p, '='); - if(argptr!=NULL) - argptr++; - */ type=LONG; p2=p+2; }else{
--- a/output.c Wed Apr 19 22:03:51 2000 +0200 +++ b/output.c Wed Apr 19 22:10:28 2000 +0200 @@ -13,9 +13,9 @@ #include <strings.h> #include <string.h> -#include "include/misc.h" -#include "include/output.h" -#include "include/util.h" +#include <libtu/misc.h> +#include <libtu/output.h> +#include <libtu/util.h> /* verbose
--- a/parser.c Wed Apr 19 22:03:51 2000 +0200 +++ b/parser.c Wed Apr 19 22:10:28 2000 +0200 @@ -9,9 +9,9 @@ #include <string.h> -#include "include/parser.h" -#include "include/misc.h" -#include "include/output.h" +#include <libtu/parser.h> +#include <libtu/misc.h> +#include <libtu/output.h> #define MAX_TOKENS 32 #define MAX_NEST 16
--- a/tester.c Wed Apr 19 22:03:51 2000 +0200 +++ b/tester.c Wed Apr 19 22:10:28 2000 +0200 @@ -8,8 +8,9 @@ */ #include <stdio.h> -#include "include/misc.h" -#include "include/tokenizer.h" + +#include <libtu/misc.h> +#include <libtu/tokenizer.h> int main(void)
--- a/tester2.c Wed Apr 19 22:03:51 2000 +0200 +++ b/tester2.c Wed Apr 19 22:10:28 2000 +0200 @@ -8,10 +8,11 @@ */ #include <stdio.h> -#include "include/misc.h" -#include "include/tokenizer.h" -#include "include/parser.h" -#include "include/util.h" + +#include <libtu/misc.h> +#include <libtu/tokenizer.h> +#include <libtu/parser.h> +#include <libtu/util.h> static bool test_fn(Tokenizer *tokz, int n, Token *toks)
--- a/tester3.c Wed Apr 19 22:03:51 2000 +0200 +++ b/tester3.c Wed Apr 19 22:10:28 2000 +0200 @@ -8,9 +8,10 @@ */ #include <stdio.h> -#include "include/util.h" -#include "include/misc.h" -#include "include/optparser.h" + +#include <libtu/util.h> +#include <libtu/misc.h> +#include <libtu/optparser.h> static const char usage[]=
--- a/tokenizer.c Wed Apr 19 22:03:51 2000 +0200 +++ b/tokenizer.c Wed Apr 19 22:10:28 2000 +0200 @@ -15,9 +15,9 @@ #include <math.h> #include <string.h> -#include "include/tokenizer.h" -#include "include/misc.h" -#include "include/output.h" +#include <libtu/tokenizer.h> +#include <libtu/misc.h> +#include <libtu/output.h> static const char *errors[]={