main.c
changeset 14 51eb85ae4de4
parent 13 23a242d7b7fa
child 17 bd746609ba46
equal deleted inserted replaced
13:23a242d7b7fa 14:51eb85ae4de4
    39  */
    39  */
    40 int
    40 int
    41 main( int argc, char *argv[] )
    41 main( int argc, char *argv[] )
    42 {
    42 {
    43 #ifdef DEBUG
    43 #ifdef DEBUG
    44 	/* debugmode set at compile time,
    44 	/* debugmode set at compile time?  default to crankin' up the level */
    45 	 * default to display everything */
       
    46 	v.debugmode = 99;
    45 	v.debugmode = 99;
    47 #else
    46 #else
    48 	v.debugmode = 0;
    47 	v.debugmode = 0;
    49 #endif
    48 #endif
    50 
    49 
    51 	(void)signal( SIGINT, shutdown_handler );
    50 	/* defaults and global initalizations */
    52 
       
    53 	/* default database file name */
       
    54 	v.db = NULL;
       
    55 	strcpy( v.dbname, "volta.db" );
    51 	strcpy( v.dbname, "volta.db" );
       
    52 	char create_db[30]   = "";
       
    53 	v.timer.db_lastcheck = 0;
       
    54 	v.timer.start        = time( NULL );
       
    55 	v.timer.lines        = 0;
    56 
    56 
    57 	/* get_opt vars */
    57 	/* get_opt vars */
    58 	int opt = 0;
    58 	int opt = 0;
    59 	extern char *optarg;
    59 	extern char *optarg;
    60 	extern int opterr, optind, optopt;
    60 	extern int opterr, optind, optopt;
    61 
    61 
    62 	/* parse options */
    62 	/* parse options */
    63 	opterr = 0;
    63 	opterr = 0;
    64 	while ( (opt = getopt( argc, argv, "d:f:hv" )) != -1 ) {
    64 	while ( (opt = getopt( argc, argv, "c:d:f:hv" )) != -1 ) {
    65 		switch ( opt ) {
    65 		switch ( opt ) {
    66 
    66 
    67 			/* database filename */
    67 			/* create a new cdb file from ascii input */
    68 			case 'f':
    68 			case 'c':
    69 				strncpy( v.dbname, optarg, sizeof(v.dbname) );
    69 				strncpy( create_db, optarg, sizeof(create_db) );
    70 				break;
    70 				break;
    71 
    71 
    72 			/* debug */
    72 			/* debug */
    73 			case 'd':
    73 			case 'd':
    74 				if ( optarg[2] == '-' ) {
    74 				if ( optarg[2] == '-' ) {
    75 					argc++; argv -= 1;
    75 					argc++; argv -= 1;
    76 					v.debugmode = 1;
    76 					v.debugmode = 1;
    77 				}
    77 				}
    78 				sscanf( optarg, "%hu", &v.debugmode );
    78 				sscanf( optarg, "%hu", &v.debugmode );
       
    79 				break;
       
    80 
       
    81 			/* database filename */
       
    82 			case 'f':
       
    83 				strncpy( v.dbname, optarg, sizeof(v.dbname) );
    79 				break;
    84 				break;
    80 
    85 
    81 			/* help */
    86 			/* help */
    82 			case 'h':
    87 			case 'h':
    83 				usage( argv[0] );
    88 				usage( argv[0] );
   104 		}
   109 		}
   105 	}
   110 	}
   106 	argc -= optind;
   111 	argc -= optind;
   107 	argv += optind;
   112 	argv += optind;
   108 
   113 
   109 	/* set timer vars for lines/sec counter */
   114 	/* create a new database if requested */
   110 	if ( v.debugmode > 2 ) {
   115 	if ( (int)strlen(create_db) > 0 ) {
   111 		v.timer.start = time( NULL );
   116 		return( db_create_new( create_db ) );
   112 		v.timer.lines = 0;
       
   113 	}
   117 	}
   114 
   118 
   115 	/* get the initial database handle or bomb immediately. */
       
   116 	if ( db_attach() != SQLITE_OK ) exit( 1 );
       
   117 
       
   118 	/* enter stdin parsing loop */
   119 	/* enter stdin parsing loop */
       
   120 	(void)signal( SIGINT, shutdown_handler );
   119 	unsigned char exitval = accept_loop();
   121 	unsigned char exitval = accept_loop();
   120 	shutdown_actions();
   122 	shutdown_actions();
   121 	return( exitval );
   123 	return( exitval );
   122 }
   124 }
   123 
   125 
   127  *
   129  *
   128  */
   130  */
   129 void
   131 void
   130 shutdown_actions( void )
   132 shutdown_actions( void )
   131 {
   133 {
   132 	sqlite3_finalize( v.db_stmt.match_request );
   134 	close( v.db_fd );
   133 	sqlite3_finalize( v.db_stmt.get_rewrite_rule );
       
   134 	sqlite3_close( v.db );
       
   135 	report_speed();
   135 	report_speed();
   136 }
   136 }
   137 
   137 
   138 
   138 
   139 /*
   139 /*