interpret.c
interpret.c · 147.1 KB
From the archive lpmud-3.1.2-DR.tar.gz
Download the archive « Back to the archive contents
#include <varargs.h>
#include <stdio.h>
#include <setjmp.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/types.h> /* sys/types.h and netinet/in.h are here to enable include of comm.h below */
#include <sys/stat.h>
#ifdef MARK
#include <prof.h>
#endif
#include <memory.h>
#ifdef MARK
#define CASE(x) case x: MARK(x);
#else
#define CASE(x) case x:
#endif
#include "lint.h"
#include "lang.h"
#include "exec.h"
#include "interpret.h"
#include "config.h"
#include "object.h"
#include "wiz_list.h"
#include "instrs.h"
#include "patchlevel.h"
#include "comm.h"
#include "switch.h"
#ifdef RUSAGE /* Defined in config.h */
#include <sys/resource.h>
extern int getrusage PROT((int, struct rusage *));
#ifdef sun
extern int getpagesize();
#endif
#ifndef RUSAGE_SELF
#define RUSAGE_SELF 0
#endif
#endif
#if defined(__GNUC__) && !defined(lint)
#define INLINE /* inline */ /* Another time ! */
#else
#define INLINE
#endif
extern struct object *master_ob;
extern void print_svalue PROT((struct svalue *));
extern void say PROT((struct svalue *, struct vector *));
static struct svalue *sapply PROT((char *, struct object *, int));
static void do_trace PROT((char *, char *, char *));
static int apply_low PROT((char *, struct object *, int));
static int inter_sscanf PROT((int));
extern int remote_command;
static int strpref PROT((char *, char *));
static void add_attribute PROT((char *, struct object *, int, int));
static Attribute *find_attribute PROT((char *, struct object *));
static int query_attribute PROT((char *, struct object *));
static int get_attribute PROT((char *, struct object *));
static struct vector *get_attr_vector PROT((struct object *));
static void delete_attribute PROT((char *, struct object *));
extern struct object *obj_list_destruct;
extern struct object *previous_ob;
extern char *last_verb, *last_arg;
extern struct svalue const0, const1;
struct program *current_prog;
struct object *frame_ob;
extern int current_time;
extern struct object *current_heart_beat, *current_interactive;
static int tracedepth;
#define TRACE_CALL 1
#define TRACE_CALL_OTHER 2
#define TRACE_RETURN 4
#define TRACE_ARGS 8
#define TRACE_EXEC 16
#define TRACE_HEART_BEAT 32
#define TRACE_APPLY 64
#define TRACE_OBJNAME 128
#define TRACETST(b) (command_giver->interactive->trace_level & (b))
#define TRACEP(b) \
(command_giver && command_giver->interactive && TRACETST(b) && \
(command_giver->interactive->trace_prefix == 0 || \
(current_object && strpref(command_giver->interactive->trace_prefix, \
current_object->name))) )
#define TRACEHB (current_heart_beat == 0 || (command_giver->interactive->trace_level & TRACE_HEART_BEAT))
/*
* Inheritance:
* An object X can inherit from another object Y. This is done with
* the statement 'inherit "file";'
* The inherit statement will clone a copy of that file, call reset
* in it, and set a pointer to Y from X.
* Y has to be removed from the linked list of all objects.
* All variables declared by Y will be copied to X, so that X has access
* to them.
*
* If Y isn't loaded when it is needed, X will be discarded, and Y will be
* loaded separetly. X will then be reloaded again.
*/
extern int d_flag;
extern int current_line, eval_cost;
/*
* These are the registers used at runtime.
* The control stack saves registers to be restored when a function
* will return. That means that control_stack[0] will have almost no
* interesting values, as it will terminate execution.
*/
static char *pc; /* Program pointer. */
static struct svalue *fp; /* Pointer to first argument. */
static struct svalue *sp; /* Points to value of last push. */
static short *break_sp; /* Points to address to branch to
* at next F_BREAK */
static int function_index_offset; /* Needed for inheritance */
static int variable_index_offset; /* Needed for inheritance */
static struct svalue start_of_stack[EVALUATOR_STACK_SIZE];
struct svalue catch_value; /* Used to throw an error to a catch */
static struct control_stack control_stack[MAX_TRACE];
static struct control_stack *csp; /* Points to last element pushed */
/*
* May current_object shadow object 'ob' ? We rely heavily on the fact that
* function names are pointers to shared strings, which means that equality
* can be tested simply through pointer comparison.
*/
int validate_shadowing(ob)
struct object *ob;
{
int i, j;
struct program *shadow = current_object->prog, *victim = ob->prog;
struct svalue *ret;
if (current_object->shadowing)
error("shadow: Already shadowing.\n");
if (current_object->shadowed)
error("shadow: Can't shadow when shadowed.\n");
if (current_object->super)
error("The shadow must not reside inside another object.\n");
if (ob->shadowing)
error("Can't shadow a shadow.\n");
for (i=0; i < shadow->num_functions; i++) {
for (j=0; j < victim->num_functions; j++) {
if (shadow->functions[i].name != victim->functions[j].name)
continue;
if (victim->functions[j].type & TYPE_MOD_NO_MASK)
error("Illegal to shadow 'nomask' function \"%s\".\n",
victim->functions[j].name);
}
}
push_object(ob, "validate_shadowing");
ret = apply_master_ob("query_allow_shadow", 1);
if (!(ob->flags & O_DESTRUCTED) &&
ret && !(ret->type == T_NUMBER && ret->u.number == 0))
{
return 1;
}
return 0;
}
/*
* Information about assignments of values:
*
* There are three types of l-values: Local variables, global variables
* and vector elements.
*
* The local variables are allocated on the stack together with the arguments.
* the register 'frame_pointer' points to the first argument.
*
* The global variables must keep their values between executions, and
* have space allocated at the creation of the object.
*
* Elements in vectors are similar to global variables. There is a reference
* count to the whole vector, that states when to deallocate the vector.
* The elements consists of 'struct svalue's, and will thus have to be freed
* immediately when over written.
*/
/*
* Push an object pointer on the stack. Note that the reference count is
* incremented.
* A destructed object must never be pushed on the stack.
*/
INLINE
void push_object(ob, from)
struct object *ob;
char *from;
{
char buf[50];
sp++;
if (sp == &start_of_stack[EVALUATOR_STACK_SIZE])
fatal("stack overflow\n");
sp->type = T_OBJECT;
sp->u.ob = ob;
if (from)
sprintf(buf, "push_object (%s)", from);
else strcpy(buf, "push_object");
add_ref(ob, buf);
}
/*
* Push a number on the value stack.
*/
INLINE
void push_number(n)
int n;
{
sp++;
if (sp == &start_of_stack[EVALUATOR_STACK_SIZE])
fatal("stack overflow\n");
sp->type = T_NUMBER;
sp->u.number = n;
}
/*
* Push a string on the value stack.
*/
INLINE
void push_string(p, type)
char *p;
int type;
{
sp++;
if (sp == &start_of_stack[EVALUATOR_STACK_SIZE])
fatal("stack overflow\n");
sp->type = T_STRING;
sp->string_type = type;
switch(type) {
case STRING_MALLOC:
sp->u.string = string_copy(p);
break;
case STRING_SHARED:
sp->u.string = make_shared_string(p);
break;
case STRING_CONSTANT:
sp->u.string = p;
break;
}
}
/*
* Get address to a valid global variable.
*/
static INLINE struct svalue *find_value(num)
int num;
{
#ifdef DEBUG
if (num >= current_object->prog->num_variables) {
fatal("Illegal variable access %d(%d). See trace above.\n",
num, current_object->prog->num_variables);
}
#endif
return ¤t_object->variables[num];
}
/*
* Free the data that an svalue is pointing to. Not the svalue
* itself.
*/
void free_svalue(v, from)
struct svalue *v;
char *from;
{
switch(v->type) {
case T_STRING:
switch(v->string_type) {
case STRING_MALLOC:
free(v->u.string);
break;
case STRING_SHARED:
free_string(v->u.string);
break;
}
break;
case T_OBJECT:
{
char buf[80]; strcpy(buf, "free_svalue ");
if (from) { strcat(buf, "("); strcat(buf, from); strcat(buf, ")"); }
free_object(v->u.ob, buf);
break;
}
case T_POINTER:
free_vector(v->u.vec);
break;
case T_MAPPING:
free_mapping(v->u.map);
break;
}
*v = const0;
}
#ifndef COMPAT_MODE
/*
* Prepend a slash in front of a string.
*/
static char *add_slash(str)
char *str;
{
char *tmp;
tmp = xalloc(strlen(str)+2);
strcpy(tmp,"/"); strcat(tmp,str);
return tmp;
}
#endif
/*
* Assign to a svalue.
* This is done either when element in vector, or when to an identifier
* (as all identifiers are kept in a vector pointed to by the object).
*/
INLINE void assign_svalue_no_free(to, from)
struct svalue *to;
struct svalue *from;
{
#ifdef DEBUG
if (from == 0)
fatal("Null pointer to assign_svalue().\n");
#endif
*to = *from;
switch(from->type) {
case T_STRING:
switch(from->string_type) {
case STRING_MALLOC: /* No idea to make the string shared */
to->u.string = string_copy(from->u.string);
break;
case STRING_CONSTANT: /* Good idea to make it shared */
to->string_type = STRING_SHARED;
/* FALL THROUGH ! */
case STRING_SHARED: /* It already is shared */
to->u.string = make_shared_string(from->u.string);
break;
default:
fatal("Bad string type %d\n", from->string_type);
}
break;
case T_OBJECT:
add_ref(to->u.ob, "ass to var");
break;
case T_POINTER:
to->u.vec->ref++;
break;
case T_MAPPING:
to->u.map->ref++;
break;
}
}
INLINE void assign_svalue(dest, v)
struct svalue *dest;
struct svalue *v;
{
/* First deallocate the previous value. */
free_svalue(dest, "assign_svalue");
assign_svalue_no_free(dest, v);
}
void push_svalue(v)
struct svalue *v;
{
sp++;
assign_svalue_no_free(sp, v);
}
static INLINE void pop_stack() {
#ifdef DEBUG
if (sp < start_of_stack)
fatal("Stack underflow.\n");
#endif
free_svalue(sp, "pop_stack");
sp--;
}
/*
* Compute the address of an array element.
*/
static INLINE void push_indexed_lvalue()
{
struct svalue *vec, *item;
int ind;
vec = sp - 1;
if (vec->type == T_MAPPING)
{
struct mapping *m = vec->u.map;
vec = find_for_insert(m, sp);
pop_stack();
free_svalue(sp, "push_indexed_lvalue");
sp->type = T_LVALUE;
sp->u.lvalue = vec;
return;
}
ind = sp->u.number;
if (sp->type != T_NUMBER || ind < 0)
error("Illegal index\n");
pop_stack();
if (vec->type == T_STRING) {
static struct svalue one_character;
/* marion says: this is a crude part of code */
one_character.type = T_NUMBER;
if (ind > strlen(vec->u.string) || ind < 0)
one_character.u.number = 0;
else
one_character.u.number = vec->u.string[ind];
free_svalue(sp, "push_indexed_lvalue");
sp->type = T_LVALUE;
sp->u.lvalue = &one_character;
return;
}
if (vec->type != T_POINTER) error("Indexing on illegal type.\n");
if (ind >= vec->u.vec->size) error ("Index out of bounds\n");
item = &vec->u.vec->item[ind];
if (vec->u.vec->ref == 1) {
static struct svalue quickfix = { T_NUMBER };
/* marion says: but this is crude too */
/* marion blushes. */
assign_svalue (&quickfix, item);
item = &quickfix;
}
free_svalue(sp, "push_indexed_lvalue");
/* This will make 'vec' invalid to use */
sp->type = T_LVALUE;
sp->u.lvalue = item;
}
#ifdef OPCPROF
#define MAXOPC 512
static int opcount[MAXOPC];
#endif
/*
* Deallocate 'n' values from the stack.
*/
INLINE
void pop_n_elems(n)
int n;
{
#ifdef DEBUG
if (n < 0)
fatal("pop_n_elems: %d elements.\n", n);
#endif
for (; n>0; n--)
pop_stack();
}
void bad_arg(arg, instr)
int arg, instr;
{
error("Bad argument %d to %s()\n", arg, get_f_name(instr));
}
INLINE
static void push_control_stack(funp)
struct function *funp;
{
if (csp == &control_stack[MAX_TRACE-1])
error("Too deep recursion.\n");
csp++;
csp->funp = funp; /* Only used for tracebacks */
csp->ob = current_object;
csp->prev_ob = previous_ob;
csp->fp = fp;
csp->prog = current_prog;
/* csp->extern_call = 0; It is set by eval_instruction() */
csp->pc = pc;
csp->function_index_offset = function_index_offset;
csp->variable_index_offset = variable_index_offset;
csp->break_sp = break_sp;
}
/*
* Pop the control stack one element, and restore registers.
* extern_call must not be modified here, as it is used imediately after pop.
*/
static void pop_control_stack() {
#ifdef DEBUG
if (csp == control_stack - 1)
fatal("Popped out of the control stack");
#endif
current_object = csp->ob;
current_prog = csp->prog;
previous_ob = csp->prev_ob;
pc = csp->pc;
fp = csp->fp;
function_index_offset = csp->function_index_offset;
variable_index_offset = csp->variable_index_offset;
break_sp = csp->break_sp;
csp--;
}
/*
* Push a pointer to a vector on the stack. Note that the reference count
* is incremented. Newly created vectors normally have a reference count
* initialized to 1.
*/
INLINE void push_vector(v)
struct vector *v;
{
v->ref++;
sp++;
sp->type = T_POINTER;
sp->u.vec = v;
}
INLINE void push_mapping(m)
struct mapping *m;
{
m->ref++;
sp++;
sp->type = T_MAPPING;
sp->u.map = m;
}
/*
* Push a string on the stack that is already shared.
*/
static void INLINE push_shared_string(p)
char *p;
{
sp++;
sp->type = T_STRING;
sp->u.string = p;
sp->string_type = STRING_SHARED;
}
/*
* Push a string on the stack that is already malloced.
*/
static void INLINE push_malloced_string(p)
char *p;
{
sp++;
sp->type = T_STRING;
sp->u.string = p;
sp->string_type = STRING_MALLOC;
}
/*
* Push a string on the stack that is already constant.
*/
INLINE
void push_constant_string(p)
char *p;
{
sp++;
sp->type = T_STRING;
sp->u.string = p;
sp->string_type = STRING_CONSTANT;
}
static void do_trace_call(funp)
struct function *funp;
{
do_trace("Call direct ", funp->name, " ");
if (TRACEHB) {
if (TRACETST(TRACE_ARGS)) {
int i;
add_message(" with %d arguments: ", funp->num_arg);
for(i = funp->num_arg-1; i >= 0; i--) {
print_svalue(&sp[-i]);
add_message(" ");
}
}
add_message("\n");
}
}
/*
* Argument is the function to execute. If it is defined by inheritance,
* then search for the reall definition, and return it.
* There is a number of arguments on the stack. Normalize them and initialize
* local variables, so that the called function is pleased.
*/
static struct function *setup_new_frame(funp)
struct function *funp;
{
function_index_offset = 0;
variable_index_offset = 0;
while(funp->flags & NAME_INHERITED) {
function_index_offset +=
current_prog->inherit[funp->offset].function_index_offset;
variable_index_offset +=
current_prog->inherit[funp->offset].variable_index_offset;
current_prog =
current_prog->inherit[funp->offset].prog;
funp = ¤t_prog->functions[funp->function_index_offset];
}
/* Remove excessive arguments */
while(csp->num_local_variables > funp->num_arg) {
pop_stack();
csp->num_local_variables--;
}
/* Correct number of arguments and local variables */
while(csp->num_local_variables < funp->num_arg + funp->num_local) {
push_number(0);
csp->num_local_variables++;
}
tracedepth++;
if (TRACEP(TRACE_CALL)) {
do_trace_call(funp);
}
fp = sp - csp->num_local_variables + 1;
break_sp = (short*)(sp+1);
return funp;
}
static void break_point()
{
if (sp - fp - csp->num_local_variables + 1 != 0)
fatal("Bad stack pointer.\n");
}
/* marion
* maintain a small and inefficient stack of error recovery context
* data structures.
* This routine is called in three different ways:
* push=-1 Pop the stack.
* push=1 push the stack.
* push=0 No error occured, so the pushed value does not have to be
* restored. The pushed value can simply be popped into the void.
*
* The stack is implemented as a linked list of stack-objects, allocated
* from the heap, and deallocated when popped.
*/
void push_pop_error_context (push)
int push;
{
extern jmp_buf error_recovery_context;
extern int error_recovery_context_exists;
extern struct svalue catch_value;
static struct error_context_stack {
jmp_buf old_error_context;
int old_exists_flag;
struct control_stack *save_csp;
struct object *save_command_giver;
struct svalue *save_sp;
struct error_context_stack *next;
} *ecsp = 0, *p;
if (push == 1) {
/*
* Save some global variables that must be restored separately
* after a longjmp. The stack will have to be manually popped all
* the way.
*/
p = (struct error_context_stack *)xalloc (sizeof *p);
p->save_sp = sp;
p->save_csp = csp;
p->save_command_giver = command_giver;
memcpy (
(char *)p->old_error_context,
(char *)error_recovery_context,
sizeof error_recovery_context);
p->old_exists_flag = error_recovery_context_exists;
p->next = ecsp;
ecsp = p;
} else {
p = ecsp;
if (p == 0)
fatal("Catch: error context stack underflow");
if (push == 0) {
#ifdef DEBUG
if (csp != p->save_csp-1)
fatal("Catch: Lost track of csp");
#if 0
/*
* This test is not valid! The statement catch(exec("...")) will
* change the value of command_giver.
*/
if (command_giver != p->save_command_giver)
fatal("Catch: Lost track of command_giver");
#endif
#endif
} else {
/* push == -1 !
* They did a throw() or error. That means that the control
* stack must be restored manually here.
*/
csp = p->save_csp;
pop_n_elems (sp - p->save_sp);
command_giver = p->save_command_giver;
}
memcpy ((char *)error_recovery_context,
(char *)p->old_error_context,
sizeof error_recovery_context);
error_recovery_context_exists = p->old_exists_flag;
ecsp = p->next;
free ((char *)p);
}
}
static struct vector *append_vector(v, a)
struct vector *v;
struct svalue *a;
{
int i;
struct vector *new;
new = allocate_array(v->size+1);
for (i=0; i<v->size; ++i)
assign_svalue(&new->item[i], &v->item[i]);
assign_svalue(&new->item[i], a);
return new;
}
static struct vector *prepend_vector(v, a)
struct vector *v;
struct svalue *a;
{
int i;
struct vector *new;
new = allocate_array(v->size+1);
assign_svalue(&new->item[0], a);
for (i=0; i<v->size; ++i)
assign_svalue(&new->item[i+1], &v->item[i]);
return new;
}
/*
* When a vector is given as argument to an efun, all items has to be
* checked if there would be an destructed object.
* A bad problem currently is that a vector can contain another vector, so this
* should be tested too. But, there is currently no prevention against
* recursive vectors, which means that this can not be tested. Thus, the game
* may crash if a vector contains a vector that contains a destructed object
* and this top-most vector is used as an argument to an efun.
*/
/* The game won't crash when doing simple operations like assign_svalue
* on a destructed object. You have to watch out, of course, that you don't
* apply a function to it.
* to save space it is preferable that destructed objects are freed soon.
* amylaar
*/
void check_for_destr(v)
struct vector *v;
{
int i;
for (i=0; i < v->size; i++) {
if (v->item[i].type != T_OBJECT)
continue;
if (!(v->item[i].u.ob->flags & O_DESTRUCTED))
continue;
assign_svalue(&v->item[i], &const0);
}
}
/*
* Evaluate instructions at address 'p'. All program offsets are
* to current_prog->program. 'current_prog' must be setup before
* call of this function.
*
* There must not be destructed objects on the stack. The destruct_object()
* function will automatically remove all occurences. The effect is that
* all called efuns knows that they won't have destructed objects as
* arguments.
*/
#ifdef TRACE_CODE
int previous_instruction[60];
int stack_size[60];
char *previous_pc[60];
static int last;
#endif
static void eval_instruction(p)
char *p;
{
struct object *ob;
int i, num_arg;
int instruction;
#ifdef DEBUG
struct svalue *expected_stack;
#endif
struct svalue *argp;
/* Next F_RETURN at this level will return out of eval_instruction() */
csp->extern_call = 1;
pc = p;
again:
instruction = EXTRACT_UCHAR(pc);
#ifdef TRACE_CODE
previous_instruction[last] = instruction + F_OFFSET;
previous_pc[last] = pc;
stack_size[last] = sp - fp - csp->num_local_variables;
last = (last + 1) % (sizeof previous_instruction / sizeof (int));
#endif
pc++;
if (current_object && current_object->user)
current_object->user->cost++;
eval_cost++;
if (eval_cost > MAX_COST) {
printf("eval_cost too big %d\n", eval_cost);
eval_cost = 0;
error("Too long evaluation. Execution aborted.\n");
}
/*
* Execute current instruction. Note that all functions callable
* from LPC must return a value. This does not apply to control
* instructions, like F_JUMP.
*/
if (instrs[instruction].min_arg != instrs[instruction].max_arg) {
num_arg = EXTRACT_UCHAR(pc);
pc++;
if (num_arg > 0) {
if (instrs[instruction].type[0] != 0 &&
(instrs[instruction].type[0] & (sp-num_arg+1)->type) == 0) {
bad_arg(1, instruction + F_OFFSET);
}
}
if (num_arg > 1) {
if (instrs[instruction].type[1] != 0 &&
(instrs[instruction].type[1] & (sp-num_arg+2)->type) == 0) {
bad_arg(2, instruction + F_OFFSET);
}
}
} else {
num_arg = instrs[instruction].min_arg;
if (instrs[instruction].min_arg > 0) {
if (instrs[instruction].type[0] != 0 &&
(instrs[instruction].type[0] & (sp-num_arg+1)->type) == 0) {
bad_arg(1, instruction + F_OFFSET);
}
}
if (instrs[instruction].min_arg > 1) {
if (instrs[instruction].type[1] != 0 &&
(instrs[instruction].type[1] & (sp-num_arg+2)->type) == 0) {
bad_arg(2, instruction + F_OFFSET);
}
}
/*
* Safety measure. It is supposed that the evaluator knows
* the number of arguments.
*/
num_arg = -1;
}
#ifdef DEBUG
if (num_arg != -1) {
expected_stack = sp - num_arg + 1;
} else {
expected_stack = 0;
}
#endif
instruction += F_OFFSET;
#ifdef OPCPROF
if (instruction >= 0 && instruction < MAXOPC) opcount[instruction]++;
#endif
/*
* Execute the instructions. The number of arguments are correct,
* and the type of the two first arguments are also correct.
*/
if (TRACEP(TRACE_EXEC)) {
do_trace("Exec ", get_f_name(instruction), "\n");
}
switch(instruction) {
default:
fatal("Undefined instruction %s (%d)\n", get_f_name(instruction),
instruction);
/*NOTREACHED*/
CASE(F_REMOVE_SHADOW);
{
ob = current_object;
if (num_arg) { ob = sp->u.ob; pop_stack(); }
if (!ob || !ob->shadowing)
push_number(0);
else
{
if (ob->shadowed)
ob->shadowed->shadowing = ob->shadowing;
if (ob->shadowing)
ob->shadowing->shadowed = ob->shadowed;
ob->shadowing = ob->shadowed = 0;
push_object(ob, "F_REMOVE_SHADOW");
}
break;
}
CASE(F_REGEXP);
{
struct vector *v;
v = match_regexp((sp-1)->u.vec, sp->u.string);
pop_n_elems(2);
if (v == 0)
push_number(0);
else {
push_vector(v);
v->ref--; /* Will make ref count == 1 */
}
break;
}
CASE(F_SHADOW);
{
struct svalue *ret;
struct object *top;
top = ob = (sp-1)->u.ob;
if (sp->u.number == 0) {
ob = ob->shadowed;
pop_n_elems(2);
if (ob)
push_object(ob, "F_SHADOW");
else push_number(0);
break;
}
if (validate_shadowing(ob)) {
/*
* The shadow is entered first in the chain.
*/
while (ob->shadowed)
ob = ob->shadowed;
current_object->shadowing = ob;
ob->shadowed = current_object;
current_object->shadowed = 0;
pop_n_elems(2);
push_object(top, "F_SHADOW");
}
else
{
pop_n_elems(2);
push_number(0);
}
break;
}
CASE(F_POP_VALUE);
pop_stack();
break;
CASE(F_DUP);
sp++;
assign_svalue_no_free(sp, sp-1);
break;
CASE(F_JUMP_WHEN_ZERO);
{
unsigned short offset;
((char *)&offset)[0] = pc[0];
((char *)&offset)[1] = pc[1];
if (sp->type == T_NUMBER && sp->u.number == 0)
pc = current_prog->program + offset;
else
pc += 2;
pop_stack();
break;
}
CASE(F_JUMP);
{
unsigned short offset;
((char *)&offset)[0] = pc[0];
((char *)&offset)[1] = pc[1];
pc = current_prog->program + offset;
break;
}
CASE(F_JUMP_WHEN_NON_ZERO);
{
unsigned short offset;
((char *)&offset)[0] = pc[0];
((char *)&offset)[1] = pc[1];
if (sp->type == T_NUMBER && sp->u.number == 0)
pc += 2;
else
pc = current_prog->program + offset;
pop_stack();
break;
}
CASE(F_INDIRECT);
#ifdef DEBUG
if (sp->type != T_LVALUE)
fatal("Bad type to F_INDIRECT\n");
#endif
assign_svalue(sp, sp->u.lvalue);
/*
* Fetch value of a variable. It is possible that it is a variable
* that points to a destructed object. In that case, it has to
* be replaced by 0.
*/
if (sp->type == T_OBJECT && (sp->u.ob->flags & O_DESTRUCTED)) {
free_svalue(sp, "F_INDIRECT");
*sp = const0;
}
break;
CASE(F_IDENTIFIER);
sp++;
assign_svalue_no_free(sp, find_value((int)(EXTRACT_UCHAR(pc) +
variable_index_offset)));
pc++;
/*
* Fetch value of a variable. It is possible that it is a variable
* that points to a destructed object. In that case, it has to
* be replaced by 0.
*/
if (sp->type == T_OBJECT && (sp->u.ob->flags & O_DESTRUCTED)) {
free_svalue(sp, "F_ID");
*sp = const0;
}
break;
CASE(F_PUSH_IDENTIFIER_LVALUE);
sp++;
sp->type = T_LVALUE;
sp->u.lvalue = find_value((int)(EXTRACT_UCHAR(pc) +
variable_index_offset));
pc++;
break;
CASE(F_PUSH_INDEXED_LVALUE);
push_indexed_lvalue();
break;
CASE(F_INDEX_INSTR);
if ((sp-1)->type == T_MAPPING)
{
struct svalue *v;
v = find_in_mapping((sp-1)->u.map, sp);
pop_stack();
if (v)
assign_svalue(sp, v);
else
assign_svalue(sp, &const0);
}
else
{
push_indexed_lvalue();
assign_svalue_no_free(sp, sp->u.lvalue);
}
/*
* Fetch value of a variable. It is possible that it is a variable
* that points to a destructed object. In that case, it has to
* be replaced by 0.
*/
if (sp->type == T_OBJECT && (sp->u.ob->flags & O_DESTRUCTED)) {
free_svalue(sp, "F_INDEX_INSTR");
sp->type = T_NUMBER;
sp->u.number = 0;
}
break;
CASE(F_LOCAL_NAME);
sp++;
assign_svalue_no_free(sp, fp + EXTRACT_UCHAR(pc));
pc++;
/*
* Fetch value of a variable. It is possible that it is a variable
* that points to a destructed object. In that case, it has to
* be replaced by 0.
*/
if (sp->type == T_OBJECT && (sp->u.ob->flags & O_DESTRUCTED)) {
free_svalue(sp, "F_LOCAL_NAME");
*sp = const0;
}
break;
CASE(F_PUSH_LOCAL_VARIABLE_LVALUE);
sp++;
sp->type = T_LVALUE;
sp->u.lvalue = fp + EXTRACT_UCHAR(pc);
pc++;
break;
CASE(F_RETURN);
{
struct svalue sv;
sv = *sp--;
/*
* Deallocate frame and return.
*/
for (i=0; i < csp->num_local_variables; i++)
pop_stack();
sp++;
#ifdef DEBUG
if (sp != fp)
fatal("Bad stack at F_RETURN\n"); /* marion */
#endif
*sp = sv; /* This way, the same ref counts are maintained */
pop_control_stack();
tracedepth--;
if (TRACEP(TRACE_RETURN)) {
do_trace("Return", "", "");
if (TRACEHB) {
if (TRACETST(TRACE_ARGS)) {
add_message(" with value: ");
print_svalue(sp);
}
add_message("\n");
}
}
if (csp[1].extern_call) /* The control stack was popped just before */
return;
break;
}
CASE(F_BREAK_POINT);
break_point(); /* generated by lang.y when -d. Will check stack. */
break;
CASE(F_CHECK_REF);
{
int a = -1;
if (num_arg == 2)
{
if ((sp-1)->type == T_OBJECT)
a = (sp-1)->u.ob->ref;
}
else if (sp->type == T_OBJECT)
a = sp->u.ob->prog->ref;
else if (sp->type == T_POINTER)
a = sp->u.vec->ref;
pop_n_elems(num_arg);
push_number(a);
break;
}
CASE(F_CLONE_OBJECT);
ob = clone_object(sp->u.string);
pop_stack();
if (ob) {
push_object(ob, "F_CLONE_OBJECT");
} else {
push_number(0);
}
break;
CASE(F_AGGREGATE_ASSOC);
{
struct mapping *m;
unsigned short num;
((char *)&num)[0] = *pc++;
((char *)&num)[1] = *pc++;
m = load_mapping_from_aggregate(sp - num + 1, num);
pop_n_elems((int)num);
sp++;
sp->type = T_MAPPING;
sp->u.map = m;
break;
}
CASE(F_AGGREGATE);
{
struct vector *v;
unsigned short num;
((char *)&num)[0] = pc[0];
((char *)&num)[1] = pc[1];
pc += 2;
v = allocate_array((int)num);
for (i=0; i < num; i++)
assign_svalue_no_free(&v->item[i], sp + i - num + 1);
pop_n_elems((int)num);
sp++;
sp->type = T_POINTER;
sp->u.vec = v; /* Ref count already initialized */
break;
}
CASE(F_TAIL);
if (tail(sp->u.string))
assign_svalue(sp, &const1);
else
assign_svalue(sp, &const0);
break;
CASE(F_CALL_FUNCTION_BY_ADDRESS);
{
unsigned short func_index;
struct function *funp;
((char *)&func_index)[0] = pc[0];
((char *)&func_index)[1] = pc[1];
pc += 2;
func_index += function_index_offset;
/*
* Find the function in the function table. As the function may have
* been redefined by inheritance, we must look in the last table,
* which is pointed to by current_object.
*/
#ifdef DEBUG
if (func_index >= current_object->prog->num_functions)
fatal("Illegal function index\n");
#endif
/* NOT current_prog, which can be an inherited object. */
funp = ¤t_object->prog->functions[func_index];
if (funp->flags & NAME_UNDEFINED)
error("Undefined function: %s\n", funp->name);
/* Save all important global stack machine registers */
push_control_stack(funp); /* return pc is adjusted later */
/* This assigment must be done after push_control_stack() */
current_prog = current_object->prog;
/*
* If it is an inherited function, search for the real
* definition.
*/
csp->num_local_variables = EXTRACT_UCHAR(pc);
pc++;
funp = setup_new_frame(funp);
csp->pc = pc; /* The corrected return address */
pc = current_prog->program + funp->offset;
csp->extern_call = 0;
break;
}
CASE(F_SAVE_OBJECT);
save_object(current_object, sp->u.string);
/* The argument is returned */
break;
CASE(F_FIND_OBJECT);
ob = find_object2(sp->u.string);
pop_stack();
if (ob)
push_object(ob, "F_FIND_OBJECT");
else
push_number(0);
break;
CASE(F_FIND_PLAYER);
ob = find_living_object(sp->u.string, 1);
pop_stack();
if (!ob)
push_number(0);
else
push_object(ob, "F_FIND_PLAYER");
break;
CASE(F_DUMPOBJS);
{
int start, end;
if (num_arg == 1)
{
start = sp->u.number;
end = -1;
}
else
{
start = (sp-1)->u.number;
end = sp->u.number;
}
dumpobjs(start, end);
pop_n_elems(num_arg);
push_number(0);
break;
}
CASE(F_WRITE_FILE);
i = write_file((sp-1)->u.string, sp->u.string);
pop_n_elems(2);
push_number(i);
break; /* Return first argument */
CASE(F_READ_BYTES);
{
char *str;
struct svalue *arg = sp- num_arg + 1;
int start = 0, len = 0;
if (num_arg > 1)
start = arg[1].u.number;
if (num_arg == 3) {
if (arg[2].type != T_NUMBER)
bad_arg(2, instruction);
len = arg[2].u.number;
}
str = read_bytes(arg[0].u.string, start, len);
pop_n_elems(num_arg);
if (str == 0)
push_number(0);
else {
push_string(str, STRING_MALLOC);
free(str);
}
break;
}
CASE(F_WRITE_BYTES);
i = write_bytes((sp-2)->u.string, (sp-1)->u.number, sp->u.string);
pop_n_elems(3);
push_number(i);
break;
CASE(F_FILE_SIZE);
i = file_size(sp->u.string);
pop_stack();
push_number(i);
break;
CASE(F_FIND_LIVING);
ob = find_living_object(sp->u.string, 0);
pop_stack();
if (!ob)
push_number(0);
else
push_object(ob, "F_FIND_LIVING");
break;
CASE(F_TELL_OBJECT);
tell_object((sp-1)->u.ob, sp->u.string);
pop_stack(); /* Return first argument */
break;
CASE(F_RESTORE_OBJECT);
i = restore_object(current_object, sp->u.string);
pop_stack();
push_number(i);
break;
CASE(F_THIS_PLAYER);
if (num_arg && current_interactive &&
!(current_interactive->flags & O_DESTRUCTED) )
push_object(current_interactive, "F_THIS_PLAYER");
else if (command_giver && !(command_giver->flags & O_DESTRUCTED))
push_object(command_giver, "F_THIS_PLAYER");
else
push_number(0);
break;
#ifdef F_FIRST_INVENTORY
CASE(F_FIRST_INVENTORY);
ob = first_inventory(sp);
pop_stack();
if (ob)
push_object(ob, "F_FIRST_INVENTORY");
else
push_number(0);
break;
#endif /* F_FIRST_INVENTORY */
CASE(F_LIVING);
if (sp->u.ob->flags & O_ENABLE_COMMANDS)
assign_svalue(sp, &const1);
else
assign_svalue(sp, &const0);
break;
#ifdef F_GETUID
CASE(F_GETUID);
ob = sp->u.ob;
#ifdef DEBUG
if (ob->user == 0)
fatal("User is null pointer\n");
#endif
{ char *tmp;
tmp = ob->user->name;
pop_stack();
push_string(tmp, STRING_CONSTANT);
}
break;
#endif /* F_GETUID */
#ifdef F_GETEUID
CASE(F_GETEUID);
/*
* Are there any reasons to support this one in -o mode ?
*/
ob = sp->u.ob;
if (ob->eff_user) {
char *tmp;
tmp = ob->eff_user->name;
pop_stack();
push_string(tmp, STRING_CONSTANT);
}
else {
pop_stack();
push_number(0);
}
break;
#endif /* F_GETEUID */
#ifdef F_EXPORT_UID
CASE(F_EXPORT_UID);
if (current_object->eff_user==0)
error("Illegal to export uid.\n");
ob = sp->u.ob;
if (ob->eff_user) /* Only allowed to export when null */
break;
ob->user = current_object->eff_user;
break;
#endif /* F_EXPORT_UID */
#ifdef F_SETEUID
CASE(F_SETEUID);
{
struct svalue *ret;
if (sp->type == T_NUMBER) {
if (sp->u.number != 0)
bad_arg(1, F_SETEUID);
current_object->eff_user = 0;
pop_stack();
push_number(1);
break;
}
argp = sp;
if (argp->type != T_STRING)
bad_arg(1, F_SETEUID);
push_object(current_object, "F_SETEUID");
push_string(argp->u.string, STRING_CONSTANT);
ret = apply_master_ob("valid_seteuid", 2);
if (ret == 0 || ret->type != T_NUMBER || ret->u.number != 1) {
pop_stack();
push_number(0);
break;
}
current_object->eff_user = add_name(argp->u.string);
pop_stack();
push_number(1);
break;
}
#endif
#ifdef F_SETUID
CASE(F_SETUID);
setuid();
push_number(0);
break;
#endif
#ifdef F_CREATOR
CASE(F_CREATOR);
ob = sp->u.ob;
if (ob->user == 0) {
assign_svalue(sp, &const0);
} else {
pop_stack();
push_string(ob->user->name, STRING_CONSTANT);
}
break;
#endif
CASE(F_SHUTDOWN);
{
extern struct object *master_ob;
assert_master_ob_loaded();
if (current_object != master_ob)
error("shutdown() can only be called from master.c\n");
startshutdowngame();
push_number(0);
break;
}
CASE(F_EXPLODE);
{
struct vector *v;
v = explode_string((sp-1)->u.string, sp->u.string);
pop_n_elems(2);
if (v) {
push_vector(v); /* This will make ref count == 2 */
v->ref--;
} else {
push_number(0);
}
break;
}
CASE(F_FILTER_ARRAY);
{
struct vector *v;
struct svalue *arg;
arg = sp - num_arg + 1; ob = 0;
if (arg[2].type == T_OBJECT)
ob = arg[2].u.ob;
else if (arg[2].type == T_STRING)
ob = find_object(arg[2].u.string);
if (!ob)
error("Bad type argument 3 to filter_array()\n");
if (arg[0].type == T_POINTER) {
check_for_destr(arg[0].u.vec);
v = filter(arg[0].u.vec, arg[1].u.string, ob,
num_arg > 3 ? sp : (struct svalue *)0);
} else {
v = 0;
}
pop_n_elems(num_arg);
if (v) {
push_vector(v); /* This will make ref count == 2 */
v->ref--;
} else {
push_number(0);
}
break;
}
CASE(F_SET_BIT);
{
char *str;
int len, old_len, ind;
if (sp->u.number > MAX_BITS)
error("set_bit: too big bit number: %d\n", sp->u.number);
len = strlen((sp-1)->u.string);
old_len = len;
ind = sp->u.number/6;
if (ind >= len)
len = ind + 1;
str = xalloc(len+1);
str[len] = '\0';
if (old_len)
memcpy(str, (sp-1)->u.string, old_len);
if (len > old_len)
memset(str + old_len, ' ', len - old_len);
if (str[ind] > 0x3f + ' ' || str[ind] < ' ')
error("Illegal bit pattern in set_bit character %d\n", ind);
str[ind] = (str[ind] - ' ' | 1 << sp->u.number % 6) + ' ';
pop_n_elems(2);
sp++;
sp->u.string = str;
sp->string_type = STRING_MALLOC;
sp->type = T_STRING;
break;
}
CASE(F_CLEAR_BIT);
{
char *str;
int len, ind;
if (sp->u.number > MAX_BITS)
error("clear_bit: too big bit number: %d\n", sp->u.number);
len = strlen((sp-1)->u.string);
ind = sp->u.number/6;
if (ind >= len) {
/* Return first argument unmodified ! */
pop_stack();
break;
}
str = xalloc(len+1);
memcpy(str, (sp-1)->u.string, len+1); /* Including null byte */
if (str[ind] > 0x3f + ' ' || str[ind] < ' ')
error("Illegal bit pattern in clear_bit character %d\n", ind);
str[ind] = (str[ind] - ' ' & ~(1 << sp->u.number % 6)) + ' ';
pop_n_elems(2);
sp++;
sp->type = T_STRING;
sp->string_type = STRING_MALLOC;
sp->u.string = str;
break;
}
CASE(F_TEST_BIT);
{
int len;
len = strlen((sp-1)->u.string);
if (sp->u.number/6 >= len) {
pop_n_elems(2);
push_number(0);
break;
}
if ((sp-1)->u.string[sp->u.number/6] - ' ' & 1 << sp->u.number % 6) {
pop_n_elems(2);
push_number(1);
} else {
pop_n_elems(2);
push_number(0);
}
break;
}
CASE(F_QUERY_LOAD_AVERAGE);
push_string(query_load_av(), STRING_SHARED); /**MALLOC**/
break;
CASE(F_CATCH);
/*
* Catch/Throw - catch errors in system or other peoples routines.
*/
{
extern jmp_buf error_recovery_context;
extern int error_recovery_context_exists;
extern struct svalue catch_value;
unsigned short new_pc_offset;
/*
* Compute address of next instruction after the CATCH statement.
*/
((char *)&new_pc_offset)[0] = pc[0];
((char *)&new_pc_offset)[1] = pc[1];
pc += 2;
push_control_stack((struct function *)0);
csp->num_local_variables = 0; /* No extra variables */
csp->pc = current_prog->program + new_pc_offset;
csp->num_local_variables = (csp-1)->num_local_variables; /* marion */
/*
* Save some global variables that must be restored separately
* after a longjmp. The stack will have to be manually popped all
* the way.
*/
push_pop_error_context (1);
/* signal catch OK - print no err msg */
error_recovery_context_exists = 2;
if (setjmp(error_recovery_context)) {
/*
* They did a throw() or error. That means that the control
* stack must be restored manually here.
* Restore the value of expected_stack also. It is always 0
* for catch().
*/
#ifdef DEBUG
expected_stack = 0;
#endif
push_pop_error_context (-1);
pop_control_stack();
assign_svalue_no_free(++sp, &catch_value);
}
/* next error will return 1 by default */
assign_svalue(&catch_value, &const1);
break;
}
CASE(F_THROW);
/* marion
* the return from catch is now done by a 0 throw
*/
assign_svalue(&catch_value, sp--);
if (catch_value.type == T_NUMBER && catch_value.u.number == 0) {
/* We come here when no longjmp() was executed. */
pop_control_stack();
push_pop_error_context (0);
push_number(0);
} else throw_error(); /* do the longjump, with extra checks... */
break;
CASE(F_NOTIFY_FAIL);
set_notify_fail_message(sp->u.string);
/* Return the argument */
break;
CASE(F_QUERY_IDLE);
i = query_idle(sp->u.ob);
pop_stack();
push_number(i);
break;
CASE(F_IMPLODE);
{
char *str;
check_for_destr((sp-1)->u.vec);
str = implode_string((sp-1)->u.vec, sp->u.string);
pop_n_elems(2);
if (str) {
sp++;
sp->type = T_STRING;
sp->string_type = STRING_SHARED;
sp->u.string = make_shared_string(str); /**MALLOC**/
free(str);
} else {
push_number(0);
}
break;
}
CASE(F_QUERY_SNOOP);
{
struct svalue *arg1;
extern struct object *master_ob;
if (command_giver == 0 || sp->u.ob->interactive == 0) {
assign_svalue(sp, &const0);
break;
}
#ifdef COMPAT_MODE
arg1 = sapply("query_level", command_giver, 0);
if (arg1 == 0 || arg1->type != T_NUMBER ||
arg1->u.number < QUERY_SNOOP_LEVEL)
{
assign_svalue(sp, &const0);
break;
}
ob = query_snoop(sp->u.ob);
#else
assert_master_ob_loaded();
if (current_object == master_ob)
ob = query_snoop(sp->u.ob);
else
ob = 0;
#endif
pop_stack();
if (ob)
push_object(ob, "F_QUERY_SNOOP");
else
push_number(0);
break;
}
CASE(F_QUERY_IP_NUMBER);
CASE(F_QUERY_IP_NAME);
{
extern char *query_ip_number PROT((struct object *));
extern char *query_ip_name PROT((struct object *));
char *tmp;
if (num_arg == 1 && sp->type != T_OBJECT)
error("Bad optional argument to query_ip_number()\n");
if (instruction == F_QUERY_IP_NAME)
tmp = query_ip_name(num_arg ? sp->u.ob : 0);
else
tmp = query_ip_number(num_arg ? sp->u.ob : 0);
if (num_arg)
pop_stack();
if (tmp == 0)
push_number(0);
else
push_string(tmp, STRING_SHARED); /**MALLOC**/
break;
}
CASE(F_QUERY_HOST_NAME);
{
extern char *query_host_name();
char *tmp;
tmp = query_host_name();
if (tmp)
push_string(tmp, STRING_CONSTANT);
else
push_number(0);
break;
}
#ifdef F_NEXT_INVENTORY
CASE(F_NEXT_INVENTORY);
ob = sp->u.ob;
pop_stack();
if (ob->next_inv)
push_object(ob->next_inv, "F_NEXT_INVENTORY");
else
push_number(0);
break;
#endif /* F_NEXT_INVENTORY */
CASE(F_ALL_INVENTORY);
case F_CONTENTS:
{
struct vector *vec;
struct object *ob = current_object;
if (num_arg)
{
if (sp->type == T_OBJECT)
ob = sp->u.ob;
else
ob = find_object(sp->u.string);
pop_stack();
if (!ob)
{
push_number(0);
break;
}
}
vec = all_inventory(ob);
if (vec == 0) {
push_number(0);
} else {
push_vector(vec); /* This will make ref count == 2 */
vec->ref--;
}
break;
}
CASE(F_DEEP_INVENTORY);
{
struct vector *vec;
vec = deep_inventory(sp->u.ob, 0);
free_svalue(sp, "F_DEEP_INVENTORY");
sp->type = T_POINTER;
sp->u.vec = vec;
break;
}
CASE(F_ENVIRONMENT);
if (num_arg) {
ob = environment(sp);
pop_stack();
} else if (!(current_object->flags & O_DESTRUCTED)) {
ob = current_object->super;
} else
ob = 0;
if (ob)
{
push_object(ob, "F_ENVIRONMENT");
}
else
push_number(0);
break;
CASE(F_THIS_OBJECT);
if (current_object->flags & O_DESTRUCTED)
push_number(0);
else
push_object(current_object, "F_THIS_OBJECT");
break;
CASE(F_PREVIOUS_OBJECT);
if (previous_ob == 0 || (previous_ob->flags & O_DESTRUCTED))
push_number(0);
else
push_object(previous_ob, "F_PREVIOUS_OBJECT");
break;
#ifdef F_LOCALCMD
CASE(F_LOCALCMD);
print_local_commands();
push_number(0);
break;
#endif /* F_LOCALCMD */
CASE(F_SWAP);
(void)swap(sp->u.ob);
break;
CASE(F_TRACE);
{
int ot = -1;
if (command_giver && command_giver->interactive) {
struct svalue *arg;
push_constant_string("trace");
arg = apply_master_ob("query_player_level", 1);
if (arg && (arg->type != T_NUMBER || arg->u.number != 0)) {
ot = command_giver->interactive->trace_level;
command_giver->interactive->trace_level = sp->u.number;
}
}
pop_stack();
push_number(ot);
}
break;
CASE(F_TRACEPREFIX);
{
char *old = 0;
if (command_giver && command_giver->interactive) {
struct svalue *arg;
push_constant_string("trace");
arg = apply_master_ob("query_player_level",1);
if (arg && (arg->type != T_NUMBER || arg->u.number)) {
old = command_giver->interactive->trace_prefix;
if (sp->type == T_STRING) {
command_giver->interactive->trace_prefix =
make_shared_string(sp->u.string);
} else
command_giver->interactive->trace_prefix = 0;
}
}
pop_stack();
if (old) {
push_string(old, STRING_SHARED); /* Will incr ref count */
free_string(old);
} else {
push_number(0);
}
}
break;
CASE(F_TIME);
push_number(current_time);
break;
CASE(F_WIZLIST);
if (num_arg) {
wizlist(sp->u.string);
} else {
wizlist((char *)0);
push_number(0);
}
break;
#ifdef F_TRANSFER
CASE(F_TRANSFER);
{
struct object *dest;
if (sp->type == T_STRING) {
dest = find_object(sp->u.string);
if (dest == 0)
error("Object not found.\n");
} else {
dest = sp->u.ob;
}
i = transfer_object((sp-1)->u.ob, dest);
pop_n_elems(2);
push_number(i);
break;
}
#endif
#ifdef F_ADD_WORTH
CASE(F_ADD_WORTH);
if (strncmp(current_object->name, "obj/", 4) != 0 &&
strncmp(current_object->name, "std/", 4) != 0 &&
strncmp(current_object->name, "room/", 5) != 0)
error("Illegal call of add_worth.\n");
if (num_arg == 2) {
if (sp->u.ob->user)
sp->u.ob->user->total_worth += (sp-1)->u.number;
pop_stack();
} else {
if (previous_ob == 0)
break;
if (previous_ob->user)
previous_ob->user->total_worth += sp->u.number;
}
break;
#endif /* F_ADD_WORTH */
CASE(F_ADD);
{
struct svalue ret;
switch((sp-1)->type)
{
case T_STRING:
{
switch(sp->type)
{
case T_STRING:
{
char *res;
res=xalloc(strlen((sp-1)->u.string)+strlen(sp->u.string)+1);
(void)strcpy(res, (sp-1)->u.string);
(void)strcat(res, sp->u.string);
ret.type = T_STRING;
ret.string_type = STRING_SHARED;
ret.u.string = make_shared_string(res);
free(res);
break;
}
case T_NUMBER:
{
char buff[20];
char *res;
sprintf(buff, "%d", sp->u.number);
res = xalloc(strlen((sp-1)->u.string) + strlen(buff) + 1);
strcpy(res, (sp-1)->u.string);
strcat(res, buff);
ret.type = T_STRING;
ret.string_type = STRING_SHARED;
ret.u.string = make_shared_string(res);
free(res);
break;
}
case T_POINTER:
ret.type = T_POINTER;
ret.u.vec = prepend_vector(sp->u.vec, sp-1);
break;
default: error("Bad type argument to +. %d %d\n",
(sp-1)->type, sp->type);
}
break;
}
case T_NUMBER:
{
ret.type = sp->type;
switch(sp->type)
{
case T_NUMBER:
ret.u.number = sp->u.number + (sp-1)->u.number;
break;
case T_STRING:
{
char buff[20], *res;
sprintf(buff, "%d", (sp-1)->u.number);
res = xalloc(strlen(sp->u.string) + strlen(buff) + 1);
strcpy(res, buff);
strcat(res, sp->u.string);
ret.string_type = STRING_SHARED;
ret.u.string = make_shared_string(res);
free(res);
break;
}
case T_POINTER:
ret.u.vec = prepend_vector(sp->u.vec, sp-1);
break;
default: error("Bad type argument to +. %d %d\n",
(sp-1)->type, sp->type);
}
break;
}
case T_POINTER:
{
ret.type = T_POINTER;
switch(sp->type)
{
case T_POINTER:
ret.u.vec = add_array((sp-1)->u.vec, sp->u.vec);
break;
case T_NUMBER:
case T_STRING:
ret.u.vec = append_vector((sp-1)->u.vec, sp);
break;
default: error("Bad type argument to +. %d %d\n",
(sp-1)->type, sp->type);
}
break;
}
case T_MAPPING:
{
switch(sp->type)
{
case T_MAPPING:
ret.type = T_MAPPING;
ret.u.map = add_mapping((sp-1)->u.map, sp->u.map);
break;
default:
error("Bad type argument to +. %d %d\n",
(sp-1)->type, sp->type);
}
break;
}
default: error("Bad type argument to +. %d %d\n",
(sp-1)->type, sp->type);
}
pop_n_elems(2);
push_svalue(&ret);
free_svalue(&ret, "F_ADD");
break;
}
CASE(F_SUBTRACT);
{
struct svalue ret;
ret.type = (sp-1)->type;
switch((sp-1)->type)
{
case T_NUMBER:
{
switch(sp->type)
{
case T_NUMBER:
ret.u.number = (sp-1)->u.number - sp->u.number;
break;
default:
bad_arg(2, F_SUBTRACT);
break;
}
break;
}
case T_POINTER:
#ifdef COMPAT_MODE
ret.u.vec = subtract_array((sp-1)->u.vec, sp);
#else
{
if (sp->type == T_POINTER) {
struct vector *v;
v = sp->u.vec;
if (v->ref > 1) {
v = slice_array(v, 0, v->size-1 );
v->ref--;
}
sp--;
v = subtract_array(sp->u.vec, v);
free_vector(sp->u.vec);
sp->u.vec = v;
}
else bad_arg(2, F_SUBTRACT);
}
#endif
break;
default:
bad_arg(1, F_SUBTRACT);
}
pop_n_elems(2);
push_svalue(&ret);
free_svalue(&ret, "F_SUBTRACT");
break;
}
CASE(F_AND);
if (sp->type == T_POINTER && (sp-1)->type == T_POINTER) {
extern struct vector *intersect_array
PROT((struct vector *, struct vector *));
(sp-1)->u.vec = intersect_array(sp->u.vec, (sp-1)->u.vec);
sp--;
break;
}
if ((sp-1)->type != T_NUMBER)
bad_arg(1, F_AND);
if (sp->type != T_NUMBER)
bad_arg(2, F_AND);
i = (sp-1)->u.number & sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_OR);
if ((sp-1)->type != T_NUMBER)
bad_arg(1, F_OR);
if (sp->type != T_NUMBER)
bad_arg(2, F_OR);
i = (sp-1)->u.number | sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_XOR);
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
i = (sp-1)->u.number ^ sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_LSH);
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
i = (sp-1)->u.number << sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_RSH);
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
i = (sp-1)->u.number >> sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_MULTIPLY);
if ((sp-1)->type != sp->type)
bad_arg(1, instruction);
if (sp->type == T_NUMBER)
{
i = (sp-1)->u.number * sp->u.number;
sp--;
sp->u.number = i;
}
else if (sp->type == T_MAPPING)
{
struct mapping *m;
m = compose_mapping((sp-1)->u.map, sp->u.map);
pop_n_elems(2);
push_mapping(m);
}
else
bad_arg(2, instruction);
break;
CASE(F_DIVIDE);
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
if (sp->u.number == 0)
error("Division by zero\n");
i = (sp-1)->u.number / sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_MOD);
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
if (sp->u.number == 0)
error("Modulus by zero.\n");
i = (sp-1)->u.number % sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_GT);
if ((sp-1)->type == T_STRING && sp->type == T_STRING) {
i = strcmp((sp-1)->u.string, sp->u.string) > 0;
pop_n_elems(2);
push_number(i);
break;
}
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
i = (sp-1)->u.number > sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_GE);
if ((sp-1)->type == T_STRING && sp->type == T_STRING) {
i = strcmp((sp-1)->u.string, sp->u.string) >= 0;
pop_n_elems(2);
push_number(i);
break;
}
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
i = (sp-1)->u.number >= sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_LT);
if ((sp-1)->type == T_STRING && sp->type == T_STRING) {
i = strcmp((sp-1)->u.string, sp->u.string) < 0;
pop_n_elems(2);
push_number(i);
break;
}
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
i = (sp-1)->u.number < sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_LE);
if ((sp-1)->type == T_STRING && sp->type == T_STRING) {
i = strcmp((sp-1)->u.string, sp->u.string) <= 0;
pop_n_elems(2);
push_number(i);
break;
}
if ((sp-1)->type != T_NUMBER)
bad_arg(1, instruction);
if (sp->type != T_NUMBER)
bad_arg(2, instruction);
i = (sp-1)->u.number <= sp->u.number;
sp--;
sp->u.number = i;
break;
CASE(F_EQ);
if ((sp-1)->type != sp->type) {
pop_stack();
assign_svalue(sp, &const0);
break;
}
switch(sp->type) {
case T_NUMBER:
i = (sp-1)->u.number == sp->u.number;
break;
case T_POINTER:
i = (sp-1)->u.vec == sp->u.vec;
break;
case T_STRING:
i = strcmp((sp-1)->u.string, sp->u.string) == 0;
break;
case T_OBJECT:
i = (sp-1)->u.ob == sp->u.ob;
break;
default:
i = 0;
break;
}
pop_n_elems(2);
push_number(i);
break;
CASE(F_NE);
if ((sp-1)->type != sp->type) {
pop_stack();
assign_svalue(sp, &const1);
break;
}
switch(sp->type) {
case T_NUMBER:
i = (sp-1)->u.number != sp->u.number;
break;
case T_STRING:
i = strcmp((sp-1)->u.string, sp->u.string);
break;
case T_POINTER:
i = (sp-1)->u.vec != sp->u.vec;
break;
case T_OBJECT:
i = (sp-1)->u.ob != sp->u.ob;
break;
default:
fatal("Illegal type to !=\n");
}
pop_n_elems(2);
push_number(i);
break;
#ifdef F_LOG_FILE
CASE(F_LOG_FILE);
log_file((sp-1)->u.string, sp->u.string);
pop_stack();
break; /* Return first argument */
#endif /* F_LOG_FILE */
CASE(F_NOT);
if (sp->type == T_NUMBER && sp->u.number == 0)
sp->u.number = 1;
else
assign_svalue(sp, &const0);
break;
CASE(F_COMPL);
if (sp->type != T_NUMBER)
error("Bad argument to ~\n");
sp->u.number = ~ sp->u.number;
break;
CASE(F_NEGATE);
if (sp->type != T_NUMBER)
error("Bad argument to unary minus\n");
sp->u.number = - sp->u.number;
break;
CASE(F_INC);
if (sp->type != T_LVALUE)
error("Bad argument to ++\n");
if (sp->u.lvalue->type != T_NUMBER)
error("++ of non-numeric argument\n");
sp->u.lvalue->u.number++;
assign_svalue(sp, sp->u.lvalue);
break;
CASE(F_DEC);
if (sp->type != T_LVALUE)
error("Bad argument to --\n");
if (sp->u.lvalue->type != T_NUMBER)
error("-- of non-numeric argument\n");
sp->u.lvalue->u.number--;
assign_svalue(sp, sp->u.lvalue);
break;
CASE(F_POST_INC);
if (sp->type != T_LVALUE)
error("Bad argument to ++\n");
if (sp->u.lvalue->type != T_NUMBER)
error("++ of non-numeric argument\n");
sp->u.lvalue->u.number++;
assign_svalue(sp, sp->u.lvalue);
sp->u.number--;
break;
CASE(F_POST_DEC);
if (sp->type != T_LVALUE)
error("Bad argument to --\n");
if (sp->u.lvalue->type != T_NUMBER)
error("-- of non-numeric argument\n");
sp->u.lvalue->u.number--;
assign_svalue(sp, sp->u.lvalue);
sp->u.number++;
break;
CASE(F_CALL_OTHER);
{
struct svalue *arg, tmp;
arg = sp - num_arg + 1;
if (arg[0].type == T_OBJECT)
ob = arg[0].u.ob;
else {
ob = find_object(arg[0].u.string);
if (ob == 0)
error("Could not find object %s\n", arg[0].u.string);
/* Raistlin -- Was this
fatal("find_object() returned 0\n"); */
}
if ((current_object->flags & O_DESTRUCTED) ||
(ob->flags & O_DESTRUCTED)) {
/*
* No external calls may be done when this object is
* destructed.
*/
pop_n_elems(num_arg);
push_number(0);
break;
}
if (arg[1].u.string[0] == ':')
error("Illegal function name in call_other: %s\n",
arg[1].u.string);
/*
* Send the remaining arguments to the function.
*/
if (TRACEP(TRACE_CALL_OTHER)) {
do_trace("Call other ", arg[1].u.string, "\n");
}
if (apply_low(arg[1].u.string, ob, num_arg-2) == 0) {
/* Function not found */
pop_n_elems(2);
push_number(0);
break;
}
/*
* The result of the function call is on the stack. But, so
* is the function name and object that was called.
* These have to be removed.
*/
tmp = *sp--; /* Copy the function call result */
pop_n_elems(2); /* Remove old arguments to call_other */
*++sp = tmp; /* Re-insert function result */
break;
}
CASE(F_INTP);
if (sp->type == T_NUMBER)
assign_svalue(sp, &const1);
else
assign_svalue(sp, &const0);
break;
CASE(F_STRINGP);
if (sp->type == T_STRING)
assign_svalue(sp, &const1);
else
assign_svalue(sp, &const0);
break;
CASE(F_OBJECTP);
if (sp->type == T_OBJECT)
assign_svalue(sp, &const1);
else
assign_svalue(sp, &const0);
break;
CASE(F_POINTERP);
if (sp->type == T_POINTER)
assign_svalue(sp, &const1);
else
assign_svalue(sp, &const0);
break;
CASE(F_MAPP);
if (sp->type == T_MAPPING)
assign_svalue(sp, &const1);
else
assign_svalue(sp, &const0);
break;
CASE(F_EXTRACT);
{
int len, from, to;
struct svalue *arg;
char *res;
arg = sp - num_arg + 1;
if (num_arg == 1)
break; /* Simply return argument */
if (arg[0].type == T_POINTER)
{
struct vector *r;
int from, to, i;
from = arg[1].u.number;
if (num_arg == 3) to = arg[2].u.number;
else to = arg[0].u.vec->size-1;
if (from > arg[0].u.vec->size-1 || to < from) {
pop_n_elems(num_arg);
push_number(0);
break;
}
if (to > arg[0].u.vec->size-1)
to = arg[0].u.vec->size-1;
r = allocate_array(to - from + 1);
for (i = from; i <= to; ++i)
assign_svalue(&r->item[i-from], &arg[0].u.vec->item[i]);
pop_n_elems(num_arg);
push_vector(r);
r->ref--;
break;
}
len = strlen(arg[0].u.string);
from = arg[1].u.number;
if (from < 0)
from = len + from;
if (from >= len) {
pop_n_elems(num_arg);
push_string("", STRING_CONSTANT);
break;
}
if (num_arg == 2) {
res = make_shared_string(arg->u.string + from);
pop_n_elems(2);
push_shared_string(res); /**MALLOC**/
break;
}
if (arg[2].type != T_NUMBER)
error("Bad third argument to extract()\n");
to = arg[2].u.number;
if (to < 0)
to = len + to;
if (to < from) {
pop_n_elems(3);
push_string("", STRING_CONSTANT);
break;
}
if (to >= len)
to = len-1;
if (to == len-1) {
res = make_shared_string(arg->u.string + from);
pop_n_elems(3);
push_shared_string(res); /**MALLOC**/
break;
}
res = xalloc(to - from + 2);
strncpy(res, arg[0].u.string + from, to - from + 1);
res[to - from + 1] = '\0';
pop_n_elems(3);
push_shared_string(make_shared_string(res)); /*MALLOC*/
free(res);
break;
}
CASE(F_ATOI);
{
int i = atoi(sp->u.string);
pop_stack();
push_number(i);
break;
}
CASE(F_RANGE);
{
if (sp[-1].type != T_NUMBER)
error("Bad type of start interval to [ .. ] range.\n");
if (sp[0].type != T_NUMBER)
error("Bad type of end interval to [ .. ] range.\n");
if (sp[-2].type == T_POINTER) {
struct vector *v;
v = slice_array(sp[-2].u.vec, sp[-1].u.number, sp[0].u.number);
pop_n_elems(3);
if (v) {
push_vector(v);
v->ref--; /* Will make ref count == 1 */
} else {
push_number(0);
}
} else if (sp[-2].type == T_STRING) {
int len, from, to;
char *res;
len = strlen(sp[-2].u.string);
from = sp[-1].u.number;
if (from < 0)
from = len + from;
if (from >= len) {
pop_n_elems(3);
push_string("", STRING_CONSTANT);
break;
}
to = sp[0].u.number;
if (to < 0)
to = len + to;
if (to < from) {
pop_n_elems(3);
push_string("", STRING_CONSTANT);
break;
}
if (to >= len)
to = len-1;
if (to == len-1) {
res = string_copy(sp[-2].u.string + from);
pop_n_elems(3);
push_malloced_string(res);
break;
}
res = xalloc(to - from + 2);
strncpy(res, sp[-2].u.string + from, to - from + 1);
res[to - from + 1] = '\0';
pop_n_elems(3);
push_malloced_string(res);
} else {
error("Bad argument to [ .. ] range operand.\n");
}
break;
}
CASE(F_QUERY_ARG);
case F_THIS_ARG:
if (last_arg == 0) {
push_number(0);
break;
}
push_string(last_arg, STRING_CONSTANT);
break;
CASE(F_QUERY_VERB);
case F_THIS_VERB:
if (last_verb == 0) {
push_number(0);
break;
}
push_string(last_verb, STRING_CONSTANT);
break;
CASE(F_EXEC);
i =replace_interactive((sp-1)->u.ob, sp->u.ob, current_prog->name);
pop_n_elems(2);
push_number(i);
break;
CASE(F_FILE_NAME);
{
char *name,*res;
name = sp->u.ob->name;
#ifdef COMPAT_MODE
res = string_copy(name);
#else
res = add_slash(name);
#endif
pop_stack();
push_shared_string(make_shared_string(res)); /*MALLOC*/
free(res);
break;
}
CASE(F_USERS);
push_vector(users()); /* users() has already set ref count to 1 */
sp->u.vec->ref--;
break;
CASE(F_CALL_OUT);
{
struct svalue *arg = sp - num_arg + 1;
if (!(current_object->flags & O_DESTRUCTED))
new_call_out(current_object, arg[0].u.string, arg[1].u.number,
num_arg == 3 ? sp : 0);
pop_n_elems(num_arg);
push_number(0);
}
break;
CASE(F_CALL_OUT_INFO);
push_vector(get_all_call_outs());
sp->u.vec->ref--; /* Was set to 1 at allocation */
break;
CASE(F_REMOVE_CALL_OUT);
i = remove_call_out(current_object, sp->u.string);
pop_stack();
push_number(i);
break;
#ifdef F_INHERIT_LIST
CASE(F_INHERIT_LIST)
{
struct vector *vec;
extern struct vector *inherit_list PROT((struct object *));
vec = inherit_list(sp->u.ob);
pop_stack();
push_vector(vec);
break;
}
#endif
CASE(F_WRITE);
do_write(sp);
break;
CASE(F_MOVE_OBJECT);
{
struct object *o1, *o2;
if ((sp-1)->type == T_OBJECT)
o1 = (sp-1)->u.ob;
else {
o1 = find_object((sp-1)->u.string);
if (o1 == 0)
error("move_object failed\n");
}
if (sp->type == T_OBJECT)
o2 = sp->u.ob;
else {
o2 = find_object(sp->u.string);
if (o2 == 0)
error("move_object failed\n");
}
move_object(o1, o2);
pop_stack();
break;
}
CASE(F_FUNCTION_EXISTS);
{
char *str, *res;
str = function_exists((sp-1)->u.string, sp->u.ob);
pop_n_elems(2);
if (str) {
extern char *strrchr();
/* Raistlin -- made it use shared strings */
res = str;
/* marion - strip trailing .c */
if (str = strrchr (res, (int)'.')) *str = 0;
#ifdef COMPAT_MODE
push_shared_string(make_shared_string(res));
#else
{
res = add_slash(res);
push_shared_string(make_shared_string(res));
free(res);
/**MALLOC**/
}
#endif
} else {
push_number(0);
}
break;
}
CASE(F_SNOOP);
{
/* This one takes a variable number of arguments. It returns
* 0 or an object.
*/
#ifdef COMPAT_MODE
struct svalue *a;
if (!command_giver || num_arg == 2)
{
pop_n_elems (num_arg);
push_number (0);
}
a = apply("query_level", command_giver, 0);
if (!a || a->type != T_NUMBER || a->u.number < SNOOP_LEVEL)
{
pop_n_elems(num_arg);
push_number(0);
}
else if (num_arg == 0)
{
set_snoop(command_giver, (struct object *)0);
push_number(0);
}
else
set_snoop(command_giver, sp->u.ob);
#else
if (!command_giver) {
pop_n_elems(num_arg);
push_number(0);
} else {
ob = 0; /* Do not remove this, it is not 0 by default */
switch (num_arg) {
case 1:
if (new_set_snoop(sp->u.ob, 0))
ob = sp->u.ob;
break;
case 2:
if (new_set_snoop((sp-1)->u.ob, sp->u.ob))
ob = sp->u.ob;
break;
default:
ob = 0;
break;
}
pop_n_elems(num_arg);
if (ob)
push_object(ob, "F_SNOOP");
else
push_number(0);
}
#endif
break;
}
CASE(F_REMOVE_ACTION);
{
if (!command_giver || (command_giver->flags & O_DESTRUCTED) ||
!current_object || (current_object->flags & O_DESTRUCTED))
{
pop_stack();
push_number(0);
}
else
{
i = remove_one_sent(current_object, command_giver, sp->u.string);
pop_stack();
push_number(i);
}
break;
}
CASE(F_ADD_ACTION);
{
struct svalue *arg = sp - num_arg + 1;
if (num_arg == 3) {
if (arg[2].type != T_NUMBER)
bad_arg(3, instruction);
}
add_action(arg[0].u.string,
num_arg > 1 ? arg[1].u.string : 0,
num_arg > 2 ? arg[2].u.number : 0);
pop_n_elems(num_arg-1);
break;
}
CASE(F_ADD_VERB);
add_verb(sp->u.string, 0);
break;
CASE(F_ADD_XVERB);
add_verb(sp->u.string, 1);
break;
CASE(F_ALLOCATE);
{
struct vector *v;
v = allocate_array(sp->u.number); /* Will have ref count == 1 */
pop_stack();
push_vector(v);
v->ref--;
break;
}
CASE(F_ALLOCATE_MAPPING);
{
struct mapping *m;
m = allocate_mapping(sp->u.number);
pop_stack();
push_mapping(m);
m->ref--;
break;
}
CASE(F_ED);
if (num_arg == 0) {
struct svalue *arg;
char *err_file;
if (command_giver == 0 || command_giver->interactive == 0) {
push_number(0);
break;
}
arg = sapply("query_real_name", command_giver, 0);
if (arg == 0 || arg->type != T_STRING) {
push_number(0);
break;
}
err_file = get_error_file(arg->u.string);
if (err_file == 0) {
push_number(0);
break;
}
ed_start(err_file, 0, 0);
push_number(1);
break;
} else if (num_arg == 1) {
ed_start(sp->u.string, 0, 0);
} else {
if (sp->type == T_STRING)
ed_start((sp-1)->u.string, sp->u.string, current_object);
else
ed_start((sp-1)->u.string, 0 , 0);
pop_stack();
}
break;
CASE(F_CRYPT);
{
char salt[2];
char *res;
char *choise =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
if (sp->type == T_STRING && strlen(sp->u.string) >= 2) {
salt[0] = sp->u.string[0];
salt[1] = sp->u.string[1];
} else {
salt[0] = choise[random_number(strlen(choise))];
salt[1] = choise[random_number(strlen(choise))];
}
#ifdef sun
res = make_shared_string(_crypt((sp-1)->u.string, salt)); /**MALLOC**/
#else
res = make_shared_string(crypt((sp-1)->u.string, salt)); /**MALLOC**/
#endif
pop_n_elems(2);
push_shared_string(res);
break;
}
#ifdef F_CREATE_WIZARD
CASE(F_CREATE_WIZARD);
{
char *str;
struct svalue *arg = sp - num_arg + 1;
str = create_wizard(arg[0].u.string,
num_arg == 2 ? arg[1].u.string : 0);
pop_n_elems(num_arg);
if (str)
push_string(str, STRING_CONSTANT);
else
push_number(0);
break;
}
#endif
CASE(F_DESTRUCT);
{
int really;
if (num_arg == 2)
{
really = sp->u.number;
pop_stack();
}
else really = 0;
destruct_object(sp, really);
break;
}
CASE(F_RANDOM);
if (sp->u.number <= 0) {
sp->u.number = 0;
break;
}
sp->u.number = random_number(sp->u.number);
break;
#ifdef F_SAY
CASE(F_SAY);
{
static struct {
struct vector v;
struct svalue second_item[1];
} vtmp = { { 2, 1,
#ifdef DEBUG
1,
#endif
(struct wiz_list *)NULL,
{ { T_NUMBER } } }, { { T_OBJECT } }
};
if (num_arg == 1) {
vtmp.v.item[0].type = T_NUMBER; /* this marks the place for the
command_giver
*/
vtmp.v.item[1].type = T_NUMBER; /* will not match any object... */
vtmp.v.item[1].u.number = 0;
say(sp, &vtmp.v);
} else {
if ( sp->type == T_POINTER ) {
if (sp->u.vec->ref > 1) {
struct vector *vtmpp =
slice_array(sp->u.vec, 0, sp->u.vec->size-1);
say(sp-1, vtmpp);
free_vector(vtmpp);
} else
say(sp-1, sp->u.vec);
} else {
vtmp.v.item[0].type = T_NUMBER;
vtmp.v.item[1].type = T_OBJECT;
vtmp.v.item[1].u.ob = sp->u.ob;
add_ref(sp->u.ob, "F_SAY");
say(sp-1, &vtmp.v);
vtmp.v.size = 2;
vtmp.v.ref = 1;
#ifdef DEBUG
vtmp.v.extra_ref = 1;
#endif
vtmp.v.item[1].type = T_NUMBER;
vtmp.v.item[1].u.number = 0;
}
pop_stack();
}
break;
}
break;
#endif /* F_SAY */
#ifdef F_TELL_ROOM
CASE(F_TELL_ROOM);
{
extern struct vector null_vector;
struct svalue *arg = sp- num_arg + 1;
struct vector *avoid;
if (arg[0].type == T_OBJECT)
ob = arg[0].u.ob;
else {
ob = find_object(arg[0].u.string);
if (ob == 0)
error("Object not found.\n");
}
if (num_arg == 2) {
avoid = &null_vector;
avoid->ref++;
} else {
extern struct vector *order_alist PROT((struct vector *));
struct vector *vtmpp;
static struct vector vtmp = { 1, 1,
#ifdef DEBUG
1,
#endif
(struct wiz_list *)NULL,
{ { T_POINTER } }
};
if (arg[2].type != T_POINTER)
bad_arg(3, instruction);
vtmp.item[0].u.vec = arg[2].u.vec;
if (vtmp.item[0].u.vec->ref > 1) {
vtmp.item[0].u.vec->ref--;
vtmp.item[0].u.vec = slice_array(
vtmp.item[0].u.vec, 0, vtmp.item[0].u.vec->size-1);
}
sp--;
vtmpp = order_alist(&vtmp);
avoid = vtmpp->item[0].u.vec;
vtmpp->item[0].u.vec = vtmp.item[0].u.vec;
free_vector(vtmpp);
}
tell_room(ob, sp, avoid);
free_vector(avoid);
pop_stack();
break;
}
#endif /* F_TELL_ROOM */
#ifdef F_SHOUT
CASE(F_SHOUT);
shout_string(sp->u.string);
break;
#endif /* F_SHOUT */
CASE(F_WHILE);
fatal("F_WHILE should not appear.\n");
CASE(F_DO);
fatal("F_DO should not appear.\n");
CASE(F_FOR);
fatal("F_FOR should not appear.\n");
CASE(F_SWITCH);
{
extern char* findstring PROT((char*));
unsigned short offset,break_adr;
int d,s,r;
char *l,*end_tab;
static short off_tab[] = { 0*6,1*6,3*6,7*6,15*6,31*6,63*6,127*6,255*6,
511*6,1023*6,2047*6,4095*6,8191*6 };
((char *)&offset)[0] = pc[1];
((char *)&offset)[1] = pc[2];
((char *)&break_adr)[0] = pc[3];
((char *)&break_adr)[1] = pc[4];
*--break_sp = break_adr;
if ( ( i = EXTRACT_UCHAR(pc) >> 4 ) != 0xf ) {
if ( sp->type == T_NUMBER && !sp->u.number ) {
/* special case: uninitalized string */
s = ZERO_AS_STR_CASE_LABEL;
} else if ( sp->type == T_STRING ) {
switch(sp->string_type) {
case STRING_SHARED:
s = (int)sp->u.string;
break;
default:
s = (int)findstring(sp->u.string);
break;
}
} else {
bad_arg(1, F_SWITCH);
}
} else {
if (sp->type != T_NUMBER) bad_arg(1, F_SWITCH);
s = sp->u.number;
i = (int)pc[0] &0xf ;
}
pop_stack();
end_tab = current_prog->program + break_adr;
if ( i >= 14 )
if ( i == 14 ) {
/* fastest switch format : lookup table */
l = current_prog->program + offset;
((char *)&d)[0] = end_tab[-4];
((char *)&d)[1] = end_tab[-3];
((char *)&d)[2] = end_tab[-2];
((char *)&d)[3] = end_tab[-1];
if ( s >= d && l + (s=(s-d)*sizeof(short)) < end_tab - 4 ) {
((char *)&offset)[0] = l[s];
((char *)&offset)[1] = l[s+1];
if (offset) {
pc = current_prog->program + offset;
break;
}
}
/* default */
((char *)&offset)[0] = pc[5];
((char *)&offset)[1] = pc[6];
pc = current_prog->program + offset;
break;
} else
fatal("unsupported switch table format.\n");
l = current_prog->program + offset + off_tab[i];
d = (off_tab[i]+6) >> 1;
if (d == 3) d=0;
for(;;) {
((char *)&r)[0] = l[0];
((char *)&r)[1] = l[1];
((char *)&r)[2] = l[2];
((char *)&r)[3] = l[3];
if (s < r)
if (d < 6) {
if (!d) { /* test for range */
((char *)&offset)[0] = l[-2];
((char *)&offset)[1] = l[-1];
/* F_BREAK is required to be > 1 */
if (offset <= 1) {
((char *)&r)[0] = l[-6];
((char *)&r)[1] = l[-5];
((char *)&r)[2] = l[-4];
((char *)&r)[3] = l[-3];
if (s >= r) {
/* s is in the range */
if (!offset) {
/* range with lookup table */
((char *)&offset)[0] = l[4];
((char *)&offset)[1] = l[5];
l = current_prog->program + offset +
(s-r) * sizeof(short);
((char *)&offset)[0] = l[0];
((char *)&offset)[1] = l[1];
break;
}
((char *)&offset)[0] = l[4];
((char *)&offset)[1] = l[5];
break;
}
}
/* use default address */
((char *)&offset)[0] = pc[5];
((char *)&offset)[1] = pc[6];
break;
} /* !d */
d = 0;
} else {
/* d >= 6 */
l -= d;
d >>= 1;
}
else if (s > r) {
if (d < 6) {
if (!d) { /* test for range */
((char *)&offset)[0] = l[4];
((char *)&offset)[1] = l[5];
if (offset <= 1) {
((char *)&r)[0] = l[6];
((char *)&r)[1] = l[7];
((char *)&r)[2] = l[8];
((char *)&r)[3] = l[9];
if (s <= r) {
/* s is in the range */
if (!offset) {
/* range with lookup table */
((char *)&offset)[0] = l[10];
((char *)&offset)[1] = l[11];
l = current_prog->program + offset +
(s-r) * sizeof(short);
((char *)&offset)[0] = l[0];
((char *)&offset)[1] = l[1];
break;
}
((char *)&offset)[0] = l[10];
((char *)&offset)[1] = l[11];
break;
}
}
/* use default address */
((char *)&offset)[0] = pc[5];
((char *)&offset)[1] = pc[6];
break;
} /* !d */
d = 0;
} else {
/* d >= 6 */
l += d;
while (l >= end_tab) {
d >>= 1;
if (d <= 3) {
if (!d) break;
d = 0;
}
l -= d;
}
d >>= 1;
}
} else {
/* s == r */
((char *)&offset)[0] = l[4];
((char *)&offset)[1] = l[5];
if ( !l[-2] && !l[-1] ) {
/* end of range with lookup table */
((char *)&r)[0] = l[-6];
((char *)&r)[1] = l[-5];
((char *)&r)[2] = l[-4];
((char *)&r)[3] = l[-3];
l = current_prog->program + offset + (s-r)*sizeof(short);
((char *)&offset)[0] = l[0];
((char *)&offset)[1] = l[1];
}
if (offset <= 1) {
if (!offset) {
/* start of range with lookup table */
((char *)&offset)[0] = l[10];
((char *)&offset)[1] = l[11];
l = current_prog->program + offset;
((char *)&offset)[0] = l[0];
((char *)&offset)[1] = l[1];
} else {
((char *)&offset)[0] = l[10];
((char *)&offset)[1] = l[11];
}
}
break;
}
}
pc = current_prog->program + offset;
break;
}
CASE(F_BREAK);
{
pc = current_prog->program + *break_sp++;
break;
}
CASE(F_SUBSCRIPT);
fatal("F_SUBSCRIPT should not appear.\n");
CASE(F_STRLEN);
i = strlen(sp->u.string);
pop_stack();
push_number(i);
break;
CASE(F_SIZEOF);
if (sp->type == T_POINTER)
i = sp->u.vec->size;
else if (sp->type == T_MAPPING)
i = sp->u.map->size;
else
i = 0;
pop_stack();
push_number(i);
break;
CASE(F_LOWER_CASE);
{
char *str = string_copy(sp->u.string);
for (i = strlen(str)-1; i>=0; i--)
if (isalpha(str[i]))
str[i] |= 'a' - 'A';
pop_stack();
push_shared_string(make_shared_string(str)); /*MALLOC*/
free(str);
break;
}
CASE(F_SET_HEART_BEAT);
i = set_heart_beat(current_object, sp->u.number);
sp->u.number = i;
break;
CASE(F_CAPITALIZE);
if (islower(sp->u.string[0])) {
char *str;
str = string_copy(sp->u.string);
str[0] += 'A' - 'a';
pop_stack();
push_shared_string(make_shared_string(str));
free(str);/*MALLOC*/
}
break;
CASE(F_PROCESS_STRING);
{
extern char *process_string PROT((char *));
char *str;
str = process_string(sp->u.string);
if (str != sp->u.string) {
pop_stack();
push_shared_string(make_shared_string(str));
}
break;
}
CASE(F_COMMAND);
{
struct svalue *arg = sp - num_arg + 1;
if (num_arg == 1)
i = command_for_object(arg[0].u.string, 0);
else
#ifdef COMPAT_MODE
i = command_for_object(arg[0].u.string, arg[1].u.ob);
#else
error("Too many arguments to command().\n");
#endif
pop_n_elems(num_arg);
push_number(i);
break;
}
CASE(F_GET_DIR);
{
struct vector *v;
v = get_dir(sp->u.string);
pop_stack();
if (v) { push_vector(v); v->ref--; } /* Will now be 1. */
else push_number(0);
break;
}
CASE(F_LS);
if (sp->type == T_NUMBER)
list_files(NULL);
else
list_files(sp->u.string);
break;
CASE(F_RM);
i = remove_file(sp->u.string);
pop_stack();
push_number(i);
break;
CASE(F_CAT);
{
struct svalue *arg = sp- num_arg + 1;
int start = 0, len = 0;
if (num_arg > 1)
start = arg[1].u.number;
if (num_arg == 3) {
if (arg[2].type != T_NUMBER)
bad_arg(2, instruction);
len = arg[2].u.number;
}
i = print_file(arg[0].u.string, start, len);
pop_n_elems(num_arg);
push_number(i);
break;
}
CASE(F_MKDIR);
{
char *path;
#ifdef COMPAT_MODE
path = check_file_name(sp->u.string, 1);
#else
path = check_valid_path(sp->u.string, current_object->eff_user,
"mkdir", 1);
#endif
/* pop_stack(); marion - what the heck ??? */
if (path == 0 || mkdir(path, 0770) == -1)
assign_svalue(sp, &const0);
else
assign_svalue(sp, &const1);
break;
}
CASE(F_RMDIR);
{
char *path;
#ifdef COMPAT_MODE
path = check_file_name(sp->u.string, 1);
#else
path = check_valid_path(sp->u.string, current_object->eff_user,
"rmdir", 1);
#endif
/* pop_stack(); */
if (path == 0 || rmdir(path) == -1)
assign_svalue(sp, &const0);
else
assign_svalue(sp, &const1);
break;
}
CASE(F_KEYS);
{
struct vector *v;
v = mapping_indices(sp->u.map);
pop_stack();
push_vector(v);
break;
}
CASE(F_INPUT_TO);
{
struct svalue *arg = sp - num_arg + 1;
int flag = 1;
if (num_arg == 1 || sp->type == T_NUMBER && sp->u.number == 0)
flag = 0;
i = input_to(arg[0].u.string, flag);
pop_n_elems(num_arg);
push_number(i);
break;
}
CASE(F_SET_LIVING_NAME);
set_living_name(current_object, sp->u.string);
break;
CASE(F_PARSE_COMMAND);
{
struct svalue *arg;
num_arg = EXTRACT_UCHAR(pc);
pc++;
arg = sp - num_arg + 1;
if (arg[0].type != T_STRING)
bad_arg(1, F_PARSE_COMMAND);
if (arg[1].type != T_OBJECT && arg[1].type != T_POINTER)
bad_arg(2, F_PARSE_COMMAND);
if (arg[2].type != T_STRING)
bad_arg(3, F_PARSE_COMMAND);
if (arg[1].type == T_POINTER)
check_for_destr(arg[1].u.vec);
i = parse(arg[0].u.string, &arg[1], arg[2].u.string, &arg[3],
num_arg-3);
pop_n_elems(num_arg); /* Get rid of all arguments */
push_number(i); /* Push the result value */
break;
}
CASE(F_SSCANF);
num_arg = EXTRACT_UCHAR(pc);
pc++;
i = inter_sscanf(num_arg);
pop_n_elems(num_arg);
push_number(i);
break;
CASE(F_ENABLE_COMMANDS);
enable_commands(1);
push_number(1);
break;
CASE(F_DISABLE_COMMANDS);
enable_commands(0);
push_number(0);
break;
CASE(F_PRESENT);
{
struct svalue *arg = sp - num_arg + 1;
ob = object_present(arg, num_arg == 1 ? 0 : arg[1].u.ob);
pop_n_elems(num_arg);
if (ob)
push_object(ob, "F_PRESENT");
else
push_number(0);
}
break;
#ifdef F_SET_LIGHT
CASE(F_SET_LIGHT);
{
struct object *o1;
add_light(current_object, sp->u.number);
o1 = current_object;
while(o1->super)
o1 = o1->super;
sp->u.number = o1->total_light;
break;
}
#endif /* F_SET_LIGHT */
CASE(F_CONST0);
push_number(0);
break;
CASE(F_CONST1);
push_number(1);
break;
CASE(F_NUMBER);
((char *)&i)[0] = pc[0];
((char *)&i)[1] = pc[1];
((char *)&i)[2] = pc[2];
((char *)&i)[3] = pc[3];
pc += 4;
push_number(i);
break;
CASE(F_ASSIGN);
#ifdef DEBUG
if (sp[-1].type != T_LVALUE)
fatal("Bad argument to F_ASSIGN\n");
#endif
assign_svalue((sp-1)->u.lvalue, sp);
assign_svalue(sp-1, sp);
pop_stack();
break;
CASE(F_CTIME);
{
char *cp, *a;
int n = current_time;
if (num_arg) { n = sp->u.number; pop_stack(); }
cp = string_copy(time_string(n));
/* Now strip the newline. */
a = strchr(cp, '\n');
if (a)
*a = '\0';
push_shared_string(make_shared_string(cp));
free(cp);/*MALLOC*/
break;
}
CASE(F_ADD_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_ADD_EQ);
argp = sp[-1].u.lvalue;
switch(argp->type)
{
case T_MAPPING:
{
if (sp->type == T_MAPPING)
{
struct mapping *m;
m = add_mapping(argp->u.map, sp->u.map);
pop_n_elems(2);
push_mapping(m);
m->ref--;
}
else
bad_arg(2, F_ADD_EQ);
break;
}
case T_POINTER:
{
struct vector *v;
if (sp->type != T_POINTER)
v = append_vector(argp->u.vec, sp);
else
v = add_array(argp->u.vec, sp->u.vec);
pop_n_elems(2);
push_vector(v);
v->ref--;
break;
}
case T_STRING:
{
char *new_str;
switch(sp->type)
{
case T_STRING:
{
new_str=xalloc(strlen(argp->u.string)+
strlen(sp->u.string)+1);
strcpy(new_str, argp->u.string);
strcat(new_str, sp->u.string);
pop_n_elems(2);
push_shared_string(make_shared_string(new_str));
free(new_str);/**MALLOC**/
break;
}
case T_NUMBER:
{
char buff[20];
sprintf(buff, "%d", sp->u.number);
new_str = xalloc(strlen(argp->u.string)+strlen(buff)+1);
strcpy(new_str, argp->u.string);
strcat(new_str, buff);
pop_n_elems(2);
push_shared_string(make_shared_string(new_str));
free(new_str);/**MALLOC**/
break;
}
case T_POINTER: {
struct vector *v;
v = prepend_vector(sp->u.vec, argp);
pop_n_elems(2);
push_vector(v);
v->ref--;
break;
}
default: error("Bad type RHS to +=\n");
}
}
break;
case T_NUMBER: {
switch(sp->type)
{
case T_NUMBER:
i = argp->u.number + sp->u.number;
pop_n_elems(2);
push_number(i);
break;
case T_POINTER:
{
struct vector *v;
v = prepend_vector(sp->u.vec, argp);
pop_n_elems(2);
push_vector(v); /* This will make ref count == 2 */
v->ref--;
break;
}
case T_STRING:
{
char buff[20], *new_str;
sprintf(buff, "%d", argp->u.number);
new_str=xalloc(strlen(sp->u.string) + strlen(buff) + 1);
strcpy(new_str, buff);
strcat(new_str, sp->u.string);
pop_n_elems(2);
push_shared_string(make_shared_string(new_str));
free(new_str);/**MALLOC**/
break;
}
default:
error("Bad type number to rhs +=.\n");
}
}
break;
default:
error("Bad type to lhs +=");
}
assign_svalue(argp, sp);
break;
CASE(F_SUB_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_SUB_EQ);
argp = sp[-1].u.lvalue;
switch(argp->type)
{
case T_NUMBER:
if (sp->type != T_NUMBER)
error("Bad right type to -=\n");
i = argp->u.number - sp->u.number;
pop_n_elems(2);
push_number(i);
break;
case T_POINTER:
#ifdef COMPAT_MODE
{
struct vector *v;
v = subtract_array(argp->u.vec, sp);
pop_n_elems(2);
if (v) { push_vector(v); v->ref--; }
else push_number(0);
break;
}
#else
{
struct vector *v;
if (sp->type != T_POINTER)
error("Bad right type to -=");
v = sp->u.vec;
if (v->ref > 1) {
v = slice_array(v, 0, v->size-1 );
v->ref--;
}
sp--;
v = subtract_array(argp->u.vec, v);
free_vector(argp->u.vec);
argp->u.vec = v;
break;
}
#endif
default: error("Bad lhs to -=\n");
}
assign_svalue(argp, sp);
break;
CASE(F_MULT_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_MULT_EQ);
argp = sp[-1].u.lvalue;
if (argp->type != sp->type)
error("Type mismatch to operator *=\n");
switch(sp->type)
{
case T_NUMBER:
{
i = argp->u.number * sp->u.number;
pop_n_elems(2);
push_number(i);
break;
}
case T_MAPPING:
{
struct mapping *m;
m = compose_mapping(argp->u.map, sp->u.map);
pop_n_elems(2);
push_mapping(m);
break;
}
default: bad_arg(2, F_MULT_EQ);
}
assign_svalue(argp, sp);
break;
CASE(F_AND_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_AND_EQ);
argp = sp[-1].u.lvalue;
if (argp->type != T_NUMBER)
error("Bad left type to &=.\n");
if (sp->type != T_NUMBER)
error("Bad right type to &=");
i = argp->u.number & sp->u.number;
pop_n_elems(2);
push_number(i);
assign_svalue(argp, sp);
break;
CASE(F_OR_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_OR_EQ);
argp = sp[-1].u.lvalue;
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_OR_EQ);
argp = sp[-1].u.lvalue;
if (argp->type != T_NUMBER)
error("Bad left type to |=.\n");
if (sp->type != T_NUMBER)
error("Bad right type to |=");
i = argp->u.number | sp->u.number;
pop_n_elems(2);
push_number(i);
assign_svalue(argp, sp);
break;
CASE(F_XOR_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_XOR_EQ);
argp = sp[-1].u.lvalue;
if (argp->type != T_NUMBER)
error("Bad left type to ^=.\n");
if (sp->type != T_NUMBER)
error("Bad right type to ^=");
i = argp->u.number ^ sp->u.number;
pop_n_elems(2);
push_number(i);
assign_svalue(argp, sp);
break;
CASE(F_LSH_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_LSH_EQ);
argp = sp[-1].u.lvalue;
if (argp->type != T_NUMBER)
error("Bad left type to <<=.\n");
if (sp->type != T_NUMBER)
error("Bad right type to <<=");
i = argp->u.number << sp->u.number;
pop_n_elems(2);
push_number(i);
assign_svalue(argp, sp);
break;
CASE(F_RSH_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_RSH_EQ);
argp = sp[-1].u.lvalue;
if (argp->type != T_NUMBER)
error("Bad left type to >>=.\n");
if (sp->type != T_NUMBER)
error("Bad right type to >>=");
i = argp->u.number >> sp->u.number;
pop_n_elems(2);
push_number(i);
assign_svalue(argp, sp);
break;
#ifdef F_COMBINE_FREE_LIST
CASE(F_COMBINE_FREE_LIST);
#ifdef MALLOC_malloc
push_number(resort_free_list());
#else
push_number(0);
#endif
break;
#endif
CASE(F_DIV_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_DIV_EQ);
argp = sp[-1].u.lvalue;
if (argp->type != T_NUMBER)
error("Bad left type to /=.\n");
if (sp->type != T_NUMBER)
error("Bad right type to /=");
if (sp->u.number == 0)
error("Division by 0\n");
i = argp->u.number / sp->u.number;
pop_n_elems(2);
push_number(i);
assign_svalue(argp, sp);
break;
CASE(F_MOD_EQ);
if (sp[-1].type != T_LVALUE)
bad_arg(1, F_MOD_EQ);
argp = sp[-1].u.lvalue;
if (argp->type != T_NUMBER)
error("Bad left type to %=.\n");
if (sp->type != T_NUMBER)
error("Bad right type to %=");
if (sp->u.number == 0)
error("Division by 0\n");
i = argp->u.number % sp->u.number;
pop_n_elems(2);
push_number(i);
assign_svalue(argp, sp);
break;
CASE(F_STRING);
{
unsigned short string_number;
((char *)&string_number)[0] = pc[0];
((char *)&string_number)[1] = pc[1];
pc += 2;
push_string(current_prog->strings[string_number],
STRING_CONSTANT);
break;
}
#ifdef F_RUSAGE
CASE(F_RUSAGE);
{
char buff[500];
struct rusage rus;
long utime, stime;
int maxrss;
if (getrusage(RUSAGE_SELF, &rus) < 0)
buff[0] = 0;
else {
utime = rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
stime = rus.ru_stime.tv_sec * 1000 + rus.ru_stime.tv_usec / 1000;
maxrss = rus.ru_maxrss;
#ifdef sun
maxrss *= getpagesize() / 1024;
#endif
sprintf(buff, "%ld %ld %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
utime, stime, maxrss, rus.ru_ixrss, rus.ru_idrss,
rus.ru_isrss, rus.ru_minflt, rus.ru_majflt, rus.ru_nswap,
rus.ru_inblock, rus.ru_oublock, rus.ru_msgsnd,
rus.ru_msgrcv, rus.ru_nsignals, rus.ru_nvcsw,
rus.ru_nivcsw);
}
push_string(buff, STRING_SHARED);
break;
}
#endif
CASE(F_CINDENT);
{
char *path;
#ifndef COMPAT_MODE
path = check_valid_path(sp->u.string, current_object->eff_user, "cindent", 1);
#else
path = check_file_name(sp->u.string, 1);
#endif
pop_stack();
if (path) {
if (indent_program(path)) {
push_number(1);
break;
}
} else {
add_message("Illegal attempt to indent\n");
}
push_number(0);
break;
}
CASE(F_DESCRIBE);
{
char *str;
int live;
if (num_arg < 3) live = 0;
else {
if (sp->type != T_NUMBER) bad_arg (3, F_DESCRIBE);
live = sp->u.number;
pop_stack ();
}
str = describe_items(sp-1, sp->u.string, live);
pop_n_elems(2);
if (str) push_shared_string (make_shared_string(str));
else push_number(0);
break;
}
CASE(F_UNIQUE_ARRAY); {
extern struct vector
*make_unique PROT((struct vector *arr,char *func,
struct svalue *skipnum));
struct vector *res;
if (num_arg < 3)
res = make_unique((sp-1)->u.vec, sp->u.string, &const0);
else {
res = make_unique ((sp-2)->u.vec, (sp-1)->u.string, sp);
pop_stack ();
}
pop_n_elems(2);
if (res) { push_vector(res); res->ref--; }
else push_number(0);
break;
}
CASE(F_VERSION); {
char buff[12];
sprintf(buff, "%5.5s%02d-DR", GAME_VERSION, PATCH_LEVEL);
push_string(buff, STRING_SHARED);
break;
}
CASE(F_MAP_ARRAY); {
struct vector *res = NULL;
struct mapping *map = NULL;
struct svalue *arg;
arg = sp - num_arg + 1; ob = 0;
if (arg[2].type == T_OBJECT)
ob = arg[2].u.ob;
else if (arg[2].type == T_STRING)
ob = find_object(arg[2].u.string);
if (!ob)
bad_arg (3, F_MAP_ARRAY);
if (arg[0].type == T_POINTER)
res = map_array (arg[0].u.vec, arg[1].u.string, ob,
num_arg > 3 ? sp : (struct svalue *)0);
else if (arg[0].type == T_MAPPING)
map = map_mapping (arg[0].u.map, arg[1].u.string, ob,
num_arg > 3 ? sp : (struct svalue *)0);
else
res = 0;
pop_n_elems (num_arg);
if (res) { push_vector(res); sp->u.vec->ref--; }
else if (map) { push_mapping(map); sp->u.map->ref--; }
else push_number (0);
break;
}
/* ADDED -- 5-91 Raistlin -- Americanized standard LP */
/*
CASE(F_REMOVE_LIVING_NAME)
remove_one_living_name(sp->u.string, current_object);
break;
*/
CASE(F_ROOT);
{
struct object *obj = current_object;
int i;
if (num_arg == 1)
obj = sp->u.ob;
i = (index(obj->name, '#') == NULL);
pop_n_elems(num_arg);
push_number(i);
break;
}
CASE(F_DELETEM);
{
mapping_delete((sp-1)->u.map, sp);
pop_stack();
break;
}
CASE(F_DELETEA);
{
struct svalue *arg = sp - num_arg + 1;
struct vector *ret;
int j = arg[1].u.number;
if (num_arg == 3)
{
if (arg[2].type != T_NUMBER)
bad_arg(3, F_DELETEA);
j = arg[2].u.number;
}
ret = delete_elements(arg[0].u.vec, arg[1].u.number, j);
pop_n_elems(num_arg);
if (ret) { push_vector(ret); ret->ref--; }
else push_number(0);
break;
}
CASE(F_EXISTS);
{
struct stat st;
int offset = 0;
char *file = sp->u.string;
if (*file == '/') offset = 1;
pop_stack();
if (stat(file+offset, &st) != -1)
push_number(1);
else push_number(0);
break;
}
CASE(F_PAD);
{
struct svalue *arg = sp-num_arg+1;
char *new, *str, numbuf[20];
int len = arg[1].u.number, strl;
int right_justify = 0, padchar = ' ';
if (num_arg == 3)
{
if (arg[2].type != T_NUMBER)
bad_arg(3, F_PAD);
padchar = arg[2].u.number;
}
if (arg[0].type == T_NUMBER)
{
sprintf(numbuf, "%d", arg[0].u.number);
str = numbuf;
}
else
str = arg[0].u.string;
if (len < 0)
{
len = -len;
right_justify = 1;
}
if (len == 0)
{
/* Nothing to pad!! */
pop_n_elems(num_arg);
push_constant_string("");
break;
}
strl = strlen(str);
if (strl > len) strl = len;
new = xalloc(len+1);
if (right_justify)
{
memset(new, padchar, len);
if (strl)
strncpy(new+len-strl, str, strl);
}
else
{
if (strl)
strncpy(new, str, len);
if (strl < len)
memset(new+strl, padchar, len-strl);
}
new[len] = '\0';
pop_n_elems(num_arg);
push_shared_string(make_shared_string(new));
free(new);
break;
}
CASE(F_REMOTE_COMMAND);
push_number(remote_command);
break;
CASE(F_IN_EDITOR);
{
struct object *a = command_giver;
if (num_arg==1)
{
a = sp->u.ob;
pop_stack();
}
if (a && a->interactive && a->interactive->ed_buffer)
push_number(1);
else
push_number(0);
break;
}
CASE(F_GRAB_FILE);
{
struct svalue *arg = sp - num_arg + 1;
int start=0, len=0;
struct vector *r;
if (num_arg > 1)
start = arg[1].u.number;
if (num_arg == 3)
len = arg[2].u.number;
r = grab_file(arg[0].u.string, start, len);
pop_n_elems(num_arg);
if (r) { push_vector(r); sp->u.vec->ref--; }
else push_number(0);
break;
}
CASE(F_FILES);
{
struct vector *r;
r = get_files(sp->u.string);
pop_stack();
if (r) { push_vector(r); sp->u.vec->ref--; }
else push_number(0);
break;
}
#ifdef F_POSSESSIVE
CASE(F_POSSESSIVE);
{
struct object *obj = command_giver;
if (num_arg == 1) obj = sp->u.ob;
pop_n_elems(num_arg);
if (!obj || obj->flags & O_DESTRUCTED)
push_number(0);
else
{
struct svalue *r = sapply("query_gender", obj, 0);
if (!r || r->type != T_STRING)
push_constant_string("its");
else
{
if (!strcmp(r->u.string, "male"))
push_constant_string("his");
else if (!strcmp(r->u.string, "female"))
push_constant_string("her");
else push_constant_string("its");
}
}
break;
}
#endif
#ifdef F_OBJECTIVE
CASE(F_OBJECTIVE);
{
struct object *obj;
if (num_arg == 1) obj = sp->u.ob;
else obj = command_giver;
pop_n_elems(num_arg);
if (!obj || obj->flags & O_DESTRUCTED)
push_number(0);
else
{
struct svalue *r = sapply("query_gender", obj, 0);
if (!r || r->type != T_STRING)
push_constant_string("it");
else
{
if (!strcmp(r->u.string, "male"))
push_constant_string("him");
else if (!strcmp(r->u.string, "female"))
push_constant_string("her");
else push_constant_string("it");
}
}
break;
}
#endif
#ifdef F_SUBJECTIVE
CASE(F_SUBJECTIVE);
{
struct object *obj;
if (num_arg == 1) obj = sp->u.ob;
else obj = command_giver;
pop_n_elems(num_arg);
if (!obj || obj->flags & O_DESTRUCTED)
push_number(0);
else
{
struct svalue *r = sapply("query_gender", obj, 0);
if (!r || r->type != T_STRING)
push_constant_string("it");
else
{
if (!strcmp(r->u.string, "male"))
push_constant_string("he");
else if (!strcmp(r->u.string, "female"))
push_constant_string("she");
else push_constant_string("it");
}
}
break;
}
#endif
CASE(F_REMOVE_INTERACTIVE)
remove_interactive_player(sp->u.ob);
break;
CASE(F_CP)
{
int i;
i = copy_file((sp-1)->u.string, sp->u.string);
pop_n_elems(2);
push_number(i);
break;
}
CASE(F_RENAME);
{
int i;
i = rename_file((sp-1)->u.string, sp->u.string);
pop_n_elems(2);
push_number(i);
break;
}
CASE(F_TYPEOF);
{
char *ret;
int t;
t = sp->type;
pop_stack();
switch(t) {
case T_OBJECT:
ret = "object";
break;
case T_NUMBER:
ret = "int";
break;
case T_POINTER:
ret = "vector";
break;
case T_STRING:
ret = "string";
break;
default: ret = "";
}
push_constant_string(ret);
break;
}
CASE(F_CALLER);
{
struct object *o = caller();
if (o)
push_object(o, "F_CALLER");
else push_number(0);
break;
}
CASE(F_READ_FILE);
{
char *str;
struct svalue *arg = sp- num_arg + 1;
int start = 0, len = 0;
if (num_arg > 1)
start = arg[1].u.number;
if (num_arg == 3)
{
if (arg[2].type != T_NUMBER)
bad_arg(2, instruction);
len = arg[2].u.number;
}
/* back compatibility with American versions */
#ifdef COMPAT_MODE
else if (num_arg == 2)
len = 1;
#endif
str = read_file(arg[0].u.string, start, len);
pop_n_elems(num_arg);
if (str == 0)
push_number(0);
else {
push_string(str, STRING_MALLOC);
Showing the first 100 KB of 147.1 KB. Download the archive for the full file.