main.c
changeset 4 5701b7859a31
parent 2 8c88756f81b0
child 7 e4f1a551d45c
equal deleted inserted replaced
3:97f767832c52 4:5701b7859a31
    29 */
    29 */
    30 
    30 
    31 /*
    31 /*
    32  * TODO
    32  * TODO
    33  *
    33  *
    34  * empty struct not necessary?
       
    35  * inet_pton( AF_INET, *char src, dest )
    34  * inet_pton( AF_INET, *char src, dest )
    36  * an option to run the DB out of memory?
    35  * an option to run the DB out of memory?
    37  * PRAGMA user_version = 1;
       
    38  *
    36  *
    39  */
    37  */
    40 
    38 
    41 #include "volta.h"
    39 #include "volta.h"
    42 unsigned short int debugmode;
    40 #include "db.h"
    43 
    41 
       
    42 struct v_globals v;
    44 
    43 
    45 /*
    44 /*
    46  * Parse command line options, perform actions, and enter accept loop.
    45  * Parse command line options, perform actions, and enter accept loop.
    47  *
    46  *
    48  */
    47  */
    49 int
    48 int
    50 main( int argc, char *argv[] )
    49 main( int argc, char *argv[] )
    51 {
    50 {
    52 	/* opt action flags */
       
    53 	struct {
       
    54 		unsigned char init;
       
    55 		unsigned char dump;
       
    56 	} actions = {0};
       
    57 
       
    58 #ifdef DEBUG
    51 #ifdef DEBUG
    59 	/* debugmode set at compile time,
    52 	/* debugmode set at compile time,
    60 	 * default to display everything */
    53 	 * default to display everything */
    61 	debugmode = 99;
    54 	v.debugmode = 99;
    62 #else
    55 #else
    63 	debugmode = 0;
    56 	v.debugmode = 0;
    64 #endif
    57 #endif
       
    58 
       
    59 	/* default database file name */
       
    60 	v.db = NULL;
       
    61 	strcpy( v.dbname, "volta.db" );
    65 
    62 
    66 	/* get_opt vars */
    63 	/* get_opt vars */
    67 	int opt = 0;
    64 	int opt = 0;
    68 	opterr  = 0;
    65 	opterr  = 0;
    69 
    66 
    70 	/* parse options */
    67 	/* parse options */
    71 	while ( (opt = getopt( argc, argv, "a:d:hv" )) != -1 ) {
    68 	while ( (opt = getopt( argc, argv, "d:f:hv" )) != -1 ) {
    72 		switch ( opt ) {
    69 		switch ( opt ) {
    73 
    70 
    74 			/* action */
    71 			/* database filename */
    75 			case 'a':
    72 			case 'f':
    76 				if ( strcmp( optarg, "init" ) == 0 ) actions.init++;
    73 				strncpy( v.dbname, optarg, sizeof(v.dbname) );
    77 				if ( strcmp( optarg, "dump" ) == 0 ) actions.dump++;
       
    78 				break;
    74 				break;
    79 
    75 
    80 			/* debug */
    76 			/* debug */
    81 			case 'd':
    77 			case 'd':
    82 				if ( optarg[2] == '-' ) {
    78 				if ( optarg[2] == '-' ) {
    83 					argc++; argv -= 1;
    79 					argc++; argv -= 1;
    84 					debugmode = 1;
    80 					v.debugmode = 1;
    85 				}
    81 				}
    86 				sscanf( optarg, "%hu", &debugmode );
    82 				sscanf( optarg, "%hu", &v.debugmode );
    87 				break;
    83 				break;
    88 
    84 
    89 			/* help */
    85 			/* help */
    90 			case 'h':
    86 			case 'h':
    91 				usage( argv[0] );
    87 				usage( argv[0] );
    98 
    94 
    99 			/* unknown option or option argument missing */
    95 			/* unknown option or option argument missing */
   100 			case '?':
    96 			case '?':
   101 				switch( optopt ) {
    97 				switch( optopt ) {
   102 					case 'd': /* no debug argument, default to level 1 */
    98 					case 'd': /* no debug argument, default to level 1 */
   103 						debugmode = 1;
    99 						v.debugmode = 1;
   104 						break;
   100 						break;
   105 					default:
   101 					default:
   106 						usage( argv[0] );
   102 						usage( argv[0] );
   107 						return( 1 );
   103 						return( 1 );
   108 				}
   104 				}
   112 		}
   108 		}
   113 	}
   109 	}
   114 	argc -= optind;
   110 	argc -= optind;
   115 	argv += optind;
   111 	argv += optind;
   116 
   112 
   117 	/* perform any requested actions */
   113 	/* get the initial database handle or bomb immediately. */
   118 	if ( actions.init ) {
   114 	if ( db_attach() != SQLITE_OK ) exit( 1 );
   119 		db_initialize();
       
   120 		return( 0 );
       
   121 	}
       
   122 	if ( actions.dump ) {
       
   123 		debug( 1, LOC, "dump.\n" );
       
   124 		return( 0 );
       
   125 	}
       
   126 
   115 
   127 	/* enter stdin parsing loop */
   116 	/* enter stdin parsing loop */
   128 	return accept_loop();
   117 	return accept_loop();
   129 }
   118 }
   130 
   119