int parse_opts(int argc, char * const *argv, char const **ttyfile, char const **conffile, int *verify, int *usertscts, struct sdp_work **cmd_head) { char c; *conffile = NULL; *ttyfile = NULL; static struct option long_options[] = { {"help", no_argument, 0, 'h' }, {"verify", no_argument, 0, 'v' }, {"no-rtscts", no_argument, 0, 'n' }, {0, 0, 0, 0 }, }; while ((c = getopt_long(argc, argv, "+hvn", long_options, NULL)) != -1) { switch (c) { case 'h': case '?': print_usage(); return -1; case 'n': *usertscts = 0; break; case 'v': *verify = 1; break; } } // Options parsed, get mandatory arguments... if (optind >= argc) { fprintf(stderr, "non optional argument UART is missing\n"); return -1; } *ttyfile = argv[optind]; optind++; if (optind >= argc) { fprintf(stderr, "non optional argument CONFIG is missing\n"); return -1; } *conffile = argv[optind]; optind++; if (optind < argc) { // Parse optional job arguments... *cmd_head = parse_cmd_args(argc - optind, &argv[optind]); } return 0; }
boost::optional<Config> create_config(int argc, char const* argv[]) { opts::variables_map vmap = parse_cmd_args(argc, argv); if (vmap.count("help")) { std::cout << create_parse_format() << std::endl; return boost::optional<Config>(); } Config config; config.image_path = vmap.count("image_path") ? vmap["image_path"].as<std::string>() : ""; return boost::optional<Config>(config); }
int parse_opts(int argc, char * const *argv, char const **configdir, int *verify, struct sdp_work **cmd_head) { int c; static struct option long_options[] = { {"help", no_argument, 0, 'h' }, {"debugmode", no_argument, 0, 'd' }, {"verify", no_argument, 0, 'v' }, {"configdir", required_argument, 0, 'c' }, {0, 0, 0, 0 }, }; while ((c = getopt_long(argc, argv, "+hdvc:", long_options, NULL)) != -1) { switch (c) { case 'h': case '?': print_usage(); return 1; case 'd': debugmode = 1; /* global extern */ break; case 'v': *verify = 1; break; case 'c': *configdir = optarg; break; } } if (optind < argc) { // Parse optional job arguments... *cmd_head = parse_cmd_args(argc - optind, &argv[optind]); } else { *cmd_head = NULL; } return 0; }
int main(int argc, char *argv[]) { pid_t pid; mpssenv_init(&mpssenv); parse_cmd_args(argc, argv); setsighandlers(); if (logfp != stderr) { if ((logfp = fopen(LOGFILE_NAME, "a+")) == NULL) { fprintf(stderr, "cannot open logfile '%s'\n", LOGFILE_NAME); exit(EBADF); } } mpsslog(PINFO, "MPSS Daemon start\n"); if ((miclist = mpss_get_miclist(&mpssenv, NULL)) == NULL) { mpsslog(PINFO, "MIC module not loaded\n"); exit(2); } if (logfp == stderr) { start_daemon(); } else { start_pid = getpid(); switch ((pid = fork())) { case -1: fprintf(stderr, "cannot fork: %s\n", strerror(errno)); exit(ENOEXEC); case 0: start_daemon(); default: pause(); } } exit(0); }
/* Initialise the game and return the state structure or NULL on failure: */ nbstate *init(int argc, char *argv[]) { GR_PROP *prop; nbstate *state; GR_SCREEN_INFO si; GR_BITMAP cursor = 0; GR_WM_PROPERTIES props; /* Try to connect to the Nano-X server: */ if(GrOpen() < 1) { fprintf(stderr, "Couldn't connect to Nano-X server\n"); return NULL; } /* Check that the server was built with alpha blending support * (necessary for the alpha blended sprites and special effects): */ GrGetScreenInfo(&si); if(!si.alphablend) { fprintf(stderr, "Error: Nano-X server was built without alpha " "blending support\nSet ALPHABLENDING = 1 in " "include/device.h, rebuild, and try again.\n"); return NULL; } /* Allocate the state structure and initialise it with defaults: */ if(!(state = malloc(sizeof(nbstate)))) return NULL; setup_default_state(state); /* Try to parse the command line arguments: */ if(parse_cmd_args(state, argc, argv)) { free(state); return NULL; } /* Try to load the game file: */ if(load_game_file(state)) { free(state); return NULL; } /* Load the high score file: */ load_hiscore(state); /* Calculate the canvas size: */ state->canvaswidth = state->width * state->brickwidth; state->canvasheight = state->scores.h + state->ball.s->h + (2 * BALLS_BORDER) + (state->height * state->brickheight) + 3 * state->brickheight + state->batheight; /* Place the bat in the centre of the window: */ state->batx = state->canvaswidth / 2; /* Create various pixmaps and alpha channels: */ state->scores.p = GrNewPixmap(state->canvaswidth, state->scores.h, NULL); state->scores.a = GrNewAlpha(state->canvaswidth, state->scores.h); state->canvas = GrNewPixmap(state->canvaswidth, state->canvasheight, NULL); /* The new and old canvasses are only used when screen fading is * enabled: */ if(state->faderate) { state->newcanvas = GrNewPixmap(state->canvaswidth, state->canvasheight, NULL); state->oldcanvas = GrNewPixmap(state->canvaswidth, state->canvasheight, NULL); } state->brickalpha = GrNewAlpha(state->brickwidth, state->brickheight); /* Start off with the canvas completely black: */ GrSetGCForeground(state->gc, GR_COLOR_BLACK); GrFillRect(state->canvas, state->gc, 0, 0, state->canvaswidth, state->canvasheight); /* If there is a window manager running, place the window off the * screen and let the window manager move it where it wants (avoids * flicker if we were to draw the window on screen before the window * manager moved it somewhere else): */ if(GrGetWindowProperty(GR_ROOT_WINDOW_ID, "WINDOW_MANAGER", &prop)) { free(prop); state->winx = GR_OFF_SCREEN; } /* Create the output window: */ state->wid = GrNewWindow(GR_ROOT_WINDOW_ID, state->winx, 0, state->canvaswidth, state->canvasheight, 0, 0, 0); /* Set the window title: */ props.flags = GR_WM_FLAGS_TITLE; props.title = "NanoBreaker"; GrSetWMProperties(state->wid, &props); /* Make the cursor over the output window be invisible: */ GrSetCursor(state->wid, 1, 1, 1, 1, 0, 0, &cursor, &cursor); /* Select the events we want to receive for the output window: */ GrSelectEvents(state->wid, GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_KEY_UP | GR_EVENT_MASK_UPDATE | GR_EVENT_MASK_TIMER); /* Select for mouse position events on the root window so we can move * the bat even when the pointer strays outside the window (which it * often does because the cursor is invisible you don't know exactly * where it is): */ GrSelectEvents(GR_ROOT_WINDOW_ID, GR_EVENT_MASK_MOUSE_POSITION); /* Map the output window (make it visible): */ GrMapWindow(state->wid); /* Create the one second periodic timer that is used to decrement the * power-up and power-down timers: */ state->tid = GrCreateTimer(state->wid, 1000, GR_TRUE); /* Reset the game: */ reset_game(state); return state; /* Return the newly allocated state structure. */ }
static void add_cmd(const char *cmdstr) { sed_cmd_t *sed_cmd; unsigned len, n; /* Append this line to any unfinished line from last time. */ if (G.add_cmd_line) { char *tp = xasprintf("%s\n%s", G.add_cmd_line, cmdstr); free(G.add_cmd_line); cmdstr = G.add_cmd_line = tp; } /* If this line ends with unescaped backslash, request next line. */ n = len = strlen(cmdstr); while (n && cmdstr[n-1] == '\\') n--; if ((len - n) & 1) { /* if odd number of trailing backslashes */ if (!G.add_cmd_line) G.add_cmd_line = xstrdup(cmdstr); G.add_cmd_line[len-1] = '\0'; return; } /* Loop parsing all commands in this line. */ while (*cmdstr) { /* Skip leading whitespace and semicolons */ cmdstr += strspn(cmdstr, semicolon_whitespace); /* If no more commands, exit. */ if (!*cmdstr) break; /* if this is a comment, jump past it and keep going */ if (*cmdstr == '#') { /* "#n" is the same as using -n on the command line */ if (cmdstr[1] == 'n') G.be_quiet++; cmdstr = strpbrk(cmdstr, "\n\r"); if (!cmdstr) break; continue; } /* parse the command * format is: [addr][,addr][!]cmd * |----||-----||-| * part1 part2 part3 */ sed_cmd = xzalloc(sizeof(sed_cmd_t)); /* first part (if present) is an address: either a '$', a number or a /regex/ */ cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match); sed_cmd->beg_line_orig = sed_cmd->beg_line; /* second part (if present) will begin with a comma */ if (*cmdstr == ',') { int idx; cmdstr++; if (*cmdstr == '+' && isdigit(cmdstr[1])) { /* http://sed.sourceforge.net/sedfaq3.html#s3.3 * Under GNU sed 3.02+, ssed, and sed15+, <address2> * may also be a notation of the form +num, * indicating the next num lines after <address1> is * matched. * GNU sed 4.2.1 accepts even "+" (meaning "+0"). * We don't (we check for isdigit, see above), think * about the "+-3" case. */ char *end; /* code is smaller compared to using &cmdstr here: */ idx = strtol(cmdstr+1, &end, 10); sed_cmd->end_line = -2 - idx; cmdstr = end; } else { idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match); cmdstr += idx; idx--; /* if 0, trigger error check below */ } if (idx < 0) bb_error_msg_and_die("no address after comma"); sed_cmd->end_line_orig = sed_cmd->end_line; } /* skip whitespace before the command */ cmdstr = skip_whitespace(cmdstr); /* Check for inversion flag */ if (*cmdstr == '!') { sed_cmd->invert = 1; cmdstr++; /* skip whitespace before the command */ cmdstr = skip_whitespace(cmdstr); } /* last part (mandatory) will be a command */ if (!*cmdstr) bb_error_msg_and_die("missing command"); sed_cmd->cmd = *cmdstr++; cmdstr = parse_cmd_args(sed_cmd, cmdstr); /* cmdstr now points past args. * GNU sed requires a separator, if there are more commands, * else it complains "char N: extra characters after command". * Example: "sed 'p;d'". We also allow "sed 'pd'". */ /* Add the command to the command array */ *G.sed_cmd_tail = sed_cmd; G.sed_cmd_tail = &sed_cmd->next; } /* If we glued multiple lines together, free the memory. */ free(G.add_cmd_line); G.add_cmd_line = NULL; }
int main(int argc,char *argv[]) { int i,filecount; static K2PDFOPT_SETTINGS _k2settings; K2PDFOPT_SETTINGS *k2settings; static STRBUF _cmdline,_env,_usermenu; STRBUF *cmdline,*env,*usermenu; k2settings=&_k2settings; cmdline=&_cmdline; env=&_env; usermenu=&_usermenu; strbuf_init(cmdline); strbuf_init(env); strbuf_init(usermenu); strbuf_cpy(env,getenv("K2PDFOPT")); for (i=1;i<argc;i++) strbuf_cat_with_quotes(cmdline,argv[i]); k2sys_init(); k2pdfopt_settings_init(k2settings); /* Only set ansi and user interface */ filecount=parse_cmd_args(k2settings,env,cmdline,usermenu,2,0); if (k2settings->show_usage) { k2sys_header(); if (k2settings->query_user==0 #if (defined(WIN32) || defined(WIN64)) || !win_has_own_window() #endif ) k2usage_show_all(stdout); else { if (!k2pdfopt_usage()) { k2sys_close(k2settings); strbuf_free(usermenu); strbuf_free(env); strbuf_free(cmdline); return(0); } } if (k2settings->query_user!=0) k2sys_enter_to_exit(k2settings); k2sys_close(k2settings); strbuf_free(usermenu); strbuf_free(env); strbuf_free(cmdline); return(0); } if (k2settings->query_user<0) #if (defined(WIN32) || defined(WIN64)) { if (win_has_own_window()) k2settings->query_user=1; else k2settings->query_user=(filecount==0); } #else k2settings->query_user=1; #endif #if (!defined(WIN32) && !defined(WIN64)) if (k2settings->query_user) { int tty_rows; tty_rows = get_ttyrows(); for (i=0;i<tty_rows-16;i++) aprintf("\n"); } #endif k2sys_header(); /* ** Set all options from command-line arguments */ parse_cmd_args(k2settings,env,cmdline,usermenu,1,0); /* ** Get user input */ if (k2pdfopt_menu(k2settings,filecount,env,cmdline,usermenu)==-1) { strbuf_free(usermenu); strbuf_free(env); strbuf_free(cmdline); k2sys_close(k2settings); return(0); } /* ** Re-init and then re-parse after all user menu entries applied. */ k2pdfopt_settings_init(k2settings); parse_cmd_args(k2settings,env,cmdline,usermenu,1,0); /* ** Sanity check / adjust user inputs */ k2pdfopt_settings_sanity_check(k2settings); /* ** Process files */ parse_cmd_args(k2settings,env,cmdline,usermenu,0,1); /* ** All done. */ strbuf_free(usermenu); strbuf_free(env); strbuf_free(cmdline); k2sys_enter_to_exit(k2settings); k2sys_close(k2settings); return(0); }
static void add_cmd(const char *cmdstr) { sed_cmd_t *sed_cmd; unsigned len, n; /* Append this line to any unfinished line from last time. */ if (G.add_cmd_line) { char *tp = xasprintf("%s\n%s", G.add_cmd_line, cmdstr); free(G.add_cmd_line); cmdstr = G.add_cmd_line = tp; } /* If this line ends with unescaped backslash, request next line. */ n = len = strlen(cmdstr); while (n && cmdstr[n-1] == '\\') n--; if ((len - n) & 1) { /* if odd number of trailing backslashes */ if (!G.add_cmd_line) G.add_cmd_line = xstrdup(cmdstr); G.add_cmd_line[len-1] = '\0'; return; } /* Loop parsing all commands in this line. */ while (*cmdstr) { /* Skip leading whitespace and semicolons */ cmdstr += strspn(cmdstr, semicolon_whitespace); /* If no more commands, exit. */ if (!*cmdstr) break; /* if this is a comment, jump past it and keep going */ if (*cmdstr == '#') { /* "#n" is the same as using -n on the command line */ if (cmdstr[1] == 'n') G.be_quiet++; cmdstr = strpbrk(cmdstr, "\n\r"); if (!cmdstr) break; continue; } /* parse the command * format is: [addr][,addr][!]cmd * |----||-----||-| * part1 part2 part3 */ sed_cmd = xzalloc(sizeof(sed_cmd_t)); /* first part (if present) is an address: either a '$', a number or a /regex/ */ cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match); sed_cmd->beg_line_orig = sed_cmd->beg_line; /* second part (if present) will begin with a comma */ if (*cmdstr == ',') { int idx; cmdstr++; idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match); if (!idx) bb_error_msg_and_die("no address after comma"); cmdstr += idx; } /* skip whitespace before the command */ cmdstr = skip_whitespace(cmdstr); /* Check for inversion flag */ if (*cmdstr == '!') { sed_cmd->invert = 1; cmdstr++; /* skip whitespace before the command */ cmdstr = skip_whitespace(cmdstr); } /* last part (mandatory) will be a command */ if (!*cmdstr) bb_error_msg_and_die("missing command"); sed_cmd->cmd = *cmdstr++; cmdstr = parse_cmd_args(sed_cmd, cmdstr); /* Add the command to the command array */ *G.sed_cmd_tail = sed_cmd; G.sed_cmd_tail = &sed_cmd->next; } /* If we glued multiple lines together, free the memory. */ free(G.add_cmd_line); G.add_cmd_line = NULL; }
/* * Main program */ int main(int argc, char *argv[]) { int i = 0; int ret = 0; struct sockaddr_in client_address; int client_sockfd = 0; pthread_t threads[MAX_THREADS]; /* Parse commandline args */ params = malloc(sizeof(cmd_params)); ret = parse_cmd_args(&argc, argv); if (params->help) { display_help_page(); exit(0); } if (params->version) { display_version_info(); exit(0); } if (ret < 0) { if (ret == -2) logline(LOG_ERROR, "Error: Invalid port range specified (-p)"); if (ret == -6) logline(LOG_ERROR, "Error: Invalid log level option specified (-l)."); logline(LOG_ERROR, "Use the -h option if you need help."); exit(ret); } /* Set log level */ switch (params->loglevel) { case 1: set_loglevel(LOG_ERROR); break; case 2: set_loglevel(LOG_INFO); break; case 3: set_loglevel(LOG_DEBUG); break; default: set_loglevel(LOG_ERROR); } /* Setup signal handler */ signal(SIGINT, shutdown_server); signal(SIGTERM, shutdown_server); /* Show banner and stuff */ show_gnu_banner(); /* Startup the server listener */ if (startup_server() < 0) { logline(LOG_ERROR, "Error during server startup. Please consult debug log for details."); exit(-1); } /* Post ready message */ logline(LOG_INFO, "Server listening on %s, port %d", params->ip, params->port); switch (params->loglevel) { case LOG_ERROR: logline(LOG_INFO, "Log level set to ERROR"); break; case LOG_INFO: logline(LOG_INFO, "Log level set to INFO"); break; case LOG_DEBUG: logline(LOG_INFO, "Log level set to DEBUG"); break; default: logline(LOG_INFO, "Unknown log level specified"); break; } /* Handle connections */ while (1) { logline(LOG_INFO, "Waiting for incoming connection..."); /* Accept a client connection */ client_len = sizeof(client_address); client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, (socklen_t *)&client_len); if (client_sockfd > 0) { logline(LOG_INFO, "Server accepted new connection on socket id %d", client_sockfd); /* A connection between a client and the server has been established. * Now create a new thread and handover the client_sockfd */ pthread_mutex_lock(&curr_thread_count_mutex); if (curr_thread_count < MAX_THREADS) { /* Prepare client infos in handy structure */ client_info *ci = (client_info *)malloc(sizeof(client_info)); ci->sockfd = client_sockfd; ci->address = client_address; sprintf(ci->nickname, "anonymous_%d", client_sockfd); /* Add client info to linked list */ llist_insert(&list_start, ci); llist_show(&list_start); /* Pass client info and invoke new thread */ ret = pthread_create(&threads[curr_thread_count], NULL, (void *)&proc_client, (void *)&client_sockfd); /* only pass socket id ? */ if (ret == 0) { pthread_detach(threads[curr_thread_count]); curr_thread_count++; /* Notify server and clients */ logline(LOG_INFO, "User %s joined the chat.", ci->nickname); logline(LOG_DEBUG, "main(): Connections used: %d of %d", curr_thread_count, MAX_THREADS); } else { free(ci); close(client_sockfd); } } else { logline(LOG_ERROR, "Max. connections reached. Connection limit is %d. Connection dropped.", MAX_THREADS); close(client_sockfd); } pthread_mutex_unlock(&curr_thread_count_mutex); } else { /* Connection could not be established. Post error and exit. */ perror(strerror(errno)); exit(-3); } } free(params); return 0; }
int main(int argc, char **argv) { setlocale(LC_ALL, ""); // Comment-out on non-Posix systems clock_t time_start = clock(); time_t time_t_start; time(&time_t_start); argv_0_basename = basename(argv[0]); get_usage_string(usage, USAGE_LEN); // This is a big scary string, so build it elsewhere //printf("sizeof(cmd_args)=%zd\n", sizeof(cmd_args)); parse_cmd_args(argc, argv, usage, &cmd_args); if (cmd_args.class_algo == EXCHANGE || cmd_args.class_algo == EXCHANGE_BROWN) memusage += sizeof(float) * ENTROPY_TERMS_MAX; // We'll build the precomputed entropy terms after reporting memusage struct_model_metadata global_metadata; // The list of unique words should always include <s>, unknown word, and </s> map_update_count(&word_map, UNKNOWN_WORD, 0, 0); // Should always be first map_update_count(&word_map, "<s>", 0, 1); map_update_count(&word_map, "</s>", 0, 2); // Open input FILE *in_train_file = stdin; if (in_train_file_string) in_train_file = fopen(in_train_file_string, "r"); if (in_train_file == NULL) { fprintf(stderr, "%s: Error: Unable to open input file %s\n", argv_0_basename, in_train_file_string); fflush(stderr); exit(15); } // Process input sentences size_t input_memusage = 0; const struct_model_metadata input_model_metadata = process_input(cmd_args, in_train_file, &word_map, &initial_bigram_map, &input_memusage); memusage += input_memusage; fclose(in_train_file); clock_t time_input_processed = clock(); if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Corpus processed in %'.2f CPU secs. %'lu lines, %'u types, %'lu tokens, current memusage: %'.1fMB\n", argv_0_basename, (double)(time_input_processed - time_start)/CLOCKS_PER_SEC, input_model_metadata.line_count, input_model_metadata.type_count, input_model_metadata.token_count, (double)memusage / 1048576); fflush(stderr); global_metadata.token_count = input_model_metadata.token_count; global_metadata.type_count = map_count(&word_map); // Filter out infrequent words, reassign word_id's, and build a mapping from old word_id's to new word_id's sort_by_count(&word_map); word_id_t * restrict word_id_remap = calloc(sizeof(word_id_t), input_model_metadata.type_count); get_ids(&word_map, word_id_remap); word_id_t number_of_deleted_words = filter_infrequent_words(cmd_args, &global_metadata, &word_map, word_id_remap); // Get list of unique words char * * restrict word_list = (char **)malloc(sizeof(char*) * global_metadata.type_count); memusage += sizeof(char*) * global_metadata.type_count; reassign_word_ids(&word_map, word_list, word_id_remap); get_keys(&word_map, word_list); sort_by_id(&word_map); // Check or set number of classes if (cmd_args.num_classes >= global_metadata.type_count) { // User manually set number of classes is too low fprintf(stderr, "%s: Error: Number of classes (%u) is not less than vocabulary size (%u). Decrease the value of --classes\n", argv_0_basename, cmd_args.num_classes, global_metadata.type_count); fflush(stderr); exit(3); } else if (cmd_args.num_classes == 0) { // User did not manually set number of classes at all cmd_args.num_classes = (wclass_t) (sqrt(global_metadata.type_count) * 1.2); } // Build array of word_counts word_count_t * restrict word_counts = malloc(sizeof(word_count_t) * global_metadata.type_count); memusage += sizeof(word_count_t) * global_metadata.type_count; build_word_count_array(&word_map, word_list, word_counts, global_metadata.type_count); // Initialize clusters, and possibly read-in external class file wclass_t * restrict word2class = malloc(sizeof(wclass_t) * global_metadata.type_count); memusage += sizeof(wclass_t) * global_metadata.type_count; init_clusters(cmd_args, global_metadata.type_count, word2class, word_counts, word_list); if (initial_class_file != NULL) import_class_file(&word_map, word2class, initial_class_file, cmd_args.num_classes); // Overwrite subset of word mappings, from user-provided initial_class_file // Remap word_id's in initial_bigram_map remap_and_rev_bigram_map(&initial_bigram_map, &new_bigram_map, &new_bigram_map_rev, word_id_remap, map_find_id(&word_map, UNKNOWN_WORD, -1)); global_metadata.start_sent_id = map_find_id(&word_map, "<s>", -1);; // need this for tallying emission probs global_metadata.end_sent_id = map_find_id(&word_map, "</s>", -1);; // need this for tallying emission probs global_metadata.line_count = map_find_count(&word_map, "</s>"); // Used for calculating perplexity if (global_metadata.line_count == 0) { fprintf(stderr, "%s: Warning: Number of lines is 0. Include <s> and </s> in your ngram counts, or perplexity values will be unreliable.\n", argv_0_basename); fflush(stderr); } //printf("init_bigram_map hash_count=%u\n", HASH_COUNT(initial_bigram_map)); fflush(stdout); //printf("new_bigram_map hash_count=%u\n", HASH_COUNT(new_bigram_map)); fflush(stdout); free(word_id_remap); memusage -= sizeof(word_id_t) * input_model_metadata.type_count; delete_all(&word_map); // static delete_all_bigram(&initial_bigram_map); // static memusage -= input_memusage; // Initialize and set word bigram listing clock_t time_bigram_start = clock(); size_t bigram_memusage = 0; size_t bigram_rev_memusage = 0; struct_word_bigram_entry * restrict word_bigrams = NULL; struct_word_bigram_entry * restrict word_bigrams_rev = NULL; if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Word bigram listing ... ", argv_0_basename); fflush(stderr); #pragma omp parallel sections // Both bigram listing and reverse bigram listing can be done in parallel { #pragma omp section { //sort_bigrams(&new_bigram_map); // speeds things up later word_bigrams = calloc(global_metadata.type_count, sizeof(struct_word_bigram_entry)); memusage += sizeof(struct_word_bigram_entry) * global_metadata.type_count; bigram_memusage = set_bigram_counts(word_bigrams, new_bigram_map); // Copy entries in word_counts to struct_word_bigram_entry.headword_count since that struct entry is already loaded when clustering for (word_id_t word = 0; word < global_metadata.type_count; word++) word_bigrams[word].headword_count = word_counts[word]; } // Initialize and set *reverse* word bigram listing #pragma omp section { if (cmd_args.rev_alternate) { // Don't bother building this if it won't be used //sort_bigrams(&new_bigram_map_rev); // speeds things up later word_bigrams_rev = calloc(global_metadata.type_count, sizeof(struct_word_bigram_entry)); memusage += sizeof(struct_word_bigram_entry) * global_metadata.type_count; bigram_rev_memusage = set_bigram_counts(word_bigrams_rev, new_bigram_map_rev); // Copy entries in word_counts to struct_word_bigram_entry.headword_count since that struct entry is already loaded when clustering for (word_id_t word = 0; word < global_metadata.type_count; word++) word_bigrams_rev[word].headword_count = word_counts[word]; } } } delete_all_bigram(&new_bigram_map); delete_all_bigram(&new_bigram_map_rev); memusage += bigram_memusage + bigram_rev_memusage; clock_t time_bigram_end = clock(); if (cmd_args.verbose >= -1) fprintf(stderr, "in %'.2f CPU secs. Bigram memusage: %'.1f MB\n", (double)(time_bigram_end - time_bigram_start)/CLOCKS_PER_SEC, (bigram_memusage + bigram_rev_memusage)/(double)1048576); fflush(stderr); //print_word_bigrams(global_metadata, word_bigrams, word_list); // Build <v,c> counts, which consists of a word followed by a given class word_class_count_t * restrict word_class_counts = calloc(1 + cmd_args.num_classes * global_metadata.type_count , sizeof(word_class_count_t)); if (word_class_counts == NULL) { fprintf(stderr, "%s: Error: Unable to allocate enough memory for <v,c>. %'.1f MB needed. Maybe increase --min-count\n", argv_0_basename, ((cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t)) / (double)1048576 )); fflush(stderr); exit(13); } memusage += cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t); fprintf(stderr, "%s: Allocating %'.1f MB for word_class_counts: num_classes=%u x type_count=%u x sizeof(w-cl-count_t)=%zu\n", argv_0_basename, (double)(cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t)) / 1048576 , cmd_args.num_classes, global_metadata.type_count, sizeof(word_class_count_t)); fflush(stderr); build_word_class_counts(cmd_args, word_class_counts, word2class, word_bigrams, global_metadata.type_count/*, word_list*/); //print_word_class_counts(cmd_args, global_metadata, word_class_counts); // Build reverse: <c,v> counts: class followed by word. This and the normal one are both pretty fast, so no need to parallelize this word_class_count_t * restrict word_class_rev_counts = NULL; if (cmd_args.rev_alternate) { // Don't bother building this if it won't be used word_class_rev_counts = calloc(1 + cmd_args.num_classes * global_metadata.type_count , sizeof(word_class_count_t)); if (word_class_rev_counts == NULL) { fprintf(stderr, "%s: Warning: Unable to allocate enough memory for <v,c>. %'.1f MB needed. Falling back to --rev-alternate 0\n", argv_0_basename, ((cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t)) / (double)1048576 )); fflush(stderr); cmd_args.rev_alternate = 0; } else { memusage += cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t); fprintf(stderr, "%s: Allocating %'.1f MB for word_class_rev_counts: num_classes=%u x type_count=%u x sizeof(w-cl-count_t)=%zu\n", argv_0_basename, (double)(cmd_args.num_classes * global_metadata.type_count * sizeof(word_class_count_t)) / 1048576 , cmd_args.num_classes, global_metadata.type_count, sizeof(word_class_count_t)); fflush(stderr); build_word_class_counts(cmd_args, word_class_rev_counts, word2class, word_bigrams_rev, global_metadata.type_count/*, word_list*/); } } // Calculate memusage for count_arrays for (unsigned char i = 1; i <= cmd_args.max_array; i++) { memusage += 2 * (powi(cmd_args.num_classes, i) * sizeof(wclass_count_t)); //printf("11 memusage += %zu (now=%zu) count_arrays\n", 2 * (powi(cmd_args.num_classes, i) * sizeof(wclass_count_t)), memusage); fflush(stdout); } clock_t time_model_built = clock(); if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Finished loading %'lu tokens and %'u types (%'u filtered) from %'lu lines in %'.2f CPU secs\n", argv_0_basename, global_metadata.token_count, global_metadata.type_count, number_of_deleted_words, global_metadata.line_count, (double)(time_model_built - time_start)/CLOCKS_PER_SEC); fflush(stderr); if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Approximate memory usage at clustering: %'.1fMB\n", argv_0_basename, (double)memusage / 1048576); fflush(stderr); cluster(cmd_args, global_metadata, word_counts, word_list, word2class, word_bigrams, word_bigrams_rev, word_class_counts, word_class_rev_counts); // Now print the final word2class mapping if (cmd_args.verbose >= 0) { FILE *out_file = stdout; if (out_file_string) out_file = fopen(out_file_string, "w"); if (out_file == NULL) { fprintf(stderr, "%s: Error: Unable to open output file %s\n", argv_0_basename, out_file_string); fflush(stderr); exit(16); } if (cmd_args.class_algo == EXCHANGE && (!cmd_args.print_word_vectors)) { print_words_and_classes(out_file, global_metadata.type_count, word_list, word_counts, word2class, (int)cmd_args.class_offset, cmd_args.print_freqs); } else if (cmd_args.class_algo == EXCHANGE && cmd_args.print_word_vectors) { print_words_and_vectors(out_file, cmd_args, global_metadata, word_list, word2class, word_bigrams, word_bigrams_rev, word_class_counts, word_class_rev_counts); } fclose(out_file); } clock_t time_clustered = clock(); time_t time_t_end; time(&time_t_end); double time_secs_total = difftime(time_t_end, time_t_start); if (cmd_args.verbose >= -1) fprintf(stderr, "%s: Finished clustering in %'.2f CPU seconds. Total wall clock time was about %lim %lis\n", argv_0_basename, (double)(time_clustered - time_model_built)/CLOCKS_PER_SEC, (long)time_secs_total/60, ((long)time_secs_total % 60) ); free(word2class); free(word_bigrams); free(word_list); free(word_counts); exit(0); }
static void parse_argv(ChildProcs *child_procs, Opts *opts, int argc, char *argv[]) { child_procs->n_cmds = 1; char **args_end = argv + argc; for (char **i = argv + 1; i < args_end; ++i) { if (! strcmp(*i, SEP)) ++child_procs->n_cmds; } child_procs->cmds = calloc(child_procs->n_cmds, sizeof(Cmd)); *opts = (Opts) { .signal_everything = false }; parse_cmd_args(opts, child_procs->cmds, argv + 1, args_end); char **arg_it = child_procs->cmds->args; int cmd_idx = 0; for (; arg_it < args_end; ++arg_it) { if (! strcmp(*arg_it, SEP)) { *arg_it = 0; // replace with null to terminate when passed to execvp if (arg_it + 1 == args_end) { fprintf(stderr, "command must follow `---'\n"); exit(1); } parse_cmd_args(opts, child_procs->cmds + (++cmd_idx), arg_it + 1, args_end); Cmd *cmd = child_procs->cmds + cmd_idx; arg_it = cmd->args - 1; } } } int main(int argc, char *argv[]) { if (argc == 1) { fprintf(stderr, "please supply at least one command to run\n"); return 1; } ChildProcs child_procs; Opts opts; parse_argv(&child_procs, &opts, argc, argv); int n_watch_cmds = 0; for (int i = 0; i < child_procs.n_cmds; ++i) { if (child_procs.cmds[i].watch) ++n_watch_cmds; } // if -f hasn't been used then watch every command if (0 == n_watch_cmds) { for (int i = 0; i < child_procs.n_cmds; ++i) { if (! child_procs.cmds[i].configuring) { ++n_watch_cmds; child_procs.cmds[i].watch = true; } } } Cmd **watch_cmds = calloc(n_watch_cmds, sizeof(Cmd *)); { int watch_cmd_end = 0; for (int i = 0; i < child_procs.n_cmds; ++i) { if (child_procs.cmds[i].watch) watch_cmds[watch_cmd_end++] = child_procs.cmds + i; } } install_term_and_int_handlers(); int error_code; run_configure_cmds(child_procs.n_cmds, child_procs.cmds); if (running) { run_cmds(child_procs.n_cmds, child_procs.cmds); error_code = wait_for_requested_commands_to_exit(n_watch_cmds, watch_cmds); remove_term_and_int_handlers(); alarm(WAIT_FOR_PROC_DEATH_TIMEOUT); kill(opts.signal_everything ? -1 : 0, SIGTERM); wait_for_all_processes_to_exit(error_code); DPRINTF("all processes exited cleanly"); } free(watch_cmds); free(child_procs.cmds); return error_code; }
int main (int argc, char *argv[]) { int res = 0; struct arguments opts = parse_cmd_args(argc, argv); if ((opts.flags & Optionvalue_ts::ERROR) != 0) { std::cerr << "There has been an error" << std::endl; return EXIT_FAILURE; } if (opts.flags == Optionvalue_ts::HELP) { print_help(); return EXIT_SUCCESS; } if (opts.filename.size() == 0) { std::cerr << "No filename provided" << std::endl; return EXIT_FAILURE; } // // Setup the driver Driver driver; global_driver = &driver; if ((opts.flags & Optionvalue_ts::PARSE_ONLY) != 0) { if (driver.parse(opts.filename) == nullptr) { std::cerr << "Error parsing file" << std::endl; res = EXIT_FAILURE; } else { res = EXIT_SUCCESS; } } else if ((opts.flags & Optionvalue_ts::DUMP_AST) != 0) { if (driver.parse(opts.filename) == nullptr) { std::cerr << "Error parsing file" << std::endl; return EXIT_FAILURE; } try { AstDumpVisitor dump_visitor; AstWalker<AstDumpVisitor, bool> dump_walker(dump_visitor); dump_walker.walk_specification(driver.result); std::cout << dump_visitor.get_dump(); res = EXIT_SUCCESS; } catch (char const *e) { std::cerr << "Abort after catching a string: "<< e; res = EXIT_FAILURE; } } else { if (driver.parse(opts.filename) == nullptr) { std::cerr << "Error parsing file " << driver.result << std::endl; return EXIT_FAILURE; } TypecheckVisitor typecheck_visitor(driver); AstWalker<TypecheckVisitor, Type*> typecheck_walker(typecheck_visitor); typecheck_walker.walk_specification(driver.result); if (!driver.ok()) { res = EXIT_FAILURE; } else { ExecutionContext ctx(driver.function_table, driver.get_init_rule(), (opts.flags & Optionvalue_ts::SYMBOLIC) != 0, (opts.flags & Optionvalue_ts::FILEOUT) != 0, (opts.flags & Optionvalue_ts::DUMP_UPDATES) != 0); if ((opts.flags & Optionvalue_ts::DEBUGINFO_FILTER) != 0) { ctx.set_debuginfo_filter(opts.debuginfo_filter); } ExecutionVisitor visitor(ctx, driver); ExecutionWalker walker(visitor); try { walker.run(); res = EXIT_SUCCESS; } catch (const RuntimeException& ex) { std::cerr << "Abort after runtime exception: "<< ex.what() << std::endl;; res = EXIT_FAILURE; } catch (const ImpossibleException& ex) { res = EXIT_SUCCESS; } catch (char * e) { std::cerr << "Abort after catching a string: "<< e << std::endl; res = EXIT_FAILURE; } } } if (driver.result) { delete driver.result; } return res; }
void add_cmd(char *cmdstr) { static char *add_cmd_line=NULL; sed_cmd_t *sed_cmd; int temp; /* Append this line to any unfinished line from last time. */ if(add_cmd_line) { int lastlen=strlen(add_cmd_line); char *tmp=xmalloc(lastlen+strlen(cmdstr)+2); memcpy(tmp,add_cmd_line,lastlen); tmp[lastlen]='\n'; strcpy(tmp+lastlen+1,cmdstr); free(add_cmd_line); cmdstr=add_cmd_line=tmp; } else add_cmd_line=NULL; /* If this line ends with backslash, request next line. */ temp=strlen(cmdstr); if(temp && cmdstr[temp-1]=='\\') { if(!add_cmd_line) add_cmd_line=strdup(cmdstr); add_cmd_line[temp-1]=0; return; } /* Loop parsing all commands in this line. */ while(*cmdstr) { /* Skip leading whitespace and semicolons */ cmdstr += strspn(cmdstr, semicolon_whitespace); /* If no more commands, exit. */ if(!*cmdstr) break; /* if this is a comment, jump past it and keep going */ if (*cmdstr == '#') { /* "#n" is the same as using -n on the command line */ if (cmdstr[1] == 'n') be_quiet++; if(!(cmdstr=strpbrk(cmdstr, "\n\r"))) break; continue; } /* parse the command * format is: [addr][,addr][!]cmd * |----||-----||-| * part1 part2 part3 */ sed_cmd = xcalloc(1, sizeof(sed_cmd_t)); /* first part (if present) is an address: either a '$', a number or a /regex/ */ cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match); /* second part (if present) will begin with a comma */ if (*cmdstr == ',') { int idx; cmdstr++; idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match); if (!idx) bb_error_msg_and_die("get_address: no address found in string\n"); cmdstr += idx; } /* skip whitespace before the command */ while (isspace(*cmdstr)) cmdstr++; /* Check for inversion flag */ if (*cmdstr == '!') { sed_cmd->invert = 1; cmdstr++; /* skip whitespace before the command */ while (isspace(*cmdstr)) cmdstr++; } /* last part (mandatory) will be a command */ if (!*cmdstr) bb_error_msg_and_die("missing command"); sed_cmd->cmd = *(cmdstr++); cmdstr = parse_cmd_args(sed_cmd, cmdstr); /* Add the command to the command array */ sed_cmd_tail->next = sed_cmd; sed_cmd_tail = sed_cmd_tail->next; } /* If we glued multiple lines together, free the memory. */ if(add_cmd_line) { free(add_cmd_line); add_cmd_line=NULL; } }
/* ** Conversion thread. This is launched as a separate thread to do the ** conversions while the dialog box displays the progress. */ static void k2gui_cbox_start_conversion(void *data) { void **ptrs; int i; K2PDFOPT_CONVERSION *k2conv; K2PDFOPT_SETTINGS *k2settings; K2PDFOPT_FILELIST_PROCESS k2listproc; static char *funcname="k2gui_cbox_start_conversion"; ptrs=(void **)data; k2conv=(K2PDFOPT_CONVERSION *)ptrs[0]; k2settings=&k2conv->k2settings; /* ** Count files */ k2gui_cbox_set_num_files(1); k2gui_cbox_set_files_completed(0,"Counting files..."); overwrite_set(1); for (i=k2listproc.filecount=0;i<k2conv->k2files.n;i++) { k2listproc.bmp=NULL; k2listproc.outname=NULL; k2listproc.mode=K2PDFOPT_FILELIST_PROCESS_MODE_GET_FILECOUNT; k2pdfopt_proc_wildarg(k2settings,k2conv->k2files.file[i],&k2listproc); willus_mem_free((double **)&k2listproc.outname,funcname); } if (k2listproc.filecount==0) { k2gui_cbox_set_files_completed(0,"No files"); k2gui_alertbox(0,"No files","No files can be opened for conversion."); k2gui_cbox_conversion_thread_cleanup(); return; } k2gui_cbox_set_num_files(k2listproc.filecount); k2gui_cbox_set_files_completed(0,NULL); /* ** Process files */ k2gui_cbox_set_error_count(0); k2gui_cbox_freelist(); overwrite_set(0); for (i=k2listproc.filecount=0;i<k2conv->k2files.n;i++) { char *buf; K2PDFOPT_CONVERSION *k2c; STRBUF *cmdline,_cmdline; willus_mem_alloc_warn((void **)&buf,1024,funcname,10); k2gui_cbox_set_num_pages(1); sprintf(buf,"Starting conversion of %s...",k2gui_short_name(k2conv->k2files.file[i])); k2gui_cbox_set_pages_completed(0,buf); willus_mem_alloc_warn((void **)&k2c,sizeof(K2PDFOPT_CONVERSION),funcname,10); k2pdfopt_conversion_init(k2c); cmdline=&_cmdline; strbuf_init(cmdline); k2gui_settings_to_cmdline(cmdline,&k2conv->k2settings); parse_cmd_args(k2c,k2gui->env,cmdline,&k2gui->cmdxtra,1,1); k2listproc.outname=NULL; k2listproc.bmp=NULL; k2listproc.mode=K2PDFOPT_FILELIST_PROCESS_MODE_CONVERT_FILES; k2pdfopt_proc_wildarg(&k2c->k2settings,k2conv->k2files.file[i],&k2listproc); #if (WILLUSDEBUGX & 0x2000) printf("\n\nDone conversion...\n\n"); #endif str0_addstring(&k2gui_cbox->filelist,&k2gui_cbox->filelist_na,k2listproc.outname); willus_mem_free((double **)&k2listproc.outname,funcname); k2pdfopt_conversion_close(k2c); willus_mem_free((double **)&k2c,funcname); willus_mem_free((double **)&buf,funcname); } k2gui_cbox_set_files_completed(k2listproc.filecount,NULL); if (k2listproc.filecount==k2conv->k2files.n && k2gui_cbox->error_count==0) k2gui_cbox->successful=1; k2gui_cbox_conversion_thread_cleanup(); }
int main(int argc, char **argv) { char errbuf[_POSIX2_LINE_MAX], dummy; size_t size; struct cmdentry *cmd = NULL; (void) setlocale(LC_ALL, ""); SLIST_INIT(&commands); argc--, argv++; if (argc > 0) { if (argv[0][0] == '-') { struct cmdtab *p; p = lookup(&argv[0][1]); if (p == (struct cmdtab *)-1) errx(1, "%s: ambiguous request", &argv[0][1]); if (p == (struct cmdtab *)0) errx(1, "%s: unknown request", &argv[0][1]); curcmd = p; argc--, argv++; } parse_cmd_args (argc, argv); } kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); if (kd != NULL) { /* * Try to actually read something, we may be in a jail, and * have /dev/null opened as /dev/mem. */ if (kvm_nlist(kd, namelist) != 0 || namelist[0].n_value == 0 || kvm_read(kd, namelist[0].n_value, &dummy, sizeof(dummy)) != sizeof(dummy)) { kvm_close(kd); kd = NULL; } } if (kd == NULL) { /* * Maybe we are lacking permissions? Retry, this time with bogus * devices. We can now use sysctl only. */ use_kvm = 0; kd = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, _PATH_DEVNULL, O_RDONLY, errbuf); if (kd == NULL) { error("%s", errbuf); exit(1); } } signal(SIGHUP, die); signal(SIGINT, die); signal(SIGQUIT, die); signal(SIGTERM, die); /* * Initialize display. Load average appears in a one line * window of its own. Current command's display appears in * an overlapping sub-window of stdscr configured by the display * routines to minimize update work by curses. */ initscr(); CMDLINE = LINES - 1; wnd = (*curcmd->c_open)(); if (wnd == NULL) { warnx("couldn't initialize display"); die(0); } wload = newwin(1, 0, 1, 20); if (wload == NULL) { warnx("couldn't set up load average window"); die(0); } gethostname(hostname, sizeof (hostname)); size = sizeof(clkinfo); if (sysctlbyname("kern.clockrate", &clkinfo, &size, NULL, 0) || size != sizeof(clkinfo)) { error("kern.clockrate"); die(0); } hertz = clkinfo.stathz; (*curcmd->c_init)(); curcmd->c_flags |= CF_INIT; labels(); if (curcmd->c_cmd != NULL) SLIST_FOREACH (cmd, &commands, link) if (!curcmd->c_cmd(cmd->cmd, cmd->argv)) warnx("command is not understood"); dellave = 0.0; display(); noecho(); crmode(); keyboard(); /*NOTREACHED*/ return EXIT_SUCCESS; }
int main (int argc, char **argv) { ///////////////////// // Parse Arguments // ///////////////////// params *pars = new params; init_pars(pars); parse_cmd_args(argc, argv, pars); if( pars->version ) { printf("ngsF v%s\nCompiled on %s @ %s", version, __DATE__, __TIME__); #ifdef _USE_BGZF printf(" (BGZF library)\n"); #else printf(" (STD library)\n"); #endif exit(0); } if( pars->verbose >= 1 ) { printf("==> Input Arguments:\n"); printf("\tglf file: %s\n\tinit_values: %s\n\tfreq_fixed: %s\n\tout file: %s\n\tn_ind: %d\n\tn_sites: %lu\n\tchunk_size: %lu\n\tfast_lkl: %s\n\tapprox_EM: %s\n\tcall_geno: %s\n\tmax_iters: %d\n\tmin_epsilon: %.10f\n\tn_threads: %d\n\tseed: %lu\n\tquick: %s\n\tversion: %s\n\tverbose: %d\n\n", pars->in_glf, pars->init_values, pars->freq_fixed ? "true":"false", pars->out_file, pars->n_ind, pars->n_sites, pars->max_chunk_size, pars->fast_lkl ? "true":"false", pars->approx_EM ? "true":"false", pars->call_geno ? "true":"false", pars->max_iters, pars->min_epsilon, pars->n_threads, pars->seed, pars->quick ? "true":"false", version, pars->verbose); } if( pars->verbose > 4 ) printf("==> Verbose values greater than 4 for debugging purpose only. Expect large amounts of info on screen\n"); ///////////////////// // Check Arguments // ///////////////////// if(pars->in_glf == NULL) error(__FUNCTION__,"GL input file (-glf) missing!"); else if( strcmp(pars->in_glf, "-") == 0 ) { pars->in_glf_type = new char[6]; pars->in_glf_type = strcat(pars->in_glf_type, "STDIN"); } else { pars->in_glf_type = strrchr(pars->in_glf, '.'); if(pars->in_glf_type == NULL) error(__FUNCTION__,"invalid file type!"); } if(pars->out_file == NULL) error(__FUNCTION__,"output file (-out) missing!"); if(pars->n_ind == 0) error(__FUNCTION__,"number of individuals (-n_ind) missing!"); if(pars->n_sites == 0) error(__FUNCTION__,"number of sites (-n_sites) missing!"); /////////////////////// // Check input files // /////////////////////// // Get file total size struct stat st; stat(pars->in_glf, &st); if( strcmp(pars->in_glf_type, "STDIN") != 0 ) { if( pars->n_sites == st.st_size/sizeof(double)/pars->n_ind/3 && strcmp(pars->in_glf_type, ".glf") == 0 ) { if(pars->verbose >= 1) printf("==> UNCOMP input file (\"%s\"): number of sites (%lu) match expected file size\n", pars->in_glf_type, pars->n_sites); } else if( strcmp(pars->in_glf_type, ".glf") != 0 ) { if( pars->verbose >= 1) printf("==> COMPRESSED input file (\"%s\"): number of sites (%lu) do NOT match expected file size\n", pars->in_glf_type, pars->n_sites); } else error(__FUNCTION__,"wrong number of sites or invalid/corrupt file!"); } // Adjust max_chunk_size in case of fewer sites if(pars->max_chunk_size > pars->n_sites) { if( pars->verbose >= 1 ) printf("==> Fewer sites (%lu) than chunk_size (%lu). Reducing chunk size to match number of sites\n", pars->n_sites, pars->max_chunk_size); pars->max_chunk_size = pars->n_sites; } // Calculate total number of chunks pars->n_chunks = ceil( (double) pars->n_sites/ (double) pars->max_chunk_size ); if( pars->verbose >= 1 ) printf("==> Analysis will be run in %ld chunk(s)\n", pars->n_chunks); // Alocate memory for the chunk index pars->chunks_voffset = new int64_t[pars->n_chunks]; memset(pars->chunks_voffset, 0, pars->n_chunks*sizeof(int64_t)); // Adjust thread number to chunks if(pars->n_chunks < pars->n_threads) { if( pars->verbose >= 1 ) printf("==> Fewer chunks (%ld) than threads (%d). Reducing the number of threads to match number of chunks\n", pars->n_chunks, pars->n_threads); pars->n_threads = pars->n_chunks; } // Open input file #ifdef _USE_BGZF if( pars->verbose >= 1 ) printf("==> Using BGZF I/O library\n"); // Open BGZIP file if( strcmp(pars->in_glf_type, ".bgz") == 0 ) { if( (pars->in_glf_fh = bgzf_open(pars->in_glf, "rb")) < 0 ) error(__FUNCTION__,"Cannot open BGZIP file!"); } else error(__FUNCTION__,"BGZF library only supports BGZIP files!"); bgzf_set_cache_size(pars->in_glf_fh, CACHE_SIZE * 1024uL * 1024uL * 1024uL); #else if( pars->verbose >= 1 ) printf("==> Using native I/O library\n"); // Open GLF file if( strcmp(pars->in_glf_type, "STDIN") == 0 ) pars->in_glf_fh = stdin; else if( strcmp(pars->in_glf_type, ".glf") == 0 ) { if( (pars->in_glf_fh = fopen(pars->in_glf, "rb")) == NULL ) error(__FUNCTION__,"Cannot open GLF file!"); } else error(__FUNCTION__,"Standard library only supports UNCOMPRESSED GLF files!"); // Allocate memory and read from the file pars->data = new double* [pars->n_sites]; for(uint64_t s = 0; s < pars->n_sites; s++) { pars->data[s] = new double[pars->n_ind * 3]; if( fread (pars->data[s], sizeof(double), pars->n_ind * 3, pars->in_glf_fh) != pars->n_ind * 3) error(__FUNCTION__,"cannot read GLF file!"); if(pars->call_geno) call_geno(pars->data[s], pars->n_ind, 3); } #endif if( pars->in_glf_fh == NULL ) error(__FUNCTION__,"cannot open GLF file!"); /////////////////////////////////// // Declare variables for results // /////////////////////////////////// out_data *output = new out_data; output->site_freq = new double[pars->n_sites]; output->site_freq_num = new double[pars->n_sites]; output->site_freq_den = new double[pars->n_sites]; output->site_prob_var = new double[pars->n_sites]; output->site_tmpprob_var = new double[pars->n_sites]; output->indF = new double[pars->n_ind]; output->indF_num = new double[pars->n_ind]; output->indF_den = new double[pars->n_ind]; output->ind_lkl = new double[pars->n_ind]; // Initialize output init_output(pars, output); ////////////////// // Analyze Data // ////////////////// if( pars->verbose >= 1 && !pars->fast_lkl && strcmp("e", pars->init_values) != 0 ) { printf("==> Initial LogLkl: %.15f\n", full_HWE_like(pars, output->site_freq, output->indF, 0, pars->n_ind)); fflush(stdout); } do_EM(pars, output); if( pars->verbose >= 1 ) printf("\nFinal logLkl: %f\n", output->global_lkl); ////////////////// // Print Output // ////////////////// FILE *out_file; if( pars->verbose >= 1 ) printf("Printing Output...\n"); out_file = fopen(pars->out_file, "w"); if(out_file == NULL) error(__FUNCTION__,"Cannot open OUTPUT file!"); for(uint16_t i = 0; i < pars->n_ind; i++) fprintf(out_file,"%f\n", output->indF[i]); fclose(out_file); ////////////////////// // Close Input File // ////////////////////// if( pars->verbose >= 1 ) printf("Exiting...\n"); #ifdef _USE_BGZF bgzf_close(pars->in_glf_fh); #else for(uint64_t s = 0; s < pars->n_sites; s++) delete [] pars->data[s]; delete [] pars->data; fclose(pars->in_glf_fh); #endif ///////////////// // Free Memory // ///////////////// delete [] output->site_freq; delete [] output->site_freq_num; delete [] output->site_freq_den; delete [] output->site_prob_var; delete [] output->indF; delete [] output->indF_num; delete [] output->indF_den; delete [] output->ind_lkl; delete output; //if( strcmp("e", pars->init_values) == 0 ) //delete [] pars->init_values; delete [] pars->chunks_voffset; delete pars; return 0; }
/* * App entry point. The place where everything starts... */ int main(int argc, char *argv[]) { int ret = 0; /* Initialize pseudo-random number generator */ srand((unsigned)time(NULL)); /* Allocate structures on heap */ params = malloc(sizeof(cmd_params)); /* Parse commandline args */ ret = parse_cmd_args(&argc, argv); if (params->help) { display_help_page(); return 0; } if (params->version) { display_version_info(); return 0; } if (ret < 0) { switch (ret) { case -1: logline(LOG_ERROR, "Error: No server specified (use option -s)"); break; case -2: logline(LOG_ERROR, "Error: Input file must be specified (use option -i)"); break; case -3: logline(LOG_ERROR, "Error: Servers could not be interpreted (use option -s)"); break; case -4: logline(LOG_ERROR, "Error: Invalid log level option specified (use option -l)."); break; case -5: logline(LOG_ERROR, "Error: No domain specified (use -d option)."); break; default: logline(LOG_ERROR, "Error: An unknown error occurred during parsing of command line args."); } logline(LOG_ERROR, "Use the -h option if you need help."); return ret; } /* Set log level */ switch (params->loglevel) { case 1: set_loglevel(LOG_ERROR); break; case 2: set_loglevel(LOG_INFO); break; case 3: set_loglevel(LOG_DEBUG); break; default: set_loglevel(LOG_ERROR); } show_gnu_banner(); printf("Executing %s Version %s\n", APP_NAME, APP_VERSION); printf("\n"); /* Display help page if desired. Otherwise, do DNS lookups */ ret = do_dns_lookups(); if (ret < 0) { logline(LOG_ERROR, "Error: Could not perform DNS lookups. Errorcode: %d", ret); } /* Free memory on heap */ free(params); return ret; }