struct arg_int* arg_int1(const char* shortopts, const char* longopts, const char *datatype, const char *glossary) { return arg_intn(shortopts,longopts,datatype,1,1,glossary); }
int main(int argc, char **argv) { struct arg_int *val = arg_intn(NULL,NULL,NULL,2,100,"must be an even number of non-zero integer values that sum to 100"); struct arg_end *end = arg_end(20); void* argtable[] = {val,end}; const char* progname = "callbacks"; int nerrors; int exitcode=0; int i; /* 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; } /* replace the default arg_int parsing and error validation routines with our own custom routines */ val->hdr.scanfn = (arg_scanfn*)myscanfn; val->hdr.checkfn = (arg_checkfn*)mycheckfn; val->hdr.errorfn = (arg_errorfn*)myerrorfn; /* special case: no command line options induces brief help */ if (argc==1) { printf("Usage: %s ", progname); arg_print_syntax(stdout,argtable,"\n"); arg_print_glossary(stdout,argtable,"where: %s %s\n"); exitcode=0; goto exit; } /* Parse the command line as defined by argtable[] */ nerrors = arg_parse(argc,argv,argtable); /* 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); exitcode=1; goto exit; } /* parsing was succesful, print the values obtained */ for (i=0; i<val->count; i++) printf("val->ival[%d] = %d\n",i, val->ival[i]); 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. FILE* load; uint16_t flash[0x10000]; char leading[0x100]; unsigned int i; bool uread = true; vm_t* vm; int nerrors; bstring ss, st; host_context_t* dtemu = malloc(sizeof(host_context_t)); const char* warnprefix = "no-"; // Define arguments. struct arg_lit* show_help = arg_lit0("h", "help", "Show this help."); struct arg_file* input_file = arg_file1(NULL, NULL, "<file>", "The input file, or - to read from standard input."); struct arg_file* execution_dump_file = arg_file0("e", "execution-dump", "<file>", "Produce a very large execution dump file."); struct arg_lit* debug_mode = arg_lit0("d", "debug", "Show each executed instruction."); struct arg_lit* terminate_mode = arg_lit0("t", "show-on-terminate", "Show state of machine when program is terminated."); struct arg_lit* headless_mode = arg_lit0("h", "headless", "Run machine witout displaying monitor and SPED output"); struct arg_lit* legacy_mode = arg_lit0("l", "legacy", "Automatically initialize hardware to legacy values."); struct arg_str* warning_policies = arg_strn("W", NULL, "policy", 0, _WARN_COUNT * 2 + 10, "Modify warning policies."); struct arg_lit* little_endian_mode = arg_lit0(NULL, "little-endian", "Use little endian serialization (for compatibility with older versions)."); 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_int* radiation = arg_intn("r", NULL, "<n>", 0, 1, "Radiation factor (higher is less radiation)"); struct arg_lit* catch_fire = arg_lit0("c", "catch-fire", "The virtual machine should catch fire instead of halting."); struct arg_end* end = arg_end(20); void* argtable[] = { input_file, warning_policies, debug_mode, execution_dump_file, terminate_mode, headless_mode, legacy_mode, little_endian_mode, radiation, catch_fire, verbose, quiet, 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, "emulator"); printd(LEVEL_DEFAULT, "syntax:\n dtemu"); 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); // Show version information. version_print(bautofree(bfromcstr("Emulator"))); // 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()) { // Handle the error. dautohandle(); printd(LEVEL_ERROR, "emulator: error occurred.\n"); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); return 1; } // Zero out the flash space. for (i = 0; i < 0x10000; i++) flash[i] = 0x0; // Zero out the leading space. for (i = 0; i < 0x100; i++) leading[i] = 0x0; // Load from either file or stdin. if (strcmp(input_file->filename[0], "-") != 0) { // Open file. load = fopen(input_file->filename[0], "rb"); if (load == NULL) dhalt(ERR_EMU_LOAD_FILE_FAILED, input_file->filename[0]); } else { // Windows needs stdin in binary mode. #ifdef _WIN32 _setmode(_fileno(stdin), _O_BINARY); #endif // Set load to stdin. load = stdin; } // Read leading component. for (i = 0; i < strlen(ldata_objfmt); i++) leading[i] = fgetc(load); fseek(load, 0, SEEK_SET); // Read up to 0x10000 words. for (i = 0; i < 0x10000 && !feof(load); i++) iread(&flash[i], load); fclose(load); // Check to see if the first X bytes matches the header // for intermediate code and stop if it does. ss = bfromcstr(""); st = bfromcstr(ldata_objfmt); for (i = 0; i < strlen(ldata_objfmt); i++) bconchar(ss, leading[i]); if (biseq(ss, st)) dhalt(ERR_INTERMEDIATE_EXECUTION, NULL); // Set up the host context. glfwInit(); dtemu->create_context = &dtemu_create_context; dtemu->activate_context = &dtemu_activate_context; dtemu->swap_buffers = &dtemu_swap_buffers; dtemu->destroy_context = &dtemu_destroy_context; dtemu->get_ud = &dtemu_get_ud; // And then use the VM. vm = vm_create(); vm->debug = (debug_mode->count > 0); vm_flash(vm, flash); // Set radiation and catch fire settings. if (radiation->count == 1) vm->radiation_factor = radiation->ival[0]; if (catch_fire->count == 1) vm->can_fire = true; // Init hardware. vm_hw_clock_init(vm); if (headless_mode->count < 1) vm->host = dtemu; vm_hw_sped3_init(vm); vm_hw_lem1802_init(vm); vm_hw_m35fd_init(vm); vm_hw_lua_init(vm); if (legacy_mode->count > 0) { for (i = 0; i < vm_hw_count(vm); i++) { hw_t* device = vm_hw_get_device(vm, i); if (device == NULL) continue; if (device->id == 0x7349F615 && device->manufacturer == 0x1C6C8B36) { vm_hw_lem1802_mem_set_screen((struct lem1802_hardware*)device->userdata, 0x8000); break; } } } vm_execute(vm, execution_dump_file->count > 0 ? execution_dump_file->filename[0] : NULL); if (terminate_mode->count > 0) { fprintf(stderr, "\n"); fprintf(stderr, "A: 0x%04X [A]: 0x%04X\n", vm->registers[REG_A], vm->ram[vm->registers[REG_A]]); fprintf(stderr, "B: 0x%04X [B]: 0x%04X\n", vm->registers[REG_B], vm->ram[vm->registers[REG_B]]); fprintf(stderr, "C: 0x%04X [C]: 0x%04X\n", vm->registers[REG_C], vm->ram[vm->registers[REG_C]]); fprintf(stderr, "X: 0x%04X [X]: 0x%04X\n", vm->registers[REG_X], vm->ram[vm->registers[REG_X]]); fprintf(stderr, "Y: 0x%04X [Y]: 0x%04X\n", vm->registers[REG_Y], vm->ram[vm->registers[REG_Y]]); fprintf(stderr, "Z: 0x%04X [Z]: 0x%04X\n", vm->registers[REG_Z], vm->ram[vm->registers[REG_Z]]); fprintf(stderr, "I: 0x%04X [I]: 0x%04X\n", vm->registers[REG_I], vm->ram[vm->registers[REG_I]]); fprintf(stderr, "J: 0x%04X [J]: 0x%04X\n", vm->registers[REG_J], vm->ram[vm->registers[REG_J]]); fprintf(stderr, "PC: 0x%04X SP: 0x%04X\n", vm->pc, vm->sp); fprintf(stderr, "EX: 0x%04X IA: 0x%04X\n", vm->ex, vm->ia); } vm_hw_lua_free(vm); vm_free(vm); arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); glfwTerminate(); return 0; }
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) { int exitcode = 0; int nerrors = 0; /* Prepare command line arguments */ struct arg_str *latero_ip = arg_str1(NULL,"latero_ip","IP", "Latero server IP address"); struct arg_lit *print_resp= arg_lit0("p", "print", "print response packet"); struct arg_int *numpkt = arg_int0("n", "numpkt","<n>", "How many packets (default is 1)"); struct arg_int *dacval = arg_intn(NULL,"dac","<int>",0,4,"dac values (up to 4 values)"); struct arg_lit *rd = arg_lit0("r", "read", "read"); struct arg_lit *wr = arg_lit0("w", "write", "write"); struct arg_int *addr = arg_int0("a", "addr",NULL, "address"); struct arg_int *value = arg_int0("v", "value",NULL, "value"); struct arg_lit *mainctrl = arg_lit0(NULL,"mainctrl", "Raw commands are for the main controller"); struct arg_int *tpat = arg_int0("t","testpat","<n>", "Run Test Pattern:1=Split, 2=AllPin, 3=RowCol"); struct arg_lit *latio = arg_lit0(NULL,"lateroio", "Raw commands are for the Latero IO card"); struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); struct arg_end *end = arg_end(10); void* argtable[] = {latero_ip,print_resp,numpkt,dacval, rd, wr, addr, value, mainctrl, tpat, latio, help,end}; //latero_ip->sval[0] = "192.168.1.108"; /* verify the argtable[] entries were allocated sucessfully */ if (arg_nullcheck(argtable) != 0) { /* NULL entries were detected, some allocations must have failed */ printf("Insufficient memory (argtable)\n"); exitcode = 1; goto exit; } numpkt->ival[0] = 1; tpat->ival[0] = 0; nerrors = arg_parse(argc,argv,argtable); /* special case: '--help' takes precedence over error reporting */ if (help->count > 0) { printf("Usage: %s", argv[0]); arg_print_syntax(stdout,argtable,"\n"); printf("Latero client demonstration program version 1, revision 1\n"); arg_print_glossary(stdout,argtable," %-25s %s\n"); exitcode=0; goto 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 = 0; goto exit; } if( wr->count > 0 ) { if( addr->count != 1 || value->count != 1 ) { printf("Write requires an address and a value\n"); exitcode = 1; goto exit; } if( mainctrl->count + latio->count == 0 ) { printf("Write requires a destination (--mainctrl or --lateroio)\n"); exitcode = 1; goto exit; } } if( rd->count > 0 ) { if( addr->count != 1 ) { printf("Read requires an address\n"); exitcode = 1; goto exit; } if( mainctrl->count + latio->count == 0 ) { printf("Read requires a destination (--mainctrl or --lateroio)\n"); exitcode = 1; goto exit; } } return( my_main( latero_ip->sval[0], print_resp->count, numpkt->ival[0], dacval->ival, dacval->count, rd->count, wr->count, addr->ival[0], value->ival[0], mainctrl->count, latio->count, tpat->ival[0]) ); exit: printf("Program Ended\n"); arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return(exitcode); }
ArgTableEntry_Int::ArgTableEntry_Int(std::string shortopts, std::string longopts, std::string glossary) { m_argInt = arg_intn(shortopts.c_str(), longopts.c_str(), "", 0, 1, glossary.c_str()); m_type = ArgType_t::INT; }
int main( int const argc, char **argv) { struct arg_int *arg_init_num_nodes = arg_intn("n", NULL, "<n>", 0, 1, "Initial number of nodes (n >= 1)."); if (arg_init_num_nodes != NULL) { arg_init_num_nodes->ival[0] = 1; } struct arg_str *arg_init_link_prob = arg_strn("p", NULL, "<prob>", 0, 1, "Initial link probability (0 <= p <= 1)."); if (arg_init_link_prob != NULL) { arg_init_link_prob->sval[0] = "0.2"; } struct arg_int *arg_num_links = arg_intn("m", NULL, "<n>", 0, 1, "Number of links to each new node (m <= n)."); struct arg_int *arg_time_steps = arg_intn("T", NULL, "<n>", 0, 1, "The number of time steps (T >= 0)."); if (arg_time_steps != NULL) { arg_time_steps->ival[0] = 1000; } struct arg_str *arg_dot_path = arg_strn("d", NULL, "<path>", 0, 1, "Path to write a dot file to."); struct arg_int *arg_seed = arg_intn("s", NULL, "<seed>", 0, 1, "Random seed."); struct arg_int *arg_num_samples = arg_intn("k", NULL, "<samples>", 0, 1, "Number of samples to use for the clustering coefficient approx."); if (arg_num_samples != NULL) { arg_num_samples->ival[0] = 50000; } struct arg_str *arg_anim_path = arg_strn("a", NULL, "<path>", 0, 1, "Path to write the animation scipt (stdout default)."); struct arg_str *arg_edges_path = arg_strn("e", NULL, "<path>", 0, 1, "Path to read an edges file from."); struct arg_lit *arg_real = arg_lit0("R", NULL, "Run only deterministic algorithm."); struct arg_lit *arg_approx = arg_lit0("A", NULL, "Run only heuristic algorithm."); struct arg_end *end = arg_end(10); void *arg_table[] = { arg_init_num_nodes, arg_init_link_prob, arg_num_links, arg_time_steps, arg_dot_path, arg_seed, arg_num_samples, arg_anim_path, arg_edges_path, arg_real, arg_approx, end }; int exit_code = EXIT_SUCCESS; sfn_t *sfn = NULL; if (arg_nullcheck(arg_table) != 0) { fprintf(stderr, "[ERROR] Insufficient memory for argtable.\n"); exit_code = EXIT_FAILURE; goto exit; } int const num_errors = arg_parse(argc, argv, arg_table); if (num_errors > 0) { arg_print_errors(stderr, end, SFN_PROG_NAME); exit_code = EXIT_FAILURE; goto exit; } int const init_num_nodes = arg_init_num_nodes->ival[0]; double const init_link_prob = strtod(arg_init_link_prob->sval[0], NULL); int num_links; if (arg_num_links->count == 0) { num_links = init_num_nodes; } else { num_links = arg_num_links->ival[0]; } int const time_steps = arg_time_steps->ival[0]; int const num_samples = arg_num_samples->ival[0]; if (init_num_nodes < 1 || time_steps <= 0 || num_links > init_num_nodes || init_link_prob > 1 || init_link_prob <= 0 || num_links <= 0) { fprintf(stderr, "Invalid arguments.\n"); exit_code = EXIT_FAILURE; goto exit; } if (arg_seed->count > 0) { fprintf(stderr, "Seeding PRNG with %d.\n", arg_seed->ival[0]); srand(arg_seed->ival[0]); } if (arg_anim_path->count > 0) { if ((anim = fopen(arg_anim_path->sval[0], "w")) == NULL) { fprintf(stderr, "[ERROR} Cannot open animation path."); exit_code = EXIT_FAILURE; goto exit; } } if (arg_edges_path->count > 0) { /* Read graph edges from a text file. */ FILE *edges; if ((edges = fopen(arg_edges_path->sval[0], "r")) == NULL) { fprintf(stderr, "[ERROR} Cannot open edges file path."); exit_code = EXIT_FAILURE; goto exit; } /* String buffer that contains the result of the bash command execution. */ char number_of_nodes[80]; /* Number of nodes to allocate the data structure for the scale free network with. */ int num_nodes = atoi(fgets(number_of_nodes, 80, edges)); fclose(edges); /* Allocate space for the scale free network data structure. */ if ((sfn = sfn_alloc(num_nodes, 0, 0)) == NULL) { fprintf(stderr, "[ERROR] Insufficient memory for sfn.\n"); exit_code = EXIT_FAILURE; goto exit; } sfn->num_nodes = num_nodes; if (sfn_edges_init(sfn, arg_edges_path->sval[0]) != 0) { fprintf(stderr, "[ERROR] Failed at populating the network with edges.\n"); exit_code = EXIT_FAILURE; goto exit; } } else { if ((sfn = sfn_alloc(init_num_nodes, num_links, time_steps)) == NULL) { fprintf(stderr, "[ERROR] Insufficient memory for sfn.\n"); exit_code = EXIT_FAILURE; goto exit; } sfn_init(sfn, init_num_nodes, init_link_prob); sfn_ba(sfn, num_links, time_steps); } struct timeval before, after; if (! arg_real->count > 0) { gettimeofday(&before, NULL); double d1; double const approx_cc = approximate_clustering_coefficient(sfn, num_samples); gettimeofday(&after, NULL); d1 = (after.tv_sec - before.tv_sec) * 1000.0 + (after.tv_usec - before.tv_usec) / 1000.0; printf("ACC\t%.100f\n", approx_cc); printf("ACCT\t%.100f\n", d1); } if (! arg_approx->count > 0) { double real_cc, d2; gettimeofday(&before, NULL); if ((real_cc = calculate_clustering_coefficient(sfn)) < 0.0) { fprintf(stderr, "[ERROR] Insufficient memory for calc coeff.\n"); exit_code = EXIT_FAILURE; goto exit; } gettimeofday(&after, NULL); d2 = (after.tv_sec - before.tv_sec) * 1000.0 + (after.tv_usec - before.tv_usec) / 1000.0; printf("CC\t%.100f\n", real_cc); printf("CCT\t%.100f\n", d2); } if (arg_dot_path->count > 0) { fprintf(stderr, "Writing DOT file..."); fflush(stderr); if (!sfn_write_dot_file(sfn, arg_dot_path->sval[0])) { fprintf(stderr, "[ERROR] Failed to write dot file.\n"); exit_code = EXIT_FAILURE; goto exit; } fprintf(stderr, "done.\n"); } exit: if (sfn != NULL) { sfn_free(sfn); } if (anim != NULL) { fclose(anim); } arg_freetable(arg_table, sizeof(arg_table) / sizeof(arg_table[0])); return exit_code; }
int main(int argc, char **argv) { struct arg_int *a = arg_int1(NULL,NULL,"a","a is <int>"); struct arg_int *b = arg_int0(NULL,NULL,"b","b is <int>"); struct arg_int *c = arg_int0(NULL,NULL,"c","c is <int>"); struct arg_int *d = arg_intn("dD","delta","<int>",0,3,"d can occur 0..3 times"); struct arg_int *e = arg_int0(NULL,"eps,eqn","<int>","eps is optional"); struct arg_int *f = arg_intn("fF","filler","<int>",0,3,"f can occur 0..3 times"); struct arg_lit *help = arg_lit0(NULL,"help","print this help and exit"); struct arg_end *end = arg_end(20); void* argtable[] = {a,b,c,d,e,f,help,end}; int nerrors; int exitcode=0; int i; long sum=0; /* printf("a=%p\n",a); printf("b=%p\n",b); printf("c=%p\n",c); printf("d=%p\n",d); printf("e=%p\n",e); printf("f=%p\n",f); printf("help=%p\n",help); printf("end=%p\n",end); printf("argtable=%p\n",argtable); */ /* print the command line */ for (i=0; i<argc; i++) printf("%s ",argv[i]); printf("\n"); /* 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; } /* allow missing argument values for the f argument, and set defaults to -1 */ f->hdr.flag |= ARG_HASOPTVALUE; for (i=0; i<f->hdr.maxcount; i++) f->ival[i] = -1; /* 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 ", argv[0]); arg_print_syntax(stdout,argtable,"\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,argv[0]); exitcode=1; goto exit; } /* parsing complete, verify all args sum to zero */ for (i=0; i<a->count; i++) { printf("a[%d]=%d\n",i,a->ival[i]); sum += a->ival[i]; } for (i=0; i<b->count; i++) { printf("b[%d]=%d\n",i,b->ival[i]); sum += b->ival[i]; } for (i=0; i<c->count; i++) { printf("c[%d]=%d\n",i,c->ival[i]); sum += c->ival[i]; } for (i=0; i<d->count; i++) { printf("d[%d]=%d\n",i,d->ival[i]); sum += d->ival[i]; } for (i=0; i<e->count; i++) { printf("e[%d]=%d\n",i,e->ival[i]); sum += e->ival[i]; } for (i=0; i<f->count; i++) { printf("f[%d]=%d\n",i,f->ival[i]); sum += f->ival[i]; } printf("sum=%ld\n",sum); if (sum!=0) { printf("%s: error - sum=%ld is non-zero\n",argv[0],sum); exitcode=1; goto exit; } exit: /* deallocate each non-null entry in argtable[] */ arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); printf("%s: exitcode=%d\n\n",argv[0],exitcode); /* close stdin and stdout to stop memcheck whining about their memory not being freed */ fclose(stdin); fclose(stdout); return exitcode; }
int main(int argc, char **argv) { struct arg_int *foo = arg_intn("f","foo", NULL,1,3, "takes an integer value (defaults to 9)"); struct arg_int *bar = arg_intn("b","bar", NULL,1,3, "takes an optional integer value (defaults to 5)"); 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_end *end = arg_end(20); void* argtable[] = {foo,bar,help,version,end}; const char* progname = "hasoptvalue"; int nerrors; int exitcode=0; int i; /* 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 foo default values to 9 */ for (i=0; i<foo->hdr.maxcount; i++) foo->ival[i]=9; /* allow bar to have optional values */ /* and set bar default values to 5 */ bar->hdr.flag |= ARG_HASOPTVALUE; for (i=0; i<bar->hdr.maxcount; i++) bar->ival[i]=5; /* 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"); 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 2005, 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; } /* command line arguments are successfully parsed at this point. */ /* print what we have parsed */ printf("%d instances of --foo detected on command line\n", foo->count); for (i=0; i<foo->hdr.maxcount; i++) printf("foo[%d] = %d\n", i, foo->ival[i]); printf("%d instances of --bar detected on command line\n", bar->count); for (i=0; i<bar->hdr.maxcount; i++) printf("bar[%d] = %d\n", i, bar->ival[i]); exit: /* deallocate each non-null entry in argtable[] */ arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); return exitcode; }