diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/argparse.c | 193 | ||||
-rw-r--r-- | util/iobuf.c | 27 | ||||
-rw-r--r-- | util/logger.c | 27 |
3 files changed, 145 insertions, 102 deletions
diff --git a/util/argparse.c b/util/argparse.c index 3d51d014c..89e6b26c0 100644 --- a/util/argparse.c +++ b/util/argparse.c @@ -27,101 +27,101 @@ #include "util.h" -#ifdef DOCUMENTATION -@Summary arg_parse - #include <wk/lib.h> - - typedef struct { - char *argc; /* pointer to argc (value subject to change) */ - char ***argv; /* pointer to argv (value subject to change) */ - unsigned flags; /* Global flags (DO NOT CHANGE) */ - int err; /* print error about last option */ - /* 1 = warning, 2 = abort */ - int r_opt; /* return option */ - int r_type; /* type of return value (0 = no argument found)*/ - union { - int ret_int; - long ret_long - ulong ret_ulong; - char *ret_str; - } r; /* Return values */ - struct { - int index; - const char *last; - } internal; /* DO NOT CHANGE */ - } ARGPARSE_ARGS; - - typedef struct { - int short_opt; - const char *long_opt; - unsigned flags; - } ARGPARSE_OPTS; - - int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts ); - -@Description - This is my replacement for getopt(). See the example for a typical usage. - Global flags are: - Bit 0 : Do not remove options form argv - Bit 1 : Do not stop at last option but return other args - with r_opt set to -1. - Bit 2 : Assume options and real args are mixed. - Bit 3 : Do not use -- to stop option processing. - Bit 4 : Do not skip the first arg. - Bit 5 : allow usage of long option with only one dash - all other bits must be set to zero, this value is modified by the function - so assume this is write only. - Local flags (for each option): - Bit 2-0 : 0 = does not take an argument - 1 = takes int argument - 2 = takes string argument - 3 = takes long argument - 4 = takes ulong argument - Bit 3 : argument is optional (r_type will the be set to 0) - Bit 4 : allow 0x etc. prefixed values. - If can stop the option processing by setting opts to NULL, the function will - then return 0. -@Return Value - Returns the args.r_opt or 0 if ready - r_opt may be -2 to indicate an unknown option. -@See Also - ArgExpand -@Notes - You do not need to process the options 'h', '--help' or '--version' - because this function includes standard help processing; but if you - specify '-h', '--help' or '--version' you have to do it yourself. - The option '--' stops argument processing; if bit 1 is set the function - continues to return normal arguments. - To process float args or unsigned args you must use a string args and do - the conversion yourself. -@Example - - ARGPARSE_OPTS opts[] = { - { 'v', "verbose", 0 }, - { 'd', "debug", 0 }, - { 'o', "output", 2 }, - { 'c', "cross-ref", 2|8 }, - { 'm', "my-option", 1|8 }, - { 500, "have-no-short-option-for-this-long-option", 0 }, - {0} }; - ARGPARSE_ARGS pargs = { &argc, &argv, 0 } - - while( ArgParse( &pargs, &opts) ) { - switch( pargs.r_opt ) { - case 'v': opt.verbose++; break; - case 'd': opt.debug++; break; - case 'o': opt.outfile = pargs.r.ret_str; break; - case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break; - case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break; - case 500: opt.a_long_one++; break - default : pargs.err = 1; break; /* force warning output */ - } - } - if( argc > 1 ) - log_fatal( "Too many args"); - -#endif /*DOCUMENTATION*/ +/********************************* + * @Summary arg_parse + * #include <wk/lib.h> + * + * typedef struct { + * char *argc; pointer to argc (value subject to change) + * char ***argv; pointer to argv (value subject to change) + * unsigned flags; Global flags (DO NOT CHANGE) + * int err; print error about last option + * 1 = warning, 2 = abort + * int r_opt; return option + * int r_type; type of return value (0 = no argument found) + * union { + * int ret_int; + * long ret_long + * ulong ret_ulong; + * char *ret_str; + * } r; Return values + * struct { + * int index; + * const char *last; + * } internal; DO NOT CHANGE + * } ARGPARSE_ARGS; + * + * typedef struct { + * int short_opt; + * const char *long_opt; + * unsigned flags; + * } ARGPARSE_OPTS; + * + * int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts ); + * + * @Description + * This is my replacement for getopt(). See the example for a typical usage. + * Global flags are: + * Bit 0 : Do not remove options form argv + * Bit 1 : Do not stop at last option but return other args + * with r_opt set to -1. + * Bit 2 : Assume options and real args are mixed. + * Bit 3 : Do not use -- to stop option processing. + * Bit 4 : Do not skip the first arg. + * Bit 5 : allow usage of long option with only one dash + * all other bits must be set to zero, this value is modified by the function + * so assume this is write only. + * Local flags (for each option): + * Bit 2-0 : 0 = does not take an argument + * 1 = takes int argument + * 2 = takes string argument + * 3 = takes long argument + * 4 = takes ulong argument + * Bit 3 : argument is optional (r_type will the be set to 0) + * Bit 4 : allow 0x etc. prefixed values. + * If can stop the option processing by setting opts to NULL, the function will + * then return 0. + * @Return Value + * Returns the args.r_opt or 0 if ready + * r_opt may be -2 to indicate an unknown option. + * @See Also + * ArgExpand + * @Notes + * You do not need to process the options 'h', '--help' or '--version' + * because this function includes standard help processing; but if you + * specify '-h', '--help' or '--version' you have to do it yourself. + * The option '--' stops argument processing; if bit 1 is set the function + * continues to return normal arguments. + * To process float args or unsigned args you must use a string args and do + * the conversion yourself. + * @Example + * + * ARGPARSE_OPTS opts[] = { + * { 'v', "verbose", 0 }, + * { 'd', "debug", 0 }, + * { 'o', "output", 2 }, + * { 'c', "cross-ref", 2|8 }, + * { 'm', "my-option", 1|8 }, + * { 500, "have-no-short-option-for-this-long-option", 0 }, + * {0} }; + * ARGPARSE_ARGS pargs = { &argc, &argv, 0 } + * + * while( ArgParse( &pargs, &opts) ) { + * switch( pargs.r_opt ) { + * case 'v': opt.verbose++; break; + * case 'd': opt.debug++; break; + * case 'o': opt.outfile = pargs.r.ret_str; break; + * case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break; + * case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break; + * case 500: opt.a_long_one++; break + * default : pargs.err = 1; break; -- force warning output -- + * } + * } + * if( argc > 1 ) + * log_fatal( "Too many args"); + * + */ static void set_opt_arg(ARGPARSE_ARGS *arg, unsigned flags, char *s); @@ -394,6 +394,10 @@ show_help( ARGPARSE_OPTS *opts, unsigned flags ) if( flags & 32 ) puts("\n(A single dash may be used instead of the double ones)"); } + if( *(s=strusage(26)) ) { /* bug reports to ... */ + putchar('\n'); + fputs(s, stdout); + } fflush(stdout); exit(0); } @@ -487,6 +491,7 @@ default_strusage( int level ) case 15: p = "[Untitled]"; break; case 23: p = "[unknown]"; break; case 24: p = ""; break; + case 26: p = ""; break; case 12: p = "This is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU General Public License as published by\n" diff --git a/util/iobuf.c b/util/iobuf.c index 6c4a5f5da..466f07e31 100644 --- a/util/iobuf.c +++ b/util/iobuf.c @@ -249,6 +249,7 @@ iobuf_alloc(int usage, size_t bufsize) a->d.size = bufsize; a->no = ++number; a->subno = 0; + a->opaque = NULL; return a; } @@ -280,7 +281,13 @@ iobuf_close( IOBUF a ) int iobuf_cancel( IOBUF a ) { - /* FIXME: do an unlink if usage is 2 */ + const char *s; + + if( a->usage == 2 ) { + s = iobuf_get_fname(a); + if( s && *s ) + remove(s); /* remove the file. Fixme: this will fail for MSDOZE*/ + } /* because the file is still open */ return iobuf_close(a); } @@ -404,6 +411,7 @@ iobuf_push_filter( IOBUF a, b->recorder.buf = NULL; /* make a link from the new stream to the original stream */ a->chain = b; + a->opaque = b->opaque; /* setup the function on the new stream */ a->filter = f; @@ -734,6 +742,23 @@ iobuf_get_filelength( IOBUF a ) } /**************** + * Retrieve the filename + */ +const char * +iobuf_get_fname( IOBUF a ) +{ + struct stat st; + + for( ; a; a = a->chain ) + if( !a->chain && a->filter == file_filter ) { + file_filter_ctx_t *b = a->filter_ov; + return b->fname; + } + + return NULL; +} + +/**************** * Start the block write mode, see rfc1991.new for details. * A value of 0 for N stops this mode (flushes and writes * the end marker) diff --git a/util/logger.c b/util/logger.c index 803420cd8..9c1b86b23 100644 --- a/util/logger.c +++ b/util/logger.c @@ -25,6 +25,19 @@ #include "util.h" +static char pidstring[15]; + + +void +set_log_pid( int pid ) +{ + if( pid ) + sprintf(pidstring,"[%u]", (unsigned)pid ); + else + *pidstring = 0; +} + + /**************** * General interface for printing a line * level 0 := print to /dev/null @@ -62,7 +75,7 @@ log_info( const char *fmt, ... ) { va_list arg_ptr ; - fprintf(stderr, "info: " ) ; + fprintf(stderr, "info%s: ", pidstring ) ; va_start( arg_ptr, fmt ) ; vfprintf(stderr,fmt,arg_ptr) ; va_end(arg_ptr); @@ -73,7 +86,7 @@ log_error( const char *fmt, ... ) { va_list arg_ptr ; - fprintf(stderr, "error: " ) ; + fprintf(stderr, "error%s: ", pidstring ) ; va_start( arg_ptr, fmt ) ; vfprintf(stderr,fmt,arg_ptr) ; va_end(arg_ptr); @@ -84,7 +97,7 @@ log_fatal( const char *fmt, ... ) { va_list arg_ptr ; - fprintf(stderr, "Fatal: " ) ; + fprintf(stderr, "Fatal%s: ", pidstring ) ; va_start( arg_ptr, fmt ) ; vfprintf(stderr,fmt,arg_ptr) ; va_end(arg_ptr); @@ -96,7 +109,7 @@ log_bug( const char *fmt, ... ) { va_list arg_ptr ; - fprintf(stderr, "\nInternal Error: " ) ; + fprintf(stderr, "\nInternal Error%s: ", pidstring ) ; va_start( arg_ptr, fmt ) ; vfprintf(stderr,fmt,arg_ptr) ; va_end(arg_ptr); @@ -109,7 +122,7 @@ log_debug( const char *fmt, ... ) { va_list arg_ptr ; - fprintf(stderr, "DBG: " ) ; + fprintf(stderr, "DBG%s: ", pidstring ) ; va_start( arg_ptr, fmt ) ; vfprintf(stderr,fmt,arg_ptr) ; va_end(arg_ptr); @@ -122,7 +135,7 @@ log_hexdump( const char *text, char *buf, size_t len ) { int i; - fprintf(stderr, "DBG: %s", text ); + fprintf(stderr, "DBG%s: %s", pidstring, text ); for(i=0; i < len; i++ ) fprintf(stderr, " %02X", ((byte*)buf)[i] ); fputc('\n', stderr); @@ -132,7 +145,7 @@ log_hexdump( const char *text, char *buf, size_t len ) void log_mpidump( const char *text, MPI a ) { - fprintf(stderr, "DBG: %s", text ); + fprintf(stderr, "DBG%s: %s", pidstring, text ); mpi_print(stderr, a, 1 ); fputc('\n', stderr); } |