int main( int argc, char * argv[]) { if (argc != 8){ fprintf(stderr, "use: GenLogStrips name #packages #cities #planes #locs_per_city #trucks_per_city #goals\n"); exit(1); } name = argv[1]; npackages = atoi(argv[2]); ncities = atoi(argv[3]); nplanes = atoi(argv[4]); nlocs = atoi(argv[5]); ntrucks = atoi(argv[6]); ngoals = atoi(argv[7]); gettimeofday(&tv,&tzp); seed = (( tv.tv_sec & 0177 ) * 1000000) + tv.tv_usec; srandom(seed); print_header(); print_init(); print_final(); return 0; }
int main(int argc, char **argv) { int i=0; char* inquery=NULL; char query[MAXQUERYLEN]; int pcount=0; int ql,found; char* res=NULL; char* database=DEFAULT_DATABASE; char* params[MAXPARAMS]; char* values[MAXPARAMS]; int hformat=1; // for header 0: csv, 1: json: reset later after reading params // Set up timeout signal and abnormal termination handlers: // the termination handler clears the read lock and detaches database. // This may fail, however, for some lock strategies and in case // nontrivial operations are taken in the handler. #if _MSC_VER // no signals on windows #else signal(SIGSEGV,termination_handler); signal(SIGINT,termination_handler); signal(SIGFPE,termination_handler); signal(SIGABRT,termination_handler); signal(SIGTERM,termination_handler); signal(SIGALRM,timeout_handler); alarm(TIMEOUT_SECONDS); #endif // for debugging print the plain content-type immediately // printf("content-type: text/plain\r\n"); // get the cgi query: passed by server or given on the command line inquery=getenv("QUERY_STRING"); if (inquery==NULL && argc>1) inquery=argv[1]; // or use your own query string for testing a la // inquery="db=1000&op=search&field=1&value=2&compare=equal&type=record&from=0&count=3"; // parse the query if (inquery==NULL || inquery[0]=='\0') errhalt(NOQUERY_ERR); ql=strlen(inquery); if (ql>MAXQUERYLEN) errhalt(LONGQUERY_ERR); strcpy((char*)query,inquery); //printf("query: %s\n",query); pcount=parse_query(query,ql,params,values); if (pcount<=0) errhalt(MALFQUERY_ERR); //for(i=0;i<pcount;i++) { // printf("param %s val %s\n",params[i],values[i]); //} // query is now successfully parsed: find the database for(i=0;i<pcount;i++) { if (strncmp(params[i],"db",MAXQUERYLEN)==0) { if ((values[i]!=NULL) && (values[i][0]!='\0')) { if (atoi(values[i])==0 && !(values[i][0]=='0' && values[i][1]=='\0')) { errhalt(DB_PARAM_ERR); } database=values[i]; break; } } } //find the operation and dispatch found=0; for(i=0;i<pcount;i++) { if (strncmp(params[i],"op",MAXQUERYLEN)==0) { if (strncmp(values[i],"search",MAXQUERYLEN)==0) { found=1; res=search(database,params,values,pcount,&hformat); // here the locks should be freed and database detached break; } else if (strncmp(values[i],"recids",MAXQUERYLEN)==0) { found=1; res=recids(database,params,values,pcount,&hformat); // here the locks should be freed and database detached break; } else { errhalt(UNKNOWN_OP_ERR); } } } if (!found) errhalt(NO_OP_ERR); print_final(res,hformat); if (res!=NULL) free(res); // not really necessary and wastes time: process exits return 0; }
char* process_query(char* inquery, thread_data_p tdata) { int i=0; char *query; char querybuf[MAXQUERYLEN]; int pcount=0; int ql,found; char* res=NULL; char* database=DEFAULT_DATABASE; char* params[MAXPARAMS]; char* values[MAXPARAMS]; // first NULL values potentially left from earlier thread calls #if _MSC_VER #else tdata->db=NULL; #endif tdata->database=NULL; tdata->lock_id=0; tdata->intype=0; tdata->jsonp=NULL; tdata->format=1; tdata->showid=0; tdata->depth=MAX_DEPTH_DEFAULT; tdata->maxdepth=MAX_DEPTH_DEFAULT; tdata->strenc=2; tdata->buf=NULL; tdata->bufptr=NULL; tdata->bufsize=0; // or use your own query string for testing a la // inquery="db=1000&op=search&field=1&value=2&compare=equal&type=record&from=0&count=3"; // parse the query if (inquery==NULL || inquery[0]=='\0') { if (tdata->iscgi && tdata->method==POST_METHOD_CODE) return errhalt(CGI_QUERY_ERR,tdata); else return errhalt(NOQUERY_ERR,tdata); } ql=strlen(inquery); if (ql>MAXQUERYLEN) return errhalt(LONGQUERY_ERR,tdata); if (tdata->isserver) query=inquery; else { strcpy((char*)querybuf,inquery); query=(char*)querybuf; } //fprintf(stderr, "query: %s\n", query); pcount=parse_query(query,ql,params,values); if (pcount<=0) return errhalt(MALFQUERY_ERR,tdata); //for(i=0;i<pcount;i++) { // printf("param %s val %s\n",params[i],values[i]); //} // query is now successfully parsed: // find the database #ifdef DEFAULT_DBASE database=DEFAULT_DBASE; #endif if ((tdata->global)->conf->default_dbase.used>0) database=(tdata->global)->conf->default_dbase.vals[0]; for(i=0;i<pcount;i++) { if (strncmp(params[i],"db",MAXQUERYLEN)==0) { if ((values[i]!=NULL) && (values[i][0]!='\0')) { if (atoi(values[i])==0 && !(values[i][0]=='0' && values[i][1]=='\0')) { return errhalt(DB_PARAM_ERR,tdata); } database=values[i]; break; } } } tdata->database=database; // try to find jsonp for(i=0;i<pcount;i++) { if (strncmp(params[i],JSONP_PARAM,MAXQUERYLEN)==0) { tdata->jsonp=values[i]; break; } } //find the operation and dispatch found=0; for(i=0;i<pcount;i++) { if (strncmp(params[i],"op",MAXQUERYLEN)==0) { if (!strncmp(values[i],"count",MAXQUERYLEN)){ found=1; res=search(tdata,params,values,pcount,COUNT_CODE); break; } else if (!strncmp(values[i],"search",MAXQUERYLEN) || !strncmp(values[i],"select",MAXQUERYLEN)) { found=1; res=search(tdata,params,values,pcount,SEARCH_CODE); break; } else if (!strncmp(values[i],"insert",MAXQUERYLEN)) { found=1; res=insert(tdata,params,values,pcount); break; } else if (!strncmp(values[i],"update",MAXQUERYLEN)) { found=1; res=search(tdata,params,values,pcount,UPDATE_CODE); break; } else if (!strncmp(values[i],"delete",MAXQUERYLEN)){ found=1; res=search(tdata,params,values,pcount,DELETE_CODE); break; } else if (!strncmp(values[i],"create",MAXQUERYLEN)) { found=1; res=create(tdata,params,values,pcount); break; } else if (!strncmp(values[i],"drop",MAXQUERYLEN)) { found=1; res=drop(tdata,params,values,pcount); break; } else { return errhalt(UNKNOWN_OP_ERR,tdata); } } } if (!found) return errhalt(NO_OP_ERR,tdata); if (tdata->isserver) { if (tdata->inbuf!=NULL) { free(tdata->inbuf); tdata->inbuf=NULL; } return res; } else { print_final(res,tdata); // freeing here is not really necessary and wastes time: process exits anyway if (tdata->inbuf!=NULL) free(tdata->inbuf); if (res!=NULL) free(res); return NULL; } }