Esempio n. 1
0
int main(int argc, char *argv[]){
    
    FILE *fd;
    job_registry_entry *en;
    time_t now;
    time_t purge_time=0;
    char constraint[JOBID_MAX_LEN+1];
    char constraint2[5];
    char *query=NULL;
    char *queryStates=NULL;
    char *query_err=NULL;

    char *pidfile=NULL;
    char string_now[11];
    char *tpath;
    
    int version=0;
    int tmptim;
    int finstr_len=0;
    int loop_interval=DEFAULT_LOOP_INTERVAL;
    
    int fsq_ret=0;
    
    int c;
    
    int confirm_time=0;
    
    static int help;
    static int short_help;
    
    while (1) {
	static struct option long_options[] =
	{
	    {"help",      no_argument,     &help,       1},
	    {"usage",     no_argument,     &short_help, 1},
	    {"nodaemon",  no_argument,       0, 'o'},
	    {"version",   no_argument,       0, 'v'},
	    {0, 0, 0, 0}
	};
	
	int option_index = 0;
	
	c = getopt_long (argc, argv, "vo",long_options, &option_index);
	
	if (c == -1){
	    break;
	}
	
	switch (c)
	{
	    
	    case 0:
		if (long_options[option_index].flag != 0){
		    break;
		}
		
	    case 'v':
		version=1;
		break;
		
	    case 'o':
		nodmn=1;
		break;
		
	    case '?':
		break;
		
	    default:
		abort ();
	}
    }
    
    //check if another instance is running 
    char **ptr;
    char out[3];
    fgets(out, sizeof(out),popen("ps -d | grep -c BUpdaterSGE","r"));
    strtoken(out,'\n',&ptr);
    if (strcmp(ptr[0],"1")!=0){
	fprintf(stderr,"There is another instance of BUpdaterSGE running.\nExiting ...\n");
	return -1;
    }
    freetoken(&ptr,1);

    if(help){
	usage();
    }
    
    if(short_help){
	short_usage();
    }
    
    argv0 = argv[0];
    
    signal(SIGHUP,sighup);
    
    if(version) {
	printf("%s Version: %s\n",progname,VERSION);
	exit(EXIT_SUCCESS);
    }  
    
    /* Checking configuration */
    check_config_file("UPDATER"); 
    
    cha = config_read(NULL);
    if (cha == NULL)
    {
	fprintf(stderr,"Error reading config: ");
	perror("");
	return -1;
    }
    config_setenv(NULL);
    
    ret = config_get("bupdater_child_poll_timeout",cha);
    if (ret != NULL){
	tmptim=atoi(ret->value);
	if (tmptim > 0) bfunctions_poll_timeout = tmptim*1000;
    }
    
    ret = config_get("bupdater_debug_level",cha);
    if (ret != NULL){
	debug=atoi(ret->value);
    }
    
    ret = config_get("bupdater_debug_logfile",cha);
    if (ret != NULL){
	debuglogname=strdup(ret->value);
	if(debuglogname == NULL){
	    sysfatal("strdup failed for debuglogname in main: %r");
	}
    }
    if(debug <=0){
	debug=0;
    }
    
    if(debuglogname){
	if((debuglogfile = fopen(debuglogname, "a+"))==0){
	    debug = 0;
	}
    }else{
	debug = 0;
    }

    ret = config_get("debug_level",cha);
    if (ret != NULL){
	debug=atoi(ret->value);
    }

    ret = config_get("debug_logfile",cha);
    if (ret != NULL){
	debuglogname=strdup(ret->value);
	if(debuglogname == NULL){
	    sysfatal("strdup failed for debuglogname in main: %r");
	}
    }
    if(debug <=0){
	debug=0;
    }

    if(debuglogname){
	if((debuglogfile = fopen(debuglogname, "a+"))==0){
	    debug = 0;
	}
    }else{
	debug = 0;
    }

    ret = config_get("sge_binpath",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key sge_binpath not found\n",argv0);
    } else {
	sge_binpath=strdup(ret->value);
	if(sge_binpath == NULL){
	    sysfatal("strdup failed for sge_binpath in main: %r");
	}
    }

    ret = config_get("sge_rootpath",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key sge_rootpath not found\n",argv0);
    } else {
	sge_rootpath=strdup(ret->value);
	if(sge_rootpath == NULL){
	    sysfatal("strdup failed for sge_rootpath in main: %r");
	}
	
	tpath=make_message("%s",sge_rootpath);
	if (opendir(tpath)==NULL){
	    do_log(debuglogfile, debug, 1, "%s: dir %s does not exist or is not readable\n",argv0,tpath);
	    sysfatal("dir %s does not exist or is not readable: %r",tpath);
	}
	free(tpath);
    }

    ret = config_get("sge_cellname",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key sge_cellname not found\n",argv0);
    } else {
	sge_cellname=strdup(ret->value);
	if(sge_cellname == NULL){
	    sysfatal("strdup failed for sge_cellname in main: %r");
	}
    }

    ret = config_get("sge_rootpath",cha);
    if (ret == NULL){
	if(debug){
	    fprintf(debuglogfile, "%s: key sge_rootpath not found\n",argv0);
	    fflush(debuglogfile);
	}
    } else {
	sge_rootpath=strdup(ret->value);
	if(sge_rootpath == NULL){
	    sysfatal("strdup failed for sge_rootpath in main: %r");
	}
    }

    ret = config_get("sge_cellname",cha);
    if (ret == NULL){
	if(debug){
	    fprintf(debuglogfile, "%s: key sge_cellname not found\n",argv0);
	    fflush(debuglogfile);
	}
    } else {
	sge_cellname=strdup(ret->value);
	if(sge_cellname == NULL){
	    sysfatal("strdup failed for sge_cellname in main: %r");
	}
    }

    ret = config_get("job_registry",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key job_registry not found\n",argv0);
	sysfatal("job_registry not defined. Exiting");
    } else {
	reg_file=strdup(ret->value);
	if(reg_file == NULL){
	    sysfatal("strdup failed for reg_file in main: %r");
	}
    }

    ret = config_get("purge_interval",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key purge_interval not found using the default:%d\n",argv0,purge_interval);
    } else {
	purge_interval=atoi(ret->value);
    }

    ret = config_get("finalstate_query_interval",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key finalstate_query_interval not found using the default:%d\n",argv0,finalstate_query_interval);
    } else {
	finalstate_query_interval=atoi(ret->value);
    }

    ret = config_get("alldone_interval",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key alldone_interval not found using the default:%d\n",argv0,alldone_interval);
    } else {
	alldone_interval=atoi(ret->value);
    }

    ret = config_get("bupdater_loop_interval",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key bupdater_loop_interval not found using the default:%d\n",argv0,loop_interval);
    } else {
	loop_interval=atoi(ret->value);
    }

    ret = config_get("sge_qacct_retry_interval",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key sge_qacct_retry_interval not found using the default:%d\n",argv0,qacct_retry_interval);
    } else {
	qacct_retry_interval=atoi(ret->value);
    }

    ret = config_get("bupdater_pidfile",cha);
    if (ret == NULL){
	do_log(debuglogfile, debug, 1, "%s: key bupdater_pidfile not found\n",argv0);
    } else {
	pidfile=strdup(ret->value);
	if(pidfile == NULL){
	    sysfatal("strdup failed for pidfile in main: %r");
	}
    }

    ret = config_get("job_registry_use_mmap",cha);
    if (ret == NULL){
        do_log(debuglogfile, debug, 1, "%s: key job_registry_use_mmap not found. Default is NO\n",argv0);
    } else {
        do_log(debuglogfile, debug, 1, "%s: key job_registry_use_mmap is set to %s\n",argv0,ret->value);
    }

    if( !nodmn ) daemonize();

    if( pidfile ){
	writepid(pidfile);
	free(pidfile);
    }

    config_free(cha);
    rha=job_registry_init(reg_file, BY_BATCH_ID);
    if (rha == NULL){
	do_log(debuglogfile, debug, 1, "%s: Error initialising job registry %s\n",argv0,reg_file);
	fprintf(stderr,"%s: Error initialising job registry %s :",argv0,reg_file);
	perror("");
    }
   for(;;){
	/* Purge old entries from registry */
	now=time(0);
	if(now - purge_time > 86400){
	    if(job_registry_purge(reg_file, now-purge_interval,0)<0){
		do_log(debuglogfile, debug, 1, "%s: Error purging job registry %s\n",argv0,reg_file);
		fprintf(stderr,"%s: Error purging job registry %s :",argv0,reg_file);
		perror("");
	    }else{
		purge_time=time(0);
	    }
	}
	
	//IntStateQuery();
	fd = job_registry_open(rha, "r");
	if (fd == NULL)
	{
	    do_log(debuglogfile, debug, 1, "%s: Error opening job registry %s\n",argv0,reg_file);
	    fprintf(stderr,"%s: Error opening job registry %s :",argv0,reg_file);
	    perror("");
	    sleep(loop_interval);
	}
	if (job_registry_rdlock(rha, fd) < 0)
	{
	    do_log(debuglogfile, debug, 1, "%s: Error read locking job registry %s\n",argv0,reg_file);
	    fprintf(stderr,"%s: Error read locking job registry %s :",argv0,reg_file);
	    perror("");
	    sleep(loop_interval);
	}
	job_registry_firstrec(rha,fd);
	fseek(fd,0L,SEEK_SET);

	if((query=calloc(STR_CHARS*2,1)) == 0){
	    sysfatal("can't malloc query %r");
	}
	if((queryStates=calloc(STR_CHARS*2,1)) == 0){
	    sysfatal("can't malloc query %r");
	}
	
	query[0]=' ';
	queryStates[0]=' ';
	while ((en = job_registry_get_next(rha, fd)) != NULL)
	{
	    if(((now - en->mdate) > finalstate_query_interval) && en->status!=3 && en->status!=4)
	    {
		/* create the constraint that will be used in condor_history command in FinalStateQuery*/
		snprintf(constraint, sizeof(constraint), " %s",en->batch_id);
		if (en->status==0) snprintf(constraint2, sizeof(constraint2), " u");
		if (en->status==1) snprintf(constraint2, sizeof(constraint2), " q");
		if (en->status==2) snprintf(constraint2, sizeof(constraint2), " r");
		if (en->status==5) snprintf(constraint2, sizeof(constraint2), " h");
		query=realloc(query,strlen(query)+strlen(constraint)+1);
		queryStates=realloc(queryStates,strlen(queryStates)+strlen(constraint2)+1);
		strcat(query,constraint);
		strcat(queryStates,constraint2);
		runfinal=TRUE;
	    }
	    /* Assign Status=4 and ExitStatus=-1 to all entries that after alldone_interval are still not in a final state(3 or 4) */
	    if((now - en->mdate > alldone_interval) && en->status!=3 && en->status!=4 && !runfinal)
	    {
		time_t now;
		now=time(0);
		snprintf(string_now,sizeof(string_now),"%d",now);
		AssignState(en->batch_id,"4" ,"-1","\0","\0",string_now);
	    }
	   free(en);
	}
	if(runfinal){
	    if((query_err=calloc((int)strlen(query),1)) == 0)
		sysfatal("can't malloc query_err %r");
	    FinalStateQuery(query,queryStates,query_err);
	    free(query_err);
	}
	free(query);
	free(queryStates);
	fclose(fd);
	if (runfinal){
	    runfinal=FALSE;
	}
	sleep (loop_interval);
    } //for

    job_registry_destroy(rha);
    return(0);
}
Esempio n. 2
0
int main(int argc, char *argv[]){

	FILE *fd;
	job_registry_entry *en;
	time_t now;
	time_t purge_time=0;
	time_t last_consistency_check=0;
	char *pidfile=NULL;
	char *first_duplicate=NULL;
	
	struct pollfd *remupd_pollset = NULL;
	int remupd_nfds;
	
	int version=0;
	int first=TRUE;
	int tmptim;
	time_t finalquery_start_date;
	int loop_interval=DEFAULT_LOOP_INTERVAL;
	
	int rc;				
	int c;				
	
	pthread_t RecUpdNetThd;

	int confirm_time=0;	

        static int help;
        static int short_help;
	
	bact.njobs = 0;
	bact.jobs = NULL;
	
	while (1) {
		static struct option long_options[] =
		{
		{"help",      no_argument,     &help,       1},
		{"usage",     no_argument,     &short_help, 1},
		{"nodaemon",  no_argument,       0, 'o'},
		{"version",   no_argument,       0, 'v'},
		{"prefix",    required_argument, 0, 'p'},
		{0, 0, 0, 0}
		};

		int option_index = 0;
     
		c = getopt_long (argc, argv, "vop:",long_options, &option_index);
     
		if (c == -1){
			break;
		}
     
		switch (c)
		{

		case 0:
		if (long_options[option_index].flag != 0){
			break;
		}
     
		case 'v':
			version=1;
			break;
	       
		case 'o':
			nodmn=1;
			break;

		case 'p':
			break;

		case '?':
			break;
     
		default:
			abort ();
		}
	}
	
	if(help){
		usage();
	}
	 
	if(short_help){
		short_usage();
	}
	
	argv0 = argv[0];
	
        signal(SIGHUP,sighup);

	if(version) {
		printf("%s Version: %s\n",progname,VERSION);
		exit(EXIT_SUCCESS);
	}   

        /* Checking configuration */
        check_config_file("UPDATER");

	cha = config_read(NULL);
	if (cha == NULL)
	{
		fprintf(stderr,"Error reading config: ");
		perror("");
		return -1;
	}
        config_setenv(NULL);

	ret = config_get("bupdater_child_poll_timeout",cha);
	if (ret != NULL){
		tmptim=atoi(ret->value);
		if (tmptim > 0) bfunctions_poll_timeout = tmptim*1000;
	}

	ret = config_get("bupdater_debug_level",cha);
	if (ret != NULL){
		debug=atoi(ret->value);
	}
	
	ret = config_get("bupdater_debug_logfile",cha);
	if (ret != NULL){
		debuglogname=strdup(ret->value);
                if(debuglogname == NULL){
                        sysfatal("strdup failed for debuglogname in main: %r");
                }
	}
	if(debug <=0){
		debug=0;
	}
    
	if(debuglogname){
		if((debuglogfile = fopen(debuglogname, "a+"))==0){
			debug = 0;
		}
	} else {
		debug = 0;
	}
	
        ret = config_get("slurm_binpath",cha);
        if (ret == NULL){
                do_log(debuglogfile, debug, 1, "%s: key slurm_binpath not found\n",argv0);
        } else {
                slurm_binpath=strdup(ret->value);
                if(slurm_binpath == NULL){
                        sysfatal("strdup failed for slurm_binpath in main: %r");
                }
        }
	
	ret = config_get("job_registry",cha);
	if (ret == NULL){
		do_log(debuglogfile, debug, 1, "%s: key job_registry not found\n",argv0);
		sysfatal("job_registry not defined. Exiting");
	} else {
		registry_file=strdup(ret->value);
                if(registry_file == NULL){
                        sysfatal("strdup failed for registry_file in main: %r");
                }
	}
	
	ret = config_get("purge_interval",cha);
	if (ret == NULL){
		do_log(debuglogfile, debug, 1, "%s: key purge_interval not found using the default:%d\n",argv0,purge_interval);
	} else {
		purge_interval=atoi(ret->value);
	}
	
	ret = config_get("finalstate_query_interval",cha);
	if (ret == NULL){
		do_log(debuglogfile, debug, 1, "%s: key finalstate_query_interval not found using the default:%d\n",argv0,finalstate_query_interval);
	} else {
		finalstate_query_interval=atoi(ret->value);
	}
	
	ret = config_get("alldone_interval",cha);
	if (ret == NULL){
		do_log(debuglogfile, debug, 1, "%s: key alldone_interval not found using the default:%d\n",argv0,alldone_interval);
	} else {
		alldone_interval=atoi(ret->value);
	}
	
	ret = config_get("bupdater_consistency_check_interval",cha);
	if (ret == NULL){
		do_log(debuglogfile, debug, 1, "%s: key bupdater_consistency_check_interval not found using the default:%d\n",argv0,bupdater_consistency_check_interval);
	} else {
		bupdater_consistency_check_interval=atoi(ret->value);
	}

	ret = config_get("bupdater_pidfile",cha);
	if (ret == NULL){
		do_log(debuglogfile, debug, 1, "%s: key bupdater_pidfile not found\n",argv0);
	} else {
		pidfile=strdup(ret->value);
                if(pidfile == NULL){
                        sysfatal("strdup failed for pidfile in main: %r");
                }
	}

	ret = config_get("bupdater_loop_interval",cha);
	if (ret == NULL){
		do_log(debuglogfile, debug, 1, "%s: key bupdater_loop_interval not found - using the default:%d\n",argv0,loop_interval);
	} else {
		loop_interval=atoi(ret->value);
	}
	
	ret = config_get("job_registry_use_mmap",cha);
	if (ret == NULL){
		do_log(debuglogfile, debug, 1, "%s: key job_registry_use_mmap not found. Default is NO\n",argv0);
	} else {
		do_log(debuglogfile, debug, 1, "%s: key job_registry_use_mmap is set to %s\n",argv0,ret->value);
	}
	
	remupd_conf = config_get("job_registry_add_remote",cha);
	if (remupd_conf == NULL){
		do_log(debuglogfile, debug, 1, "%s: key job_registry_add_remote not found\n",argv0);
	}else{
		if (job_registry_updater_setup_receiver(remupd_conf->values,remupd_conf->n_values,&remupd_head) < 0){
			do_log(debuglogfile, debug, 1, "%s: Cannot set network receiver(s) up for remote update\n",argv0);
			fprintf(stderr,"%s: Cannot set network receiver(s) up for remote update \n",argv0);
       		}
 
		if (remupd_head == NULL){
			do_log(debuglogfile, debug, 1, "%s: Cannot find values for network endpoints in configuration file (attribute 'job_registry_add_remote').\n",argv0);
			fprintf(stderr,"%s: Cannot find values for network endpoints in configuration file (attribute 'job_registry_add_remote').\n", argv0);
		}

		if ((remupd_nfds = job_registry_updater_get_pollfd(remupd_head, &remupd_pollset)) < 0){
			do_log(debuglogfile, debug, 1, "%s: Cannot setup poll set for receiving data.\n",argv0);
    			fprintf(stderr,"%s: Cannot setup poll set for receiving data.\n", argv0);
		}
		if (remupd_pollset == NULL || remupd_nfds == 0){
			do_log(debuglogfile, debug, 1, "%s: No poll set available for receiving data.\n",argv0);
			fprintf(stderr,"%s: No poll set available for receiving data.\n",argv0);
		}
	
	}
	
	if( !nodmn ) daemonize();


	if( pidfile ){
		writepid(pidfile);
		free(pidfile);
	}
	
	rha=job_registry_init(registry_file, BY_BATCH_ID);
	if (rha == NULL){
		do_log(debuglogfile, debug, 1, "%s: Error initialising job registry %s\n",argv0,registry_file);
		fprintf(stderr,"%s: Error initialising job registry %s :",argv0,registry_file);
		perror("");
	}
	
	if (remupd_conf != NULL){
		pthread_create(&RecUpdNetThd, NULL, (void *(*)(void *))ReceiveUpdateFromNetwork, (void *)NULL);
	
		if (job_registry_updater_setup_sender(remupd_conf->values,remupd_conf->n_values,0,&remupd_head_send) < 0){
			do_log(debuglogfile, debug, 1, "%s: Cannot set network sender(s) up for remote update\n",argv0);
			fprintf(stderr,"%s: Cannot set network sender(s) up for remote update \n",argv0);
       		}
		if (remupd_head_send == NULL){
			do_log(debuglogfile, debug, 1, "%s: Cannot find values for network endpoints in configuration file (attribute 'job_registry_add_remote').\n",argv0);
			fprintf(stderr,"%s: Cannot find values for network endpoints in configuration file (attribute 'job_registry_add_remote').\n", argv0);
		}
	}

	config_free(cha);
	
	for(;;){
		/* Purge old entries from registry */
		now=time(0);
		if(now - purge_time > 86400){
			if((rc=job_registry_purge(registry_file, now-purge_interval,0))<0){
				do_log(debuglogfile, debug, 1, "%s: Error purging job registry %s:%d\n",argv0,registry_file,rc);
                	        fprintf(stderr,"%s: Error purging job registry %s :",argv0,registry_file);
                	        perror("");

			}
			purge_time=time(0);
		}
		
		now=time(0);
		if(now - last_consistency_check > bupdater_consistency_check_interval){
			if(job_registry_check_index_key_uniqueness(rha,&first_duplicate)==JOB_REGISTRY_FAIL){
				do_log(debuglogfile, debug, 1, "%s: Found job registry duplicate entry. The first one is:%s.\nJobid should be removed or registry directory should be removed.\n",argv0,first_duplicate);
               	        	fprintf(stderr,"%s: Found job registry duplicate entry. The first one is:%s.\nJobid should be removed or registry directory should be removed.",argv0,first_duplicate);
 
			}
			last_consistency_check=time(0);
		}
		
		IntStateQuery();
		
		fd = job_registry_open(rha, "r");
		if (fd == NULL){
			do_log(debuglogfile, debug, 1, "%s: Error opening job registry %s\n",argv0,registry_file);
			fprintf(stderr,"%s: Error opening job registry %s :",argv0,registry_file);
			perror("");
			sleep(loop_interval);
			continue;
		}
		if (job_registry_rdlock(rha, fd) < 0){
			do_log(debuglogfile, debug, 1, "%s: Error read locking job registry %s\n",argv0,registry_file);
			fprintf(stderr,"%s: Error read locking job registry %s :",argv0,registry_file);
			perror("");
			sleep(loop_interval);
			continue;
		}
		job_registry_firstrec(rha,fd);
		fseek(fd,0L,SEEK_SET);
		
		first=TRUE;
		finalquery_start_date = time(0);
		
		while ((en = job_registry_get_next(rha, fd)) != NULL){

			if((bupdater_lookup_active_jobs(&bact,en->batch_id) != BUPDATER_ACTIVE_JOBS_SUCCESS) && en->status!=REMOVED && en->status!=COMPLETED){
			
				do_log(debuglogfile, debug, 3, "%s: bupdater_lookup_active_jobs returned: %d for jobid: %s\n",argv0,bupdater_lookup_active_jobs(&bact,en->batch_id),en->batch_id);

				confirm_time=atoi(en->updater_info);
				if(confirm_time==0){
					confirm_time=en->mdate;
				}
			
				/* Assign Status=4 and ExitStatus=999 to all entries that after alldone_interval are still not in a final state(3 or 4)*/
				if(now-confirm_time>alldone_interval){
					AssignFinalState(en->batch_id);
					free(en);
					continue;
				}
								
				if(en->status==IDLE && strlen(en->updater_info)>0){
					if (en->mdate < finalquery_start_date){
						finalquery_start_date=en->mdate;
					}
					do_log(debuglogfile, debug, 2, "%s: FinalStateQuery needed for jobid=%s with status=%d\n",argv0,en->batch_id,en->status);
					runfinal=TRUE;
				}else if((now-confirm_time>finalstate_query_interval) && (now > next_finalstatequery)){
					if (en->mdate < finalquery_start_date){
						finalquery_start_date=en->mdate;
					}
					do_log(debuglogfile, debug, 2, "%s: FinalStateQuery needed for jobid=%s with status=%d\n",argv0,en->batch_id,en->status);
					runfinal=TRUE;
				}
				
			
			}
			free(en);
		}
		
		if(runfinal_oldlogs){
			FinalStateQuery(0,1);
			runfinal_oldlogs=FALSE;
			runfinal=FALSE;
		}else if(runfinal){
			FinalStateQuery(finalquery_start_date,1);
			runfinal=FALSE;
		}
		fclose(fd);		
		sleep(loop_interval);
	}
	
	job_registry_destroy(rha);
	
	return 0;
	
}
Esempio n. 3
0
int main(int argc, char **argv)
{
    int c, n;
    int b_flag=FALSE;
    int n_flag=FALSE;
    char *f_flag=NULL;
    char *m_flag=NULL;
    char cmd[MAXPATH];
    char *args[MAXARGS];
    
    int option_index;
    static struct option long_options[] =
    {
	{ "news-batch",   0, 0, 'b'},	/* Process news batch */
	{ "news-mode",    0, 0, 'n'},	/* Set news mode */
	{ "batch-file",   1, 0, 'f'},	/* Read batch file for list of articles */
	{ "maxmsg",       1, 0, 'm'},	/* New output packet after N msgs */

	{ "help",         0, 0, 'h'},	/* Help */
	{ 0,              0, 0, 0  }
    };

#ifdef SIGPIPE
    /* ignore SIGPIPE */
    signal(SIGPIPE, SIG_IGN);
#endif

    log_program(PROGRAM);

    /* init configuration */
    cf_initialize();
    cf_read_config_file(CONFIG);

    while ((c = getopt_long(argc, argv, "bnf:m:h",
			    long_options, &option_index     )) != EOF)
	switch (c) {
	/***** ftnmail options *****/
	case 'b':
	    b_flag = TRUE;
	    break;
	case 'n':
	    n_flag = TRUE;
	    break;
	case 'f':
	    f_flag = optarg;
	    break;
	case 'm':
	    m_flag = optarg;
	    break;
	    
	/***** common options *****/
	case 'h':
	    usage();
	    exit(0);
	    break;
	default:
	    short_usage();
	    exit(EX_USAGE);
	    break;
	}

    /* complete path of rfc2ftn */
    BUF_COPY3(cmd, cf_p_libexecdir(), "/", RFC2FTN);

    /* build args[] */
    n = 0;
    args[n++] = RFC2FTN;
    if(b_flag)
    {
	args[n++] = "-b";
    }
    if(n_flag)
    {
	args[n++] = "-n";
    }
    if(f_flag)
    {
	args[n++] = "-f";
	args[n++] = f_flag;
    }
    if(m_flag)
    {
	args[n++] = "-m";
	args[n++] = m_flag;
    }

    args[n++] = NULL;

#if 0
    /* debug */
    printf("cmd=%s\n", cmd);
    for(n=0; args[n]; n++)
	printf("args[%d]=%s\n", n, args[n]);
    exit(0);
#endif

    /* exec */
    if( execv(cmd, args) == ERROR )
	fglog("$can't exec %s", cmd);
	
    /* Only reached if error */
    exit(1);

    /**NOT REACHED**/
    return 1;
}
Esempio n. 4
0
int
main(int argc, char *argv[])
{
  int c;
  FILE *ifp = 0, *ofp = 0;
  const char *ifp_filename = "<stdin>";
  struct font_reader fr;
  int max_blocklen = -1;

  Clp_Parser *clp =
    Clp_NewParser(argc, (const char * const *)argv, sizeof(options) / sizeof(options[0]), options);
  program_name = Clp_ProgramName(clp);

  /* interpret command line arguments using CLP */
  while (1) {
    int opt = Clp_Next(clp);
    switch (opt) {

     case BLOCK_LEN_OPT:
      max_blocklen = clp->val.i;
      if (max_blocklen <= 0) {
	max_blocklen = 1;
	error("warning: block length raised to %d", max_blocklen);
      }
      break;

     output_file:
     case OUTPUT_OPT:
      if (ofp)
	fatal_error("output file already specified");
      if (strcmp(clp->vstr, "-") == 0)
	ofp = stdout;
      else {
	ofp = fopen(clp->vstr, "wb");
	if (!ofp) fatal_error("%s: %s", clp->vstr, strerror(errno));
      }
      break;

     case HELP_OPT:
      usage();
      exit(0);
      break;

     case VERSION_OPT:
      printf("t1binary (LCDF t1utils) %s\n", VERSION);
      printf("Copyright (C) 1992-2010 I. Lee Hetherington, Eddie Kohler et al.\n\
This is free software; see the source for copying conditions.\n\
There is NO warranty, not even for merchantability or fitness for a\n\
particular purpose.\n");
      exit(0);
      break;

     case Clp_NotOption:
      if (ifp && ofp)
	fatal_error("too many arguments");
      else if (ifp)
	goto output_file;
      if (strcmp(clp->vstr, "-") == 0)
	ifp = stdin;
      else {
	ifp_filename = clp->vstr;
	ifp = fopen(clp->vstr, "r");
	if (!ifp) fatal_error("%s: %s", clp->vstr, strerror(errno));
      }
      break;

     case Clp_Done:
      goto done;

     case Clp_BadOption:
      short_usage();
      exit(1);
      break;

    }
  }

 done:
  if (!ifp) ifp = stdin;
  if (!ofp) ofp = stdout;

#if defined(_MSDOS) || defined(_WIN32)
  /* As we are processing a PFB (binary) output */
  /* file, we must set its file mode to binary. */
  _setmode(_fileno(ofp), _O_BINARY);
#endif

  /* prepare font reader and pfb writer */
  fr.output_ascii = pfb_output_ascii;
  fr.output_binary = pfb_output_binary;
  fr.output_end = pfb_output_end;
  init_pfb_writer(&w, max_blocklen, ofp);

  /* peek at first byte to see if it is the PFB marker 0x80 */
  c = getc(ifp);
  ungetc(c, ifp);

  /* do the file */
  if (c == PFB_MARKER)
    process_pfb(ifp, ifp_filename, &fr);
  else if (c == '%')
    process_pfa(ifp, ifp_filename, &fr);
  else
    fatal_error("%s does not start with font marker (`%%' or 0x80)", ifp_filename);

  fclose(ifp);
  fclose(ofp);

  if (!w.binary_blocks_written)
    fatal_error("no binary blocks written! Are you sure this was a font?");

  return 0;
}
Esempio n. 5
0
/* Returns true if everything was OK */
bool parse_args(int argc, char **argv, struct slogic_handle *handle)
{
	int c;
	int libusb_debug_level = 0;
	char *endptr;
        opterr = 0;
	/* TODO: Add a -d flag to turn on internal debugging */
	while ((c = getopt(argc, argv, "n:f:r:hb:t:o:u:")) != -1) {
		switch (c) {
		case 'n':
			n_samples = strtol(optarg, &endptr, 10);
			if (*endptr != '\0' || n_samples <= 0) {
				short_usage("Invalid number of samples, must be a positive integer: %s", optarg);
				return false;
			}
			break;
		case 'f':
			output_file_name = optarg;
			break;
		case 'r':
			sample_rate = slogic_parse_sample_rate(optarg);
			if (!sample_rate) {
				short_usage("Invalid sample rate: %s", optarg);
				return false;
			}
			break;
		case 'h':
			full_usage();
			return false;
		case 'b':
			handle->transfer_buffer_size = strtol(optarg, &endptr, 10);
			if (*endptr != '\0' || handle->transfer_buffer_size <= 0) {
				short_usage("Invalid transfer buffer size, must be a positive integer: %s", optarg);
				return false;
			}
			break;
		case 't':
			handle->n_transfer_buffers = strtol(optarg, &endptr, 10);
			if (*endptr != '\0' || handle->n_transfer_buffers <= 0) {
				short_usage("Invalid transfer buffer count, must be a positive integer: %s", optarg);
				return false;
			}
			break;
		case 'o':
			handle->transfer_timeout = strtol(optarg, &endptr, 10);
			if (*endptr != '\0' || handle->transfer_timeout <= 0) {
				short_usage("Invalid transfer timeout, must be a positive integer: %s", optarg);
				return false;
			}
			break;
		case 'u':
			libusb_debug_level = strtol(optarg, &endptr, 10);
			if (*endptr != '\0' || libusb_debug_level < 0 || libusb_debug_level > 3) {
				short_usage("Invalid libusb debug level, must be a positive integer between "
					    "0 and 3: %s", optarg);
				return false;
			}
			libusb_set_debug(handle->context, libusb_debug_level);
			break;
		/* I'm not sure in which cases this really can happen, but optopt is not valid. */
		default:
			short_usage("Something is wrong. Use %s -h for usage.", c, me);
			return false;
		case '?':
			short_usage("Unknown argument: %c. Use %s -h for usage.", optopt, me);
			return false;
		}
	}

	if (!output_file_name) {
		short_usage("An output file has to be specified.", optarg);
		return false;
	}

	if (!sample_rate) {
		short_usage("A sample rate has to be specified.", optarg);
		return false;
	}

	if (!n_samples) {
		n_samples = sample_rate->samples_per_second;
	}

	return true;
}
Esempio n. 6
0
int main(int argc, char *argv[])
{
    int append=0;
    int create=0;
    int slim=0;
    int sanitize=0;
    int long_usage_bool=0;
    int pass_prompt=0;
    int projection = PROJ_SPHERE_MERC;
    int expire_tiles_zoom = -1;
    int expire_tiles_zoom_min = -1;
    int enable_hstore = 0;
    int enable_multi = 0;
    const char *expire_tiles_filename = "dirty_tiles";
    const char *db = "gis";
    const char *username=NULL;
    const char *host=NULL;
    const char *password=NULL;
    const char *port = "5432";
    const char *tblsmain_index = "pg_default"; // default TABLESPACE for index on main tables
    const char *tblsmain_data = "pg_default";  // default TABLESPACE for main tables
    const char *tblsslim_index = "pg_default"; // default TABLESPACE for index on slim mode tables
    const char *tblsslim_data = "pg_default";  // default TABLESPACE for slim mode tables
    const char *conninfo = NULL;
    const char *prefix = "planet_osm";
    const char *style = OSM2PGSQL_DATADIR "/default.style";
    const char *temparg;
    const char *output_backend = "pgsql";
    const char *input_reader = "auto";
    const char **hstore_columns = NULL;
    int n_hstore_columns = 0;
    int keep_coastlines=0;
    int cache = 800;
    struct output_options options;
    PGconn *sql_conn;
    
    int (*streamFile)(char *, int, struct osmdata_t *);

    printf("osm2pgsql SVN version %s\n\n", VERSION);

    while (1) {
        int c, option_index = 0;
        static struct option long_options[] = {
            {"append",   0, 0, 'a'},
            {"bbox",     1, 0, 'b'},
            {"create",   0, 0, 'c'},
            {"database", 1, 0, 'd'},
            {"latlong",  0, 0, 'l'},
            {"verbose",  0, 0, 'v'},
            {"slim",     0, 0, 's'},
            {"prefix",   1, 0, 'p'},
            {"proj",     1, 0, 'E'},
            {"merc",     0, 0, 'm'},
            {"oldmerc",  0, 0, 'M'},
            {"utf8-sanitize", 0, 0, 'u'},
            {"cache",    1, 0, 'C'},
            {"username", 1, 0, 'U'},
            {"password", 0, 0, 'W'},
            {"host",     1, 0, 'H'},
            {"port",     1, 0, 'P'},
            {"tablespace-index", 1, 0, 'i'},
            {"tablespace-slim-data", 1, 0, 200},
            {"tablespace-slim-index", 1, 0, 201},
            {"tablespace-main-data", 1, 0, 202},
            {"tablespace-main-index", 1, 0, 203},
            {"help",     0, 0, 'h'},
            {"style",    1, 0, 'S'},
            {"expire-tiles", 1, 0, 'e'},
            {"expire-output", 1, 0, 'o'},
            {"output",   1, 0, 'O'},
            {"extra-attributes", 0, 0, 'x'},
            {"hstore", 0, 0, 'k'},
            {"hstore-column", 1, 0, 'z'},
            {"multi-geometry", 0, 0, 'G'},
            {"keep-coastlines", 0, 0, 'K'},
            {"input-reader", 1, 0, 'r'},
            {"version", 0, 0, 'V'},
            {0, 0, 0, 0}
        };

        c = getopt_long (argc, argv, "ab:cd:KhlmMp:suvU:WH:P:i:E:C:S:e:o:O:xkGz:r:V", long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
            case 'a': append=1;   break;
            case 'b': osmdata.bbox=optarg; break;
            case 'c': create=1;   break;
            case 'v': verbose=1;  break;
            case 's': slim=1;     break;
            case 'K': keep_coastlines=1;     break;
            case 'u': sanitize=1; break;
            case 'l': projection=PROJ_LATLONG;  break;
            case 'm': projection=PROJ_SPHERE_MERC; break;
            case 'M': projection=PROJ_MERC; break;
            case 'E': projection=-atoi(optarg); break;
            case 'p': prefix=optarg; break;
            case 'd': db=optarg;  break;
            case 'C': cache = atoi(optarg); break;
            case 'U': username=optarg; break;
            case 'W': pass_prompt=1; break;
            case 'H': host=optarg; break;
            case 'P': port=optarg; break;
            case 'S': style=optarg; break;
            case 'i': tblsmain_index=tblsslim_index=optarg; break;
            case 200: tblsslim_data=optarg; break;    
            case 201: tblsslim_index=optarg; break;    
            case 202: tblsmain_data=optarg; break;    
            case 203: tblsmain_index=optarg; break;    
            case 'e':
                expire_tiles_zoom_min = atoi(optarg);
                temparg = strchr(optarg, '-');
                if (temparg) expire_tiles_zoom = atoi(temparg + 1);
                if (expire_tiles_zoom < expire_tiles_zoom_min) expire_tiles_zoom = expire_tiles_zoom_min;
                break;
            case 'o': expire_tiles_filename=optarg; break;
            case 'O': output_backend = optarg; break;
            case 'x': osmdata.extra_attributes=1; break;
            case 'k': enable_hstore=1; break;
            case 'z': 
                n_hstore_columns++;
                hstore_columns = (const char**)realloc(hstore_columns, sizeof(&n_hstore_columns) * n_hstore_columns);
                hstore_columns[n_hstore_columns-1] = optarg;
                break;
            case 'G': enable_multi=1; break;
            case 'r': input_reader = optarg; break;
            case 'h': long_usage_bool=1; break;
            case 'V': exit(EXIT_SUCCESS);
            case '?':
            default:
                short_usage(argv[0]);
                exit(EXIT_FAILURE);
        }
    }

    if (long_usage_bool) {
        long_usage(argv[0]);
        exit(EXIT_SUCCESS);
    }

    if (argc == optind) {  // No non-switch arguments
        short_usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    if (append && create) {
        fprintf(stderr, "Error: --append and --create options can not be used at the same time!\n");
        exit(EXIT_FAILURE);
    }

    if (cache < 0) cache = 0;

    if (pass_prompt)
        password = simple_prompt("Password:"******"PGPASS");
    }	
        

    conninfo = build_conninfo(db, username, password, host, port);
    sql_conn = PQconnectdb(conninfo);
    if (PQstatus(sql_conn) != CONNECTION_OK) {
        fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(sql_conn));
        exit(EXIT_FAILURE);
    }
    PQfinish(sql_conn);

    text_init();
    initList(&osmdata.tags);

    osmdata.count_node = osmdata.max_node = 0;
    osmdata.count_way  = osmdata.max_way  = 0;
    osmdata.count_rel  = osmdata.max_rel  = 0;

    LIBXML_TEST_VERSION

    project_init(projection);
    fprintf(stderr, "Using projection SRS %d (%s)\n", 
        project_getprojinfo()->srs, project_getprojinfo()->descr );

    if (parse_bbox(&osmdata))
        return 1;

    options.conninfo = conninfo;
    options.prefix = prefix;
    options.append = append;
    options.slim = slim;
    options.projection = project_getprojinfo()->srs;
    options.scale = (projection==PROJ_LATLONG)?10000000:100;
    options.mid = slim ? &mid_pgsql : &mid_ram;
    options.cache = cache;
    options.style = style;
    options.tblsmain_index = tblsmain_index;
    options.tblsmain_data = tblsmain_data;
    options.tblsslim_index = tblsslim_index;
    options.tblsslim_data = tblsslim_data;
    options.expire_tiles_zoom = expire_tiles_zoom;
    options.expire_tiles_zoom_min = expire_tiles_zoom_min;
    options.expire_tiles_filename = expire_tiles_filename;
    options.enable_multi = enable_multi;
    options.enable_hstore = enable_hstore;
    options.hstore_columns = hstore_columns;
    options.n_hstore_columns = n_hstore_columns;
    options.keep_coastlines = keep_coastlines;

    if (strcmp("pgsql", output_backend) == 0) {
      osmdata.out = &out_pgsql;
    } else if (strcmp("gazetteer", output_backend) == 0) {
      osmdata.out = &out_gazetteer;
    } else if (strcmp("null", output_backend) == 0) {
      osmdata.out = &out_null;
    } else {
      fprintf(stderr, "Output backend `%s' not recognised. Should be one of [pgsql, gazetteer, null].\n", output_backend);
      exit(EXIT_FAILURE);
    }

    if (strcmp("auto", input_reader) != 0) {
      if (strcmp("libxml2", input_reader) == 0) {
        streamFile = &streamFileXML2;
      } else if (strcmp("primitive", input_reader) == 0) {
        streamFile = &streamFilePrimitive;
#ifdef BUILD_READER_PBF
      } else if (strcmp("pbf", input_reader) == 0) {
        streamFile = &streamFilePbf;
#endif
      } else {
        fprintf(stderr, "Input parser `%s' not recognised. Should be one of [libxml2, primitive"
#ifdef BUILD_READER_PBF
	      ", pbf"
#endif
	      "].\n", input_reader);
      exit(EXIT_FAILURE);
      }
    }

    osmdata.out->start(&options);

    realloc_nodes(&osmdata);
    realloc_members(&osmdata);

    if (sizeof(int*) == 4 && options.slim != 1) {
        fprintf(stderr, "\n!! You are running this on 32bit system, so at most\n");
        fprintf(stderr, "!! 3GB of RAM can be used. If you encounter unexpected\n");
        fprintf(stderr, "!! exceptions during import, you should try running in slim\n");
        fprintf(stderr, "!! mode using parameter -s.\n");
    }

    while (optind < argc) {
        /* if input_reader is not forced by -r switch try to auto-detect it
           by file extension */
        if (strcmp("auto", input_reader) == 0) {
#ifdef BUILD_READER_PBF
          if (strcasecmp(".pbf",argv[optind]+strlen(argv[optind])-4) == 0) {
            streamFile = &streamFilePbf;
          } else {
            streamFile = &streamFileXML2;
          }
#else
          streamFile = &streamFileXML2;
#endif
        }
        time_t start, end;

        fprintf(stderr, "\nReading in file: %s\n", argv[optind]);
        time(&start);
        if (streamFile(argv[optind], sanitize, &osmdata) != 0)
            exit_nicely();
        time(&end);
        fprintf(stderr, "  parse time: %ds\n", (int)(end - start));
        optind++;
    }

    xmlCleanupParser();
    xmlMemoryDump();
    
    if (osmdata.count_node || osmdata.count_way || osmdata.count_rel) {
        fprintf(stderr, "\n");
        fprintf(stderr, "Node stats: total(%d), max(%d)\n", osmdata.count_node, osmdata.max_node);
        fprintf(stderr, "Way stats: total(%d), max(%d)\n", osmdata.count_way, osmdata.max_way);
        fprintf(stderr, "Relation stats: total(%d), max(%d)\n", osmdata.count_rel, osmdata.max_rel);
    }
    osmdata.out->stop();
    
    free(osmdata.nds);
    free(osmdata.members);
    
    // free the column pointer buffer
    free(hstore_columns);

    project_exit();
    text_exit();
    fprintf(stderr, "\n");

    return 0;
}
Esempio n. 7
0
int
main (int argc, char **argv)
{
    int		 retval;
    PRFileDesc	*in_file;
    FILE	*out_file;	/* not PRFileDesc until SECU accepts it */
    int		 crequest, dresponse;
    int		 prequest, presponse;
    int		 ccert, vcert;
    const char	*db_dir, *date_str, *cert_usage_str, *name;
    const char	*responder_name, *responder_url, *signer_name;
    PRBool	 add_acceptable_responses, add_service_locator;
    SECItem	*data = NULL;
    PLOptState	*optstate;
    SECStatus	 rv;
    CERTCertDBHandle *handle = NULL;
    SECCertUsage cert_usage;
    int64	 verify_time;
    CERTCertificate *cert = NULL;
    PRBool ascii = PR_FALSE;

    retval = -1;		/* what we return/exit with on error */

    program_name = PL_strrchr(argv[0], '/');
    program_name = program_name ? (program_name + 1) : argv[0];

    in_file = PR_STDIN;
    out_file = stdout;

    crequest = 0;
    dresponse = 0;
    prequest = 0;
    presponse = 0;
    ccert = 0;
    vcert = 0;

    db_dir = NULL;
    date_str = NULL;
    cert_usage_str = NULL;
    name = NULL;
    responder_name = NULL;
    responder_url = NULL;
    signer_name = NULL;

    add_acceptable_responses = PR_FALSE;
    add_service_locator = PR_FALSE;

    optstate = PL_CreateOptState (argc, argv, "AHLPR:S:V:d:l:pr:s:t:u:w:");
    if (optstate == NULL) {
	SECU_PrintError (program_name, "PL_CreateOptState failed");
	return retval;
    }

    while (PL_GetNextOpt (optstate) == PL_OPT_OK) {
	switch (optstate->option) {
	  case '?':
	    short_usage (program_name);
	    return retval;

	  case 'A':
	    add_acceptable_responses = PR_TRUE;
	    break;

	  case 'H':
	    long_usage (program_name);
	    return retval;

	  case 'L':
	    add_service_locator = PR_TRUE;
	    break;

	  case 'P':
	    presponse = 1;
	    break;

	  case 'R':
	    dresponse = 1;
	    name = optstate->value;
	    break;

	  case 'S':
	    ccert = 1;
	    name = optstate->value;
	    break;

	  case 'V':
	    vcert = 1;
	    name = optstate->value;
	    break;

	  case 'a':
	    ascii = PR_TRUE;
	    break;

	  case 'd':
	    db_dir = optstate->value;
	    break;

	  case 'l':
	    responder_url = optstate->value;
	    break;

	  case 'p':
	    prequest = 1;
	    break;

	  case 'r':
	    crequest = 1;
	    name = optstate->value;
	    break;

	  case 's':
	    signer_name = optstate->value;
	    break;

	  case 't':
	    responder_name = optstate->value;
	    break;

	  case 'u':
	    cert_usage_str = optstate->value;
	    break;

	  case 'w':
	    date_str = optstate->value;
	    break;
	}
    }

    PL_DestroyOptState(optstate);

    if ((crequest + dresponse + prequest + presponse + ccert + vcert) != 1) {
	PR_fprintf (PR_STDERR, "%s: must specify exactly one command\n\n",
		    program_name);
	short_usage (program_name);
	return retval;
    }

    if (vcert) {
	if (cert_usage_str == NULL) {
	    PR_fprintf (PR_STDERR, "%s: verification requires cert usage\n\n",
			program_name);
	    short_usage (program_name);
	    return retval;
	}

	rv = cert_usage_from_char (cert_usage_str, &cert_usage);
	if (rv != SECSuccess) {
	    PR_fprintf (PR_STDERR, "%s: invalid cert usage (\"%s\")\n\n",
			program_name, cert_usage_str);
	    long_usage (program_name);
	    return retval;
	}
    }

    if (ccert + vcert) {
	if (responder_url != NULL || responder_name != NULL) {
	    /*
	     * To do a full status check, both the URL and the cert name
	     * of the responder must be specified if either one is.
	     */
	    if (responder_url == NULL || responder_name == NULL) {
		if (responder_url == NULL)
		    PR_fprintf (PR_STDERR,
				"%s: must also specify responder location\n\n",
				program_name);
		else
		    PR_fprintf (PR_STDERR,
				"%s: must also specify responder name\n\n",
				program_name);
		short_usage (program_name);
		return retval;
	    }
	}

	if (date_str != NULL) {
	    rv = DER_AsciiToTime (&verify_time, (char *) date_str);
	    if (rv != SECSuccess) {
		SECU_PrintError (program_name, "error converting time string");
		PR_fprintf (PR_STDERR, "\n");
		long_usage (program_name);
		return retval;
	    }
	} else {
	    verify_time = PR_Now();
	}
    }

    retval = -2;		/* errors change from usage to runtime */

    /*
     * Initialize the NSPR and Security libraries.
     */
    PR_Init (PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
    db_dir = SECU_ConfigDirectory (db_dir);
    rv = NSS_Init (db_dir);
    if (rv != SECSuccess) {
	SECU_PrintError (program_name, "NSS_Init failed");
	goto prdone;
    }
    SECU_RegisterDynamicOids();

    if (prequest + presponse) {
	MAKE_FILE_BINARY(stdin);
	data = read_file_into_item (in_file, siBuffer);
	if (data == NULL) {
	    SECU_PrintError (program_name, "problem reading input");
	    goto nssdone;
	}
    }

    if (crequest + dresponse + presponse + ccert + vcert) {
	handle = CERT_GetDefaultCertDB();
	if (handle == NULL) {
	    SECU_PrintError (program_name, "problem getting certdb handle");
	    goto nssdone;
	}

	/*
	 * It would be fine to do the enable for all of these commands,
	 * but this way we check that everything but an overall verify
	 * can be done without it.  That is, that the individual pieces
	 * work on their own.
	 */
	if (vcert) {
	    rv = CERT_EnableOCSPChecking (handle);
	    if (rv != SECSuccess) {
		SECU_PrintError (program_name, "error enabling OCSP checking");
		goto nssdone;
	    }
	}

	if ((ccert + vcert) && (responder_name != NULL)) {
	    rv = CERT_SetOCSPDefaultResponder (handle, responder_url,
					       responder_name);
	    if (rv != SECSuccess) {
		SECU_PrintError (program_name,
				 "error setting default responder");
		goto nssdone;
	    }

	    rv = CERT_EnableOCSPDefaultResponder (handle);
	    if (rv != SECSuccess) {
		SECU_PrintError (program_name,
				 "error enabling default responder");
		goto nssdone;
	    }
	}
    }

#define NOTYET(opt)							\
	{								\
	    PR_fprintf (PR_STDERR, "%s not yet working\n", opt);	\
	    exit (-1);							\
	}

    if (name) {
        cert = find_certificate(handle, name, ascii);
    }

    if (crequest) {
	if (signer_name != NULL) {
	    NOTYET("-s");
	}
	rv = create_request (out_file, handle, cert, add_service_locator,
			     add_acceptable_responses);
    } else if (dresponse) {
	if (signer_name != NULL) {
	    NOTYET("-s");
	}
	rv = dump_response (out_file, handle, cert, responder_url);
    } else if (prequest) {
	rv = print_request (out_file, data);
    } else if (presponse) {
	rv = print_response (out_file, data, handle);
    } else if (ccert) {
	if (signer_name != NULL) {
	    NOTYET("-s");
	}
	rv = get_cert_status (out_file, handle, cert, name, verify_time);
    } else if (vcert) {
	if (signer_name != NULL) {
	    NOTYET("-s");
	}
	rv = verify_cert (out_file, handle, cert, name, cert_usage, verify_time);
    }

    if (rv != SECSuccess)
	SECU_PrintError (program_name, "error performing requested operation");
    else
	retval = 0;

nssdone:
    if (cert) {
        CERT_DestroyCertificate(cert);
    }

    if (data != NULL) {
	SECITEM_FreeItem (data, PR_TRUE);
    }

    if (handle != NULL) {
 	CERT_DisableOCSPDefaultResponder(handle);        
 	CERT_DisableOCSPChecking (handle);
    }

    if (NSS_Shutdown () != SECSuccess) {
	retval = 1;
    }

prdone:
    PR_Cleanup ();
    return retval;
}
Esempio n. 8
0
int main (int argc, char **argv)
{
    typedef double SBS[2][3][SCALE_BLOCK][SBLIMIT];
    SBS *sb_sample;
    typedef double JSBS[3][SCALE_BLOCK][SBLIMIT];
    JSBS *j_sample;
    typedef double IN[2][HAN_SIZE];
    IN *win_que;
    typedef unsigned int SUB[2][3][SCALE_BLOCK][SBLIMIT];
    SUB *subband;

    frame_info frame;
    frame_header header;
    char original_file_name[MAX_NAME_SIZE];
    char encoded_file_name[MAX_NAME_SIZE];
    short **win_buf;
    static short buffer[2][1152];
    static unsigned int bit_alloc[2][SBLIMIT], scfsi[2][SBLIMIT];
    static unsigned int scalar[2][3][SBLIMIT], j_scale[3][SBLIMIT];
    static double smr[2][SBLIMIT], lgmin[2][SBLIMIT], max_sc[2][SBLIMIT];
    // FLOAT snr32[32];
    short sam[2][1344];		/* was [1056]; */
    int model, nch, error_protection;
    static unsigned int crc;
    int sb, ch, adb;
    unsigned long frameBits, sentBits = 0;
    unsigned long num_samples;
    int lg_frame;
    int i;

    /* Used to keep the SNR values for the fast/quick psy models */
    static FLOAT smrdef[2][32];

    static int psycount = 0;
    extern int minimum;

    sb_sample = (SBS *) mem_alloc (sizeof (SBS), "sb_sample");
    j_sample = (JSBS *) mem_alloc (sizeof (JSBS), "j_sample");
    win_que = (IN *) mem_alloc (sizeof (IN), "Win_que");
    subband = (SUB *) mem_alloc (sizeof (SUB), "subband");
    win_buf = (short **) mem_alloc (sizeof (short *) * 2, "win_buf");

    /* clear buffers */
    memset ((char *) buffer, 0, sizeof (buffer));
    memset ((char *) bit_alloc, 0, sizeof (bit_alloc));
    memset ((char *) scalar, 0, sizeof (scalar));
    memset ((char *) j_scale, 0, sizeof (j_scale));
    memset ((char *) scfsi, 0, sizeof (scfsi));
    memset ((char *) smr, 0, sizeof (smr));
    memset ((char *) lgmin, 0, sizeof (lgmin));
    memset ((char *) max_sc, 0, sizeof (max_sc));
    //memset ((char *) snr32, 0, sizeof (snr32));
    memset ((char *) sam, 0, sizeof (sam));

    global_init ();

    header.extension = 0;
    frame.header = &header;
    frame.tab_num = -1;		/* no table loaded */
    frame.alloc = NULL;
    header.version = MPEG_AUDIO_ID;	/* Default: MPEG-1 */

    programName = argv[0];
    if (argc == 1)		/* no command-line args */
        short_usage ();
    else
        parse_args (argc, argv, &frame, &model, &num_samples, original_file_name,
                    encoded_file_name);
    print_config (&frame, &model, original_file_name, encoded_file_name);

    /* this will load the alloc tables and do some other stuff */
    hdr_to_frps (&frame);
    nch = frame.nch;
    error_protection = header.error_protection;

    while (get_audio (musicin, buffer, num_samples, nch, &header) > 0) {
        if (glopts.verbosity > 1)
            if (++frameNum % 10 == 0)
                fprintf (stderr, "[%4u]\r", frameNum);
        fflush (stderr);
        win_buf[0] = &buffer[0][0];
        win_buf[1] = &buffer[1][0];

        adb = available_bits (&header, &glopts);
        lg_frame = adb / 8;
        if (header.dab_extension) {
            /* in 24 kHz we always have 4 bytes */
            if (header.sampling_frequency == 1)
                header.dab_extension = 4;
            /* You must have one frame in memory if you are in DAB mode                 */
            /* in conformity of the norme ETS 300 401 http://www.etsi.org               */
            /* see bitstream.c            */
            if (frameNum == 1)
                minimum = lg_frame + MINIMUM;
            adb -= header.dab_extension * 8 + header.dab_length * 8 + 16;
        }

        {
            int gr, bl, ch;
            /* New polyphase filter
            Combines windowing and filtering. Ricardo Feb'03 */
            for( gr = 0; gr < 3; gr++ )
                for ( bl = 0; bl < 12; bl++ )
                    for ( ch = 0; ch < nch; ch++ )
                        WindowFilterSubband( &buffer[ch][gr * 12 * 32 + 32 * bl], ch,
                                             &(*sb_sample)[ch][gr][bl][0] );
        }

#ifdef REFERENCECODE
        {
            /* Old code. left here for reference */
            int gr, bl, ch;
            for (gr = 0; gr < 3; gr++)
                for (bl = 0; bl < SCALE_BLOCK; bl++)
                    for (ch = 0; ch < nch; ch++) {
                        window_subband (&win_buf[ch], &(*win_que)[ch][0], ch);
                        filter_subband (&(*win_que)[ch][0], &(*sb_sample)[ch][gr][bl][0]);
                    }
        }
#endif


#ifdef NEWENCODE
        scalefactor_calc_new(*sb_sample, scalar, nch, frame.sblimit);
        find_sf_max (scalar, &frame, max_sc);
        if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
            /* this way we calculate more mono than we need */
            /* but it is cheap */
            combine_LR_new (*sb_sample, *j_sample, frame.sblimit);
            scalefactor_calc_new (j_sample, &j_scale, 1, frame.sblimit);
        }
#else
        scale_factor_calc (*sb_sample, scalar, nch, frame.sblimit);
        pick_scale (scalar, &frame, max_sc);
        if (frame.actual_mode == MPG_MD_JOINT_STEREO) {
            /* this way we calculate more mono than we need */
            /* but it is cheap */
            combine_LR (*sb_sample, *j_sample, frame.sblimit);
            scale_factor_calc (j_sample, &j_scale, 1, frame.sblimit);
        }
#endif



        if ((glopts.quickmode == TRUE) && (++psycount % glopts.quickcount != 0)) {
            /* We're using quick mode, so we're only calculating the model every
               'quickcount' frames. Otherwise, just copy the old ones across */
            for (ch = 0; ch < nch; ch++) {
                for (sb = 0; sb < SBLIMIT; sb++)
                    smr[ch][sb] = smrdef[ch][sb];
            }
        } else {
            /* calculate the psymodel */
            switch (model) {
            case -1:
                psycho_n1 (smr, nch);
                break;
            case 0:	/* Psy Model A */
                psycho_0 (smr, nch, scalar, (FLOAT) s_freq[header.version][header.sampling_frequency] * 1000);
                break;
            case 1:
                psycho_1 (buffer, max_sc, smr, &frame);
                break;
            case 2:
                for (ch = 0; ch < nch; ch++) {
                    psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                }
                break;
            case 3:
                /* Modified psy model 1 */
                psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                break;
            case 4:
                /* Modified Psycho Model 2 */
                for (ch = 0; ch < nch; ch++) {
                    psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                }
                break;
            case 5:
                /* Model 5 comparse model 1 and 3 */
                psycho_1 (buffer, max_sc, smr, &frame);
                fprintf(stdout,"1 ");
                smr_dump(smr,nch);
                psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                fprintf(stdout,"3 ");
                smr_dump(smr,nch);
                break;
            case 6:
                /* Model 6 compares model 2 and 4 */
                for (ch = 0; ch < nch; ch++)
                    psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"2 ");
                smr_dump(smr,nch);
                for (ch = 0; ch < nch; ch++)
                    psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"4 ");
                smr_dump(smr,nch);
                break;
            case 7:
                fprintf(stdout,"Frame: %i\n",frameNum);
                /* Dump the SMRs for all models */
                psycho_1 (buffer, max_sc, smr, &frame);
                fprintf(stdout,"1");
                smr_dump(smr, nch);
                psycho_3 (buffer, max_sc, smr, &frame, &glopts);
                fprintf(stdout,"3");
                smr_dump(smr,nch);
                for (ch = 0; ch < nch; ch++)
                    psycho_2 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], //snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"2");
                smr_dump(smr,nch);
                for (ch = 0; ch < nch; ch++)
                    psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"4");
                smr_dump(smr,nch);
                break;
            case 8:
                /* Compare 0 and 4 */
                psycho_n1 (smr, nch);
                fprintf(stdout,"0");
                smr_dump(smr,nch);

                for (ch = 0; ch < nch; ch++)
                    psycho_4 (&buffer[ch][0], &sam[ch][0], ch, &smr[ch][0], // snr32,
                              (FLOAT) s_freq[header.version][header.sampling_frequency] *
                              1000, &glopts);
                fprintf(stdout,"4");
                smr_dump(smr,nch);
                break;
            default:
                fprintf (stderr, "Invalid psy model specification: %i\n", model);
                exit (0);
            }

            if (glopts.quickmode == TRUE)
                /* copy the smr values and reuse them later */
                for (ch = 0; ch < nch; ch++) {
                    for (sb = 0; sb < SBLIMIT; sb++)
                        smrdef[ch][sb] = smr[ch][sb];
                }

            if (glopts.verbosity > 4)
                smr_dump(smr, nch);




        }

