Clean up: use separate function for fatal error
[ftsbench.git] / ftsbench.c
index 44788fd..3c8b7f6 100644 (file)
@@ -88,7 +88,7 @@ usage() {
                "  -l LEXFILE - file with words and its frequents (default gendata/lex)\n"
                "  -g GAMMAFILE - file with doc's length distribution (default \n"
                "                 gendata/gamma-lens)\n"
-               "  -l FLGAS - options for db's schema (see below)\n"
+               "  -l FLAGS - options for db's schema (see below)\n"
                "  -s ID - SQL mode: output is a SQL queries, ID is an identifier for insert\n"
                "          statement\n"
                "  -q - do not print progress message\n",
@@ -108,7 +108,7 @@ usage() {
                "  -l LEXFILE - file with words and its frequents (default gendata/query-lex)\n"
                "  -g GAMMAFILE - file with doc's length distribution (default \n"
                "                 gendata/query-lens)\n"
-               "  -l FLGAS - options for db's schema (see below)\n"
+               "  -l FLAGS - options for db's schema (see below)\n"
                "  -s ID - SQL mode: output is a SQL queries, ID is an identifier for insert\n"
                "          statement\n"
                "  -r - row mode: timing every query\n"
@@ -141,16 +141,14 @@ getRDBMS(char *name) {
                        if ( DBDesc[i].init )
                                return DBDesc[i].rdbms; 
                } else if ( strcasecmp(name,DBDesc[i].shortname) == 0 ) {
-                       if ( DBDesc[i].init == NULL ) {
-                               fprintf(stderr,"Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
-                               exit(1);
-                       }
+                       if ( DBDesc[i].init == NULL ) 
+                               fatal("Support of '%s' isn't compiled-in\n", DBDesc[i].longname);
+
                        return DBDesc[i].rdbms;
                }
        }
 
-       fprintf(stderr,"Can't find a RDBMS\n");
-       exit(1);
+       fatal("Can't find a RDBMS\n");
        
        return NULLSQL;
 }
@@ -170,14 +168,11 @@ getFLAGS(char *flg) {
        if ( strcasestr(flg,"or") )
                flags |= FLG_OR;
 
-       if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) {
-               fprintf(stderr,"GIN and GiST flags are mutually exclusive\n");
-               exit(1);
-       }
-       if ( (flags & FLG_AND) && (flags & FLG_OR) ) {
-               fprintf(stderr,"AND and OR flags are mutually exclusive\n");
-               exit(1);
-       } else if ( ( flags & ( FLG_AND | FLG_OR ) ) == 0 )
+       if ( (flags & FLG_GIST) && (flags & FLG_GIN) ) 
+               fatal("GIN and GiST flags are mutually exclusive\n");
+       if ( (flags & FLG_AND) && (flags & FLG_OR) ) 
+               fatal("AND and OR flags are mutually exclusive\n");
+       else if ( ( flags & ( FLG_AND | FLG_OR ) ) == 0 )
                flags |= FLG_AND;
 
        return flags;
@@ -188,10 +183,8 @@ initConnections(RDBMS rdbms, int n, char *connstr) {
        ftsDB   **dbs = (ftsDB**)malloc(sizeof(ftsDB*) * n);
        int i;
 
-       if (!dbs) {
-               fprintf(stderr,"Not enough mwmory\n");
-               exit(1);
-       }
+       if (!dbs) 
+               fatal("Not enough mwmory\n");
 
        for(i=0;i<n;i++) { 
                dbs[i] = DBDesc[rdbms].init(connstr);
@@ -311,6 +304,20 @@ report(const char *format, ...) {
        fflush(stdout);
 }
 
+void
+fatal(const char *format, ...) {
+       va_list args;
+
+       va_start(args, format);
+       vfprintf(stderr, format, args);
+       va_end(args);
+
+       fflush(stderr);
+
+       exit(1);
+}
+
+
 extern char *optarg;
 
 int
@@ -432,10 +439,8 @@ main(int argn, char *argv[]) {
 
        pthread_mutex_lock( &mutexFinish );
                for(i=0;i<nClients;i++) {
-                       if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) {
-                               fprintf(stderr,"pthread_create failed: %s\n", strerror(errno));
-                               exit(1);
-                       }
+                       if ( pthread_create(tid+i, NULL, execBench, (void*)dbs[i]) != 0 ) 
+                               fatal("pthread_create failed: %s\n", strerror(errno));
                }
 
                for(;;) {
@@ -459,10 +464,8 @@ main(int argn, char *argv[]) {
                        sleepTo.tv_sec = time(NULL) + 1;
                        res = pthread_cond_timedwait( &condFinish, &mutexFinish, &sleepTo );
 
-                       if ( !(res == ETIMEDOUT || res == 0) ) {
-                               fprintf(stderr,"pthread_cond_timedwait failed: %s\n", strerror(errno));
-                               exit(1);
-                       }
+                       if ( !(res == ETIMEDOUT || res == 0) ) 
+                               fatal("pthread_cond_timedwait failed: %s\n", strerror(errno));
                }
                elapsed = elapsedtime(&begin);
                pthread_mutex_unlock( &mutexFinish );