volta.c
changeset 1 823d42546cea
parent 0 eac7211fe522
equal deleted inserted replaced
0:eac7211fe522 1:823d42546cea
     1 /* vim: set noet nosta sw=4 ts=4 ft=c : */
     1 /* vim: set noet nosta sw=4 ts=4 ft=c : */
     2 /* Squid docs:
     2 /* Squid docs:
     3 ---------------------------------------------------------------------------
     3    ---------------------------------------------------------------------------
     4 TAG: url_rewrite_program
     4 TAG: url_rewrite_program
     5 Specify the location of the executable for the URL rewriter.
     5 Specify the location of the executable for the URL rewriter.
     6 Since they can perform almost any function there isn't one included.
     6 Since they can perform almost any function there isn't one included.
     7 
     7 
     8 For each requested URL rewriter will receive on line with the format
     8 For each requested URL rewriter will receive on line with the format
    28  * TODO
    28  * TODO
    29  *
    29  *
    30  * flush stdout on writes
    30  * flush stdout on writes
    31  * empty struct not necessary?
    31  * empty struct not necessary?
    32  * inet_pton( AF_INET, *char src, dest )
    32  * inet_pton( AF_INET, *char src, dest )
       
    33  * an option to run the DB out of memory?
       
    34  * PRAGMA user_version = 1;
    33  *
    35  *
    34  */
    36  */
    35 
    37 
    36 #include "volta.h"
    38 #include "volta.h"
       
    39 #include <time.h>
       
    40 unsigned short int debugmode;
       
    41 
    37 
    42 
    38 int
    43 int
    39 main( int argc, char *argv[] ) {
    44 main( int argc, char *argv[] ) {
    40 
    45 
    41 	int opt;
    46 	/* opt action flags */
       
    47 	struct {
       
    48 		unsigned short int init;
       
    49 	} actions = {0};
    42 
    50 
    43 	/* storage for line received from squid */
    51 #ifdef DEBUG
    44 	char line[ LINE_BUFSIZE ];
    52 	/* debugmode set at compile time, default to display everything */
       
    53 	debugmode = 99;
       
    54 #else
       
    55 	debugmode = 0;
       
    56 #endif
    45 
    57 
    46 	while ( (opt = getopt( argc, argv, "a:hv" )) != -1 ) {
    58 	/* get_opt vars */
       
    59 	int opt = 0;
       
    60 	opterr  = 0;
       
    61 
       
    62 	/* parse options */
       
    63 	while ( (opt = getopt( argc, argv, "a:d:hv" )) != -1 ) {
    47 		switch ( opt ) {
    64 		switch ( opt ) {
       
    65 
       
    66 			/* action */
    48 			case 'a':
    67 			case 'a':
    49 				printf( "a -> '%s' (no-op at the moment)\n", optarg );
    68 				if ( strcmp( optarg, "init" ) == 0 ) actions.init++;
    50 				break;
    69 				break;
       
    70 
       
    71 			/* debug */
       
    72 			case 'd':
       
    73 				if ( optarg[0] == '-' ) {
       
    74 					argc++; argv -= 1;
       
    75 					debugmode = 1;
       
    76 				}
       
    77 				sscanf( optarg, "%hu", &debugmode );
       
    78 				break;
       
    79 
       
    80 			/* help */
    51 			case 'h':
    81 			case 'h':
    52 				usage( argv[0] );
    82 				usage( argv[0] );
    53 				return( 0 );
    83 				return( 0 );
       
    84 
       
    85 			/* version */
    54 			case 'v':
    86 			case 'v':
    55 				printf( "%s version %s\n", PROG, VERSION );
    87 				printf( "%s %s\n", PROG, VERSION );
    56 				return( 0 );
    88 				return( 0 );
       
    89 
       
    90 			/* unknown option or option argument missing */
    57 			case '?':
    91 			case '?':
       
    92 				switch( optopt ) {
       
    93 					case 'd': /* no debug argument, default to level 1 */
       
    94 						debugmode = 1;
       
    95 						break;
       
    96 					default:
       
    97 						usage( argv[0] );
       
    98 						return( 1 );
       
    99 				}
       
   100 
    58 			default:
   101 			default:
    59 				break;
   102 				break;
    60 		}
   103 		}
    61 	}
   104 	}
    62 	argc -= optind;
   105 	argc -= optind;
    63 	argv += optind;
   106 	argv += optind;
    64 
   107 
    65 	/* start stdin line loop */
   108 	/* perform actions */
       
   109 	if ( actions.init ) {
       
   110 		debug( 1, LOC, "init! init! init!\n" );
       
   111 		return( 0 );
       
   112 	}
       
   113 
       
   114 	/* start stdin parsing loop */
       
   115 	char line[ LINE_BUFSIZE ];
       
   116 	debug( 1, LOC, "Waiting for input...\n" );
    66 	while( fgets( line, LINE_BUFSIZE, stdin ) != NULL ) parse( line );
   117 	while( fgets( line, LINE_BUFSIZE, stdin ) != NULL ) parse( line );
    67 
   118 
    68 	/* stdin closed */
   119 	/* stdin closed */
    69     debug( "End of stream, shutting down.\n" );
   120 	debug( 1, LOC, "End of stream, shutting down.\n" );
    70     return( 0 );
   121 	return( 0 );
    71 }
   122 }
       
   123 
    72 
   124 
    73 /*
   125 /*
    74  * Basic usage
   126  * Basic usage
    75  */
   127  */
    76 void
   128 void
    77 usage( char *prg )
   129 usage( char *prg )
    78 {
   130 {
    79 	printf( "%s [-vh] [-a <init>]\n", prg );
   131 	printf( "%s [-vh] [-d <level>] [-a <init>]\n", prg );
    80 	printf( "    -v Display version\n" );
   132 	printf( "    -v Display version\n" );
       
   133 	printf( "    -d <level> Show debug information on stderr\n" );
    81 	printf( "    -h Usage (you're lookin' at it)\n" );
   134 	printf( "    -h Usage (you're lookin' at it)\n" );
    82 	printf( "    -a Perform an action:\n" );
   135 	printf( "    -a Perform an action, being one of:\n" );
    83 	printf( "         init: Initialize a new database\n" );
   136 	printf( "         init: Initialize a new, empty database\n" );
    84 	printf( "\n" );
   137 	printf( "\n" );
    85 	return;
   138 	return;
    86 }
   139 }
    87 
   140 
    88 
   141 
    89 /*
   142 /*
    90  * Debug function, only output to stderr if -DDEBUG set
   143  * Debug function, only output to stderr if the debug level is
       
   144  * equal or greated to the output level.
       
   145  *
       
   146  * 	level: The minimum debug level that must be set for the 
       
   147  * 	       line to be logged.
       
   148  * 	file:  The current code file that is emitting the log
       
   149  * 	line:  The line number of the code file that is emitting the log
       
   150  * 	... :  any printf style strings and formats that constitute the log message
    91  */
   151  */
    92 void
   152 void
    93 debug( const char *fmt, ... )
   153 debug( int level, char *file, int line, const char *fmt, ... )
    94 {
   154 {
    95     va_list args;
   155 	if ( debugmode < level ) return;
       
   156 
       
   157 	char timestamp[20];
       
   158 	time_t t = time( NULL );
       
   159 	struct tm *now = localtime( &t );
       
   160 	strftime( timestamp, 20, "%F %T", now );
       
   161 
       
   162 	va_list args;
    96 	va_start( args, fmt );
   163 	va_start( args, fmt );
    97 #ifdef DEBUG
   164 	fprintf( stderr, "%s [%s] #%d (%s:%04d): ", PROG, timestamp, getpid(), file, line );
    98 	fprintf( stderr, "%s %d: ", PROG, getpid() );
       
    99 	vfprintf( stderr, fmt, args );
   165 	vfprintf( stderr, fmt, args );
   100 #endif
       
   101 	va_end( args );
   166 	va_end( args );
       
   167 
       
   168 	return;
   102 }
   169 }
   103 
   170