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.9 91/01/11 18:00:18 lwall Locked $
7 * $Log: evalargs.xc,v $
8 * Revision 3.0.1.9 91/01/11 18:00:18 lwall
9 * patch42: <> input to individual array elements was suboptimal
11 * Revision 3.0.1.8 90/11/10 01:35:49 lwall
12 * patch38: array slurps are now faster and take less memory
14 * Revision 3.0.1.7 90/10/15 16:48:11 lwall
15 * patch29: non-existent array values no longer cause core dumps
16 * patch29: added caller
18 * Revision 3.0.1.6 90/08/09 03:37:15 lwall
19 * patch19: passing *name to subroutine now forces filehandle and array creation
20 * patch19: `command` in array context now returns array of lines
21 * patch19: <handle> input is a little more efficient
23 * Revision 3.0.1.5 90/03/27 15:54:42 lwall
24 * patch16: MSDOS support
26 * Revision 3.0.1.4 90/02/28 17:38:37 lwall
27 * patch9: $#foo -= 2 didn't work
29 * Revision 3.0.1.3 89/11/17 15:25:07 lwall
30 * patch5: constant numeric subscripts disappeared in ?:
32 * Revision 3.0.1.2 89/11/11 04:33:05 lwall
33 * patch2: Configure now locates csh
35 * Revision 3.0.1.1 89/10/26 23:12:55 lwall
36 * patch1: glob didn't free a temporary string
38 * Revision 3.0 89/10/18 15:17:16 lwall
43 for (anum = 1; anum <= maxarg; anum++) {
44 argflags = arg[anum].arg_flags;
45 argtype = arg[anum].arg_type;
46 argptr = arg[anum].arg_ptr;
50 st[++sp] = &str_undef;
59 deb("%d.EXPR =>\n",anum);
62 sp = eval(argptr.arg_arg,
63 (argflags & AF_ARYOK) ? G_ARRAY : G_SCALAR, sp);
64 if (sp + (maxarg - anum) > stack->ary_max)
65 astore(stack, sp + (maxarg - anum), Nullstr);
66 st = stack->ary_array; /* possibly reallocated */
72 deb("%d.CMD (%lx) =>\n",anum,argptr.arg_cmd);
75 sp = cmd_exec(argptr.arg_cmd, gimme, sp);
76 if (sp + (maxarg - anum) > stack->ary_max)
77 astore(stack, sp + (maxarg - anum), Nullstr);
78 st = stack->ary_array; /* possibly reallocated */
83 case O_ITEM2: argtype = 2; break;
84 case O_ITEM3: argtype = 3; break;
85 default: argtype = anum; break;
87 str = afetch(stab_array(argptr.arg_stab),
88 arg[argtype].arg_len - arybase, TRUE);
91 (void)sprintf(buf,"LARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
92 arg[argtype].arg_len);
99 case O_ITEM2: argtype = 2; break;
100 case O_ITEM3: argtype = 3; break;
101 default: argtype = anum; break;
103 st[++sp] = afetch(stab_array(argptr.arg_stab),
104 arg[argtype].arg_len - arybase, FALSE);
107 (void)sprintf(buf,"ARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
108 arg[argtype].arg_len);
114 stab = argptr.arg_stab;
115 st[++sp] = (STR*)stab;
116 if (!stab_xarray(stab))
118 if (!stab_xhash(stab))
121 stab_io(stab) = stio_new();
124 (void)sprintf(buf,"STAR *%s",stab_name(argptr.arg_stab));
130 str = st[++sp] = (STR*)argptr.arg_stab;
133 (void)sprintf(buf,"LSTAR *%s",stab_name(argptr.arg_stab));
139 st[++sp] = STAB_STR(argptr.arg_stab);
142 (void)sprintf(buf,"STAB $%s",stab_name(argptr.arg_stab));
151 deb("%d.LEXPR =>\n",anum);
154 if (argflags & AF_ARYOK) {
155 sp = eval(argptr.arg_arg, G_ARRAY, sp);
156 if (sp + (maxarg - anum) > stack->ary_max)
157 astore(stack, sp + (maxarg - anum), Nullstr);
158 st = stack->ary_array; /* possibly reallocated */
161 sp = eval(argptr.arg_arg, G_SCALAR, sp);
162 st = stack->ary_array; /* possibly reallocated */
170 (void)sprintf(buf,"LVAL $%s",stab_name(argptr.arg_stab));
175 str = STAB_STR(argptr.arg_stab);
177 fatal("panic: A_LVAL");
180 if (argflags & AF_PRE) {
181 if (argflags & AF_UP)
187 str = arg->arg_ptr.arg_str;
189 else if (argflags & AF_POST) {
190 st[sp] = str_static(str);
191 if (argflags & AF_UP)
196 str = arg->arg_ptr.arg_str;
203 stab = argptr.arg_stab;
204 str = stab_array(argptr.arg_stab)->ary_magic;
205 if (optype != O_SASSIGN || argflags & (AF_PRE|AF_POST))
206 str_numset(str,(double)(stab_array(stab)->ary_fill+arybase));
211 fatal("panic: A_LEXPR");
214 stab = argptr.arg_stab;
215 st[++sp] = stab_array(stab)->ary_magic;
216 str_numset(st[sp],(double)(stab_array(stab)->ary_fill+arybase));
222 st[++sp] = argptr.arg_str;
228 (void) interp(str,argptr.arg_str,sp);
229 st = stack->ary_array;
236 tmps = str_get(interp(str,argptr.arg_str,sp));
237 st = stack->ary_array;
239 taintproper("Insecure dependency in ``");
241 fp = mypopen(tmps,"r");
244 if (gimme == G_SCALAR) {
245 while (str_gets(str,fp,str->str_cur) != Nullch)
250 if (++sp > stack->ary_max) {
251 astore(stack, sp, Nullstr);
252 st = stack->ary_array;
254 str = st[sp] = Str_new(56,80);
255 if (str_gets(str,fp,0) == Nullch) {
259 if (str->str_len - str->str_cur > 20) {
260 str->str_len = str->str_cur+1;
261 Renew(str->str_ptr, str->str_len, char);
266 statusvalue = mypclose(fp);
271 if (gimme == G_SCALAR)
279 if (curcsv->wantarray == G_ARRAY)
289 last_in_stab = stabent(str_get(STAB_STR(argptr.arg_stab)),TRUE);
290 old_record_separator = record_separator;
293 argflags |= AF_POST; /* enable newline chopping */
294 last_in_stab = argptr.arg_stab;
295 old_record_separator = record_separator;
297 record_separator = 0;
300 record_separator = 0;
302 record_separator = '\n';
307 last_in_stab = argptr.arg_stab;
308 old_record_separator = record_separator;
310 if (anum > 1) /* assign to scalar */
311 gimme = G_SCALAR; /* force context to scalar */
312 if (gimme == G_ARRAY)
316 if (stab_io(last_in_stab)) {
317 fp = stab_io(last_in_stab)->ifp;
319 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
320 if (stab_io(last_in_stab)->flags & IOF_START) {
321 stab_io(last_in_stab)->flags &= ~IOF_START;
322 stab_io(last_in_stab)->lines = 0;
323 if (alen(stab_array(last_in_stab)) < 0) {
324 tmpstr = str_make("-",1); /* assume stdin */
325 (void)apush(stab_array(last_in_stab), tmpstr);
328 fp = nextargv(last_in_stab);
329 if (!fp) { /* Note: fp != stab_io(last_in_stab)->ifp */
330 (void)do_close(last_in_stab,FALSE); /* now it does*/
331 stab_io(last_in_stab)->flags |= IOF_START;
334 else if (argtype == A_GLOB) {
335 (void) interp(str,stab_val(last_in_stab),sp);
336 st = stack->ary_array;
337 tmpstr = Str_new(55,0);
339 str_set(tmpstr, "perlglob ");
340 str_scat(tmpstr,str);
341 str_cat(tmpstr," |");
344 str_nset(tmpstr,cshname,cshlen);
345 str_cat(tmpstr," -cf 'set nonomatch; glob ");
346 str_scat(tmpstr,str);
347 str_cat(tmpstr,"'|");
349 str_set(tmpstr, "echo ");
350 str_scat(tmpstr,str);
352 "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
355 (void)do_open(last_in_stab,tmpstr->str_ptr,
357 fp = stab_io(last_in_stab)->ifp;
363 warn("Read on closed filehandle <%s>",stab_name(last_in_stab));
364 when = str->str_len; /* remember if already alloced */
366 Str_Grow(str,80); /* try short-buffering it */
370 else if (!str_gets(str,fp, optype == O_RCAT ? str->str_cur : 0)) {
372 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
373 fp = nextargv(last_in_stab);
376 (void)do_close(last_in_stab,FALSE);
377 stab_io(last_in_stab)->flags |= IOF_START;
379 else if (argflags & AF_POST) {
380 (void)do_close(last_in_stab,FALSE);
383 record_separator = old_record_separator;
384 if (gimme == G_ARRAY) {
392 stab_io(last_in_stab)->lines++;
395 str->str_tainted = 1; /* Anything from the outside world...*/
397 if (argflags & AF_POST) {
398 if (str->str_cur > 0)
400 if (str->str_ptr[str->str_cur] == record_separator)
401 str->str_ptr[str->str_cur] = '\0';
404 for (tmps = str->str_ptr; *tmps; tmps++)
405 if (!isalpha(*tmps) && !isdigit(*tmps) &&
406 index("$&*(){}[]'\";\\|?<>~`",*tmps))
408 if (*tmps && stat(str->str_ptr,&statbuf) < 0)
409 goto keepgoing; /* unmatched wildcard? */
411 if (gimme == G_ARRAY) {
412 if (str->str_len - str->str_cur > 20) {
413 str->str_len = str->str_cur+1;
414 Renew(str->str_ptr, str->str_len, char);
417 if (++sp > stack->ary_max) {
418 astore(stack, sp, Nullstr);
419 st = stack->ary_array;
421 str = Str_new(58,80);
424 else if (!when && str->str_len - str->str_cur > 80) {
425 /* try to reclaim a bit of scalar space on 1st alloc */
426 if (str->str_cur < 60)
429 str->str_len = str->str_cur+40; /* allow some slop */
430 Renew(str->str_ptr, str->str_len, char);
433 record_separator = old_record_separator;
441 deb("%d.%s = '%s'\n",anum,tmps,str_peek(st[sp]));