Tue, 15 Feb 2005 18:57:52 +0100
Tailorization of trunk
Import of the upstream sources from the repository
http://tao.uab.es/ion/svn/libtu/trunk
as of revision 2
/* * 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 <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef CONFIG_LOCALE #include <libintl.h> #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; 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); } }