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.8 90/11/10 01:35:49 lwall Locked $
7 * $Log: evalargs.xc,v $
8 * Revision 3.0.1.8 90/11/10 01:35:49 lwall
9 * patch38: array slurps are now faster and take less memory
11 * Revision 3.0.1.7 90/10/15 16:48:11 lwall
12 * patch29: non-existent array values no longer cause core dumps
13 * patch29: added caller
15 * Revision 3.0.1.6 90/08/09 03:37:15 lwall
16 * patch19: passing *name to subroutine now forces filehandle and array creation
17 * patch19: `command` in array context now returns array of lines
18 * patch19: <handle> input is a little more efficient
20 * Revision 3.0.1.5 90/03/27 15:54:42 lwall
21 * patch16: MSDOS support
23 * Revision 3.0.1.4 90/02/28 17:38:37 lwall
24 * patch9: $#foo -= 2 didn't work
26 * Revision 3.0.1.3 89/11/17 15:25:07 lwall
27 * patch5: constant numeric subscripts disappeared in ?:
29 * Revision 3.0.1.2 89/11/11 04:33:05 lwall
30 * patch2: Configure now locates csh
32 * Revision 3.0.1.1 89/10/26 23:12:55 lwall
33 * patch1: glob didn't free a temporary string
35 * Revision 3.0 89/10/18 15:17:16 lwall
40 for (anum = 1; anum <= maxarg; anum++) {
41 argflags = arg[anum].arg_flags;
42 argtype = arg[anum].arg_type;
43 argptr = arg[anum].arg_ptr;
47 st[++sp] = &str_undef;
56 deb("%d.EXPR =>\n",anum);
59 sp = eval(argptr.arg_arg,
60 (argflags & AF_ARYOK) ? G_ARRAY : G_SCALAR, sp);
61 if (sp + (maxarg - anum) > stack->ary_max)
62 astore(stack, sp + (maxarg - anum), Nullstr);
63 st = stack->ary_array; /* possibly reallocated */
69 deb("%d.CMD (%lx) =>\n",anum,argptr.arg_cmd);
72 sp = cmd_exec(argptr.arg_cmd, gimme, sp);
73 if (sp + (maxarg - anum) > stack->ary_max)
74 astore(stack, sp + (maxarg - anum), Nullstr);
75 st = stack->ary_array; /* possibly reallocated */
80 case O_ITEM2: argtype = 2; break;
81 case O_ITEM3: argtype = 3; break;
82 default: argtype = anum; break;
84 str = afetch(stab_array(argptr.arg_stab),
85 arg[argtype].arg_len - arybase, TRUE);
88 (void)sprintf(buf,"LARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
89 arg[argtype].arg_len);
96 case O_ITEM2: argtype = 2; break;
97 case O_ITEM3: argtype = 3; break;
98 default: argtype = anum; break;
100 st[++sp] = afetch(stab_array(argptr.arg_stab),
101 arg[argtype].arg_len - arybase, FALSE);
104 (void)sprintf(buf,"ARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
105 arg[argtype].arg_len);
111 stab = argptr.arg_stab;
112 st[++sp] = (STR*)stab;
113 if (!stab_xarray(stab))
115 if (!stab_xhash(stab))
118 stab_io(stab) = stio_new();
121 (void)sprintf(buf,"STAR *%s",stab_name(argptr.arg_stab));
127 str = st[++sp] = (STR*)argptr.arg_stab;
130 (void)sprintf(buf,"LSTAR *%s",stab_name(argptr.arg_stab));
136 st[++sp] = STAB_STR(argptr.arg_stab);
139 (void)sprintf(buf,"STAB $%s",stab_name(argptr.arg_stab));
148 deb("%d.LEXPR =>\n",anum);
151 if (argflags & AF_ARYOK) {
152 sp = eval(argptr.arg_arg, G_ARRAY, sp);
153 if (sp + (maxarg - anum) > stack->ary_max)
154 astore(stack, sp + (maxarg - anum), Nullstr);
155 st = stack->ary_array; /* possibly reallocated */
158 sp = eval(argptr.arg_arg, G_SCALAR, sp);
159 st = stack->ary_array; /* possibly reallocated */
167 (void)sprintf(buf,"LVAL $%s",stab_name(argptr.arg_stab));
172 str = STAB_STR(argptr.arg_stab);
174 fatal("panic: A_LVAL");
177 if (argflags & AF_PRE) {
178 if (argflags & AF_UP)
184 str = arg->arg_ptr.arg_str;
186 else if (argflags & AF_POST) {
187 st[sp] = str_static(str);
188 if (argflags & AF_UP)
193 str = arg->arg_ptr.arg_str;
200 stab = argptr.arg_stab;
201 str = stab_array(argptr.arg_stab)->ary_magic;
202 if (optype != O_SASSIGN || argflags & (AF_PRE|AF_POST))
203 str_numset(str,(double)(stab_array(stab)->ary_fill+arybase));
208 fatal("panic: A_LEXPR");
211 stab = argptr.arg_stab;
212 st[++sp] = stab_array(stab)->ary_magic;
213 str_numset(st[sp],(double)(stab_array(stab)->ary_fill+arybase));
219 st[++sp] = argptr.arg_str;
225 (void) interp(str,argptr.arg_str,sp);
226 st = stack->ary_array;
233 tmps = str_get(interp(str,argptr.arg_str,sp));
234 st = stack->ary_array;
236 taintproper("Insecure dependency in ``");
238 fp = mypopen(tmps,"r");
241 if (gimme == G_SCALAR) {
242 while (str_gets(str,fp,str->str_cur) != Nullch)
247 if (++sp > stack->ary_max) {
248 astore(stack, sp, Nullstr);
249 st = stack->ary_array;
251 str = st[sp] = Str_new(56,80);
252 if (str_gets(str,fp,0) == Nullch) {
256 if (str->str_len - str->str_cur > 20) {
257 str->str_len = str->str_cur+1;
258 Renew(str->str_ptr, str->str_len, char);
263 statusvalue = mypclose(fp);
268 if (gimme == G_SCALAR)
276 if (curcsv->wantarray == G_ARRAY)
286 last_in_stab = stabent(str_get(STAB_STR(argptr.arg_stab)),TRUE);
287 old_record_separator = record_separator;
290 argflags |= AF_POST; /* enable newline chopping */
291 last_in_stab = argptr.arg_stab;
292 old_record_separator = record_separator;
294 record_separator = 0;
297 record_separator = 0;
299 record_separator = '\n';
304 last_in_stab = argptr.arg_stab;
305 old_record_separator = record_separator;
307 if (anum > 1) /* assign to scalar */
308 gimme = G_SCALAR; /* force context to scalar */
309 if (gimme == G_ARRAY)
313 if (stab_io(last_in_stab)) {
314 fp = stab_io(last_in_stab)->ifp;
316 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
317 if (stab_io(last_in_stab)->flags & IOF_START) {
318 stab_io(last_in_stab)->flags &= ~IOF_START;
319 stab_io(last_in_stab)->lines = 0;
320 if (alen(stab_array(last_in_stab)) < 0) {
321 tmpstr = str_make("-",1); /* assume stdin */
322 (void)apush(stab_array(last_in_stab), tmpstr);
325 fp = nextargv(last_in_stab);
326 if (!fp) { /* Note: fp != stab_io(last_in_stab)->ifp */
327 (void)do_close(last_in_stab,FALSE); /* now it does*/
328 stab_io(last_in_stab)->flags |= IOF_START;
331 else if (argtype == A_GLOB) {
332 (void) interp(str,stab_val(last_in_stab),sp);
333 st = stack->ary_array;
334 tmpstr = Str_new(55,0);
336 str_set(tmpstr, "perlglob ");
337 str_scat(tmpstr,str);
338 str_cat(tmpstr," |");
341 str_nset(tmpstr,cshname,cshlen);
342 str_cat(tmpstr," -cf 'set nonomatch; glob ");
343 str_scat(tmpstr,str);
344 str_cat(tmpstr,"'|");
346 str_set(tmpstr, "echo ");
347 str_scat(tmpstr,str);
349 "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
352 (void)do_open(last_in_stab,tmpstr->str_ptr,
354 fp = stab_io(last_in_stab)->ifp;
360 warn("Read on closed filehandle <%s>",stab_name(last_in_stab));
364 else if (!str_gets(str,fp, optype == O_RCAT ? str->str_cur : 0)) {
366 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
367 fp = nextargv(last_in_stab);
370 (void)do_close(last_in_stab,FALSE);
371 stab_io(last_in_stab)->flags |= IOF_START;
373 else if (argflags & AF_POST) {
374 (void)do_close(last_in_stab,FALSE);
377 record_separator = old_record_separator;
378 if (gimme == G_ARRAY) {
386 stab_io(last_in_stab)->lines++;
389 str->str_tainted = 1; /* Anything from the outside world...*/
391 if (argflags & AF_POST) {
392 if (str->str_cur > 0)
394 if (str->str_ptr[str->str_cur] == record_separator)
395 str->str_ptr[str->str_cur] = '\0';
398 for (tmps = str->str_ptr; *tmps; tmps++)
399 if (!isalpha(*tmps) && !isdigit(*tmps) &&
400 index("$&*(){}[]'\";\\|?<>~`",*tmps))
402 if (*tmps && stat(str->str_ptr,&statbuf) < 0)
403 goto keepgoing; /* unmatched wildcard? */
405 if (gimme == G_ARRAY) {
406 if (str->str_len - str->str_cur > 20) {
407 str->str_len = str->str_cur+1;
408 Renew(str->str_ptr, str->str_len, char);
411 if (++sp > stack->ary_max) {
412 astore(stack, sp, Nullstr);
413 st = stack->ary_array;
415 str = Str_new(58,80);
419 record_separator = old_record_separator;
427 deb("%d.%s = '%s'\n",anum,tmps,str_peek(st[sp]));