#ifdef NEWENCODE
        sf_transmission_pattern (scalar, scfsi, &frame);
        main_bit_allocation_new (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
        //main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);

        if (error_protection)
            CRC_calc (&frame, bit_alloc, scfsi, &crc);

        write_header (&frame, &bs);
        //encode_info (&frame, &bs);
        if (error_protection)
            putbits (&bs, crc, 16);
        write_bit_alloc (bit_alloc, &frame, &bs);
        //encode_bit_alloc (bit_alloc, &frame, &bs);
        write_scalefactors(bit_alloc, scfsi, scalar, &frame, &bs);
        //encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
        subband_quantization_new (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
                                  *subband, &frame);
        //subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
        //	  *subband, &frame);
        write_samples_new(*subband, bit_alloc, &frame, &bs);
        //sample_encoding (*subband, bit_alloc, &frame, &bs);
#else
        transmission_pattern (scalar, scfsi, &frame);
        main_bit_allocation (smr, scfsi, bit_alloc, &adb, &frame, &glopts);
        if (error_protection)
            CRC_calc (&frame, bit_alloc, scfsi, &crc);
        encode_info (&frame, &bs);
        if (error_protection)
            encode_CRC (crc, &bs);
        encode_bit_alloc (bit_alloc, &frame, &bs);
        encode_scale (bit_alloc, scfsi, scalar, &frame, &bs);
        subband_quantization (scalar, *sb_sample, j_scale, *j_sample, bit_alloc,
                              *subband, &frame);
        sample_encoding (*subband, bit_alloc, &frame, &bs);
#endif


        /* If not all the bits were used, write out a stack of zeros */
        for (i = 0; i < adb; i++)
            put1bit (&bs, 0);
        if (header.dab_extension) {
            /* Reserve some bytes for X-PAD in DAB mode */
            putbits (&bs, 0, header.dab_length * 8);

            for (i = header.dab_extension - 1; i >= 0; i--) {
                CRC_calcDAB (&frame, bit_alloc, scfsi, scalar, &crc, i);
                /* this crc is for the previous frame in DAB mode  */
                if (bs.buf_byte_idx + lg_frame < bs.buf_size)
                    bs.buf[bs.buf_byte_idx + lg_frame] = crc;
                /* reserved 2 bytes for F-PAD in DAB mode  */
                putbits (&bs, crc, 8);
            }
            putbits (&bs, 0, 16);
        }

        frameBits = sstell (&bs) - sentBits;

        if (frameBits % 8) {	/* a program failure */
            fprintf (stderr, "Sent %ld bits = %ld slots plus %ld\n", frameBits,
                     frameBits / 8, frameBits % 8);
            fprintf (stderr, "If you are reading this, the program is broken\n");
            fprintf (stderr, "email [mfc at NOTplanckenerg.com] without the NOT\n");
            fprintf (stderr, "with the command line arguments and other info\n");
            exit (0);
        }

        sentBits += frameBits;
    }

    close_bit_stream_w (&bs);

    if ((glopts.verbosity > 1) && (glopts.vbr == TRUE)) {
        int i;
#ifdef NEWENCODE
        extern int vbrstats_new[15];
#else
        extern int vbrstats[15];
#endif
        fprintf (stdout, "VBR stats:\n");
        for (i = 1; i < 15; i++)
            fprintf (stdout, "%4i ", bitrate[header.version][i]);
        fprintf (stdout, "\n");
        for (i = 1; i < 15; i++)
#ifdef NEWENCODE
            fprintf (stdout,"%4i ",vbrstats_new[i]);
#else
            fprintf (stdout, "%4i ", vbrstats[i]);
#endif
        fprintf (stdout, "\n");
    }

    fprintf (stderr,
             "Avg slots/frame = %.3f; b/smp = %.2f; bitrate = %.3f kbps\n",
             (FLOAT) sentBits / (frameNum * 8),
             (FLOAT) sentBits / (frameNum * 1152),
             (FLOAT) sentBits / (frameNum * 1152) *
             s_freq[header.version][header.sampling_frequency]);

    if (fclose (musicin) != 0) {
        fprintf (stderr, "Could not close \"%s\".\n", original_file_name);
        exit (2);
    }

    fprintf (stderr, "\nDone\n");
    exit (0);
}
Esempio n. 9
0
int
main (int argc, char **argv)
{
    int		 retval = 0;  /* 0 - test succeeded.  -1 - test failed */
    SECStatus	 rv;
    PLOptState	*optstate;
    PLOptStatus  optstatus;
    char	*program_name;
    const char  *input_file = NULL; 	/* read encrypted data from here (or create) */
    const char  *output_file = NULL;	/* write new encrypted data here */
    const char  *value = default_value;	/* Use this for plaintext */
    SECItem     data;
    SECItem     result = {0, 0, 0};
    SECItem     text;
    PRBool      ascii = PR_FALSE;
    secuPWData  pwdata = { PW_NONE, 0 };

    pr_stderr = PR_STDERR;
    result.data = 0;
    text.data = 0; text.len = 0;

    program_name = PL_strrchr(argv[0], '/');
    program_name = program_name ? (program_name + 1) : argv[0];

    optstate = PL_CreateOptState (argc, argv, "?Had:i:o:t:vf:p:");
    if (optstate == NULL) {
	SECU_PrintError (program_name, "PL_CreateOptState failed");
	return -1;
    }

    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
	switch (optstate->option) {
	  case '?':
	    short_usage (program_name);
	    return retval;

	  case 'H':
	    long_usage (program_name);
	    return retval;

	  case 'a':
	    ascii = PR_TRUE;
	    break;

	  case 'd':
	    SECU_ConfigDirectory(optstate->value);
	    break;

          case 'i':
            input_file = optstate->value;
            break;

          case 'o':
            output_file = optstate->value;
            break;

          case 't':
            value = optstate->value;
            break;

	  case 'f':
	    if (pwdata.data) {
		PORT_Free(pwdata.data);
		short_usage(program_name);
		return -1;
	    }
	    pwdata.source = PW_FROMFILE;
	    pwdata.data = PORT_Strdup(optstate->value);
	    break;

	  case 'p':
	    if (pwdata.data) {
		PORT_Free(pwdata.data);
		short_usage(program_name);
		return -1;
	    }
	    pwdata.source = PW_PLAINTEXT;
	    pwdata.data = PORT_Strdup(optstate->value);
	    break;

          case 'v':
            verbose = PR_TRUE;
            break;
	}
    }
    PL_DestroyOptState(optstate);
    if (optstatus == PL_OPT_BAD) {
	short_usage (program_name);
	return -1;
    }
    if (!output_file && !input_file && value == default_value) {
	short_usage (program_name);
	PR_fprintf (pr_stderr, "Must specify at least one of -t, -i or -o \n");
	return -1;
    }

    /*
     * Initialize the Security libraries.
     */
    PK11_SetPasswordFunc(SECU_GetModulePassword);

    if (output_file) {
	rv = NSS_InitReadWrite(SECU_ConfigDirectory(NULL));
    } else {
	rv = NSS_Init(SECU_ConfigDirectory(NULL));
    }
    if (rv != SECSuccess) {
	SECU_PrintError(program_name, "NSS_Init failed");
	retval = -1;
	goto prdone;
    }

    /* Convert value into an item */
    data.data = (unsigned char *)value;
    data.len = strlen(value);

    /* Get the encrypted result, either from the input file
     * or from encrypting the plaintext value
     */
    if (input_file)
    {
      if (verbose) printf("Reading data from %s\n", input_file);

      if (!strcmp(input_file, "-")) {
	retval = readStdin(&result);
        ascii = PR_TRUE;
      } else {
        retval = readInputFile(input_file, &result);
      }
      if (retval != 0) 
	goto loser;
      if (ascii) {
	/* input was base64 encoded.  Decode it. */
	SECItem newResult = {0, 0, 0};
	SECItem *ok = NSSBase64_DecodeBuffer(NULL, &newResult, 
	                       (const char *)result.data, result.len);
	if (!ok) {
	  SECU_PrintError(program_name, "Base 64 decode failed");
	  retval = -1;
	  goto loser;
	}
	SECITEM_ZfreeItem(&result, PR_FALSE);
	result = *ok;
      }
    }
    else
    {
      SECItem keyid = { 0, 0, 0 };
      SECItem outBuf = { 0, 0, 0 };
      PK11SlotInfo *slot = NULL;

      /* sigh, initialize the key database */
      slot = PK11_GetInternalKeySlot();
      if (slot && PK11_NeedUserInit(slot)) {
	switch (pwdata.source) {
	case PW_FROMFILE:
	    rv = SECU_ChangePW(slot, 0, pwdata.data);
	    break;
	case PW_PLAINTEXT:
	    rv = SECU_ChangePW(slot, pwdata.data, 0);
	    break;
	default:
            rv = SECU_ChangePW(slot, "", 0);
	    break;
	}
        if (rv != SECSuccess) {
            SECU_PrintError(program_name, "Failed to initialize slot \"%s\"",
                                    PK11_GetSlotName(slot));
            return SECFailure;
        }
      }
      if (slot) {
	PK11_FreeSlot(slot);
      }

      rv = PK11SDR_Encrypt(&keyid, &data, &result, &pwdata);
      if (rv != SECSuccess) {
        if (verbose) 
	  SECU_PrintError(program_name, "Encrypt operation failed\n");
        retval = -1;
        goto loser;
      }

      if (verbose) printf("Encrypted result is %d bytes long\n", result.len);

      if (!strcmp(output_file, "-")) {
        ascii = PR_TRUE;
      }

      if (ascii) {
      	/* base64 encode output. */
	char * newResult = NSSBase64_EncodeItem(NULL, NULL, 0, &result);
	if (!newResult) {
	  SECU_PrintError(program_name, "Base 64 encode failed\n");
	  retval = -1;
	  goto loser;
	}
	outBuf.data = (unsigned char *)newResult;
	outBuf.len  = strlen(newResult);
	if (verbose) 
	  printf("Base 64 encoded result is %d bytes long\n", outBuf.len);
      } else {
	outBuf = result;
      }

      /* -v printf("Result is %.*s\n", text.len, text.data); */
      if (output_file) {
         PRFileDesc *file;
         PRInt32 count;

         if (verbose) printf("Writing result to %s\n", output_file);
	 if (!strcmp(output_file, "-")) {
	   file = PR_STDOUT;
	 } else {
	   /* Write to file */
	   file = PR_Open(output_file, PR_CREATE_FILE|PR_WRONLY, 0666);
	 }
         if (!file) {
            if (verbose) 
		SECU_PrintError(program_name, 
                                "Open of output file %s failed\n",
                                output_file);
            retval = -1;
            goto loser;
         }

         count = PR_Write(file, outBuf.data, outBuf.len);

	 if (file == PR_STDOUT) {
	   puts("");
	 } else {
	   PR_Close(file);
	 }

         if (count != outBuf.len) {
           if (verbose) SECU_PrintError(program_name, "Write failed\n");
           retval = -1;
           goto loser;
         }
	 if (ascii) {
	   free(outBuf.data);
	 }
      }
    }

    /* Decrypt the value */
    rv = PK11SDR_Decrypt(&result, &text, &pwdata);
    if (rv != SECSuccess) {
      if (verbose) SECU_PrintError(program_name, "Decrypt operation failed\n");
      retval = -1; 
      goto loser;
    }

    if (verbose) printf("Decrypted result is \"%.*s\"\n", text.len, text.data);

    /* Compare to required value */
    if (text.len != data.len || memcmp(data.data, text.data, text.len) != 0)
    {
      if (verbose) PR_fprintf(pr_stderr, "Comparison failed\n");
      retval = -1;
      goto loser;
    }

