# HG changeset patch # User tuomov # Date 1091238251 -7200 # Node ID 81e9a32b1c0c536295e00cc4da4a2aecbbaee667 # Parent 9a1ce6020530b7a0b5bc397a73b584f4d5bb3172 trunk: changeset 1689 Removed stuff from optparser. diff -r 9a1ce6020530 -r 81e9a32b1c0c misc.h --- a/misc.h Mon Jul 26 17:40:53 2004 +0200 +++ b/misc.h Sat Jul 31 03:44:11 2004 +0200 @@ -15,9 +15,6 @@ #include #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)) diff -r 9a1ce6020530 -r 81e9a32b1c0c optparser.c --- a/optparser.c Mon Jul 26 17:40:53 2004 +0200 +++ b/optparser.c Sat Jul 31 03:44:11 2004 +0200 @@ -1,7 +1,7 @@ /* * libtu/optparser.c * - * Copyright (c) Tuomo Valkonen 1999-2002. + * Copyright (c) Tuomo Valkonen 1999-2004. * * You may distribute and modify this library under the terms of either * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. @@ -14,6 +14,7 @@ #include "misc.h" #include "optparser.h" #include "output.h" +#include "private.h" #define O_ARGS(o) (o->flags&OPT_OPT_ARG) @@ -21,25 +22,6 @@ #define O_OPT_ARG(o) (O_ARGS(o)==OPT_OPT_ARG) #define O_ID(o) (o->optid) -#define OPT_ID_HELP OPT_ID_RESERVED('h') -#define OPT_ID_VERSION OPT_ID_RESERVED('V') -#define OPT_ID_ABOUT OPT_ID_RESERVED('a'|OPT_ID_NOSHORT_FLAG) - - -static OptParserOpt common_opts[]={ - {OPT_ID_HELP, "help", 0, NULL, DUMMY_TR("Show this help")}, - {OPT_ID_VERSION, "version", 0, NULL, DUMMY_TR("Show program version")}, - {OPT_ID_ABOUT, "about", 0, NULL, DUMMY_TR("Show about text")}, - {0, NULL, 0, NULL, NULL} -}; - - -static OptParserCommonInfo dummy_cinfo[]={ - NULL, /* version */ - "Usage: $p\n", /* usage_tmpl */ - NULL /* about */ -}; - static const OptParserOpt *o_opts=NULL; static char *const *o_current=NULL; @@ -49,30 +31,21 @@ static const char*o_tmp=NULL; static int o_error=0; static int o_mode=OPTP_CHAIN; -static const OptParserCommonInfo *o_cinfo=NULL; - - -/* */ - - -static void print_help(const OptParserOpt *opts, bool midlong, - const OptParserCommonInfo *cinfo); /* */ void optparser_init(int argc, char *const argv[], int mode, - const OptParserOpt *opts, const OptParserCommonInfo *cinfo) + const OptParserOpt *opts) { o_mode=mode; - o_opts=(opts==NULL ? common_opts : opts); + o_opts=opts; o_current=argv+1; o_left=argc-1; o_chain_ptr=NULL; o_args_left=0; o_tmp=NULL; - o_cinfo=(cinfo==NULL ? dummy_cinfo : cinfo); } @@ -110,7 +83,7 @@ }; -static int optparser_do_get_opt() +int optparser_get_opt() { #define RET(X) return o_tmp=p, o_error=X const char *p, *p2=NULL; @@ -173,7 +146,6 @@ o=o_opts; -again: for(; O_ID(o); o++){ if(type==LONG){ /* Do long option (--foo=bar) */ @@ -244,11 +216,6 @@ return O_ID(o); } - if(o!=&(common_opts[3])){ - o=common_opts; - goto again; - } - if(dash) RET(E_OPT_INVALID_OPTION); @@ -257,33 +224,6 @@ } -int optparser_get_opt() -{ - int oid=optparser_do_get_opt(); - - if(oid<=0 || (oid&OPT_ID_RESERVED_FLAG)==0) - return oid; - - switch(oid){ - case OPT_ID_ABOUT: - if(o_cinfo->about!=NULL) - printf("%s", o_cinfo->about); - break; - - case OPT_ID_VERSION: - if(o_cinfo->version!=NULL) - printf("%s\n", o_cinfo->version); - break; - - case OPT_ID_HELP: - print_help(o_opts, o_mode==OPTP_MIDLONG, o_cinfo); - break; - } - - exit(EXIT_SUCCESS); -} - - /* */ @@ -511,65 +451,21 @@ } -static void print_opts(const OptParserOpt *opts, bool midlong, - const OptParserCommonInfo *cinfo) +void optparser_printhelp(int mode, const OptParserOpt *opts) { uint w, maxw=0; const OptParserOpt *o; + bool midlong=mode&OPTP_MIDLONG; o=opts; -again: for(; O_ID(o); o++){ w=opt_w(o, midlong); if(w>maxw) maxw=w; } - if(o!=&(common_opts[3])){ - o=common_opts; - goto again; - } - o=opts; -again2: + for(; O_ID(o); o++) print_opt(o, midlong, maxw, TERM_W); - - if(o!=&(common_opts[3])){ - printf("\n"); - o=common_opts; - goto again2; - } } - - -static void print_help(const OptParserOpt *opts, bool midlong, - const OptParserCommonInfo *cinfo) -{ - const char *tmp, *p=cinfo->usage_tmpl; - size_t len; - size_t start; - - while(1){ - tmp=strchr(p, '$'); - - if(tmp==NULL){ - writef(stdout, p, strlen(p)); - return; - } - - if(tmp!=p) - writef(stdout, p, tmp-p); - - p=tmp+1; - - if(*p=='p'){ - tmp=prog_execname(); - writef(stdout, tmp, strlen(tmp)); - }else if(*p=='o'){ - print_opts(opts, midlong, cinfo); - } - p++; - } -} - diff -r 9a1ce6020530 -r 81e9a32b1c0c optparser.h --- a/optparser.h Mon Jul 26 17:40:53 2004 +0200 +++ b/optparser.h Sat Jul 31 03:44:11 2004 +0200 @@ -1,7 +1,7 @@ /* * libtu/optparser.h * - * Copyright (c) Tuomo Valkonen 1999-2002. + * Copyright (c) Tuomo Valkonen 1999-2004. * * You may distribute and modify this library under the terms of either * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. @@ -54,14 +54,6 @@ const char *descr; } OptParserOpt; - -typedef struct _OptParserCommonInfo{ - const char *version; - const char *usage_tmpl; - const char *about; -} OptParserCommonInfo; - - enum{ OPT_ID_END=0, OPT_ID_ARGUMENT=1, @@ -74,9 +66,10 @@ }; -extern void optparser_init(int argc, char *const argv[], int mode, - const OptParserOpt *opts, - const OptParserCommonInfo *cinfo); +extern void optparser_init(int argc, char *const argv[], int mode, + const OptParserOpt *opts); + +extern void optparser_printhelp(int mode, const OptParserOpt *opts); extern int optparser_get_opt(); extern const char* optparser_get_arg(); diff -r 9a1ce6020530 -r 81e9a32b1c0c output.c --- a/output.c Mon Jul 26 17:40:53 2004 +0200 +++ b/output.c Sat Jul 31 03:44:11 2004 +0200 @@ -20,6 +20,7 @@ #include "misc.h" #include "output.h" #include "util.h" +#include "private.h" #if !defined(HAS_SYSTEM_ASPRINTF) #include "snprintf_2.2/snprintf.h" @@ -112,7 +113,7 @@ static void fallback_warn() { put_prog_name(); - fprintf(stderr, "Oops. Error string compilation failed: %s", + fprintf(stderr, TR("Oops. Error string compilation failed: %s"), strerror(errno)); } @@ -243,12 +244,12 @@ if(obj!=NULL){ if(line>0) - asprintf(&res1, TR("%s:%d: "), obj, line); + asprintf(&res1, "%s:%d: ", obj, line); else asprintf(&res1, "%s: ", obj); }else{ if(line>0) - asprintf(&res1, TR("%d: "), line); + asprintf(&res1, "%d: ", line); } vasprintf(&res2, p, args); if(res1!=NULL){ @@ -287,14 +288,14 @@ char *res; if(obj!=NULL){ if(line>0) - asprintf(&res, TR("%s:%d: %s\n"), obj, line, strerror(errno)); + asprintf(&res, "%s:%d: %s\n", obj, line, strerror(errno)); else asprintf(&res, "%s: %s\n", obj, strerror(errno)); }else{ if(line>0) - asprintf(&res, TR("%d: %s\n"), line, strerror(errno)); + asprintf(&res, "%d: %s\n", line, strerror(errno)); else - asprintf(&res, TR("%s\n"), strerror(errno)); + asprintf(&res, "%s\n", strerror(errno)); } return res; } diff -r 9a1ce6020530 -r 81e9a32b1c0c private.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/private.h Sat Jul 31 03:44:11 2004 +0200 @@ -0,0 +1,27 @@ +/* + * libtu/private.h + * + * Copyright (c) Tuomo Valkonen 2004. + * + * You may distribute and modify this library under the terms of either + * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. + */ + +#ifndef LIBTU_PRIVATE_H +#define LIBTU_PRIVATE_H + +#ifdef CF_L10N + +#include + +#define TR(X) dgettext("libtu", X) + +#else + +#define TR(X) (X) + +#endif + +#define DUMMY_TR(X) X + +#endif /* LIBTU_PRIVATE_H */ diff -r 9a1ce6020530 -r 81e9a32b1c0c tokenizer.c --- a/tokenizer.c Mon Jul 26 17:40:53 2004 +0200 +++ b/tokenizer.c Sat Jul 31 03:44:11 2004 +0200 @@ -18,6 +18,7 @@ #include "tokenizer.h" #include "misc.h" #include "output.h" +#include "private.h" static const char *errors[]={ diff -r 9a1ce6020530 -r 81e9a32b1c0c util.c --- a/util.c Mon Jul 26 17:40:53 2004 +0200 +++ b/util.c Sat Jul 31 03:44:11 2004 +0200 @@ -33,25 +33,6 @@ } -void libtu_init_copt(int argc, char *const argv[], - const OptParserCommonInfo *cinfo) -{ - int opt; - - libtu_init(argv[0]); - - optparser_init(argc, argv, OPTP_DEFAULT, NULL, cinfo); - - while((opt=optparser_get_opt())){ - switch(opt){ - default: - optparser_print_error(); - exit(EXIT_FAILURE); - } - } -} - - const char *prog_execname() { return progname; diff -r 9a1ce6020530 -r 81e9a32b1c0c util.h --- a/util.h Mon Jul 26 17:40:53 2004 +0200 +++ b/util.h Sat Jul 31 03:44:11 2004 +0200 @@ -18,10 +18,6 @@ #include "optparser.h" extern void libtu_init(const char *argv0); -extern void libtu_init_copt(int argc, char *const argv[], - const OptParserCommonInfo *cinfo); - - extern const char *prog_execname(); #endif /* LIBTU_UTIL_H */