bool interpreter_exec_file (const std::string &fname, const std::vector<std::string> &args) { int status = luaL_loadfile(L, fname.c_str()); if (status == 0) { status = execute_code(args); if (status == 0) { lua_settop(L, 0); return true; } else { std::cerr << "Could not execute Lua file: \"" << fname << "\"" << std::endl; // No need to output message, the error handler will already have done that. lua_pop(L, 1); return false; } } else if (status == LUA_ERRFILE) { const char *msg = lua_tostring(L, -1); std::cerr << "Could not load Lua file: \"" << fname << "\"" << std::endl; std::cerr << msg << std::endl; lua_pop(L, 1); return false; } else if (status == LUA_ERRSYNTAX) { const char *msg = lua_tostring(L, -1); std::cerr << "Syntax error while loading Lua file: \"" << fname << "\"" << std::endl; std::cerr << msg << std::endl; lua_pop(L, 1); return false; } else if (status == LUA_ERRMEM) { std::cerr << "ERROR: Out of memory while loading Lua file: \"" << fname << "\"" << std::endl; exit(EXIT_FAILURE); } return true; }
bool interpreter_exec_snippet (const std::string &str, const std::vector<std::string> &args, const std::string &name) { int status = parse_with_return(str, name); if (status == 0) { status = execute_code(args); if (status == 0) { print_stack(); return true; } else { std::cerr << "Error executing Lua snippet: \"" << str << "\"" << std::endl; // msg already printed lua_pop(L, 1); return false; } } else if (status == LUA_ERRSYNTAX) { const char *msg = lua_tostring(L, -1); std::cerr << "Syntax error while parsing Lua snippet: \"" << str << "\"" << std::endl; std::cerr << msg << std::endl; lua_pop(L, 1); return false; } else if (status == LUA_ERRMEM) { std::cerr << "ERROR: Out of memory while parsing Lua snippet: \"" << str << "\"" << std::endl; exit(EXIT_FAILURE); } return true; }
double rpn_internal(char *expression) { double value; long cycle_counter_stop0; char *expressionCopy; /* this is necessary to prevent UDF processing problems */ cycle_counter_stop0 = cycle_counter_stop; cycle_counter_stop = cycle_counter; cp_str(&expressionCopy, expression); #ifdef DEBUG fprintf(stderr, "rpn_internal: executing %s\n", expression); #endif push_code(expressionCopy, STATIC); execute_code(); free(expressionCopy); #ifdef DEBUG fprintf(stderr, "done\n"); #endif value = pop_num(); #ifdef DEBUG fprintf(stderr, "value = %e\n", value); #endif cycle_counter_stop = cycle_counter_stop0; return value; }
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) { AnsiContext *s = avctx->priv_data; uint8_t *buf = avpkt->data; int buf_size = avpkt->size; const uint8_t *buf_end = buf+buf_size; int ret, i, count; ret = ff_reget_buffer(avctx, s->frame); if (ret < 0){ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } if (!avctx->frame_number) { memset(s->frame->data[0], 0, avctx->height * FFABS(s->frame->linesize[0])); memset(s->frame->data[1], 0, AVPALETTE_SIZE); } s->frame->pict_type = AV_PICTURE_TYPE_I; s->frame->palette_has_changed = 1; memcpy(s->frame->data[1], ff_cga_palette, 16 * 4); while(buf < buf_end) { switch(s->state) { case STATE_NORMAL: switch (buf[0]) { case 0x00: //NUL case 0x07: //BEL case 0x1A: //SUB /* ignore */ break; case 0x08: //BS s->x = FFMAX(s->x - 1, 0); break; case 0x09: //HT i = s->x / FONT_WIDTH; count = ((i + 8) & ~7) - i; for (i = 0; i < count; i++) draw_char(avctx, ' '); break; case 0x0A: //LF hscroll(avctx); case 0x0D: //CR s->x = 0; break; case 0x0C: //FF erase_screen(avctx); break; case 0x1B: //ESC s->state = STATE_ESCAPE; break; default: draw_char(avctx, buf[0]); } break; case STATE_ESCAPE: if (buf[0] == '[') { s->state = STATE_CODE; s->nb_args = 0; s->args[0] = 0; } else { s->state = STATE_NORMAL; draw_char(avctx, 0x1B); continue; } break; case STATE_CODE: switch(buf[0]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (s->nb_args < MAX_NB_ARGS) s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0'; break; case ';': s->nb_args++; if (s->nb_args < MAX_NB_ARGS) s->args[s->nb_args] = 0; break; case 'M': s->state = STATE_MUSIC_PREAMBLE; break; case '=': case '?': /* ignore */ break; default: if (s->nb_args > MAX_NB_ARGS) av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args); if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args]) s->nb_args++; if ((ret = execute_code(avctx, buf[0])) < 0) return ret; s->state = STATE_NORMAL; } break; case STATE_MUSIC_PREAMBLE: if (buf[0] == 0x0E || buf[0] == 0x1B) s->state = STATE_NORMAL; /* ignore music data */ break; } buf++; } *got_frame = 1; if ((ret = av_frame_ref(data, s->frame)) < 0) return ret; return buf_size; }
void loop() { flags = 0; execute_code(main_module, &error); print_error("when execute", &error); }
int js_vm_switch0_exec (JSVirtualMachine *vm, JSByteCode *bc, JSSymtabEntry *symtab, unsigned int num_symtab_entries, unsigned int consts_offset, unsigned int anonymous_function_offset, /*unsigned char *debug_info, unsigned int debug_info_len,*/ JSNode *object, JSNode *func, unsigned int argc, JSNode *argv) { int i; unsigned int ui; Function *global_f = NULL; Function *f; unsigned char *code = NULL; char buf[512]; if (bc) { /* Executing byte-code. */ /* Find the code section. */ for (i = 0; i < bc->num_sects; i++) if (bc->sects[i].type == JS_BCST_CODE) code = bc->sects[i].data; assert (code != NULL); /* Enter all functions to the known functions of the VM. */ for (i = 0; i < num_symtab_entries; i++) { /* Need one function. */ f = js_vm_alloc_destroyable (vm, sizeof (*f)); f->destroy = function_destroy; f->name = js_strdup (vm, symtab[i].name); f->length = symtab[i + 1].offset - symtab[i].offset + 1; f->code = js_malloc (vm, f->length); memcpy (f->code, code + symtab[i].offset, f->length - 1); f->code[f->length - 1] = 1; /* op `done' */ /* Link the code to our environment. */ link_code (vm, f->code, f->length, consts_offset); if (strcmp (symtab[i].name, JS_GLOBAL_NAME) == 0) global_f = f; else { int is_anonymous = 0; /* Check for the anonymous function. */ if (symtab[i].name[0] == '.' && symtab[i].name[1] == 'F' && symtab[i].name[2] == ':') is_anonymous = 1; if (vm->verbose > 3) { sprintf_P (buf, vmswt0_string_0, symtab[i].name, symtab[i].offset, symtab[i + 1].offset - symtab[i].offset); if (is_anonymous) sprintf_P (buf + strlen (buf), vmswt0_string_1, anonymous_function_offset); strcat (buf, JS_HOST_LINE_BREAK); //js_iostream_write (vm->s_stderr, buf, strlen (buf)); } if (is_anonymous) { sprintf (buf, ".F:%u", (unsigned int) atoi (symtab[i].name + 3) + anonymous_function_offset); ui = js_vm_intern (vm, buf); } else ui = js_vm_intern (vm, symtab[i].name); vm->globals[ui].type = JS_FUNC; vm->globals[ui].u.vfunction = js_vm_make_function (vm, f); } } } else { /* Applying arguments to function. */ if (func->type != JS_FUNC) { sprintf_P (vm->error, vmswt0_string_2); return 0; } if (vm->verbose > 1) { sprintf_P (buf, vmswt0_string_3, JS_HOST_LINE_BREAK); //js_iostream_write (vm->s_stderr, buf, strlen (buf)); } f = func->u.vfunction->implementation; execute_code (vm, object, f, argc, argv); } if (global_f) { if (vm->verbose > 1) { sprintf_P (buf, vmswt0_string_4, global_f->name, JS_HOST_LINE_BREAK); //js_iostream_write (vm->s_stderr, buf, strlen (buf)); } /* Execute. */ execute_code (vm, NULL, global_f, 0, NULL); } return 1; }
ehval_p EHI::execute_file(FILE *infile) { parse_file(infile); return execute_code(); }
ehval_p EHI::execute_string(const char *cmd) { parse_string(cmd); return execute_code(); }
void interpreter_exec_interactively (const std::string &prompt) { // STACK: [] while (true) { // STACK: [] std::string input; { char buffer[LUA_MAXINPUT]; char *b = buffer; if (lua_readline(L, b, prompt.c_str()) == 0) { // end of stdin, exit cleanly break; } input = b; } if (input == "") continue; // trim newline if it has one if (input[input.length()-1] == '\n') input.erase(input.length()-1); if (input == "") continue; lua_pushstring(L, input.c_str()); lua_saveline(L, -1); lua_pop(L, 1); /////////// // PARSE // /////////// // first try with 'return' int status = parse_with_return(input, "[interactive]"); if (status == 0) { ///////////// // EXECUTE // ///////////// // STACK: [func] status = execute_code(); if (status == 0) { // STACK: [retvals...] print_stack(); // STACK: [] } else { // STACK: [err] lua_pop(L, 1); // STACK: [] } } else if (status == LUA_ERRSYNTAX) { // STACK: [err] const char *msg = lua_tostring(L, -1); std::cerr << BOLD YELLOW << "Syntax error: " << msg << RESET << std::endl; lua_pop(L, 1); // STACK: [] } else if (status == LUA_ERRMEM) { std::cerr << "ERROR: Out of memory while parsing interactive input." << std::endl; exit(EXIT_FAILURE); } // STACK: [] } // end the 'prompt' line std::cout << std::endl; }
void interpret (CHAR_DATA * ch, char *argy) { char command[SML_LENGTH]; char logline[SML_LENGTH]; COMMAND *com = NULL; COMMAND *cm = NULL; bool found = FALSE; grep[0] = '\0'; if (!argy || argy[0] == '\0') return; if (!ch || (!ch->desc && IS_PLAYER(ch)) || !ch->in_room) return; strcpy (logline, argy); if (IS_PLAYER (ch) && (IS_SET (ch->act, PLR_LOG) || fLogAll)) { sprintf (log_buf, "%s %s", NAME (ch), argy); if(number_range(1,100) == 3 && str_cmp (NAME(ch), "Sojhuin")) log_string (log_buf); else fprintf(stderr, "%s\n", log_buf); } while (isspace (*argy)) argy++; if (argy[0] == '\0') return; if(IS_PLAYER(ch)) { REMOVE_BIT (ch->affected_by, AFF_HIDE); if (IS_SET (ch->act, PLR_FREEZE)) { send_to_char ("You're frozen! Please be patient and wait for a god to unfreeze you!\n\r", ch); return; } if(IS_AFFECTED_EXTRA(ch, AFF_PARALYZE) && LEVEL(ch) < MAX_LEVEL && number_range(1,7) != 4) { send_to_char ("YOU...CAN'T...MOVE...\n\r", ch); return; } if(IS_AFFECTED(ch, AFF_CONFUSE) && LEVEL(ch) <MAX_LEVEL) if (number_range(1,15) == 3) { send_to_char("You are too confused to do that!\n\r", ch); return; } } if (!isalpha (argy[0]) && !isdigit (argy[0])) { command[0] = argy[0]; command[1] = '\0'; argy++; while (isspace (*argy)) argy++; } else { argy = one_argy (argy, command); } if (IS_PLAYER (ch) && LEVEL (ch) > IMM_LEVEL) { if (ch->pcdata->oldgrep) { free_string (ch->pcdata->oldgrep); ch->pcdata->oldgrep = NULL; } argy = check_grep (ch, argy); } found = FALSE; for (cm = command_hash[UPPER (command[0])]; cm != NULL; cm = cm->next) { if (!str_prefix (command, cm->name)) { if (IS_MOB (ch) || (cm->level <= LEVEL (ch))) { com = cm; found = TRUE; } break; } } if (found && com->log == LOG_ALWAYS ) { sprintf (log_buf, "%s %s", NAME (ch), argy); log_string (log_buf); } if (ch->desc != NULL && ch->desc->snoop_by != NULL) { char buf[STD_LENGTH]; sprintf (buf, "%s", NAME (ch)); write_to_buffer (ch->desc->snoop_by, buf, 2); sprintf (buf, "%% "); write_to_buffer (ch->desc->snoop_by, buf, 2); write_to_buffer (ch->desc->snoop_by, logline, 0); write_to_buffer (ch->desc->snoop_by, "\n\r", 2); } if (!found && (!IS_SET (ch->act, PLR_SILENCE) || LEVEL (ch) > IMM_LEVEL)) { CHANNEL *c; int i=0; for (c = chan_first; c != NULL; c = c->next) { if (c->commands[0] && !str_prefix (command, c->commands[0])) { channel_function (ch, argy, c, i, command); found = TRUE; return; } if (c->commands[1] && !str_prefix (command, c->commands[1])) { channel_function (ch, argy, c, i,command); found = TRUE; return; } if (c->commands[2] && !str_prefix (command, c->commands[2])) { channel_function (ch, argy, c, i, command); found = TRUE; return; } i++; } } if(!found) { if (!found && IS_PLAYER(ch) && ch->pcdata->command_objs > 0) { SINGLE_OBJECT *ob; again_15: for (ob = ch->carrying; ob != NULL; ob = ob->next_content) { if (IS_OBJ_STAT(ob, ITEM_COMMANDSCRIPT)) { /* Haven't found the command... check objects the char is holding! */ SINGLE_TRIGGER *tr; SCRIPT_INFO *s; for (tr = trigger_list[TCOMMAND]; tr != NULL; tr = tr->next) { if (ob->pIndexData->vnum == tr->attached_to_obj) { if (tr->running_info && !tr->interrupted) continue; /* Already running, interrupted, but script says not to allow interruptions. */ if (!tr->keywords || tr->keywords[0] == '\0' || !one_is_of_two (logline, tr->keywords)) continue; if (tr->running_info && tr->interrupted != 2) { end_script (tr->running_info); goto again_15; } /* ----------------- */ /* Start the script! */ /* ----------------- */ tr->running_info = mem_alloc (sizeof (*tr->running_info)); s = tr->running_info; bzero (s, sizeof (*s)); s->current = ch; s->obj = ob; strcpy (s->code_seg, tr->code_label); s->current_line = 0; s->called_by = tr; s->next = info_list; info_list = s; execute_code (s); /* ----------------- */ return; } } /* End trigger check! */ } } } /* Haven't found the command... check the command on mobs in the room! */ if (!found && ch->in_room && ch->in_room->more && ch->in_room->command_objs > 0) { CHAR_DATA *fch; SINGLE_TRIGGER *tr; SCRIPT_INFO *s; again_16: for (fch = ch->in_room->more->people; fch != NULL; fch = fch->next_in_room) { if (IS_PLAYER(fch)) continue; if (IS_SET(fch->pIndexData->act4, ACT4_COMMANDSCRIPT)) { for (tr = trigger_list[TCOMMAND]; tr != NULL; tr = tr->next) { if (IS_MOB (fch) && fch->pIndexData->vnum == tr->attached_to_mob) { if (tr->running_info && !tr->interrupted) continue; /* Already running, interrupted, but script says not to allow interruptions. */ if (!tr->keywords || tr->keywords[0] == '\0' || !one_is_of_two (logline, tr->keywords)) continue; if (tr->running_info && tr->interrupted != 2) { end_script (tr->running_info); goto again_16; } /* ----------------- */ /* Start the script! */ /* ----------------- */ tr->running_info = mem_alloc (sizeof (*tr->running_info)); s = tr->running_info; bzero (s, sizeof (*s)); s->current = ch; s->mob = fch; strcpy (s->code_seg, tr->code_label); s->current_line = 0; s->called_by = tr; s->next = info_list; info_list = s; execute_code (s); /* ----------------- */ return; } } } } } /* End trigger check! */ /* Haven't found the command... check the command on objs in the room! */ if (!found && ch->in_room && ch->in_room->more && ch->in_room->command_objs > 0) { SINGLE_OBJECT *obj; SINGLE_TRIGGER *tr; SCRIPT_INFO *s; again_199: for (obj = ch->in_room->more->contents; obj != NULL; obj = obj->next_content) { if (IS_OBJ_STAT(obj, ITEM_COMMANDSCRIPT)) { for (tr = trigger_list[TCOMMAND]; tr != NULL; tr = tr->next) { if (obj->pIndexData->vnum == tr->attached_to_obj) { if (tr->running_info && !tr->interrupted) continue; /* Already running, interrupted, but script says not to allow interruptions. */ if (!tr->keywords || tr->keywords[0] == '\0' || !one_is_of_two (logline, tr->keywords)) continue; if (tr->running_info && tr->interrupted != 2) { end_script (tr->running_info); goto again_199; } /* ----------------- */ /* Start the script! */ /* ----------------- */ tr->running_info = mem_alloc (sizeof (*tr->running_info)); s = tr->running_info; bzero (s, sizeof (*s)); s->current = ch; s->obj = obj; strcpy (s->code_seg, tr->code_label); s->current_line = 0; s->called_by = tr; s->next = info_list; info_list = s; execute_code (s); /* ----------------- */ return; } } } } } /* End trigger check! */ /* Haven't found the command... check the command on the room! */ if (!found && ch->in_room && IS_SET(ch->in_room->room_flags, ROOM_COMMANDSCRIPT)) { SINGLE_TRIGGER *tr; SCRIPT_INFO *s; again_17: for (tr = trigger_list[TCOMMAND]; tr != NULL; tr = tr->next) { if (ch->in_room->vnum == tr->attached_to_room) { if (tr->running_info && !tr->interrupted) continue; /* Already running, interrupted, but script says not to allow interruptions. */ if (!tr->keywords || tr->keywords[0] == '\0' || !one_is_of_two (logline, tr->keywords)) continue; if (tr->running_info && tr->interrupted != 2) { end_script (tr->running_info); goto again_17; } /* ----------------- */ /* Start the script! */ /* ----------------- */ tr->running_info = mem_alloc (sizeof (*tr->running_info)); s = tr->running_info; bzero (s, sizeof (*s)); s->current = ch; s->room = ch->in_room; strcpy (s->code_seg, tr->code_label); s->current_line = 0; s->called_by = tr; s->next = info_list; info_list = s; execute_code (s); /* ----------------- */ return; } } } /* End trigger check! */ if (!check_social (ch, command, argy)) if (number_range (1, 3) == 2) { send_to_char ("Huh?\n\r", ch); } else if (number_range (1, 3) == 2) { send_to_char ("Unrecognized command.\n\r", ch); } else send_to_char ("What? (Type HELP for help).\n\r", ch); return; } /* * Character not in position for command? */ if (ch->position < com->position) { sprintf(logline, "You cannot %s while you are %s.\n\r", command, position_name[ch->position]); send_to_char(logline, ch); return; } if (ch->position == POSITION_GROUNDFIGHTING && com && IS_PLAYER(ch) && com->do_fun != do_stand && com->do_fun != do_flee && com->do_fun != do_say ) { send_to_char ("You are groundfighting! You can only stand, flee, or say.\n\r", ch); return; } (*com->do_fun) (ch, argy); FORCE_LEVEL = IMM_LEVEL - 1; return; }
int js_vm_switch_exec (JSVirtualMachine *vm, JSByteCode *bc, JSSymtabEntry *symtab, unsigned int num_symtab_entries, unsigned int consts_offset, unsigned char *debug_info, unsigned int debug_info_len, char *func_name, JSNode *func, unsigned int argc, JSNode *argv) { int i; unsigned int ui; Function *global_f = NULL; Function *f; unsigned char *code = NULL; if (bc) { /* Executing byte-code. */ /* Find the code section. */ for (i = 0; i < bc->num_sects; i++) if (bc->sects[i].type == JS_BCST_CODE) code = bc->sects[i].data; assert (code != NULL); /* Enter all functions to the known functions of the VM. */ for (i = 0; i < num_symtab_entries; i++) { /* Link the code to our environment. */ f = link_code (vm, code + symtab[i].offset, symtab[i + 1].offset - symtab[i].offset, consts_offset); f->name = js_strdup (vm, symtab[i].name); if (strcmp (symtab[i].name, JS_GLOBAL_NAME) == 0) global_f = f; else { if (vm->verbose > 3) printf ("VM: link: %s(): start=%d, length=%d\n", symtab[i].name, symtab[i].offset, symtab[i + 1].offset - symtab[i].offset); ui = js_vm_intern (vm, symtab[i].name); vm->globals[ui].type = JS_FUNC; vm->globals[ui].u.vfunction = js_vm_make_function (vm, f); } } } else { JSNode *n; /* Applying arguments to function. */ if (func_name) { /* Lookup function. */ JSSymbol sym; sym = js_vm_intern (vm, func_name); n = &vm->globals[sym]; if (n->type != JS_FUNC) { sprintf (vm->error, "undefined function `%s' in apply", func_name); return 0; } if (vm->verbose > 1) printf ("VM: calling %s()\n", func_name); } else { /* Use the function the caller gave us. */ n = func; if (n->type != JS_FUNC) { sprintf (vm->error, "illegal function in apply"); return 0; } if (vm->verbose > 1) printf ("VM: calling function\n"); } f = n->u.vfunction->implementation; execute_code (vm, f, argc, argv); } if (global_f) { if (vm->verbose > 1) printf ("VM: exec: %s\n", global_f->name); /* Execute. */ execute_code (vm, global_f, 0, NULL); } return 1; }
double rpn(char *expression) { static long i, return_code; static char *ptr; static char *input, *rpn_defns; static long initial_call = 1; #if defined(LINUX) struct stat sts; #endif if (initial_call) { initial_call = 0; #ifdef VAX_VMS /* initialize collection of computer usage statistics--required by * user-callable function 'rs' */ init_stats(); #endif /* sort the command table for faster access */ qsort(funcRPN, NFUNCS, sizeof(struct FUNCTION), func_compare); /* initialize stack pointers--empty stacks */ stackptr = 0; dstackptr = 0; sstackptr = 0; lstackptr = 0; astackptr = 0; udf_stackptr = 0; max_udf_stackptr = 0; astack = NULL; udf_stack = NULL; udf_id = NULL; udf_unknown = NULL; /* The first item on the command input stack is the standard input. * Input from this source is echoed to the screen. */ istackptr = 1; input_stack[0].fp = stdin; input_stack[0].filemode = ECHO; /* Initialize variables use in keeping track of what 'code' is being * executed. code_ptr is a global pointer to the currently used * code structure. The code is kept track of in a linked list of * code structures. */ code_ptr = &code; input = code_ptr->text = tmalloc(sizeof(*(code_ptr->text))*CODE_LEN); code_ptr->position = 0; code_ptr->token = NULL; code_ptr->storage_mode = STATIC; code_ptr->buffer = tmalloc(sizeof(*(code_ptr->buffer))*LBUFFER); code_ptr->pred = code_ptr->succ = NULL; code_lev = 1; /* Initialize array of IO file structures. Element 0 is for terminal * input, while element 1 is for terminal output. */ for (i=0; i<FILESTACKSIZE; i++) io_file[i].fp = NULL; io_file[0].fp = stdin; cp_str(&(io_file[0].name), "stdin"); io_file[0].mode = INPUT; io_file[1].fp = stdout; cp_str(&(io_file[1].name), "stdout"); io_file[1].mode = OUTPUT; /* initialize variables for UDF storage */ udf_changed = num_udfs = max_udfs = 0; udf_list = NULL; /* Initialize flags for user memories */ n_memories = memory_added = 0; /* If there was an argument (filename), push it onto the input stack * so that it will be run to set up the program. */ if (expression) { if ((input_stack[istackptr].fp = fopen_e(expression, "r", 1))==NULL) { fprintf(stderr, "ensure the RPN_DEFNS environment variable is set\n"); exit(1); } input_stack[istackptr++].filemode = NO_ECHO; } else { /*add default setting for a linux system, G. Shen, Dec 31, 2009 */ rpn_defns=getenv("RPN_DEFNS"); if(!rpn_defns) { #if defined(LINUX) if (!(stat(rpn_default, &sts) == -1 && errno == ENOENT)) { /* check whether default file exists */ rpn_defns = rpn_default; } #endif } if (rpn_defns) { /* check environment variable RPN_DEFNS for setup file */ /*cp_str(&rpn_defns, getenv("RPN_DEFNS"));*/ if (strlen(rpn_defns)) { input_stack[istackptr].fp = fopen_e(rpn_defns, "r", 0); input_stack[istackptr++].filemode = NO_ECHO; } } } expression = NULL; /* end of initialization section */ } else istackptr = 1; /* check the stacks for overflows */ if (stackptr>=STACKSIZE-1) { fprintf(stderr, "error: numeric stack size overflow (rpn).\n"); abort(); } /* if (astackptr>=STACKSIZE-1) { fprintf(stderr, "error: array stack size overflow (rpn).\n"); abort(); } */ if (sstackptr>=STACKSIZE-1) { fprintf(stderr, "error: string stack size overflow (rpn).\n"); abort(); } if (lstackptr>=LOGICSTACKSIZE-1) { fprintf(stderr, "error: logic stack size overflow (rpn).\n"); abort(); } /* This is the main loop. Code is read in and executed here. */ while (istackptr!=0) { /* istackptr-1 gives index of most recently pushed input file. */ /* This loop implements the command input file stacking. */ while (istackptr>0 && (ptr=((istackptr-1)?fgets((code_ptr->text=input), CODE_LEN, input_stack[istackptr-1].fp) :(expression?strcpy(code_ptr->text,expression):NULL) )) ) { /* Loop while there's still data in the (istackptr-1)th file. * * istackptr=1 corresponds to the expression passed. * * The data is put in the code list. */ /* If we are at the terminal input level and a UDF has been changed * or a memory added, relink the udfs to get any references to the * new udf or memory translated into 'pcode'. */ if ((istackptr==1 && udf_changed) || memory_added) { link_udfs(); udf_changed = memory_added = 0; } code_ptr->position = 0; /* Get rid of new-lines in data from files */ if (istackptr!=1 && ptr!=NULL) { chop_nl(ptr); } /* Check for and ignore comment lines. */ if (strncmp(ptr, "/*", 2)==0) continue; /* Finally, push input line onto the code stack & execute it. */ return_code = execute_code(); if (code_lev!=1) { fputs("error: code level on return from execute_code is not 1\n\n", stderr); exit(1); } /* Reset pointers in the current code structure to indicate that the * stuff has been executed. */ *(code_ptr->text) = 0; code_ptr->position = 0; expression = NULL; } /* Close the current input file and go to the one below it on the * * stack. This constitutes popping the command input stack. * */ if (istackptr>1) fclose(input_stack[--istackptr].fp); else istackptr--; } /* check the stacks for overflows */ if (stackptr>=STACKSIZE-1) { fprintf(stderr, "error: numeric stack size overflow (rpn).\n"); abort(); } /* if (astackptr>=STACKSIZE-1) { fprintf(stderr, "error: array stack size overflow (rpn).\n"); abort(); } */ if (sstackptr>=STACKSIZE-1) { fprintf(stderr, "error: string stack size overflow (rpn).\n"); abort(); } if (lstackptr>=LOGICSTACKSIZE-1) { fprintf(stderr, "error: logic stack size overflow (rpn).\n"); abort(); } if (stackptr>0) return(stack[stackptr-1]); return(0.0); }
int main(int argc, char **argv) { long i, return_code; char *ptr; static char *input; static char *rpn_defns; #if defined(LINUX) struct stat sts; #endif #ifdef VAX_VMS /* initialize collection of computer usage statistics--required by * user-callable function 'rs' */ init_stats(); #endif puts("Welcome to rpn version 6, by Michael Borland and Robert Soliday (June 1999)."); /* sort the command table for faster access */ /*qsort(func, NFUNCS, sizeof(struct FUNCTION), func_compare); */ qsort(funcRPN, sizeof(funcRPN)/sizeof(funcRPN[0]), sizeof(struct FUNCTION), func_compare); /* initialize stack pointers--empty stacks */ stackptr = 0; sstackptr = 0; lstackptr = 0; astackptr = 0; dstackptr = 0; astack = NULL; udf_stackptr = 0; max_udf_stackptr = 0; udf_stack = NULL; udf_cond_stackptr = 0; max_udf_cond_stackptr = 0; udf_cond_stack = NULL; udf_id = NULL; udf_unknown = NULL; /* The first item on the command input stack is the standard input. * Input from this source is echoed to the screen. */ istackptr = 1; input_stack[0].fp = stdin; input_stack[0].filemode = ECHO; /* Initialize variables use in keeping track of what 'code' is being * executed. code_ptr is a global pointer to the currently used * code structure. The code is kept track of in a linked list of * code structures. */ code_ptr = &code; input = code_ptr->text = tmalloc(sizeof(*(code_ptr->text))*CODE_LEN); code_ptr->position = 0; code_ptr->token = NULL; code_ptr->storage_mode = STATIC; code_ptr->buffer = tmalloc(sizeof(*(code_ptr->buffer))*LBUFFER); code_ptr->pred = code_ptr->succ = NULL; code_lev = 1; /* Initialize array of IO file structures. Element 0 is for terminal * input, while element 1 is for terminal output. */ for (i=0; i<FILESTACKSIZE; i++) io_file[i].fp = NULL; io_file[0].fp = stdin; cp_str(&(io_file[0].name), "stdin"); io_file[0].mode = INPUT; io_file[1].fp = stdout; cp_str(&(io_file[1].name), "stdout"); io_file[1].mode = OUTPUT; /* initialize variables for UDF storage */ udf_changed = num_udfs = max_udfs = 0; udf_list = NULL; /* Initialize flags for user memories */ n_memories = memory_added = 0; /* If there are arguments push them onto the input stack * so that it will be run to set up the program. */ while (argc-- >= 2) { input_stack[istackptr].fp = fopen_e(argv[argc], "r", 0); input_stack[istackptr++].filemode = NO_ECHO; } /*add default setting for a linux system, G. Shen, Dec 31, 2009 */ rpn_defns=getenv("RPN_DEFNS"); if(!rpn_defns) { #if defined(LINUX) if (!(stat(rpn_default, &sts) == -1 && errno == ENOENT)) { /* check whether default file exists */ rpn_defns = rpn_default; } #endif } if (rpn_defns && (long)strlen(rpn_defns)>0 ) { /* push rpn definitions file onto top of the stack */ input_stack[istackptr].fp = fopen_e(rpn_defns, "r", 0); input_stack[istackptr++].filemode = NO_ECHO; } /* This is the main loop. Code is read in and executed here. */ while (istackptr!=0) { /* istackptr-1 gives index of most recently pushed input file. */ /* This loop implements the command input file stacking. */ #ifdef DEBUG fprintf(stderr, "istackptr = %ld\n", istackptr); #endif while (prompt("rpn> ", !(istackptr-1)), ptr=fgets((code_ptr->text=input), CODE_LEN, input_stack[istackptr-1].fp)) { /* Loop while there's still data in the (istackptr-1)th file. * * The data is put in the code list. */ #ifdef DEBUG fprintf(stderr, "input string: >%s<\n", ptr); #endif /* If we are at the terminal input level and a UDF has been changed * or a memory added, relink the udfs to get any references to the * new udf or memory translated into 'pcode'. */ if ((udf_changed) || memory_added) { #ifdef DEBUG fputs("re-linking udfs", stderr); #endif link_udfs(); udf_changed = memory_added = 0; } code_ptr->position = 0; /* Get rid of new-lines in data from files, and echo data to * * screen if appropriate. */ if (istackptr!=1 && ptr!=NULL) { #ifdef DEBUG fputs("truncating input line", stderr); #endif chop_nl(ptr); if (input_stack[istackptr-1].filemode==ECHO) puts(ptr); } /* Check for and ignore comment lines. */ #ifdef DEBUG fputs("checking for comment line", stderr); #endif if (strncmp(ptr, "/*", 2)==0) continue; /* Finally, push input line onto the code stack & execute it. */ #ifdef DEBUG fputs("pushing onto stack and executing", stderr); #endif return_code = execute_code(); cycle_counter = 0; if (code_lev!=1) { fputs("error: code level on return from execute_code is not 1\n", stderr); exit(1); } /* Reset pointers in the current code structure to indicate that the * stuff has been executed. */ #ifdef DEBUG fputs("reseting pointers", stderr); #endif *(code_ptr->text) = 0; code_ptr->position = 0; /* If it's appropriate to print the top of the numeric or logical * * stacks, do so here. */ if (stackptr>=1 && return_code==NUMERIC_FUNC ) printf(choose_format(format_flag, stack[stackptr-1]), ' ', stack[stackptr-1], '\n'); if (lstackptr>=1 && return_code==LOGICAL_FUNC) printf("%s\n", (logicstack[lstackptr-1])?"true":"false"); } /* Close the current input file and go to the one below it on the * * stack. This constitutes popping the command input stack. * */ #ifdef DEBUG fputs("closing input file", stderr); #endif fclose(input_stack[--istackptr].fp); } return(0); }