loser:
    if (text.data) SECITEM_ZfreeItem(&text, PR_FALSE);
    if (result.data) SECITEM_ZfreeItem(&result, PR_FALSE);
    if (NSS_Shutdown() != SECSuccess) {
       exit(1);
    }

prdone:
    PR_Cleanup ();
    if (pwdata.data) {
	PORT_Free(pwdata.data);
    }
    return retval;
}
Esempio n. 10
0
int
main (int argc, char **argv)
{
    int		 retval = 0;  /* 0 - test succeeded.  -1 - test failed */
    SECStatus	 rv;
    PLOptState	*optstate;
    char	*program_name;
    char  *input_file = NULL; 	/* read encrypted data from here (or create) */
    char  *output_file = NULL;	/* write new encrypted data here */
    char  *log_file = NULL;	/* write new encrypted data here */
    FILE	*inFile = stdin;
    FILE	*outFile = stdout;
    FILE	*logFile = NULL;
    PLOptStatus optstatus;
    secuPWData  pwdata = { PW_NONE, NULL };


    program_name = PL_strrchr(argv[0], '/');
    program_name = program_name ? (program_name + 1) : argv[0];

    optstate = PL_CreateOptState (argc, argv, "Hd:f:i:o:l:p:?");
    if (optstate == NULL) {
        SECU_PrintError (program_name, "PL_CreateOptState failed");
        return 1;
    }

    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
        switch (optstate->option) {
        case '?':
            short_usage (program_name);
            return 1;

        case 'H':
            long_usage (program_name);
            return 1;

        case 'd':
            SECU_ConfigDirectory(optstate->value);
            break;

        case 'i':
            input_file = PL_strdup(optstate->value);
            break;

        case 'o':
            output_file = PL_strdup(optstate->value);
            break;

        case 'l':
            log_file = PL_strdup(optstate->value);
            break;

        case 'f':
            pwdata.source = PW_FROMFILE;
            pwdata.data = PL_strdup(optstate->value);
            break;

        case 'p':
            pwdata.source = PW_PLAINTEXT;
            pwdata.data = PL_strdup(optstate->value);
            break;

        }
    }
    PL_DestroyOptState(optstate);
    if (optstatus == PL_OPT_BAD) {
        short_usage (program_name);
        return 1;
    }

    if (input_file) {
        inFile = fopen(input_file,"r");
        if (inFile == NULL) {
            perror(input_file);
            return 1;
        }
        PR_Free(input_file);
    }
    if (output_file) {
        outFile = fopen(output_file,"w+");
        if (outFile == NULL) {
            perror(output_file);
            return 1;
        }
        PR_Free(output_file);
    }
    if (log_file) {
        if (log_file[0] == '-')
            logFile = stderr;
        else
            logFile = fopen(log_file,"w+");
        if (logFile == NULL) {
            perror(log_file);
            return 1;
        }
        PR_Free(log_file);
    }

    /*
     * Initialize the Security libraries.
     */
    PK11_SetPasswordFunc(SECU_GetModulePassword);
    rv = NSS_Init(SECU_ConfigDirectory(NULL));
    if (rv != SECSuccess) {
        SECU_PrintError (program_name, "NSS_Init failed");
        retval = 1;
        goto prdone;
    }

    /* Get the encrypted result, either from the input file
     * or from encrypting the plaintext value
     */
    while (fgets(dataString, sizeof dataString, inFile)) {
        unsigned char c = dataString[0];

        if (c == 'M' && isBase64(dataString)) {
            doDecrypt(dataString, outFile, logFile, &pwdata);
        } else if (c == '~' && isBase64(dataString + 1)) {
            doDecode(dataString, outFile, logFile);
        } else {
            fputs(dataString, outFile);
        }
    }
    if (pwdata.data)
        PR_Free(pwdata.data);

    fclose(outFile);
    fclose(inFile);
    if (logFile && logFile != stderr) {
        fclose(logFile);
    }

    if (NSS_Shutdown() != SECSuccess) {
        SECU_PrintError (program_name, "NSS_Shutdown failed");
        exit(1);
    }

