diff options
author | Marcus Brinkmann <[email protected]> | 2002-01-22 16:29:12 +0000 |
---|---|---|
committer | Marcus Brinkmann <[email protected]> | 2002-01-22 16:29:12 +0000 |
commit | b863cea7d70e32d0b0bcd84f87f5350818722242 (patch) | |
tree | 92bb4b2448f5b2eb5651a730b47e3a71f8f438ce /jnlib | |
parent | Remove obsolete item. (diff) | |
download | gpgme-b863cea7d70e32d0b0bcd84f87f5350818722242.tar.gz gpgme-b863cea7d70e32d0b0bcd84f87f5350818722242.zip |
Update to current version in newpg module.
Diffstat (limited to 'jnlib')
-rw-r--r-- | jnlib/ChangeLog | 16 | ||||
-rw-r--r-- | jnlib/Makefile.am | 17 | ||||
-rw-r--r-- | jnlib/argparse.c | 2 | ||||
-rw-r--r-- | jnlib/libjnlib-config.h | 16 | ||||
-rw-r--r-- | jnlib/logging.c | 163 | ||||
-rw-r--r-- | jnlib/logging.h | 27 | ||||
-rw-r--r-- | jnlib/strlist.c | 133 | ||||
-rw-r--r-- | jnlib/strlist.h | 43 |
8 files changed, 346 insertions, 71 deletions
diff --git a/jnlib/ChangeLog b/jnlib/ChangeLog index 1d750bfa..b5f723cf 100644 --- a/jnlib/ChangeLog +++ b/jnlib/ChangeLog @@ -5,6 +5,22 @@ unconditionally. Reported by Jose Carlos Garcia Sogo <[email protected]>. +2002-01-19 Werner Koch <[email protected]> + + * logging.c (log_get_stream): New. + +2001-12-05 Werner Koch <[email protected]> + + * logging.c (log_set_prefix): New. + (do_logv): Include prefix and pid only if enabled. Print time only + when explicitly enabled. + (log_logv): New. + * logging.h: Include log_logv() only when requested. + +2001-11-06 Werner Koch <[email protected]> + + * strlist.c, strlist.h: New. Taken from pgnupg/util/strgutil.c + 2001-08-30 Werner Koch <[email protected]> * logging.c (log_printf): Don't pass NULL instead of arg_ptr. diff --git a/jnlib/Makefile.am b/jnlib/Makefile.am index 1049f3c2..515a49a6 100644 --- a/jnlib/Makefile.am +++ b/jnlib/Makefile.am @@ -26,11 +26,14 @@ noinst_LIBRARIES = libjnlib.a #libjnlib_a_LDFLAGS = -libjnlib_a_SOURCES = libjnlib-config.h \ - xmalloc.c xmalloc.h \ - stringhelp.c stringhelp.h \ - argparse.c argparse.h \ - logging.c logging.h \ - types.h mischelp.h - +libjnlib_a_SOURCES = \ + libjnlib-config.h \ + stringhelp.c stringhelp.h \ + strlist.c strlist.h \ + argparse.c argparse.h \ + logging.c logging.h \ + dotlock.c dotlock.h \ + types.h mischelp.h + +# xmalloc.c xmalloc.h diff --git a/jnlib/argparse.c b/jnlib/argparse.c index ba635585..0e353e4d 100644 --- a/jnlib/argparse.c +++ b/jnlib/argparse.c @@ -900,7 +900,7 @@ strusage( int level ) switch( level ) { case 11: p = "foo"; break; case 13: p = "0.0"; break; - case 14: p = "Copyright (C) 2000 Free Software Foundation, Inc."; break; + case 14: p = "Copyright (C) 2001 Free Software Foundation, Inc."; break; case 15: p = "This program comes with ABSOLUTELY NO WARRANTY.\n" "This is free software, and you are welcome to redistribute it\n" diff --git a/jnlib/libjnlib-config.h b/jnlib/libjnlib-config.h index de8e6a47..ec31d356 100644 --- a/jnlib/libjnlib-config.h +++ b/jnlib/libjnlib-config.h @@ -26,11 +26,9 @@ #ifndef LIBJNLIB_CONFIG_H #define LIBJNLIB_CONFIG_H -#include "xmalloc.h" +#include <gcrypt.h> /* gcry_malloc & Cie. */ #include "logging.h" - - #ifdef USE_SIMPLE_GETTEXT int set_gettext_file( const char *filename ); const char *gettext( const char *msgid ); @@ -58,11 +56,11 @@ #endif /* !USE_SIMPLE_GETTEXT */ -#define jnlib_xmalloc(a) xmalloc( (a) ) -#define jnlib_xcalloc(a,b) xcalloc( (a), (b) ) -#define jnlib_xrealloc(a,n) xrealloc( (a), (n) ) -#define jnlib_xstrdup(a) xstrdup( (a) ) -#define jnlib_free(a) free( (a) ) +#define jnlib_xmalloc(a) gcry_xmalloc( (a) ) +#define jnlib_xcalloc(a,b) gcry_xcalloc( (a), (b) ) +#define jnlib_xrealloc(a,n) gcry_xrealloc( (a), (n) ) +#define jnlib_xstrdup(a) gcry_xstrdup( (a) ) +#define jnlib_free(a) gcry_free( (a) ) #define jnlib_log_debug log_debug #define jnlib_log_info log_info @@ -73,3 +71,5 @@ #endif /*LIBJNUTIL_CONFIG_H*/ + + diff --git a/jnlib/logging.c b/jnlib/logging.c index 69b94b1b..2e0d53ae 100644 --- a/jnlib/logging.c +++ b/jnlib/logging.c @@ -31,26 +31,23 @@ #include <stdarg.h> #include <errno.h> #include <time.h> +#include <sys/types.h> +#include <unistd.h> #ifdef __MINGW32__ #include <io.h> #endif +#define JNLIB_NEED_LOG_LOGV 1 #include "libjnlib-config.h" #include "logging.h" -enum my_log_levels { - MY_LOG_BEGIN, /* only print the timestamp if configured */ - MY_LOG_CONT, - MY_LOG_INFO, - MY_LOG_WARN, - MY_LOG_ERROR, - MY_LOG_FATAL, - MY_LOG_BUG, - MY_LOG_DEBUG -}; static FILE *logstream; -static int use_time; +static char prefix_buffer[80]; +static int with_time; +static int with_prefix; +static int with_pid; + static int missing_lf; static int errorcount; @@ -95,61 +92,93 @@ log_set_file( const char *name ) if( logstream && logstream != stderr ) fclose( logstream ); logstream = fp; - use_time = fp != stderr; missing_lf = 0; } +void +log_set_prefix (const char *text, unsigned int flags) +{ + if (text) + { + strncpy (prefix_buffer, text, sizeof (prefix_buffer)-1); + prefix_buffer[sizeof (prefix_buffer)-1] = 0; + } + + with_prefix = (flags & 1); + with_time = (flags & 2); + with_pid = (flags & 4); +} + int log_get_fd() { return fileno(logstream?logstream:stderr); } -static void -do_logv( int level, const char *fmt, va_list arg_ptr ) +FILE * +log_get_stream () { - if( !logstream ) - logstream = stderr; - - if( missing_lf && level != MY_LOG_CONT ) - putc('\n', logstream ); - missing_lf = 0; + return logstream?logstream:stderr; +} - if( use_time && level != MY_LOG_CONT ) { - /* Note this does not work for multiple line logging as we would - * need to print to a buffer first */ - struct tm *tp; - time_t atime = time(NULL); - tp = localtime( &atime ); - fprintf( logstream, "%04d-%02d-%02d %02d:%02d:%02d ", - 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, - tp->tm_hour, tp->tm_min, tp->tm_sec ); +static void +do_logv( int level, const char *fmt, va_list arg_ptr ) +{ + if (!logstream) + logstream = stderr; + + if (missing_lf && level != JNLIB_LOG_CONT) + putc('\n', logstream ); + missing_lf = 0; + + if (level != JNLIB_LOG_CONT) + { /* Note this does not work for multiple line logging as we would + * need to print to a buffer first */ + if (with_time) + { + struct tm *tp; + time_t atime = time (NULL); + + tp = localtime (&atime); + fprintf (logstream, "%04d-%02d-%02d %02d:%02d:%02d ", + 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, + tp->tm_hour, tp->tm_min, tp->tm_sec ); + } + if (with_prefix) + fputs (prefix_buffer, logstream); + if (with_pid) + fprintf (logstream, "[%u]", (unsigned int)getpid ()); + if (!with_time) + putc (':', logstream); + putc (' ', logstream); } - switch ( level ) { - case MY_LOG_BEGIN: break; - case MY_LOG_CONT: break; - case MY_LOG_INFO: break; - case MY_LOG_WARN: break; - case MY_LOG_ERROR: break; - case MY_LOG_FATAL: fputs("Fatal: ",logstream ); break; - case MY_LOG_BUG: fputs("Ohhhh jeeee: ", logstream); break; - case MY_LOG_DEBUG: fputs("DBG: ", logstream ); break; - default: fprintf(logstream,"[Unknown log level %d]: ", level ); break; + switch (level) + { + case JNLIB_LOG_BEGIN: break; + case JNLIB_LOG_CONT: break; + case JNLIB_LOG_INFO: break; + case JNLIB_LOG_WARN: break; + case JNLIB_LOG_ERROR: break; + case JNLIB_LOG_FATAL: fputs("Fatal: ",logstream ); break; + case JNLIB_LOG_BUG: fputs("Ohhhh jeeee: ", logstream); break; + case JNLIB_LOG_DEBUG: fputs("DBG: ", logstream ); break; + default: fprintf(logstream,"[Unknown log level %d]: ", level ); break; } - if( fmt ) { - vfprintf(logstream,fmt,arg_ptr) ; - if( *fmt && fmt[strlen(fmt)-1] != '\n' ) - missing_lf = 1; + if (fmt) + { + vfprintf(logstream,fmt,arg_ptr) ; + if (*fmt && fmt[strlen(fmt)-1] != '\n') + missing_lf = 1; } - if( level == MY_LOG_FATAL ) - exit(2); - if( level == MY_LOG_BUG ) - abort(); + if (level == JNLIB_LOG_FATAL) + exit(2); + if (level == JNLIB_LOG_BUG) + abort(); } static void @@ -163,6 +192,11 @@ do_log( int level, const char *fmt, ... ) } +void +log_logv (int level, const char *fmt, va_list arg_ptr) +{ + do_logv (level, fmt, arg_ptr); +} void log_info( const char *fmt, ... ) @@ -170,7 +204,7 @@ log_info( const char *fmt, ... ) va_list arg_ptr ; va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_INFO, fmt, arg_ptr ); + do_logv( JNLIB_LOG_INFO, fmt, arg_ptr ); va_end(arg_ptr); } @@ -180,7 +214,7 @@ log_error( const char *fmt, ... ) va_list arg_ptr ; va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_ERROR, fmt, arg_ptr ); + do_logv( JNLIB_LOG_ERROR, fmt, arg_ptr ); va_end(arg_ptr); /* protect against counter overflow */ if( errorcount < 30000 ) @@ -194,7 +228,7 @@ log_fatal( const char *fmt, ... ) va_list arg_ptr ; va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_FATAL, fmt, arg_ptr ); + do_logv( JNLIB_LOG_FATAL, fmt, arg_ptr ); va_end(arg_ptr); abort(); /* never called, bugs it makes the compiler happy */ } @@ -205,7 +239,7 @@ log_bug( const char *fmt, ... ) va_list arg_ptr ; va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_BUG, fmt, arg_ptr ); + do_logv( JNLIB_LOG_BUG, fmt, arg_ptr ); va_end(arg_ptr); abort(); /* never called, but it makes the compiler happy */ } @@ -216,7 +250,7 @@ log_debug( const char *fmt, ... ) va_list arg_ptr ; va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_DEBUG, fmt, arg_ptr ); + do_logv( JNLIB_LOG_DEBUG, fmt, arg_ptr ); va_end(arg_ptr); } @@ -227,16 +261,35 @@ log_printf (const char *fmt, ...) va_list arg_ptr; va_start (arg_ptr, fmt); - do_logv (fmt ? MY_LOG_CONT : MY_LOG_BEGIN, fmt, arg_ptr); + do_logv (fmt ? JNLIB_LOG_CONT : JNLIB_LOG_BEGIN, fmt, arg_ptr); va_end (arg_ptr); } +/* Print a hexdump of BUFFER. With TEXT of NULL print just the raw + dump, with TEXT just an empty string, print a trailing linefeed, + otherwise print an entire debug line. */ +void +log_printhex (const char *text, const void *buffer, size_t length) +{ + if (text && *text) + log_debug ("%s ", text); + if (length) + { + const unsigned char *p = buffer; + log_printf ("%02X", *p); + for (length--, p++; length--; p++) + log_printf (" %02X", *p); + } + if (text) + log_printf ("\n"); +} + #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) void bug_at( const char *file, int line, const char *func ) { - do_log( MY_LOG_BUG, + do_log( JNLIB_LOG_BUG, ("... this is a bug (%s:%d:%s)\n"), file, line, func ); abort(); /* never called, but it makes the compiler happy */ } @@ -244,7 +297,7 @@ bug_at( const char *file, int line, const char *func ) void bug_at( const char *file, int line ) { - do_log( MY_LOG_BUG, + do_log( JNLIB_LOG_BUG, _("you found a bug ... (%s:%d)\n"), file, line); abort(); /* never called, but it makes the compiler happy */ } diff --git a/jnlib/logging.h b/jnlib/logging.h index 83158f0b..7b7b8c8a 100644 --- a/jnlib/logging.h +++ b/jnlib/logging.h @@ -24,9 +24,12 @@ #include <stdio.h> #include "mischelp.h" + int log_get_errorcount (int clear); void log_set_file( const char *name ); +void log_set_prefix (const char *text, unsigned int flags); int log_get_fd(void); +FILE *log_get_stream (void); #ifdef JNLIB_GCC_M_FUNCTION void bug_at( const char *file, int line, const char *func ) JNLIB_GCC_A_NR; @@ -36,12 +39,36 @@ int log_get_fd(void); # define BUG() bug_at( __FILE__ , __LINE__ ) #endif +/* To avoid mandatory inclusion of stdarg and other stuff, do it only + if explicitly requested to do so. */ +#ifdef JNLIB_NEED_LOG_LOGV +#include <stdarg.h> +enum jnlib_log_levels { + JNLIB_LOG_BEGIN, + JNLIB_LOG_CONT, + JNLIB_LOG_INFO, + JNLIB_LOG_WARN, + JNLIB_LOG_ERROR, + JNLIB_LOG_FATAL, + JNLIB_LOG_BUG, + JNLIB_LOG_DEBUG +}; +void log_logv (int level, const char *fmt, va_list arg_ptr); +#endif /*JNLIB_NEED_LOG_LOGV*/ + + void log_bug( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); void log_fatal( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2); void log_error( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void log_info( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void log_debug( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); void log_printf( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2); +void log_printhex (const char *text, const void *buffer, size_t length); #endif /*LIBJNLIB_LOGGING_H*/ + + + + + diff --git a/jnlib/strlist.c b/jnlib/strlist.c new file mode 100644 index 00000000..7cbaf5e0 --- /dev/null +++ b/jnlib/strlist.c @@ -0,0 +1,133 @@ +/* strlist.c - string helpers + * Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GnuPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include <config.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <ctype.h> + +#include "libjnlib-config.h" +#include "strlist.h" + + +void +free_strlist( STRLIST sl ) +{ + STRLIST sl2; + + for(; sl; sl = sl2 ) { + sl2 = sl->next; + jnlib_free(sl); + } +} + + +STRLIST +add_to_strlist( STRLIST *list, const char *string ) +{ + STRLIST sl; + + sl = jnlib_xmalloc( sizeof *sl + strlen(string)); + sl->flags = 0; + strcpy(sl->d, string); + sl->next = *list; + *list = sl; + return sl; +} + +#if 0 +/**************** + * same as add_to_strlist() but if is_utf8 is *not* set a conversion + * to UTF8 is done + */ +STRLIST +add_to_strlist2( STRLIST *list, const char *string, int is_utf8 ) +{ + STRLIST sl; + + if( is_utf8 ) + sl = add_to_strlist( list, string ); + else { + char *p = native_to_utf8( string ); + sl = add_to_strlist( list, p ); + m_free( p ); + } + return sl; +} +#endif + +STRLIST +append_to_strlist( STRLIST *list, const char *string ) +{ + STRLIST r, sl; + + sl = jnlib_xmalloc( sizeof *sl + strlen(string)); + sl->flags = 0; + strcpy(sl->d, string); + sl->next = NULL; + if( !*list ) + *list = sl; + else { + for( r = *list; r->next; r = r->next ) + ; + r->next = sl; + } + return sl; +} + +#if 0 +STRLIST +append_to_strlist2( STRLIST *list, const char *string, int is_utf8 ) +{ + STRLIST sl; + + if( is_utf8 ) + sl = append_to_strlist( list, string ); + else { + char *p = native_to_utf8( string ); + sl = append_to_strlist( list, p ); + m_free( p ); + } + return sl; +} +#endif + +STRLIST +strlist_prev( STRLIST head, STRLIST node ) +{ + STRLIST n; + + for(n=NULL; head && head != node; head = head->next ) + n = head; + return n; +} + +STRLIST +strlist_last( STRLIST node ) +{ + if( node ) + for( ; node->next ; node = node->next ) + ; + return node; +} + + + diff --git a/jnlib/strlist.h b/jnlib/strlist.h new file mode 100644 index 00000000..53c0bc75 --- /dev/null +++ b/jnlib/strlist.h @@ -0,0 +1,43 @@ +/* strlist.h + * Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GnuPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#ifndef LIBJNLIB_STRLIST_H +#define LIBJNLIB_STRLIST_H + +struct string_list { + struct string_list *next; + unsigned int flags; + char d[1]; +}; +typedef struct string_list *STRLIST; + + +void free_strlist( STRLIST sl ); +STRLIST add_to_strlist( STRLIST *list, const char *string ); +STRLIST add_to_strlist2( STRLIST *list, const char *string, int is_utf8 ); +STRLIST append_to_strlist( STRLIST *list, const char *string ); +STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 ); +STRLIST strlist_prev( STRLIST head, STRLIST node ); +STRLIST strlist_last( STRLIST node ); + +#define FREE_STRLIST(a) do { free_strlist((a)); (a) = NULL ; } while(0) + + +#endif /*LIBJNLIB_STRLIST_H*/ |