void vi_x86_inst_read_checkpoint(struct vi_x86_inst_t *inst, FILE *f) { int count; char name[MAX_STRING_SIZE]; char asm_code[MAX_STRING_SIZE]; char asm_micro_code[MAX_STRING_SIZE]; /* ID */ count = fread(&inst->id, 1, sizeof inst->id, f); if (count != sizeof inst->id) panic("%s: cannot read checkpoint", __FUNCTION__); /* Speculative mode */ count = fread(&inst->spec_mode, 1, sizeof inst->spec_mode, f); if (count != sizeof inst->spec_mode) panic("%s: cannot read checkpoint", __FUNCTION__); /* Stage */ count = fread(&inst->stage, 1, sizeof inst->stage, f); if (count != sizeof inst->stage) panic("%s: cannot read checkpoint", __FUNCTION__); /* Assembly code */ str_read_from_file(f, asm_code, sizeof asm_code); str_read_from_file(f, asm_micro_code, sizeof asm_micro_code); inst->asm_code = str_set(inst->asm_code, asm_code); inst->asm_micro_code = str_set(inst->asm_micro_code, asm_micro_code); /* Instruction name */ snprintf(name, sizeof name, "i-%lld", inst->id); inst->name = str_set(inst->name, name); }
void FPGAKernelSetInputOutputFormat(FPGAKernel *self, char *format, int type) { char *temp; char *delim = ";", *delim1 = ","; char *param, *param1; int i; int param_num, *param_counts; short *param_sizes; /* Duplicate argument string */ format = str_set(NULL, format); temp = xstrdup(format); param_num = 0; for (param = strtok(temp, delim); param; param = strtok(NULL, delim)) { param_num++; } param_sizes = xcalloc(param_num, sizeof(int)); param_counts = xcalloc(param_num, sizeof(int)); temp = xstrdup(format); i = 0; for (param = strtok(temp, delim); param; param = strtok(NULL, delim)) { param = str_set(NULL, param); param1 = strtok(param, delim1); if(!strcmp(param1, "long") || !strcmp(param1, "double")) param_sizes[i] = 8; else if (!strcmp(param1, "short")) param_sizes[i] = 2; else if (!strcmp(param1, "char")) param_sizes[i] = 1; else param_sizes[i] = 4; param1 = strtok(NULL, delim1); param_counts[i] = atoi(param1); i++; } if (!type) { self->in_param_num = param_num; self->in_param_sizes = param_sizes; self->in_param_counts = param_counts; } else { self->out_param_num = param_num; self->out_param_sizes = param_sizes; self->out_param_counts = param_counts; } }
void FPGAKernelAddImpsString(FPGAKernel *self, char *imps, implement_param type) { char *delim = " "; char *param; /* Duplicate argument string */ imps = str_set(NULL, imps); switch (type) { case WIDTH: self->widths = list_create(); break; case LENGTH: self->lengths = list_create(); break; case HEIGHT: self->heights = list_create(); break; default: fatal("Undefined Implement Parameter Type!\n"); } /* Tokens */ int i = 0; for (param = strtok(imps, delim); param; param = strtok(NULL, delim)) { param = str_set(NULL, param); int temp = atoi(param); switch (type) { case WIDTH: list_add(self->widths, &temp); break; case LENGTH: list_add(self->lengths, &temp); break; case HEIGHT: list_add(self->heights, &temp); break; default: fatal("Undefined Implement Parameter Type!\n"); } i++; } if (i != self->num_implements) fatal("Unmatched number of implements!"); /* Free argument string */ str_free(imps); }
static void show_option( enum OptType type, Bool *pflag, char *short_opt, char *long_opt, char *help_text, char *help_arg ) { STR_DEFINE(msg, STR_SIZE); int count_opts = 0; if ( type == OptDeprecated ) return; /* skip deprecated options */ /* show default option */ if ( ( type == OptSet && *pflag ) || ( type == OptClear && ! *pflag ) ) str_set( msg, "* " ); else str_set( msg, " " ); if ( *short_opt ) { /* dont show short_opt if short_opt is same as long_opt, except for extra '-', e.g. -sdcc and --sdcc */ if ( !( *long_opt && strcmp( short_opt, long_opt + 1 ) == 0 ) ) { str_append_sprintf( msg, "%s", short_opt ); count_opts++; } } if ( *long_opt ) { if ( count_opts ) str_append( msg, ", " ); str_append_sprintf( msg, "%s", long_opt ); count_opts++; } if ( *help_arg ) { str_append_sprintf( msg, "=%s", help_arg ); } if ( str_len(msg) > ALIGN_HELP ) printf( "%s\n%-*s %s\n", str_data(msg), ALIGN_HELP, "", help_text ); else printf( "%-*s %s\n", ALIGN_HELP, str_data(msg), help_text ); STR_DELETE(msg); }
static void tr_co(char **args) { char *src = args[1]; char *dst = args[2]; if (src && dst && str_get(map(src))) str_set(map(dst), str_get(map(src))); }
void vi_x86_context_read_checkpoint(struct vi_x86_context_t *context, FILE *f) { int count; char name[MAX_STRING_SIZE]; /* Name */ str_read_from_file(f, name, sizeof name); context->name = str_set(context->name, name); /* ID */ count = fread(&context->id, 1, sizeof context->id, f); if (count != sizeof context->id) panic("%s: cannot read checkpoint", __FUNCTION__); /* Core ID */ count = fread(&context->core_id, 1, sizeof context->core_id, f); if (count != sizeof context->core_id) panic("%s: cannot read checkpoint", __FUNCTION__); /* Thread ID */ count = fread(&context->thread_id, 1, sizeof context->thread_id, f); if (count != sizeof context->thread_id) panic("%s: cannot read checkpoint", __FUNCTION__); /* Parent PID */ count = fread(&context->parent_id, 1, sizeof context->parent_id, f); if (count != sizeof context->parent_id) panic("%s: cannot read checkpoint", __FUNCTION__); /* Creation cycle */ count = fread(&context->creation_cycle, 1, sizeof context->creation_cycle, f); if (count != sizeof context->creation_cycle) panic("%s: cannot read checkpoint", __FUNCTION__); }
void frm_spatial_report_config_read(struct config_t *config) { char *section; char *file_name; /*Nothing if section or config is not present */ section = frm_spatial_report_section_name; if (!config_section_exists(config, section)) { /*no spatial profiling */ return; } /* Spatial reports are active */ frm_spatial_report_active = 1; /* Interval */ config_var_enforce(config, section, "Interval"); spatial_profiling_interval = config_read_int(config, section, "Interval", spatial_profiling_interval); /* File name */ config_var_enforce(config, section, "File"); file_name = config_read_string(config, section, "File", NULL); if (!file_name || !*file_name) fatal("%s: %s: invalid or missing value for 'File'", frm_spatial_report_section_name, section); spatial_report_filename = str_set(NULL, file_name); spatial_report_file = file_open_for_write(spatial_report_filename); if (!spatial_report_file) fatal("%s: could not open spatial report file", spatial_report_filename); }
/* Add environment variables from the actual environment plus * the list attached in the argument 'env'. */ void X86ContextAddEnv(X86Context *self, char *env) { struct x86_loader_t *loader = self->loader; extern char **environ; char *next; char *str; int i; /* Add variables from actual environment */ for (i = 0; environ[i]; i++) { str = str_set(NULL, environ[i]); linked_list_add(loader->env, str); } /* Add the environment vars provided in 'env' */ while (env) { /* Skip spaces */ while (*env == ' ') env++; if (!*env) break; /* Get new environment variable */ switch (*env) { case '"': case '\'': if (!(next = strchr(env + 1, *env))) fatal("%s: wrong format", __FUNCTION__); *next = 0; str = str_set(NULL, env + 1); linked_list_add(loader->env, str); env = next + 1; break; default: str = str_set(NULL, env); linked_list_add(loader->env, str); env = NULL; } } }
STR * str_make(char *s) { register STR *str = str_new(0); str_set(str,s); return str; }
struct vi_x86_inst_t *vi_x86_inst_create(long long id, char *name, char *asm_code, char *asm_micro_code, int spec_mode, enum vi_x86_inst_stage_t stage) { struct vi_x86_inst_t *inst; /* Initialize */ inst = xcalloc(1, sizeof(struct vi_x86_inst_t)); inst->id = id; inst->name = str_set(NULL, name); inst->asm_code = str_set(NULL, asm_code); inst->asm_micro_code = str_set(NULL, asm_micro_code); inst->spec_mode = spec_mode; inst->stage = stage; /* Return */ return inst; }
/** * Finish dots percent mode. If in hash verification mode, * then print the results of file check. * * @param info pointer to the file-info structure * @param process_res non-zero if error occurred while hashing/checking */ static void dots_finish_percents(struct file_info *info, int process_res) { char buf[80]; info->error = process_res; if ((percents.points % 74) != 0) { log_msg("%s 100%%\n", str_set(buf, ' ', 74 - (percents.points%74) )); } print_results_on_check(info, 0); }
void X86ContextAddArgsString(X86Context *self, char *args) { struct x86_loader_t *loader = self->loader; char *delim = " "; char *arg; /* Duplicate argument string */ args = str_set(NULL, args); /* Tokens */ for (arg = strtok(args, delim); arg; arg = strtok(NULL, delim)) { arg = str_set(NULL, arg); linked_list_add(loader->args, arg); } /* Free argument string */ str_free(args); }
void X86ContextAddArgsVector(X86Context *self, int argc, char **argv) { struct x86_loader_t *loader = self->loader; char *arg; int i; for (i = 0; i < argc; i++) { arg = str_set(NULL, argv[i]); linked_list_add(loader->args, arg); } }
static void tr_as(char **args) { int reg; char *s1, *s2, *s; reg = map(args[1]); s1 = str_get(reg) ? str_get(reg) : ""; s2 = args[2] ? args[2] : ""; s = xmalloc(strlen(s1) + strlen(s2) + 1); strcpy(s, s1); strcat(s, s2); str_set(reg, s); free(s); }
static void Llvm2siSymbolCreate(Llvm2siSymbol *self, char *name, enum llvm2si_symbol_type_t type, int reg, int count) { /* Check valid name */ if (!name || !*name) fatal("%s: empty symbol name", __FUNCTION__); /* Initialize */ self->name = str_set(self->name, name); self->type = type; self->reg = reg; self->count = count; }
struct vi_x86_core_t *vi_x86_core_create(char *name) { struct vi_x86_core_t *core; /* Initialize */ core = xcalloc(1, sizeof(struct vi_x86_core_t)); core->name = str_set(NULL, name); core->context_table = hash_table_create(0, FALSE); core->inst_table = hash_table_create(0, FALSE); /* Return */ return core; }
static void tr_coa(char **args) { char *src = args[1]; char *dst = args[2]; if (src && dst && str_get(map(src))) { struct sbuf sb; sbuf_init(&sb); if (str_get(map(dst))) sbuf_append(&sb, str_get(map(dst))); sbuf_append(&sb, str_get(map(src))); str_set(map(dst), sbuf_buf(&sb)); sbuf_done(&sb); } }
static char *get_opts_ext_filename( char *filename, char *opts_ext ) { STR_DEFINE(ext, FILENAME_MAX); char *ret; init_module(); str_set( ext, FILEEXT_SEPARATOR ); str_append( ext, opts_ext ); ret = path_replace_ext(filename, str_data(ext)); STR_DELETE(ext); return ret; }
static void tr_chop(char **args) { struct sbuf sbuf; int id; id = map(args[1]); if (str_get(id)) { sbuf_init(&sbuf); sbuf_append(&sbuf, str_get(id)); if (!sbuf_empty(&sbuf)) { sbuf_cut(&sbuf, sbuf_len(&sbuf) - 1); str_set(id, sbuf_buf(&sbuf)); } sbuf_done(&sbuf); } }
static void tr_de(char **args) { struct sbuf sbuf; int id; if (!args[1]) return; id = map(args[1]); sbuf_init(&sbuf); if (args[0][1] == 'a' && args[0][2] == 'm' && str_get(id)) sbuf_append(&sbuf, str_get(id)); macrobody(&sbuf, args[2] ? args[2] : "."); str_set(id, sbuf_buf(&sbuf)); sbuf_done(&sbuf); if (!n_cp && args[3]) /* parse the arguments as request argv[3] */ str_dset(id, str_dget(map(args[3]))); }
static void tr_coi(char **args) { char *reg = args[1]; char *path = args[2]; char buf[1024]; FILE *fp; if (!reg || !reg[0] || !path || !path[0]) return; if ((fp = fopen(path + 1, "r"))) { struct sbuf sb; sbuf_init(&sb); while (fgets(buf, sizeof(buf), fp)) sbuf_append(&sb, buf); str_set(map(reg), sbuf_buf(&sb)); sbuf_done(&sb); fclose(fp); } }
struct vi_x86_context_t *vi_x86_context_create(char *name, int id, int parent_id) { struct vi_x86_context_t *context; /* Allocate */ context = calloc(1, sizeof(struct vi_x86_context_t)); if (!context) fatal("%s: out of memory", __FUNCTION__); /* Initialize */ context->name = str_set(NULL, name); context->id = id; context->parent_id = parent_id; context->creation_cycle = vi_state_get_current_cycle(); /* Return */ return context; }
/*----------------------------------------------------------------------------- * return full symbol name NAME@MODULE stored in strpool *----------------------------------------------------------------------------*/ char *Symbol_fullname( Symbol *sym ) { STR_DEFINE(name, STR_SIZE); char *ret; str_set( name, sym->name ); if ( sym->module && sym->module->modname ) { str_append_char( name, '@' ); str_append( name, sym->module->modname ); } ret = strpool_add( str_data(name) ); STR_DELETE(name); return ret; }
void restore_scan_state(void) { ScanState *save; init_module(); save = (ScanState *)utarray_back(scan_state); sym = save->sym; str_set(input_buf, save->input_buf); at_bol = save->at_bol; EOL = save->EOL; cs = save->cs; act = save->act; p = save->p >= 0 ? str_data(input_buf) + save->p : NULL; pe = save->pe >= 0 ? str_data(input_buf) + save->pe : NULL; eof = save->eof >= 0 ? str_data(input_buf) + save->eof : NULL; ts = save->ts >= 0 ? str_data(input_buf) + save->ts : NULL; te = save->te >= 0 ? str_data(input_buf) + save->te : NULL; // str_set(sym_string, save->sym_string); expect_opcode = save->expect_opcode; utarray_pop_back(scan_state); }
/** * Check hash sums in a hash file. * Lines beginning with ';' and '#' are ignored. * * @param hash_file_path - the path of the file with hash sums to verify. * @param chdir - true if function should emulate chdir to directory of filepath before checking it. * @return zero on success, -1 on fail */ int check_hash_file(file_t* file, int chdir) { FILE *fd; char buf[2048]; size_t pos; const char *ralign; timedelta_t timer; struct file_info info; const char* hash_file_path = file->path; int res = 0, line_num = 0; double time; /* process --check-embedded option */ if(opt.mode & MODE_CHECK_EMBEDDED) { unsigned crc32_be; if(find_embedded_crc32(hash_file_path, &crc32_be)) { /* initialize file_info structure */ memset(&info, 0, sizeof(info)); info.full_path = rsh_strdup(hash_file_path); info.file = file; file_info_set_print_path(&info, info.full_path); info.sums_flags = info.hc.hash_mask = RHASH_CRC32; info.hc.flags = HC_HAS_EMBCRC32; info.hc.embedded_crc32_be = crc32_be; res = verify_sums(&info); fflush(rhash_data.out); if(!rhash_data.interrupted) { if(res == 0) rhash_data.ok++; else if(res == -1 && errno == ENOENT) rhash_data.miss++; rhash_data.processed++; } free(info.full_path); file_info_destroy(&info); } else { log_warning(_("file name doesn't contain a CRC32: %s\n"), hash_file_path); return -1; } return 0; } /* initialize statistics */ rhash_data.processed = rhash_data.ok = rhash_data.miss = 0; rhash_data.total_size = 0; if(file->mode & FILE_IFSTDIN) { fd = stdin; hash_file_path = "<stdin>"; } else if( !(fd = rsh_fopen_bin(hash_file_path, "rb") )) { log_file_error(hash_file_path); return -1; } pos = strlen(hash_file_path)+16; ralign = str_set(buf, '-', (pos < 80 ? 80 - (int)pos : 2)); fprintf(rhash_data.out, _("\n--( Verifying %s )%s\n"), hash_file_path, ralign); fflush(rhash_data.out); rhash_timer_start(&timer); /* mark the directory part of the path, by setting the pos index */ if(chdir) { pos = strlen(hash_file_path); for(; pos > 0 && !IS_PATH_SEPARATOR(hash_file_path[pos]); pos--); if(IS_PATH_SEPARATOR(hash_file_path[pos])) pos++; } else pos = 0; /* read crc file line by line */ for(line_num = 0; fgets(buf, 2048, fd); line_num++) { char* line = buf; char* path_without_ext = NULL; /* skip unicode BOM */ if(line_num == 0 && buf[0] == (char)0xEF && buf[1] == (char)0xBB && buf[2] == (char)0xBF) line += 3; if(*line == 0) continue; /* skip empty lines */ if(is_binary_string(line)) { log_error(_("file is binary: %s\n"), hash_file_path); if(fd != stdin) fclose(fd); return -1; } /* skip comments and empty lines */ if(IS_COMMENT(*line) || *line == '\r' || *line == '\n') continue; memset(&info, 0, sizeof(info)); if(!hash_check_parse_line(line, &info.hc, !feof(fd))) continue; if(info.hc.hash_mask == 0) continue; info.print_path = info.hc.file_path; info.sums_flags = info.hc.hash_mask; /* see if crc file contains a hash sum without a filename */ if(info.print_path == NULL) { char* point; path_without_ext = rsh_strdup(hash_file_path); point = strrchr(path_without_ext, '.'); if(point) { *point = '\0'; file_info_set_print_path(&info, path_without_ext); } } if(info.print_path != NULL) { file_t file_to_check; int is_absolute = IS_PATH_SEPARATOR(info.print_path[0]); IF_WINDOWS(is_absolute = is_absolute || (info.print_path[0] && info.print_path[1] == ':')); /* if filename shall be prepended by a directory path */ if(pos && !is_absolute) { size_t len = strlen(info.print_path); info.full_path = (char*)rsh_malloc(pos + len + 1); memcpy(info.full_path, hash_file_path, pos); strcpy(info.full_path + pos, info.print_path); } else { info.full_path = rsh_strdup(info.print_path); } memset(&file_to_check, 0, sizeof(file_t)); file_to_check.path = info.full_path; rsh_file_stat(&file_to_check); info.file = &file_to_check; /* verify hash sums of the file */ res = verify_sums(&info); fflush(rhash_data.out); rsh_file_cleanup(&file_to_check); file_info_destroy(&info); if(rhash_data.interrupted) { free(path_without_ext); break; } /* update statistics */ if(res == 0) rhash_data.ok++; else if(res == -1 && errno == ENOENT) rhash_data.miss++; rhash_data.processed++; } free(path_without_ext); } time = rhash_timer_stop(&timer); fprintf(rhash_data.out, "%s\n", str_set(buf, '-', 80)); print_check_stats(); if(rhash_data.processed != rhash_data.ok) rhash_data.error_flag = 1; if(opt.flags & OPT_SPEED && rhash_data.processed > 1) { print_time_stats(time, rhash_data.total_size, 1); } rhash_data.processed = 0; res = ferror(fd); /* check that crc file has been read without errors */ if(fd != stdin) fclose(fd); return (res == 0 ? 0 : -1); }
void FPGAKernelSetSimFile(FPGAKernel *self, char *sim_file) { char sim_full_path[MAX_STRING_SIZE]; FPGAKernelGetFullPath(self, sim_file, sim_full_path, MAX_STRING_SIZE); self->sim_file = str_set(NULL, sim_full_path); }
void FPGAKernelSetName(FPGAKernel *self, char *name) { self->kernel_name = str_set(NULL, name); }
int main(int argc, char const *argv[]) { /* str_new */ str_t string = str_new(); /* str_set */ str_set(string, " %d%d%d", 1, 2, 3); /* str_append */ str_append(string, "appending end"); /* str_println */ str_println(string); /* str_reverse */ str_reverse(string); str_println(string); /* str_length */ printf("size before trimming:\t%zu\n", str_length(string)); /* str_trim */ str_trim(string); printf("size after trimming:\t%zu\n", str_length(string)); /* str_substr */ str_t substr = str_substr(string, 0, 3); printf("substr before swap:\t"); str_println(substr); printf("string before swap:\t"); str_println(string); str_swap(substr, string); printf("substr after swap:\t"); str_println(substr); printf("string after swap:\t"); str_println(string); printf("is string empty?\t%s\n", str_isempty(string) ? "Yes" : "No"); printf("is substr equal to string?\t%s\n", str_compare(substr, string) ? "No" : "Yes"); /* str_readFromFile */ str_readFromFile(string, "neostring.c"); printf("string size: %zu\t", str_length(string)); printf("string capacity: %zu\n", string->capacity); str_set(string, "ok"); printf("Before trimToSize():\n"); printf("string size: %zu\t", str_length(string)); printf("string capacity: %zu\n", string->capacity); /* str_trimToSize */ str_trimToSize(string); printf("After trimToSize():\n"); printf("string size: %zu\t", str_length(string)); printf("string capacity: %zu\n", string->capacity); str_set(string, "hello, world"); str_println(string); printf("%zu\n", string->size); printf("string has prefix ello?\t%s\n", str_hasPrefix(string, "ello") ? "Yes" : "No"); printf("%zu\n", string->size); printf("string has suffix orld?\t%s\n", str_hasSuffix(string, "orld") ? "Yes" : "No"); /* str_clone */ str_t clone = str_clone(string); /* str_toupper */ str_toupper(clone); /* str_writeToFile */ str_writeToFile(clone, "./test.txt"); /* str_destroy */ str_destroy(string); str_destroy(clone); str_destroy(substr); return 0; }
/* get the next line of input, normalize end of line termination (i.e. convert "\r", "\r\n" and "\n\r" to "\n" Calls the new_line_cb call back and returns the pointer to the null-terminated text data in Str *line, including the final "\n". Returns NULL on end of file. */ char *SrcFile_getline( SrcFile *self ) { int c, c1; Bool found_newline; char *line; /* clear result string */ str_clear( self->line ); /* check for line stack */ if ( ! List_empty( self->line_stack ) ) { line = List_pop( self->line_stack ); /* we own the string now and need to release memory */ str_set( self->line, line ); m_free( line ); /* dont increment line number as we are still on same file input line */ return str_data(self->line); } /* check for EOF condition */ if ( self->file == NULL ) return NULL; /* read characters */ found_newline = FALSE; while ( ! found_newline && ( c = getc( self->file ) ) != EOF ) { switch ( c ) { case '\r': case '\n': c1 = getc( self->file ); if ( ( c1 == '\r' || c1 == '\n' ) && /* next char also newline */ c1 != c ) /* "\r\n" or "\n\r" */ { /* c1 will be discarded */ } else /* not composite newline - push back */ { if ( c1 != EOF ) { ungetc( c1, self->file ); /* push back except EOF */ } } /* normalize newline and fall through to default */ found_newline = TRUE; c = '\n'; default: str_append_char( self->line, c ); } } /* terminate string if needed */ if ( str_len(self->line) > 0 && ! found_newline ) str_append_char( self->line, '\n' ); /* signal new line, even empty one, to show end line in list */ self->line_nr++; call_new_line_cb( self->filename, self->line_nr, str_data(self->line) ); /* check for end of file even if EOF found, we need to return any chars in line first */ if ( str_len(self->line) > 0 ) { return str_data(self->line); } else { /* EOF - close file */ myfclose( self->file ); /* close input */ self->file = NULL; // call_new_line_cb( NULL, 0, NULL ); return NULL; /* EOF */ } }
void X86ThreadSetName(X86Thread *self, char *name) { self->name = str_set(self->name, name); }