prdone:
    PR_Cleanup ();
    return retval;
}
Esempio n. 11
0
int main(int argc, char * argv[]) {
	int optc;
	int mode = 'g';
	char *url;
	char *token = NULL;
	char *timeout = NULL;
	char *infile = NULL;
	char *owner = NULL;
	char *orig_url = NULL;
	char *content_type = "application/x-www-form-urlencoded";
	char *auth_realm = NULL;
	char *pauth_realm = NULL;
	char *ns = NULL;
	int scope = ND_LOCK_SCOPE_EXCLUSIVE;
	int infinite = 0;
	int force_overwrite = 0;
	ndAuthCtxtPtr auth;

	/* OMIT xml errors. */
	xmlSetGenericErrorFunc(NULL, null_error_handler);

	while ( (optc = getopt_long(argc, argv, optstring, long_options, NULL))
			!= -1 )
	{
		switch (optc) {
			case 'c':	mode = 'c';
						orig_url = optarg;
						break;
			case 'm':	mode = 'm';
						orig_url = optarg;
						break;
			case 'e':	mode = 'e';
						{
							char *value = optarg;
							ndPropPtr prp = ndPropNew();

							/* Locate the equal sign. */
							while (*value && (*value != '='))
								value++;

							/* Capture assigned value,
							 * and crop property name. */
							if (*value)
								*value++ = '\0';

							prp->name = strdup(optarg);
							prp->ns = ns ? strdup(ns) : NULL;
							prp->value = *value ? strdup(value)
												: NULL;
							prp->type = *value ? NDPROP_PATCH
											   : NDPROP_REMOVE;
							prp->next = propreq;

							propreq = prp;

						}
						break;
			case 'F':	mode = 'F';
						break;
			case 'g':	mode = 'F';
						{
							ndPropPtr prp = ndPropNew();

							prp->name = strdup(optarg);
							prp->ns = ns ? strdup(ns) : NULL;
							prp->value = NULL;
							prp->type = NDPROP_FIND;
							prp->next = propreq;

							propreq = prp;
						}
						break;
			case 'l':	mode = 'l';
						timeout = "Infinite";
						scope = ND_LOCK_SCOPE_EXCLUSIVE;
						break;
			case 'i':	timeout = optarg;
						break;
			case 'n':	mode = 'n';
						format |= ND_PRINT_NAMEONLY;
						break;
			case 'o':	owner = optarg;
						break;
			case 'd':	mode = 'd';
						break;
			case 'D':	debug = 1;
						break;
			case 'f':	force_overwrite = 1;
						break;
			case 'S':	format |= ND_PRINT_AS_SEXP;
						break;
			case 'q':	format |= ND_PRINT_QUIETLY;
						break;
			case 'v':	format |= ND_PRINT_VERBOSELY;
						break;
			case 'a':	auth_realm = optarg;
						break;
			case 'A':	pauth_realm = optarg;
						break;
			case 'P':	mode = 'P';
						infile = optarg;
						break;
			case 'T':	content_type = optarg;
						break;
			case 'N':	ns = optarg;
						break;
			case 'u':	mode = 'u';
						break;
			case 'k':	mode = 'k';
						break;
			case 'r':	infinite = 1;
						break;
			case 's':	if (! strcmp("shared", optarg))
							scope = ND_LOCK_SCOPE_SHARED;
						break;
			case 't':	token = optarg;
						break;
			case 'p':	mode = 'p';
						infile = optarg;
						break;
			case 'V':	print_version(argv[0]);
						exit(EXIT_SUCCESS);
						break;
			case 'h':	/* Long help. */
						usage(argv[0]);
						exit(EXIT_SUCCESS);
						break;
			case '?':	/* Short help and catch-all. */
			default:	short_usage(argv[0]);
						exit(EXIT_FAILURE);
						break;
		}
	}

	url = argv[optind];

	if (url == NULL) {
		fprintf(stderr, "%s: No target URL was specified.\n",
				basename(argv[0]));
		exit(EXIT_FAILURE);
	}

	auth = ndCreateAuthCtxt(authenticate, auth_notify,
								auth_realm, pauth_realm);
	switch (mode) {
		case 'c':	{
						int code;

						code = ndCopy(orig_url, auth, url,
										force_overwrite, token);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(code) )
							error_exit(format, "COPY failed, `%s'",
										ndReasonPhrase(code));
					};
					break;
		case 'e':	{
						int code;
						ndNodeInfoPtr ret = NULL;

						if (propreq && propreq->next == NULL
								&& propreq->ns == NULL)
							propreq->ns = ns;

						code = ndPropPatch(url, auth, propreq, token, &ret);

						if ( RETURNED_AN_ERROR(code) )
							error_exit(format, "PROPPATCH failed, `%s'",
										ndReasonPhrase(code));
						else
							if (code == 207)
								ndNodeInfoListPrint(stdout, ret, format);
					};
					break;
		case 'm':	{
						int code;

						code = ndMove(orig_url, auth, url,
										force_overwrite, token);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(code) )
							error_exit(format, "MOVE failed, `%s'",
										ndReasonPhrase(code));
					};
					break;
		case 'g':	{
#ifndef WAIT_FOR_END
						char *ct_return = NULL;
						int	code;

						if (token != NULL)
							error_exit(format, "token is not required");

						code = ndGetPrint(url, auth, &ct_return, stdout);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(code) )
							error_exit(format, "GET failed, `%s'",
										ndReasonPhrase(code));
