struct arg_file* arg_file1(const char* shortopts, const char* longopts, const char *datatype, const char *glossary) { return arg_filen(shortopts,longopts,datatype,1,1,glossary); }
int main(int argc, char *argv[]) { // First: Sort out the command line. struct arg_file *files_to_verify = arg_filen(NULL, NULL, "<table file>", 0, 100000, "GRT parts to verify."); struct arg_dbl *percent = arg_dbl0(NULL, "percent", "[0-100]", "Percent of chains to randomly verify."); struct arg_int *sequential = arg_int0(NULL, "sequential", "<n>", "Check every nth chain"); /* void *argtable[] = {table_files, output_directory, bits, move_source, end}; // Get arguments, collect data, check for basic errors. if (arg_nullcheck(argtable) != 0) { printf("error: insufficient memory\n"); } // Look for errors int nerrors = arg_parse(argc,argv,argtable); if (nerrors > 0) { // Print errors, exit. arg_print_errors(stdout,end,argv[0]); // Print help. printf("\n\nOptions: \n"); arg_print_glossary(stdout,argtable," %-20s %s\n"); exit(1); } */ }
int cargs_parse(int argc, char *argv[], bstring *input_fn, bstring *output_fn, bstring *log_fn) { struct arg_lit *help, *version; struct arg_file *arg_input_fn, *arg_output_fn, *arg_log_fn; struct arg_end *end; void *argtable[] = { help = arg_litn("h", "help", 0, 100, "display this help and exit"), version = arg_litn("v", "version", 0, 100, "display version info and exit"), arg_input_fn = arg_filen("i", "input", "input_file", 1, 1, "input file (required)"), arg_output_fn = arg_filen("o", "output", "output_file", 0, 1, "output file (optional)"), arg_log_fn = arg_filen("l", "log", "log_file", 0, 1, "log file (optional)"), end = arg_end(20), }; int rc = 0; int nerrors = 0; nerrors = arg_parse(argc, argv, argtable); // special case: help and version take precedence over error reporting if(help->count >= 1) { rc = cargs_print_help(argtable, help); goto exit; } if(version->count >= 1) { rc = cargs_print_version(version); goto exit; } // If the parser returned any errors, then display them and exit. if(nerrors > 0) { rc = cargs_print_error(end); goto exit; } cargs_export_file_name(arg_input_fn, arg_output_fn, arg_log_fn, input_fn, output_fn, log_fn); exit: arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return rc; }
int main(int argc, char * argv[]) { void* argtable[] = { input = arg_filen( "i", "input", "<string>", 0, 100, "input file") , o_validate = arg_strn( "v", "validate", "<string>", 0, 10, "validate operations") , o_h = arg_file0( NULL, "output-h", "<string>", "output h file dir") , o_lib_c = arg_file0( NULL, "output-lib-c", "<string>", "output c lib file") , o_lib_c_arg = arg_str0( NULL, "output-lib-c-arg", "<string>", "output c lib file") , o_lib_bin = arg_file0( NULL, "output-lib-bin", "<string>", "output c lib file") , help = arg_lit0( NULL, "help", "print this help and exit") , end = arg_end(20) }; struct error_monitor em_buf; error_monitor_t em; int rv; int nerrors; cpe_error_monitor_init(&em_buf, cpe_error_log_to_consol, 0); em = &em_buf; rv = -1; if (arg_nullcheck(argtable) != 0) { CPE_ERROR(em, "init arg table fail!"); goto exit; } nerrors = arg_parse(argc,argv,argtable); if (help->count > 0) { printf("Usage: %s", argv[0]); arg_print_syntax(stdout,argtable,"\n"); rv = 0; goto exit; } if (nerrors > 0) { arg_print_errors(stdout, end, argv[0]); printf("Try '%s --help' for more information.\n", argv[0]); goto exit; } rv = tools_main(em); exit: arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return rv; }
int main(int argc, char *argv[]) { /* the global arg_xxx structs are initialised within the argtable */ void *argtable[] = { help = arg_lit0(NULL, "help", "display this help and exit"), version = arg_lit0(NULL, "version", "display version info and exit"), a = arg_lit0("a", NULL,"the -a option"), b = arg_lit0("b", NULL, "the -b option"), c = arg_lit0("c", NULL, "the -c option"), scal = arg_int0(NULL, "scalar", "<n>", "foo value"), verb = arg_lit0("v", "verbose", "verbose output"), o = arg_file0("o", NULL, "myfile", "output file"), file = arg_filen(NULL, NULL, "<file>", 0, 100, "input files"), end = arg_end(20), }; int exitcode = 0; char progname[] = "testargtable3.exe"; int nerrors; nerrors = arg_parse(argc,argv,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("Usage: %s", progname); arg_print_syntax(stdout, argtable, "\n"); printf("List information about the FILE(s) " "(the current directory by default).\n\n"); arg_print_glossary(stdout, argtable, " %-25s %s\n"); exitcode = 0; goto exit; } /* If the parser returned any errors then display them and exit */ if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout, end, progname); printf("Try '%s --help' for more information.\n", progname); exitcode = 1; goto exit; } exit: /* deallocate each non-null entry in argtable[] */ arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return exitcode; }
int main(int argc, char **argv) { // parameter structures struct arg_lit *param_stop = arg_lit0("p", "stop", "stop animation"); struct arg_lit *param_single = arg_lit0("s", "single", "single animation"); struct arg_lit *param_loop = arg_lit0("l", "loop", "loop animation"); struct arg_int *param_frame = arg_int0("f", "frame", "<n>", "frame data"); struct arg_int *param_pos = arg_int0("p", "pos", "<n>", "frame pos in eeprom"); struct arg_int *param_delay = arg_int0("d", "delay", "<n>", "frame delay in eeprom"); struct arg_lit *param_save = arg_lit0("v", "save", "save one frame to the EEPROM (position, delay and frame required)"); //struct arg_lit *param_tty = arg_lit0("t", "terminal", "terminal mode"); //struct arg_lit *param_daemon = arg_lit0("d", "daemon", "daemon mode"); struct arg_lit *param_help = arg_lit0("h", "help", "show help"); struct arg_file *param_file = arg_filen("i", "ihex-file","<file>", 0, 1, "iHex input file to write to the EEPROM"); struct arg_end *param_end = arg_end(20); // default parameter param_frame->ival[0] = 0x00000000; param_pos->ival[0] = 0; param_delay->ival[0] = 1; // argtable array void *argtable[] = { param_stop, param_single, param_loop, param_frame, param_pos, param_delay, param_save, param_file, //param_tty, //param_daemon, param_help, param_end }; // parse the commandline arguments int nerrors = arg_parse(argc,argv,argtable); if (arg_nullcheck(argtable) != 0) { printf("error: insufficient memory\n"); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return 0; } if (nerrors == 0 && param_help->count > 0) { printf("usage: ./clcc <OPTIONS>\n"); arg_print_glossary(stdout, argtable, "\t%-25s %s\n"); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return 0; } // parsing failed or to less parameter given if (nerrors > 0 || argc < 2 || (param_save->count > 0 && (param_pos->count == 0 || param_delay->count == 0 || param_frame->count == 0)) ) { arg_print_errors(stdout,param_end,"clcc"); printf("usage: ./clcc <OPTIONS>\n"); arg_print_syntaxv(stdout,argtable,"\n"); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return -1; } if (lc_init() != SUCCESSFULLY_CONNECTED) { arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); lc_close(); return -2; } if (param_stop->count > 0) { printf("stop animation loop\n"); lc_setMode(MODE_ANIMATION_STOP); } else if (param_loop->count > 0) { printf("starting animation loop\n"); lc_setMode(MODE_ANIMATION_LOOP); } else if (param_single->count > 0 ) { printf("starting animation as single shot\n"); lc_setMode(MODE_ANIMATION_SINGLE); } else if (param_save->count > 0 && param_pos->count > 0 && param_delay->count > 0 && param_frame->count > 0) { lc_setMode(MODE_ANIMATION_STOP); int frame = param_frame->ival[0]; int delay = param_delay->ival[0]; int pos = param_pos->ival[0]; printf("saving frame: index=%d delay=%d frame=0x%08x\n", pos, delay, frame); lc_saveFrame(frame, delay, pos); } else if (param_frame->count > 0) { lc_setMode(MODE_ANIMATION_STOP); int frame = param_frame->ival[0]; printf("view frame: data=0x%08x\n", frame); lc_setFrame(frame); } //printf("file[%d]=%s\n",i,file->filename[i]);*/ lc_close(); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return 0; }
int main(int argc, char **argv) { /* SYNTAX 1: insert [-nvR] <file> [file]... -o <file> */ struct arg_rex *cmd1 = arg_rex1(NULL, NULL, "insert", NULL, REG_ICASE, NULL); struct arg_lit *noact1 = arg_lit0("n", NULL, "take no action"); struct arg_lit *verbose1 = arg_lit0("v", "verbose", "verbose messages"); struct arg_lit *recurse1 = arg_lit0("R", NULL, "recurse through subdirectories"); struct arg_file *infiles1 = arg_filen(NULL, NULL, NULL, 1,argc+2, "input file(s)"); struct arg_file *outfile1 = arg_file0("o", NULL, "<output>", "output file (default is \"-\")"); struct arg_end *end1 = arg_end(20); void* argtable1[] = {cmd1,noact1,verbose1,recurse1,infiles1,outfile1,end1}; int nerrors1; /* SYNTAX 2: remove [-nv] <file> */ struct arg_rex *cmd2 = arg_rex1(NULL, NULL, "remove", NULL, REG_ICASE, NULL); struct arg_lit *noact2 = arg_lit0("n", NULL, NULL); struct arg_lit *verbose2 = arg_lit0("v", "verbose", NULL); struct arg_file *infiles2 = arg_file1(NULL, NULL, NULL, NULL); struct arg_end *end2 = arg_end(20); void* argtable2[] = {cmd2,noact2,verbose2,infiles2,end2}; int nerrors2; /* SYNTAX 3: search [-v] <pattern> [-o <file>] [--help] [--version] */ struct arg_rex *cmd3 = arg_rex1(NULL, NULL, "search", NULL, REG_ICASE, NULL); struct arg_lit *verbose3 = arg_lit0("v", "verbose", NULL); struct arg_str *pattern3 = arg_str1(NULL, NULL, "<pattern>", "search string"); struct arg_file *outfile3 = arg_file0("o", NULL, "<output>", NULL); struct arg_end *end3 = arg_end(20); void* argtable3[] = {cmd3,verbose3,pattern3,outfile3,end3}; int nerrors3; /* SYNTAX 4: [-help] [-version] */ struct arg_lit *help4 = arg_lit0(NULL,"help", "print this help and exit"); struct arg_lit *version4 = arg_lit0(NULL,"version", "print version information and exit"); struct arg_end *end4 = arg_end(20); void* argtable4[] = {help4,version4,end4}; int nerrors4; const char* progname = "multisyntax"; int exitcode=0; /* verify all argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable1)!=0 || arg_nullcheck(argtable2)!=0 || arg_nullcheck(argtable3)!=0 || arg_nullcheck(argtable4)!=0 ) { /* NULL entries were detected, some allocations must have failed */ printf("%s: insufficient memory\n",progname); exitcode=1; goto exit; } /* set any command line default values prior to parsing */ outfile1->filename[0]="-"; outfile3->filename[0]="-"; /* Above we defined a separate argtable for each possible command line syntax */ /* and here we parse each one in turn to see if any of them are successful */ nerrors1 = arg_parse(argc,argv,argtable1); nerrors2 = arg_parse(argc,argv,argtable2); nerrors3 = arg_parse(argc,argv,argtable3); nerrors4 = arg_parse(argc,argv,argtable4); /* Execute the appropriate main<n> routine for the matching command line syntax */ /* In this example program our alternate command line syntaxes are mutually */ /* exclusive, so we know in advance that only one of them can be successful. */ if (nerrors1==0) exitcode = mymain1(noact1->count, verbose1->count, recurse1->count, outfile1->filename[0], infiles1->filename, infiles1->count); else if (nerrors2==0) exitcode = mymain2(noact2->count, verbose2->count, infiles2->filename[0]); else if (nerrors3==0) exitcode = mymain3(verbose3->count, pattern3->sval[0], outfile3->filename[0]); else if (nerrors4==0) exitcode = mymain4(help4->count, version4->count, progname, argtable1, argtable2, argtable3, argtable4); else { /* We get here if the command line matched none of the possible syntaxes */ if (cmd1->count > 0) { /* here the cmd1 argument was correct, so presume syntax 1 was intended target */ arg_print_errors(stdout,end1,progname); printf("usage: %s ", progname); arg_print_syntax(stdout,argtable1,"\n"); } else if (cmd2->count > 0) { /* here the cmd2 argument was correct, so presume syntax 2 was intended target */ arg_print_errors(stdout,end2,progname); printf("usage: %s ", progname); arg_print_syntax(stdout,argtable2,"\n"); } else if (cmd3->count > 0) { /* here the cmd3 argument was correct, so presume syntax 3 was intended target */ arg_print_errors(stdout,end3,progname); printf("usage: %s ", progname); arg_print_syntax(stdout,argtable3,"\n"); } else { /* no correct cmd literals were given, so we cant presume which syntax was intended */ printf("%s: missing <insert|remove|search> command.\n",progname); printf("usage 1: %s ", progname); arg_print_syntax(stdout,argtable1,"\n"); printf("usage 2: %s ", progname); arg_print_syntax(stdout,argtable2,"\n"); printf("usage 3: %s ", progname); arg_print_syntax(stdout,argtable3,"\n"); printf("usage 4: %s", progname); arg_print_syntax(stdout,argtable4,"\n"); } } exit: /* deallocate each non-null entry in each argtable */ arg_freetable(argtable1,sizeof(argtable1)/sizeof(argtable1[0])); arg_freetable(argtable2,sizeof(argtable2)/sizeof(argtable2[0])); arg_freetable(argtable3,sizeof(argtable3)/sizeof(argtable3[0])); arg_freetable(argtable4,sizeof(argtable4)/sizeof(argtable4[0])); return exitcode; }
int main(int argc, char** argv) { int exitstatus = 0; /* declare CL args */ arg_lit_t *help = (arg_lit_t*) arg_lit0("h", "help", "prints the command glossary"); arg_lit_t *myip = (arg_lit_t*) arg_lit0("m", NULL, "prints the external ip of the interface currently being used"); arg_lit_t *bounce_opt = (arg_lit_t*) arg_lit0("b", NULL, "creates a bouncer to send the message received back to the sender"); arg_lit_t *listen_opt = (arg_lit_t*) arg_lit0("l", NULL, "creates a listener to print the messages received"); arg_file_t *proto = (arg_file_t*) arg_filen("p", "protocol", "acronym", 0, 1, "specify the protocol being manipulated"); arg_file_t *source = (arg_file_t*) arg_filen("s", "source", "x.x.x.x", 0, 1, "specify the source IP"); arg_file_t *dest = (arg_file_t*) arg_filen("d", "dest", "x.x.x.x", 0, 1, "specify the destination IP"); arg_int_t *sport = (arg_int_t*) arg_intn(NULL, "srcport", "short", 0, 1, "specify the source port if applicable"); arg_int_t *dport = (arg_int_t*) arg_intn(NULL, "dstport", "short", 0, 1, "specify the destination port if applicable"); arg_str_t *mcontent = (arg_str_t*) arg_strn(NULL, NULL, "string", 0, 1, "message content as a string"); arg_end_t *end = (arg_end_t*) arg_end(20); void *argtable[] = {help,myip,bounce_opt,listen_opt,proto,source, dest,sport,dport,mcontent,end}; if(arg_nullcheck(argtable) != 0) { fprintf(stderr, "error: insufficient memory"); exitstatus = -1; goto exit_prog; } /* parse and act */ int nerrors = arg_parse(argc,argv,argtable); if(nerrors == 0) { char sourceipbuf[INET6_ADDRSTRLEN]; size_t contentlen = 0; char message_content[MAX_MESSAGELEN + 1]; /* get glossary */ if(help->count) { arg_print_glossary(stdout, argtable, "%-25s %s\n"); } /* get current IP address */ else if(myip->count) { if(getmyip(sourceipbuf) == 0) { printf("Your packets will have the source IP address %s\n", sourceipbuf); } else { fprintf(stderr, "error: could not get your IP address.\n"); exitstatus = -1; goto exit_prog; } } /* start bouncer */ else if(bounce_opt->count) { if(!proto->count) { fprintf(stderr, "error: expected <protocol> specified.\n"); exitstatus = -1; goto exit_prog; } enum Protocol protocol = parse_protocol(proto->filename[0]); if(protocol == proto_UDP) { if(!sport->count) { fprintf(stderr, "error: expected <srcport> specified.\n"); exitstatus = -1; goto exit_prog; } printf("Starting bouncer for UDP packets on port %u...\n", sport->ival[0]); /* start_udp_listener(sport->ival[0], bounce_udp_packet);*/ } else { fprintf(stderr, "Bouncing for %s packets is not supported.\n", proto->filename[0]); exitstatus = -1; goto exit_prog; } } /* start listener */ else if(listen_opt->count) { if(!proto->count) { fprintf(stderr, "error: expected <protocol> specified.\n"); exitstatus = -1; goto exit_prog; } enum Protocol protocol = parse_protocol(proto->filename[0]); if(protocol == proto_UDP) { if(!sport->count) { fprintf(stderr, "error: expected <srcport> specified.\n"); exitstatus = -1; goto exit_prog; } printf("Starting listener for UDP packets on port %u...\n", sport->ival[0]); char filter[FILTER_BUFLEN]; memset(filter, 0, FILTER_BUFLEN); sprintf(filter, "udp dst port %i", sport->ival[0]); start_listener(filter, print_udp_packet); /* start_udp_listener(sport->ival[0], print_packet);*/ } else { fprintf(stderr, "Listening for %s packets is not supported.\n", proto->filename[0]); exitstatus = -1; goto exit_prog; } } /* send packet */ else { /* take into account stdin reading if necessary */ if(!mcontent->count) { contentlen = read(STDIN_FILENO, message_content, MAX_MESSAGELEN); if(contentlen < 0) { fprintf(stderr, "error: could not read message from stdin.\n"); perror("read"); exitstatus = -1; goto exit_prog; } message_content[contentlen] = '\0'; } else { int tempstrlen = strlen(mcontent->sval[0]); contentlen = tempstrlen > MAX_MESSAGELEN ? MAX_MESSAGELEN : tempstrlen; memcpy(message_content, mcontent->sval[0], contentlen); message_content[contentlen] = '\0'; } if(!proto->count || !dest->count) { fprintf(stderr, "error: expected <protocol> and <dest> specified.\n"); exitstatus = -1; goto exit_prog; } if(!source->count) { if(getmyip(sourceipbuf) != 0) { fprintf(stderr, "error: could not get your IP address.\n"); exitstatus = -1; goto exit_prog; } } else { strncpy(sourceipbuf, source->filename[0], INET6_ADDRSTRLEN); } enum Protocol protocol = parse_protocol(proto->filename[0]); if(protocol == proto_ICMP) { time_t t; if(time(&t) == -1) { fprintf(stderr, "error: could not get timestamp.\n"); exitstatus = -1; goto exit_prog; } printf("Sending ICMP ping packet...\nSource -> %s\n" "Destination -> %s\n" "Message -> %i\n", sourceipbuf, dest->filename[0], (int) t); /* construct ICMP header */ int err; int payloadsize = sizeof(icmpheader_t) + sizeof(time_t); char ip_payload[payloadsize]; /* copy in timestamp */ /* we must do this first for the checksum calculation */ t = htonl(t); memcpy(ip_payload + sizeof(icmpheader_t), &t, sizeof(time_t)); /* identifier is lower 16 bits, sequence number is upper 16 bits */ uint32_t rest = htons(0x00); rest <<= 16; rest |= htons(0x7b); if((err = fill_icmp_header((icmpheader_t*) ip_payload, 8, 0, rest, sizeof(time_t))) != 0) { fprintf(stderr, "error: could not fill icmp header, returned %i.\n", err); exitstatus = -1; goto exit_prog; } /* send the ip packet */ ipheader_t iph; iph.ip_p = 1; /* ICMP */ inet_aton(sourceipbuf, (struct in_addr*) &iph.ip_src); inet_aton(dest->filename[0], (struct in_addr*) &iph.ip_dst); if((err = send_ip_packet(&iph, ip_payload, payloadsize)) != 0) { fprintf(stderr, "error: could not send ip packet, returned %i.\n", err); exitstatus = -1; goto exit_prog; } } else if(protocol == proto_UDP) { /* get port info */ unsigned short srcport = sport->count ? (unsigned short) sport->ival[0] : 0; if(!dport->count) { fprintf(stderr, "error: <dstport> specified.\n"); exitstatus = -1; goto exit_prog; } unsigned short dstport = (unsigned short) dport->ival[0]; printf("Sending UDP packet...\nSource -> %s:%i\n" "Destination -> %s:%i\n" "Message Length -> %u bytes\n", sourceipbuf, srcport, dest->filename[0], dstport, (unsigned int) contentlen); /* construct UDP header */ int err; int payloadsize = sizeof(udpheader_t) + contentlen; char ip_payload[payloadsize]; if((err = fill_udp_header((udpheader_t*) ip_payload, srcport, dstport, contentlen)) != 0) { fprintf(stderr, "error: could not fill udp header, returned %i.\n", err); exitstatus = -1; goto exit_prog; } /* set up IP payload */ memcpy(ip_payload + sizeof(udpheader_t), message_content, contentlen); /* send the ip packet */ ipheader_t iph; iph.ip_p = 17; /* UDP */ inet_aton(sourceipbuf, (struct in_addr*) &iph.ip_src); inet_aton(dest->filename[0], (struct in_addr*) &iph.ip_dst); if((err = send_ip_packet(&iph, ip_payload, payloadsize)) != 0) { fprintf(stderr, "error: could not send ip packet, returned %i.\n", err); exitstatus = -1; goto exit_prog; } } else if(protocol == proto_TCP) { printf("TCP currently not supported.\n"); } else { fprintf(stderr, "error: protocol %s is not supported.\n", proto->filename[0]); } } } else { arg_print_errors(stdout, end, argv[0]); exitstatus = -1; goto exit_prog; } exit_prog: arg_freetable(argtable, sizeof(argtable)/sizeof(argtable[0])); exit(exitstatus); }
int main(int argc, char **argv) { const char* progname = "rm"; struct arg_lit *dir = arg_lit0("d", "directory", "unlink file(s), even if it is a non-empty directory"); struct arg_rem *dir2 = arg_rem( NULL, "(super-user only)"); struct arg_lit *force = arg_lit0("f", "force", "ignore nonexistant files, never prompt"); struct arg_lit *inter = arg_lit0("i", "interactive", "prompt before any removal"); struct arg_lit *recur = arg_lit0("rR","recursive", "remove the contents of directories recursively"); struct arg_lit *verb = arg_lit0("v", "verbose", "explain what is being done"); struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); struct arg_lit *vers = arg_lit0(NULL,"version", "print version information and exit"); struct arg_file *files = arg_filen(NULL,NULL,NULL,1,argc+2,NULL); struct arg_end *end = arg_end(20); void* argtable[] = {dir,dir2,force,inter,recur,verb,help,vers,files,end}; int exitcode=0; int nerrors; /* verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable) != 0) { /* NULL entries were detected, some allocations must have failed */ printf("%s: insufficient memory\n",progname); exitcode=1; goto exit; } /* Parse the command line as defined by argtable[] */ nerrors = arg_parse(argc,argv,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("Usage: %s", progname); arg_print_syntax(stdout,argtable,"\n"); printf("Remove (unlink) the specified file(s).\n\n"); arg_print_glossary(stdout,argtable," %-20s %s\n"); printf("\nReport bugs to <no-one> as this is just an example program.\n"); exitcode=0; goto exit; } /* special case: '--version' takes precedence error reporting */ if (vers->count > 0) { printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); printf("September 2003, Stewart Heitmann\n"); exitcode=0; goto exit; } /* If the parser returned any errors then display them and exit */ if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout,end,progname); printf("Try '%s --help' for more information.\n",progname); exitcode=1; goto exit; } /* command line options are all ok, now perform the "rm" functionality */ mymain(dir->count, force->count, inter->count, recur->count, verb->count, files->filename, files->count); exit: /* deallocate each non-null entry in argtable[] */ arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return exitcode; }
int main(int argc, char **argv) { const char *progname = "mv"; struct arg_str *backupc = arg_str0(NULL, "backup", "[CONTROL]", "make a backup of each existing destination file"); struct arg_lit *backup = arg_lit0("b", NULL, "like --backup but does not accept an argument"); struct arg_lit *force = arg_lit0("f", "force", "do not prompt before overwriting"); struct arg_rem *force1 = arg_rem (NULL, " equivalent to --reply=yes"); struct arg_lit *interact = arg_lit0("i", "interactive", "Prompt before overwriting"); struct arg_rem *interact1= arg_rem (NULL, " equivalent to --reply=yes"); struct arg_str *reply = arg_str0(NULL,"reply", "{yes,no,query}", "specify how to handle the prompt about an"); struct arg_rem *reply1 = arg_rem (NULL, " existing destination file"); struct arg_lit *strpslsh = arg_lit0(NULL,"strip-trailing-slashes", "remove any trailing slashes from each SOURCE argument"); struct arg_str *suffix = arg_str0("S", "suffix", "SUFFIX", "override the usual backup suffix"); struct arg_str *targetd = arg_str0(NULL,"target-directory", "DIRECTORY", "move all SOURCE arguments into DIRECTORY"); struct arg_lit *update = arg_lit0("u", "update", "copy only when the SOURCE file is newer"); struct arg_rem *update1 = arg_rem (NULL, " than the destination file or when the"); struct arg_rem *update2 = arg_rem (NULL, " destination file is missing"); struct arg_lit *verbose = arg_lit0("v", "verbose", "explain what is being done"); struct arg_lit *help = arg_lit0(NULL,"help", "display this help and exit"); struct arg_lit *version = arg_lit0(NULL,"version", "display version information and exit"); struct arg_file *files = arg_filen(NULL, NULL, "SOURCE", 1, argc+2, NULL); struct arg_rem *dest = arg_rem ("DEST|DIRECTORY", NULL); struct arg_end *end = arg_end(20); void* argtable[] = {backupc,backup,force,force1,interact,interact1,reply,reply1,strpslsh,suffix,targetd,update,update1,update2,verbose,help,version,files,dest,end}; int exitcode=0; int nerrors; /* verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable) != 0) { /* NULL entries were detected, some allocations must have failed */ printf("%s: insufficient memory\n",progname); exitcode=1; goto exit; } /* Set default argument values prior to parsing */ backupc->sval[0] = "existing"; /* --backup={none,off,numbered,t,existing,nil,simple,never} */ suffix->sval[0] = "~"; /* --suffix=~ */ reply->sval[0] = "query"; /* --reply={yes,no,query} */ targetd->sval[0] = NULL; /* Parse the command line as defined by argtable[] */ nerrors = arg_parse(argc,argv,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("Usage: %s", progname); arg_print_syntax(stdout,argtable,"\n"); printf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n\n"); arg_print_glossary(stdout,argtable," %-30s %s\n"); printf("\nThe backup suffix is \"~\", unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n" "The version control method may be selected via the --backup option or through\n" "the VERSION_CONTROL environment variable. Here are the values:\n\n" " none, off never make backups (even if --backup is given)\n" " numbered, t make numbered backups\n" " existing, nil numbered if numbered backups exist, simple otherwise\n" " simple, never always make simple backups\n\n" "Report bugs to <foo@bar>.\n"); exitcode=0; goto exit; } /* special case: '--version' takes precedence error reporting */ if (version->count > 0) { printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); printf("September 2003, Stewart Heitmann\n"); exitcode=0; goto exit; } /* If the parser returned any errors then display them and exit */ if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout,end,progname); printf("Try '%s --help' for more information.\n",progname); exitcode=1; goto exit; } /* Command line parsing is complete, do the main processing */ exitcode = mymain(backupc->sval[0], backup->count, force->count, interact->count, reply->sval[0], strpslsh->count, suffix->sval[0], targetd->sval[0], update->count, verbose->count, files->filename, files->count); exit: /* deallocate each non-null entry in argtable[] */ arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return exitcode; }
int _tmain(int argc, char* argv[]) { // determine my process name _splitpath(argv[0],NULL,NULL,gszProcName,NULL); #else int main(int argc, const char** argv) { // determine my process name CUtility::splitpath((char *)argv[0],NULL,gszProcName); #endif int iScreenLogLevel; // level of screen diagnostics int iFileLogLevel; // level of file diagnostics char szLogFile[_MAX_PATH]; // write diagnostics to this file int Rslt; int Idx; int iMode = 0; // processing mode int iRandSeed = 0; // random base seed to use (if < 0 then current time used to seed generator) int KMerLen; // what length kmer to generate for 1..cMaxKMerLen int NumInFileSpecs; // number of input file specs char *pszInFiles[cMaxInFileSpecs]; // input control aligned reads files char szOutputFile[_MAX_PATH]; // command line args struct arg_lit *help = arg_lit0("hH","help", "print this help and exit"); struct arg_lit *version = arg_lit0("v","version,ver", "print version information and exit"); struct arg_int *FileLogLevel=arg_int0("f", "FileLogLevel", "<int>","Level of diagnostics written to screen and logfile 0=fatal,1=errors,2=info,3=diagnostics,4=debug"); struct arg_file *LogFile = arg_file0("F","log","<file>", "diagnostics log file"); struct arg_int *Mode = arg_int0("m","mode","<int>", "processing mode - 0 randomise genome"); struct arg_int *kmerlen = arg_int0("k","kmerlen","<int>", "maintain frequency composition of K-mer length (default = 1, range 1..15)"); struct arg_file *InFiles = arg_filen("i",NULL,"<file>",1,cMaxInFileSpecs, "input genome assembly multifasta files (s) to randomise"); struct arg_file *OutFile= arg_file1("o",NULL,"<file>", "output randomised assembly to this file as multifasta"); struct arg_int *RandSeed = arg_int0("s","randseed","<int>", "random seed to use (default is use system time)"); struct arg_end *end = arg_end(20); void *argtable[] = {help,version,FileLogLevel,LogFile,Mode,kmerlen,InFiles,OutFile,RandSeed,end}; char **pAllArgs; int argerrors; argerrors = CUtility::arg_parsefromfile(argc,(char **)argv,&pAllArgs); if(argerrors >= 0) argerrors = arg_parse(argerrors,pAllArgs,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("\n%s Kanga randomise genome K-mers, Version %s\nOptions ---\n", gszProcName,cpszProgVer); arg_print_syntax(stdout,argtable,"\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); printf("\nNote: Parameters can be entered into a parameter file, one parameter per line."); printf("\n To invoke this parameter file then precede its name with '@'"); printf("\n e.g. %s @myparams.txt\n",gszProcName); printf("\nPlease report any issues regarding usage of %s at https://github.com/csiro-crop-informatics/biokanga/issues\n\n",gszProcName); exit(1); } /* special case: '--version' takes precedence error reporting */ if (version->count > 0) { printf("\n%s Version %s\n",gszProcName,cpszProgVer); exit(1); } if (!argerrors) { if(FileLogLevel->count && !LogFile->count) { printf("\nError: FileLogLevel '-f%d' specified but no logfile '-F<logfile>\n'",FileLogLevel->ival[0]); exit(1); } iScreenLogLevel = iFileLogLevel = FileLogLevel->count ? FileLogLevel->ival[0] : eDLInfo; if(iFileLogLevel < eDLNone || iFileLogLevel > eDLDebug) { printf("\nError: FileLogLevel '-l%d' specified outside of range %d..%d\n",iFileLogLevel,eDLNone,eDLDebug); exit(1); } if(LogFile->count) { strncpy(szLogFile,LogFile->filename[0],_MAX_PATH); szLogFile[_MAX_PATH-1] = '\0'; } else { iFileLogLevel = eDLNone; szLogFile[0] = '\0'; } // now that log parameters have been parsed then initialise diagnostics log system if(!gDiagnostics.Open(szLogFile,(etDiagLevel)iScreenLogLevel,(etDiagLevel)iFileLogLevel,true)) { printf("\nError: Unable to start diagnostics subsystem\n"); if(szLogFile[0] != '\0') printf(" Most likely cause is that logfile '%s' can't be opened/created\n",szLogFile); exit(1); } gDiagnostics.DiagOut(eDLInfo,gszProcName,"Version: %s",cpszProgVer); iMode = Mode->count ? Mode->ival[0] : 0; if(iMode < 0 || iMode >= cMaxSupportedModes) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Unsupported Mode '-m%d' requested",iMode); exit(1); } KMerLen = 0; switch(iMode) { case 0: // generate random species fasta sequence for(NumInFileSpecs=Idx=0;NumInFileSpecs < cMaxInFileSpecs && Idx < InFiles->count; Idx++) { pszInFiles[Idx] = NULL; if(pszInFiles[NumInFileSpecs] == NULL) pszInFiles[NumInFileSpecs] = new char [_MAX_PATH]; strncpy(pszInFiles[NumInFileSpecs],InFiles->filename[Idx],_MAX_PATH); pszInFiles[NumInFileSpecs][_MAX_PATH-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(pszInFiles[NumInFileSpecs]); if(pszInFiles[NumInFileSpecs][0] != '\0') NumInFileSpecs++; } if(!NumInFileSpecs) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: After removal of whitespace, no input file(s) specified with '-i<filespec>' option)\n"); exit(1); } KMerLen = kmerlen->count ? kmerlen->ival[0] : cDfltKMerLen; if(KMerLen < 1 || KMerLen > cMaxKMerLen) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: K-mer length specified with '-k%d' is outside of range 1..10",KMerLen); exit(1); } if(!OutFile->count || OutFile->filename[0][0] == '\0') { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: No output fasta file specified with '-o<filename>'"); exit(1); } strncpy(szOutputFile,OutFile->filename[0],_MAX_PATH); szOutputFile[_MAX_PATH] = '\0'; if(!RandSeed->count) iRandSeed = -1; else { iRandSeed = RandSeed->ival[0]; if(iRandSeed < 0 || iRandSeed > 32767) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Random seed specified as '-s%d' must be between 0 and 32767",iRandSeed); exit(1); } } break; } gDiagnostics.DiagOut(eDLInfo,gszProcName,"Processing parameters:"); switch(iMode) { case 0: gDiagnostics.DiagOutMsgOnly(eDLInfo,"Mode: 0 (randomise genome K-mers)"); for(Idx=0; Idx < NumInFileSpecs; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"Use frequency compositions from these genome multifasta file(s) (%d): '%s'",Idx+1,pszInFiles[Idx]); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Maintain K-mer composition of length: %d",KMerLen); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Genome output file: '%s'",szOutputFile); if(iRandSeed >= 0) gDiagnostics.DiagOutMsgOnly(eDLInfo,"Random seed: %d",iRandSeed); else gDiagnostics.DiagOutMsgOnly(eDLInfo,"Random seed: will use current time as seed"); break; } gStopWatch.Start(); #ifdef _WIN32 SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS); #endif Rslt = GenerateRandFasta(iMode,KMerLen,NumInFileSpecs,pszInFiles,szOutputFile,iRandSeed); Rslt = Rslt >=0 ? 0 : 1; gDiagnostics.DiagOut(eDLInfo,gszProcName,"Exit code: %d Total processing time: %s",Rslt,gStopWatch.Read()); exit(Rslt); } else { printf("\n%s Kanga randomise genome K-mers, Version %s\n",gszProcName,cpszProgVer); arg_print_errors(stdout,end,gszProcName); arg_print_syntax(stdout,argtable,"\nUse '-h' to view option and parameter usage\n"); exit(1); } return 0; }
int _tmain(int argc, char* argv[]) { // determine my process name _splitpath(argv[0],NULL,NULL,gszProcName,NULL); #else int main(int argc, const char** argv) { // determine my process name CUtility::splitpath((char *)argv[0],NULL,gszProcName); #endif int iFileLogLevel; // level of file diagnostics int iScreenLogLevel; // level of file diagnostics char szLogFile[_MAX_PATH]; // write diagnostics to this file int Rslt = 0; // function result code >= 0 represents success, < 0 on failure int Idx; etPMode PMode; // processing mode int MinCovBases; // accept SNPs with at least this number covering bases double MaxPValue; // accept SNPs with at most this P-value int MinSpeciesTotCntThres; // individual species must have at least this number of total bases at SNP loci to count as SNP - 0 if no threshold int AltSpeciesMaxCnt; // only report markers if no other species has more than this number of counts at the putative SNP loci int MinSpeciesWithCnts; // only report markers where at least this number of species has SNP at the SNP loci char szRefGenome[cMaxLenName+1]; // reference genome against which other relative genomes were aligned int NumRelGenomes; // number of relative genome names char *pszRelGenomes[cRRMaxInFileSpecs]; // names of relative genome names int NumSNPFiles; // number of input SNP files char *pszSNPFiles[cRRMaxInFileSpecs]; // input SNP files int NumAlignFiles; // number of input alignment files char *pszAlignFiles[cRRMaxInFileSpecs]; // input alignment files char szMarkerFile[_MAX_PATH]; // write markers to this file int NumberOfProcessors; // number of installed CPUs int NumThreads; // number of threads (0 defaults to number of CPUs) // command line args struct arg_lit *help = arg_lit0("hH","help", "print this help and exit"); struct arg_lit *version = arg_lit0("v","version,ver", "print version information and exit"); struct arg_int *FileLogLevel=arg_int0("f", "FileLogLevel", "<int>","Level of diagnostics written to logfile 0=fatal,1=errors,2=info,3=diagnostics,4=debug"); struct arg_file *LogFile = arg_file0("F","log","<file>", "diagnostics log file"); struct arg_int *pmode = arg_int0("m","mode","<int>", "Marker processing mode: 0 - default"); struct arg_int *mincovbases=arg_int0("b", "MinCovBases","<int>","Filter out SNPs with less than this number of covering bases (default 5)"); struct arg_dbl *maxpvalue=arg_dbl0("p", "MaxPValue","<dbl>", "Filter out SNPs with P-Value higher (default 0.05)"); struct arg_int *mintotcntthres = arg_int0("z","mintotcntthres","<int>", "Species must have at least this number of total bases covering marker loci"); struct arg_int *mincovspecies=arg_int0("Z", "mincovspecies","<int>","Do not report markers unless this minimum number of species have SNP at same loci"); struct arg_int *altspeciesmaxcnt = arg_int0("a","altspeciesmaxcnt","<int>", "Only report markers if no other species has more than this number of counts at the putative SNP loci (defaults to 1)"); struct arg_str *refgenome=arg_str1("r", "refgenome","<str>", "alignments and SNPs of relative genomes were against this genome assembly (default 'RefGenome')"); struct arg_str *relgenomes = arg_strn("R","relgenomes","<relgenomes>",1,cRRMaxInFileSpecs,"alignments and SNPs from these genomes"); struct arg_file *snpfiles = arg_filen("i","insnps","<file>",1,cRRMaxInFileSpecs,"Load SNPs from file(s)"); struct arg_file *alignfiles = arg_filen("I","inaligns","<file>",1,cRRMaxInFileSpecs,"Load alignments from file(s)"); struct arg_file *markerfile = arg_file1("o","out","<file>", "Output markers to this file"); struct arg_int *threads = arg_int0("T","threads","<int>", "number of processing threads 0..128 (defaults to 0 which sets threads to number of CPU cores)"); struct arg_end *end = arg_end(100); void *argtable[] = {help,version,FileLogLevel,LogFile, pmode,mincovbases,maxpvalue,mintotcntthres,altspeciesmaxcnt,mincovspecies,refgenome,relgenomes,snpfiles,alignfiles,markerfile,threads, end}; char **pAllArgs; int argerrors; argerrors = CUtility::arg_parsefromfile(argc,(char **)argv,&pAllArgs); if(argerrors >= 0) argerrors = arg_parse(argerrors,pAllArgs,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("\n%s Generate Markers, Version %s\nOptions ---\n", gszProcName,cpszProgVer); arg_print_syntax(stdout,argtable,"\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); printf("\nNote: Parameters can be entered into a parameter file, one parameter per line."); printf("\n To invoke this parameter file then precede its name with '@'"); printf("\n e.g. %s @myparams.txt\n",gszProcName); printf("\nPlease report any issues regarding usage of %s at https://github.com/csiro-crop-informatics/biokanga/issues\n\n",gszProcName); exit(1); } /* special case: '--version' takes precedence error reporting */ if (version->count > 0) { printf("\n%s Version %s\n",gszProcName,cpszProgVer); exit(1); } if (!argerrors) { if(FileLogLevel->count && !LogFile->count) { printf("\nError: FileLogLevel '-f%d' specified but no logfile '-F<logfile>\n'",FileLogLevel->ival[0]); exit(1); } iScreenLogLevel = iFileLogLevel = FileLogLevel->count ? FileLogLevel->ival[0] : eDLInfo; if(iFileLogLevel < eDLNone || iFileLogLevel > eDLDebug) { printf("\nError: FileLogLevel '-l%d' specified outside of range %d..%d\n",iFileLogLevel,eDLNone,eDLDebug); exit(1); } if(LogFile->count) { strncpy(szLogFile,LogFile->filename[0],_MAX_PATH); szLogFile[_MAX_PATH-1] = '\0'; } else { iFileLogLevel = eDLNone; szLogFile[0] = '\0'; } // now that log parameters have been parsed then initialise diagnostics log system if(!gDiagnostics.Open(szLogFile,(etDiagLevel)iScreenLogLevel,(etDiagLevel)iFileLogLevel,true)) { printf("\nError: Unable to start diagnostics subsystem\n"); if(szLogFile[0] != '\0') printf(" Most likely cause is that logfile '%s' can't be opened/created\n",szLogFile); exit(1); } gDiagnostics.DiagOut(eDLInfo,gszProcName,"Version: %s",cpszProgVer); PMode = (etPMode)(pmode->count ? pmode->ival[0] : ePMdefault); if(PMode < ePMdefault || PMode >= ePMplaceholder) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Processing mode '-m%d' specified outside of range %d..%d\n",PMode,ePMdefault,(int)ePMplaceholder-1); exit(1); } #ifdef _WIN32 SYSTEM_INFO SystemInfo; GetSystemInfo(&SystemInfo); NumberOfProcessors = SystemInfo.dwNumberOfProcessors; #else NumberOfProcessors = sysconf(_SC_NPROCESSORS_CONF); #endif int MaxAllowedThreads = min(cMaxWorkerThreads,NumberOfProcessors); // limit to be at most cMaxWorkerThreads if((NumThreads = threads->count ? threads->ival[0] : MaxAllowedThreads)==0) NumThreads = MaxAllowedThreads; if(NumThreads < 0 || NumThreads > MaxAllowedThreads) { gDiagnostics.DiagOut(eDLWarn,gszProcName,"Warning: Number of threads '-T%d' specified was outside of range %d..%d",NumThreads,1,MaxAllowedThreads); gDiagnostics.DiagOut(eDLWarn,gszProcName,"Warning: Defaulting number of threads to %d",MaxAllowedThreads); NumThreads = MaxAllowedThreads; } MinCovBases = mincovbases->count ? mincovbases->ival[0] : 5; if(MinCovBases < 1 || MinCovBases > 10000) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Minimum covering bases '-b%d' must be in range 1..1000",MinCovBases); exit(1); } AltSpeciesMaxCnt = altspeciesmaxcnt->count ? altspeciesmaxcnt->ival[0] : 1; if(AltSpeciesMaxCnt < 1 || AltSpeciesMaxCnt > 10000) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Max alternative species coverage '-a%d' at putative marker loci must be in range 1..1000",AltSpeciesMaxCnt); exit(1); } MaxPValue = maxpvalue->count ? maxpvalue->dval[0] : 0.05; if(MaxPValue < 0.0 || MaxPValue > 0.25) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Maximum P-Value '-p%1.4f' must be in range 0.0..0.25",MaxPValue); exit(1); } if(refgenome->count) { strncpy(szRefGenome,refgenome->sval[0],cMaxLenName); szRefGenome[cMaxLenName-1]= '\0'; } else strcpy(szRefGenome,"RefGenome"); if(!relgenomes->count) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: No alignment from genome name(s) specified with with '-R<relgenomes>' option)"); exit(1); } for(NumRelGenomes=Idx=0;NumRelGenomes < cRRMaxInFileSpecs && Idx < relgenomes->count; Idx++) { pszRelGenomes[Idx] = NULL; if(pszRelGenomes[NumRelGenomes] == NULL) pszRelGenomes[NumRelGenomes] = new char [_MAX_PATH]; strncpy(pszRelGenomes[NumRelGenomes],relgenomes->sval[Idx],_MAX_PATH); pszRelGenomes[NumRelGenomes][_MAX_PATH-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(pszRelGenomes[NumRelGenomes]); if(pszRelGenomes[NumRelGenomes][0] != '\0') NumRelGenomes++; } strncpy(szMarkerFile,markerfile->filename[0],_MAX_PATH); szMarkerFile[_MAX_PATH-1] = '\0'; if(!snpfiles->count) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: No input SNP file(s) specified with with '-i<filespec>' option)"); exit(1); } for(NumSNPFiles=Idx=0;NumSNPFiles < cRRMaxInFileSpecs && Idx < snpfiles->count; Idx++) { pszSNPFiles[Idx] = NULL; if(pszSNPFiles[NumSNPFiles] == NULL) pszSNPFiles[NumSNPFiles] = new char [_MAX_PATH]; strncpy(pszSNPFiles[NumSNPFiles],snpfiles->filename[Idx],_MAX_PATH); pszSNPFiles[NumSNPFiles][_MAX_PATH-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(pszSNPFiles[NumSNPFiles]); if(pszSNPFiles[NumSNPFiles][0] != '\0') NumSNPFiles++; } if(!NumSNPFiles) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: After removal of whitespace, no input SNP file(s) specified with '-i<filespec>' option"); exit(1); } if(!alignfiles->count) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: No input alignment file(s) specified with with '-I<filespec>' option)"); exit(1); } for(NumAlignFiles=Idx=0;NumAlignFiles < cRRMaxInFileSpecs && Idx < alignfiles->count; Idx++) { pszAlignFiles[Idx] = NULL; if(pszAlignFiles[NumAlignFiles] == NULL) pszAlignFiles[NumAlignFiles] = new char [_MAX_PATH]; strncpy(pszAlignFiles[NumAlignFiles],alignfiles->filename[Idx],_MAX_PATH); pszAlignFiles[NumAlignFiles][_MAX_PATH-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(pszAlignFiles[NumAlignFiles]); if(pszAlignFiles[NumAlignFiles][0] != '\0') NumAlignFiles++; } if(!NumAlignFiles) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: After removal of whitespace, no input alignment file(s) specified with '-I<filespec>' option"); exit(1); } // number of alignment files must be same as the number of SNP files and genome names! if(NumAlignFiles != NumSNPFiles && NumAlignFiles != NumRelGenomes) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Expected same number of genome names, alignment files and SNP files, %d genome names, %d alignment files, %d SNP files",NumRelGenomes,NumAlignFiles,NumSNPFiles); exit(1); } MinSpeciesTotCntThres = mincovspecies->count ? mincovspecies->ival[0] : 1; if(MinSpeciesTotCntThres < 1 || MinSpeciesTotCntThres > 10000) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Minimum species total bases '-z%d' must be in range 1..10000",MinSpeciesTotCntThres); exit(1); } MinSpeciesWithCnts = mincovspecies->count ? mincovspecies->ival[0] : 1; if(MinSpeciesWithCnts < 1 || MinSpeciesWithCnts > NumAlignFiles) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Minimum species to call marker '-Z%d' must be in range 1..%d",NumAlignFiles); exit(1); } // show user current resource limits #ifndef _WIN32 gDiagnostics.DiagOut(eDLInfo, gszProcName, "Resources: %s",CUtility::ReportResourceLimits()); #endif gDiagnostics.DiagOut(eDLInfo,gszProcName,"Processing parameters:"); const char *pszDescr; switch(PMode) { case ePMdefault: pszDescr = "Default marker processing"; break; } gDiagnostics.DiagOutMsgOnly(eDLInfo,"Processing mode is : '%s'",pszDescr); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Minimum coverage : %d",MinCovBases); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Maximum P-Value : %1.4f'",MaxPValue); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Reference genome assembly name : '%s'",szRefGenome); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Minimum total bases for species at SNP call loci : %d",MinSpeciesTotCntThres); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Maximum alternative species coverage at SNP call loci : %d",AltSpeciesMaxCnt); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Minimum number of species with SNP call at same loci : %d",MinSpeciesWithCnts); for(Idx=0; Idx < NumRelGenomes; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"Alignments and SNPs from this genome (%d) : '%s'",Idx+1,pszRelGenomes[Idx]); for(Idx=0; Idx < NumSNPFiles; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"Input SNP file (%d) : '%s'",Idx+1,pszSNPFiles[Idx]); for(Idx=0; Idx < NumAlignFiles; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"Input alignment file (%d) : '%s'",Idx+1,pszAlignFiles[Idx]); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Output markers to file : '%s'",szMarkerFile); gDiagnostics.DiagOutMsgOnly(eDLInfo,"number of threads : %d",NumThreads); #ifdef _WIN32 SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS); #endif gStopWatch.Start(); Rslt = Process(PMode,MinCovBases,MaxPValue,MinSpeciesTotCntThres,MinSpeciesWithCnts,AltSpeciesMaxCnt,szRefGenome,NumRelGenomes,pszRelGenomes,NumThreads,NumSNPFiles,pszSNPFiles,NumAlignFiles,pszAlignFiles,szMarkerFile); gStopWatch.Stop(); Rslt = Rslt >=0 ? 0 : 1; gDiagnostics.DiagOut(eDLInfo,gszProcName,"Exit code: %d Total processing time: %s",Rslt,gStopWatch.Read()); exit(Rslt); } else { printf("\n%s Generate Markers, Version %s\n",gszProcName,cpszProgVer); arg_print_errors(stdout,end,gszProcName); arg_print_syntax(stdout,argtable,"\nUse '-h' to view option and parameter usage\n"); exit(1); } return 0; }
void conf_configure_app(configuration_ctx_t* ctx) { struct arg_lit* help = arg_lit0(OPT_HELP_SHORT, OPT_HELP_LONG, OPT_HELP_DESCR); struct arg_lit* helpF = arg_lit0(OPT_HELP_SHORT, OPT_HELP_LONG, OPT_HELP_DESCR); struct arg_lit* helpS = arg_lit0(OPT_HELP_SHORT, OPT_HELP_LONG, OPT_HELP_DESCR); struct arg_lit* grep = arg_lit0(OPT_GREP_SHORT, OPT_GREP_LONG, OPT_GREP_DESCR); struct arg_lit* grepF = arg_lit0(OPT_GREP_SHORT, OPT_GREP_LONG, OPT_GREP_DESCR); struct arg_lit* grepS = arg_lit0(OPT_GREP_SHORT, OPT_GREP_LONG, OPT_GREP_DESCR); struct arg_str* string = arg_str1(OPT_STR_SHORT, OPT_STR_LONG, NULL, OPT_STR_DESCR); struct arg_str* stringG = arg_str0(OPT_STR_SHORT, OPT_STR_LONG, NULL, OPT_STR_DESCR); struct arg_file* file = arg_file1(OPT_FILE_SHORT, OPT_FILE_LONG, NULL, OPT_FILE_DESCR); struct arg_file* fileG = arg_file0(OPT_FILE_SHORT, OPT_FILE_LONG, NULL, OPT_FILE_DESCR); struct arg_str* macro = arg_str1(OPT_MACRO_SHORT, OPT_MACRO_LONG, NULL, OPT_MACRO_DESCR); struct arg_str* macroS = arg_str1(OPT_MACRO_SHORT, OPT_MACRO_LONG, NULL, OPT_MACRO_DESCR); struct arg_str* macroF = arg_str1(OPT_MACRO_SHORT, OPT_MACRO_LONG, NULL, OPT_MACRO_DESCR); struct arg_file* files = arg_filen(OPT_F_SHORT, OPT_F_LONG, NULL, 1, ctx->argc + 2, OPT_F_DESCR); struct arg_file* filesS = arg_filen(OPT_F_SHORT, OPT_F_LONG, NULL, 1, ctx->argc + 2, OPT_F_DESCR); struct arg_file* filesF = arg_filen(OPT_F_SHORT, OPT_F_LONG, NULL, 1, ctx->argc + 2, OPT_F_DESCR); struct arg_end* end = arg_end(10); struct arg_end* endF = arg_end(10); struct arg_end* endS = arg_end(10); void* argtable[] = {help, grep, stringG, fileG, macro, files, end}; void* argtableF[] = {helpF, grepF, file, macroF, filesF, endF}; void* argtableS[] = {helpS, grepS, string, macroS, filesS, endS}; if(arg_nullcheck(argtable) != 0 || arg_nullcheck(argtableF) != 0 || arg_nullcheck(argtableS) != 0) { prconf_print_syntax(argtable, argtableS, argtableF); goto cleanup; } int nerrors = arg_parse(ctx->argc, ctx->argv, argtable); int nerrorsF = arg_parse(ctx->argc, ctx->argv, argtableF); int nerrorsS = arg_parse(ctx->argc, ctx->argv, argtableS); if(nerrors > 0 || help->count > 0) { prconf_print_syntax(argtable, argtableS, argtableF); if(help->count == 0 && ctx->argc > 1) { arg_print_errors(stdout, end, PROGRAM_NAME); } goto cleanup; } if(nerrorsS == 0) { ctx->on_string(filesS, macroS->sval[0], string->sval[0], grepS->count > 0); } else if(nerrorsF == 0) { ctx->on_file(filesS, macroF->sval[0], file->filename[0], grepF->count > 0); } else { prconf_print_syntax(argtable, argtableS, argtableF); arg_print_errors(stdout, endF, PROGRAM_NAME); arg_print_errors(stdout, endS, PROGRAM_NAME); } cleanup: arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); arg_freetable(argtableS, sizeof(argtableS) / sizeof(argtableS[0])); arg_freetable(argtableF, sizeof(argtableF) / sizeof(argtableF[0])); }
int kangax(int argc, char* argv[]) { // determine my process name _splitpath(argv[0],NULL,NULL,gszProcName,NULL); #else int kangax(int argc, char** argv) { // determine my process name CUtility::splitpath((char *)argv[0],NULL,gszProcName); #endif int iScreenLogLevel; // level of screen diagnostics int iFileLogLevel; // level of file diagnostics char szLogFile[_MAX_PATH]; // write diagnostics to this file int Rslt; char szOutputFileSpec[_MAX_PATH]; int MinSeqLen; // only accept for indexing sequences which are at least this length int SimGenomeSize; // if 1..1000 then simulating indexing of a genome of this size in Gbp. int NumInputFileSpecs; // number of input file specs char *pszInputFileSpecs[cMaxInFileSpecs]; // input files char szDescription[cMBSFFileDescrLen]; char szTitle[cMBSFShortFileDescrLen]; char szRefSpecies[cMaxDatasetSpeciesChrom]; int iMode; // processing mode bool bSOLiD; // colorspace (SOLiD) generation int NumberOfProcessors; // number of installed CPUs int NumThreads; // number of threads (0 defaults to number of CPUs) char szSQLiteDatabase[_MAX_PATH]; // results summaries to this SQLite file char szExperimentName[cMaxDatasetSpeciesChrom+1]; // experiment name char szExperimentDescr[1000]; // describes experiment // command line args struct arg_lit *help = arg_lit0("hH","help", "print this help and exit"); struct arg_lit *version = arg_lit0("v","version,ver", "print version information and exit"); struct arg_int *FileLogLevel=arg_int0("f", "FileLogLevel", "<int>","Level of diagnostics written to logfile 0=fatal,1=errors,2=info,3=diagnostics,4=debug"); struct arg_file *LogFile = arg_file0("F","log","<file>", "diagnostics log file"); struct arg_int *Mode=arg_int0("m", "mode", "<int>", "Processing mode, 0=standard, 1=bisulphite index, 2=simulated genome (default 0)"); struct arg_lit *solid = arg_lit0("C","colorspace", "Generate for colorspace (SOLiD)"); struct arg_int *simgenomesize=arg_int0("s", "simgenomesize", "<int>","Simulated genome size in Gbp (default 5, range 1..1000"); struct arg_int *minseqlen=arg_int0("l", "minseqlen", "<int>","Do not accept for indexing sequences less than this length (default 50, range 1..1000000)"); struct arg_file *infiles = arg_filen("i",NULL,"<file>",0,cMaxInFileSpecs, "input from wildcarded kangas or fasta files"); struct arg_file *OutFile = arg_file0("o",NULL,"<file>", "output suffix array file"); struct arg_str *Descr = arg_str0("d","descr","<string>", "full description"); struct arg_str *Title = arg_str0("t","title","<string>", "short title"); struct arg_str *RefSpecies = arg_str1("r","ref","<string>", "reference species"); struct arg_int *threads = arg_int0("T","threads","<int>", "number of processing threads 0..128 (defaults to 0 which sets threads to number of CPU cores)"); struct arg_file *summrslts = arg_file0("q","sumrslts","<file>", "Output results summary to this SQLite3 database file"); struct arg_str *experimentname = arg_str0("w","experimentname","<str>", "experiment name SQLite3 database file"); struct arg_str *experimentdescr = arg_str0("W","experimentdescr","<str>", "experiment description SQLite3 database file"); struct arg_end *end = arg_end(200); void *argtable[] = {help,version,FileLogLevel,LogFile, summrslts,experimentname,experimentdescr, Mode,minseqlen,simgenomesize,solid,infiles,OutFile,RefSpecies,Descr,Title, threads,end}; char **pAllArgs; int argerrors; argerrors = CUtility::arg_parsefromfile(argc,(char **)argv,&pAllArgs); if(argerrors >= 0) argerrors = arg_parse(argerrors,pAllArgs,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("\n%s %s %s, Version %s\nOptions ---\n", gszProcName,gpszSubProcess->pszName,gpszSubProcess->pszFullDescr,cpszProgVer); arg_print_syntax(stdout,argtable,"\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); printf("\nNote: Parameters can be entered into a parameter file, one parameter per line."); printf("\n To invoke this parameter file then precede its name with '@'"); printf("\n e.g. %s %s @myparams.txt\n",gszProcName,gpszSubProcess->pszName); printf("\nPlease report any issues regarding usage of %s at https://github.com/csiro-crop-informatics/biokanga/issues\n\n",gszProcName); return(1); } /* special case: '--version' takes precedence error reporting */ if (version->count > 0) { printf("\n%s %s Version %s\n",gszProcName,gpszSubProcess->pszName,cpszProgVer); return(1); } if (!argerrors) { if(FileLogLevel->count && !LogFile->count) { printf("\nError: FileLogLevel '-f%d' specified but no logfile '-F<logfile>'",FileLogLevel->ival[0]); exit(1); } iScreenLogLevel = iFileLogLevel = FileLogLevel->count ? FileLogLevel->ival[0] : eDLInfo; if(iFileLogLevel < eDLNone || iFileLogLevel > eDLDebug) { printf("\nError: FileLogLevel '-l%d' specified outside of range %d..%d",iFileLogLevel,eDLNone,eDLDebug); exit(1); } if(LogFile->count) { strncpy(szLogFile,LogFile->filename[0],_MAX_PATH); szLogFile[_MAX_PATH-1] = '\0'; } else { iFileLogLevel = eDLNone; szLogFile[0] = '\0'; } // now that log parameters have been parsed then initialise diagnostics log system if(!gDiagnostics.Open(szLogFile,(etDiagLevel)iScreenLogLevel,(etDiagLevel)iFileLogLevel,true)) { printf("\nError: Unable to start diagnostics subsystem\n"); if(szLogFile[0] != '\0') printf(" Most likely cause is that logfile '%s' can't be opened/created\n",szLogFile); exit(1); } gDiagnostics.DiagOut(eDLInfo,gszProcName,"Subprocess %s Version %s starting",gpszSubProcess->pszName,cpszProgVer); gExperimentID = 0; gProcessID = 0; gProcessingID = 0; szSQLiteDatabase[0] = '\0'; szExperimentName[0] = '\0'; szExperimentDescr[0] = '\0'; if(experimentname->count) { strncpy(szExperimentName,experimentname->sval[0],sizeof(szExperimentName)); szExperimentName[sizeof(szExperimentName)-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(szExperimentName); CUtility::ReduceWhitespace(szExperimentName); } else szExperimentName[0] = '\0'; MinSeqLen = 0; gExperimentID = 0; gProcessID = 0; gProcessingID = 0; szSQLiteDatabase[0] = '\0'; szExperimentDescr[0] = '\0'; if(summrslts->count) { strncpy(szSQLiteDatabase,summrslts->filename[0],sizeof(szSQLiteDatabase)-1); szSQLiteDatabase[sizeof(szSQLiteDatabase)-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(szSQLiteDatabase); if(strlen(szSQLiteDatabase) < 1) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: After removal of whitespace, no SQLite database specified with '-q<filespec>' option"); return(1); } if(strlen(szExperimentName) < 1) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: After removal of whitespace, no SQLite experiment name specified with '-w<str>' option"); return(1); } if(experimentdescr->count) { strncpy(szExperimentDescr,experimentdescr->sval[0],sizeof(szExperimentDescr)-1); szExperimentDescr[sizeof(szExperimentDescr)-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(szExperimentDescr); } if(strlen(szExperimentDescr) < 1) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: After removal of whitespace, no SQLite experiment description specified with '-W<str>' option"); return(1); } gExperimentID = gSQLiteSummaries.StartExperiment(szSQLiteDatabase,false,true,szExperimentName,szExperimentName,szExperimentDescr); if(gExperimentID < 1) return(1); gProcessID = gSQLiteSummaries.AddProcess((char *)gpszSubProcess->pszName,(char *)gpszSubProcess->pszName,(char *)gpszSubProcess->pszFullDescr); if(gProcessID < 1) return(1); gProcessingID = gSQLiteSummaries.StartProcessing(gExperimentID,gProcessID,(char *)cpszProgVer); if(gProcessingID < 1) return(1); gDiagnostics.DiagOut(eDLInfo,gszProcName,"Initialised SQLite database '%s' for results summary collection",szSQLiteDatabase); gDiagnostics.DiagOut(eDLInfo,gszProcName,"SQLite database experiment identifier for '%s' is %d",szExperimentName,gExperimentID); gDiagnostics.DiagOut(eDLInfo,gszProcName,"SQLite database process identifier for '%s' is %d",(char *)gpszSubProcess->pszName,gProcessID); gDiagnostics.DiagOut(eDLInfo,gszProcName,"SQLite database processing instance identifier is %d",gProcessingID); } else { szSQLiteDatabase[0] = '\0'; szExperimentDescr[0] = '\0'; } iMode = Mode->count ? Mode->ival[0] : 0; if(iMode < 0 || iMode > 2) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: processing mode '-m%d' must be specified in range %d..%d",iMode,0,1); exit(1); } if(iMode == 2) { SimGenomeSize = simgenomesize->count ? simgenomesize->ival[0] : 5; if(SimGenomeSize < 1 || iMode > cMaxSimGenomeGbp) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: simulated genome size in Gbp '-s%d' must be specified in range 1..%d",SimGenomeSize,cMaxSimGenomeGbp); exit(1); } } else SimGenomeSize = 0; bSOLiD = solid->count ? true : false; int Idx; if(iMode != 2) { MinSeqLen = minseqlen->count ? minseqlen->ival[0] : 50; if(MinSeqLen < 1) MinSeqLen = 1; else if(MinSeqLen > 1000000) MinSeqLen = 1000000; for(NumInputFileSpecs=Idx=0;NumInputFileSpecs < cMaxInFileSpecs && Idx < infiles->count; Idx++) { pszInputFileSpecs[Idx] = NULL; if(pszInputFileSpecs[NumInputFileSpecs] == NULL) pszInputFileSpecs[NumInputFileSpecs] = new char [_MAX_PATH]; strncpy(pszInputFileSpecs[NumInputFileSpecs],infiles->filename[Idx],_MAX_PATH); pszInputFileSpecs[NumInputFileSpecs][_MAX_PATH-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(pszInputFileSpecs[NumInputFileSpecs]); if(pszInputFileSpecs[NumInputFileSpecs][0] != '\0') NumInputFileSpecs++; } if(!NumInputFileSpecs) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: After removal of whitespace, no input file(s) specified with '-i<filespec>' option)\n"); exit(1); } } else { NumInputFileSpecs = 0; pszInputFileSpecs[0] = NULL; } if(OutFile->count) { strcpy(szOutputFileSpec,OutFile->filename[0]); CUtility::TrimQuotedWhitespcExtd(szOutputFileSpec); } else szOutputFileSpec[0] = '\0'; if(iMode != 2 && szOutputFileSpec[0] == '\0') { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: no output file(s) specified with '-o<filespec>' option)\n"); exit(1); } strncpy(szRefSpecies,RefSpecies->sval[0],cMaxDatasetSpeciesChrom); szRefSpecies[cMaxDatasetSpeciesChrom-1] = '\0'; if(!Title->count) strcpy(szTitle,szRefSpecies); else { strncpy(szTitle,Title->sval[0],cMBSFShortFileDescrLen); szTitle[cMBSFShortFileDescrLen-1] = '\0'; } if(!Descr->count) strcpy(szDescription,szRefSpecies); else { strncpy(szDescription,Descr->sval[0],cMBSFFileDescrLen); szDescription[cMBSFFileDescrLen-1] = '\0'; } // show user current resource limits #ifndef _WIN32 gDiagnostics.DiagOut(eDLInfo, gszProcName, "Resources: %s",CUtility::ReportResourceLimits()); #endif int AvailGBMemory; #ifdef _WIN32 SYSTEM_INFO SystemInfo; GetSystemInfo(&SystemInfo); NumberOfProcessors = SystemInfo.dwNumberOfProcessors; MEMORYSTATUSEX status; status.dwLength = sizeof(status); GlobalMemoryStatusEx(&status); AvailGBMemory = (int)(status.ullTotalPhys / 0x040000000); // set to be gigabytes #else NumberOfProcessors = sysconf(_SC_NPROCESSORS_CONF); long pages = sysconf(_SC_PHYS_PAGES); long page_size = sysconf(_SC_PAGE_SIZE); AvailGBMemory = (int)(((INT64)pages * (INT64)page_size)/0x040000000); #endif int MaxAllowedThreads = min(cMaxWorkerThreads,NumberOfProcessors); // limit to be at most cMaxWorkerThreads if((NumThreads = threads->count ? threads->ival[0] : MaxAllowedThreads)==0) NumThreads = MaxAllowedThreads; if(NumThreads < 0 || NumThreads > MaxAllowedThreads) { gDiagnostics.DiagOut(eDLWarn,gszProcName,"Warning: Number of threads '-T%d' specified was outside of range %d..%d",NumThreads,1,MaxAllowedThreads); gDiagnostics.DiagOut(eDLWarn,gszProcName,"Warning: Defaulting number of threads to %d",MaxAllowedThreads); NumThreads = MaxAllowedThreads; } gDiagnostics.DiagOut(eDLInfo,gszProcName,"Processing parameters:"); if(iMode == 2) { gDiagnostics.DiagOutMsgOnly(eDLInfo,"Simulating indexing of a genome of size: %dGbp",SimGenomeSize); if(AvailGBMemory < (SimGenomeSize * 7)) // allowing for 6 bytes per base plus overheads gDiagnostics.DiagOutMsgOnly(eDLWarn,"May be memory allocation problems when loading or indexing this simulated genome, available memory: %dGB",AvailGBMemory); } if(bSOLiD) gDiagnostics.DiagOutMsgOnly(eDLInfo,"Process for colorspace (SOLiD)"); if(iMode != 2) { gDiagnostics.DiagOutMsgOnly(eDLInfo,"Accepting for indexing sequences of length at least: %dbp",MinSeqLen); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Process input files as: '%s'",iMode == 0 ? "standard" : "bisulphite"); for(Idx=0; Idx < NumInputFileSpecs; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"Input source file spec: '%s'",pszInputFileSpecs[Idx]); } if(szOutputFileSpec[0] != '\0') gDiagnostics.DiagOutMsgOnly(eDLInfo,"Output to suffix array file: '%s'",szOutputFileSpec); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Reference species: '%s'",szRefSpecies); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Title text: '%s'",szTitle); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Descriptive text: '%s'",szDescription); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Number of threads : %d",NumThreads); if(szExperimentName[0] != '\0') gDiagnostics.DiagOutMsgOnly(eDLInfo,"This processing reference: %s",szExperimentName); #ifdef _WIN32 SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS); #endif gStopWatch.Start(); Rslt = CreateBioseqSuffixFile(iMode,MinSeqLen,SimGenomeSize,NumThreads,bSOLiD,NumInputFileSpecs,pszInputFileSpecs,szOutputFileSpec,szRefSpecies,szDescription,szTitle); Rslt = Rslt >=0 ? 0 : 1; if(gExperimentID > 0) { if(gProcessingID) gSQLiteSummaries.EndProcessing(gProcessingID,Rslt); gSQLiteSummaries.EndExperiment(gExperimentID); } gStopWatch.Stop(); gDiagnostics.DiagOut(eDLInfo,gszProcName,"Exit code: %d Total processing time: %s",Rslt,gStopWatch.Read()); exit(Rslt); } else { printf("\n%s %s %s, Version %s\n", gszProcName,gpszSubProcess->pszName,gpszSubProcess->pszFullDescr,cpszProgVer); arg_print_errors(stdout,end,gszProcName); arg_print_syntax(stdout,argtable,"\nUse '-h' to view option and parameter usage\n"); exit(1); } }
int main(int argc, char* argv[]) { // Define our variables. FILE* in; FILE* out; int nerrors, i; char* test; uint16_t offset, current, store, mem_index; struct lprov_entry* required = NULL; struct lprov_entry* provided = NULL; struct lprov_entry* adjustment = NULL; struct lprov_entry* temp = NULL; // Define arguments. struct arg_lit* show_help = arg_lit0("h", "help", "Show this help."); struct arg_file* input_files = arg_filen(NULL, NULL, "<file>", 1, 100, "The input object files."); struct arg_file* output_file = arg_file1("o", "output", "<file>", "The output file (or - to send to standard output)."); struct arg_end *end = arg_end(20); void *argtable[] = { show_help, input_files, output_file, end }; // Parse arguments. nerrors = arg_parse(argc,argv,argtable); if (nerrors != 0 || show_help->count != 0) { if (show_help->count != 0) arg_print_errors(stdout, end, "linker"); printf("syntax:\n linker"); arg_print_syntax(stdout, argtable, "\n"); printf("options:\n"); arg_print_glossary(stdout, argtable, " %-25s %s\n"); return 1; } // Open the output file for writing. out = fopen(output_file->filename[0], "wb"); if (out == NULL) { // Handle the error. fprintf(stderr, "linker: unable to write to output file.\n"); return 1; } // We initially need to get a list of ALL provided // labels before we can start replacing them. offset = 0; for (i = 0; i < input_files->count; i++) { // Open the input file. in = fopen(input_files->filename[i], "rb"); if (in == NULL) { // Handle the error. fprintf(stderr, "linker: unable to read input file '%s'.\n", input_files->filename[i]); fclose(out); return 1; } // Is this the object format? test = malloc(strlen(ldata_objfmt) + 1); memset(test, 0, strlen(ldata_objfmt) + 1); fread(test, 1, strlen(ldata_objfmt), in); fseek(in, 1, SEEK_CUR); if (strcmp(test, ldata_objfmt) != 0) { // Handle the error. fprintf(stderr, "linker: input file '%s' is not in object format 1.0.\n", input_files->filename[i]); fclose(in); fclose(out); return 1; } free(test); // Load only the provided label entries into memory. objfile_load(input_files->filename[i], in, &offset, &provided, NULL, NULL); // Close the file. fclose(in); } // Now we can start replacing the labels with the provided values // since we have ALL of the provided labels available. offset = 0; for (i = 0; i < input_files->count; i++) { // Open the input file. in = fopen(input_files->filename[i], "rb"); if (in == NULL) { // Handle the error. fprintf(stderr, "linker: unable to read input file '%s'.\n", input_files->filename[i]); fclose(out); return 1; } // Skip over the object format label; we already tested // for this in phase 1. fseek(in, strlen(ldata_objfmt) + 1, SEEK_CUR); // Load only the required and adjustment entries into memory. current = offset; objfile_load(input_files->filename[i], in, &offset, NULL, &required, &adjustment); // Copy all of the input file's data into the output // file, word by word. mem_index = 0; fprintf(stderr, "BEGIN %s\n", input_files->filename[i]); while (!feof(in)) { // Read a word. fread(&store, sizeof(uint16_t), 1, in); // For some strange reason, the last two bytes get // written twice, as if it's only EOF after you // attempt to read past the end again. I'm not sure // why the semantics are like this, but checking again // for EOF here prevents us writing double. if (feof(in)) break; // Check to see if we need to do something with this // word, such as adjusting it. if (lprov_find_by_address(adjustment, mem_index) != NULL) { // We need to adjust this word by the offset. store += current; fprintf(stderr, "ADJUSTED 0x%04X: 0x%04X -> 0x%04X\n", mem_index, store - current, store); } // Check to see if we need to resolve this word into // an actual address because it was imported. temp = lprov_find_by_address(required, mem_index); if (temp != NULL) { // Find the position we should change this to. temp = lprov_find_by_label(provided, temp->label); // We need to set this word to the proper location. fprintf(stderr, "RESOLVED 0x%04X: 0x%04X -> 0x%04X\n", mem_index, store, temp->address); store = temp->address; } // Now write the (potentially modified) word to the // output. fprintf(stderr, " >> WRITE 0x%04X\n", store); fwrite(&store, sizeof(uint16_t), 1, out); // Increment memory position. mem_index++; } // Close the file. fclose(in); // Reset and free the required and adjustment linked list. // FIXME: Actually free the lists! required = NULL; adjustment = NULL; } // Close file. fprintf(stderr, "linker: completed successfully.\n", input_files->filename[i]); fclose(out); return 0; }
int ParseMyArgs(int argc, char *argv[]) { int exitcode=0; int nerrors; argtable[0] =i= arg_lit0("i", "insert", "insert a tag into the text file"); argtable[1]=s= arg_lit0("s", "separate", "separate a text file into smaller parts"); arg_rem("NULL", "new files with number suffix"); argtable[2] =author= arg_lit0(NULL,"author", "print the author of each file"); argtable[3]=keyword= arg_str0(NULL, "keyword", "WORD", "keyword to seperate the parts in "); arg_rem(NULL, " a text book"); argtable[4] =size= arg_int0(NULL,"size", "SIZE", "size of each of parts"); argtable[5] =help= arg_lit0(NULL,"help", "display this help and exit"); argtable[6] =version= arg_lit0(NULL,"version", "display version information and exit"); argtable[7] = files = arg_filen(NULL, NULL, "FILE", 0, argc+2, NULL), argtable[8] = end= arg_end(20); /* verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable) != 0) { /* NULL entries were detected, some allocations must have failed */ printf("%s: insufficient memory\n",argv[0]); exitcode=1; goto exit; } /* Parse the command line as defined by argtable[] */ nerrors = arg_parse(argc,argv,argtable); if(author->count >0) { printf("This software is developed by a teacher and some students\n"); printf("Detail can be found in README file"); } /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("Usage: %s", argv[0]); arg_print_syntax(stdout,argtable,"\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); exitcode=1; goto exit; } /* special case: '--version' takes precedence error reporting */ if (version->count > 0){ printf("Dec 2010, \n"); exitcode=1; goto exit; } /* If the parser returned any errors then display them and exit */ if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout,end,argv[0]); printf("Try '%s --help' for more information.\n",argv[0]); exitcode=1; goto exit; } /* Command line parsing is complete, do the main processing */ exit: return exitcode; }
int _tmain(int argc, char* argv[]) { // determine my process name _splitpath(argv[0],NULL,NULL,gszProcName,NULL); #else int main(int argc, const char** argv) { // determine my process name CUtility::splitpath((char *)argv[0],NULL,gszProcName); #endif int iScreenLogLevel; // level of screen diagnostics int iFileLogLevel; // level of file diagnostics char szLogFile[_MAX_PATH]; // write diagnostics to this file int Rslt; int Idx; int iProcMode; char Strand; // process features on this strand int MinLen; int JoinLen; int LenFileList; int NumIncludeChroms; char *pszIncludeChroms[cMaxIncludeChroms]; int NumExcludeChroms; char *pszExcludeChroms[cMaxExcludeChroms]; etBEDRegion Region; // process for this functional region only int NumInFiles; // number of control input files char *pszInFileSpecs[cMaxNumBedFiles]; // input (wildcards allowed) BED files char szOutFile[_MAX_PATH]; // write merged to this file // command line args struct arg_lit *help = arg_lit0("h","help", "print this help and exit"); struct arg_lit *version = arg_lit0("v","version,ver", "print version information and exit"); struct arg_int *FileLogLevel=arg_int0("f", "FileLogLevel", "<int>","Level of diagnostics written to screen and logfile 0=fatal,1=errors,2=info,3=diagnostics,4=debug"); struct arg_file *LogFile = arg_file0("F","log","<file>", "diagnostics log file"); struct arg_int *ProcMode = arg_int0("m","mode","<int>", "merge processing mode: 0 - Strand independent union, 1 - Strand dependent union"); struct arg_int *strand = arg_int0("s","strand","<int>", "filter for this strand: 0 - any, 1 - Watson '+', 2 - Crick '-' (default is any)"); struct arg_int *region = arg_int0("r","genomicregion","<int>", "Retain annotated regions 0:ALL,1:Intergenic,2:Exons,3:Introns,4:CDS,5:UTRs,6:5'UTR,7:3'UTR (default = ALL)"); struct arg_int *minlen = arg_int0("l","minlen","<int>", "generated merged features must be of at least this length (default is 20)"); struct arg_int *joinlen = arg_int0("j","joinlen","<int>", "merge features which are separated by at most this length, 0 if no merging (default is 1)"); struct arg_str *excludechroms = arg_strn("Z","chromexclude","<string>",0,cMaxExcludeChroms,"regular expressions defining chromosomes to always exclude"); struct arg_str *includechroms = arg_strn("z","chromeinclude","<string>",0,cMaxIncludeChroms,"regular expressions defining chromosomes to explicitly include if not already excluded"); struct arg_file *infiles = arg_filen("i","srcfiles","<file>",0, cMaxNumBedFiles,"merge features contained in these BED files (wildcards alllowed)"); struct arg_file *OutFile = arg_file1("o",NULL,"<file>", "output merged features to this BED file"); struct arg_end *end = arg_end(20); void *argtable[] = {help,version,FileLogLevel,LogFile, ProcMode,strand,minlen,joinlen,region,excludechroms,includechroms,infiles,OutFile, end}; char **pAllArgs; int argerrors; argerrors = CUtility::arg_parsefromfile(argc,(char **)argv,&pAllArgs); if(argerrors >= 0) argerrors = arg_parse(argerrors,pAllArgs,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("\n%s BED Merge Blocks, Version %s\nOptions ---\n", gszProcName,cpszProgVer); arg_print_syntax(stdout,argtable,"\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); printf("\nNote: Parameters can be entered into a parameter file, one parameter per line."); printf("\n To invoke this parameter file then precede its name with '@'"); printf("\n e.g. %s @myparams.txt\n",gszProcName); printf("\nPlease report any issues regarding usage of %s at https://github.com/csiro-crop-informatics/biokanga/issues\n\n",gszProcName); exit(1); } /* special case: '--version' takes precedence error reporting */ if (version->count > 0) { printf("\n%s Version %s\n",gszProcName,cpszProgVer); exit(1); } if (!argerrors) { if(FileLogLevel->count && !LogFile->count) { printf("\nError: FileLogLevel '-f%d' specified but no logfile '-F<logfile>'",FileLogLevel->ival[0]); exit(1); } iScreenLogLevel = iFileLogLevel = FileLogLevel->count ? FileLogLevel->ival[0] : eDLInfo; if(iFileLogLevel < eDLNone || iFileLogLevel > eDLDebug) { printf("\nError: FileLogLevel '-l%d' specified outside of range %d..%d",iFileLogLevel,eDLNone,eDLDebug); exit(1); } if(LogFile->count) { strncpy(szLogFile,LogFile->filename[0],_MAX_PATH); szLogFile[_MAX_PATH-1] = '\0'; } else { iFileLogLevel = eDLNone; szLogFile[0] = '\0'; } iProcMode = ProcMode->count ? ProcMode->ival[0] : ePMDefault; if(iProcMode < ePMDefault || iProcMode >= ePMplaceholder) { printf("Error: Processing mode '-p%d' is not in range 0..%d",iProcMode,ePMplaceholder-1); exit(1); } MinLen = minlen->count ? minlen->ival[0] : cDfltMergeLen; if(MinLen < cMinMergeLen || MinLen > cMaxMergeLen) { printf("Error: minimum length '-l%d' is not in range %d..%d",MinLen,cMinMergeLen,cMaxMergeLen); exit(1); } JoinLen = joinlen->count ? joinlen->ival[0] : 1; if(JoinLen < 0 || JoinLen > cMaxMergeLen) { printf("Error: Join length '-j%d' is not in range 0..%d",JoinLen,cMaxMergeLen); exit(1); } Strand = strand->count ? strand->ival[0] : 0; if(Strand < 0 || Strand > 2) { printf("\nError: Strand '-s%d' specified outside of range 0..2",Strand); exit(1); } switch(Strand) { case 1: Strand = (int)'+'; break; case 2: Strand = (int)'-'; break; case 0: Strand = (int)'*'; break; } Region = (etBEDRegion)(region->count ? region->ival[0] : eMEGRAny); // default as being any region if(Region < eMEGRAny || Region > eMEG3UTR) { printf("\nSpecified region '-g%d' outside of range 0..%d",Region,eMEG3UTR); exit(1); } NumIncludeChroms = includechroms->count; for(Idx=0;Idx < includechroms->count; Idx++) { LenFileList = (int)strlen(includechroms->sval[Idx]); pszIncludeChroms[Idx] = new char [LenFileList+1]; strcpy(pszIncludeChroms[Idx],includechroms->sval[Idx]); TrimQuotes(pszIncludeChroms[Idx]); } NumExcludeChroms = excludechroms->count; for(Idx=0;Idx < excludechroms->count; Idx++) { LenFileList = (int)strlen(excludechroms->sval[Idx]); pszExcludeChroms[Idx] = new char [LenFileList+1]; strcpy(pszExcludeChroms[Idx],excludechroms->sval[Idx]); TrimQuotes(pszExcludeChroms[Idx]); } if(!infiles->count) { printf("\nError: No input file(s) specified with with '-i<filespec>' option)"); exit(1); } for(NumInFiles=Idx=0;NumInFiles < cMaxNumBedFiles && Idx < infiles->count; Idx++) { pszInFileSpecs[Idx] = NULL; if(pszInFileSpecs[NumInFiles] == NULL) pszInFileSpecs[NumInFiles] = new char [_MAX_PATH]; strncpy(pszInFileSpecs[NumInFiles],infiles->filename[Idx],_MAX_PATH); pszInFileSpecs[NumInFiles][_MAX_PATH-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(pszInFileSpecs[NumInFiles]); if(pszInFileSpecs[NumInFiles][0] != '\0') NumInFiles++; } if(!NumInFiles) { printf("\nError: After removal of whitespace, no input file(s) specified with '-i<filespec>' option)"); exit(1); } strncpy(szOutFile,OutFile->filename[0],_MAX_PATH); szOutFile[_MAX_PATH-1] = '\0'; // now that command parameters have been parsed then initialise diagnostics log system if(!gDiagnostics.Open(szLogFile,(etDiagLevel)iScreenLogLevel,(etDiagLevel)iFileLogLevel,true)) { printf("\nError: Unable to start diagnostics subsystem."); if(szLogFile[0] != '\0') printf(" Most likely cause is that logfile '%s' can't be opened/created",szLogFile); exit(1); } gDiagnostics.DiagOut(eDLInfo,gszProcName,"Version: %s Processing parameters:",cpszProgVer); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Processing Mode: %d (%s)",iProcMode,ProcMode2Txt((etProcMode)iProcMode)); gDiagnostics.DiagOutMsgOnly(eDLInfo,"process for this strand only: '%c'",(char)Strand); gDiagnostics.DiagOutMsgOnly(eDLInfo,"generated merged features will be at least this length: %d",MinLen); if(JoinLen == 0) gDiagnostics.DiagOutMsgOnly(eDLInfo,"keep separate features, no merging"); else gDiagnostics.DiagOutMsgOnly(eDLInfo,"merge features separated by upto: %d",JoinLen); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Retain Region: %s",Region2Txt((etBEDRegion)Region)); for(Idx = 0; Idx < NumIncludeChroms; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"reg expressions defining chroms to include: '%s'",pszIncludeChroms[Idx]); for(Idx = 0; Idx < NumExcludeChroms; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"reg expressions defining chroms to exclude: '%s'",pszExcludeChroms[Idx]); for(Idx=0;Idx < NumInFiles; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"merge features from this input BED file (%d): '%s'",Idx+1,pszInFileSpecs[Idx]); gDiagnostics.DiagOutMsgOnly(eDLInfo,"Output merged features into BED file: '%s'",szOutFile); gStopWatch.Start(); #ifdef _WIN32 SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS); #endif Rslt = Process((etProcMode)iProcMode,Strand,MinLen,JoinLen,Region, NumIncludeChroms, // number of chromosome regular expressions to include pszIncludeChroms, // array of include chromosome regular expressions NumExcludeChroms, // number of chromosome expressions to exclude pszExcludeChroms, // array of exclude chromosome regular expressions NumInFiles,pszInFileSpecs,szOutFile); gStopWatch.Stop(); Rslt = Rslt >=0 ? 0 : 1; gDiagnostics.DiagOut(eDLInfo,gszProcName,"Exit code: %d Total processing time: %s",Rslt,gStopWatch.Read()); exit(Rslt); } else { printf("\n%s BED Merge Blocks, Version %s\n",gszProcName,cpszProgVer); arg_print_errors(stdout,end,gszProcName); arg_print_syntax(stdout,argtable,"\nUse '-h' to view option and parameter usage\n"); exit(1); } }
int _tmain(int argc, char* argv[]) { // determine my process name _splitpath(argv[0],NULL,NULL,gszProcName,NULL); #else int main(int argc, const char** argv) { // determine my process name CUtility::splitpath((char *)argv[0],NULL,gszProcName); #endif int iScreenLogLevel; // level of screen diagnostics int iFileLogLevel; // level of file diagnostics char szLogFile[_MAX_PATH]; // write diagnostics to this file int Rslt; etPMode PMode; // processing mode int Idx; int NumInputFiles; // number of input sequence files char *pszInFastaFile[cMaxInFileSpecs]; // names of input sequence files (wildcards allowed) char szRsltsFile[_MAX_PATH]; // output stats to this file // command line args struct arg_lit *help = arg_lit0("h","help", "print this help and exit"); struct arg_lit *version = arg_lit0("v","version,ver", "print version information and exit"); struct arg_int *FileLogLevel=arg_int0("f", "FileLogLevel", "<int>","Level of diagnostics written to screen and logfile 0=fatal,1=errors,2=info,3=diagnostics,4=debug"); struct arg_file *LogFile = arg_file0("F","log","<file>", "diagnostics log file"); struct arg_int *pmode = arg_int0("m","mode","<int>", "Processing mode: 0 - BED output, single exon"); struct arg_file *pinputfiles = arg_filen("i","infasta","<file>",1,cMaxInFileSpecs,"input fasta file(s)"); struct arg_file *RsltsFile = arg_file0("o","output","<file>", "output BED file"); struct arg_end *end = arg_end(20); void *argtable[] = {help,version,FileLogLevel,LogFile, pmode,pinputfiles,RsltsFile, end}; char **pAllArgs; int argerrors; argerrors = CUtility::arg_parsefromfile(argc,(char **)argv,&pAllArgs); if(argerrors >= 0) argerrors = arg_parse(argerrors,pAllArgs,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("\n%s Generate BED file from multifasta file, Version %s\nOptions ---\n", gszProcName,cpszProgVer); arg_print_syntax(stdout,argtable,"\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); printf("\nNote: Parameters can be entered into a parameter file, one parameter per line."); printf("\n To invoke this parameter file then precede its name with '@'"); printf("\n e.g. %s @myparams.txt\n",gszProcName); printf("\nPlease report any issues regarding usage of %s at https://github.com/csiro-crop-informatics/biokanga/issues\n\n",gszProcName); exit(1); } /* special case: '--version' takes precedence error reporting */ if (version->count > 0) { printf("\n%s Version %s\n",gszProcName,cpszProgVer); exit(1); } if (!argerrors) { if(FileLogLevel->count && !LogFile->count) { printf("\nError: FileLogLevel '-f%d' specified but no logfile '-F<logfile>\n'",FileLogLevel->ival[0]); exit(1); } iScreenLogLevel = iFileLogLevel = FileLogLevel->count ? FileLogLevel->ival[0] : eDLInfo; if(iFileLogLevel < eDLNone || iFileLogLevel > eDLDebug) { printf("\nError: FileLogLevel '-l%d' specified outside of range %d..%d\n",iFileLogLevel,eDLNone,eDLDebug); exit(1); } if(LogFile->count) { strncpy(szLogFile,LogFile->filename[0],_MAX_PATH); szLogFile[_MAX_PATH-1] = '\0'; } else { iFileLogLevel = eDLNone; szLogFile[0] = '\0'; } // now that log parameters have been parsed then initialise diagnostics log system if(!gDiagnostics.Open(szLogFile,(etDiagLevel)iScreenLogLevel,(etDiagLevel)iFileLogLevel,true)) { printf("\nError: Unable to start diagnostics subsystem\n"); if(szLogFile[0] != '\0') printf(" Most likely cause is that logfile '%s' can't be opened/created\n",szLogFile); exit(1); } gDiagnostics.DiagOut(eDLInfo,gszProcName,"Version: %s",cpszProgVer); PMode = (etPMode)(pmode->count ? pmode->ival[0] : ePMdefault); if(PMode < ePMdefault || PMode >= ePMplaceholder) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: Processing mode '-m%d' specified outside of range %d..%d\n",PMode,ePMdefault,(int)ePMplaceholder-1); exit(1); } NumInputFiles = 0; for(NumInputFiles=Idx=0;NumInputFiles < cMaxInFileSpecs && Idx < pinputfiles->count; Idx++) { pszInFastaFile[Idx] = NULL; if(pszInFastaFile[NumInputFiles] == NULL) pszInFastaFile[NumInputFiles] = new char [_MAX_PATH]; strncpy(pszInFastaFile[NumInputFiles],pinputfiles->filename[Idx],_MAX_PATH); pszInFastaFile[NumInputFiles][_MAX_PATH-1] = '\0'; CUtility::TrimQuotedWhitespcExtd(pszInFastaFile[NumInputFiles]); if(pszInFastaFile[NumInputFiles][0] != '\0') NumInputFiles++; } if(!NumInputFiles) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: After removal of whitespace, no input file(s) specified with '-i<filespec>' option)\n"); exit(1); } if(!RsltsFile->count) { gDiagnostics.DiagOut(eDLFatal,gszProcName,"Error: No output BED file specified"); exit(1); } strncpy(szRsltsFile,RsltsFile->filename[0],_MAX_PATH); szRsltsFile[_MAX_PATH-1] = '\0'; gDiagnostics.DiagOut(eDLInfo,gszProcName,"Processing parameters:"); for(Idx=0; Idx < NumInputFiles; Idx++) gDiagnostics.DiagOutMsgOnly(eDLInfo,"input fasta sequence files (%d): '%s'",Idx+1,pszInFastaFile[Idx]); if(szRsltsFile[0] != '\0') gDiagnostics.DiagOutMsgOnly(eDLInfo,"Output to BED file: '%s'",szRsltsFile); #ifdef _WIN32 SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS); #endif // processing here... gStopWatch.Start(); Rslt = Process(PMode,NumInputFiles,pszInFastaFile,szRsltsFile); gStopWatch.Stop(); Rslt = Rslt >=0 ? 0 : 1; gDiagnostics.DiagOut(eDLInfo,gszProcName,"Exit code: %d Total processing time: %s",Rslt,gStopWatch.Read()); exit(Rslt); } else { printf("\n%s Generate BED file from multifasta file, Version %s\n",gszProcName,cpszProgVer); arg_print_errors(stdout,end,gszProcName); arg_print_syntax(stdout,argtable,"\nUse '-h' to view option and parameter usage\n"); exit(1); } return 0; }
/** * @brief Parse command line parameters. Will exit if help/usage etc * are called or or call Log(&rLog, LOG_FATAL, ) if an error was detected. * * @param[out] user_opts * User parameter struct, with defaults already set. * @param[in] argc * mains argc * @param[in] argv * mains argv * */ void ParseCommandLine(cmdline_opts_t *user_opts, int argc, char **argv) { /* argtable command line parsing: * see * http://argtable.sourceforge.net/doc/argtable2-intro.html * * basic structure is: arg_xxxN: * xxx can be int, lit, db1, str, rex, file or date * If N = 0, arguments may appear zero-or-once; N = 1 means * exactly once, N = n means up to maxcount times * * * @note: changes here, might also affect main.cpp:ConvertOldCmdLine() * */ struct arg_rem *rem_seq_input = arg_rem(NULL, "\nSequence Input:"); struct arg_file *opt_seqin = arg_file0("i", "in,infile", "{<file>,-}", "Multiple sequence input file (- for stdin)"); struct arg_file *opt_hmm_in = arg_filen(NULL, "hmm-in", "<file>", /*min*/ 0, /*max*/ 128, "HMM input files"); struct arg_lit *opt_dealign = arg_lit0(NULL, "dealign", "Dealign input sequences"); struct arg_file *opt_profile1 = arg_file0(NULL, "profile1,p1", "<file>", "Pre-aligned multiple sequence file (aligned columns will be kept fix)"); struct arg_file *opt_profile2 = arg_file0(NULL, "profile2,p2", "<file>", "Pre-aligned multiple sequence file (aligned columns will be kept fix)"); struct arg_str *opt_seqtype = arg_str0("t", "seqtype", "{Protein, RNA, DNA}", "Force a sequence type (default: auto)"); /* struct arg_lit *opt_force_protein = arg_lit0(NULL, "protein", "Set sequence type to protein even if Clustal guessed nucleic acid"); */ struct arg_str *opt_infmt = arg_str0(NULL, "infmt", "{a2m=fa[sta],clu[stal],msf,phy[lip],selex,st[ockholm],vie[nna]}", "Forced sequence input file format (default: auto)"); struct arg_rem *rem_guidetree = arg_rem(NULL, "\nClustering:"); struct arg_str *opt_pairdist = arg_str0("p", "pairdist", "{ktuple}", "Pairwise alignment distance measure"); struct arg_file *opt_distmat_in = arg_file0(NULL, "distmat-in", "<file>", "Pairwise distance matrix input file (skips distance computation)"); struct arg_file *opt_distmat_out = arg_file0(NULL, "distmat-out", "<file>", "Pairwise distance matrix output file"); struct arg_file *opt_guidetree_in = arg_file0(NULL, "guidetree-in", "<file>", "Guide tree input file (skips distance computation and guide-tree clustering step)"); struct arg_file *opt_guidetree_out = arg_file0(NULL, "guidetree-out", "<file>", "Guide tree output file"); /* AW: mbed is default since at least R253 struct arg_lit *opt_mbed = arg_lit0(NULL, "mbed", "Fast, Mbed-like clustering for guide-tree calculation"); struct arg_lit *opt_mbed_iter = arg_lit0(NULL, "mbed-iter", "Use Mbed-like clustering also during iteration"); */ /* Note: might be better to use arg_str (mbed=YES/NO) but I don't want to introduce an '=' into pipeline, FS, r250 -> */ struct arg_lit *opt_full = arg_lit0(NULL, "full", "Use full distance matrix for guide-tree calculation (might be slow; mBed is default)"); struct arg_lit *opt_full_iter = arg_lit0(NULL, "full-iter", "Use full distance matrix for guide-tree calculation during iteration (might be slowish; mBed is default)"); struct arg_str *opt_clustering = arg_str0("c", "clustering", "{UPGMA}", "Clustering method for guide tree"); struct arg_rem *rem_aln_output = arg_rem(NULL, "\nAlignment Output:"); struct arg_file *opt_outfile = arg_file0("o", "out,outfile", "{file,-}", "Multiple sequence alignment output file (default: stdout)"); struct arg_str *opt_outfmt = arg_str0(NULL, "outfmt", "{a2m=fa[sta],clu[stal],msf,phy[lip],selex,st[ockholm],vie[nna]}", "MSA output file format (default: fasta)"); struct arg_rem *rem_iteration = arg_rem(NULL, "\nIteration:"); struct arg_str *opt_num_iterations = arg_str0(NULL, "iterations,iter", /* FIXME "{<n>,auto}", "Number of combined guide-tree/HMM iterations"); */ "<n>", "Number of (combined guide-tree/HMM) iterations"); struct arg_int *opt_max_guidetree_iterations = arg_int0(NULL, "max-guidetree-iterations", "<n>", "Maximum number guidetree iterations"); struct arg_int *opt_max_hmm_iterations = arg_int0(NULL, "max-hmm-iterations", "<n>", "Maximum number of HMM iterations"); struct arg_rem *rem_limits = arg_rem(NULL, "\nLimits (will exit early, if exceeded):"); struct arg_int *opt_max_seq = arg_int0(NULL, "maxnumseq", "<n>", "Maximum allowed number of sequences"); struct arg_int *opt_max_seqlen = arg_int0(NULL, "maxseqlen", "<l>", "Maximum allowed sequence length"); struct arg_rem *rem_misc = arg_rem(NULL, "\nMiscellaneous:"); struct arg_lit *opt_autooptions = arg_lit0(NULL, "auto", "Set options automatically (might overwrite some of your options)"); struct arg_int *opt_threads = arg_int0(NULL, "threads", "<n>", "Number of processors to use"); struct arg_file *opt_logfile = arg_file0("l", "log", "<file>", "Log all non-essential output to this file"); struct arg_lit *opt_help = arg_lit0("h", "help", "Print this help and exit"); struct arg_lit *opt_version = arg_lit0(NULL, "version", "Print version information and exit"); struct arg_lit *opt_long_version = arg_lit0(NULL, "long-version", "Print long version information and exit"); struct arg_lit *opt_verbose = arg_litn("v", "verbose", 0, 3, "Verbose output (increases if given multiple times)"); struct arg_lit *opt_force = arg_lit0(NULL, "force", "Force file overwriting"); struct arg_int *opt_macram = arg_int0(NULL, "MAC-RAM", "<n>", /* keep this quiet for the moment, FS r240 -> */ NULL/*"maximum amount of RAM to use for MAC algorithm (in MB)"*/); struct arg_end *opt_end = arg_end(10); /* maximum number of errors * to store */ void *argtable[] = {rem_seq_input, opt_seqin, opt_hmm_in, opt_dealign, opt_profile1, opt_profile2, opt_seqtype, /* opt_force_protein, */ opt_infmt, rem_guidetree, #if 0 /* no other options then default available or not implemented */ opt_pairdist, #endif opt_distmat_in, opt_distmat_out, opt_guidetree_in, opt_guidetree_out, opt_full, /* FS, r250 -> */ opt_full_iter, /* FS, r250 -> */ #if 0 /* no other options then default available */ opt_clustering, #endif rem_aln_output, opt_outfile, opt_outfmt, rem_iteration, opt_num_iterations, opt_max_guidetree_iterations, opt_max_hmm_iterations, rem_limits, opt_max_seq, opt_max_seqlen, rem_misc, opt_autooptions, opt_threads, opt_logfile, opt_help, opt_verbose, opt_version, opt_long_version, opt_force, opt_macram, /* FS, r240 -> r241 */ opt_end}; int nerrors; /* Verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable)) { Log(&rLog, LOG_FATAL, "insufficient memory (for argtable allocation)"); } /* Parse the command line as defined by argtable[] */ nerrors = arg_parse(argc, argv, argtable); /* Special case: '--help' takes precedence over error reporting */ if (opt_help->count > 0) { printf("%s - %s (%s)\n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_CODENAME); printf("\n"); printf("If you like Clustal-Omega please cite:\n%s\n", CITATION); printf("If you don't like Clustal-Omega, please let us know why (and cite us anyway).\n"); /* printf("You can contact reach us under %s\n", PACKAGE_BUGREPORT); */ printf("\n"); printf("Check http://www.clustal.org for more information and updates.\n"); printf("\n"); printf("Usage: %s", basename(argv[0])); arg_print_syntax(stdout,argtable, "\n"); printf("\n"); printf("A typical invocation would be: %s -i my-in-seqs.fa -o my-out-seqs.fa -v\n", basename(argv[0])); printf("See below for a list of all options.\n"); arg_print_glossary(stdout, argtable, " %-25s %s\n"); arg_freetable(argtable, sizeof(argtable)/sizeof(argtable[0])); exit(EXIT_SUCCESS); } /* Special case: '--version' takes precedence over error reporting */ if (opt_version->count > 0) { printf("%s\n", PACKAGE_VERSION); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); exit(EXIT_SUCCESS); } /* Special case: '--long-version' takes precedence over error reporting */ if (opt_long_version->count > 0) { char zcLongVersion[1024]; PrintLongVersion(zcLongVersion, sizeof(zcLongVersion)); printf("%s\n", zcLongVersion); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); exit(EXIT_SUCCESS); } /* If the parser returned any errors then display them and exit */ if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout, opt_end, PACKAGE); fprintf(stderr, "For more information try: %s --help\n", argv[0]); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); exit(EXIT_FAILURE); } /* ------------------------------------------------------------ * * Command line successfully parsed. Now transfer values to * user_opts. While doing so, make sure that given input files * exist and given output files are writable do not exist, or if * they do, should be overwritten. * * No logic checks here! They are done in a different function * * ------------------------------------------------------------*/ /* not part of user_opts because it declared in src/util.h */ if (0 == opt_verbose->count) { rLog.iLogLevelEnabled = LOG_WARN; } else if (1 == opt_verbose->count) { rLog.iLogLevelEnabled = LOG_INFO; } else if (2 == opt_verbose->count) { rLog.iLogLevelEnabled = LOG_VERBOSE; } else if (3 == opt_verbose->count) { rLog.iLogLevelEnabled = LOG_DEBUG; } user_opts->aln_opts.bAutoOptions = opt_autooptions->count; user_opts->bDealignInputSeqs = opt_dealign->count; /* NOTE: full distance matrix used to be default - there was --mbed flag but no --full flag after r250 decided that mBed should be default - now need --full flag to turn off mBed. wanted to retain mBed Boolean, so simply added --full flag. if both flags set (erroneously) want --mbed to overwrite --full, therefore do --full 1st, the --mbed, FS, r250 */ if (opt_full->count){ user_opts->aln_opts.bUseMbed = FALSE; } if (opt_full_iter->count){ user_opts->aln_opts.bUseMbedForIteration = FALSE; } user_opts->bForceFileOverwrite = opt_force->count; /* log-file */ if (opt_logfile->count > 0) { user_opts->pcLogFile = CkStrdup(opt_logfile->filename[0]); /* warn if already exists or not writable */ if (FileExists(user_opts->pcLogFile) && ! user_opts->bForceFileOverwrite) { Log(&rLog, LOG_FATAL, "%s '%s'. %s", "Cowardly refusing to overwrite already existing file", user_opts->pcLogFile, "Use --force to force overwriting."); } if (! FileIsWritable(user_opts->pcLogFile)) { Log(&rLog, LOG_FATAL, "Sorry, I do not have permission to write to file '%s'.", user_opts->pcLogFile); } } /* normal sequence input (no profile) */ if (opt_seqin->count > 0) { user_opts->pcSeqInfile = CkStrdup(opt_seqin->filename[0]); } /* Input limitations */ /* maximum number of sequences */ if (opt_max_seq->count > 0) { user_opts->iMaxNumSeq = opt_max_seq->ival[0]; } /* maximum sequence length */ if (opt_max_seqlen->count > 0) { user_opts->iMaxSeqLen = opt_max_seqlen->ival[0]; } /* Output format */ if (opt_infmt->count > 0) { /* avoid gcc warning about discarded qualifier */ char *tmp = (char *)opt_infmt->sval[0]; user_opts->iSeqInFormat = String2SeqfileFormat(tmp); } else { user_opts->iSeqInFormat = SQFILE_UNKNOWN; } /* Sequence type */ if (opt_seqtype->count > 0) { if (STR_NC_EQ(opt_seqtype->sval[0], "protein")) { user_opts->iSeqType = SEQTYPE_PROTEIN; } else if (STR_NC_EQ(opt_seqtype->sval[0], "rna")) { user_opts->iSeqType = SEQTYPE_RNA; } else if (STR_NC_EQ(opt_seqtype->sval[0], "dna")) { user_opts->iSeqType = SEQTYPE_DNA; } else { Log(&rLog, LOG_FATAL, "Unknown sequence type '%s'", opt_seqtype->sval[0]); } } /* if (opt_force_protein->count > 0) { user_opts->iSeqType = SEQTYPE_PROTEIN; } */ /* Profile input */ if (opt_profile1->count > 0) { user_opts->pcProfile1Infile = CkStrdup(opt_profile1->filename[0]); if (! FileExists(user_opts->pcProfile1Infile)) { Log(&rLog, LOG_FATAL, "File '%s' does not exist.", user_opts->pcProfile1Infile); } } if (opt_profile2->count > 0) { user_opts->pcProfile2Infile = CkStrdup(opt_profile2->filename[0]); if (! FileExists(user_opts->pcProfile2Infile)) { Log(&rLog, LOG_FATAL, "File '%s' does not exist.", user_opts->pcProfile2Infile); } } /* HMM input */ user_opts->aln_opts.iHMMInputFiles = 0; user_opts->aln_opts.ppcHMMInput = NULL; if (opt_hmm_in->count>0) { int iAux; user_opts->aln_opts.iHMMInputFiles = opt_hmm_in->count; user_opts->aln_opts.ppcHMMInput = (char **) CKMALLOC( user_opts->aln_opts.iHMMInputFiles * sizeof(char*)); for (iAux=0; iAux<opt_hmm_in->count; iAux++) { user_opts->aln_opts.ppcHMMInput[iAux] = CkStrdup(opt_hmm_in->filename[iAux]); if (! FileExists(user_opts->aln_opts.ppcHMMInput[iAux])) { Log(&rLog, LOG_FATAL, "File '%s' does not exist.", user_opts->aln_opts.ppcHMMInput[iAux]); } } } /* Pair distance method */ if (opt_pairdist->count > 0) { if (STR_NC_EQ(opt_pairdist->sval[0], "ktuple")) { user_opts->aln_opts.iPairDistType = PAIRDIST_KTUPLE; } else { Log(&rLog, LOG_FATAL, "Unknown pairdist method '%s'", opt_pairdist->sval[0]); } } /* Distance matrix input */ if (opt_distmat_in->count > 0) { user_opts->aln_opts.pcDistmatInfile = CkStrdup(opt_distmat_in->filename[0]); if (! FileExists(user_opts->aln_opts.pcDistmatInfile)) { Log(&rLog, LOG_FATAL, "File '%s' does not exist.", user_opts->aln_opts.pcDistmatInfile); } } /* Distance matrix output */ if (opt_distmat_out->count > 0) { user_opts->aln_opts.pcDistmatOutfile = CkStrdup(opt_distmat_out->filename[0]); /* warn if already exists or not writable */ if (FileExists(user_opts->aln_opts.pcDistmatOutfile) && ! user_opts->bForceFileOverwrite) { Log(&rLog, LOG_FATAL, "%s '%s'. %s", "Cowardly refusing to overwrite already existing file", user_opts->aln_opts.pcDistmatOutfile, "Use --force to force overwriting."); } if (! FileIsWritable(user_opts->aln_opts.pcDistmatOutfile)) { Log(&rLog, LOG_FATAL, "Sorry, I do not have permission to write to file '%s'.", user_opts->aln_opts.pcDistmatOutfile); } } /* Clustering * */ if (opt_clustering->count > 0) { if (STR_NC_EQ(opt_clustering->sval[0], "upgma")) { user_opts->aln_opts.iClusteringType = CLUSTERING_UPGMA; } else { Log(&rLog, LOG_FATAL, "Unknown guide-tree clustering method '%s'", opt_clustering->sval[0]); } } /* Guidetree input */ if (opt_guidetree_in->count > 0) { user_opts->aln_opts.pcGuidetreeInfile = CkStrdup(opt_guidetree_in->filename[0]); if (! FileExists(user_opts->aln_opts.pcGuidetreeInfile)) { Log(&rLog, LOG_FATAL, "File '%s' does not exist.", user_opts->aln_opts.pcGuidetreeInfile); } } /* Guidetree output */ if (opt_guidetree_out->count > 0) { user_opts->aln_opts.pcGuidetreeOutfile = CkStrdup(opt_guidetree_out->filename[0]); /* warn if already exists or not writable */ if (FileExists(user_opts->aln_opts.pcGuidetreeOutfile) && ! user_opts->bForceFileOverwrite) { Log(&rLog, LOG_FATAL, "%s '%s'. %s", "Cowardly refusing to overwrite already existing file", user_opts->aln_opts.pcGuidetreeOutfile, "Use --force to force overwriting."); } if (! FileIsWritable(user_opts->aln_opts.pcGuidetreeOutfile)) { Log(&rLog, LOG_FATAL, "Sorry, I do not have permission to write to file '%s'.", user_opts->aln_opts.pcGuidetreeOutfile); } } /* max guidetree iterations */ if (opt_max_guidetree_iterations->count > 0) { user_opts->aln_opts.iMaxGuidetreeIterations = opt_max_guidetree_iterations->ival[0]; } /* max guidetree iterations */ if (opt_max_hmm_iterations->count > 0) { user_opts->aln_opts.iMaxHMMIterations = opt_max_hmm_iterations->ival[0]; } /* number of iterations */ if (opt_num_iterations->count > 0) { if (STR_NC_EQ(opt_num_iterations->sval[0], "auto")) { Log(&rLog, LOG_FATAL, "Automatic iteration not supported at the moment."); user_opts->aln_opts.bIterationsAuto = TRUE; } else { int iAux; user_opts->aln_opts.bIterationsAuto = FALSE; for (iAux=0; iAux<(int)strlen(opt_num_iterations->sval[0]); iAux++) { if (! isdigit(opt_num_iterations->sval[0][iAux])) { Log(&rLog, LOG_FATAL, "Couldn't iteration parameter: %s", opt_num_iterations->sval[0]); } } user_opts->aln_opts.iNumIterations = atoi(opt_num_iterations->sval[0]); } } /* Alignment output */ if (opt_outfile->count > 0) { user_opts->pcAlnOutfile = CkStrdup(opt_outfile->filename[0]); /* warn if already exists or not writable */ if (FileExists(user_opts->pcAlnOutfile) && ! user_opts->bForceFileOverwrite) { Log(&rLog, LOG_FATAL, "%s '%s'. %s", "Cowardly refusing to overwrite already existing file", user_opts->pcAlnOutfile, "Use --force to force overwriting."); } if (! FileIsWritable(user_opts->pcAlnOutfile)) { Log(&rLog, LOG_FATAL, "Sorry, I do not have permission to write to file '%s'.", user_opts->pcAlnOutfile); } } /* Output format */ if (opt_outfmt->count > 0) { /* avoid gcc warning about discarded qualifier */ char *tmp = (char *)opt_outfmt->sval[0]; user_opts->iAlnOutFormat = String2SeqfileFormat(tmp); if (SQFILE_UNKNOWN == user_opts->iAlnOutFormat) { Log(&rLog, LOG_FATAL, "Unknown output format '%s'", opt_outfmt->sval[0]); } } /* Number of threads */ #ifdef HAVE_OPENMP if (opt_threads->count > 0) { if (opt_threads->ival[0] <= 0) { Log(&rLog, LOG_FATAL, "Changing number of threads to %d doesn't make sense.", opt_threads->ival[0]); } user_opts->iThreads = opt_threads->ival[0]; } #else if (opt_threads->count > 0) { if (opt_threads->ival[0] > 1) { Log(&rLog, LOG_FATAL, "Cannot change number of threads to %d. %s was build without OpenMP support.", opt_threads->ival[0], PACKAGE_NAME); } } #endif /* max MAC RAM (maximum amount of RAM set aside for MAC algorithm) */ if (opt_macram->count > 0) { /* FS, r240 -> r241 */ user_opts->aln_opts.rHhalignPara.iMacRamMB = opt_macram->ival[0]; } arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); UserOptsLogicCheck(user_opts); return; }
int main(int argc, char* argv[]) { // Define arguments. struct arg_lit* show_help = arg_lit0("h", "help", "Show this help."); struct arg_str* type_assembler = arg_str0("t", NULL, "<type>", "The type of assembler to output for."); struct arg_file* input_file = arg_file1(NULL, NULL, "<file>", "The input file (or - to read from standard input)."); struct arg_file* output_file = arg_file1("o", "output", "<file>", "The output file (or - to send to standard output)."); // 20 is maxcount for include directories, this has to be set to some constant number. struct arg_file* include_dirs = arg_filen("I", NULL, "<directory>", 0, 20, "Adds the directory <dir> to the directories to be searched for header files."); struct arg_lit* verbose = arg_litn("v", NULL, 0, LEVEL_EVERYTHING - LEVEL_DEFAULT, "Increase verbosity."); struct arg_lit* quiet = arg_litn("q", NULL, 0, LEVEL_DEFAULT - LEVEL_SILENT, "Decrease verbosity."); struct arg_end* end = arg_end(20); void* argtable[] = { output_file, show_help, type_assembler, include_dirs, input_file, verbose, quiet, end }; // Parse arguments. int nerrors = arg_parse(argc, argv, argtable); version_print(bautofree(bfromcstr("Compiler"))); if (nerrors != 0 || show_help->count != 0) { if (nerrors != 0) arg_print_errors(stderr, end, "compiler"); fprintf(stderr, "syntax:\n dtcc"); arg_print_syntax(stderr, argtable, "\n"); fprintf(stderr, "options:\n"); arg_print_glossary(stderr, argtable, " %-25s %s\n"); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Set verbosity level. debug_setlevel(LEVEL_DEFAULT + verbose->count - quiet->count); // Set global path variable. osutil_setarg0(bautofree(bfromcstr(argv[0]))); // Run the preprocessor. ppfind_add_path(bautofree(bfromcstr("."))); ppfind_add_path(bautofree(bfromcstr("include"))); ppfind_add_autopath(bautofree(bfromcstr(input_file->filename[0]))); for (int i = 0; i < include_dirs->count; ++i) ppfind_add_path(bautofree(bfromcstr(include_dirs->filename[i]))); bstring pp_result_name = pp_do(bautofree(bfromcstr(input_file->filename[0]))); if (pp_result_name == NULL) { fprintf(stderr, "compiler: invalid result returned from preprocessor.\n"); pp_cleanup(bautofree(pp_result_name)); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Parse C. yyout = stderr; yyin = fopen((const char*)(pp_result_name->data), "r"); if (yyin == NULL) { pp_cleanup(bautofree(pp_result_name)); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } yyparse(); if (yyin != stdin) fclose(yyin); pp_cleanup(bautofree(pp_result_name)); if (program == NULL) { std::cerr << "An error occurred while compiling." << std::endl; arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Assembler type. const char* asmtype = "toolchain"; if (type_assembler->count > 0) asmtype = type_assembler->sval[0]; // Initially save to a temporary file. std::string temp = std::string(tempnam(".", "cc.")); // Generate assembly using the AST. try { AsmGenerator generator(asmtype); AsmBlock* block = program->compile(generator); std::ofstream output(temp.c_str(), std::ios::out | std::ios::trunc); if (output.bad() || output.fail()) { printd(LEVEL_ERROR, "compiler: temporary file not writable.\n"); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } output << *block << std::endl; output.close(); delete block; } catch (CompilerException* ex) { std::string msg = ex->getMessage(); std::cerr << "An error occurred while compiling." << std::endl; std::cerr << msg << std::endl; arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Re-open the temporary file for reading. std::ifstream input(temp.c_str(), std::ios::in); if (input.bad() || input.fail()) { printd(LEVEL_ERROR, "compiler: temporary file not readable.\n"); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Open the output file. std::ostream* output; if (strcmp(output_file->filename[0], "-") != 0) { // Write to file. output = new std::ofstream(output_file->filename[0], std::ios::out | std::ios::trunc); if (output->bad() || output->fail()) { printd(LEVEL_ERROR, "compiler: output file not readable.\n"); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } } else { // Set output to cout. output = &std::cout; } // Copy data. std::copy(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(*output)); // Close files and delete temporary. if (strcmp(output_file->filename[0], "-") != 0) { ((std::ofstream*)output)->close(); delete output; } input.close(); unlink(temp.c_str()); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 0; }
int GRTCLCrackCommandLineData::ParseCommandLine(int argc, char *argv[]) { int deviceCount, i; std::vector<std::string> webTableFilenames; // Command line argument parsing with argtable struct arg_lit *verbose = arg_lit0("v", "verbose", "verbose output"); struct arg_lit *silent = arg_lit0(NULL, "silent", "silence all output"); // Table related options struct arg_file *table_file = arg_filen(NULL,NULL,"<file>", 0, 10000, "GRT Tables to use"); struct arg_file *hash_file = arg_file0("f","hashfile","<file>", "Hashfile to use"); struct arg_str *hash_value = arg_str0("s", "hashstring", "hashstring", "The hash string"); struct arg_str *hash_type = arg_str1("h", "hashtype", "{NTLM, MD4, MD5, SHA1}", "hash type to crack"); // CUDA related params struct arg_int *device = arg_int0("d", "device", "<n>", "OpenCL device to use"); struct arg_int *platform = arg_int0("p", "platform", "<n>", "OpenCL platform to use"); struct arg_int *m = arg_int0("m", "ms", "<n>", "target step time in ms"); struct arg_int *blocks = arg_int0("b", "blocks", "<n>", "number of thread blocks to run"); struct arg_int *threads = arg_int0("t", "threads", "<n>", "number of threads per block"); struct arg_lit *zerocopy = arg_lit0("z", "zerocopy", "use zerocopy memory"); struct arg_file *o = arg_file0("o", "outputfile", "outputfile", "output file for results"); // hexoutput: Adds hex output to all password outputs. struct arg_lit *hex_output = arg_lit0(NULL, "hexoutput", "Adds hex output to all hash outputs"); struct arg_lit *amd_kernels = arg_lit0(NULL, "amd", "use AMD vector kernels"); struct arg_int *vector_width = arg_int0(NULL, "vectorwidth", "<n>", "vector width"); struct arg_lit *debug = arg_lit0(NULL, "debug", "Use debug display class"); struct arg_lit *devdebug = arg_lit0(NULL, "devdebug", "Developer debugging output"); struct arg_str *debugfiles = arg_str0(NULL, "debugdumpfiles", "<filename>", "Filename base to dump candidates and chains to"); struct arg_int *prefetch_count = arg_int0(NULL, "prefetch", "<n>", "number of prefetch threads"); struct arg_int *candidates_to_skip = arg_int0(NULL, "skip", "<n>", "number of candidate hashes to skip"); struct arg_str *table_url = arg_str0(NULL, "tableurl", "<URL>", "URL of the web table script"); struct arg_str *table_username = arg_str0(NULL, "tableusername", "<username>", "Username, if required, for the web table script"); struct arg_str *table_password = arg_str0(NULL, "tablepassword", "<password>", "Password, if required, for the web table script"); struct arg_end *end = arg_end(20); void *argtable[] = {verbose,silent,table_file,hash_value,hash_file,hash_type, device,platform,m,blocks,threads,zerocopy,o,amd_kernels,vector_width, debug, devdebug, prefetch_count,table_url,table_username,table_password, candidates_to_skip,debugfiles,hex_output,end}; // Get arguments, collect data, check for basic errors. if (arg_nullcheck(argtable) != 0) { printf("error: insufficient memory\n"); } // Look for errors int nerrors = arg_parse(argc,argv,argtable); if (nerrors > 0) { // Print errors, exit. arg_print_errors(stdout,end,argv[0]); //arg_print_syntax(stdout,argtable,"\n\n"); printf("\n\nOptions: \n"); arg_print_glossary(stdout,argtable," %-20s %s\n"); exit(1); } // Verbose & silent if (verbose->count) { this->Verbose = 1; } if (silent->count) { this->Silent = 1; } if (zerocopy->count) { this->CUDAUseZeroCopy = 1; } if (debug->count) { this->Debug = 1; } if (devdebug->count) { this->Debug = 1; this->DeveloperDebug = 1; } if (debugfiles->count) { this->DebugDump = 1; this->DebugDumpFilenameBase = *debugfiles->sval; } if (prefetch_count->count) { this->NumberPrefetchThreads = *prefetch_count->ival; } if (candidates_to_skip->count) { this->CandidateHashesToSkip = *candidates_to_skip->ival; } // Web table stuff if (table_url->count) { this->useWebTable = 1; this->tableURL = *table_url->sval; // If someone has NOT specified the candidates to skip, set to default. if (!candidates_to_skip->count) { this->CandidateHashesToSkip = DEFAULT_CANDIDATES_TO_SKIP; } } if (table_username->count) { this->tableUsername = *table_username->sval; } if (table_password->count) { this->tablePassword = *table_password->sval; } this->HashType = this->GRTHashTypes->GetHashIdFromString(*hash_type->sval); if (this->HashType == -1) { printf("Unknown hash type %s: Exiting.\n\n", *hash_type->sval); exit(1); } int correct_length = this->GRTHashTypes->GetHashLengthFromId(this->HashType); // if we know the correct length, we make sure the hash is the correct length if (correct_length != 0) { if ((hash_value->count) && (strlen(*hash_value->sval) != correct_length)) { printf("Hash string is not %d hex characters. Exiting.\n\n", correct_length); exit(1); } } if (hash_value->count) { convertAsciiToBinary(*hash_value->sval, this->Hash, 16); } else if (hash_file->count) { this->hashFileName = hash_file->filename[0]; this->useHashFile = 1; } else { printf("Must provide a hash value or a hash file!\n"); exit(1); } if (o->count) { this->outputHashFileName = o->filename[0]; this->useOutputHashFile = 1; } // Desired kernel time if (m->count) { this->KernelTimeMs = *m->ival; } // Do this to emulate CUDA behavior for now... // Threads - if not set, leave at default 0 if (threads->count) { this->OpenCLWorkitems = *threads->ival; } // Blocks - if not set, leave at default 0 if (blocks->count) { this->OpenCLWorkgroups = *blocks->ival * this->OpenCLWorkitems; } if (hex_output->count) { this->AddHexOutput = 1; } // Allocate space for the list of pointers // Create the table header type if (this->useWebTable) { this->TableHeader = new GRTTableHeaderVWeb(); this->TableHeader->setWebURL(this->tableURL); this->TableHeader->setWebUsername(this->tableUsername); this->TableHeader->setWebPassword(this->tablePassword); GRTTableHeaderVWeb *WebTableHeader = (GRTTableHeaderVWeb *)this->TableHeader; webTableFilenames = WebTableHeader->getHashesFromServerByType(this->HashType); } else { // V1 will work for both V1 & V2 types this->TableHeader = new GRTTableHeaderV1(); } // If we don't have any table filenames, get the ones from the web. // Note: The script ONLY reutrns valid tables. if ((table_file->count == 0) && this->useWebTable) { this->Table_File_Count = webTableFilenames.size(); this->Table_Filenames = (char **)malloc(this->Table_File_Count * sizeof(char *)); for (i = 0; i < this->Table_File_Count; i++) { // Increment size by 1 for null termination this->Table_Filenames[i] = (char *)malloc((webTableFilenames.at(i).size() + 1) * sizeof(char)); strcpy(this->Table_Filenames[i], webTableFilenames.at(i).c_str()); } } else { this->Table_File_Count = table_file->count; this->Table_Filenames = (char **)malloc(this->Table_File_Count * sizeof(char *)); // Handle the file list sanely for (i = 0; i < table_file->count; i++) { // Check to ensure the file is valid if (!this->TableHeader->isValidTable(table_file->filename[i], -1)) { printf("%s is not a valid GRT table! Exiting.\n", table_file->filename[i]); exit(1); } // Check to ensure the file is of the right type if (!this->TableHeader->isValidTable(table_file->filename[i], this->HashType)) { printf("%s is not a valid %s GRT table!\n", table_file->filename[i], this->GRTHashTypes->GetHashStringFromId(this->HashType)); exit(1); } // Increment size by 1 for null termination this->Table_Filenames[i] = (char *)malloc((strlen(table_file->filename[i]) + 1) * sizeof(char)); strcpy(this->Table_Filenames[i], table_file->filename[i]); } } // Finally, set the CUDA device and look for errors. if (device->count) { this->OpenCLDevice = *device->ival; } if (platform->count) { this->OpenCLPlatform = *platform->ival; } if (amd_kernels->count) { this->useAmdKernels = 1; this->vectorWidth = 4; } if (vector_width->count) { this->vectorWidth = *vector_width->ival; } }
int main(int argc, char **argv) { struct arg_lit *list = arg_lit0("lL",NULL, "list files"); struct arg_lit *recurse = arg_lit0("R",NULL, "recurse through subdirectories"); struct arg_int *repeat = arg_int0("k","scalar",NULL, "define scalar value k (default is 3)"); struct arg_str *defines = arg_strn("D","define","MACRO",0,argc+2, "macro definitions"); struct arg_file *outfile = arg_file0("o",NULL,"<output>", "output file (default is \"-\")"); struct arg_lit *verbose = arg_lit0("v","verbose,debug", "verbose messages"); struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); struct arg_lit *version = arg_lit0(NULL,"version", "print version information and exit"); struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,argc+2, "input file(s)"); struct arg_end *end = arg_end(20); void* argtable[] = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end}; const char* progname = "myprog"; int nerrors; int exitcode=0; /* verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable) != 0) { /* NULL entries were detected, some allocations must have failed */ printf("%s: insufficient memory\n",progname); exitcode=1; goto exit; } /* set any command line default values prior to parsing */ repeat->ival[0]=3; outfile->filename[0]="-"; /* Parse the command line as defined by argtable[] */ nerrors = arg_parse(argc,argv,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("Usage: %s", progname); arg_print_syntax(stdout,argtable,"\n"); printf("This program demonstrates the use of the argtable2 library\n"); printf("for parsing command line arguments.\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); exitcode=0; goto exit; } /* special case: '--version' takes precedence error reporting */ if (version->count > 0) { printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); printf("September 2003, Stewart Heitmann\n"); exitcode=0; goto exit; } /* If the parser returned any errors then display them and exit */ if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout,end,progname); printf("Try '%s --help' for more information.\n",progname); exitcode=1; goto exit; } /* special case: uname with no command line options induces brief help */ if (argc==1) { printf("Try '%s --help' for more information.\n",progname); exitcode=0; goto exit; } /* normal case: take the command line options at face value */ exitcode = mymain(list->count, recurse->count, repeat->ival[0], defines->sval, defines->count, outfile->filename[0], verbose->count, infiles->filename, infiles->count); exit: /* deallocate each non-null entry in argtable[] */ arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return exitcode; }
int main(int argc, char **argv) { /* The argtable[] entries define the command line options */ void *argtable[] = { a = arg_lit0("a", "all", "do not hide entries starting with ."), A = arg_lit0("A", "almost-all", "do not list implied . and .."), author = arg_lit0(NULL,"author", "print the author of each file"), b = arg_lit0("b", "escape", "print octal escapes for nongraphic characters"), blocksize = arg_int0(NULL,"block-size","SIZE", "use SIZE-byte blocks"), B = arg_lit0("B", "ignore-backups", "do not list implied entries ending with ~"), c = arg_lit0("c", NULL, "with -lt: sort by, and show, ctime (time of last"), arg_rem(NULL, " modification of file status information)"), arg_rem(NULL, " with -l: show ctime and sort by name"), arg_rem(NULL, " otherwise: sort by ctime"), C = arg_lit0("C", NULL, "list entries by columns"), color = arg_str0(NULL,"color","WHEN", "control whether color is used to distinguish file"), arg_rem(NULL, " types. WHEN may be `never', `always', or `auto'"), d = arg_lit0("d", "directory", "list directory entries instead of contents,"), arg_rem(NULL, " and do not dereference symbolic links"), D = arg_lit0("D", "dired", "generate output designed for Emacs' dired mode"), f = arg_lit0("f", NULL, "do not sort, enable -aU, disable -lst"), F = arg_lit0("F", "classify", "append indicator (one of */=@|) to entries"), format = arg_str0(NULL,"format","WORD", "across -x, commas -m, horizontal -x, long -l,"), arg_rem (NULL, " single-column -1, verbose -l, vertical -C"), fulltime = arg_lit0(NULL,"full-time", "like -l --time-style=full-iso"), g = arg_lit0("g", NULL, "like -l, but do not list owner"), G = arg_lit0("G", "no-group", "inhibit display of group information"), h = arg_lit0("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"), si = arg_lit0(NULL,"si", "likewise, but use powers of 1000 not 1024"), H = arg_lit0("H", "dereference-command-line","follow symbolic links listed on the command line"), deref = arg_lit0(NULL,"dereference-command-line-symlink-to-dir","follow each command line symbolic link"), arg_rem(NULL, " that points to a directory"), indic = arg_str0(NULL,"indicator-style","WORD","append indicator with style WORD to entry names:"), arg_rem (NULL, " none (default), classify (-F), file-type (-p)"), i = arg_lit0("i", "inode", "print index number of each file"), I = arg_str0("I", "ignore","PATTERN", "do not list implied entries matching shell PATTERN"), k = arg_lit0("k", NULL, "like --block-size=1K"), l = arg_lit0("l", NULL, "use a long listing format"), L = arg_lit0("L", "dereference", "when showing file information for a symbolic"), arg_rem (NULL, " link, show information for the file the link"), arg_rem (NULL, " references rather than for the link itself"), m = arg_lit0("m", NULL, "fill width with a comma separated list of entries"), n = arg_lit0("n", "numeric-uid-gid", "like -l, but list numeric UIDs and GIDs"), N = arg_lit0("N", "literal", "print raw entry names (don't treat e.g. control"), arg_rem (NULL, " characters specially)"), o = arg_lit0("o", NULL, "like -l, but do not list group information"), p = arg_lit0("p", "file-type", "append indicator (one of /=@|) to entries"), q = arg_lit0("q", "hide-control-chars", "print ? instead of non graphic characters"), shcont = arg_lit0(NULL,"show-control-chars", "show non graphic characters as-is (default"), arg_rem (NULL, "unless program is `ls' and output is a terminal)"), Q = arg_lit0("Q", "quote-name", "enclose entry names in double quotes"), Qstyle = arg_str0(NULL,"quoting-style","WORD","use quoting style WORD for entry names:"), arg_rem (NULL, " literal, locale, shell, shell-always, c, escape"), r = arg_lit0("r", "reverse", "reverse order while sorting"), R = arg_lit0("R", "recursive", "list subdirectories recursively"), s = arg_lit0("s", "size", "print size of each file, in blocks"), S = arg_lit0("S", NULL, "sort by file size"), sort = arg_str0(NULL,"sort","WORD", "extension -X, none -U, size -S, time -t, version -v,"), arg_rem (NULL, "status -c, time -t, atime -u, access -u, use -u"), Time = arg_str0(NULL,"time","WORD", "show time as WORD instead of modification time:"), arg_rem (NULL, " atime, access, use, ctime or status; use"), arg_rem (NULL, " specified time as sort key if --sort=time"), timesty = arg_str0(NULL, "time-style","STYLE", "show times using style STYLE:"), arg_rem (NULL, " full-iso, long-iso, iso, locale, +FORMAT"), arg_rem (NULL, "FORMAT is interpreted like `date'; if FORMAT is"), arg_rem (NULL, "FORMAT1<newline>FORMAT2, FORMAT1 applies to"), arg_rem (NULL, "non-recent files and FORMAT2 to recent files;"), arg_rem (NULL, "if STYLE is prefixed with `posix-', STYLE"), arg_rem (NULL, "takes effect only outside the POSIX locale"), t = arg_lit0("t", NULL, "sort by modification time"), T = arg_int0("T", "tabsize", "COLS", "assume tab stops at each COLS instead of 8"), u = arg_lit0("u", NULL, "with -lt: sort by, and show, access time"), arg_rem (NULL, " with -l: show access time and sort by name"), arg_rem (NULL, " otherwise: sort by access time"), U = arg_lit0("U", NULL, "do not sort; list entries in directory order"), v = arg_lit0("v", NULL, "sort by version"), w = arg_int0("w", "width", "COLS", "assume screen width instead of current value"), x = arg_lit0("x", NULL, "list entries by lines instead of by columns"), X = arg_lit0("X", NULL, "sort alphabetically by entry extension"), one = arg_lit0("1", NULL, "list one file per line"), help = arg_lit0(NULL,"help", "display this help and exit"), version = arg_lit0(NULL,"version", "display version information and exit"), files = arg_filen(NULL, NULL, "FILE", 0, argc+2, NULL), end = arg_end(20), }; const char *progname = "ls"; int exitcode=0; int nerrors; /* verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable) != 0) { /* NULL entries were detected, some allocations must have failed */ printf("%s: insufficient memory\n",progname); exitcode=1; goto exit; } /* allow optional argument values for --color */ /* and set the default value to "always" */ color->hdr.flag |= ARG_HASOPTVALUE; color->sval[0] = "always"; /* Parse the command line as defined by argtable[] */ nerrors = arg_parse(argc,argv,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("Usage: %s", progname); arg_print_syntax(stdout,argtable,"\n"); printf("List information about the FILE(s) (the current directory by default).\n"); printf("Sort entries alphabetically if none of -cftuSUX nor --sort.\n\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); printf("\nSIZE may be (or may be an integer optionally followed by) one of following:\n" "kB 1000, K 1024, MB 1,000,000, M 1,048,576, and so on for G, T, P, E, Z, Y.\n\n" "By default, color is not used to distinguish types of files. That is\n" "equivalent to using --color=none. Using the --color option without the\n" "optional WHEN argument is equivalent to using --color=always. With\n" "--color=auto, color codes are output only if standard output is connected\n" "to a terminal (tty).\n\n" "Report bugs to <foo@bar>.\n"); exitcode=0; goto exit; } /* special case: '--version' takes precedence error reporting */ if (version->count > 0) { printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); printf("September 2003, Stewart Heitmann\n"); exitcode=0; goto exit; } /* If the parser returned any errors then display them and exit */ if (nerrors > 0) { /* Display the error details contained in the arg_end struct.*/ arg_print_errors(stdout,end,progname); printf("Try '%s --help' for more information.\n",progname); exitcode=1; goto exit; } /* Command line parsing is complete, do the main processing */ exitcode = mymain(); exit: /* deallocate each non-null entry in argtable[] */ arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return exitcode; }
int main(int argc, char* argv[]) { // Define our variables. int nerrors, i; int32_t saved = 0; // The number of words saved during compression and optimization. struct errinfo* errval; const char* prepend = "error: "; const char* warnprefix = "no-"; int msglen; char* msg; int target; // Define arguments. struct arg_lit* show_help = arg_lit0("h", "help", "Show this help."); struct arg_str* target_arg = arg_str0("l", "link-as", "target", "Link as the specified object, can be 'image', 'static' or 'kernel'."); struct arg_file* symbol_file = arg_file0("s", "symbols", "<file>", "Produce a combined symbol file (~triples memory usage!)."); struct arg_str* symbol_ext = arg_str0(NULL, "symbol-extension", "ext", "When -s is used, specifies the extension for symbol files. Defaults to \"dsym16\"."); struct arg_file* input_files = arg_filen(NULL, NULL, "<file>", 1, 100, "The input object files."); struct arg_file* output_file = arg_file1("o", "output", "<file>", "The output file (or - to send to standard output)."); struct arg_file* kernel_file = arg_file0("k", "kernel", "<file>", "Directly link in the specified kernel."); struct arg_file* jumplist_file = arg_file0("j", "jumplist", "<file>", "Link against the specified jumplist."); struct arg_str* warning_policies = arg_strn("W", NULL, "policy", 0, _WARN_COUNT * 2 + 10, "Modify warning policies."); struct arg_lit* keep_output_arg = arg_lit0(NULL, "keep-outputs", "Keep the .OUTPUT entries in the final static library (used for stdlib)."); struct arg_lit* little_endian_mode = arg_lit0(NULL, "little-endian", "Use little endian serialization (for compatibility with older versions)."); struct arg_lit* no_short_literals_arg = arg_lit0(NULL, "no-short-literals", "Do not compress literals to short literals."); struct arg_int* opt_level = arg_int0("O", NULL, "<level>", "The optimization level."); struct arg_lit* opt_mode = arg_lit0("S", NULL, "Favour runtime speed over size when optimizing."); struct arg_lit* verbose = arg_litn("v", NULL, 0, LEVEL_EVERYTHING - LEVEL_DEFAULT, "Increase verbosity."); struct arg_lit* quiet = arg_litn("q", NULL, 0, LEVEL_DEFAULT - LEVEL_SILENT, "Decrease verbosity."); struct arg_end* end = arg_end(20); void* argtable[] = { show_help, target_arg, keep_output_arg, little_endian_mode, opt_level, opt_mode, no_short_literals_arg, symbol_ext, symbol_file, kernel_file, jumplist_file, warning_policies, output_file, input_files, verbose, quiet, end }; // Parse arguments. nerrors = arg_parse(argc, argv, argtable); version_print(bautofree(bfromcstr("Linker"))); if (nerrors != 0 || show_help->count != 0) { if (show_help->count != 0) arg_print_errors(stdout, end, "linker"); printd(LEVEL_DEFAULT, "syntax:\n dtld"); arg_print_syntax(stderr, argtable, "\n"); printd(LEVEL_DEFAULT, "options:\n"); arg_print_glossary(stderr, argtable, " %-25s %s\n"); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Set verbosity level. debug_setlevel(LEVEL_DEFAULT + verbose->count - quiet->count); // Set global path variable. osutil_setarg0(bautofree(bfromcstr(argv[0]))); // Set endianness. isetmode(little_endian_mode->count == 0 ? IMODE_BIG : IMODE_LITTLE); // Set up warning policies. dsetwarnpolicy(warning_policies); // Set up error handling. if (dsethalt()) { errval = derrinfo(); // FIXME: Use bstrings here. msglen = strlen(derrstr[errval->errid]) + strlen(prepend) + 1; msg = malloc(msglen); memset(msg, '\0', msglen); strcat(msg, prepend); strcat(msg, derrstr[errval->errid]); printd(LEVEL_ERROR, msg, errval->errdata); // Handle the error. printd(LEVEL_ERROR, "linker: error occurred.\n"); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Check to make sure target is correct. if (target_arg->count == 0) target = IMAGE_APPLICATION; else { if (strcmp(target_arg->sval[0], "image") == 0) target = IMAGE_APPLICATION; else if (strcmp(target_arg->sval[0], "static") == 0) target = IMAGE_STATIC_LIBRARY; else if (strcmp(target_arg->sval[0], "kernel") == 0) target = IMAGE_KERNEL; else { // Invalid option. dhalt(ERR_INVALID_TARGET_NAME, NULL); } } // Load all passed objects and use linker bin system to // produce result. bins_init(); for (i = 0; i < input_files->count; i++) if (!bins_load(bautofree(bfromcstr(input_files->filename[i])), symbol_file->count > 0, (symbol_file->count > 0 && symbol_ext->count > 0) ? symbol_ext->sval[0] : "dsym16")) // Failed to load one of the input files. dhalt(ERR_BIN_LOAD_FAILED, input_files->filename[i]); bins_associate(); bins_sectionize(); bins_flatten(bautofree(bfromcstr("output"))); if (target == IMAGE_KERNEL) bins_write_jump(); saved = bins_optimize( opt_mode->count == 0 ? OPTIMIZE_SIZE : OPTIMIZE_SPEED, opt_level->count == 0 ? OPTIMIZE_NONE : opt_level->ival[0]); if (no_short_literals_arg->count == 0 && target != IMAGE_STATIC_LIBRARY) saved += bins_compress(); else if (no_short_literals_arg->count == 0) dwarn(WARN_SKIPPING_SHORT_LITERALS_TYPE, NULL); else dwarn(WARN_SKIPPING_SHORT_LITERALS_REQUEST, NULL); bins_resolve( target == IMAGE_STATIC_LIBRARY, target == IMAGE_STATIC_LIBRARY); bins_save( bautofree(bfromcstr("output")), bautofree(bfromcstr(output_file->filename[0])), target, keep_output_arg->count > 0, symbol_file->count > 0 ? symbol_file->filename[0] : NULL, jumplist_file->count > 0 ? jumplist_file->filename[0] : NULL); bins_free(); if (saved > 0) printd(LEVEL_DEFAULT, "linker: saved %i words during optimization.\n", saved); else if (saved < 0) printd(LEVEL_DEFAULT, "linker: increased by %i words during optimization.\n", -saved); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 0; }
ArgTableEntry_File::ArgTableEntry_File(std::string shortopts, std::string longopts, std::string glossary) { m_argFile = arg_filen(shortopts.c_str(), longopts.c_str(), "", 0, 1, glossary.c_str()); m_type = ArgType_t::FILE; }