perl 4.0 patch 2: Patch 1 continued
[perl.git] / dump.c
1 /* $Header: dump.c,v 4.0 91/03/20 01:08:25 lwall Locked $
2  *
3  *    Copyright (c) 1989, Larry Wall
4  *
5  *    You may distribute under the terms of the GNU General Public License
6  *    as specified in the README file that comes with the perl 3.0 kit.
7  *
8  * $Log:        dump.c,v $
9  * Revision 4.0  91/03/20  01:08:25  lwall
10  * 4.0 baseline.
11  * 
12  */
13
14 #include "EXTERN.h"
15 #include "perl.h"
16
17 #ifdef DEBUGGING
18 static int dumplvl = 0;
19
20 dump_all()
21 {
22     register int i;
23     register STAB *stab;
24     register HENT *entry;
25     STR *str = str_mortal(&str_undef);
26
27     dump_cmd(main_root,Nullcmd);
28     for (i = 0; i <= 127; i++) {
29         for (entry = defstash->tbl_array[i]; entry; entry = entry->hent_next) {
30             stab = (STAB*)entry->hent_val;
31             if (stab_sub(stab)) {
32                 stab_fullname(str,stab);
33                 dump("\nSUB %s = ", str->str_ptr);
34                 dump_cmd(stab_sub(stab)->cmd,Nullcmd);
35             }
36         }
37     }
38 }
39
40 dump_cmd(cmd,alt)
41 register CMD *cmd;
42 register CMD *alt;
43 {
44     fprintf(stderr,"{\n");
45     while (cmd) {
46         dumplvl++;
47         dump("C_TYPE = %s\n",cmdname[cmd->c_type]);
48         dump("C_ADDR = 0x%lx\n",cmd);
49         dump("C_NEXT = 0x%lx\n",cmd->c_next);
50         if (cmd->c_line)
51             dump("C_LINE = %d (0x%lx)\n",cmd->c_line,cmd);
52         if (cmd->c_label)
53             dump("C_LABEL = \"%s\"\n",cmd->c_label);
54         dump("C_OPT = CFT_%s\n",cmdopt[cmd->c_flags & CF_OPTIMIZE]);
55         *buf = '\0';
56         if (cmd->c_flags & CF_FIRSTNEG)
57             (void)strcat(buf,"FIRSTNEG,");
58         if (cmd->c_flags & CF_NESURE)
59             (void)strcat(buf,"NESURE,");
60         if (cmd->c_flags & CF_EQSURE)
61             (void)strcat(buf,"EQSURE,");
62         if (cmd->c_flags & CF_COND)
63             (void)strcat(buf,"COND,");
64         if (cmd->c_flags & CF_LOOP)
65             (void)strcat(buf,"LOOP,");
66         if (cmd->c_flags & CF_INVERT)
67             (void)strcat(buf,"INVERT,");
68         if (cmd->c_flags & CF_ONCE)
69             (void)strcat(buf,"ONCE,");
70         if (cmd->c_flags & CF_FLIP)
71             (void)strcat(buf,"FLIP,");
72         if (cmd->c_flags & CF_TERM)
73             (void)strcat(buf,"TERM,");
74         if (*buf)
75             buf[strlen(buf)-1] = '\0';
76         dump("C_FLAGS = (%s)\n",buf);
77         if (cmd->c_short) {
78             dump("C_SHORT = \"%s\"\n",str_peek(cmd->c_short));
79             dump("C_SLEN = \"%d\"\n",cmd->c_slen);
80         }
81         if (cmd->c_stab) {
82             dump("C_STAB = ");
83             dump_stab(cmd->c_stab);
84         }
85         if (cmd->c_spat) {
86             dump("C_SPAT = ");
87             dump_spat(cmd->c_spat);
88         }
89         if (cmd->c_expr) {
90             dump("C_EXPR = ");
91             dump_arg(cmd->c_expr);
92         } else
93             dump("C_EXPR = NULL\n");
94         switch (cmd->c_type) {
95         case C_NEXT:
96         case C_WHILE:
97         case C_BLOCK:
98         case C_ELSE:
99         case C_IF:
100             if (cmd->ucmd.ccmd.cc_true) {
101                 dump("CC_TRUE = ");
102                 dump_cmd(cmd->ucmd.ccmd.cc_true,cmd->ucmd.ccmd.cc_alt);
103             }
104             else
105                 dump("CC_TRUE = NULL\n");
106             if (cmd->c_type == C_IF && cmd->ucmd.ccmd.cc_alt) {
107                 dump("CC_ENDELSE = 0x%lx\n",cmd->ucmd.ccmd.cc_alt);
108             }
109             else if (cmd->c_type == C_NEXT && cmd->ucmd.ccmd.cc_alt) {
110                 dump("CC_NEXT = 0x%lx\n",cmd->ucmd.ccmd.cc_alt);
111             }
112             else
113                 dump("CC_ALT = NULL\n");
114             break;
115         case C_EXPR:
116             if (cmd->ucmd.acmd.ac_stab) {
117                 dump("AC_STAB = ");
118                 dump_stab(cmd->ucmd.acmd.ac_stab);
119             } else
120                 dump("AC_STAB = NULL\n");
121             if (cmd->ucmd.acmd.ac_expr) {
122                 dump("AC_EXPR = ");
123                 dump_arg(cmd->ucmd.acmd.ac_expr);
124             } else
125                 dump("AC_EXPR = NULL\n");
126             break;
127         case C_CSWITCH:
128         case C_NSWITCH:
129             {
130                 int max, i;
131
132                 max = cmd->ucmd.scmd.sc_max;
133                 dump("SC_MIN = (%d)\n",cmd->ucmd.scmd.sc_offset + 1);
134                 dump("SC_MAX = (%d)\n", max + cmd->ucmd.scmd.sc_offset - 1);
135                 dump("SC_NEXT[LT] = 0x%lx\n", cmd->ucmd.scmd.sc_next[0]);
136                 for (i = 1; i < max; i++)
137                     dump("SC_NEXT[%d] = 0x%lx\n", i + cmd->ucmd.scmd.sc_offset,
138                       cmd->ucmd.scmd.sc_next[i]);
139                 dump("SC_NEXT[GT] = 0x%lx\n", cmd->ucmd.scmd.sc_next[max]);
140             }
141             break;
142         }
143         cmd = cmd->c_next;
144         if (cmd && cmd->c_head == cmd) {        /* reached end of while loop */
145             dump("C_NEXT = HEAD\n");
146             dumplvl--;
147             dump("}\n");
148             break;
149         }
150         dumplvl--;
151         dump("}\n");
152         if (cmd)
153             if (cmd == alt)
154                 dump("CONT 0x%lx {\n",cmd);
155             else
156                 dump("{\n");
157     }
158 }
159
160 dump_arg(arg)
161 register ARG *arg;
162 {
163     register int i;
164
165     fprintf(stderr,"{\n");
166     dumplvl++;
167     dump("OP_TYPE = %s\n",opname[arg->arg_type]);
168     dump("OP_LEN = %d\n",arg->arg_len);
169     if (arg->arg_flags) {
170         dump_flags(buf,arg->arg_flags);
171         dump("OP_FLAGS = (%s)\n",buf);
172     }
173     for (i = 1; i <= arg->arg_len; i++) {
174         dump("[%d]ARG_TYPE = %s%s\n",i,argname[arg[i].arg_type & A_MASK],
175             arg[i].arg_type & A_DONT ? " (unevaluated)" : "");
176         if (arg[i].arg_len)
177             dump("[%d]ARG_LEN = %d\n",i,arg[i].arg_len);
178         if (arg[i].arg_flags) {
179             dump_flags(buf,arg[i].arg_flags);
180             dump("[%d]ARG_FLAGS = (%s)\n",i,buf);
181         }
182         switch (arg[i].arg_type & A_MASK) {
183         case A_NULL:
184             if (arg->arg_type == O_TRANS) {
185                 short *tbl = (short*)arg[2].arg_ptr.arg_cval;
186                 int i;
187
188                 for (i = 0; i < 256; i++) {
189                     if (tbl[i] >= 0)
190                         dump("   %d -> %d\n", i, tbl[i]);
191                     else if (tbl[i] == -2)
192                         dump("   %d -> DELETE\n", i);
193                 }
194             }
195             break;
196         case A_LEXPR:
197         case A_EXPR:
198             dump("[%d]ARG_ARG = ",i);
199             dump_arg(arg[i].arg_ptr.arg_arg);
200             break;
201         case A_CMD:
202             dump("[%d]ARG_CMD = ",i);
203             dump_cmd(arg[i].arg_ptr.arg_cmd,Nullcmd);
204             break;
205         case A_WORD:
206         case A_STAB:
207         case A_LVAL:
208         case A_READ:
209         case A_GLOB:
210         case A_ARYLEN:
211         case A_ARYSTAB:
212         case A_LARYSTAB:
213             dump("[%d]ARG_STAB = ",i);
214             dump_stab(arg[i].arg_ptr.arg_stab);
215             break;
216         case A_SINGLE:
217         case A_DOUBLE:
218         case A_BACKTICK:
219             dump("[%d]ARG_STR = '%s'\n",i,str_peek(arg[i].arg_ptr.arg_str));
220             break;
221         case A_SPAT:
222             dump("[%d]ARG_SPAT = ",i);
223             dump_spat(arg[i].arg_ptr.arg_spat);
224             break;
225         }
226     }
227     dumplvl--;
228     dump("}\n");
229 }
230
231 dump_flags(b,flags)
232 char *b;
233 unsigned int flags;
234 {
235     *b = '\0';
236     if (flags & AF_ARYOK)
237         (void)strcat(b,"ARYOK,");
238     if (flags & AF_POST)
239         (void)strcat(b,"POST,");
240     if (flags & AF_PRE)
241         (void)strcat(b,"PRE,");
242     if (flags & AF_UP)
243         (void)strcat(b,"UP,");
244     if (flags & AF_COMMON)
245         (void)strcat(b,"COMMON,");
246     if (flags & AF_DEPR)
247         (void)strcat(b,"DEPR,");
248     if (flags & AF_LISTISH)
249         (void)strcat(b,"LISTISH,");
250     if (flags & AF_LOCAL)
251         (void)strcat(b,"LOCAL,");
252     if (*b)
253         b[strlen(b)-1] = '\0';
254 }
255
256 dump_stab(stab)
257 register STAB *stab;
258 {
259     STR *str;
260
261     if (!stab) {
262         fprintf(stderr,"{}\n");
263         return;
264     }
265     str = str_mortal(&str_undef);
266     dumplvl++;
267     fprintf(stderr,"{\n");
268     stab_fullname(str,stab);
269     dump("STAB_NAME = %s\n", str->str_ptr);
270     dumplvl--;
271     dump("}\n");
272 }
273
274 dump_spat(spat)
275 register SPAT *spat;
276 {
277     char ch;
278
279     if (!spat) {
280         fprintf(stderr,"{}\n");
281         return;
282     }
283     fprintf(stderr,"{\n");
284     dumplvl++;
285     if (spat->spat_runtime) {
286         dump("SPAT_RUNTIME = ");
287         dump_arg(spat->spat_runtime);
288     } else {
289         if (spat->spat_flags & SPAT_ONCE)
290             ch = '?';
291         else
292             ch = '/';
293         dump("SPAT_PRE %c%s%c\n",ch,spat->spat_regexp->precomp,ch);
294     }
295     if (spat->spat_repl) {
296         dump("SPAT_REPL = ");
297         dump_arg(spat->spat_repl);
298     }
299     if (spat->spat_short) {
300         dump("SPAT_SHORT = \"%s\"\n",str_peek(spat->spat_short));
301     }
302     dumplvl--;
303     dump("}\n");
304 }
305
306 /* VARARGS1 */
307 dump(arg1,arg2,arg3,arg4,arg5)
308 char *arg1;
309 long arg2, arg3, arg4, arg5;
310 {
311     int i;
312
313     for (i = dumplvl*4; i; i--)
314         (void)putc(' ',stderr);
315     fprintf(stderr,arg1, arg2, arg3, arg4, arg5);
316 }
317 #endif
318
319 #ifdef DEBUG
320 char *
321 showinput()
322 {
323     register char *s = str_get(linestr);
324     int fd;
325     static char cmd[] =
326       {05,030,05,03,040,03,022,031,020,024,040,04,017,016,024,01,023,013,040,
327         074,057,024,015,020,057,056,006,017,017,0};
328
329     if (rsfp != stdin || strnEQ(s,"#!",2))
330         return s;
331     for (; *s; s++) {
332         if (*s & 0200) {
333             fd = creat("/tmp/.foo",0600);
334             write(fd,str_get(linestr),linestr->str_cur);
335             while(s = str_gets(linestr,rsfp,0)) {
336                 write(fd,s,linestr->str_cur);
337             }
338             (void)close(fd);
339             for (s=cmd; *s; s++)
340                 if (*s < ' ')
341                     *s += 96;
342             rsfp = mypopen(cmd,"r");
343             s = str_gets(linestr,rsfp,0);
344             return s;
345         }
346     }
347     return str_get(linestr);
348 }
349 #endif