#else /* WAIT_FOR_END */
						xmlBufferPtr buf = NULL;
						int code;
						char *ct_return = NULL;

						if (token != NULL)
							error_exit(format, "token is not required");

						code = ndGet(url, auth, &ct_return, &buf);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(code) )
		
							error_exit(format, "GET failed, `%s'",
										ndReasonPhrase(code));
	
						if ( buf )
							fprintf(stdout, "%s", xmlBufferContent(buf));
#endif /* WAIT_FOR_END */
					};
					break;
		case 'n':
		case 'F':	{
						ndNodeInfoPtr ret = NULL;
						int code;
						int depth;

						if (token != NULL)
							error_exit(format, "token is not required");

						depth = ( url[strlen(url) - 1] == '/' )
									? ( infinite
											? ND_DEPTH_INFINITE
											: ND_DEPTH_1 )
									: ND_DEPTH_0;

						if (propreq && propreq->next == NULL
								&& propreq->ns == NULL)
							propreq->ns = ns;

						code = ndPropFind(url, auth, propreq, depth,
										  (mode == 'n') ? 1 : 0,
										  &ret);
						ndFreeAuthCtxt(auth);
	
						if (ret)
							ndNodeInfoListPrint(stdout, ret, format);
						else
							error_exit(format, "PROPFIND failed, `%s'",
										ndReasonPhrase(code));
					};
					break;
		case 'l':	{
						ndLockInfoPtr lock = NULL;
						int code;
						int depth;

						depth = ( url[strlen(url) - 1] == '/' )
									? ( infinite
											? ND_DEPTH_INFINITE
											: ND_DEPTH_1 )
									: ND_DEPTH_0;

						code = ndLock(url, auth, depth,
									owner ? owner : getenv("USER"),
									scope, timeout, &lock);
						ndFreeAuthCtxt(auth);

						if (lock) {
							ndLockInfoPrint(stdout, lock, format);
						} else
							error_exit(format, "LOCK failed, `%s'",
										ndReasonPhrase(code));
					};
					break;
		case 'u':	{
						int ret;
						int depth;

						if (token == NULL)
							error_exit(format, "token is required");
		
						depth = ( url[strlen(url) - 1] == '/' )
									? ( infinite
											? ND_DEPTH_INFINITE
											: ND_DEPTH_1 )
									: ND_DEPTH_0;

						ret = ndUnlock(url, auth, depth, token);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(ret) )
							error_exit(format, "UNLOCK failed, `%s'",
										ndReasonPhrase(ret));
					};
					break;
		case 'P':	{
						xmlBufferPtr buf = xmlBufferCreate();
						int len;
						unsigned char s [1024];
						int ret;
						FILE *fp;

						if ((fp = fopen(infile, "r")) == NULL)
							error_exit(format, "%s, %s", infile,
										strerror(errno));

						while ( (len = fread(s, sizeof(unsigned char),
												sizeof(s), fp))
									> 0)
							xmlBufferAdd(buf, s, len);

						fclose(fp);
						ret = ndPostPrint(url, auth,
									(char *) xmlBufferContent(buf),
									xmlBufferLength(buf),
									&content_type, stdout);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(ret) )
							error_exit(format, "POST failed, `%s'",
										ndReasonPhrase(ret));
					};
					break;
		case 'k':	{
						int ret;

						ret = ndMkCol(url, auth, token);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(ret) )
							error_exit(format, "MKCOL failed, `%s'",
										ndReasonPhrase(ret));
					};
					break;
		case 'd':	{
						int ret;

						ret = ndDelete(url, auth, token);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(ret) )
							error_exit(format, "DELETE failed, `%s'",
										ndReasonPhrase(ret));
					};
					break;
		case 'p':	{
						int len;
						unsigned char s [1024];
						int code;
						FILE *fp;
						ndNodeInfoPtr ret = NULL;
						xmlBufferPtr buf = xmlBufferCreate();

						if ((fp = fopen(infile, "r")) == NULL)
							error_exit(format, "%s, %s",
										infile, strerror(errno));

						while ( (len = fread(s, sizeof(unsigned char),
												sizeof(s), fp))
								> 0)
							xmlBufferAdd(buf, s, len);

		
						fclose(fp);
						code = ndPut(url, auth,
									(char *) xmlBufferContent(buf),
									xmlBufferLength(buf), token, &ret);
						ndFreeAuthCtxt(auth);

						if ( RETURNED_AN_ERROR(code) )
							error_exit(format, "PUT failed, `%s'",
									ndReasonPhrase(code));
	
						else
							ndNodeInfoListPrint(stdout, ret, format);
					};
					break;
	};
	return 0;
}; /* main(int, char * []) */
Esempio n. 12
0
int main(int argc, char *argv[])
{
    int append=0;
    int create=0;
    int slim=0;
    int sanitize=0;
    int long_usage_bool=0;
    int pass_prompt=0;
    int projection = PROJ_SPHERE_MERC;
    int expire_tiles_zoom = -1;
    int expire_tiles_zoom_min = -1;
    int enable_hstore = HSTORE_NONE;
    int hstore_match_only = 0;
    int enable_multi = 0;
    int parallel_indexing = 1;
    int flat_node_cache_enabled = 0;
#ifdef __amd64__
    int alloc_chunkwise = ALLOC_SPARSE | ALLOC_DENSE;
#else
    int alloc_chunkwise = ALLOC_SPARSE;
#endif
    int num_procs = 1;
    int droptemp = 0;
    int unlogged = 0;
    int excludepoly = 0;
    time_t start, end;
    time_t overall_start, overall_end;
    time_t now;
    time_t end_nodes;
    time_t end_way;
    time_t end_rel;
    const char *expire_tiles_filename = "dirty_tiles";
    const char *db = "gis";
    const char *username=NULL;
    const char *host=NULL;
    const char *password=NULL;
    const char *port = "5432";
    const char *tblsmain_index = NULL; /* no default TABLESPACE for index on main tables */
    const char *tblsmain_data = NULL;  /* no default TABLESPACE for main tables */
    const char *tblsslim_index = NULL; /* no default TABLESPACE for index on slim mode tables */
    const char *tblsslim_data = NULL;  /* no default TABLESPACE for slim mode tables */
    const char *conninfo = NULL;
    const char *prefix = "planet_osm";
    const char *style = OSM2PGSQL_DATADIR "/default.style";
    const char *temparg;
    const char *output_backend = "pgsql";
    const char *input_reader = "auto";
    const char **hstore_columns = NULL;
    const char *flat_nodes_file = NULL;
    int n_hstore_columns = 0;
    int keep_coastlines=0;
    int cache = 800;
    struct output_options options;
    PGconn *sql_conn;
    
    int (*streamFile)(char *, int, struct osmdata_t *);

    printf("osm2pgsql SVN version %s (%lubit id space)\n\n", VERSION, 8 * sizeof(osmid_t));

    while (1) {
        int c, option_index = 0;
        static struct option long_options[] = {
            {"append",   0, 0, 'a'},
            {"bbox",     1, 0, 'b'},
            {"create",   0, 0, 'c'},
            {"database", 1, 0, 'd'},
            {"latlong",  0, 0, 'l'},
            {"verbose",  0, 0, 'v'},
            {"slim",     0, 0, 's'},
            {"prefix",   1, 0, 'p'},
            {"proj",     1, 0, 'E'},
            {"merc",     0, 0, 'm'},
            {"oldmerc",  0, 0, 'M'},
            {"utf8-sanitize", 0, 0, 'u'},
            {"cache",    1, 0, 'C'},
            {"username", 1, 0, 'U'},
            {"password", 0, 0, 'W'},
            {"host",     1, 0, 'H'},
            {"port",     1, 0, 'P'},
            {"tablespace-index", 1, 0, 'i'},
            {"tablespace-slim-data", 1, 0, 200},
            {"tablespace-slim-index", 1, 0, 201},
            {"tablespace-main-data", 1, 0, 202},
            {"tablespace-main-index", 1, 0, 203},
            {"help",     0, 0, 'h'},
            {"style",    1, 0, 'S'},
            {"expire-tiles", 1, 0, 'e'},
            {"expire-output", 1, 0, 'o'},
            {"output",   1, 0, 'O'},
            {"extra-attributes", 0, 0, 'x'},
            {"hstore", 0, 0, 'k'},
            {"hstore-all", 0, 0, 'j'},
            {"hstore-column", 1, 0, 'z'},
            {"hstore-match-only", 0, 0, 208},
            {"multi-geometry", 0, 0, 'G'},
            {"keep-coastlines", 0, 0, 'K'},
            {"input-reader", 1, 0, 'r'},
            {"version", 0, 0, 'V'},
            {"disable-parallel-indexing", 0, 0, 'I'},
            {"cache-strategy", 1, 0, 204},
            {"number-processes", 1, 0, 205},
            {"drop", 0, 0, 206},
            {"unlogged", 0, 0, 207},
            {"flat-nodes",1,0,209},
            {"exclude-invalid-polygon",0,0,210},
            {0, 0, 0, 0}
        };

        c = getopt_long (argc, argv, "ab:cd:KhlmMp:suvU:WH:P:i:IE:C:S:e:o:O:xkjGz:r:V", long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
            case 'a': append=1;   break;
            case 'b': osmdata.bbox=optarg; break;
            case 'c': create=1;   break;
            case 'v': verbose=1;  break;
            case 's': slim=1;     break;
            case 'K': keep_coastlines=1;     break;
            case 'u': sanitize=1; break;
            case 'l': projection=PROJ_LATLONG;  break;
            case 'm': projection=PROJ_SPHERE_MERC; break;
            case 'M': projection=PROJ_MERC; break;
            case 'E': projection=-atoi(optarg); break;
            case 'p': prefix=optarg; break;
            case 'd': db=optarg;  break;
            case 'C': cache = atoi(optarg); break;
            case 'U': username=optarg; break;
            case 'W': pass_prompt=1; break;
            case 'H': host=optarg; break;
            case 'P': port=optarg; break;
            case 'S': style=optarg; break;
            case 'i': tblsmain_index=tblsslim_index=optarg; break;
            case 200: tblsslim_data=optarg; break;    
            case 201: tblsslim_index=optarg; break;    
            case 202: tblsmain_data=optarg; break;    
            case 203: tblsmain_index=optarg; break;    
            case 'e':
                expire_tiles_zoom_min = atoi(optarg);
                temparg = strchr(optarg, '-');
                if (temparg) expire_tiles_zoom = atoi(temparg + 1);
                if (expire_tiles_zoom < expire_tiles_zoom_min) expire_tiles_zoom = expire_tiles_zoom_min;
                break;
            case 'o': expire_tiles_filename=optarg; break;
            case 'O': output_backend = optarg; break;
            case 'x': osmdata.extra_attributes=1; break;
            case 'k': enable_hstore=HSTORE_NORM; break;
            case 208: hstore_match_only = 1; break;
            case 'j': enable_hstore=HSTORE_ALL; break;
            case 'z': 
                n_hstore_columns++;
                hstore_columns = (const char**)realloc(hstore_columns, sizeof(&n_hstore_columns) * n_hstore_columns);
                hstore_columns[n_hstore_columns-1] = optarg;
                break;
            case 'G': enable_multi=1; break;
            case 'r': input_reader = optarg; break;
            case 'h': long_usage_bool=1; break;
            case 'I': 
#ifdef HAVE_PTHREAD
                parallel_indexing=0; 
#endif
                break;
            case 204:
                if (strcmp(optarg,"dense") == 0) alloc_chunkwise = ALLOC_DENSE;
                if (strcmp(optarg,"chunk") == 0) alloc_chunkwise = ALLOC_DENSE | ALLOC_DENSE_CHUNK;
                if (strcmp(optarg,"sparse") == 0) alloc_chunkwise = ALLOC_SPARSE;
                if (strcmp(optarg,"optimized") == 0) alloc_chunkwise = ALLOC_DENSE | ALLOC_SPARSE;
                break;
            case 205: num_procs = atoi(optarg); break;
            case 206: droptemp = 1; break;
            case 207: unlogged = 1; break;
            case 209:
            	flat_node_cache_enabled = 1;
            	flat_nodes_file = optarg;
            	break;
            case 210: excludepoly = 1; exclude_broken_polygon(); break;
            case 'V': exit(EXIT_SUCCESS);
            case '?':
            default:
                short_usage(argv[0]);
                exit(EXIT_FAILURE);
        }
    }

    if (long_usage_bool) {
        long_usage(argv[0]);
        exit(EXIT_SUCCESS);
    }

    if (argc == optind) {  /* No non-switch arguments */
        short_usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    if (append && create) {
        fprintf(stderr, "Error: --append and --create options can not be used at the same time!\n");
        exit(EXIT_FAILURE);
    }

    if (droptemp && !slim) {
        fprintf(stderr, "Error: --drop only makes sense with --slim.\n");
        exit(EXIT_FAILURE);
    }

    if (unlogged && !create) {
        fprintf(stderr, "Warning: --unlogged only makes sense with --create; ignored.\n");
        unlogged = 0;
    }

    if (enable_hstore == HSTORE_NONE && !n_hstore_columns && hstore_match_only)
    {
        fprintf(stderr, "Warning: --hstore-match-only only makes sense with --hstore, --hstore-all, or --hstore-column; ignored.\n");
        hstore_match_only = 0;
    }

    if (cache < 0) cache = 0;

    if (num_procs < 1) num_procs = 1;

    if (pass_prompt)
        password = simple_prompt("Password:"******"PGPASS");
    }	

    conninfo = build_conninfo(db, username, password, host, port);
    sql_conn = PQconnectdb(conninfo);
    if (PQstatus(sql_conn) != CONNECTION_OK) {
        fprintf(stderr, "Error: Connection to database failed: %s\n", PQerrorMessage(sql_conn));
        exit(EXIT_FAILURE);
    }
    if (unlogged && PQserverVersion(sql_conn) < 90100) {
        fprintf(stderr, "Error: --unlogged works only with PostgreSQL 9.1 and above, but\n");
        fprintf(stderr, "you are using PostgreSQL %d.%d.%d.\n", PQserverVersion(sql_conn) / 10000, (PQserverVersion(sql_conn) / 100) % 100, PQserverVersion(sql_conn) % 100);
        exit(EXIT_FAILURE);
    }

    PQfinish(sql_conn);

    text_init();
    initList(&osmdata.tags);

    osmdata.count_node = osmdata.max_node = 0;
    osmdata.count_way  = osmdata.max_way  = 0;
    osmdata.count_rel  = osmdata.max_rel  = 0;

    LIBXML_TEST_VERSION

    project_init(projection);
    fprintf(stderr, "Using projection SRS %d (%s)\n", 
        project_getprojinfo()->srs, project_getprojinfo()->descr );

    if (parse_bbox(&osmdata))
        return 1;

    options.conninfo = conninfo;
    options.prefix = prefix;
    options.append = append;
    options.slim = slim;
    options.projection = project_getprojinfo()->srs;
    options.scale = (projection==PROJ_LATLONG)?10000000:100;
    options.mid = slim ? &mid_pgsql : &mid_ram;
    options.cache = cache;
    options.style = style;
    options.tblsmain_index = tblsmain_index;
    options.tblsmain_data = tblsmain_data;
    options.tblsslim_index = tblsslim_index;
    options.tblsslim_data = tblsslim_data;
    options.expire_tiles_zoom = expire_tiles_zoom;
    options.expire_tiles_zoom_min = expire_tiles_zoom_min;
    options.expire_tiles_filename = expire_tiles_filename;
    options.enable_multi = enable_multi;
    options.enable_hstore = enable_hstore;
    options.hstore_match_only = hstore_match_only;
    options.hstore_columns = hstore_columns;
    options.n_hstore_columns = n_hstore_columns;
    options.keep_coastlines = keep_coastlines;
    options.parallel_indexing = parallel_indexing;
    options.alloc_chunkwise = alloc_chunkwise;
    options.num_procs = num_procs;
    options.droptemp = droptemp;
    options.unlogged = unlogged;
    options.flat_node_cache_enabled = flat_node_cache_enabled;
    options.flat_node_file = flat_nodes_file;
    options.excludepoly = excludepoly;

    if (strcmp("pgsql", output_backend) == 0) {
      osmdata.out = &out_pgsql;
    } else if (strcmp("gazetteer", output_backend) == 0) {
      osmdata.out = &out_gazetteer;
    } else if (strcmp("null", output_backend) == 0) {
      osmdata.out = &out_null;
    } else {
      fprintf(stderr, "Output backend `%s' not recognised. Should be one of [pgsql, gazetteer, null].\n", output_backend);
      exit(EXIT_FAILURE);
    }
    options.out = osmdata.out;

    if (strcmp("auto", input_reader) != 0) {
      if (strcmp("libxml2", input_reader) == 0) {
        streamFile = &streamFileXML2;
      } else if (strcmp("primitive", input_reader) == 0) {
        streamFile = &streamFilePrimitive;
#ifdef BUILD_READER_PBF
      } else if (strcmp("pbf", input_reader) == 0) {
        streamFile = &streamFilePbf;
#endif
      } else if (strcmp("o5m", input_reader) == 0) {
          streamFile = &streamFileO5m;
      } else {
        fprintf(stderr, "Input parser `%s' not recognised. Should be one of [libxml2, primitive, o5m"
#ifdef BUILD_READER_PBF
	      ", pbf"
#endif
	      "].\n", input_reader);
      exit(EXIT_FAILURE);
      }
    }

    time(&overall_start);
    osmdata.out->start(&options);

    realloc_nodes(&osmdata);
    realloc_members(&osmdata);

    if (sizeof(int*) == 4 && options.slim != 1) {
        fprintf(stderr, "\n!! You are running this on 32bit system, so at most\n");
        fprintf(stderr, "!! 3GB of RAM can be used. If you encounter unexpected\n");
        fprintf(stderr, "!! exceptions during import, you should try running in slim\n");
        fprintf(stderr, "!! mode using parameter -s.\n");
    }

    while (optind < argc) {
        /* if input_reader is not forced by -r switch try to auto-detect it
           by file extension */
        if (strcmp("auto", input_reader) == 0) {
#ifdef BUILD_READER_PBF
          if (strcasecmp(".pbf",argv[optind]+strlen(argv[optind])-4) == 0) {
            streamFile = &streamFilePbf;
          } else if (strcasecmp(".o5m",argv[optind]+strlen(argv[optind])-4) == 0) {
              streamFile = &streamFileO5m;
          } else {
            streamFile = &streamFileXML2;
          }
#else
          streamFile = &streamFileXML2;
#endif
        }
        fprintf(stderr, "\nReading in file: %s\n", argv[optind]);
        time(&start);
        if (streamFile(argv[optind], sanitize, &osmdata) != 0)
            exit_nicely();
        time(&end);
        fprintf(stderr, "  parse time: %ds\n", (int)(end - start));
        optind++;
    }

    xmlCleanupParser();
    xmlMemoryDump();
    
    if (osmdata.count_node || osmdata.count_way || osmdata.count_rel) {
        time(&now);
        end_nodes = osmdata.start_way > 0 ? osmdata.start_way : now;
        end_way = osmdata.start_rel > 0 ? osmdata.start_rel : now;
        end_rel =  now;
        fprintf(stderr, "\n");
        fprintf(stderr, "Node stats: total(%" PRIdOSMID "), max(%" PRIdOSMID ") in %is\n", osmdata.count_node, osmdata.max_node,
                osmdata.count_node > 0 ? (int)(end_nodes - osmdata.start_node) : 0);
        fprintf(stderr, "Way stats: total(%" PRIdOSMID "), max(%" PRIdOSMID ") in %is\n", osmdata.count_way, osmdata.max_way,
                osmdata.count_way > 0 ? (int)(end_way - osmdata.start_way) : 0);
        fprintf(stderr, "Relation stats: total(%" PRIdOSMID "), max(%" PRIdOSMID ") in %is\n", osmdata.count_rel, osmdata.max_rel,
                osmdata.count_rel > 0 ? (int)(end_rel - osmdata.start_rel) : 0);
    }
    osmdata.out->stop();
    
    free(osmdata.nds);
    free(osmdata.members);
    
    /* free the column pointer buffer */
    free(hstore_columns);

    project_exit();
    text_exit();
    fprintf(stderr, "\n");
    time(&overall_end);
    fprintf(stderr, "Osm2pgsql took %ds overall\n", (int)(overall_end - overall_start));

    return 0;
}
Esempio n. 13
0
/**********************************************************************
 *
 *	main
 */
