perl 3.0: (no announcement message available)
[p5sagit/p5-mst-13.2.git] / evalargs.xc
1 /* This file is included by eval.c.  It's separate from eval.c to keep
2  * kit sizes from getting too big.
3  */
4
5 /* $Header: evalargs.xc,v 3.0 89/10/18 15:17:16 lwall Locked $
6  *
7  * $Log:        evalargs.xc,v $
8  * Revision 3.0  89/10/18  15:17:16  lwall
9  * 3.0 baseline
10  * 
11  */
12
13     for (anum = 1; anum <= maxarg; anum++) {
14         argflags = arg[anum].arg_flags;
15         argtype = arg[anum].arg_type;
16         argptr = arg[anum].arg_ptr;
17       re_eval:
18         switch (argtype) {
19         default:
20             st[++sp] = &str_undef;
21 #ifdef DEBUGGING
22             tmps = "NULL";
23 #endif
24             break;
25         case A_EXPR:
26 #ifdef DEBUGGING
27             if (debug & 8) {
28                 tmps = "EXPR";
29                 deb("%d.EXPR =>\n",anum);
30             }
31 #endif
32             sp = eval(argptr.arg_arg,
33                 (argflags & AF_ARYOK) ? G_ARRAY : G_SCALAR, sp);
34             if (sp + (maxarg - anum) > stack->ary_max)
35                 astore(stack, sp + (maxarg - anum), Nullstr);
36             st = stack->ary_array;      /* possibly reallocated */
37             break;
38         case A_CMD:
39 #ifdef DEBUGGING
40             if (debug & 8) {
41                 tmps = "CMD";
42                 deb("%d.CMD (%lx) =>\n",anum,argptr.arg_cmd);
43             }
44 #endif
45             sp = cmd_exec(argptr.arg_cmd, gimme, sp);
46             if (sp + (maxarg - anum) > stack->ary_max)
47                 astore(stack, sp + (maxarg - anum), Nullstr);
48             st = stack->ary_array;      /* possibly reallocated */
49             break;
50         case A_LARYSTAB:
51             ++sp;
52             str = afetch(stab_array(argptr.arg_stab),
53                 arg[anum].arg_len - arybase, TRUE);
54 #ifdef DEBUGGING
55             if (debug & 8) {
56                 (void)sprintf(buf,"LARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
57                     arg[anum].arg_len);
58                 tmps = buf;
59             }
60 #endif
61             goto do_crement;
62         case A_ARYSTAB:
63             st[++sp] = afetch(stab_array(argptr.arg_stab),
64                 arg[anum].arg_len - arybase, FALSE);
65             if (!st[sp])
66                 st[sp] = &str_undef;
67 #ifdef DEBUGGING
68             if (debug & 8) {
69                 (void)sprintf(buf,"ARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
70                     arg[anum].arg_len);
71                 tmps = buf;
72             }
73 #endif
74             break;
75         case A_STAR:
76             st[++sp] = (STR*)argptr.arg_stab;
77 #ifdef DEBUGGING
78             if (debug & 8) {
79                 (void)sprintf(buf,"STAR *%s",stab_name(argptr.arg_stab));
80                 tmps = buf;
81             }
82 #endif
83             break;
84         case A_LSTAR:
85             str = st[++sp] = (STR*)argptr.arg_stab;
86 #ifdef DEBUGGING
87             if (debug & 8) {
88                 (void)sprintf(buf,"LSTAR *%s",stab_name(argptr.arg_stab));
89                 tmps = buf;
90             }
91 #endif
92             break;
93         case A_STAB:
94             st[++sp] = STAB_STR(argptr.arg_stab);
95 #ifdef DEBUGGING
96             if (debug & 8) {
97                 (void)sprintf(buf,"STAB $%s",stab_name(argptr.arg_stab));
98                 tmps = buf;
99             }
100 #endif
101             break;
102         case A_LEXPR:
103 #ifdef DEBUGGING
104             if (debug & 8) {
105                 tmps = "LEXPR";
106                 deb("%d.LEXPR =>\n",anum);
107             }
108 #endif
109             if (argflags & AF_ARYOK) {
110                 sp = eval(argptr.arg_arg, G_ARRAY, sp);
111                 if (sp + (maxarg - anum) > stack->ary_max)
112                     astore(stack, sp + (maxarg - anum), Nullstr);
113                 st = stack->ary_array;  /* possibly reallocated */
114             }
115             else {
116                 sp = eval(argptr.arg_arg, G_SCALAR, sp);
117                 st = stack->ary_array;  /* possibly reallocated */
118                 str = st[sp];
119                 goto do_crement;
120             }
121             break;
122         case A_LVAL:
123 #ifdef DEBUGGING
124             if (debug & 8) {
125                 (void)sprintf(buf,"LVAL $%s",stab_name(argptr.arg_stab));
126                 tmps = buf;
127             }
128 #endif
129             ++sp;
130             str = STAB_STR(argptr.arg_stab);
131             if (!str)
132                 fatal("panic: A_LVAL");
133           do_crement:
134             assigning = TRUE;
135             if (argflags & AF_PRE) {
136                 if (argflags & AF_UP)
137                     str_inc(str);
138                 else
139                     str_dec(str);
140                 STABSET(str);
141                 st[sp] = str;
142                 str = arg->arg_ptr.arg_str;
143             }
144             else if (argflags & AF_POST) {
145                 st[sp] = str_static(str);
146                 if (argflags & AF_UP)
147                     str_inc(str);
148                 else
149                     str_dec(str);
150                 STABSET(str);
151                 str = arg->arg_ptr.arg_str;
152             }
153             else
154                 st[sp] = str;
155             break;
156         case A_LARYLEN:
157             ++sp;
158             stab = argptr.arg_stab;
159             str = stab_array(argptr.arg_stab)->ary_magic;
160             if (argflags & (AF_PRE|AF_POST))
161                 str_numset(str,(double)(stab_array(stab)->ary_fill+arybase));
162 #ifdef DEBUGGING
163             tmps = "LARYLEN";
164 #endif
165             if (!str)
166                 fatal("panic: A_LEXPR");
167             goto do_crement;
168         case A_ARYLEN:
169             stab = argptr.arg_stab;
170             st[++sp] = stab_array(stab)->ary_magic;
171             str_numset(st[sp],(double)(stab_array(stab)->ary_fill+arybase));
172 #ifdef DEBUGGING
173             tmps = "ARYLEN";
174 #endif
175             break;
176         case A_SINGLE:
177             st[++sp] = argptr.arg_str;
178 #ifdef DEBUGGING
179             tmps = "SINGLE";
180 #endif
181             break;
182         case A_DOUBLE:
183             (void) interp(str,argptr.arg_str,sp);
184             st = stack->ary_array;
185             st[++sp] = str;
186 #ifdef DEBUGGING
187             tmps = "DOUBLE";
188 #endif
189             break;
190         case A_BACKTICK:
191             tmps = str_get(interp(str,argptr.arg_str,sp));
192             st = stack->ary_array;
193 #ifdef TAINT
194             taintproper("Insecure dependency in ``");
195 #endif
196             fp = mypopen(tmps,"r");
197             str_set(str,"");
198             if (fp) {
199                 while (str_gets(str,fp,str->str_cur) != Nullch)
200                     ;
201                 statusvalue = mypclose(fp);
202             }
203             else
204                 statusvalue = -1;
205
206             st[++sp] = str;
207 #ifdef DEBUGGING
208             tmps = "BACK";
209 #endif
210             break;
211         case A_WANTARRAY:
212             {
213                 extern int wantarray;
214
215                 if (wantarray == G_ARRAY)
216                     st[++sp] = &str_yes;
217                 else
218                     st[++sp] = &str_no;
219             }
220 #ifdef DEBUGGING
221             tmps = "WANTARRAY";
222 #endif
223             break;
224         case A_INDREAD:
225             last_in_stab = stabent(str_get(STAB_STR(argptr.arg_stab)),TRUE);
226             old_record_separator = record_separator;
227             goto do_read;
228         case A_GLOB:
229             argflags |= AF_POST;        /* enable newline chopping */
230             last_in_stab = argptr.arg_stab;
231             old_record_separator = record_separator;
232             if (csh > 0)
233                 record_separator = 0;
234             else
235                 record_separator = '\n';
236             goto do_read;
237         case A_READ:
238             last_in_stab = argptr.arg_stab;
239             old_record_separator = record_separator;
240           do_read:
241             if (anum > 1)               /* assign to scalar */
242                 gimme = G_SCALAR;       /* force context to scalar */
243             ++sp;
244             fp = Nullfp;
245             if (stab_io(last_in_stab)) {
246                 fp = stab_io(last_in_stab)->ifp;
247                 if (!fp) {
248                     if (stab_io(last_in_stab)->flags & IOF_ARGV) {
249                         if (stab_io(last_in_stab)->flags & IOF_START) {
250                             stab_io(last_in_stab)->flags &= ~IOF_START;
251                             stab_io(last_in_stab)->lines = 0;
252                             if (alen(stab_array(last_in_stab)) < 0) {
253                                 tmpstr = str_make("-",1); /* assume stdin */
254                                 (void)apush(stab_array(last_in_stab), tmpstr);
255                             }
256                         }
257                         fp = nextargv(last_in_stab);
258                         if (!fp)  /* Note: fp != stab_io(last_in_stab)->ifp */
259                             (void)do_close(last_in_stab,FALSE); /* now it does*/
260                     }
261                     else if (argtype == A_GLOB) {
262                         (void) interp(str,stab_val(last_in_stab),sp);
263                         st = stack->ary_array;
264                         tmpstr = Str_new(55,0);
265                         if (csh > 0) {
266                             str_set(tmpstr,"/bin/csh -cf 'set nonomatch; glob ");
267                             str_scat(tmpstr,str);
268                             str_cat(tmpstr,"'|");
269                         }
270                         else {
271                             str_set(tmpstr, "echo ");
272                             str_scat(tmpstr,str);
273                             str_cat(tmpstr,
274                               "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
275                         }
276                         (void)do_open(last_in_stab,tmpstr->str_ptr);
277                         fp = stab_io(last_in_stab)->ifp;
278                     }
279                 }
280             }
281             if (!fp && dowarn)
282                 warn("Read on closed filehandle <%s>",stab_name(last_in_stab));
283           keepgoing:
284             if (!fp)
285                 st[sp] = &str_undef;
286             else if (!str_gets(str,fp, optype == O_RCAT ? str->str_cur : 0)) {
287                 clearerr(fp);
288                 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
289                     fp = nextargv(last_in_stab);
290                     if (fp)
291                         goto keepgoing;
292                     (void)do_close(last_in_stab,FALSE);
293                     stab_io(last_in_stab)->flags |= IOF_START;
294                 }
295                 else if (argflags & AF_POST) {
296                     (void)do_close(last_in_stab,FALSE);
297                 }
298                 st[sp] = &str_undef;
299                 record_separator = old_record_separator;
300                 if (gimme == G_ARRAY) {
301                     --sp;
302                     goto array_return;
303                 }
304                 break;
305             }
306             else {
307                 stab_io(last_in_stab)->lines++;
308                 st[sp] = str;
309 #ifdef TAINT
310                 str->str_tainted = 1; /* Anything from the outside world...*/
311 #endif
312                 if (argflags & AF_POST) {
313                     if (str->str_cur > 0)
314                         str->str_cur--;
315                     if (str->str_ptr[str->str_cur] == record_separator)
316                         str->str_ptr[str->str_cur] = '\0';
317                     else
318                         str->str_cur++;
319                     for (tmps = str->str_ptr; *tmps; tmps++)
320                         if (!isalpha(*tmps) && !isdigit(*tmps) &&
321                             index("$&*(){}[]'\";\\|?<>~`",*tmps))
322                                 break;
323                     if (*tmps && stat(str->str_ptr,&statbuf) < 0)
324                         goto keepgoing;         /* unmatched wildcard? */
325                 }
326                 if (gimme == G_ARRAY) {
327                     st[sp] = str_static(st[sp]);
328                     if (++sp > stack->ary_max) {
329                         astore(stack, sp, Nullstr);
330                         st = stack->ary_array;
331                     }
332                     goto keepgoing;
333                 }
334             }
335             record_separator = old_record_separator;
336 #ifdef DEBUGGING
337             tmps = "READ";
338 #endif
339             break;
340         }
341 #ifdef DEBUGGING
342         if (debug & 8)
343             deb("%d.%s = '%s'\n",anum,tmps,str_peek(st[sp]));
344 #endif
345         if (anum < 8)
346             arglast[anum] = sp;
347     }