Debian Codegraph @

terps/agility/disassemble.c

raw symbols SCIP
1 /* disassemble.c-- Metacommand debugging output */
2 /* Copyright (C) 1996-1999,2001 Robert Masenten */
3 /* This program may be redistributed under the terms of the
4 GNU General Public License, version 2; see agility.h for details. */
5 /* */
6 /* This is part of the source for AGiliTy: the (Mostly) Universal */
7 /* AGT Interpreter */
8
9 /* This contains the disassembler used by AGiliTY and AGTOut. */
10
11 #include <stdarg.h>
12 #include <ctype.h>
13 #include "agility.h"
14
15
16 void dbgprintf(const char *fmt,...)
17 {
18 va_list vp;
19 char buff[300];
20
21 va_start(vp,fmt);
22 vsprintf(buff,fmt,vp);
23 va_end(vp);
24
25 debugout(buff);
26 }
27
28
29 static void print_msg(descr_ptr dp)
30 {
31 int j;
32 descr_line *txt;
33
34 txt=read_descr(dp.start,dp.size);
35 if (txt!=NULL) {
36 for(j=0;txt[j]!=NULL;j++)
37 {
38 dbgprintf("\n");
39 debugout(txt[j]);
40 }
41 }
42 free_descr(txt);
43 }
44
45
46 static char *getname(int inum)
47 /* Name should be 20 chars or less */
48 {
49 if (inum==0) return rstrdup("* 0 *");
50 return objname(inum);
51 }
52
53
54 extern integer dobj, iobj, actor;
55
56 void print_special_obj(int i)
57 /* This is called by the disassembler in agtdbg.c */
58 /* i=0 NOUN, 1 OBJECT, 2 NAME */
59 {
60 int dval;
61 char *s;
62 switch(i)
63 {
64 case 0: dval=dobj; dbgprintf("NOUN"); break;
65 case 1: dval=iobj; dbgprintf("OBJECT"); break;
66 case 2: dval=actor; dbgprintf("NAME"); break;
67 default:
68 dval=0; /* Silence compiler warnings. */
69 fatal("INTERNAL ERROR: Invalid *dval* in print_special_obj.");
70 }
71 if (dbgflagptr==NULL)
72 /* This determines whether we are linked with agtout or agil */
73 return;
74 s=getname(dval);
75 dbgprintf("(%d:%s)",dval,s);
76 rfree(s);
77 }
78
79 #define printval(str,index,ptr) {dbgprintf("[%s%d",str,index);\
80 if (ptr==NULL) dbgprintf("]");\
81 else dbgprintf("=%ld]",(long)ptr[index]);}
82
83 int argout(int dtype, int dval, int optype)
84 {
85 char *s;
86
87 if (dtype&AGT_VAR) dtype=AGT_VAR;
88
89 if ((optype&3)==1) /* variable */
90 dtype=AGT_VAR;
91 if (optype&2) {/* NOUN or OBJECT */
92 if (dtype>=64 && dtype!=AGT_NUM)
93 dbgprintf("ILL:");
94 if (optype==2)
95 print_special_obj(0); /* NOUN */
96 else
97 print_special_obj(1); /* OBJECT */
98 return 0;
99 }
100
101 if (!interp_arg)
102 dbgprintf("%d",dval);
103 else {
104 if (dtype<64) {
105 if (dval==-1)
106 print_special_obj(2); /* NAME */
107 else {
108 s=getname(dval);
109 if (dtype&(AGT_ITEM|AGT_CREAT|AGT_SELF|AGT_WORN))
110 dbgprintf("<%d:%s>",dval,s);
111 else
112 dbgprintf("{%d:%s}",dval,s);
113 rfree(s);
114 }
115 } else if ( (dtype&AGT_VAR)!=0) {
116 if (dval==-1)
117 print_tos();
118 else
119 printval("Var",dval,dbgvarptr);
120 } else switch(dtype)
121 {
122 case AGT_TIME:
123 dbgprintf("%2d:%2d",dval/100,dval%100);break;
124 case AGT_NUM: /* Numeric */
125 dbgprintf("%d",dval);break;
126 case AGT_FLAG: /* Flag */
127 printval("Flg",dval,dbgflagptr);break;
128 case AGT_ROOMFLAG: /* Roomflag */
129 dbgprintf("RoomFlag%d",dval);break;
130 case AGT_QUEST: /* Question */
131 if (dval<=MaxQuestion && dval>=1 && question!=NULL) {
132 dbgprintf("\nQ%d:%s\n",dval,question[dval-1]);
133 dbgprintf("[A:%s]",answer[dval-1]);
134 } else if (quest_ptr!=NULL) {
135 dbgprintf("\nQ%d: ",dval);
136 print_msg(quest_ptr[dval-1]);
137 dbgprintf("[A:");
138 print_msg(ans_ptr[dval-1]);
139 }
140 break;
141 case AGT_MSG: /* Message */
142 if (dval>last_message || dval<1 || msg_ptr==NULL)
143 dbgprintf("ILLEGAL MESSAGE");
144 else {
145 dbgprintf("(Msg%d)",dval);
146 if (!dbg_nomsg)
147 print_msg(msg_ptr[dval-1]);
148 }
149 break;
150 case AGT_ERR: /* Message */
151 if (dval>NUM_ERR || dval<1 || err_ptr==NULL)
152 dbgprintf("ILLEGAL MESSAGE");
153 else {
154 dbgprintf("(Std%d)",dval);
155 if (!dbg_nomsg)
156 print_msg(err_ptr[dval-1]);
157 }
158 break;
159 case AGT_STR: /* String */
160 if (dval-1>=MAX_USTR || userstr==NULL)
161 dbgprintf("ILLEGAL STRING");
162 else
163 dbgprintf("\nStr%d:%s",dval,userstr[dval]);
164 break;
165 case AGT_CNT: /* Counter */
166 printval("Cnt",dval,dbgcntptr);break;
167 case AGT_DIR: /* Direction */
168 if (dval>=1 && dval<=13)
169 dbgprintf("%s",exitname[dval-1]);
170 else dbgprintf("ILL_DIR(%d)",dval);
171 break;
172 case AGT_SUB: /* Subroutine */
173 dbgprintf("Subroutine %d",dval);break;
174 case AGT_PIC: /* Picture */
175 case AGT_PIX:
176 dbgprintf("Picture #%d",dval);break;
177 case AGT_FONT: /* Font */
178 dbgprintf("Font #%d",dval);break;
179 case AGT_SONG: /* Song */
180 dbgprintf("Song #%d",dval);break;
181 case AGT_OBJFLAG:
182 dbgprintf("ObjFlag%d",dval);break;
183 case AGT_OBJPROP:
184 dbgprintf("ObjProp%d",dval);break;
185 case AGT_ATTR:
186 if (dval<0 || dval>=NUM_ATTR)
187 dbgprintf("UnkownAttr%d",dval);
188 else
189 dbgprintf("%s",attrlist[dval].name);
190 break;
191 case AGT_PROP:
192 if (dval<0 || dval>=NUM_PROP)
193 dbgprintf("UnknownProp%d",dval);
194 else
195 dbgprintf("%s",proplist[dval].name);
196 break;
197 case AGT_EXIT:
198 if (dval>=exitmsg_base)
199 argout(AGT_MSG,dval-exitmsg_base,0);
200 else
201 argout(AGT_ROOM,dval,0);
202 break;
203 default:
204 dbgprintf("?+%d",dval);
205 }
206 }
207 return 1;
208 }
209
210
211 void debug_newline(integer op, rbool first_nl)
212 {
213 rbool early_nl;
214
215 if (!dbg_nomsg) return;
216 early_nl=(op==1008||op==1027||op==1083||op==1105
217 ||(op>=1126&&op<=1131));
218 if (early_nl==first_nl)
219 debugout("\n");
220 }
221
222
223 void debug_cmd_out(int ip,integer op,int arg1,int arg2, int optype)
224 {
225 int j;
226 const opdef *opdata;
227 rbool save_dbg_nomsg;
228
229 dbgprintf(" %2d:",ip);
230 save_dbg_nomsg=0; /* Just to silence compiler warnings. */
231
232 opdata=get_opdef(op);
233 if (opdata==&illegal_def)
234 dbgprintf("ILLEGAL %d\n",op);
235 else {
236 if (op>=END_ACT) dbgprintf("!"); /* "Terminal" Actions */
237 else if (op<=MAX_COND) dbgprintf("?"); /* Condition */
238 if (op==1063) { /* RandomMessage needs special handling */
239 save_dbg_nomsg=dbg_nomsg;
240 dbg_nomsg=1;
241 }
242 dbgprintf("%s",opdata->opcode);
243 for(j=0;j<opdata->argnum;j++) {
244 dbgprintf("\t");
245 argout(j==0 ? opdata->arg1 : opdata->arg2 , j==0 ? arg1 : arg2,
246 optype>>2);
247 optype<<=2;
248 }
249 if (op==1063)
250 dbg_nomsg=save_dbg_nomsg;
251 }
252 debug_newline(op,1);
253 }
254
255
256 void debug_head(int i)
257 {
258 int v, w, a;
259
260 v=verb_code(command[i].verbcmd);
261 if (v>=BASE_VERB && v<BASE_VERB+DUMB_VERB && syntbl[synlist[v]]!=0)
262 w=syntbl[synlist[v]];
263 else w=command[i].verbcmd;
264 if (command[i].actor>0) {
265 dbgprintf("CMD %d: ",i);
266 a=command[i].actor;
267 } else {
268 dbgprintf("REDIR: ");
269 a=-command[i].actor;
270 }
271
272 if (a==2)
273 dbgprintf("anybody, ");
274 else if (a>2) {
275 char *name;
276 name=objname(a);
277 name[0]=toupper(name[0]);
278 dbgprintf("%s, ",name);
279 rfree(name);
280 }
281
282 dbgprintf("%s ", w==0 ? "any" : dict[w]);
283 if (command[i].noun_adj!=0)
284 dbgprintf("%s ",gdict(command[i].noun_adj) );
285 dbgprintf("%s %s ",gdict(command[i].nouncmd),
286 (ver==3)? gdict(command[i].prep) : "->");
287 if (command[i].obj_adj!=0)
288 dbgprintf("%s ",gdict(command[i].obj_adj) );
289 dbgprintf("%s\n",gdict(command[i].objcmd) );
290
291 }
292
293
294