--- a/util.c Sat May 20 17:43:36 2000 +0200 +++ b/util.c Sun May 21 17:33:48 2000 +0200 @@ -18,16 +18,12 @@ #include <libtu/misc.h> -static const ProgInfo *proginfo=NULL; static const char *progname=NULL; -static void common_opts(int *argc, char *argv[]); - -void libtu_init_argv0(const char *argv0, const ProgInfo *info) +void libtu_init(const char *argv0) { progname=argv0; - proginfo=info; #ifdef CONFIG_LOCALE textdomain(simple_basename(argv0)); @@ -35,11 +31,22 @@ } -void libtu_init(int *argc, char *argv[], const ProgInfo *info) +void libtu_init_copt(int argc, char *const argv[], + const OptParserCommonInfo *cinfo) { - libtu_init_argv0(argv[0], info); + int opt; + + libtu_init(argv[0]); + + optparser_init(argc, argv, OPTP_DEFAULT, NULL, cinfo); - common_opts(argc, argv); + while((opt=optparser_get_opt())){ + switch(opt){ + default: + optparser_print_error(); + exit(EXIT_FAILURE); + } + } } @@ -48,178 +55,3 @@ return progname; } - -const ProgInfo *prog_info() -{ - return proginfo; -} - - -const char *prog_name() -{ - return proginfo ? proginfo->name : NULL; -} - - -const char *prog_version() -{ - return proginfo ? proginfo->version : NULL; -} - - -const char *prog_authors() -{ - return proginfo ? proginfo->authors : NULL; -} - - -const char *prog_license() -{ - return proginfo ? proginfo->license : NULL; -} - - -const char *prog_usage() -{ - return proginfo ? proginfo->usage : NULL; -} - - -/* */ - - -static char *usages[][2]={ - {"--help", "Show this help\n"}, - {"--version", "Show program version\n"}, - {"--authors", "Show program authors\n"}, - {"--license", "Show program license\n"}, - {"--proginfo", "Show program info\n"}, - {NULL,} -}; - - -static void common_usage(size_t start, size_t len) -{ - size_t l; - int i; - - for(i=0; usages[i][0]!=NULL; i++){ - - for(l=0; l<start; l++){ - putc(' ', stdout); - }; - - l=strlen(usages[i][0]); - writef(stdout, usages[i][0], l); - - do{ - putc(' ', stdout); - }while(++l<len-start); - - writef(stdout, usages[i][1], strlen(usages[i][1])); - } -} - - -static void show_usage(const char *p) -{ - const char *tmp; - size_t len; - size_t start; - - do{ - tmp=strchr(p, '\n'); - - if(tmp==NULL){ - len=strlen(p); - if(len==0) - break; - }else{ - len=tmp-p; - } - - start=strspn(p, " "); - - if(p[start]=='$' && p[start+1]=='u'){ - tmp=prog_execname(); - if(start!=0) - writef(stdout, p, start); - printf(TR("Usage: %s"), tmp); - writef(stdout, p+start+2, len-start-2); - putc('\n', stdout); - }else if(p[start]=='$' && p[start+1]=='c'){ - common_usage(start, len); - }else{ - writef(stdout, p, len); - putc('\n', stdout); - } - - p+=len+1; - }while(*(p-1)!='\0'); - -} - - -static void common_opts(int *argcp, char *argv[]) -{ - int argc=*argcp; - const char *p, *p2; - - argc--; - argv++; - - for(; argc>0; argc--, argv++){ - if(strcmp(*argv, "--help")==0){ - p=prog_usage(); - if(p==NULL){ - printf(TR("Usage: %s [options]\n" - "Where options are:\n\n"), prog_execname()); - common_usage(5, 20); - printf("\n"); - }else{ - show_usage(p); - } - }else if(strcmp(*argv, "--version")==0){ - p=prog_version(); - if(p==NULL) - printf(TR("No version available\n")); - else - printf("%s\n", p); - }else if(strcmp(*argv, "--authors")==0){ - p=prog_authors(); - if(p==NULL) - printf(TR("No author(s) info available\n")); - else - printf("%s\n", p); - }else if(strcmp(*argv, "--license")==0){ - p=prog_license(); - if(p==NULL) - printf(TR("No license available\n")); - else - printf("%s", TR(p)); - }else if(strcmp(*argv, "--proginfo")==0){ - p2=prog_version(); - p=prog_name(); - - if(p==NULL){ - p=prog_execname(); - - if(p==NULL){ - printf(TR("No name available\n")); - break; - } - } - - if(p2) - printf("%s v%s\n", p, p2); - else - printf("%s\n", p); - }else if(strcmp(*argv, "--")){ - break; - }else{ - continue; - } - - exit(EXIT_SUCCESS); - } -}