Пример #1
0
// Process command-line arguments
void process_args(int argc, char *argv[], int *max_outer_t) {
  (*max_outer_t) = omp_get_max_threads();
  for (int i=1; i<argc; ++i) {  
    if (argv[i][0] == '-') {
      switch (argv[i][1]) {
      case 'o': // set max_outer_threads
	if (sscanf(&argv[i][2], "%d", max_outer_t) != 1 || *max_outer_t < 1) {
	  fprintf(stderr, "%s Warning: argument of -o option unacceptable: %s\n", argv[0], &argv[i][2]);
	  help_message(argv[0]);
	}
	break;
      case 'h': // print help message
	help_message(argv[0]);
	exit(0);
	break;
      default:
	fprintf(stderr, "%s: Warning: command-line option ignored: %s\n", argv[0], argv[i]);
	help_message(argv[0]);
	break;
      }
    } else {
      fprintf(stderr, "%s: Warning: command-line option ignored: %s\n", argv[0], argv[i]);
      help_message(argv[0]);
    }
  }
}
int execute_command(char cmd_char, int reg[], int nreg,
                    int mem[], int memlen)
{

  if (cmd_char == '?' || cmd_char == 'h')
    help_message();

  switch (cmd_char) {
  case 'd':
    dump_control_unit(pc, ir, running, reg, nreg);
    dump_memory(mem, memlen);
    return 0;
    break;

  case 'q':
    return 1;
    break;

  case '\n':
    one_instruction_cycle(reg, nreg, mem, memlen);
    return 0;
    break;

  default:
    printf("Please enter a valid character");
    break;
  }

  return 0;
}
Пример #3
0
int main(int argc, char *argv[])
{
    int error = 0;

    switch(argc)
    {
        case 1:
            user_mode();
            break;
        case 2:
            if(strcmp(argv[1],"-penc") == 0)
            {
                resolve(encode_character);
            }
            else if(strcmp(argv[1],"-pdec") == 0)
            {
                resolve(decode_character);
            }
            else if(strcmp(argv[1],"--help") == 0)
            {
                print_help(argv[0]);
                return 0;
            }
            else
            {
                error = 1;
            }
            break;
        case 3:
            if(strcmp(argv[1],"-enc") == 0)
            {
                file_mode(argv[2],ENCODE);
            }
            else if(strcmp(argv[1],"-dec") == 0)
            {
                file_mode(argv[2],DECODE);
            }
            else
            {
                error = 1;
            }
            break;
        default:
            error = 1;
    }

    if(error)
    {
        fprintf(stderr, "\n\tERROR");
        fprintf(stderr, "%s\n", help_message(argv[0]));
        fflush(stderr);
        return 1;
    }

    return 0;
}
Пример #4
0
void
process_options(int argc, char *argv[]) {
    int c;
    int index;
    char *opt_o_value = NULL;

    while( (c = getopt(argc, argv, "hvno:")) != -1 ) {
        switch(c) {
            case 'h':
                help_message();
                exit(0);
                break;
            case 'v':
                version_info();
                exit(0);
                break;
            case 'o':
                opt_o_value = optarg;
                break;
            case 'n':
                opt_trim = 0;    /* disable trimming -- like 454 tools */
                break;
            case '?':
                exit(1);
             default:
                abort();
        }
    }

    if ( opt_o_value != NULL ) {
        strncpy(fastq_file, opt_o_value, FASTQ_FILENAME_MAX_LENGTH);
    }

    /* process the remaining command line arguments */
    for (index = optind; index < argc; index++) {
        strncpy(sff_file, argv[index], SFF_FILENAME_MAX_LENGTH);
    }

//    /* just take the first passed in non-getopt argument as the sff file */
//    strncpy(sff_file, argv[optind], SFF_FILENAME_MAX_LENGTH);

    /* ensure that a sff file was at least passed in! */
    if ( !strlen(sff_file) ) {
        fprintf(stderr, "%s %s '%s %s' %s\n",
                "[err] Need to specify a sff file!",
                "See", PRG_NAME, "-h", "for usage!");
        exit(1);
    }
}
Пример #5
0
int
process_options(int argc, char *argv[]) {
    int c;
    int index;
    char *opt_p_value = NULL;

    while( (c = getopt(argc, argv, "hp:D:I:S:m:")) != -1 ) {
        switch(c) {
            case 'h':
                help_message();
                exit(0);
                break;
            case 'p':
                opt_p_value = optarg;
                break;
            case 'm':
                max_cost = atoi(optarg);
                break;
            case 'I':
                cost_ins = atoi(optarg);
                break;
            case 'D':
                cost_del = atoi(optarg);
                break;
            case 'S':
                cost_subs = atoi(optarg);
                break;
            case '?':
                exit(1);
             default:
                abort();
        }
    }

    /* ascertain whether a query pattern was given */
    if ( opt_p_value == NULL ) {
        fprintf(stderr, "%s : %s\n", PRG_NAME,
                        "[err] Specify a search pattern via the '-p' option!");
        exit(1);
    }
    else {
        strncpy(regexp, opt_p_value, MAX_CHAR);
    }

    return optind;
}
Пример #6
0
int execute_command(char cmd_char, CPU *cpu) {
  if (cmd_char == '?' || cmd_char == 'h') {
    help_message();
  } else if (cmd_char == 'd') {
    printf("Dumping CPU and MEM:\n");
    dump_CPU(cpu);
    dump_memory(cpu);
  } else if (cmd_char == 'q') {
    printf("Quitting. \n");
    exit(0);
  } else if (cmd_char == '\n' || cmd_char == ' ') {
    one_instruction_cycle(cpu);
  } else {
    printf("Unkown Command: %c \n",cmd_char);
    return 1;
  }
  return 0;
}
Пример #7
0
// Execute a nonnumeric command; complain if it's not 'h', '?', 'd', 'q' or '\n'
// Return true for the q command, false otherwise
//
int execute_command(char cmd_char, int reg[], int nreg, int mem[], int memlen) {
	if (cmd_char == '?' || cmd_char == 'h') {
		help_message();
	} else if (cmd_char == 'd') {
		printf("Dumping control and memory:\n");
		dump_control_unit(pc, ir, running, reg, NREG);
		dump_memory(mem, MEMLEN);
	} else if (cmd_char == 'q') {
		printf("Quitting\n");
		exit(0);
	} else if (cmd_char == '\n' || cmd_char == ' ') {
		one_instruction_cycle(reg,nreg,mem,memlen);
	} else {
		printf("Unkown command: %c; Ignoring it.\n",cmd_char);
		return 1;
	}
	return 0;
}
Пример #8
0
/*!
 * THE MAIN FUNCTION
 */
