static int config_channel(struct timer *tr, char **arg_text) { char *which_text = get_arg(arg_text); char *value_text = get_arg(arg_text); address_t which; address_t value; if (!(which_text && value_text)) { printc_err("timer: config: expected channel and value\n"); return -1; } if (expr_eval(which_text, &which) < 0) { printc_err("timer: can't parse channel number: %s\n", which_text); return -1; } if (expr_eval(value_text, &value) < 0) { printc_err("timer: can't parse channel value: %s\n", value_text); return -1; } if (which > tr->size) { printc_err("timer: invalid channel number: %d\n", which); return -1; } trigger_capture(tr, which, tr->ctls[which] & CCI, value); return 0; }
/* * Eval an expression tree. Calls the given function on each node, * passing it the given context & the name; return value is &/|/! of * results of evaluating atoms. * * No short circuiting ever occurs. fn must return 0 or 1 (otherwise * our mixing of C's bitwise & boolean here may give surprises). */ static int expr_eval(struct nvlist *expr, int (*fn)(const char *, void *), void *context) { int lhs, rhs; switch (expr->nv_int) { case FX_ATOM: return ((*fn)(expr->nv_name, context)); case FX_NOT: return (!expr_eval(expr->nv_next, fn, context)); case FX_AND: lhs = expr_eval(expr->nv_ptr, fn, context); rhs = expr_eval(expr->nv_next, fn, context); return (lhs & rhs); case FX_OR: lhs = expr_eval(expr->nv_ptr, fn, context); rhs = expr_eval(expr->nv_next, fn, context); return (lhs | rhs); } panic("expr_eval %d", expr->nv_int); return (0); }
static void *hvm_str_replace(const char *method, hefesto_common_list_ctx **string_var, hefesto_type_t *otype, hefesto_var_list_ctx **lo_vars, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions) { size_t offset = 0, outsz; char *regex_arg, *pattern_arg; char *replaced_buffer = NULL; const char *m; char errors[HEFESTO_MAX_BUFFER_SIZE]; void *usr_regex, *usr_pattern, *result; hefesto_type_t etype = HEFESTO_VAR_TYPE_STRING; here_search_program_ctx *search_program; *otype = HEFESTO_VAR_TYPE_INT; result = hefesto_mloc(sizeof(hefesto_int_t)); *(hefesto_int_t *)result = 0; if (string_var == NULL || (*string_var) == NULL || (*string_var)->data == NULL) return result; for (m = method; *m != '(' && *m != 0; m++); regex_arg = get_arg_from_call(m+1, &offset); pattern_arg = get_arg_from_call(m+1, &offset); usr_regex = expr_eval(regex_arg, lo_vars, gl_vars, functions, &etype, &outsz); if ((search_program = here_compile(usr_regex, errors)) != NULL) { usr_pattern = expr_eval(pattern_arg, lo_vars, gl_vars, functions, &etype, &outsz); *(hefesto_int_t *)result = here_replace_string((*string_var)->data, search_program, usr_pattern, &replaced_buffer, &outsz); free((*string_var)->data); (*string_var)->data = (char *) hefesto_mloc(outsz+1); (*string_var)->dsize = outsz; memset((*string_var)->data, 0, outsz+1); strncpy((*string_var)->data, replaced_buffer, outsz+1); free(replaced_buffer); free(usr_pattern); del_here_search_program_ctx(search_program); } else { hlsc_info(HLSCM_MTYPE_RUNTIME, HLSCM_SYN_ERROR_INVAL_REGEX, errors); } free(usr_regex); return result; }
static int cmd_hexout(cproc_t cp, char **arg) { device_t dev = cproc_device(cp); stab_t stab = cproc_stab(cp); char *off_text = get_arg(arg); char *len_text = get_arg(arg); char *filename = *arg; int off; int length; struct hexout_data hexout; if (!(off_text && len_text && *filename)) { fprintf(stderr, "hexout: need offset, length and filename\n"); return -1; } if (expr_eval(stab, off_text, &off) < 0 || expr_eval(stab, len_text, &length) < 0) return -1; if (hexout_start(&hexout, filename) < 0) return -1; while (length) { uint8_t buf[128]; int count = length; if (count > sizeof(buf)) count = sizeof(buf); printf("Reading %d bytes from 0x%04x...\n", count, off); if (dev->readmem(dev, off, buf, count) < 0) { perror("hexout: can't read memory"); goto fail; } if (hexout_feed(&hexout, off, buf, count) < 0) goto fail; length -= count; off += count; } if (hexout_flush(&hexout) < 0) goto fail; if (fclose(hexout.file) < 0) { perror("hexout: error on close"); return -1; } return 0; fail: fclose(hexout.file); unlink(filename); return -1; }
static int config_channel(struct timer *tr, char **arg_text) { char *which_text = get_arg(arg_text); char *value_text = get_arg(arg_text); address_t which; address_t value; int oldval; uint16_t edge_flags = 0; if (!(which_text && value_text)) { printc_err("timer: config: expected channel and value\n"); return -1; } if (expr_eval(which_text, &which) < 0) { printc_err("timer: can't parse channel number: %s\n", which_text); return -1; } if (expr_eval(value_text, &value) < 0) { printc_err("timer: can't parse channel value: %s\n", value_text); return -1; } if (which > tr->size) { printc_err("timer: invalid channel number: %d\n", which); return -1; } oldval = tr->ctls[which] & CCI; tr->ctls[which] &= ~CCI; if (value) tr->ctls[which] |= CCI; if (oldval && !value) edge_flags |= CM1; if (!oldval && value) edge_flags |= CM0; printc_dbg("Timer channel %d: %s => %s\n", which, oldval ? "H" : "L", value ? "H" : "L"); if ((tr->ctls[which] & edge_flags) && (tr->ctls[which] & CAP)) { if (tr->ctls[which] & CCIFG) { printc_dbg("Timer capture overflow\n"); tr->ctls[which] |= COV; } else { printc_dbg("Timer capture interrupt triggered\n"); tr->ccrs[which] = tr->tar; tr->ctls[which] |= CCIFG; } } return 0; }
static int cmd_md(cproc_t cp, char **arg) { stab_t stab = cproc_stab(cp); device_t dev = cproc_device(cp); char *off_text = get_arg(arg); char *len_text = get_arg(arg); int offset = 0; int length = 0x40; if (!off_text) { fprintf(stderr, "md: offset must be specified\n"); return -1; } if (expr_eval(stab, off_text, &offset) < 0) { fprintf(stderr, "md: can't parse offset: %s\n", off_text); return -1; } if (len_text) { if (expr_eval(stab, len_text, &length) < 0) { fprintf(stderr, "md: can't parse length: %s\n", len_text); return -1; } } else if (offset + length > 0x10000) { length = 0x10000 - offset; } if (offset < 0 || length <= 0 || (offset + length) > 0x10000) { fprintf(stderr, "md: memory out of range\n"); return -1; } while (length) { uint8_t buf[128]; int blen = length > sizeof(buf) ? sizeof(buf) : length; if (dev->readmem(dev, offset, buf, blen) < 0) return -1; cproc_hexdump(cp, offset, buf, blen); offset += blen; length -= blen; } return 0; }
static void expr_term(struct num_exp_ctx *ctx, unsigned int *vp) { char *tok; tok = expr_get(ctx); if (*tok == '(') { expr_eval(ctx, vp, 1); expr_expect(ctx, ')'); } else if (isdigit(*tok)) { char *ep; *vp = strtoul(tok, &ep, 0); if (*ep) expr_fail(ctx); } else if (*tok == '$') { sc_macro_t *mac; char *argv[32]; int argc; if (!(mac = find_macro(ctx->state->profile, tok + 1))) expr_fail(ctx); argc = build_argv(ctx->state, "<expr>", mac->value, argv, 32); if (argc < 0 || get_uint_eval(ctx->state, argc, argv, vp) < 0) expr_fail(ctx); } else { parse_error(ctx->state, "Unexpected token \"%s\" in expression", tok); expr_fail(ctx); } }
/* * We have finished reading everything. Tack the objects down: calculate * selection. */ int fixobjects(void) { struct objects *oi; struct nvlist *flathead, **flatp; int err, sel; err = 0; for (oi = allobjects; oi != NULL; oi = oi->oi_next) { /* Optional: see if it is to be included. */ if (oi->oi_optx != NULL) { flathead = NULL; flatp = &flathead; sel = expr_eval(oi->oi_optx, oi->oi_flags & OI_NEEDSFLAG ? fixfsel : fixsel, &flatp); oi->oi_optf = flathead; if (!sel) continue; } oi->oi_flags |= OI_SEL; } return (err); }
static int isearch_addr(const char *term, char **arg, struct isearch_query *q) { int which = toupper(*term) == 'S' ? ISEARCH_SRC_ADDR : ISEARCH_DST_ADDR; const char *addr_text; address_t addr; if (q->flags & which) { printc_err("isearch: address already specified\n"); return -1; } addr_text = get_arg(arg); if (!addr_text) { printc_err("isearch: address expected\n"); return -1; } if (expr_eval(addr_text, &addr) < 0) return -1; q->flags |= which; if (which == ISEARCH_SRC_ADDR) q->insn.src_addr = addr; else q->insn.dst_addr = addr; return 0; }
size_t value_size(struct value *val, struct value_dict *arguments) { if (val->size != (size_t)-1) return val->size; if (val->type->type != ARGTYPE_ARRAY) return val->size = type_sizeof(val->inferior, val->type); struct value length; if (expr_eval(val->type->u.array_info.length, val, arguments, &length) < 0) return (size_t)-1; size_t l; int o = value_extract_word(&length, (long *)&l, arguments); value_destroy(&length); if (o < 0) return (size_t)-1; size_t elt_size = type_sizeof(val->inferior, val->type->u.array_info.elt_type); if (elt_size == (size_t)-1) return (size_t)-1; return val->size = elt_size * l; }
int cmd_set(char **arg) { char *reg_text = get_arg(arg); char *val_text = get_arg(arg); int reg; address_t value = 0; address_t regs[DEVICE_NUM_REGS]; if (!(reg_text && val_text)) { printc_err("set: must specify a register and a value\n"); return -1; } reg = dis_reg_from_name(reg_text); if (reg < 0) { printc_err("set: unknown register: %s\n", reg_text); return -1; } if (expr_eval(val_text, &value) < 0) { printc_err("set: can't parse value: %s\n", val_text); return -1; } if (device_getregs(regs) < 0) return -1; regs[reg] = value; if (device_setregs(regs) < 0) return -1; show_regs(regs); return 0; }
/*********************************************************************** * should_stop * * Check whether or not the condition (bp / skipcount) of a break/watch * point are met. */ static BOOL should_stop(int bpnum) { struct dbg_breakpoint* bp = &dbg_curr_process->bp[bpnum]; if (bp->condition != NULL) { struct dbg_lvalue lvalue = expr_eval(bp->condition); if (lvalue.type.id == dbg_itype_none) { /* * Something wrong - unable to evaluate this expression. */ dbg_printf("Unable to evaluate expression "); expr_print(bp->condition); dbg_printf("\nTurning off condition\n"); break_add_condition(bpnum, NULL); } else if (!types_extract_as_integer(&lvalue)) { return FALSE; } } if (bp->skipcount > 0) bp->skipcount--; return bp->skipcount == 0; }
int cmd_mw(char **arg) { char *off_text = get_arg(arg); char *byte_text; address_t offset = 0; address_t length = 0; uint8_t buf[1024]; if (!off_text) { printc_err("md: offset must be specified\n"); return -1; } if (expr_eval(off_text, &offset) < 0) { printc_err("md: can't parse offset: %s\n", off_text); return -1; } while ((byte_text = get_arg(arg))) { if (length >= sizeof(buf)) { printc_err("md: maximum length exceeded\n"); return -1; } buf[length++] = strtoul(byte_text, NULL, 16); } if (!length) return 0; if (device_writemem(offset, buf, length) < 0) return -1; return 0; }
int ast_assign_execute( struct ast_assign *a, time_t stoptime ) { int result; if(a->expr) { char *value; char *word; value = expr_eval(a->expr,stoptime); if(value) { word = ast_bareword_execute(a->line,value); if(word) { ftsh_error(FTSH_ERROR_COMMAND,a->line,"%s=%s",a->name->text,word); if(buffer_save(a->name->text,word)) { result=1; } else { ftsh_error(FTSH_ERROR_FAILURE,a->line,"couldn't store variable '%s': %s",a->name->text,strerror(errno)); result=0; } free(word); } else { result = 0; } free(value); } else { result=0; } } else { ftsh_error(FTSH_ERROR_COMMAND,a->line,"%s=",a->name->text); buffer_delete(a->name->text); result = 1; } return result; }
static int cmd_set(cproc_t cp, char **arg) { device_t dev = cproc_device(cp); stab_t stab = cproc_stab(cp); char *reg_text = get_arg(arg); char *val_text = get_arg(arg); int reg; int value = 0; uint16_t regs[DEVICE_NUM_REGS]; if (!(reg_text && val_text)) { fprintf(stderr, "set: must specify a register and a value\n"); return -1; } reg = dis_reg_from_name(reg_text); if (reg < 0) { fprintf(stderr, "set: unknown register: %s\n", reg_text); return -1; } if (expr_eval(stab, val_text, &value) < 0) { fprintf(stderr, "set: can't parse value: %s\n", val_text); return -1; } if (dev->getregs(dev, regs) < 0) return -1; regs[reg] = value; if (dev->setregs(dev, regs) < 0) return -1; cproc_regs(cp, regs); return 0; }
static char * ast_expr_list_execute( int linenum, struct expr *e, time_t stoptime ) { char *line=0; char *v; while(e) { v = expr_eval(e,stoptime); if(v) { if(expr_is_list(e)) { int length = strlen(v); memcpy(v,&v[1],length-2); v[length-2] = 0; } if(line) { line = string_combine_multi(line," ",v,0); } else { line = v; } e=e->next; continue; } else { if(line) { free(line); line = 0; } break; } } return line; }
static void *hvm_if(hefesto_command_list_ctx *cmd, hefesto_var_list_ctx **lo_vars, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions, hefesto_int_t *should_return) { void *test, *result = NULL; hefesto_type_t etype = HEFESTO_VAR_TYPE_INT; size_t osize; hefesto_command_list_ctx *cmd_p = NULL, *tmp_cmd_p = NULL, *lst_cmd_p = NULL; if (cmd->sub != NULL) { cmd_p = cmd->sub; tmp_cmd_p = NULL; } else { cmd_p = cmd->next; /* tmp_cmd_p = cmd_p->next; cmd_p->next = NULL; */ lst_cmd_p = get_last_cmd_to_exec(cmd_p); if (lst_cmd_p != NULL) { tmp_cmd_p = lst_cmd_p->next; lst_cmd_p->next = NULL; } } test = expr_eval(cmd->expr, lo_vars, gl_vars, functions, &etype, &osize); if (test && *(hefesto_int_t *)test) { result = hvm_exec_command_list(cmd_p, lo_vars, gl_vars, functions, should_return); hvm_last_if_test = *(hefesto_int_t *)test; } else { hvm_last_if_test = 0; } /*else { if (cmd->sub != NULL) { if (cmd->next && cmd->next->instruction == HEFESTO_ELSE) { else_cmd_p = cmd->next; } } else { if (tmp_cmd_p && tmp_cmd_p->instruction == HEFESTO_ELSE) { else_cmd_p = tmp_cmd_p; } } if (else_cmd_p) { result = hvm_else(else_cmd_p, lo_vars, gl_vars, functions, should_return); } }*/ free(test); if (tmp_cmd_p != NULL) { lst_cmd_p->next = tmp_cmd_p; } return result; }
int cmd_dis(char **arg) { char *off_text = get_arg(arg); char *len_text = get_arg(arg); address_t offset = 0; address_t length = 0x40; uint8_t *buf; if (!off_text) { printc_err("dis: offset must be specified\n"); return -1; } if (expr_eval(off_text, &offset) < 0) { printc_err("dis: can't parse offset: %s\n", off_text); return -1; } if (len_text) { if (expr_eval(len_text, &length) < 0) { printc_err("dis: can't parse length: %s\n", len_text); return -1; } } else if (offset < 0x10000 && offset + length > 0x10000) { length = 0x10000 - offset; } buf = malloc(length); if (!buf) { pr_error("dis: couldn't allocate memory"); return -1; } if (device_readmem(offset, buf, length) < 0) { free(buf); return -1; } reader_set_repeat("dis 0x%x 0x%x", offset + length, length); disassemble(offset, buf, length, device_default->power_buf); free(buf); return 0; }
int cmd_md(char **arg) { char *off_text = get_arg(arg); char *len_text = get_arg(arg); address_t offset = 0; address_t length = 0x40; if (!off_text) { printc_err("md: offset must be specified\n"); return -1; } if (expr_eval(off_text, &offset) < 0) { printc_err("md: can't parse offset: %s\n", off_text); return -1; } if (len_text) { if (expr_eval(len_text, &length) < 0) { printc_err("md: can't parse length: %s\n", len_text); return -1; } } else if (offset < 0x10000 && offset + length > 0x10000) { length = 0x10000 - offset; } reader_set_repeat("md 0x%x 0x%x", offset + length, length); while (length) { uint8_t buf[4096]; int blen = length > sizeof(buf) ? sizeof(buf) : length; if (device_readmem(offset, buf, blen) < 0) return -1; hexdump(offset, buf, blen); offset += blen; length -= blen; } return 0; }
/* * We have finished reading everything. Tack the files down: calculate * selection and counts as needed. Check that the object files built * from the selected sources do not collide. */ int fixfiles(void) { struct files *fi, *ofi; struct nvlist *flathead, **flatp; int err, sel; err = 0; for (fi = allfiles; fi != NULL; fi = fi->fi_next) { /* Skip files that generated counted-device complaints. */ if (fi->fi_flags & FI_HIDDEN) continue; /* Optional: see if it is to be included. */ if (fi->fi_optx != NULL) { flathead = NULL; flatp = &flathead; sel = expr_eval(fi->fi_optx, fi->fi_flags & FI_NEEDSCOUNT ? fixcount : fi->fi_flags & FI_NEEDSFLAG ? fixfsel : fixsel, &flatp); fi->fi_optf = flathead; if (!sel) continue; } /* We like this file. Make sure it generates a unique .o. */ if (ht_insert(basetab, fi->fi_base, fi)) { if ((ofi = ht_lookup(basetab, fi->fi_base)) == NULL) panic("fixfiles ht_lookup(%s)", fi->fi_base); /* * If the new file comes from a different source, * allow the new one to override the old one. */ if (fi->fi_nvpath != ofi->fi_nvpath) { if (ht_replace(basetab, fi->fi_base, fi) != 1) panic("fixfiles ht_replace(%s)", fi->fi_base); ofi->fi_flags &= ~FI_SEL; ofi->fi_flags |= FI_HIDDEN; } else { xerror(fi->fi_srcfile, fi->fi_srcline, "object file collision on %s.o, from %s", fi->fi_base, fi->fi_nvpath->nv_name); xerror(ofi->fi_srcfile, ofi->fi_srcline, "here is the previous file: %s", ofi->fi_nvpath->nv_name); err = 1; } } fi->fi_flags |= FI_SEL; } return (err); }
static int cmd_dis(cproc_t cp, char **arg) { device_t dev = cproc_device(cp); stab_t stab = cproc_stab(cp); char *off_text = get_arg(arg); char *len_text = get_arg(arg); int offset = 0; int length = 0x40; uint8_t buf[4096]; if (!off_text) { fprintf(stderr, "dis: offset must be specified\n"); return -1; } if (expr_eval(stab, off_text, &offset) < 0) { fprintf(stderr, "dis: can't parse offset: %s\n", off_text); return -1; } if (len_text) { if (expr_eval(stab, len_text, &length) < 0) { fprintf(stderr, "dis: can't parse length: %s\n", len_text); return -1; } } else if (offset + length > 0x10000) { length = 0x10000 - offset; } if (offset < 0 || length <= 0 || length > sizeof(buf) || (offset + length) > 0x10000) { fprintf(stderr, "dis: memory out of range\n"); return -1; } if (dev->readmem(dev, offset, buf, length) < 0) return -1; cproc_disassemble(cp, offset, (uint8_t *)buf, length); return 0; }
/* * We have finished reading some "files" file, either ../../conf/files * or ./files.$machine. Make sure that everything that is flagged as * needing a count is reasonable. (This prevents ../../conf/files from * depending on some machine-specific device.) */ void checkfiles(void) { struct files *fi, *last; last = NULL; for (fi = *unchecked; fi != NULL; last = fi, fi = fi->fi_next) if ((fi->fi_flags & FI_NEEDSCOUNT) != 0) (void)expr_eval(fi->fi_optx, checkaux, fi); if (last != NULL) unchecked = &last->fi_next; }
static void expr_eval(struct num_exp_ctx *ctx, unsigned int *vp, unsigned int pri) { unsigned int left, right, new_pri; char *tok, op; expr_term(ctx, &left); while (1) { tok = __expr_get(ctx, 1); if (tok == NULL) break; op = tok[0]; new_pri = 0; switch (op) { case '*': case '/': new_pri++; case '+': case '-': new_pri++; case '&': new_pri++; case '|': new_pri++; case ')': break; default: expr_fail(ctx); } if (new_pri < pri) { expr_unget(ctx, tok); break; } pri = new_pri; expr_eval(ctx, &right, new_pri + 1); switch (op) { case '*': left *= right; break; case '/': left /= right; break; case '+': left += right; break; case '-': left -= right; break; case '&': left &= right; break; case '|': left |= right; break; default: expr_fail(ctx); } } *vp = left; }
void port_set_value(port_t *port, int value) { if (port->transform_write) { /* temporarily set the port value to the new value, * so that the write transform expression takes the new * value into consideration when evaluating the result */ int old_value = port->value; port->value = value; value = expr_eval(port->transform_write); port->value = old_value; } port->write_value(port, value); }
char *hvm_str_format(const char *str, hefesto_var_list_ctx **lo_vars, hefesto_var_list_ctx **gl_vars, hefesto_func_list_ctx *functions) { hefesto_type_t etype = HEFESTO_VAR_TYPE_STRING; char *str_fmt; size_t sz; sz = 0; str_fmt = expr_eval((char *)str, lo_vars, gl_vars, functions, &etype, &sz); return str_fmt; }
int ast_return_execute( struct ast_return *s, time_t stoptime ) { char *value; value = expr_eval(s->expr,stoptime); if(value) { ftsh_error(FTSH_ERROR_STRUCTURE,s->line,"return value is %s",value); variable_rval_set(value); return 1; } else { ftsh_error(FTSH_ERROR_FAILURE,s->line,"couldn't compute return value"); return 0; } }
int cmd_eval(char **arg) { address_t addr; char name[MAX_SYMBOL_LENGTH]; if (expr_eval(*arg, &addr) < 0) { printc_err("=: can't parse: %s\n", *arg); return -1; } print_address(addr, name, sizeof(name), 0); printc("0x%05x = %s\n", addr, name); return 0; }
/* This is called during search once per each record. It walks the list * of nvpairs and decides if a field matches. */ static int ausearch_compare(auparse_state_t *au) { rnode *r; if (au->le == NULL) return 0; r = aup_list_get_cur(au->le); if (r) { int res = expr_eval(au, r, au->expr); return res; } return 0; }
static int config_addr(address_t *addr, char **arg_text) { char *text = get_arg(arg_text); if (!text) { printc_err("console: config: expected address\n"); return -1; } if (expr_eval(text, addr) < 0) { printc_err("console: can't parse address: %s\n", text); return -1; } return 0; }
static int cmd_setbreak(cproc_t cp, char **arg) { device_t dev = cproc_device(cp); stab_t stab = cproc_stab(cp); char *addr_text = get_arg(arg); char *index_text = get_arg(arg); int index; int addr; if (!addr_text) { fprintf(stderr, "setbreak: address required\n"); return -1; } if (expr_eval(stab, addr_text, &addr) < 0) { fprintf(stderr, "setbreak: invalid address\n"); return -1; } if (index_text) { index = atoi(index_text); } else { int i; for (i = 0; i < dev->max_breakpoints; i++) { int enabled; if (!dev->getbrk(dev, i, &enabled, NULL) && !enabled) break; } if (i >= dev->max_breakpoints) { fprintf(stderr, "setbreak: all breakpoint slots are " "occupied\n"); return -1; } index = i; } printf("Setting breakpoint %d\n", index); dev->setbrk(dev, index, 1, addr); return 0; }