int main(int argc, char *argv[]) { Corpus corp("db/corpus"); int total_terms = 0; setvbuf(stdin, (char *)malloc(1048576*16), _IOFBF, 1048576*16); setvbuf(stdout, (char *)malloc(1048576*16), _IOFBF, 1048576*16); for(;;) { static char buf[16*1048576]; const char *end; if(doc_id % 1000 == 0) fprintf(stderr, "processed articles: %d, total terms: %d\r", doc_id, total_terms); if(fgets(buf, sizeof(buf)-1, stdin) != buf) break; end = strchr(buf, '\n'); run_line(buf, end, corp); total_terms += term_pos; doc_id++; term_pos = 0; } fprintf(stderr, "\n"); return 0; }
void run(bool verbose) { while (!end_of_input()) { if (0 != run_line(verbose)) { puts("ignored"); } } trie_clear(); }
int run_script(const char script[], int fd, unsigned int flags){ scmd_t *cmd_list = NULL; int result = parse_script(script, &cmd_list, flags); if( result != 0 ){ return result; } sspace_t pspace, hspace; memset(&pspace, 0, sizeof(sspace_t)); memset(&hspace, 0, sizeof(sspace_t)); enum cmd_result code = RNONE; int line = 1, nflag = flags & NFLAG; TRY_REALLOC(outbuff, MIN_CHUNK); TRY_REALLOC(sub_buff, MIN_CHUNK); outbuff[0] = '\0'; sub_buff[0] = '\0'; while( (result = s_getline(&pspace, fd) > 0 ) ){ if( code != RNONE ){ result = print_space(&pspace); if( result < 0 ){ goto exit; } switch(code){ case RQUIT: return SUCCESS; default: if( code < 0 ){ result = code; goto exit; } break; } } pspace.is_deleted = nflag; code = run_line(cmd_list, &pspace, &hspace, line, flags); line++; } result = print_space(&pspace); exit: /* TODO: clean */ scmd_list_free(cmd_list); free(outbuff); free(sub_buff); if( pspace.space != NULL ){ free(pspace.space); } if( pspace.text != NULL ){ free(pspace.text); } return result; }
void* run() { while(1) { pthread_mutex_lock(&runningMutex); if(running->ready == 1) { printf("PC: %d\t CS: %d\t DS: %d\t ACC: %d\n", running->pc, running->cs, running->ds, running->acc); if(run_line() == PROG_END) { running->ready = 0; break; } running->pc++; } pthread_mutex_unlock(&runningMutex); } }
int main() { char* input; register_commands(); while (1) { input = readline("$> "); if ( ! input) break; if (strlen(input) > 0) { add_history(input); } List* cmd_line = parse_line(input); run_line(cmd_line); free(input); } return 0; }
int main(int argc, char** argv) { int c; char* sock_name; int sock_type; int s; struct binrpc_cmd cmd; struct id_list* sock_id; char* format; char* line; char* l; quit=0; format=0; line=0; s=-1; sock_name=0; sock_type=UNIXS_SOCK; opterr=0; while((c=getopt(argc, argv, "UVhs:D:R:vf:"))!=-1){ switch(c){ case 'V': printf("version: %s\n", version); printf("%s compiled on %s \n", __FILE__, compiled); exit(0); break; case 'h': printf("version: %s\n", version); printf("%s", help_msg); exit(0); break; case 's': sock_name=optarg; break; case 'R': reply_socket=optarg; break; case 'D': sock_dir=optarg; break; case 'U': sock_type=UDP_SOCK; break; case 'v': verbose++; break; case 'f': format=str_escape(optarg); if (format==0){ fprintf(stderr, "ERROR: memory allocation failure\n"); goto error; } break; case '?': if (isprint(optopt)) fprintf(stderr, "Unknown option `-%c'.\n", optopt); else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); goto error; case ':': fprintf(stderr, "Option `-%c' requires an argument.\n", optopt); goto error; default: abort(); } } if (sock_name==0){ sock_name=DEFAULT_CTL_SOCKET; } /* init the random number generator */ srand(getpid()+time(0)); /* we don't need very strong random numbers */ if (sock_name==0){ fprintf(stderr, "ERROR: no server socket address specified\n"); goto error; } sock_id=parse_listen_id(sock_name, strlen(sock_name), sock_type); if (sock_id==0){ fprintf(stderr, "ERROR: error parsing server socket address %s\n", sock_name); goto error; } switch(sock_id->proto){ case UDP_SOCK: case TCP_SOCK: if (sock_id->port==0){ sock_id->port=DEFAULT_CTL_PORT; /* fprintf(stderr, "ERROR: no port specified: %s:<port>\n", sock_name); goto error; */ } if ((s=connect_tcpudp_socket(sock_id->name, sock_id->port, (sock_id->proto==UDP_SOCK)?SOCK_DGRAM: SOCK_STREAM))<0){ goto error; } break; case UNIXS_SOCK: case UNIXD_SOCK: if ((s=connect_unix_sock(sock_id->name, (sock_id->proto==UNIXD_SOCK)?SOCK_DGRAM: SOCK_STREAM))<0) goto error; break; case UNKNOWN_SOCK: fprintf(stderr, "ERROR: Bad socket type for %s\n", sock_name); goto error; } free(sock_id); /* not needed anymore */ sock_id=0; if (optind>=argc){ /*fprintf(stderr, "ERROR: no command specified\n"); goto error; */ }else{ if (parse_cmd(&cmd, &argv[optind], argc-optind)<0) goto error; if (run_cmd(s, &cmd, format)<0) goto error; goto end; } /* interactive mode */ if (get_kamcmd_list(s)==0){ #ifdef USE_CFG_VARS get_cfgvars_list(s); #endif /* USE_CFG_VARS */ #ifdef USE_COUNTERS get_counters_list(s); #endif /* USE_COUNTERS */ } /* banners */ printf("%s %s\n", NAME, VERSION); printf("%s\n", COPYRIGHT); printf("%s\n", DISCLAIMER); #ifdef USE_READLINE /* initialize readline */ /* allow conditional parsing of the ~/.inputrc file*/ rl_readline_name=NAME; rl_completion_entry_function=kamcmd_generator; rl_attempted_completion_function=kamcmd_completion; while(!quit){ line=readline(NAME "> "); if (line==0) /* EOF */ break; l=trim_ws(line); /* trim whitespace */ if (*l){ add_history(l); run_line(s, l, format); } free(line); line=0; } #else line=malloc(MAX_LINE_SIZE); if (line==0){ fprintf(stderr, "memory allocation error\n"); goto error; } printf(NAME "> "); fflush(stdout); /* prompt */ while(!quit && fgets(line, MAX_LINE_SIZE, stdin)){ l=trim_ws(line); if (*l){ run_line(s, l, format); } printf(NAME "> "); fflush(stdout); /* prompt */ }; free(line); line=0; #endif /* USE_READLINE */ end: /* normal exit */ if (line) free(line); if (format) free(format); if (rpc_array) free_rpc_array(rpc_array, rpc_no); #ifdef USE_CFG_VARS if (cfg_grp_lst) free_cfg_grp_lst(); if (cfg_vars_array){ free_rpc_array(cfg_vars_array, cfg_vars_no); cfg_vars_array=0; cfg_vars_no=0; } #endif /* USE_CFG_VARS */ #ifdef USE_COUNTERS if (cnt_grp_lst) free_cnt_grp_lst(); if (cnt_grps_array){ free_rpc_array(cnt_grps_array, cnt_grps_no); cnt_grps_array=0; cnt_grps_no=0; } #endif /* USE_COUNTERS */ cleanup(); exit(0); error: if (line) free(line); if (format) free(format); if (rpc_array) free_rpc_array(rpc_array, rpc_no); #ifdef USE_CFG_VARS if (cfg_grp_lst) free_cfg_grp_lst(); if (cfg_vars_array){ free_rpc_array(cfg_vars_array, cfg_vars_no); cfg_vars_array=0; cfg_vars_no=0; } #endif /* USE_CFG_VARS */ #ifdef USE_COUNTERS if (cnt_grp_lst) free_cnt_grp_lst(); if (cnt_grps_array){ free_rpc_array(cnt_grps_array, cnt_grps_no); cnt_grps_array=0; cnt_grps_no=0; } #endif /* USE_COUNTERS */ cleanup(); exit(-1); }
int shell_step() { run_line(); return OK; }