main.c
changeset 2 8c88756f81b0
child 4 5701b7859a31
equal deleted inserted replaced
1:823d42546cea 2:8c88756f81b0
       
     1 /* vim: set noet nosta sw=4 ts=4 ft=c : */
       
     2 /*
       
     3 Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu>
       
     4 All rights reserved.
       
     5 Redistribution and use in source and binary forms, with or without
       
     6 modification, are permitted provided that the following conditions are met:
       
     7 
       
     8     * Redistributions of source code must retain the above copyright
       
     9       notice, this list of conditions and the following disclaimer.
       
    10 
       
    11     * Redistributions in binary form must reproduce the above copyright
       
    12       notice, this list of conditions and the following disclaimer in the
       
    13       documentation and/or other materials provided with the distribution.
       
    14 
       
    15     * Neither the name of Mahlon E. Smith nor the names of his
       
    16       contributors may be used to endorse or promote products derived
       
    17       from this software without specific prior written permission.
       
    18 
       
    19 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
       
    20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
       
    22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
       
    23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
       
    24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
       
    26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29 */
       
    30 
       
    31 /*
       
    32  * TODO
       
    33  *
       
    34  * empty struct not necessary?
       
    35  * inet_pton( AF_INET, *char src, dest )
       
    36  * an option to run the DB out of memory?
       
    37  * PRAGMA user_version = 1;
       
    38  *
       
    39  */
       
    40 
       
    41 #include "volta.h"
       
    42 unsigned short int debugmode;
       
    43 
       
    44 
       
    45 /*
       
    46  * Parse command line options, perform actions, and enter accept loop.
       
    47  *
       
    48  */
       
    49 int
       
    50 main( int argc, char *argv[] )
       
    51 {
       
    52 	/* opt action flags */
       
    53 	struct {
       
    54 		unsigned char init;
       
    55 		unsigned char dump;
       
    56 	} actions = {0};
       
    57 
       
    58 #ifdef DEBUG
       
    59 	/* debugmode set at compile time,
       
    60 	 * default to display everything */
       
    61 	debugmode = 99;
       
    62 #else
       
    63 	debugmode = 0;
       
    64 #endif
       
    65 
       
    66 	/* get_opt vars */
       
    67 	int opt = 0;
       
    68 	opterr  = 0;
       
    69 
       
    70 	/* parse options */
       
    71 	while ( (opt = getopt( argc, argv, "a:d:hv" )) != -1 ) {
       
    72 		switch ( opt ) {
       
    73 
       
    74 			/* action */
       
    75 			case 'a':
       
    76 				if ( strcmp( optarg, "init" ) == 0 ) actions.init++;
       
    77 				if ( strcmp( optarg, "dump" ) == 0 ) actions.dump++;
       
    78 				break;
       
    79 
       
    80 			/* debug */
       
    81 			case 'd':
       
    82 				if ( optarg[2] == '-' ) {
       
    83 					argc++; argv -= 1;
       
    84 					debugmode = 1;
       
    85 				}
       
    86 				sscanf( optarg, "%hu", &debugmode );
       
    87 				break;
       
    88 
       
    89 			/* help */
       
    90 			case 'h':
       
    91 				usage( argv[0] );
       
    92 				return( 0 );
       
    93 
       
    94 			/* version */
       
    95 			case 'v':
       
    96 				printf( "%s %s\n", PROG, VERSION );
       
    97 				return( 0 );
       
    98 
       
    99 			/* unknown option or option argument missing */
       
   100 			case '?':
       
   101 				switch( optopt ) {
       
   102 					case 'd': /* no debug argument, default to level 1 */
       
   103 						debugmode = 1;
       
   104 						break;
       
   105 					default:
       
   106 						usage( argv[0] );
       
   107 						return( 1 );
       
   108 				}
       
   109 
       
   110 			default:
       
   111 				break;
       
   112 		}
       
   113 	}
       
   114 	argc -= optind;
       
   115 	argv += optind;
       
   116 
       
   117 	/* perform any requested actions */
       
   118 	if ( actions.init ) {
       
   119 		db_initialize();
       
   120 		return( 0 );
       
   121 	}
       
   122 	if ( actions.dump ) {
       
   123 		debug( 1, LOC, "dump.\n" );
       
   124 		return( 0 );
       
   125 	}
       
   126 
       
   127 	/* enter stdin parsing loop */
       
   128 	return accept_loop();
       
   129 }
       
   130