1 /* This file is included by eval.c. It's separate from eval.c to keep
2 * kit sizes from getting too big.
5 /* $Header: evalargs.xc,v 3.0.1.7 90/10/15 16:48:11 lwall Locked $
7 * $Log: evalargs.xc,v $
8 * Revision 3.0.1.7 90/10/15 16:48:11 lwall
9 * patch29: non-existent array values no longer cause core dumps
10 * patch29: added caller
12 * Revision 3.0.1.6 90/08/09 03:37:15 lwall
13 * patch19: passing *name to subroutine now forces filehandle and array creation
14 * patch19: `command` in array context now returns array of lines
15 * patch19: <handle> input is a little more efficient
17 * Revision 3.0.1.5 90/03/27 15:54:42 lwall
18 * patch16: MSDOS support
20 * Revision 3.0.1.4 90/02/28 17:38:37 lwall
21 * patch9: $#foo -= 2 didn't work
23 * Revision 3.0.1.3 89/11/17 15:25:07 lwall
24 * patch5: constant numeric subscripts disappeared in ?:
26 * Revision 3.0.1.2 89/11/11 04:33:05 lwall
27 * patch2: Configure now locates csh
29 * Revision 3.0.1.1 89/10/26 23:12:55 lwall
30 * patch1: glob didn't free a temporary string
32 * Revision 3.0 89/10/18 15:17:16 lwall
37 for (anum = 1; anum <= maxarg; anum++) {
38 argflags = arg[anum].arg_flags;
39 argtype = arg[anum].arg_type;
40 argptr = arg[anum].arg_ptr;
44 st[++sp] = &str_undef;
53 deb("%d.EXPR =>\n",anum);
56 sp = eval(argptr.arg_arg,
57 (argflags & AF_ARYOK) ? G_ARRAY : G_SCALAR, sp);
58 if (sp + (maxarg - anum) > stack->ary_max)
59 astore(stack, sp + (maxarg - anum), Nullstr);
60 st = stack->ary_array; /* possibly reallocated */
66 deb("%d.CMD (%lx) =>\n",anum,argptr.arg_cmd);
69 sp = cmd_exec(argptr.arg_cmd, gimme, sp);
70 if (sp + (maxarg - anum) > stack->ary_max)
71 astore(stack, sp + (maxarg - anum), Nullstr);
72 st = stack->ary_array; /* possibly reallocated */
77 case O_ITEM2: argtype = 2; break;
78 case O_ITEM3: argtype = 3; break;
79 default: argtype = anum; break;
81 str = afetch(stab_array(argptr.arg_stab),
82 arg[argtype].arg_len - arybase, TRUE);
85 (void)sprintf(buf,"LARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
86 arg[argtype].arg_len);
93 case O_ITEM2: argtype = 2; break;
94 case O_ITEM3: argtype = 3; break;
95 default: argtype = anum; break;
97 st[++sp] = afetch(stab_array(argptr.arg_stab),
98 arg[argtype].arg_len - arybase, FALSE);
101 (void)sprintf(buf,"ARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
102 arg[argtype].arg_len);
108 stab = argptr.arg_stab;
109 st[++sp] = (STR*)stab;
110 if (!stab_xarray(stab))
112 if (!stab_xhash(stab))
115 stab_io(stab) = stio_new();
118 (void)sprintf(buf,"STAR *%s",stab_name(argptr.arg_stab));
124 str = st[++sp] = (STR*)argptr.arg_stab;
127 (void)sprintf(buf,"LSTAR *%s",stab_name(argptr.arg_stab));
133 st[++sp] = STAB_STR(argptr.arg_stab);
136 (void)sprintf(buf,"STAB $%s",stab_name(argptr.arg_stab));
145 deb("%d.LEXPR =>\n",anum);
148 if (argflags & AF_ARYOK) {
149 sp = eval(argptr.arg_arg, G_ARRAY, sp);
150 if (sp + (maxarg - anum) > stack->ary_max)
151 astore(stack, sp + (maxarg - anum), Nullstr);
152 st = stack->ary_array; /* possibly reallocated */
155 sp = eval(argptr.arg_arg, G_SCALAR, sp);
156 st = stack->ary_array; /* possibly reallocated */
164 (void)sprintf(buf,"LVAL $%s",stab_name(argptr.arg_stab));
169 str = STAB_STR(argptr.arg_stab);
171 fatal("panic: A_LVAL");
174 if (argflags & AF_PRE) {
175 if (argflags & AF_UP)
181 str = arg->arg_ptr.arg_str;
183 else if (argflags & AF_POST) {
184 st[sp] = str_static(str);
185 if (argflags & AF_UP)
190 str = arg->arg_ptr.arg_str;
197 stab = argptr.arg_stab;
198 str = stab_array(argptr.arg_stab)->ary_magic;
199 if (optype != O_SASSIGN || argflags & (AF_PRE|AF_POST))
200 str_numset(str,(double)(stab_array(stab)->ary_fill+arybase));
205 fatal("panic: A_LEXPR");
208 stab = argptr.arg_stab;
209 st[++sp] = stab_array(stab)->ary_magic;
210 str_numset(st[sp],(double)(stab_array(stab)->ary_fill+arybase));
216 st[++sp] = argptr.arg_str;
222 (void) interp(str,argptr.arg_str,sp);
223 st = stack->ary_array;
230 tmps = str_get(interp(str,argptr.arg_str,sp));
231 st = stack->ary_array;
233 taintproper("Insecure dependency in ``");
235 fp = mypopen(tmps,"r");
238 if (gimme == G_SCALAR) {
239 while (str_gets(str,fp,str->str_cur) != Nullch)
244 if (++sp > stack->ary_max) {
245 astore(stack, sp, Nullstr);
246 st = stack->ary_array;
248 st[sp] = str_static(&str_undef);
249 if (str_gets(st[sp],fp,0) == Nullch) {
255 statusvalue = mypclose(fp);
260 if (gimme == G_SCALAR)
268 if (curcsv->wantarray == G_ARRAY)
278 last_in_stab = stabent(str_get(STAB_STR(argptr.arg_stab)),TRUE);
279 old_record_separator = record_separator;
282 argflags |= AF_POST; /* enable newline chopping */
283 last_in_stab = argptr.arg_stab;
284 old_record_separator = record_separator;
286 record_separator = 0;
289 record_separator = 0;
291 record_separator = '\n';
296 last_in_stab = argptr.arg_stab;
297 old_record_separator = record_separator;
299 if (anum > 1) /* assign to scalar */
300 gimme = G_SCALAR; /* force context to scalar */
301 if (gimme == G_ARRAY)
302 str = str_static(&str_undef);
305 if (stab_io(last_in_stab)) {
306 fp = stab_io(last_in_stab)->ifp;
308 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
309 if (stab_io(last_in_stab)->flags & IOF_START) {
310 stab_io(last_in_stab)->flags &= ~IOF_START;
311 stab_io(last_in_stab)->lines = 0;
312 if (alen(stab_array(last_in_stab)) < 0) {
313 tmpstr = str_make("-",1); /* assume stdin */
314 (void)apush(stab_array(last_in_stab), tmpstr);
317 fp = nextargv(last_in_stab);
318 if (!fp) { /* Note: fp != stab_io(last_in_stab)->ifp */
319 (void)do_close(last_in_stab,FALSE); /* now it does*/
320 stab_io(last_in_stab)->flags |= IOF_START;
323 else if (argtype == A_GLOB) {
324 (void) interp(str,stab_val(last_in_stab),sp);
325 st = stack->ary_array;
326 tmpstr = Str_new(55,0);
328 str_set(tmpstr, "perlglob ");
329 str_scat(tmpstr,str);
330 str_cat(tmpstr," |");
333 str_nset(tmpstr,cshname,cshlen);
334 str_cat(tmpstr," -cf 'set nonomatch; glob ");
335 str_scat(tmpstr,str);
336 str_cat(tmpstr,"'|");
338 str_set(tmpstr, "echo ");
339 str_scat(tmpstr,str);
341 "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
344 (void)do_open(last_in_stab,tmpstr->str_ptr,
346 fp = stab_io(last_in_stab)->ifp;
352 warn("Read on closed filehandle <%s>",stab_name(last_in_stab));
356 else if (!str_gets(str,fp, optype == O_RCAT ? str->str_cur : 0)) {
358 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
359 fp = nextargv(last_in_stab);
362 (void)do_close(last_in_stab,FALSE);
363 stab_io(last_in_stab)->flags |= IOF_START;
365 else if (argflags & AF_POST) {
366 (void)do_close(last_in_stab,FALSE);
369 record_separator = old_record_separator;
370 if (gimme == G_ARRAY) {
377 stab_io(last_in_stab)->lines++;
380 str->str_tainted = 1; /* Anything from the outside world...*/
382 if (argflags & AF_POST) {
383 if (str->str_cur > 0)
385 if (str->str_ptr[str->str_cur] == record_separator)
386 str->str_ptr[str->str_cur] = '\0';
389 for (tmps = str->str_ptr; *tmps; tmps++)
390 if (!isalpha(*tmps) && !isdigit(*tmps) &&
391 index("$&*(){}[]'\";\\|?<>~`",*tmps))
393 if (*tmps && stat(str->str_ptr,&statbuf) < 0)
394 goto keepgoing; /* unmatched wildcard? */
396 if (gimme == G_ARRAY) {
397 if (++sp > stack->ary_max) {
398 astore(stack, sp, Nullstr);
399 st = stack->ary_array;
401 str = str_static(&str_undef);
405 record_separator = old_record_separator;
413 deb("%d.%s = '%s'\n",anum,tmps,str_peek(st[sp]));