int main(REG1 int argc, char* argv[])
{
	REG2 int	optind;
	REG3 char*	s;
	REG4 int	len;
	int		nfile;
	char**		envargs;
	
#ifdef PROFILE
	prof_start(argv[0]);
#endif
	if (argc == 1)
		short_usage();
	alloc_buffer();	/* Allocate I/O buffer */
	s = getenv("DGREP");
	if (s != NULL) {
		envargs = malloc(sizeof(argv[0]) * (argc + 2));
		if (envargs == NULL)
			error("Out of memory", 2);
		envargs[0] = argv[0];
		envargs[1] = s;
		memcpy(&envargs[2], &argv[1], sizeof(argv[0]) * argc);
		argv = envargs;
	}
	optind = get_args(argc, argv);
	if (optind >= argc && !expr)
		short_usage();
	if (!expr)
		expr = argv[optind++];
#ifdef BSD
	/* It is faster to count lines with dfa in Bsd. (Bsd doesn't
	   have system memchr, and my version is too slow?) In PC
	   current method is faster. I don't know about other systems. */
	if (number)
		no_boymoore = TRUE;
#endif
	len = strlen(expr);
	if (exact && (no_boymoore || len > MAXREGMUST)) {
		/* Bm can handle only MAXREGMUST long patterns with
		   N+M worst case. So we verify match with reg_exec,
		   which does it in N+M time. (We use bm with the
		   regmust pattern from reg_comp.) */
		expr = quote_expr(expr, len);
		exact = FALSE;
	}
	if (!exact) {
		char eolbuf[2];
		eolbuf[0] = EOL1;
		eolbuf[1] = EOL2;
		s = reg_comp_eol(expr, eolbuf, NEOL(eolbuf), type_bits);
		if (s != NULL)
			error(s, 2);
	}
	if (!no_boymoore)
		regmust = exact ? expr : rbuf.regmust;
	if (regmust) {
		if (ignorecase) {
			str_toupper(regmust);
			str_toupper(expr);
		}
		regmust_only = (strcmp(regmust,expr) == 0);
		regmustlen = strlen(regmust);
		if (!regmust_only && regmustlen <= 1)
			regmust = NULL;
		else
			gosper(regmust, regmustlen, ignorecase);
	}
	use_normal = nonmatch || (regmust && number);
	nfile = argc - optind;
	if (nfile > 0) {
		if (block_mode)
			show_fname = BLOCK;
		else
			show_fname = BEFORE_LINE;
		while (optind < argc)
			dgrep_file(argv[optind++]);
	} else
		dgrep(fileno(stdin));
#ifdef TEST
	if (debug) {
		printf("maxbuf = %d\n", maxbuf);
		show_dfa_report();
		show_boyer_moore_report();
		fflush(stdout);
	}
#endif
	return (file_not_found || touch_failed) ? 2 : was_match ? 0 : 1;
}
Esempio n. 14
0
int main(int argc, char **argv)
{
    int c;
    char *c_flag=NULL;
    
    int option_index;
    static struct option long_options[] =
    {
	{ "no-newline",   0, 0, 'l'},	/* No newline */
	{ "no-output",    0, 0, 'n'},	/* No output */
	{ "test-only",    0, 0, 't'},	/* No output */

	{ "verbose",      0, 0, 'v'},	/* More verbose */
	{ "help",         0, 0, 'h'},	/* Help */
	{ "config",       1, 0, 'c'},	/* Config file */
	{ 0,              0, 0, 0  }
    };

    /* Init configuration */
    cf_initialize();


    while ((c = getopt_long(argc, argv, "lntvhc:",
			    long_options, &option_index     )) != EOF)
	switch (c) {
	/***** ftnconfig options *****/
        case 'l':
	    l_flag = TRUE;
            break;
        case 'n':
	    n_flag = TRUE;
            break;
        case 't':
	    t_flag = TRUE;
	    n_flag = TRUE;
            break;
	    
	/***** Common options *****/
	case 'v':
	    verbose++;
	    break;
	case 'h':
	    usage();
	    exit(0);
	    break;
	case 'c':
	    c_flag = optarg;
	    break;
	default:
	    short_usage();
	    exit(EX_USAGE);
	    break;
	}

    /*
     * Read config file
     */
    cf_read_config_file(c_flag ? c_flag : CONFIG);

    cf_debug();

    if(optind != argc-1) 
    {
	    short_usage();
	    exit(EX_USAGE);
    }
    
    exit( do_para(argv[optind]) ? 0 : 1 );
    }
