void parse_section_output(struct test *test, int sec, const char *datap, int len, void *refcon) { assert(sec >= 0); switch(sec) { case 0: // eof! nothing to do. break; case exSTDOUT|exNEW: write_section(test, datap, len, test->outfd, "STDOUT"); test->stdout_match = match_yes; break; case exSTDOUT: // ignore all data in the expected stdout. break; case exSTDERR|exNEW: write_section(test, datap, len, test->errfd, "STDERR"); test->stderr_match = match_yes; break; case exSTDERR: // ignore all data in the expected stderr break; default: write(test->rewritefd, datap, len); } }
void output(void) { const_array = jflag ? "private final static " : ""; free_itemsets(); free_shifts(); free_reductions(); output_prefix(); output_stored_text(); output_defines(); output_rule_data(); output_yydefred(); output_actions(); free_parser(); output_debug(); if (!jflag) { output_stype(); if (rflag) write_section(tables); write_section(header); output_trailing_text(); } write_section(jflag ? jbody : body); output_semantic_actions(); write_section(jflag ? jtrailer : trailer); if (jflag) output_trailing_text(); }
void output() #endif { free_itemsets(); free_shifts(); free_reductions(); output_stored_text(); output_defines(); output_rule_data(); output_yydefred(); output_actions(); free_parser(); output_debug(); output_stype(); if (rflag) write_section(tables); write_section(header); if (language == C) output_trailing_text(); write_section(body); output_semantic_actions(); write_section(trailer); if ((language == PERL) || (language == PYTHON)) output_trailing_text(); free_derives(); free_nullable(); }
/* * Recursively prints a tree of INI sections */ static void write_section(ini_section *s, FILE *f) { if(!s) return; fputs("\n[", f); string_to_file(f, s->name); fputs("]\n", f); write_pair(s->fields, f); /* The akward sequence is to ensure that values are not written sorted */ write_section(s->left, f); write_section(s->right, f); }
void generate_code(java_file file, size_t method_index, FILE* source) { struct java_method* method = java_get_class(file)->methods + method_index; buffer bytecode_buffer = buffer_create(1024); bool success; write_method_prologue(bytecode_buffer); write_section(bytecode_buffer, source, file); write_method_epilogue(bytecode_buffer); success = (peek_token(source) == EOF); /* Otherwise it's an unmatched ']'. */ if (success) { method->max_stack = 6; method->max_locals = 1; method->bytecode_length = buffer_length(bytecode_buffer); method->bytecode = buffer_publish(bytecode_buffer); } buffer_free(bytecode_buffer); if (!success) { printf("Error: Unmatched \"]\".\n"); abort(); } }
/** Write a single value out to file. */ static void write_value(t3_config_t *config, FILE *file, int indent) { switch (config->type) { case T3_CONFIG_BOOL: fputs(config->value.boolean ? "true" : "false", file); break; case T3_CONFIG_INT: write_int(file, config->value.integer); break; case T3_CONFIG_NUMBER: write_number(file, config->value.number); break; case T3_CONFIG_STRING: write_string(file, config->value.string); break; case T3_CONFIG_LIST: case T3_CONFIG_PLIST: fputs("( ", file); write_list(config->value.list, file, indent + 1); fputs(" )", file); break; case T3_CONFIG_SECTION: fputs("{\n", file); write_section(config->value.list, file, indent + 1); write_indent(file, indent); fputc('}', file); break; default: /* This can only happen if the client screws up the list. */ break; } }
int t3_config_write_file(t3_config_t *config, FILE *file) { if (config->type != T3_CONFIG_SECTION) { return T3_ERR_BAD_ARG; } write_section(config->value.list, file, 0); return ferror(file) ? T3_ERR_ERRNO : T3_ERR_SUCCESS; }
void output(void) { free_itemsets(); free_shifts(); free_reductions(); output_stored_text(); if (jflag) /*rwj*/ { write_section(jheader); output_stype(); } output_defines(); output_rule_data(); output_yydefred(); output_actions(); free_parser(); output_debug(); if (!jflag) /*rwj*/ output_stype(); if (rflag) write_section(tables); if (!jflag) /*rwj*/ write_section(header); output_trailing_text(); if (jflag) /*rwj*/ write_section(jbody); else write_section(body); output_semantic_actions(); if (jflag) /*rwj*/ write_section(jtrailer); else write_section(trailer); }
void output() { free_itemsets(); free_shifts(); free_reductions(); output_stored_text(); output_defines(); output_rule_data(); output_yydefred(); output_actions(); free_parser(); output_debug(); output_stype(); if (rflag) write_section("tables"); write_section("header"); output_trailing_text(); write_section("body"); output_semantic_actions(); write_section("trailer"); }
int configuration_write_to_file (const ConfigurationSection *sections, int num_sections, const char *filename) { BufferedWriter writer; const char* initialization_error = buffered_writer_init (&writer, filename, 0x1000); if (!initialization_error) { int k; buffered_writer_cat_string (&writer, configuration_file_intro_comment); for (k = 0; k < num_sections; k++, sections++) { if (sections->is_repeatable) { StringList *abstract_list = (StringList *) sections->section_structure; StringListItem *abstract_item; for (abstract_item = abstract_list->first; abstract_item; abstract_item = abstract_item->next) { write_section (&writer, sections, abstract_item, abstract_item->text); } } else write_section (&writer, sections, sections->section_structure, NULL); } if (!writer.successful) { fprintf (stderr, "error writing configuration: %s\n", writer.error_string); } return buffered_writer_dispose (&writer); } else { fprintf (stderr, "error opening configuration file for writing: %s\n", initialization_error); } return 0; }
output() { free_itemsets(); free_shifts(); free_reductions(); output_prefix(); output_stored_text(); output_defines(); output_rule_data(); output_yydefred(); output_actions(); free_parser(); output_debug(); output_stype(); if (rflag) write_section(tables); write_section(header); output_trailing_text(); write_section(body); output_semantic_actions(); write_section(trailer); }
int conf_save(const char *path, struct conf_entry *root) { FILE *f; int count; if (!(f = fopen(path, "w+"))) { DBG(DBG_ERROR, "fopen('%s') fail!", path); return -1; }; count = write_section(f, root, NULL); fclose(f); return count; }
/* * Recursive writes a section into a stream * */ static int write_section(FILE * f, struct conf_entry * section, char * branch) { struct conf_entry *entry; int count = 0; char buf[2048]; /* define a queue for sections */ struct conf_entry *queue[1024]; int head = 0; int tail = 0; if (section == NULL) return 0; DBG(DBG_INFO, "section: '%s'", section->name); entry = section; while (entry->name != NULL) { if (entry->type == CONF_TYPE(CONF_SECTION)) { queue[tail++] = (struct conf_entry *) entry; } else { if (entry->type->t_get(entry, buf)) { fprintf(f, "%s = %s\n", entry->name, buf); } else { /* Assuming the above failure means configuration stub. */ fprintf(f, "%s = %s\n", entry->name, NULL_STRING); } count++; } entry++; } for (head = 0; head < tail; head++) { entry = queue[head]; fprintf(f, "\n"); if ((branch != NULL) && (*branch != '\0')) sprintf(buf, "%s/%s", branch, entry->name); else strcpy(buf, entry->name); fprintf(f, "[%s]\n", buf); count += write_section(f, (struct conf_entry *) entry->p, buf); } return count; }
static gboolean write_configuration (Common * c, gchar * out, int dev) { int fd; gboolean ret; GHashTable *params; fd = open (out, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd == -1) { perror ("open"); return FALSE; } if (!add_line (fd, g_strdup_printf ("# configuration for device %d\n", dev))) { goto error; } g_object_get (c->cam_src, "device-parameters", ¶ms, NULL); if (!params) { g_print ("Failed to get device parameters\n"); goto error; } struct Node *n = Nodes; while (n->droid) { const gchar *p = params_get_string (params, n->droid); if (p) { if (!write_section (fd, n, p)) { goto error; } } ++n; } out: close (fd); fd = -1; return ret; error: ret = FALSE; goto out; }
/* * Saves all the sections and parameters in an ini_file to a file. * If fname is NULL, it is written to stdout. */ int ini_write(struct ini_file *ini, const char *fname) { FILE *f; if(fname) { f = fopen(fname, "w"); if(!f) return ER_FOPEN; } else f = stdout; write_pair(ini->globals, f); write_section(ini->sections, f); if(fname) fclose(f); return SUCCESS; }
void write_elf(FILE *outfile, Elf *elf) { add_symtab(elf); add_reloc(elf); add_shstrtab(elf); // Section header String *sh = make_string(); for (int i = 0; i < 64; i++) o1(sh, 0); // NULL section header // Body String *content = make_string(); for (int i = 0; i < LIST_LEN(elf->sections); i++) { write_section(sh, content, LIST_REF(elf->sections, i), 64); } align(content, 16); // ELF header String *eh = make_string(); int numsect = LIST_LEN(elf->sections) + 1; out(eh, elf_ident, sizeof(elf_ident)); o2(eh, 1); // e_type = ET_REL o2(eh, 62); // e_machine = EM_X86_64 o4(eh, 1); // e_version = EV_CURRENT o8(eh, 0); // e_entry o8(eh, 0); // e_phoff o8(eh, STRING_LEN(content) + 64); // e_shoff; o4(eh, 0); // e_flags o2(eh, 64); // e_ehsize o2(eh, 0); // e_phentsize o2(eh, 0); // e_phnum o2(eh, 64); // e_shentsize o2(eh, numsect); // e_shnum o2(eh, elf->shnum); // e_shstrndx fwrite(STRING_BODY(eh), STRING_LEN(eh), 1, outfile); fwrite(STRING_BODY(content), STRING_LEN(content), 1, outfile); fwrite(STRING_BODY(sh), STRING_LEN(sh), 1, outfile); fclose(outfile); }
/* Consumes and generates bytecode for all characters up to and including the next ']' or EOF. This function is similar to write_section(), except that this function assumes the next token is the beginning of a loop body. */ static void write_loop(buffer destination, FILE* source, java_file file) { bool success; buffer loop_body = buffer_create(64); write_section(loop_body, source, file); success = (get_token(source) == ']'); /* Otherwise it's EOF. */ if (success) { write_loop_prologue(destination, buffer_length(loop_body)); buffer_write_buffer(destination, loop_body); write_loop_epilogue(destination, buffer_length(loop_body)); } buffer_free(loop_body); if (!success) { printf("Error: Unmatched \"[\".\n"); abort(); } }
void output(void) { FILE *fp; free_itemsets(); free_shifts(); free_reductions(); if (iflag) { ++outline; fprintf(code_file, "#include \"%s\"\n", externs_file_name); fp = externs_file; } else fp = code_file; output_prefix(iflag ? externs_file : output_file); output_pure_parser(fp); output_stored_text(fp); output_stype(fp); output_parse_decl(fp); output_lex_decl(fp); output_error_decl(fp); write_section(fp, xdecls); if (iflag) { output_externs(externs_file, global_vars); if (!pure_parser) output_externs(externs_file, impure_vars); } if (iflag) { ++outline; fprintf(code_file, "#include \"%s\"\n", defines_file_name); if (!dflag) output_defines(externs_file); } else { putc_code(code_file, '\n'); output_defines(code_file); } if (dflag) output_defines(defines_file); output_rule_data(); output_yydefred(); output_actions(); free_parser(); output_debug(); if (rflag) { output_prefix(code_file); write_section(code_file, xdecls); write_section(code_file, tables); } write_section(code_file, global_vars); if (!pure_parser) { write_section(code_file, impure_vars); } write_section(code_file, hdr_defs); if (!pure_parser) { write_section(code_file, hdr_vars); } output_trailing_text(); write_section(code_file, body_1); if (pure_parser) { write_section(code_file, body_vars); } write_section(code_file, body_2); output_yyerror_call("syntax error"); write_section(code_file, body_3); output_semantic_actions(); write_section(code_file, trailer); output_yyerror_call("yacc stack overflow"); write_section(code_file, trailer_2); }
void output(void) { free_itemsets(); free_shifts(); free_reductions(); output_stored_text(); if (jflag) /*rwj*/ { write_section(jheader); output_stype(); } output_defines(); output_rule_data(); output_yydefred(); output_actions(); free_parser(); output_debug(); if (!jflag) /*rwj*/ output_stype(); if (rflag) write_section(tables); if (!jflag) /*rwj*/ write_section(header); output_trailing_text(); if (jflag) { /*rwj*/ /* yio 20020304: nodebug and throws options */ if (jdebug == TRUE) { write_section(jbody_a); if (strlen(jyyparse_throws)>0) fprintf(code_file,"throws %s\n",jyyparse_throws); write_section(jbody_b); } else { write_section(jbody_nodebug_a); if (strlen(jyyparse_throws)>0) fprintf(code_file,"throws %s\n",jyyparse_throws); write_section(jbody_nodebug_b); } } else write_section(body); output_semantic_actions(); if (jflag) { /*rwj*/ /* yio 20020304: nodebug option */ if (jdebug == TRUE) write_section(jtrailer); else write_section(jtrailer_nodebug); } else write_section(trailer); }
int conf_dump(struct conf_entry *root) { return write_section(stdout, root, NULL); }
int main (int argc, char **argv) { int o; FILE *f = NULL; char *filename = NULL; const char *location = NULL; int extract = 0; char magic[MAGIC_LEN]; while ((o = getopt(argc, argv, "hixC:")) != -1) { switch (o) { case 'i': extract = 0; break; case 'x': extract = 1; break; case 'C': location = optarg; break; case 'h': usage(argv[0]); return EXIT_SUCCESS; default: printf("ERROR: unkown argument '%c'\n\n", o); usage(argv[0]); return EXIT_FAILURE; } } if (optind >= argc) { printf("ERROR: no image-file\n\n"); usage(argv[0]); return EXIT_FAILURE; } filename = strdup(argv[optind]); if (filename == NULL) { printf("ERROR: %s\n", strerror(errno)); return EXIT_FAILURE; } if ((f = fopen(filename, "r")) == NULL) { printf("ERROR: Cannot open image file %s\n", filename); return EXIT_FAILURE; } if (location) { DIR *d = NULL; if (strlen(location) > (FILE_PATH_MAXLEN - FILE_SECTION_MAXLEN)) { printf("ERROR: location path too long\n"); goto fail; } d = opendir(location); if (!d) { printf("ERROR: location dir: %s\n", strerror(errno)); goto fail; } closedir(d); } printf("\nImage file: %s\n\n", filename); if (fread(magic, MAGIC_LEN, 1, f) != 1) { printf("ERROR: %s\n", strerror(errno)); goto fail; } if (memcmp(magic, MAGIC_HEADER, 4) == 0) { struct header h; if (fread(&h, sizeof(struct header), 1, f) != 1) { printf("ERROR: %s\n", strerror(errno)); goto fail; } if (!extract) { print_header_info(&h); printf("\n"); } } else { goto fail; } while (!feof(f)) { if (fread(magic, MAGIC_LEN, 1, f) != 1) { printf("ERROR: %s\n", strerror(errno)); goto fail; } if (memcmp(magic, MAGIC_END, 4) == 0) { struct signature s; if (fread(&s, sizeof(struct signature), 1, f) != 1) { printf("ERROR: %s\n", strerror(errno)); goto fail; } if (!extract) { printf("Sign CRC: 0x%.8x\n", ntohl(s.crc)); printf("\n"); } break; } else { /* Assume a section */ struct section s; if (fread(&s, sizeof(struct section), 1, f) < 1) { printf("ERROR: %s\n", strerror(errno)); goto fail; } if (!extract) { struct section_crc scrc; print_section_info(&s); fseek(f, ntohl(s.data_size), SEEK_CUR); if (fread(&scrc, sizeof(struct section_crc), 1, f) != 1) { printf("ERROR: %s\n", strerror(errno)); goto fail; } printf("Section CRC: 0x%.8x\n", ntohl(scrc.crc)); printf("\n"); } else { char *data = (char *)malloc(ntohl(s.data_size)); if (data == NULL) { printf("ERROR: %s\n", strerror(errno)); goto fail; } if (fread(data, ntohl(s.data_size), 1, f) != 1) { printf("ERROR: %s\n", strerror(errno)); goto fail; } if (write_section(&s, (const char *)data, location) == -1) { goto fail; } free(data); fseek(f, sizeof(struct section_crc), SEEK_CUR); } } } fclose(f); free(filename); return EXIT_SUCCESS; fail: fclose(f); free(filename); return EXIT_FAILURE; }
int main(int argc, char* argv[]) { if (argc != 3) { printf("usage:\n"); printf(" elf2tinyapp elf_file app_file\n"); return -1; } const char *elf_file = argv[1]; const char *app_file = argv[2]; printf("elf file: %s\n", elf_file); printf("app file: %s\n", app_file); struct ELF_SECTION text, rodata, data, bss; memset(&text, 0, sizeof(struct ELF_SECTION)); memset(&rodata, 0, sizeof(struct ELF_SECTION)); memset(&data, 0, sizeof(struct ELF_SECTION)); memset(&bss, 0, sizeof(struct ELF_SECTION)); text.name = ".text"; rodata.name = ".rodata"; data.name = ".data"; bss.name = ".bss"; read_section(elf_file, &text); read_section(elf_file, &rodata); read_section(elf_file, &data); read_section(elf_file, &bss); printf("text size: %d\n", text.size); printf("rodata size: %d\n", rodata.size); printf("data size: %d\n", data.size); printf("bss size: %d\n", bss.size); assert(text.size > 0); assert(rodata.size >= 0); assert(data.size >= 0); assert(bss.size >= 0); // app header struct APP_HEADER header; assert(sizeof(header) == 36); int inital_data_size = 0; if (rodata.size > 0) { inital_data_size = rodata.VMA + rodata.size - 0x310000; } if (data.size > 0) { inital_data_size = data.VMA + data.size - 0x310000; } if (bss.size > 0) { inital_data_size = bss.VMA + bss.size - 0x310000; } assert(inital_data_size >= rodata.size + data.size + bss.size); header.total_size = 0x311000; memcpy(header.sign, "tiny", 4); header.mmarea_size = 0; header.data_addr = 0x310000; header.data_size = inital_data_size; header.inital_data_pos = 0; header.opcode = 0xe9000000; header.entry_addr = (unsigned int)get_start_address(elf_file) - 0x20; header.heap_addr = (header.data_addr + inital_data_size + 3) / 4 * 4; // generate tinyapp file FILE *app_fp = fopen(app_file, "wb"); assert(app_fp != 0); write_header(app_fp, &header); write_section(app_fp, &header, &text); write_align4(app_fp); header.inital_data_pos = ftell(app_fp); write_section(app_fp, &header, &rodata); write_section(app_fp, &header, &data); write_section(app_fp, &header, &bss); // update tinyapp header write_header(app_fp, &header); fclose(app_fp); return 0; }