int main(int argc, char **argv) { option_define_bool("version", OPT_OPTIONAL, 0, NULL, version_cb, VERSION); option_define_bool("verbose", OPT_OPTIONAL, 0, &verbose, NULL, "verbose output (to stderr)"); option_define_str("blacklist_fields", OPT_OPTIONAL, NULL, NULL, parse_blacklisted_fields, "comma separated list of fields to remove"); option_define_str("encrypted_fields", OPT_OPTIONAL, NULL, NULL, parse_encrypted_fields, "comma separated list of fields to encrypt"); option_define_str("expected_key", OPT_OPTIONAL, NULL, &expected_key, NULL, "key to expect in messages before echoing to clients"); option_define_str("expected_value", OPT_OPTIONAL, NULL, &expected_value, NULL, "value to expect in --expected-key field in messages before echoing to clients"); if (!option_parse_command_line(argc, argv)) { return 1; } if ( !!expected_key ^ !!expected_value ) { fprintf(stderr, "--expected-key and --expected-value must be used together\n"); exit(1); } while (fgets(buf, BUF_SZ, stdin)) { msgRecv++; process_message(buf); } fprintf(stderr, "processed %lu lines, failed to parse %lu of them\n", msgRecv, msgFail); free_options(); free_fields(blacklisted_fields, num_blacklisted_fields); free_fields(encrypted_fields, num_encrypted_fields); return 0; }
static void nexttopic( char *word ) { a_hstackent *h; unsigned len; if( word == NULL ) { len = 0; } else { len = strlen( word ); } h = HelpMemAlloc( sizeof( a_hstackent ) + len ); if( helpStack != NULL ) { helpStack->cur = field_count( helpTab, helpCur ); helpStack->line = currLine; } h->next = helpStack; h->type = HSTCK_NAME; h->cur = 0; h->line = 0; strcpy( h->helpfname, curFile ); if( word != NULL ) { strcpy( h->word, word ); } h->word[len] = '\0'; helpStack = h; free_fields( &helpTab ); }
void rlib_free_output(rlib *r, struct rlib_element *e) { struct rlib_report_output_array *roa; struct rlib_element *next; for (; e != NULL; next = e->next, g_free(e), e = next) { roa = e->data; free_fields(r, roa); } }
static void prevtopic( void ) { a_hstackent *h; if( helpStack->next != NULL ) { h = helpStack; helpStack = helpStack->next; HelpMemFree( h ); } free_fields( &helpTab ); }
static int cgc_set_field(interp_t *interp, unsigned int num, const char *value) { char **fields, *copy; if (num > MAX_FIELDS) return 0; if (interp->fields == NULL && num > 0) { if (!cgc_read_fields(interp)) return 0; } if (num > interp->num_fields) { fields = cgc_realloc(interp->fields, num * sizeof(char *)); if (fields == NULL) return 0; interp->fields = fields; cgc_memset(&interp->fields[interp->num_fields], 0, sizeof(char *) * (num - interp->num_fields)); interp->num_fields = num; } if (num == 0) { if (cgc_strlen(value) >= BUF_SIZE - 1) return 0; cgc_strcpy(interp->buf, value); free_fields(interp); interp->field0 = interp->buf; } else { copy = cgc_strdup(value); if (copy == NULL) return 0; interp->fields[num-1] = copy; interp->field0 = NULL; } return 1; }
static bool make_tuple(compile_t* c, ast_t* ast, gentype_t* g) { // An anonymous structure with no functions and no vtable. if(setup_name(c, ast, g, false)) return true; setup_tuple_fields(g); dwarf_forward(&c->dwarf, g); bool ok = make_struct(c, g) && make_components(c, g); // Finalise debug symbols for tuple type. dwarf_composite(&c->dwarf, g); dwarf_finish(&c->dwarf); // Generate a descriptor. gendesc_init(c, g); free_fields(g); return ok; }
static bool make_nominal(compile_t* c, ast_t* ast, gentype_t* g, bool prelim) { assert(ast_id(ast) == TK_NOMINAL); ast_t* def = (ast_t*)ast_data(ast); token_id id = ast_id(def); // For traits, just return a raw object pointer. switch(id) { case TK_INTERFACE: case TK_TRAIT: g->underlying = id; g->use_type = c->object_ptr; dwarf_trait(&c->dwarf, g); return true; default: {} } // If we already exist or we're preliminary, we're done. if(setup_name(c, ast, g, prelim)) return true; if(g->primitive == NULL) { // Not a primitive type. Generate all the fields and a trace function. setup_type_fields(g); // Forward declare debug symbols for this nominal, if needed. // At this point, this can only be TK_STRUCT, TK_CLASS, TK_PRIMITIVE, or // TK_ACTOR ast nodes. TK_TYPE has been translated to any of the former // during reification. dwarf_forward(&c->dwarf, g); bool ok = make_struct(c, g); if(!g->done) ok = ok && make_trace(c, g) && make_components(c, g); if(!ok) { free_fields(g); return false; } // Finalise symbols for composite type. if(!g->done) dwarf_composite(&c->dwarf, g); } else { // Emit debug symbols for a basic type (U8, U16, U32...) dwarf_basic(&c->dwarf, g); // Create a box type. make_box_type(c, g); } if(!g->done) { // Generate a dispatch function if necessary. make_dispatch(c, g); // Create a unique global instance if we need one. make_global_instance(c, g); // Generate all the methods. if(!genfun_methods(c, g)) { free_fields(g); return false; } if(g->underlying != TK_STRUCT) gendesc_init(c, g); // Finish off the dispatch function. if(g->underlying == TK_ACTOR) { codegen_startfun(c, g->dispatch_fn, false); codegen_finishfun(c); } // Finish the dwarf frame. dwarf_finish(&c->dwarf); } free_fields(g); g->done = true; return true; }
static int cgc_read_fields(interp_t *interp) { unsigned int i, cnt, min, last; const char *fs = get_string(interp, "FS"); char *s; if (fs == NULL) goto fail; min = cgc_strlen(fs); free_fields(interp); interp->field0 = interp->buf; if (interp->buf[0] == 0) return 1; for (cnt = 0, i = 0; interp->buf[i] != 0; i++) { if (min == 0) { cnt++; } else if (cgc_strncmp(&interp->buf[i], fs, min) == 0) { i += min - 1; cnt++; } } if (cnt >= MAX_FIELDS) goto fail; interp->num_fields = cnt + 1; interp->fields = cgc_calloc(sizeof(char *), interp->num_fields); if (interp->fields == NULL) goto fail; for (last = 0, cnt = 0, i = 0; interp->buf[i] != 0; i++) { if (min != 0 && cgc_strncmp(&interp->buf[i], fs, min) != 0) continue; s = cgc_malloc(i - last + 1); if (s == NULL) goto fail; cgc_memcpy(s, &interp->buf[last], i - last); s[i - last] = 0; interp->fields[cnt++] = s; if (cnt == interp->num_fields) goto fail; if (min) i += min - 1; last = i + 1; } s = cgc_malloc(i - last + 1); if (s == NULL) goto fail; cgc_memcpy(s, &interp->buf[last], i - last); s[i - last] = 0; interp->fields[cnt++] = s; return 1; fail: free_fields(interp); return 0; }
int cgc_program_run(program_t *prog, io_t *io) { int result = EVAL_ERROR; interp_t interp; cgc_memset(&interp, 0, sizeof(interp_t)); interp.io = io; interp.prog = prog; if (!cgc_dict_init(&interp.vars, cgc_free_var)) return 0; if ((interp.buf = cgc_malloc(BUF_SIZE)) == NULL) goto done; if (!cgc_dict_add(&interp.vars, "RS", new_string(cgc_strdup("\n")))) goto done; if (!cgc_dict_add(&interp.vars, "ORS", new_string(cgc_strdup("\n")))) goto done; if (!cgc_dict_add(&interp.vars, "FS", new_string(cgc_strdup(" ")))) goto done; if (!cgc_dict_add(&interp.vars, "OFS", new_string(cgc_strdup(" ")))) goto done; while (1) { pattern_t *p; result = EVAL_FINISHED; if (!read_record(&interp)) break; #define DO_EVAL() do { \ result = cgc_eval_statements(&interp, p->stmt); \ if (result == EVAL_NEXT) \ goto next; \ else if (result != EVAL_FINISHED) \ goto done; \ } while (0) // apply BEGIN patterns for (p = interp.prog->patterns; p != NULL; p = p->next) { if (p->pattern == PATTERN_BEGIN) DO_EVAL(); } // apply normal patterns for (p = interp.prog->patterns; p != NULL; p = p->next) { if (p->pattern == PATTERN_INVALID || p->pattern == PATTERN_BEGIN || p->pattern == PATTERN_END) continue; if (p->pattern == PATTERN_EMPTY) { DO_EVAL(); } else { if (!cgc_eval_expression(&interp, (expr_t*)p->pattern)) goto done; if (coerce_bool(&interp, &interp.result)) DO_EVAL(); } } next: // apply END patterns for (p = interp.prog->patterns; p != NULL; p = p->next) { if (p->pattern == PATTERN_END) DO_EVAL(); } free_fields(&interp); } done: free_fields(&interp); cgc_free(interp.buf); cgc_dict_free(&interp.vars); return result != EVAL_ERROR; }
static int do_showhelp( char **helptopic, char *filename, EVENT (*rtn)( EVENT ), bool first ) { int err; char *ptr; unsigned len; char *htopic; eventMapFn = rtn; helpTab = NULL; helpCur = helpTab; strcpy( curFile, filename ); helpInBuf = HelpMemAlloc( BUF_LEN ); if( helpInBuf == NULL ) { HelpMemFree( helpStack ); return( HELP_NO_MEM ); } helpOutBuf = HelpMemAlloc( BUF_LEN ); if( helpOutBuf == NULL ) { HelpMemFree( helpStack ); return( HELP_NO_MEM ); } // don't fix hyperlink topics if( *helptopic != NULL && first ) { htopic = fixHelpTopic( *helptopic ); } else if( *helptopic == NULL ) { len = 1; htopic = HelpMemAlloc( len ); htopic[0] = '\0'; } else { len = strlen( *helptopic ); htopic = HelpMemAlloc( len + 1 ); strcpy( htopic, *helptopic ); } nexttopic( htopic ); for( ;; ) { err = findhelp( &tabFilter ); if( err != HELP_OK ) break; if( curEvent == EV_ESCAPE ) break; if( curEvent == EV_KILL_UI ) { break; } } SearchDlgFini(); help_close(); // This is Not Nice - we're freeing memory that // someone else allocated! Just don't do it. if( err != HELP_NO_SUBJECT ) { if( *helptopic != NULL ) { HelpMemFree( *helptopic ); *helptopic = NULL; } *filename = '\0'; } HelpMemFree( helpInBuf ); HelpMemFree( helpOutBuf ); HelpMemFree( htopic ); if( helpTab != NULL && helpCur->key2_len != 0 ) { // cross file link len = helpCur->key1_len; *helptopic = ptr = HelpMemAlloc( len + 1 ); strncpy( ptr, helpCur->keyword, len ); ptr[len] = '\0'; ptr = helpCur->keyword + len; len = helpCur->key2_len; strncpy( filename, ptr, len ); filename[len] = '\0'; if( helpCur != tabFilter.curr ) { // backwards through cross file link prevtopic(); } } else { Free_Stack(); } free_fields( &helpTab ); return( err ); }