Esempio n. 15
0
File: genplot.c Progetto: JSTLUV/ndt
/**
 * Plot anything else.
 * 
 * @param list A comma seperated list of Web100/Web10G names to
 * 	        plot.
 * @param cnt The number of items in list to plot, starting from the 
 *          start. Shouldn't be larger than the number of items in 'list'
 * @param name The file name to output to (gets .<remport>.xpls
 *          appended to the end).
 * @param snap Allocated storage for Web100 - NULL for Web10G
 * @param log A open Web100/Web10G log file
 * @param agent A Web100 agent - ignored by Web10G should be NULL
 * @param group A Web100 group - ignored by Web10G should be NULL
 * @param func (Can be NULL) A function that expects two integers for
 * 	        its arguments and returns a new integer. The first interger 
 * 			corrosponds to the item in 'list' 0 for the first etc. 
 *          The second the value in the snap. The returned value is 
 *          plotted.
 *          Called once for every list item for ever snap in the log.
 */
void plot_var(char *list, int cnt, char *name, tcp_stat_snap* snap,
              tcp_stat_log* log, tcp_stat_agent* agent, tcp_stat_group* group,
              int(*func)(const int arg, const int value)) {
#if USE_WEB10G
  estats_error* err = NULL;
#endif
  char *varg;
  /*char buf[256];
  web100_var* var;*/
  char varlist[256], lname[256], remport[8];
  char title[256];
  int i, first = 0;
  float x1, x2, y1[32], y2[32];
  FILE *fn;

  /* Write a xplot file out to the requested file.
   * Start by extracting the connection info for the 
   * page title.  Then its a series of line statements
   * with the x1 y1 x2 y2 coordinates.
   */

  memset(lname, 0, 256);

  /* Get the first snap from the log */
#if USE_WEB100
  if ((web100_snap_from_log(snap, log)) != WEB100_ERR_SUCCESS) {
    web100_perror("web100_snap_from_log");
    return;
  }
#elif USE_WEB10G
  if ((err = estats_record_read_data(&snap, log)) != NULL) {
    estats_record_close(&log);
    estats_error_print(stderr, err);
    estats_error_free(&err);
    return;
  }
#endif

  get_title(snap, agent, group, title, remport);

  if (name == NULL) {
    fn = stdout;
    /* XXX writing into a NULL pointer?? */
    // strncpy(name, "Unknown", 7);
    name = "Unknown";
  } else {
    snprintf(lname, sizeof(lname), "%s.%s.xpl", name, remport);
    fn = fopen(lname, "w");
  }

  fprintf(fn, "double double\ntitle\n");
  fprintf(fn, "%s:%s (%s)\n", title, remport, name);
  if ((strncmp(name, "Throughput", 10)) == 0)
    fprintf(fn, "xlabel\nTime\nylabel\nMbits/sec\n");
  else
    fprintf(fn, "xlabel\nTime\nylabel\nKilo Bytes\n");

  x1 = x2 = 0;
  for (i = 0; i < 32; i++) {
    y1[i] = 0;
    y2[i] = 0;
  }
  first = 0;

  for (;;) {
    /* We've already read the first item to use with get_title */
    if (first != 0) {
#if USE_WEB100
      if ((web100_snap_from_log(snap, log)) != WEB100_ERR_SUCCESS) {
#elif USE_WEB10G
      if ((err = estats_record_read_data(&snap, log)) != NULL) {
        estats_error_free(&err);
#endif
        fprintf(fn, "go\n");
        return;
      }
    }
    strncpy(varlist, list, strlen(list) + 1);
    varg = strtok(varlist, ",");
    for (i = 0; i < cnt; i++) {
      if (i == 0) {
        if (first == 0) {
          if (func) {
            x1 = func(
                i,
                checkmz(
                        tcp_stat_read_double(varg, snap, group, agent)));
          } else {
            x1 = checkmz(
                    tcp_stat_read_double(varg, snap, group, agent));
          }
        } else {
          x1 = x2;
        }
      } else {
        if (first == 0) {
          if (func) {
            y1[i - 1] = func(
                i,
                checkmz(
                    tcp_stat_read_double(varg, snap, group, agent)));
          } else {
            y1[i - 1] = checkmz(
                        tcp_stat_read_double(varg, snap, group, agent));
          }
        } else {
          y1[i - 1] = y2[i - 1];
        }
      }
      varg = strtok(NULL, ",");
    }

    first++;
    strncpy(varlist, list, strlen(list) + 1);
    varg = strtok(varlist, ",");
    for (i = 0; i < cnt; i++) {
      if (i == 0) {
        if (func) {
          x2 = func(
              i,
              checkmz(
                  tcp_stat_read_double(varg, snap, group, agent)));
        } else {
          x2 = checkmz(
              tcp_stat_read_double(varg, snap, group, agent));
        }
      } else {
        if (func) {
          y2[i - 1] = func(
              i,
              checkmz(
                  tcp_stat_read_double(varg, snap, group, agent)));
        } else {
          y2[i - 1] = checkmz(
              tcp_stat_read_double(varg, snap, group, agent));
        }
        fprintf(fn, "%s\nline %0.4f %0.4f %0.4f %0.4f\n", color[i - 1],
                x1 / 1000000, y1[i - 1] / 1024, x2 / 1000000,
                y2[i - 1] / 1024);
      }
      varg = strtok(NULL, ",");
    }
#if USE_WEB10G
    estats_val_data_free(&snap);
#endif
  }
  fprintf(fn, "go\n");
}


/**
 * Make a plot CwndTime(%) against total time.
 * 
 * @param name The file name to output to (gets .<remport>.xpls
 *          appended to the end).
 * @param snap Allocated storage for Web100 - NULL for Web10G
 * @param log A open Web100/Web10G log file
 * @param agent A Web100 agent - ignored by Web10G should be NULL
 * @param group A Web100 group - ignored by Web10G should be NULL
 */
void plot_cwndtime(char *name, tcp_stat_snap* snap, tcp_stat_log* log,
                   tcp_stat_agent* agent, tcp_stat_group* group) {
#if USE_WEB10G
  estats_error* err = NULL;
#endif
  double SndLimTimeRwin = 0, SndLimTimeSender = 0;
  char lname[256], remport[8];
  char title[256];
  char* variables[] = { ELAPSED_TIME, "SndLimTimeRwin", TIME_SENDER,
    "SndLimTimeCwnd" };
  int i, first = 0;
  double x1, x2, y1, y2;
  FILE *fn;

  memset(lname, 0, 256);

  /* Get the first snap from the log */
#if USE_WEB100
  if ((web100_snap_from_log(snap, log)) != WEB100_ERR_SUCCESS) {
    web100_perror("web100_snap_from_log");
    return;
  }
#elif USE_WEB10G
  if ((err = estats_record_read_data(&snap, log)) != NULL) {
    estats_record_close(&log);
    estats_error_print(stderr, err);
    estats_error_free(&err);
    return;
  }
#endif

  get_title(snap, agent, group, title, remport);

  if (name == NULL) {
    fn = stdout;
    /* XXX writing into a NULL pointer?? */
    // strncpy(name, "Unknown", 7);
    name = "Unknown";
  } else {
    snprintf(lname, sizeof(lname), "%s.%s.xpl", name, remport);
    fn = fopen(lname, "w");
  }

  fprintf(fn, "double double\ntitle\n");
  fprintf(fn, "%s:%s (%s)\n", title, remport, name);
  fprintf(fn, "xlabel\nTime\nylabel\nPercentage\n");

  x1 = x2 = y1 = y2 = 0;
  first = 0;

  for (;;) {
    /* We've already read the first item to use with get_title */
    if (first != 0) {
#if USE_WEB100
      if ((web100_snap_from_log(snap, log)) != WEB100_ERR_SUCCESS) {
#elif USE_WEB10G
      if ((err = estats_record_read_data(&snap, log)) != NULL) {
        estats_error_free(&err);
#endif
        fprintf(fn, "go\n");
        return;
      }
    }
    for (i = 0; i < 4; i++) {
      if (first == 0) {
        /* Give everything starting values */
        switch (i) {
          case 0: /* ELAPSED_TIME */
            x1 = tcp_stat_read_double(variables[i], snap, group, agent);
          break;
          case 1: /* "SndLimTimeRwin" */
            SndLimTimeRwin = tcp_stat_read_double(variables[i],
                                                    snap, group, agent);
          break;
          case 2: /* "SndLimTimeSender" */
            SndLimTimeSender = tcp_stat_read_double(variables[i],
                                                    snap, group, agent);
          break;
          case 3: /* "SndLimTimeCwnd" */
            y1 = tcp_stat_read_double(variables[i], snap, group, agent);
            y1 = y1 / (SndLimTimeRwin + SndLimTimeSender + y1);
          break;
        }
      } else {
        switch (i) {
          case 0: /* ELAPSED_TIME */
            x1 = x2;
            break;
          case 3: /* "SndLimTimeCwnd" */
            y1 = y2;
            break;
        }
      }
    }

    first++;
    for (i = 0; i < 4; i++) {
      if (i == 0) { /* ELAPSED_TIME */
        x2 = tcp_stat_read_double(variables[i], snap, group, agent);
      } else if (i == 1) { /* "SndLimTimeRwin" */
        SndLimTimeRwin = tcp_stat_read_double(variables[i],
                                                    snap, group, agent);
      } else if (i == 2) { /* "SndLimTimeSender" */
        SndLimTimeSender = tcp_stat_read_double(variables[i],
                                                    snap, group, agent);
      } else { /* "SndLimTimeCwnd" */
        y2 = tcp_stat_read_double(variables[i], snap, group, agent);
        y2 = y2 / (SndLimTimeRwin + SndLimTimeSender + y2);
        fprintf(fn, "%s\nline %0.4f %0.4f %0.4f %0.4f\n", color[i - 1],
                x1 / 1000000, y1, x2 / 1000000, y2);
      }
    }
#if USE_WEB10G
      estats_val_data_free(&snap);
#endif
  }
  fprintf(fn, "go\n");
}

/**
 * 
 * Loop through the supplied list of variables and prints them to
 * stdout as a new row for every snap in the log file.
 * 
 * A function 'func' can be provided to changed how these are printed.
 * 
 * @param varlist A comma seperated list of Web100/Web10G names to
 * 					print.
 * @param snap Allocated storage for Web100 - NULL for Web10G
 * @param log A open Web100/Web10G log file
 * @param agent A Web100 agent - ignored by Web10G should be NULL
 * @param group A Web100 group - ignored by Web10G should be NULL
 * @param func (Can be NULL) A function that expects two integers for
 * 	        its arguments. The first corrosponds to the item in varlist
 *          0 for the first etc. The second the value in the snap.
 *          Called once for every varlist item for ever snap in the log.
 */
void print_var(char *varlist, tcp_stat_snap* snap, tcp_stat_log* log,
               tcp_stat_agent* agent, tcp_stat_group* group,
               void(*func)(const int arg, const int value)) {
  char *varg, savelist[256];
  char title[256], remport[8];
  int i, j;
#if USE_WEB10G
  estats_error* err = NULL;
#endif
  FILE* fn;

  fn = stdout;

  /* Get the first snap from the log */
#if USE_WEB100
  if ((web100_snap_from_log(snap, log)) != WEB100_ERR_SUCCESS) {
    web100_perror("web100_snap_from_log");
    return;
  }
#elif USE_WEB10G
  if ((err = estats_record_read_data(&snap, log)) != NULL) {
    estats_record_close(&log);
    estats_error_print(stderr, err);
    estats_error_free(&err);
    return;
  }
#endif

  get_title(snap, agent, group, title, remport);
  fprintf(fn, "Extracting Data from %s:%s connection\n\n", title, remport);

  strncpy(savelist, varlist, strlen(varlist) + 1);
  printf("Index\t");
  varg = strtok(varlist, ",");
  /* Loop through varlist and print them out as the column names */
  for (j = 0;; j++) {
    if (varg == NULL)
      break;
    if (func) {
      func(-1, j);
    } else {
      printf("%10s\t", varg);
    }
    varg = strtok(NULL, ",");
  }
  printf("\n");
  /* Loop over the log file */
  for (i = 0;; i++) {
    /* We've already read the first item to use with get_title */
    if (i != 0) {
#if USE_WEB100
      if ((web100_snap_from_log(snap, log)) != WEB100_ERR_SUCCESS) {
#elif USE_WEB10G
      if ((err = estats_record_read_data(&snap, log)) != NULL) {
        estats_error_free(&err);
#endif
        printf("-------------- End Of Data  --------------\n\n");
        return;
      }
    }
    printf("%5d\t", i);
    strncpy(varlist, savelist, strlen(savelist) + 1);
    varg = strtok(varlist, ",");
    /* Loop over the vars we are printing out */
    for (j = 0;; j++) {
      if (varg == NULL) {
        break;
      }
      if (func) {
        /* Let the provided function do the printing */
        func(j, (int) tcp_stat_read_double(varg, snap, group, agent));
      } else {
        /* Do it ourself */
        double value = tcp_stat_read_double(varg, snap, group, agent);
        // weird magic number ((2^32) - 1) - 919
        if ((int) value == 4294966376) {
          printf("%10s\t", "-1");
        } else {
          printf("%10"PRId64"\t", (int64_t) value);
        }
      }
      varg = strtok(NULL, ",");
    }
#if USE_WEB10G
    estats_val_data_free(&snap);
#endif
    printf("\n");
  }
}

/* workers */
void throughput(const int arg, const int value) {
  static int duration;

  if (arg == -1) {
    if (value) {
      printf("%10s\t", "Throughput (mB/s)");
    } else {
      printf("%10s\t", ELAPSED_TIME);
    }
    return;
  }

  if (!arg) { /* duration */
    duration = value;
    printf("%10d\t", value);
  } else { /* DataBytesOut */
    printf(
        "%10.2f",
        ((8 * ((double) value)) / ((double) duration)) * 1000000.0
        / 1024.0 / 1024.0);
  }
}

int throughputPlot(const int arg, const int value) {
  static int duration;

  if (!arg) { /* duration */
    duration = value;
    return value;
  } else { /* DataBytesOut */
    return (((double) value) / ((double) duration)) * 1000000.0;
  }
}

void cwndtime(const int arg, const int value) {
  static int SndLimTimeRwin, SndLimTimeSender;

  if (arg == -1) {
    if (value == 0) {
      printf("%10s\t", ELAPSED_TIME);
    } else if (value == 3) {
      printf("%10s\t", "CwndTime (%% of total time)");
    }
    return;
  }

  if (arg == 0) { /* duration */
    printf("%10d\t", value);
  } else if (arg == 1) { /* SndLimTimeRwin */
    SndLimTimeRwin = value;
  } else if (arg == 2) { /* SndLimTimeSender */
    SndLimTimeSender = value;
  } else { /* SndLimTimeCwnd */
    printf(
        "%10.2f",
        ((double) value)
        / (((double) SndLimTimeRwin)
           + ((double) SndLimTimeSender) + ((double) value)));
  }
}

/* --- */

int main(int argc, char** argv) {
  tcp_stat_agent* agent = NULL;
  tcp_stat_connection conn = NULL;
  tcp_stat_group* group = NULL;
  tcp_stat_log* log = NULL;
  tcp_stat_snap* snap = NULL;
#if USE_WEB10G
  estats_error* err = NULL;
#endif
  char fn[128];
  char *varlist = NULL, list[1024];
  char *varg;
  int j, c, plotspd = 0, plotuser = 0;
  int plotboth = 0, plotcwnd = 0, plotrwin = 0;
  int plotcwndtime = 0;
  int k, txt = 0;

  while ((c = getopt_long(argc, argv, "hCScRbtm:v", long_options, 0)) != -1) {
    switch (c) {
      case 'b':
        plotboth = 1;
        break;
      case 'h':
        genplot_long_usage("ANL/Internet2 NDT version " VERSION " (genplot)",
                           argv[0]);
        break;
      case 'v':
        printf("ANL/Internet2 NDT version %s (genplot)\n", VERSION);
        exit(0);
        break;
      case 't':
        txt = 1;
        break;
      case 'C':
        plotcwnd = 1;
        break;
      case 'R':
        plotrwin = 1;
        break;
      case 'S':
        plotspd = 1;
        break;
      case 'c':
        plotcwndtime = 1;
        break;
      case 'm':
        varlist = optarg;
        plotuser = 1;
        break;
    }
  }

  if (optind == argc) {
    short_usage(argv[0], "Missing snaplog file");
  }

  if (argc == 1) {
    short_usage(argv[0], "ANL/Internet2 NDT version " VERSION " (genplot)");
  }

  for (j = optind; j < argc; j++) {
    snprintf(fn, sizeof(fn), "%s", argv[j]);
#if USE_WEB100
    if ((log = web100_log_open_read(fn)) == NULL) {
      web100_perror("web100_log_open_read");
      exit(EXIT_FAILURE);
    }

    if ((agent = web100_get_log_agent(log)) == NULL) {
      web100_perror("web100_get_log_agent");
      exit(EXIT_FAILURE);
    }

    if ((group = web100_get_log_group(log)) == NULL) {
      web100_perror("web100_get_log_group");
      exit(EXIT_FAILURE);
    }

    if ((conn = web100_get_log_connection(log)) == NULL) {
      web100_perror("web100_get_log_connection");
      exit(EXIT_FAILURE);
    }
#elif USE_WEB10G
    if ((err = estats_record_open(&log, fn, "r")) != NULL) {
      estats_error_print(stderr, err);
      estats_error_free(&err);
      exit(EXIT_FAILURE);
    }
#endif
    fprintf(stderr, "Extracting data from Snaplog '%s'\n\n", fn);

#if USE_WEB100
    if ((snap = web100_snapshot_alloc_from_log(log)) == NULL) {
      web100_perror("web100_snapshot_alloc_from_log");
      exit(EXIT_FAILURE);
    }
#endif

    if (plotuser == 1) {
      memset(list, 0, 1024);
      strncpy(list, ELAPSED_TIME",", 1024);
      strncat(list, varlist, strlen(varlist));
      varg = strtok(list, ",");
      for (k = 1;; k++) {
        if ((varg = strtok(NULL, ",")) == NULL)
          break;
      }
      memset(list, 0, 1024);
      strncpy(list, ELAPSED_TIME",", 1024);
      strncat(list, varlist, strlen(varlist));
      if (txt == 1)
        print_var(list, snap, log, agent, group, NULL);
      else
        plot_var(list, k, "User Defined", snap, log, agent, group,
                 NULL);
    }
    if (plotspd == 1) {
      memset(list, 0, 1024);
      strncpy(list, ELAPSED_TIME","DATA_OCT_OUT, 1024);
      if (txt == 1)
        print_var(list, snap, log, agent, group, throughput);
      else
        plot_var(list, 2, "Throughput", snap, log, agent, group,
                 throughputPlot);
    }
    if (plotcwndtime == 1) {
      memset(list, 0, 1024);
      strncpy(list,
              ELAPSED_TIME",SndLimTimeRwin,"TIME_SENDER",SndLimTimeCwnd",
              1024);
      if (txt == 1)
        print_var(list, snap, log, agent, group, cwndtime);
      else
        plot_cwndtime("Cwnd Time", snap, log, agent, group);
    }
    if (plotcwnd == 1) {
      memset(list, 0, 1024);
      strncpy(list, ELAPSED_TIME",CurCwnd", 1024);
      if (txt == 1)
        print_var(list, snap, log, agent, group, NULL);
      else
        plot_var(list, 2, "CurCwnd", snap, log, agent, group, NULL);
    }
    if (plotrwin == 1) {
      memset(list, 0, 1024);
      strncpy(list, ELAPSED_TIME",CurRwinRcvd", 1024);
      if (txt == 1)
        print_var(list, snap, log, agent, group, NULL);
      else
        plot_var(list, 2, "CurRwinRcvd", snap, log, agent, group, NULL);
    }
    if (plotboth == 1) {
      memset(list, 0, 1024);
      strncpy(list, ELAPSED_TIME",CurCwnd,CurRwinRcvd", 1024);
      if (txt == 1)
        print_var(list, snap, log, agent, group, NULL);
      else
        plot_var(list, 3, "Both", snap, log, agent, group, NULL);
    }
#if USE_WEB100
  web100_log_close_read(log);
#elif USE_WEB10G
  estats_record_close(&log);
#endif
  }

  exit(0);
}
/**
 * \brief Main function for the snowfocus program
 */
int	main(int argc, char *argv[]) {
	debug_set_ident("snowfocus");
	snowstar::CommunicatorSingleton	cs(argc, argv);
	Ice::CommunicatorPtr	ic = cs.get();

	bool	remote = false;
	int	steps = 10;
	double	exposuretime = 1.0;
	double	temperature = std::numeric_limits<double>::quiet_NaN();
	std::string	binning;
	std::string	frame;
	std::string	filtername;
	astro::focusing::Focusing::method_type	method
		= astro::focusing::Focusing::BRENNER;

	int	c;
	int	longindex;
	while (EOF != (c = getopt_long(argc, argv, "b:c:de:f:hi:m:r:Rt:",
		longopts, &longindex)))
		switch (c) {
		case 'b':
			binning = optarg;
			break;
		case 'c':
			astro::config::Configuration::set_default(optarg);
			break;
		case 'd':
			debuglevel = LOG_DEBUG;
			break;
		case 'e':
			exposuretime = std::stod(optarg);
			break;
		case 'f':
			filtername = optarg;
			break;
		case 'h':
			usage(argv[0]);
			return EXIT_SUCCESS;
		case 'm':
			method = astro::focusing::Focusing::string2method(optarg);
			break;
		case 'r':
			frame = optarg;
			break;
		case 'R':
			remote = true;
			break;
		case 's':
			steps = std::stoi(optarg);
			break;
		case 't':
			temperature = std::stod(optarg);
			break;
		default:
			throw std::runtime_error("unknown option");
		}

	// the next argument is the command
	if (argc <= optind) {
		short_usage(argv[0]);
		throw std::runtime_error("missing service argument");
	}
	std::string	argument(argv[optind++]);
	if ("help" == argument) {
		usage(argv[0]);
		return EXIT_SUCCESS;
	}
	astro::ServerName	servername(argument);
	if (argc <= optind) {
		short_usage(argv[0]);
		throw std::runtime_error("missing instrument name argument");
	}

	// make sure the server offers instruments and guiding
	if (!remote) {
		astro::discover::ServiceDiscoveryPtr     sd
			= astro::discover::ServiceDiscovery::get();
		sd->start();
		astro::discover::ServiceObject  so
			= sd->find(sd->waitfor(argument));
		if (!so.has(astro::discover::ServiceSubset::INSTRUMENTS)) {
			std::cerr << "service '" << argument;
			std::cerr << "' does not offer focusing service";
			std::cerr << std::endl;
			return EXIT_FAILURE;
		}
		if (!so.has(astro::discover::ServiceSubset::FOCUSING)) {
			std::cerr << "service '" << argument;
			std::cerr << "' does not offer focusing service";
			std::cerr << std::endl;
			return EXIT_FAILURE;
		}
	}

	std::string	instrumentname(argv[optind++]);
	if (argc <= optind) {
		short_usage(argv[0]);
		throw std::runtime_error("missing command argument");
	}
	std::string	command = argv[optind++];
	debug(LOG_DEBUG, DEBUG_LOG, 0, "command: %s", command.c_str()); 

	// get a proxy for the instruments
	Ice::ObjectPrx	base = ic->stringToProxy(
				servername.connect("Instruments"));
	InstrumentsPrx	instruments = InstrumentsPrx::checkedCast(base);

	// get the configuration
	astro::config::ConfigurationPtr	config
		= astro::config::Configuration::get();

	// check whether we have an instrument
	if (0 == instrumentname.size()) {
		throw std::runtime_error("instrument name not set");
	}
	RemoteInstrument	instrument(instruments, instrumentname);

	// make sure the server names for focuser and ccd are identical
	astro::ServerName	targetserver
					= instrument.servername(InstrumentCCD);
	if (targetserver != instrument.servername(InstrumentFocuser)) {
		throw std::runtime_error("ccd and focuser are on different "
			"servers");
	}

	// get the device names
	CcdPrx	ccdprx = instrument.ccd();
	std::string	ccdname = ccdprx->getName();
	FocuserPrx	focuserprx = instrument.focuser();
	std::string	focusername = focuserprx->getName();
	debug(LOG_DEBUG, DEBUG_LOG, 0, "ccd: %s focuser: %s", ccdname.c_str(),
		focusername.c_str());

	// first get a connection to the server
	Ice::ObjectPrx	fbase = ic->stringToProxy(
				targetserver.connect("FocusingFactory"));
	FocusingFactoryPrx	focusingfactory
		= FocusingFactoryPrx::checkedCast(fbase);

	// get the focusing interface
	FocusingPrx	focusing = focusingfactory->get(ccdname, focusername);
	debug(LOG_DEBUG, DEBUG_LOG, 0, "got a focusing proxy");

	// creating a callback
	Ice::ObjectPtr	callback = new FocusCallbackI();
	CallbackAdapter	adapter(ic);
	Ice::Identity	ident = adapter.add(callback);
	focusing->ice_getConnection()->setAdapter(adapter.adapter());
	debug(LOG_DEBUG, DEBUG_LOG, 0, "callback installed");

	// handle the simple commands
	if (command == "help") {
		short_usage(argv[0]);
		return EXIT_SUCCESS;
	}

	if (command == "status") {
		std::cout << "status: ";
		FocusState	state = focusing->status();
		std::cout << focusingstate2string(state);
		switch (state) {
		case FocusFOCUSED:
			std::cout << " ";
			std::cout << focusing->getFocuser()->current();
			break;
		default:
			break;
		}
		std::cout << std::endl;
		return EXIT_SUCCESS;
	}

	if (command == "history") {
		show_history(focusing->history());
		return EXIT_SUCCESS;
	}

	if (command == "monitor") {
		std::cout << "current status: ";
		std::cout << focusingstate2string(focusing->status());
		std::cout << std::endl;
		focusing->registerCallback(ident);
		signal(SIGINT, handler);
		while (!signal_received) {
			sleep(1);
		}
		focusing->unregisterCallback(ident);
		return EXIT_SUCCESS;
	}

	if (command == "cancel") {
		focusing->cancel();
		std::cout << "cancel command sent" << std::endl;
		return EXIT_SUCCESS;
	}

	if (command == "repository") {
		if (optind < argc) {
			std::string	reponame(argv[optind]);
			focusing->setRepositoryName(reponame);
		} else {
			std::string	reponame
				= focusing->getRepositoryName();
			if (reponame.size() > 0) {
				std::cout << "repository: " << reponame;
				std::cout << std::endl;
			} else {
				std::cout << "repository not set" << std::endl;
			}
		}
		return EXIT_SUCCESS;
	}

	// throw exception for unknown commands
	if (command != "start") {
		short_usage(argv[0]);
		throw std::runtime_error("unknown command");
	}
	debug(LOG_DEBUG, DEBUG_LOG, 0, "executing start command");

	// make sure temperature is set
	CoolerTask	coolertask(instrument, temperature);
	coolertask.stop_on_exit(true);
	coolertask.wait();

	// next two arguments are the interval boundaries
	if ((argc - optind) < 2) {
		short_usage(argv[0]);
		throw std::runtime_error("missing interval arguments");
	}
	int	min = std::stoi(argv[optind++]);
	int	max = std::stoi(argv[optind++]);
	debug(LOG_DEBUG, DEBUG_LOG, 0, "interval [%d,%d]", min, max);
	if (min >= max) {
		short_usage(argv[0]);
		throw std::runtime_error("not an interval");
	}
	debug(LOG_DEBUG, DEBUG_LOG, 0, "focusing in interval [%d,%d]",
		min, max);

	// ensure that focuser is ready
	FocusState	state = focusing->status();
	debug(LOG_DEBUG, DEBUG_LOG, 0, "current state = %d", state);
	if ((state == FocusMOVING) && (state == FocusMEASURING)) {
		short_usage(argv[0]);
		throw std::runtime_error("already focusing");
	}
	debug(LOG_DEBUG, DEBUG_LOG, 0, "focuser available");

	// set up the exposure
	astro::camera::Exposure	exposure;
	exposure.purpose(astro::camera::Exposure::focus);
	exposure.exposuretime(exposuretime);
	if (binning.size() > 0) {
		exposure.mode(astro::image::Binning(binning));
	}
	exposure.shutter(astro::camera::Shutter::OPEN);
	if (frame.size() > 0) {
		exposure.frame(astro::image::ImageRectangle(frame));
	}

	// set up the focusing
	focusing->setSteps(steps);
	focusing->setMethod(convert(method));
	focusing->setExposure(convert(exposure));
	debug(LOG_DEBUG, DEBUG_LOG, 0, "focusing set up %d steps, method %s",
		steps,
		astro::focusing::Focusing::method2string(method).c_str());

	// start the focusing process
	debug(LOG_DEBUG, DEBUG_LOG, 0, "starting between %d and %d", min, max);
	focusing->start(min, max);
	debug(LOG_DEBUG, DEBUG_LOG, 0, "focusing started, status: %d",
		focusing->status());

	// wait for the process to complete
	bool	completed = false;
	signal(SIGINT, handler);
	do {
		sleep(1);
		switch (focusing->status()) {
        	case FocusIDLE:
        	case FocusMOVING:
        	case FocusMEASURING:
			break;
        	case FocusFOCUSED:
        	case FocusFAILED:
			completed = true;
			break;
		}
	} while ((!completed) && (!signal_received));
	if (completed) {
		std::cout << "final focus position: " << focuserprx->current();
		std::cout << std::endl;

		// display the history
		show_history(focusing->history());
	} else {
		std::cout << "focusing incomplete" << std::endl;
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}