diff -r 000000000000 -r 86b7f6f9c5c0 util.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util.c Tue Feb 15 18:57:52 2005 +0100 @@ -0,0 +1,227 @@ +/* + * libtu/util.c + * + * Copyright (c) Tuomo Valkonen 1999-2000. + * + * This file is distributed under the terms of the "Artistic License". + * See the included file LICENSE for details. + */ + +#include +#include +#include +#include + +#ifdef CONFIG_LOCALE +#include +#endif + +#include "include/util.h" +#include "include/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) +{ + progname=argv0; + proginfo=info; + +#ifdef CONFIG_LOCALE + textdomain(simple_basename(argv0)); +#endif +} + + +void libtu_init(int *argc, char *argv[], const ProgInfo *info) +{ + libtu_init_argv0(argv[0], info); + + common_opts(argc, argv); +} + + +const char *prog_execname() +{ + 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; l0; 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); + } +}