int main ( int const argc , char const * const argv [] ) {
  char* buff1 = "-h";
  bool trace = false;
  char* buff2 = "-t";
  FILE * input;
  for(int x = 1; x < argc; x++){
    if(strcmp(argv[x], buff1) == 0){
      help_message ( argv [ 0 ] ) ;
    }else{
      if(strcmp(argv[x], buff2) == 0){
	trace = true;
      }else{
	input = fopen(argv[x], "r");
      }
    }
  }
 interprete(input, trace);
 fclose(input);
 return 0;
}
Пример #9
0
void
process_options(int argc, char *argv[]) {
    int c;
    int index;
    char *opt_o_value = NULL;

    while( (c = getopt(argc, argv, "hvno:")) != -1 ) {
        switch(c) {
            case 'h':
                help_message();
                exit(0);
                break;
            case 'v':
                version_info();
                exit(0);
                break;
            case 'o':
                opt_o_value = optarg;
                break;
            case 'n':
                opt_trim = 0;    /* disable trimming -- like 454 tools */
                break;
            case '?':
                exit(1);
             default:
                abort();
        }
    }

    if ( opt_o_value != NULL ) {
        strncpy(fastq_file, opt_o_value, FASTQ_FILENAME_MAX_LENGTH);
    }

    /* process the remaining command line arguments */
    for (index = optind; index < argc; index++) {
        strncpy(sff_file, argv[index], SFF_FILENAME_MAX_LENGTH);
    }

//    /* just take the first passed in non-getopt argument as the sff file */
//    strncpy(sff_file, argv[optind], SFF_FILENAME_MAX_LENGTH);
}
Пример #10
0
int main(int argc , char *argv[])
{
    int sock;
    int n;
    FILE *fp;
    char message[WORD]  = {0};
    char command[WORD] = {0};
    struct header file_header;
    struct sockaddr_in server;
    struct packet my_packet;
    struct sockaddr_in rscv;
    int rscv_size = sizeof(rscv);
    fd_set read_fds;
    fd_set master;printf("%s\n", "haha");
    FD_ZERO(&master); 
    FD_ZERO(&read_fds);

    FD_SET(STDIN, &master);
   
    memset(&file_header,0,sizeof(struct header));

    //Create socket
    sock = socket(AF_INET , SOCK_DGRAM, IPPROTO_UDP);
    if (sock == -1)
    {
        perror("sock");
        exit(errno);
    }
    FD_SET(sock, &master);
    help_message();
    while(1){
        read_fds = master;
        if (select(sock+1, &read_fds, NULL, NULL, NULL) == -1) {
                        perror("select");
                        exit(errno); 
        }
        if( FD_ISSET(STDIN, &read_fds) ){
                fgets(message , WORD , stdin);
                char *delim = " \n";
                char * pch ;
                pch = strtok(message,delim);
                
                // connect command
                if(strcmp(pch,"connect")==0){
                    char *ip = strtok(NULL,delim);
                    char *port = strtok(NULL,delim);
                    if( ip && port)
                        connect_server(sock,ip,atoi(port),&server);
                    else 
                        printf("Error ! Usage : connect [ IP ] [ PORT ]\n" );

                    
                }
                //upload command
                else if( strcmp(pch,"upload")==0 ){
                        strncpy ( command, pch, WORD);
                        command[WORD - 1] = '\0';
                        
                        char* filename = strtok(NULL,delim);
                        if(  connected == -1 ){
                            printf("ERROR! You are not connected to a remote server.\n");
                        }
                        else{
                            if( filename ){
                                fp = fopen(filename, "r");
                                if (!fp){
                                    perror("send_file fopen fp");
                                    
                                }
                                else{
                                    send_file(sock,filename,&server,fp);
                                }
                                
                                //strncpy(file_header.filename, filename, strlen(filename));
                                //MYSEND(sock, &file_header, sizeof(struct header), 0, (struct sockaddr *)&server, sizeof(server));
                                /*if (sendto(sock, &file_header, sizeof(struct header), 0, (struct sockaddr *)&server, sizeof(server)) < 0) {
                                    perror("sendto failed");
                                    return 0;
                                }*/
                            }
                            else{
                                printf("Error ! Usage : upload [ filename ]\n" );
                            }
                        }
                        
                }
                //exit command
                else if( strcmp(pch,"exit")==0 ){
                        printf("Goodbye.\n");
                        if(  (n = write(sock , pch, WORD) ) == -1){
                            perror("write");
                            exit(errno);
                        }
                        write(sock , pch, WORD);
                        
                        break;
                }
                // help command
                else if( strcmp(pch,"help")==0 ){
                        help_message();
                }
                else{
                    printf("No such command!\n" );
                }
                memset(message,0, WORD);
                memset(command,0, WORD);
                memset(&file_header,0, sizeof(struct header));
        }
        else if( FD_ISSET(sock, &read_fds) ){
                MYRSCV(sock, &my_packet, sizeof(struct packet), 0, (struct sockaddr *)&rscv, (socklen_t *)&rscv_size);
                printf("ACK : %d\n" ,my_packet.ack);
                //if( ack_goal > my_packet.ack){
                    //printf("ack_goal : %d\n", ack_goal );
                if(my_packet.ack <= last_acked && my_packet.ack >0){
                    //if(retransmit_count == 0){

                        retransmit_count++;
                        printf("packet loss ! Retransmit packet %d\n", my_packet.ack );
                        struct packet temp_packet;
                        temp_packet.sequence =  my_packet.ack;
                        memcpy(temp_packet.data, file_cache[my_packet.ack%WIN_SIZE].data, PACKET_SIZE+1);
                        //printf("packet:%d  data: %s\n" ,temp_packet.sequence,file_cache[my_packet.ack%WIN_SIZE].data);
                        MYSEND(sock, &temp_packet, sizeof(struct packet), 0, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
                    //}
                
                }
                else if(my_packet.ack > last_acked ){
                    //printf("%d %d\n",c_sequence , packets);
                    retransmit_count = 0;
                    last_acked = my_packet.ack ;
                    
                    if(packets > 0){
                        struct packet temp_packet;
                        temp_packet.sequence =  c_sequence++;
                        memset(temp_packet.data,0,PACKET_SIZE+1);
                        fread(temp_packet.data , PACKET_SIZE, 1, fp) ;
                        memset(file_cache[temp_packet.sequence%WIN_SIZE].data,0,PACKET_SIZE+1);
                        memcpy(file_cache[temp_packet.sequence%WIN_SIZE].data,temp_packet.data,PACKET_SIZE+1);
                        MYSEND(sock, &temp_packet, sizeof(struct packet), 0, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
                        packets--;
                        //printf("packet:%d  data: %s\n" ,temp_packet.sequence,temp_packet.data);
                    }
                    /*else  {
                        struct packet temp_packet;
                        temp_packet.sequence =  last_acked+1;
                        memcpy(temp_packet.data, file_cache[temp_packet.sequence %WIN_SIZE].data, PACKET_SIZE+1);
                        //printf("packet:%d  data: %s\n" ,temp_packet.sequence,file_cache[my_packet.ack%WIN_SIZE].data);
                        MYSEND(sock, &temp_packet, sizeof(struct packet), 0, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
                    }*/
                }
                /*else if( last_acked != (c_sequence-1) && my_packet.ack < 0){
                    struct packet temp_packet;
                    temp_packet.sequence =  last_acked+1;
                    memcpy(temp_packet.data, file_cache[temp_packet.sequence %WIN_SIZE].data, PACKET_SIZE+1);
                    //printf("packet:%d  data: %s\n" ,temp_packet.sequence,file_cache[my_packet.ack%WIN_SIZE].data);
                    MYSEND(sock, &temp_packet, sizeof(struct packet), 0, (struct sockaddr *)&server, sizeof(struct sockaddr_in));
                }*/
                //}
        }

        
    }

    close(sock);
    return 0;
        
}
Пример #11
0
void update_iq_foldcorr(void)
{
char s[80];
int i, j, k, m, mm;
int ia,ib;
int seg;
float scale,t1,t2,r1,r2,ta,tb,ra,rb;
float z[2*MAX_ADCHAN];
mm=twice_rxchan;
scale=5;
// Find average values for amplitude ratio and phase sum.
// for those segments where we collected BAL_AVGNUM data points.
for(seg=0; seg<bal_segments; seg++)
  {
  if(bal_flag[seg] == BAL_AVGNUM)
    {
    for(j=0; j<4*ui.rx_rf_channels; j++)z[j]=0;
    for(m=0; m<BAL_AVGNUM; m++)
      {
      k=(m*bal_segments+seg)*ui.rx_rf_channels;  
      for(j=0; j<ui.rx_rf_channels; j++)
        {
        z[4*j  ]+=bal_pos[k+j];
        t1=cos(bal_phsum[k+j]);
        t2=sin(bal_phsum[k+j]);
        z[4*j+1]+=t1;
        z[4*j+2]+=t2;
        z[4*j+3]+=bal_amprat[k+j];
        }
      }
    for(j=0; j<ui.rx_rf_channels; j++)
      {
      bal_pos[seg*ui.rx_rf_channels+j]=0.5+z[4*j  ]/BAL_AVGNUM;  
      bal_phsum[seg*ui.rx_rf_channels+j]=atan2(z[4*j+2],z[4*j+1]);
      bal_amprat[seg*ui.rx_rf_channels+j]=z[4*j+3]/BAL_AVGNUM;
      }
    }
  }  
// Expand to fft1_size and draw straight lines between the points we have.
for(j=0; j<ui.rx_rf_channels; j++)
  {
// The center point is incorrect since the spur and the signal are not
// enough separated. Replace with average from surroundings.  
  k=bal_segments*ui.rx_rf_channels/2+j;
  bal_phsum[k]=(bal_phsum[k+ui.rx_rf_channels]+bal_phsum[k-ui.rx_rf_channels])/2;
  bal_amprat[k]=(bal_amprat[k+ui.rx_rf_channels]+bal_amprat[k-ui.rx_rf_channels])/2;
  bal_pos[k]=(bal_pos[k+ui.rx_rf_channels]+bal_pos[k-ui.rx_rf_channels])/2;
  seg=0;
  while(bal_flag[seg] != BAL_AVGNUM  && seg < bal_segments) seg++;
  if(seg == bal_segments)return;
  ia=0;
  t1=bal_phsum[seg*ui.rx_rf_channels+j];
  t2=bal_amprat[seg*ui.rx_rf_channels+j];
  ta=t2*cos(t1);
  ra=t2*sin(t1);
fillbuf:;
  ib=bal_pos[seg*ui.rx_rf_channels+j];
  t1=bal_phsum[seg*ui.rx_rf_channels+j];
  t2=bal_amprat[seg*ui.rx_rf_channels+j];
  tb=t2*cos(t1);
  rb=t2*sin(t1);
fillbuf1:;
  if(ia<0)ia=0;
  if(ib>fft1_size)ib=fft1_size;
  for(i=ia; i<ib; i++)
    {
    t1=(i-ia);
    t1/=(ib-ia);
    t2=1-t1;
    r1=t2*ta+t1*tb;
    r2=t2*ra+t1*rb;
    cal_buf[mm*i+2*j  ]=r1;
    cal_buf[mm*i+2*j+1]=r2;
    }    
  ia=ib;
  ta=tb;
  ra=rb;
  seg++;
  if(seg <= bal_segments)
    {
    while(seg < bal_segments)
      {
      if(bal_flag[seg] == BAL_AVGNUM)goto fillbuf;
      seg++;
      }
    seg++;  
    ib=fft1_size;
    goto fillbuf1;
    }  
  }
// Make the new correction curve smoother.
contract_foldcorr(cal_buf);
expand_foldcorr(cal_buf,cal_tmp);
for(j=0; j<mm; j+=2)
  {
  for(i=0; i<fft1_size/2; i++)
    {
    r1=cal_buf[mm*i+j  ];
    r2=cal_buf[mm*i+j+1];
    fft1_foldcorr[mm*i+j  ]+=r1;
    fft1_foldcorr[mm*i+j+1]-=r2;
    }
  for(i=fft1_size/2+1; i<fft1_size; i++)
    {
    r1=cal_buf[mm*i+j  ];
    r2=cal_buf[mm*i+j+1];
    fft1_foldcorr[mm*i+j  ]+=r1;
    fft1_foldcorr[mm*i+j+1]+=r2;
    }
  }  
redraw:;
cal_initscreen();
for(j=0; j<mm; j+=2)
  {
  for(i=0; i<bal_screen; i++)
    {
    k=i*(fft1_size/bal_screen);
    if(k>fft1_size-1)k=fft1_size-1;
    lir_setpixel(i, cal_graph[bal_screen*j+i], 0);  
    t2=scale*cal_buf[mm*k+j];   
    if(t2 <-cal_ymax)t2=-cal_ymax;
    if(t2 >cal_ymax)t2=cal_ymax;
    if(j > 1)t2-=0.32;
    cal_graph[bal_screen*j+i]=screen_height*(cal_yzer-t2);
    lir_setpixel(i, cal_graph[bal_screen*j+i], 13);  
    lir_setpixel(i, cal_graph[bal_screen*(j+1)+i], 0);  
    t2=scale*cal_buf[mm*k+j+1];   
    if(t2 <-cal_ymax)t2=-cal_ymax;
    if(t2 >cal_ymax)t2=cal_ymax;
    if(j > 1)t2-=0.32;
    cal_graph[bal_screen*(j+1)+i]=screen_height*(0.5-t2);
    lir_setpixel(i, cal_graph[bal_screen*(j+1)+i], 10);  
    }
  }
lir_text(0,3,"Complex amplitude of spur frequency to add to signal.");
lir_text(0,4,"If amplitudes are large, run the routine once more");
lir_text(0,5,"to make sure second order effects are eliminated.");
lir_text(7,7,"Press + or - to change scale, any other key to exit.)");
sprintf(s,"Scale=%f ",scale);
lir_text(7,8,s);
await_processed_keyboard();
if(kill_all_flag) return;
if(lir_inkey == '+')
  {
  scale*=3.13;
  goto redraw;
  }
if(lir_inkey == '-')
  {
  scale/=3.13;
  goto redraw;
  }
if(lir_inkey == F1_KEY)
  {
  help_message(312);
  goto redraw;
  }
fft1_calibrate_flag|=CALIQ;
bal_updflag=0;
}
Пример #12
0
int main(int argc, char **argv)
{
    int c;
    char *progname;

    ingraph_state ig;

    Agraph_t *graph;
    Agnode_t *node;
    Agedge_t *edge;
    Agedge_t *nexte;
    Agsym_t *attr;

    char **files;

    generic_list_t *attr_list;
    generic_list_t *node_list;

    unsigned long i, j;

    opterr = 0;

    progname = strrchr(argv[0], '/');
    if (progname == NULL) {
	progname = argv[0];
    } else {
	progname++;		/* character after last '/' */
    }

    attr_list = new_generic_list(16);
    node_list = new_generic_list(16);

    while ((c = getopt(argc, argv, "hvn:N:")) != -1) {
	switch (c) {
	case 'N':
	    {
		attr_list = addattr(attr_list, optarg);
		break;
	    }
	case 'n':
	    {
		node_list = addnode(node_list, optarg);
		break;
	    }
	case 'h':
	    {
		help_message(progname);
		exit(EXIT_SUCCESS);
		break;
	    }
	case 'v':
	    {
		verbose = 1;
		break;
	    }
	case '?':
	    if (isprint(optopt)) {
		fprintf(stderr, "Unknown option `-%c'.\n", optopt);
	    } else {
		fprintf(stderr, "Unknown option character `\\x%X'.\n",
			optopt);
	    }
	    exit(EXIT_FAILURE);
	    break;

	default:
	    help_message(progname);
	    exit(EXIT_FAILURE);
	    break;
	}
    }

    /* Any arguments left? */
    if (optind < argc) {
	files = &argv[optind];
    } else {
	files = NULL;
    }

    newIngraph(&ig, files, gread);
    while ((graph = nextGraph(&ig)) != NULL) {
	if (agisdirected(graph) == 0) {
	    fprintf(stderr,
		    "*** Error: Graph is undirected! Pruning works only with directed graphs!\n");
	    exit(EXIT_FAILURE);
	}

	/* attach node data for marking to all nodes */
	aginit(graph, AGNODE, NDNAME, sizeof(ndata), 1);

	/* prune all nodes specified on the commandline */
	for (i = 0; i < node_list->used; i++) {
	    if (verbose == 1)
		fprintf(stderr, "Pruning node %s\n",
			(char *) node_list->data[i]);

	    /* check whether a node of that name exists at all */
	    node = agnode(graph, (char *) node_list->data[i], 0);
	    if (node == NULL) {
		fprintf(stderr,
			"*** Warning: No such node: %s -- gracefully skipping this one\n",
			(char *) node_list->data[i]);
	    } else {
		MARK(node);	/* Avoid cycles */
		/* Iterate over all outgoing edges */
		for (edge = agfstout(node); edge; edge = nexte) {
		    nexte = agnxtout(edge);
		    if (aghead(edge) != node) {	/* non-loop edges */
			if (verbose == 1)
			    fprintf(stderr, "Processing descendant: %s\n",
				    agnameof(aghead(edge)));
			remove_child(graph, aghead(edge));
			agdelete(graph, edge);
		    }
		}
		UNMARK(node);	/* Unmark so that it can be removed in later passes */

		/* Change attribute (e.g. border style) to show that node has been pruneed */
		for (j = 0; j < attr_list->used; j++) {
		    /* create attribute if it doesn't exist and set it */
		    attr =
			agattr(graph, AGNODE,
			       ((strattr_t *) attr_list->data[j])->n, "");
		    if (attr == NULL) {
			fprintf(stderr, "Couldn't create attribute: %s\n",
				((strattr_t *) attr_list->data[j])->n);
			exit(EXIT_FAILURE);
		    }
		    agxset(node, attr,
			   ((strattr_t *) attr_list->data[j])->v);
		}
	    }
	}
	agwrite(graph, stdout);
	agclose(graph);
    }
    free(attr_list);
    free(node_list);
    exit(EXIT_SUCCESS);
}
Пример #13
0
int main(int argc, char **argv) {

  int i,j;
  ng_t* ng;
  int verbosity;
  int mem_alloc_method; /* Method used to decide how much memory to 
			   allocate for count tables */
  int buffer_size;
  flag is_ascii;
  ngram current_ngram;
  ngram previous_ngram;
  count_t *ng_count; /* Array indicating the number of occurrances of 
			   the current 1-gram, 2-gram, ... ,n-gram 
			   Size depends on #define in general.h
			*/  
  int nlines;
  int pos_of_novelty;
  int prev_id1;
  flag contains_unks;
  int mem_alloced;

  flag displayed_oov_warning; /** Display OOV warning 
			       */

  /*  ------------------  Process command line --------------------- */

  report_version(&argc,argv);

  if (argc == 1 || pc_flagarg(&argc, argv,"-help")) {    
    /* Display help message */    
    help_message();
    exit(1);
  }

  verbosity = pc_intarg(&argc, argv,"-verbosity",DEFAULT_VERBOSITY);

  /* Initialization */
  {
    ng=init_ng(
	    &argc,
	    argv,
	    verbosity
	    );
    
    mem_alloc_method = init_alloc_method(ng, &argc, argv, &buffer_size);
    
    if (!strcmp(ng->id_gram_filename,"-") && mem_alloc_method == TWO_PASSES)
      quit(-1,"Error: If idngram is read from stdin, then cannot use -calc_mem option.\n");
    
    is_ascii = set_lmformat(pc_flagarg(&argc,argv,"-ascii_input"),
			    pc_flagarg(&argc,argv,"-bin_input"),
			    ng);  

    /* Report parameters */
    report_param(verbosity,ng,
		 is_ascii, mem_alloc_method, buffer_size);

    pc_report_unk_args(&argc,argv,verbosity);

  }

  /* --------------- Read in the vocabulary -------------- */
  read_vocab(ng,verbosity);
       		     
  /* --------------- Allocate space for the table_size array --------- */
  init_ng_table_size(ng, 
		     mem_alloc_method,
		     is_ascii,
		     verbosity,
		     buffer_size
		     );

  /* ----------- Allocate memory for tree structure -------------- */

  ng->count = NULL;
  ng->count4 = NULL;
  ng->marg_counts = NULL;
  ng->marg_counts4 = NULL;
  ng->count_table = NULL;

  ng->count = (count_ind_t **) rr_malloc(sizeof(count_ind_t *)*ng->n);
  ng->count4 = (count_t **) rr_malloc(sizeof(count_t *)*ng->n);    
  ng->count_table = (count_t **) rr_malloc(sizeof(count_t *)*ng->n);

  if (ng->four_byte_counts) {
    ng->marg_counts4 = (count_t *) rr_calloc(sizeof(count_t), ng->table_sizes[0]);

  }else {
    for (i=0;i<=ng->n-1;i++) 
      ng->count_table[i] = (count_t *) rr_calloc(ng->count_table_size+1,
						sizeof(count_t));

    ng->marg_counts = (count_ind_t *) rr_calloc(sizeof(count_ind_t),ng->table_sizes[0]);
    fprintf(stderr, "table_size %d\n",ng->table_sizes[0]);
    fflush(stderr);
  }

  ng->word_id = (id__t **) rr_malloc(sizeof(id__t *)*ng->n);

  if (ng->four_byte_alphas) {
    ng->bo_weight4 = (four_byte_t **) rr_malloc(sizeof(four_byte_t *)*ng->n);
    ng->bo_weight4[0] = (four_byte_t *) rr_malloc(sizeof(four_byte_t)*
						ng->table_sizes[0]);
  }else {
    ng->bo_weight = (bo_weight_t **) rr_malloc(sizeof(bo_weight_t *)*ng->n);
    ng->bo_weight[0] = (bo_weight_t *) rr_malloc(sizeof(bo_weight_t)*
						ng->table_sizes[0]);
  }

  ng->ind = (index__t **)  rr_malloc(sizeof(index__t *)*ng->n);

  /* First table */
  if (ng->four_byte_counts) 
    ng->count4[0] = (count_t *) rr_calloc(ng->table_sizes[0],sizeof(count_t));
  else 
    ng->count[0] = (count_ind_t *) rr_calloc(ng->table_sizes[0],sizeof(count_ind_t));

  ng->uni_probs = (uni_probs_t *) rr_malloc(sizeof(uni_probs_t)*
					   ng->table_sizes[0]);
  ng->uni_log_probs = (uni_probs_t *) rr_malloc(sizeof(uni_probs_t)*
					       ng->table_sizes[0]);

  if (ng->n >=2) 
    ng->ind[0] = (index__t *) rr_calloc(ng->table_sizes[0],sizeof(index__t));

  for (i=1;i<=ng->n-2;i++) {    
    ng->word_id[i] = (id__t *) rr_malloc(sizeof(id__t)*ng->table_sizes[i]);

    if (ng->four_byte_counts) 
      ng->count4[i] = (count_t *) rr_malloc(sizeof(count_t)*ng->table_sizes[i]);
    else 
      ng->count[i] = (count_ind_t *) rr_malloc(sizeof(count_ind_t)*ng->table_sizes[i]);

    if (ng->four_byte_alphas) 
      ng->bo_weight4[i] = (four_byte_t *) rr_malloc(sizeof(four_byte_t)*ng->table_sizes[i]);
    else 
      ng->bo_weight[i] = (bo_weight_t *) rr_malloc(sizeof(bo_weight_t)*ng->table_sizes[i]);
    
    ng->ind[i] = (index__t *) rr_malloc(sizeof(index__t)*ng->table_sizes[i]);

    mem_alloced = sizeof(count_ind_t) + sizeof(bo_weight_t) + 
		sizeof(index__t) + sizeof(id__t);
    
    if (ng->four_byte_alphas) 
      mem_alloced += 4;
   
    mem_alloced *= ng->table_sizes[i];
    
    pc_message(verbosity,2,"Allocated %d bytes to table for %d-grams.\n",
	       mem_alloced,i+1);
    
  }

  ng->word_id[ng->n-1] = (id__t *) 
    rr_malloc(sizeof(id__t)*ng->table_sizes[ng->n-1]);

  if (ng->four_byte_counts) 
    ng->count4[ng->n-1] = (count_t *) rr_malloc(sizeof(count_t)*ng->table_sizes[ng->n-1]);    
  else 
    ng->count[ng->n-1] = (count_ind_t *) rr_malloc(sizeof(count_ind_t)*ng->table_sizes[ng->n-1]);

  pc_message(verbosity,2,"Allocated (%d+%d) bytes to table for %d-grams.\n",
	     ng->four_byte_counts?sizeof(count_t):sizeof(count_ind_t),
	     sizeof(id__t)*ng->table_sizes[ng->n-1],ng->n);
  
  /* Allocate memory for table for first-byte of indices */

  ng_allocate_ptr_table(ng,NULL,0);

  /* Allocate memory for alpha array */

  ng->alpha_array = (double *) rr_malloc(sizeof(double)*ng->out_of_range_alphas);
  ng->size_of_alpha_array = 0;

  /* Allocate memory for frequency of frequency information */

  ng->freq_of_freq = (fof_t **) rr_malloc(sizeof(fof_t *)*ng->n);

  NG_DISC_METH(ng)->allocate_freq_of_freq(ng);

  /* Read n-grams into the tree */
  pc_message(verbosity,2,"Processing id n-gram file.\n");
  pc_message(verbosity,2,"20,000 n-grams processed for each \".\", 1,000,000 for each line.\n");

  /* Allocate space for ngrams id arrays */

  current_ngram.id_array = (id__t *) rr_calloc(ng->n,sizeof(id__t));
  previous_ngram.id_array = (id__t *) rr_calloc(ng->n,sizeof(id__t));
  current_ngram.n = ng->n;
  previous_ngram.n = ng->n;
  
  ng->num_kgrams = (ngram_sz_t *) rr_calloc(ng->n,sizeof(ngram_sz_t));
  ng_count = (count_t *) rr_calloc(ng->n,sizeof(count_t));
  nlines = 1;
  ng->n_unigrams = 0;

  /* Process first n-gram */  
  get_ngram(ng->id_gram_fp,&current_ngram,is_ascii);
  contains_unks = ngram_chk_contains_unks(&current_ngram,ng->n);

  /* Skip over any unknown words.  They will come first, because <UNK>
     always has a word ID of zero. */
  while (ng->vocab_type == CLOSED_VOCAB && contains_unks){
    /* Stop looking if there are no more N-Grams.  Of course, this
       means training will fail, since there are no unigrams. */
    if (get_ngram(ng->id_gram_fp,&current_ngram,is_ascii) == 0)
      break;
    contains_unks = ngram_chk_contains_unks(&current_ngram,ng->n);
  }

  for (i=0;i<=ng->n-2;i++) {
    ng->ind[i][0] = new_index(0,ng->ptr_table[i],&(ng->ptr_table_size[i]),0);
    ng->word_id[i+1][0] = current_ngram.id_array[i+1];
    ng->num_kgrams[i+1]++;
    ng_count[i] = current_ngram.count;
  }

  ng_count[0] = current_ngram.count;

  NG_DISC_METH(ng)->update_freq_of_freq(ng,ng->n-1,current_ngram.count);

  store_normal_count(ng,0,current_ngram.count,ng->n-1);

  if (current_ngram.count <= ng->cutoffs[ng->n-2]) 
    ng->num_kgrams[ng->n-1]--;

  ngram_copy(&previous_ngram,&current_ngram,ng->n);

  prev_id1 = current_ngram.id_array[0];
    
  displayed_oov_warning = 0;

  while (!rr_feof(ng->id_gram_fp)) {

    if (get_ngram(ng->id_gram_fp,&current_ngram,is_ascii)) {

      if (ng->vocab_type == CLOSED_VOCAB)
	contains_unks=ngram_chk_contains_unks(&current_ngram,ng->n);
    
      if (!contains_unks || ng->vocab_type != CLOSED_VOCAB) {

	/* Test for where this ngram differs from last - do we have an
	   out-of-order ngram? */
	pos_of_novelty = ngram_find_pos_of_novelty(&current_ngram,&previous_ngram,ng->n,nlines);
    
	nlines++; 
	show_idngram_nlines(nlines, verbosity);
    
	/* Add new n-gram as soon as it is encountered */
	/* If all of the positions 2,3,...,n of the n-gram are context
	   cues then ignore the n-gram. */
    
	if (ng->n > 1) {
	  NG_DISC_METH(ng)->update_freq_of_freq(ng,ng->n-1,current_ngram.count);
	        
	  store_normal_count(ng,ng->num_kgrams[ng->n-1],current_ngram.count,ng->n-1);
	  
	  ng->word_id[ng->n-1][ng->num_kgrams[ng->n-1]] = current_ngram.id_array[ng->n-1];
	  ng->num_kgrams[ng->n-1]++;	  
	  
	  if (ng->num_kgrams[ng->n-1] >= ng->table_sizes[ng->n-1])
	    quit(-1,"\nMore than %d %d-grams needed to be stored. Rerun with a higher table size.\n",ng->table_sizes[ng->n-1],ng->n);
	}
	/* Deal with new 2,3,...,(n-1)-grams */
      
	for (i=ng->n-2;i>=MAX(1,pos_of_novelty);i--) {

	  NG_DISC_METH(ng)->update_freq_of_freq(ng,i,ng_count[i]);
	  
	  if (ng_count[i] <= ng->cutoffs[i-1]) 
	    ng->num_kgrams[i]--;
	  else
	    store_normal_count(ng,ng->num_kgrams[i]-1,ng_count[i],i);

	  ng_count[i] = current_ngram.count;
	  ng->word_id[i][ng->num_kgrams[i]] = current_ngram.id_array[i];
	  ng->ind[i][ng->num_kgrams[i]] = new_index(ng->num_kgrams[i+1]-1,
						    ng->ptr_table[i],
						    &(ng->ptr_table_size[i]),
						    ng->num_kgrams[i]);
	  ng->num_kgrams[i]++;
	
	  if (ng->num_kgrams[i] >= ng->table_sizes[i])
	    quit(-1,"More than %d %d-grams needed to be stored. Rerun with a higher table size.\n",ng->table_sizes[i],i+1);	  
	}
      
	for (i=0;i<=pos_of_novelty-1;i++) 
	  ng_count[i] += current_ngram.count;
      
	/* Deal with new 1-grams */
      
	if (pos_of_novelty == 0) {
	  if (ng->n>1) {
	    for (i = prev_id1 + 1; i <= current_ngram.id_array[0]; i++) {
	      ng->ind[0][i] = new_index(ng->num_kgrams[1]-1,
				       ng->ptr_table[0],
				       &(ng->ptr_table_size[0]),
				       i);
	    }
	    prev_id1 = current_ngram.id_array[0];
	  }

	  NG_DISC_METH(ng)->update_freq_of_freq(ng,0,ng_count[0]);

	  if (!ng->context_cue[previous_ngram.id_array[0]]) {
	    ng->n_unigrams += ng_count[0];
	    store_normal_count(ng,previous_ngram.id_array[0],ng_count[0],0);
	  }

	  store_marginal_count(ng,previous_ngram.id_array[0],ng_count[0],0);
		      
	  ng_count[0] = current_ngram.count;
	}

	if (current_ngram.count <= ng->cutoffs[ng->n-2]) 
	  ng->num_kgrams[ng->n-1]--;

	ngram_copy(&previous_ngram,&current_ngram,ng->n);

      }else {
	if (!displayed_oov_warning){
	  pc_message(verbosity,2,"Warning : id n-gram stream contains OOV's (n-grams will be ignored).\n");
	  displayed_oov_warning = 1;
	}
      }
    }
  }

  rr_iclose(ng->id_gram_fp);

  for (i=ng->n-2;i>=1;i--) {

    NG_DISC_METH(ng)->update_freq_of_freq(ng,i,ng_count[i]);

    if (ng_count[i] <= ng->cutoffs[i-1]) 
      ng->num_kgrams[i]--;
    else 
      store_normal_count(ng,ng->num_kgrams[i]-1,ng_count[i],i);
      
  }
  
  NG_DISC_METH(ng)->update_freq_of_freq(ng,0,ng_count[0]);

  if (!ng->context_cue[current_ngram.id_array[0]]) {
    ng->n_unigrams += ng_count[0];
    store_normal_count(ng,current_ngram.id_array[0],ng_count[0],0);
  }

  store_marginal_count(ng,current_ngram.id_array[0],ng_count[0],0);

  if (ng->n>1) {
    for (i=current_ngram.id_array[0]+1;i<=ng->vocab_size;i++)
      ng->ind[0][i] = new_index(ng->num_kgrams[1],
				ng->ptr_table[0],
				&(ng->ptr_table_size[0]),
				current_ngram.id_array[0]);
  }

  /* The idngram reading is completed at this point */
  pc_message(verbosity,2,"\n");

  /* Impose a minimum unigram count, if required */

  if (ng->min_unicount > 0) {

    int nchanged= 0;

    for (i=ng->first_id;i<=ng->vocab_size;i++) {
      if ((return_count(ng->four_byte_counts,
			ng->count_table[0],
			ng->count[0],
			ng->count4[0],
			i) < ng->min_unicount) && !ng->context_cue[i]) {

	/* There was a bug in V2's switch.  Look at segment for ABSOLUTE */
	NG_DISC_METH(ng)->reduce_ug_freq_of_freq(ng,i);
	ng->n_unigrams += (ng->min_unicount - ng->count[0][i]);
	store_normal_count(ng,i,ng->min_unicount,0);
	nchanged++;
      }
    }

    if (nchanged > 0) 
      pc_message(verbosity,2,
		 "Unigram counts of %d words were bumped up to %d.\n",
		 nchanged,ng->min_unicount);
  }

  /* Count zeroton information for unigrams */

  ng->freq_of_freq[0][0] = 0;
  
  for (i=ng->first_id;i<=ng->vocab_size;i++) {
    if (return_count(ng->four_byte_counts,
		     ng->count_table[0],
		     ng->count[0],
		     ng->count4[0],
		     i) == 0) {
      ng->freq_of_freq[0][0]++;
    }
  }  

  if (ng->discounting_method == GOOD_TURING) {
    for (i=0;i<=ng->n-1;i++) 
      for (j=1;j<=ng->fof_size[i];j++) 
	pc_message(verbosity,3,"fof[%d][%d] = %d\n",i,j,ng->freq_of_freq[i][j]);
  }

  pc_message(verbosity,2,"Calculating discounted counts.\n");

  NG_DISC_METH(ng)->compute_discount_aux(ng, verbosity);
     
  /* Smooth unigram distribution, to give some mass to zerotons */     
  compute_unigram(ng,verbosity);

  /* Increment Contexts if using Good-Turing discounting-> No need otherwise,
     since all values are discounted anyway. */

  if (ng->discounting_method == GOOD_TURING) {
    pc_message(verbosity,2,"Incrementing contexts...\n");  

    for (i=ng->n-1;i>=1;i--) 
      increment_context(ng,i,verbosity);      
  }

  /* Calculate back-off weights */

  pc_message(verbosity,2,"Calculating back-off weights...\n");

  for (i=1;i<=ng->n-1;i++) 
    compute_back_off(ng,i,verbosity);

  if (!ng->four_byte_alphas) 
    pc_message(verbosity,3,"Number of out of range alphas = %d\n",
	       ng->size_of_alpha_array);

  /* Write out LM */

  pc_message(verbosity,2,"Writing out language model...\n");

  if (ng->write_arpa)
    write_arpa_lm(ng,verbosity);

  if (ng->write_bin) 
    write_bin_lm(ng,verbosity);

  pc_message(verbosity,0,"idngram2lm : Done.\n");

  return 0;    
}
Пример #14
0
int main(int argc, char *argv[])
{
    setbuf(stdout, NULL);

    uint16_t nfiles = 0;

    uint8_t filter_systemid = 0xFF, filter_tmtype = 0xFF;
    bool filter_events = false;

    bool save_packets = false;
    char outfile[256];

    for(int i = 1; i < argc; i++) {
        if(argv[i][0] == '-') {
            for(int j = 1; argv[i][j] != 0; j++) {
                switch(argv[i][j]) {
                    case 'e':
                        filter_events = true;
                        j = strlen(&argv[i][0]) - 1;
                        break;
                    case 'f':
                        if(argv[i][j+1] == 's') filter_systemid = atoi(&argv[i][j+2]);
                        if(argv[i][j+1] == 't') filter_tmtype = atoi(&argv[i][j+2]);
                        j = strlen(&argv[i][0]) - 1;
                        break;
                    case 'o':
                        save_packets = true;
                        strncpy(outfile, &argv[i][j+1], 256);
                        outfile[255] = 0;
                        j = strlen(&argv[i][0]) - 1;
                        break;
                    case '?':
                        help_message(argv[0]);
                        return -1;
                    default:
                        std::cerr << "Unknown option, use -? to list options\n";
                        return -1;
                }
            }
        } else {
            nfiles++;
        }
    }

    if(nfiles == 0) {
        help_message(argv[0]);
        return -1;
    }

    std::streampos cur;

    uint8_t buffer[TELEMETRY_PACKET_MAX_SIZE];
    buffer[0] = 0x90;

    uint16_t length;

    uint32_t count[256][256];
    uint32_t bad_checksum[256][256];
    uint64_t amount[256][256];
    uint16_t previous_frame_counter[256][256];
    uint32_t gaps[256][256];
    Clock first_systemtime = 0, last_systemtime = 0;
    bool not_coincident[16];

    std::ofstream log;
    if (save_packets) {
        log.open(outfile, std::ofstream::binary);
    }

    TelemetryPacket tp(NULL);

    for(int i = 1; i < argc; i++) {
        memset(count, 0, sizeof(count));
        memset(bad_checksum, 0, sizeof(bad_checksum));
        memset(amount, 0, sizeof(amount));
        memset(previous_frame_counter, 0xFF, sizeof(previous_frame_counter));
        memset(gaps, 0, sizeof(gaps));
        first_systemtime = last_systemtime = 0;
        memset(not_coincident, 0, sizeof(not_coincident));

        if(argv[i][0] != '-') {
            std::cout << "Inspecting " << argv[i] << std::endl;

            uint8_t percent_completed = 0;

            std::ifstream ifs(argv[i]);

            if(ifs.good()) {
                ifs.seekg(0, ifs.end);
                std::streampos size = ifs.tellg();
                ifs.seekg(0, ifs.beg);

                std::cout << size << " bytes: ";
                std::cout.flush();

                while (ifs.good()) {
                    if(ifs.get() == 0x90) {
                        if(ifs.peek() == 0xeb) {
                            cur = ifs.tellg(); // points one byte into sync word
                            ifs.seekg(5, std::ios::cur);
                            ifs.read((char *)&length, 2);

                            if(length > TELEMETRY_PACKET_MAX_SIZE-16) continue; //invalid payload size

                            ifs.seekg(cur);

                            ifs.read((char *)buffer+1, length+15);

                            tp = TelemetryPacket(buffer, length+16);
                            uint8_t this_systemid = tp.getSystemID();
                            uint8_t this_tmtype = tp.getTmType();

                            if(((!filter_events) || (((this_systemid & 0xF0) == 0x80) && (this_tmtype == 0xF3))) &&
                               ((filter_systemid == 0xFF) || (this_systemid == filter_systemid)) &&
                               ((filter_tmtype == 0xFF) || (this_tmtype == filter_tmtype))) {
                                if(tp.valid()) {
                                    cur += length+15;

                                    count[this_systemid][this_tmtype]++;
                                    amount[this_systemid][this_tmtype] += length+16;
                                    uint16_t this_frame_counter = tp.getCounter();
                                    int16_t frame_counter_difference = this_frame_counter -
                                                                       previous_frame_counter[this_systemid][this_tmtype];
                                    if((previous_frame_counter[this_systemid][this_tmtype] != 0xFFFF) &&
                                       (frame_counter_difference > 1)) {
                                        // Make an exception for quicklook spectrum packets
                                        if(!((frame_counter_difference == 6) && (this_systemid == 0x10) && ((this_tmtype & 0xF0) == 0x10))) {
                                            gaps[this_systemid][this_tmtype]++;
                                        }
                                    }
                                    previous_frame_counter[this_systemid][this_tmtype] = this_frame_counter;

                                    if(((this_systemid & 0xF0) == 0x80) && (this_tmtype == 0xF3)) {
                                        if(((buffer[19] == 0) && (buffer[20] == 0) && (buffer[21] == 0)) ||
                                           ((buffer[22] == 0) && (buffer[23] == 0) && (buffer[24] == 0))) {
                                            not_coincident[this_systemid & 0x0F] = true;
                                        }
                                    }

                                    if(first_systemtime == 0) first_systemtime = tp.getSystemTime();
                                    last_systemtime = tp.getSystemTime();

                                    if(save_packets && log.is_open()) {
                                        log.write((char *)buffer, length+16);
                                    }
                                } else {
                                    bad_checksum[this_systemid][tp.getTmType()]++;
                                }
                            }

                            if((((uint64_t)ifs.tellg())*100/size) > percent_completed) {
                                percent_completed++;
                                printf("%3u%%\b\b\b\b", percent_completed);
                            }

                            ifs.seekg(cur);
                        }
                    }
                }

                ifs.close();

                std::cout << "\nPacket breakdown:\n";
                for(int j = 0; j < 256; j++) {
                    for(int k = 0; k < 256; k++) {
                        if(count[j][k] > 0) {
                            printf("  0x%02x 0x%02x : [%4.1f%%] %lu", j, k, 100. * amount[j][k] / size, count[j][k]);
                            if(((j & 0xF0) == 0x80) && (k == 0xF3) && not_coincident[j & 0x0F]) {
                                printf(" (includes non-coincident data!)");
                            }
                            if(bad_checksum[j][k] > 0) {
                                printf(", plus %lu with bad checksums", bad_checksum[j][k]);
                            }
                            if(gaps[j][k] > 0) {
                                printf(", plus %lu gaps", gaps[j][k]);
                            }
                            printf("\n");
                        }
                    }
                }

                printf("Elapsed gondola time: %llu (%f minutes)\n\n", last_systemtime - first_systemtime, (last_systemtime - first_systemtime) * 1e-7 / 60);
            }
        }
    }

    if(save_packets && log.is_open()) {
        log.close();
    }

    return 0;
}
Пример #15
0
int main (int argc, char **argv) {

  int n;
  int verbosity;
  int max_files;
  int max_words;
  int max_chars;

  int current_word;
  int current_char;
  int start_char;		/* start boundary (possibly > than 0) */

  int no_of_spaces;
  int pos_in_string;

  int i;
  char *current_string;
  char current_temp_filename[500];
  int current_file_number;
  FILE *temp_file;

  flag text_buffer_full;

  char *text_buffer;
  char **pointers;

  char current_ngram[500];
  int current_count;

  int counter;

  char temp_directory[1000];
  char *temp_file_ext;

  flag words_set;
  flag chars_set;

  /* Process command line */

  verbosity = pc_intarg(&argc, argv,"-verbosity",DEFAULT_VERBOSITY);
  pc_message(verbosity,2,"text2wngram\n");

  report_version(&argc,argv);

  if (pc_flagarg( &argc, argv,"-help")) {
    help_message();
    exit(1);
  }

  n = pc_intarg(&argc, argv,"-n",DEFAULT_N);

  /*  max_words = pc_intarg(&argc, argv,"-words",STD_MEM*1000000/11);
  max_chars = pc_intarg(&argc, argv,"-chars",STD_MEM*7000000/11); */

  max_words = pc_intarg(&argc, argv,"-words",-1);
  max_chars = pc_intarg(&argc, argv,"-chars",-1);

  if (max_words == -1) {
    words_set = 0;
    max_words = STD_MEM*1000000/11;
  }else
    words_set = 1;

  if (max_chars == -1) {
    chars_set = 0;
    max_chars = STD_MEM*7000000/11; 
  }else
    chars_set = 1;
  
  max_files = pc_intarg(&argc, argv,"-files",DEFAULT_MAX_FILES);

  if (pc_flagarg(&argc,argv,"-compress"))
    temp_file_ext = salloc(".Z");
  else {
    if (pc_flagarg(&argc,argv,"-gzip"))
      temp_file_ext = salloc(".gz");
    else
      temp_file_ext = salloc("");
  }

  strcpy(temp_directory, "cmuclmtk-XXXXXX");
  if (mkdtemp(temp_directory) == NULL) {
     quit(-1, "Failed to create temporary folder: %s\n", strerror(errno));
  }

  pc_report_unk_args(&argc,argv,verbosity);
 
  if (words_set && !chars_set)
    max_chars = max_words * 7;

  if (!words_set && chars_set)
    max_words = max_chars / 7;

  /* If the last charactor in the directory name isn't a / then add one. */
  
  pc_message(verbosity,2,"n = %d\n",n);
  pc_message(verbosity,2,"Number of words in buffer = %d\n",max_words);
  pc_message(verbosity,2,"Number of chars in buffer = %d\n",max_chars);
  pc_message(verbosity,2,"Max number of files open at once = %d\n",max_files);
  pc_message(verbosity,2,"Temporary directory = %s\n",temp_directory);

  /* Allocate memory for the buffers */

  text_buffer = (char *) rr_malloc(sizeof(char)*max_chars);
  pc_message(verbosity,2,"Allocated %d bytes to text buffer.\n",
	     sizeof(char)*max_chars);

  pointers = (char **) rr_malloc(sizeof(char *)*max_words);
  pc_message(verbosity,2,"Allocated %d bytes to pointer array.\n",
	     sizeof(char *)*max_words);

  current_file_number = 0;

  current_word = 1;
  start_char = 0;
  current_char = 0;
  counter = 0;
  pointers[0] = text_buffer;
      
  while (!feof(stdin)) {

    current_file_number++;

    /* Read text into buffer */
    
    pc_message(verbosity,2,"Reading text into buffer...\n");

    pc_message(verbosity,2,"Reading text into the n-gram buffer...\n");
    pc_message(verbosity,2,"20,000 words processed for each \".\", 1,000,000 for each line.\n");
    
    pointers[0] = text_buffer;
    
    while ((!rr_feof(stdin)) && 
	   (current_word < max_words) && 
	   (current_char < max_chars)) {

      text_buffer[current_char] = getchar();
      if (text_buffer[current_char] == '\n' || 
	  text_buffer[current_char] == '\t' ) {
	text_buffer[current_char] = ' ';
      }
      if (text_buffer[current_char] == ' ') {
	if (current_char > start_char) {
	  if (text_buffer[current_char-1] == ' ') {
	    current_word--;
	    current_char--;
	  }
	  pointers[current_word] = &(text_buffer[current_char+1]);
	  current_word++; 
	  counter++;
	  if (counter % 20000 == 0) {
	    if (counter % 1000000 == 0)
	      pc_message(verbosity,2,"\n");
	    else
	      pc_message(verbosity,2,".");
	  }
	}
      }
      
      if (text_buffer[current_char] != ' ' || current_char > start_char) 
	current_char++;
    }

    text_buffer[current_char]='\0';


    if (current_word == max_words || rr_feof(stdin)) {
      for (i=current_char+1;i<=max_chars-1;i++)
	text_buffer[i] = ' ';

      text_buffer_full = 0;
    }else
      text_buffer_full = 1;
    
    /* Sort buffer */

    pc_message(verbosity,2,"\nSorting pointer array...\n"); 

    qsort((void *) pointers,(size_t) current_word-n,sizeof(char *),cmp_strings);
   
    /* Write out temporary file */

    sprintf(current_temp_filename,"%s/%hu%s",temp_directory, current_file_number, temp_file_ext);

    pc_message(verbosity,2,"Writing out temporary file %s...\n",current_temp_filename);
        
    temp_file = rr_oopen(current_temp_filename);
    text_buffer[current_char] = ' ';
    
    current_count = 0;
    strcpy(current_ngram,"");
    
    for (i = 0; i <= current_word-n; i++) {
      current_string = pointers[i];
      
      /* Find the nth space */

      no_of_spaces = 0;
      pos_in_string = 0;
      while (no_of_spaces < n) {	
	if (current_string[pos_in_string] == ' ')
	  no_of_spaces++;

	pos_in_string++;
      }
      
      if (!strncmp(current_string,current_ngram,pos_in_string))
	current_count++;
      else {
	if (strcmp(current_ngram,""))
	  if (fprintf(temp_file,"%s %d\n",current_ngram,current_count) < 0) 
	    quit(-1,"Error writing to temporary file %s\n",current_temp_filename);

	current_count = 1;
	strncpy(current_ngram,current_string,pos_in_string);
	current_ngram[pos_in_string] = '\0';
      }
    }
    
    rr_oclose(temp_file);

    /* Move the last n-1 words to the beginning of the buffer, and set
       correct current_word and current_char things */

    strcpy(text_buffer,pointers[current_word-n]);
    pointers[0]=text_buffer;
   
    /* Find the (n-1)th space */

    no_of_spaces=0;
    pos_in_string=0;

    if (!text_buffer_full){ 
      while (no_of_spaces<(n-1)) {
	if (pointers[0][pos_in_string]==' ') {
	  no_of_spaces++;
	  pointers[no_of_spaces] = &pointers[0][pos_in_string+1];
	}
	pos_in_string++;
      }
    }else {
      while (no_of_spaces<n) {
	if (pointers[0][pos_in_string]==' ') {
	  no_of_spaces++;
	  pointers[no_of_spaces] = &pointers[0][pos_in_string+1];
	}
	pos_in_string++;
      }
      pos_in_string--;
    }

    current_char = pos_in_string;
    current_word = n;
    /* mark boundary beyond which counting pass cannot backup */
    start_char = current_char;

  }
  /* Merge temporary files */

  pc_message(verbosity,2,"Merging temporary files...\n");

  merge_tempfiles(1,
		  current_file_number,
		  temp_directory,
		  temp_file_ext,
		  max_files,
		  stdout,
		  n,
		  verbosity); 

  rmdir(temp_directory);
  pc_message(verbosity,0,"text2wngram : Done.\n");
  
  return 0;
}
Пример #16
0
int main(int argc, char *argv[]) {

  int verbosity;
  int vocab_size;
  FILE *vocab_file;
  int buffer_size;
  flag write_ascii;
  int max_files;
  int number_of_tempfiles;
  char *vocab_filename;
  char *idngram_filename;
  char temp_word[MAX_WORD_LENGTH];
  char temp_word2[MAX_WORD_LENGTH];
  char temp_word3[MAX_WORD_LENGTH];
  flag contains_unks;
  int position_in_buffer;
  FILE *outfile;
  FILE *tempfile;
  FILE *non_unk_fp;
  ngram_rec *buffer;
  flag same_ngram;
  int i;
  int j;
  int fof_size;
  int size_of_rec;

  char temp_directory[1000];
  char *temp_file_ext;

  /* Vocab hash table things */

  struct idngram_hash_table vocabulary;
  unsigned long hash_size;
  unsigned long M;

  wordid_t *current_ngram;
  int current_count;
  wordid_t *sort_ngram;
  int sort_count;
  
  /* Process command line */

  report_version(&argc,argv);
  
  if (argc == 1 || pc_flagarg(&argc, argv,"-help")) {    
    /* Display help message */    
    help_message();
    exit(1);
  }


  n = pc_intarg( &argc, argv, "-n",DEFAULT_N);
  hash_size = pc_intarg( &argc, argv, "-hash",DEFAULT_HASH_SIZE);
  buffer_size = pc_intarg( &argc, argv, "-buffer",STD_MEM);
  write_ascii = pc_flagarg(&argc,argv,"-write_ascii");
  verbosity = pc_intarg(&argc,argv,"-verbosity",DEFAULT_VERBOSITY);
  max_files = pc_intarg( &argc, argv, "-files",DEFAULT_MAX_FILES);
  fof_size = pc_intarg(&argc,argv,"-fof_size",10);
  vocab_filename = salloc(pc_stringarg( &argc, argv, "-vocab", "" ));
  idngram_filename = salloc(pc_stringarg( &argc, argv, "-idngram", "" ));
  
  if (!strcmp("",vocab_filename)) 
    quit(-1,"Error : Must specify a vocabulary file.\n");

  if (!strcmp("",idngram_filename)) 
    quit(-1,"text2idngram : Error : Must specify idngram file.\n");
    
  if (pc_flagarg(&argc,argv,"-compress")) 
    temp_file_ext = salloc(".Z");
  else {
    if (pc_flagarg(&argc,argv,"-gzip")) 
      temp_file_ext = salloc(".gz");
    else 
      temp_file_ext = salloc("");
  }

  strcpy(temp_directory, "cmuclmtk-XXXXXX");
  if (mkdtemp(temp_directory) == NULL) {
     quit(-1, "Failed to create temporary folder: %s\n", strerror(errno));
  }

  pc_report_unk_args(&argc,argv,verbosity);

  outfile = rr_fopen(idngram_filename,"wb");
  
  pc_message(verbosity,2,"Vocab           : %s\n",vocab_filename);
  pc_message(verbosity,2,"Output idngram  : %s\n",idngram_filename);
  pc_message(verbosity,2,"Buffer size     : %d\n",buffer_size);
  pc_message(verbosity,2,"Hash table size : %d\n",hash_size);
  pc_message(verbosity,2,"Max open files  : %d\n",max_files);
  pc_message(verbosity,2,"n               : %d\n",n);
  pc_message(verbosity,2,"FOF size               : %d\n",fof_size);  

  size_of_rec = (sizeof(wordid_t) * n) + 16 - (( n* sizeof(wordid_t)) % 16);
  buffer_size *= (1000000/((sizeof(ngram_rec) + size_of_rec)));
  fprintf(stderr,"buffer size = %d\n",buffer_size);

  /* Allocate memory for hash table */

  fprintf(stderr,"Initialising hash table...\n");

  M = nearest_prime(hash_size);

  new_idngram_hashtable(&vocabulary,M);

  /* Read in the vocabulary */

  vocab_size = 0;

  vocab_file = rr_iopen(vocab_filename);

  pc_message(verbosity,2,"Reading vocabulary...\n");

  while (fgets (temp_word, sizeof(temp_word),vocab_file)) {
    if (strncmp(temp_word,"##",2)==0) continue;
    sscanf (temp_word, "%s ",temp_word2);

    /* Check for vocabulary order */
    if (vocab_size > 0 && strcmp(temp_word2,temp_word3)<0) 
      quit(-1,"wngram2idngram : Error : Vocabulary is not alphabetically ordered.\n");

    /* Check for repeated words in the vocabulary */

    if (index2(&vocabulary,temp_word2) != 0) 
      warn_on_repeated_words(temp_word);

    warn_on_wrong_vocab_comments(temp_word);

    vocab_size++;
    
    add_to_idngram_hashtable(&vocabulary,idngram_hash(temp_word2,M),temp_word2,vocab_size);
    strcpy(temp_word3,temp_word2);
  }

  if (vocab_size > MAX_VOCAB_SIZE) 
    quit(-1,"Error : Vocabulary size exceeds maximum.\n");
  
  pc_message(verbosity,2,"Allocating memory for the buffer...\n");

  buffer=(ngram_rec *) rr_malloc((buffer_size+1)*sizeof(ngram_rec));
  
  for (i=0;i<=buffer_size;i++) 
    buffer[i].word = (wordid_t *) rr_malloc(n*sizeof(wordid_t));

  /* Open the "non-OOV" tempfile */

  sprintf(temp_word, "%s/1%s", temp_directory, temp_file_ext);
  
  non_unk_fp = rr_fopen(temp_word,"w");

  pc_message(verbosity,2,"Writing non-OOV counts to temporary file %s\n",
	     temp_word);
  number_of_tempfiles = 1;

  current_ngram = (wordid_t *) rr_malloc(n*sizeof(wordid_t));
  sort_ngram = (wordid_t *) rr_malloc(n*sizeof(wordid_t));

  /* Read text into buffer */
  position_in_buffer = 0;

  while (!rr_feof(stdin)) {
    
    for (i=0;i<=n-1;i++) {
      get_word(stdin,temp_word);
      current_ngram[i]=index2(&vocabulary,temp_word);
    }
    if (scanf("%d",&current_count) != 1) 
      if (!rr_feof(stdin)) 
	quit(-1,"Error reading n-gram count from stdin.\n");

    if (!rr_feof(stdin)) {

      contains_unks = 0;
      for (i=0;i<=n-1;i++) {
	if (!current_ngram[i]) 
	  contains_unks = 1;
      }

      if (contains_unks) {
	/* Write to buffer */

	position_in_buffer++;

	if (position_in_buffer >= buffer_size) {

	  /* Sort buffer */
	  pc_message(verbosity,2,
		     "Sorting n-grams which include an OOV word...\n");

	  qsort((void*) buffer,(size_t) position_in_buffer,
		sizeof(ngram_rec),compare_ngrams2);

	  pc_message(verbosity,2,"Done.\n");

	  /* Write buffer to temporary file */

	  number_of_tempfiles++;
	  
	  sprintf(temp_word,"%s/%hu%s", temp_directory,
		  number_of_tempfiles,temp_file_ext);
	  
	  pc_message(verbosity,2,
		     "Writing sorted OOV-counts buffer to temporary file %s\n",
		     temp_word);

	  tempfile = rr_fopen(temp_word,"w");
	  
	  for (i=0;i<=n-1;i++) 
	    sort_ngram[i] = buffer[0].word[i];

	  sort_count = buffer[0].count;

	  for (i=0;i<=position_in_buffer-2;i++) {
	    
	    same_ngram = 1;
	    for (j=n-1;j>=0;j--) {
	      if (buffer[i].word[j] != sort_ngram[j]) {
		same_ngram = 0;
		j = -1;
	      }
	    }

	    if (same_ngram) 
	      sort_count += buffer[i].count;
	    else {
	      for (j=0;j<=n-1;j++) {
		rr_fwrite((char*)&sort_ngram[j],sizeof(wordid_t),1,
			  tempfile,"temporary n-gram ids");
		sort_ngram[j] = buffer[i].word[j];
	      }
	      rr_fwrite((char*)&sort_count,sizeof(int),1,tempfile,
			"temporary n-gram counts");
	      sort_count = buffer[i].count;
	    }
	  }	    
	  for (j=0;j<=n-1;j++) 
	    rr_fwrite((char*)&sort_ngram[j],sizeof(wordid_t),1,
		      tempfile,"temporary n-gram ids");

	  rr_fwrite((char*)&sort_count,sizeof(int),1,tempfile,
		    "temporary n-gram counts");
	  rr_oclose(tempfile);
	  position_in_buffer = 1;

	}
	
	for (i=0;i<=n-1;i++) 
	  buffer[position_in_buffer-1].word[i] = current_ngram[i];

	buffer[position_in_buffer-1].count = current_count;

      }else {
	/* Write to temporary file */
	for (i=0;i<=n-1;i++) 
	  rr_fwrite((char*)&current_ngram[i],sizeof(wordid_t),1,
		    non_unk_fp,"temporary n-gram ids");

	rr_fwrite((char*)&current_count,sizeof(int),1,non_unk_fp,
		  "temporary n-gram counts");
      }
    }
  }

  if (position_in_buffer > 0) {

    /* Only do this bit if we have actually seen some OOVs */
    /* Sort final buffer */    
    pc_message(verbosity,2,"Sorting final buffer...\n");

    qsort((void*) buffer,(size_t) position_in_buffer,
	  sizeof(ngram_rec),compare_ngrams2);
    
    /* Write final buffer */
    
    number_of_tempfiles++;
  
    sprintf(temp_word,"%s/%hu%s", temp_directory,
	    number_of_tempfiles,temp_file_ext);
    
    pc_message(verbosity,2,"Writing sorted buffer to temporary file %s\n", temp_word);

    tempfile = rr_fopen(temp_word,"w");
    
    for (i=0;i<=n-1;i++) 
      sort_ngram[i] = buffer[0].word[i];

    sort_count = buffer[0].count;
    
    for (i=1;i<=position_in_buffer-1;i++) {
      
      same_ngram = 1;
      for (j=n-1;j>=0;j--) {
	if (buffer[i].word[j] != sort_ngram[j]) {
	  same_ngram = 0;
	  j = -1;
	}
      }
      
      if (same_ngram) 
	sort_count += buffer[i].count;
      else {
	for (j=0;j<=n-1;j++) {
	  rr_fwrite((char*)&sort_ngram[j],sizeof(wordid_t),1,
		    tempfile,"temporary n-gram ids");
	  sort_ngram[j] = buffer[i].word[j];
	}
	rr_fwrite((char*)&sort_count,sizeof(int),1,tempfile,
		  "temporary n-gram counts");
	sort_count = buffer[i].count;
      }
    }	    
    for (j=0;j<=n-1;j++) 
      rr_fwrite((char*)&sort_ngram[j],sizeof(wordid_t),1,
		tempfile,"temporary n-gram ids");

    rr_fwrite((char*)&sort_count,sizeof(int),1,tempfile,
	      "temporary n-gram counts");
    fclose(tempfile);
    

  }
  

  /* Merge the temporary files, and output the result */
  fclose(non_unk_fp);
  pc_message(verbosity,2,"Merging temporary files...\n");
  merge_idngramfiles(1,
		     number_of_tempfiles,
		     temp_directory,
		     temp_file_ext,
		     max_files,
		     outfile,
		     write_ascii,
		     fof_size,
		     n);

  fclose(outfile);

  rmdir(temp_directory);
  pc_message(verbosity,0,"wngram2idngram : Done.\n");

  return 0;
}
Пример #17
0
//---------------------------------------------------------------------------------
int main (int argc, char **argv) {
    //Struct for MPI_Allreduce (MAX_LOC)
    struct {
	double value;
	int rank;
    } local_in, global_out;

    int ierr = 0;
    int i, j, x, y, owner_rank, numtasks, rank, k;
    
    MPI_Init (&argc, &argv); 
    MPI_Comm_size (MPI_COMM_WORLD, &numtasks);
    MPI_Comm_rank (MPI_COMM_WORLD, &rank);
    
    ierr = parse_command_line (argc, argv);
    if (ierr) { 
       MPI_Root (help_message ());
       MPI_Abort (MPI_COMM_WORLD, 000);
    }     
    MPI_Root (echo_parameters ());
    
    double *matrix = malloc (NREPS * CSIZE * RSIZE *  sizeof(double));
    double pp_max; int pp_loc;
    double t1, t2;

    MPI_Assert(numtasks, NPROC, 
	    "Number of processors specified is not consistent", 501);

    //MPI_Root (printf("Detected %d tasks. Root is %d.\n", numtasks, rank));

    Assert(init_rand(matrix, rank+1), 0, "Failed initialization!");

    //MPI_Root (printf ("\n Initial Matrix \n--------------------------------------------\n"));
    //for (k=0; k<NREPS; ++k)
	//for (i=0; i<numtasks; ++i) {
	//    if (i == rank)
	//	Assert(disp_formatted(matrix, rank, k),
	//		0,"Failed display!");
	//    MPI_Barrier (MPI_COMM_WORLD);
	//}

    double *operating_row = malloc (1 * RSIZE * sizeof(double));
    int row_start_index, col_start_index, dest, src, iters;
    int tag1 = 351, tag2 = 451, tag3 = 551;
    int pp_row_no=0, pp_col_no, pp_dummy_rank;
    MPI_Status status;

    //Define Derived MPI Datatype for communication
    MPI_Datatype MPI_ROW;
    MPI_Type_contiguous (RSIZE, MPI_DOUBLE, &MPI_ROW);
    MPI_Type_commit (&MPI_ROW);

    //Fix number of iterations and output system type
    iters = fmin (NROWS, NCOLS);
    /*
    if (NROWS>NCOLS) {	
	MPI_Root (printf ("\nSystem is over-determined. Row Count = %d, Column Count = %d.\n", NROWS, NCOLS)); }
    else if (NCOLS>NROWS) {
	MPI_Root (printf ("\nSystem is under-determined. Row Count = %d, Column Count = %d.\n", NROWS, NCOLS)); }
    else {
	MPI_Root (printf ("\nSystem is square. Row Count = Column Count = %d.\n", NCOLS)); }
	*/

    //Barrier for timing synchronization
    MPI_Barrier (MPI_COMM_WORLD);
    t1 = MPI_Wtime ();

    for (i=0; i<iters; ++i) { 
	global_to_local_map (i,i,rank,&x,&y,&owner_rank);
	row_start_index = x;
	col_start_index = y;
	if (rank == owner_rank) {
	    if (fabs(matrix[index(x,y)])<1E-14) {
		printf ("\nMatrix is singular or very nearly singular. Aborting. "
			"Error encountered while evaluating row %d.\n", i);
		MPI_Abort (MPI_COMM_WORLD, 999);
	    }
	    row_start_index += 1;
	    vec_to_vec (&matrix[index(x,0)], &operating_row[0]);
	}
	MPI_Bcast (&operating_row[0], 1, MPI_ROW, owner_rank, MPI_COMM_WORLD);
	if (x!=-1 && y!=-1) 
	    gauss_step (matrix, operating_row, row_start_index, col_start_index);
    }

    MPI_Barrier (MPI_COMM_WORLD);
    t2 = MPI_Wtime ();

    /*
    MPI_Root (printf ("\n RREF Matrix \n--------------------------------------------\n"));
    for (k=0; k<NREPS; ++k)
	for (i=0; i<numtasks; ++i) {
	    if (i == rank)
		Assert(disp_formatted(matrix, rank, k),
			0,"Failed display!");
	    MPI_Barrier (MPI_COMM_WORLD);
	}
	*/

    //MPI_Root(printf("\nReached End of Program\n"));
    MPI_Root(printf("GE algorithm took %f seconds\n", t2-t1));
    MPI_Finalize ();
    return 0;
}
Пример #18
0
void help_on_meter_graph(void)
{
int mmg_no;
int event_no;
// Nothing is selected in the data area.
mmg_no=312;
for(event_no=0; event_no<MAX_MGBUTT; event_no++)
  {
  if( mgbutt[event_no].x1 <= mouse_x && 
      mgbutt[event_no].x2 >= mouse_x &&      
      mgbutt[event_no].y1 <= mouse_y && 
      mgbutt[event_no].y2 >= mouse_y) 
    {
    switch (event_no)
      {
      case MG_TOP:
      case MG_BOTTOM:
      case MG_LEFT:
      case MG_RIGHT:
      mmg_no=102;
      break;

      case MG_CHANGE_CAL:
      mmg_no=90;
      break;

      case MG_CHANGE_TYPE:
      mmg_no=91;
      break;

      case MG_INCREASE_AVGN:
      mmg_no=92;
      break;
  
      case MG_DECREASE_AVGN:
      mmg_no=93;
      break;
  
      case MG_INCREASE_GAIN:
      mmg_no=94;
      break;
  
      case MG_DECREASE_GAIN:
      mmg_no=95;
      break;
  
      case MG_INCREASE_YREF:
      mmg_no=96;
      break;
  
      case MG_DECREASE_YREF:
      mmg_no=97;
      break;
   
      case MG_CHANGE_TRACKS:
      mmg_no=98;
      break;
      }
    }  
  }
help_message(mmg_no);
}  
Пример #19
0
int main(int argc, char **argv, char **envp)
{
    int pid, c;
    const char *prj;
    char buf[4096];
    char buf2[4096];
    int flag_d = 0;
    int __optind = 0;
    int nscr = 0;
    char *ptr;

    environ = envp;
    radare_init();

    while ((c = getopt(argc, argv, "l:fs:hb:wLvuVnxi:e:p:d")) != -1) {
        switch( c ) {
        case 'd':
            flag_d = 1;
            __optind = optind;
            break;
        case 'i':
            config.script[nscr++] = optarg;
            config.script[nscr] = NULL;
            break;
        case 'f':
            config.block_size = 0;
            break;
        case 'n':
            config.noscript = 1;
            break;
        case 'p':
            config_set("file.project", optarg);
            break;
        case 'w':
            config_set("file.write", "true");
            break;
        case 's':
            config.seek = (ut64)get_offset(optarg);
            if (config.seek < 0) config.seek = (ut64)0;
            config_set("dbg.bep", optarg);
            break;
        case 'l':
            plugin_registry(optarg);
            break;
        case 'L':
            return plugin_list();
        case 'b':
            config.block_size = (size_t)get_offset(optarg);
            config.block = (unsigned char *)realloc(config.block, config.block_size);
            config_set_i("cfg.bsize", config.block_size);
            break;
        case 'V':
            printf("radare %s %dbit on %s%dbit "TARGET" %s%s\n", VERSION,
                   sizeof(ut64)*8, (LIL_ENDIAN)?"le":"be", sizeof(void *)*8,
                   (DEBUGGER)?   "dbg "   :"", (HAVE_GUI)? "gui"   :"");
            return 0;
        case 'u':
            config.unksize = 1;
            break;
        case 'x':
            config.mode = MODE_HEXDUMP;
            break;
        case 'e':
            config_eval(optarg);
            break;
        case 'h':
            help_message(0);
            return 0;
        case 'v':
            config_set("cfg.verbose", "false");
            break;
        default:
            return 1;
        }
    }

    if (optind < argc)
        config.file = argv[optind++];

    if (!flag_d && optind < argc)
        eprintf("warning: Only the first file has been opened.\n");

    /* TODO: we have to recheck this :) */
    if (flag_d) {
        optind--;
        config.debug = 1;
        prj = config_get("file.project");
        if (__optind==argc) {
            if (prj == NULL)
                return help_message(1);
            snprintf(buf2, 4095, "dbg://%s", project_get_file(prj) );
            config.file = strdup(buf2);
        } else {
            // XXX : overflowable, must use strcatdup or stgh like that
            // TODO: support hex pids too
            pid = atoi(argv[optind]);
            buf[0]='\0';

            /* by process-id */
            if (pid > 0) {
                sprintf(buf2, "pid://%d", pid);
                config.file = strdup(buf2);
            } else {
#if DEBUGGER
                /* by program path */
                for(c=optind; argv[c]; c++) {
                    char *arg = argv[c];
                    if (c == optind)
                        arg = (char *)resolve_path(argv[c]);
                    ps.argv[c-optind] = arg;
                    strcat(buf, arg);
                    if (argv[c+1])
                        strcat(buf, " ");
                }
                ps.args = strdup(buf);
                if (strstr(buf, "://"))
                    strcpy(buf2, buf);
                else sprintf(buf2, "dbg://%s", buf);
                config.file = strdup(buf2);
                ps.filename = strdup(buf2);
                //ptr = strchr(config.file, ' ');
                //if (ptr) *ptr = '\0';
                //config.file = strdup(buf2);
#else
                eprintf("TODO: Needs debugger\n");
#endif
            }
        }
        config_set("cfg.debug", "true");
    }

    plugin_load();

    return radare_go();
}
Пример #20
0
int main(int argc, char **argv) {
   char c, *id = NULL, *spoof_addr = NULL;

   opterr = 0;

   while ((c = getopt(argc, argv, "hVi:m:")) != -1) {
      switch(c) {
         case 'h':
            help_message();
            return 0;
         case 'V':
            printf("%s\n", version);
            return 0;
         case 'i':
            if (validate_interface(optarg)) {
               id = calloc(1, strlen(optarg)+1);
               strcpy(id, optarg);
            }
            else {
               fprintf(stderr, "Invalid interface specified after -i.\n");
               return 1;
            }
            break;
         case 'm':
            if (validate_address(optarg)) {
               spoof_addr = calloc(1, strlen(optarg)+1);
               strcpy(spoof_addr, optarg);
            }
            else {
               fprintf(stderr, "Invalid address specified after -m.\n");
               return 1;
            }
            break;
         case '?':
            if (optopt == 'i' || optopt == 'm') {
               fprintf(stderr, "Option -%c requires an argument.\n", optopt);
               return 1;
            }
            else if (isprint(optopt)) {
               fprintf(stderr, "Unknown option -%c.\n", optopt);
               return 1;
            }
            else {
               fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
               return 1;
            }
         default:
            abort(); /* if we get here, bad things have happened */
      }
   }
   if (geteuid() != 0) {
      fprintf(stderr, "This tools needs to be run as root.\n");
      return 1;
   }
   if (!id) { 
      fprintf(stderr, "No interface supplied.\n");
      return 1;
   }
   if (!spoof_addr) { 
      spoof_addr = generate_mac();
      spoof_addr[ADDR_BYTES-1] = '\0';
   }
   spoof_interface_mac(id, spoof_addr);
   sleep(2);
   bounce_interface(id);
   sleep(1);
   return 0;
}
Пример #21
0
void print_help(char *program_name)
{
    printf("%s",help_message(program_name));
}
Пример #22
0
/*!
 * THE MAIN FUNCTION
 */
int main ( int const argc ,
	   char const * const argv [] ) {
  help_message ( argv [ 0 ] ) ;
  return 0 ; 	      
}
Пример #23
0
int main(int argc, char *argv[])
{
	int ndata;
	int n;
	double width;
	double init, fin;
	int ninit, nfin;
	double *x,*y, *sig;
	double a, b, siga, sigb;
	double chi2, q;
	int mwt;
	char c;
	int t;

	double ave;
	double error;
	int i,j;
	FILE *ifile;
	double minx;
	double errx;

	//There is no blank line separating data in the data file.
	int index=1;
	
	if(strcmp(argv[1],"--h")==0) help_message();
	
	if(strcmp(argv[1], "3")==0) {
		mwt=1;
	}
	else 
	{
		mwt=0;
	}


	
	sscanf(argv[1], "%d", &n);
	sscanf(argv[3], "%lf", &width);
	sscanf(argv[4], "%lf", &init);
	fin=init+width;
	
	printf("#minx\t y_int\t y_int_err\t slope\t slope_err\t chi2\t q\n");
	while(1){
		num_of_line(argv[2], index, init, fin, &ndata, &ninit, &nfin);
		//Terminate when there is no data to fit, because the access point has already reached EOF or the block size width is too small.
		if(ndata==0) exit(-1);


		if((ifile=fopen( argv[2], "r"))==NULL) {
			printf( "File open error : %s\n\a", argv[2]);
			exit(-1);
		}

		x=(double *) calloc(ndata+1, sizeof(double));
		y=(double *) calloc(ndata+1, sizeof(double));
		sig = (double *) calloc(ndata+1, sizeof(double));

		t=1;

		while(t!=index){
			if(blank_line(ifile)) {t++;}
			else move_to_next_line(ifile);
		}
		for(i=0;i<ninit-1;i++) move_to_next_line(ifile);
		for (i=1;i<=ndata;i++){
			if(fscanf(ifile, "%lf", x+i)==EOF) {
				ndata=i-1;
				break;
			}
			if(fscanf(ifile, "%lf", y+i)==EOF) {
				ndata=i-1;
				break;
			}
			if(mwt) 	if(fscanf(ifile, "%lf", sig+i)==EOF) {
				ndata=i-1;
				break;
			}
		}

		fit(x, y, ndata, sig, mwt, &a, &b, &siga, &sigb, &chi2, &q); 
		ave_error1(ndata, x, &minx, &errx);

		//printf("%f\t %.15e\t %.15e\t %.15e\t %.15e\t %lf\t %lf\n", minx, a,siga, b,sigb,chi2,q);
		printf("%f\t %.15e\t %.15e\t %.15e\t %.15e\t %lf\t %lf\n", minx, a,siga, b,sigb,chi2,q);

		free(x);
		free(y);
		free(sig);
		init+=width;
		fin+=width;
	}
}