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.6 90/08/09 03:37:15 lwall Locked $
7 * $Log: evalargs.xc,v $
8 * Revision 3.0.1.6 90/08/09 03:37:15 lwall
9 * patch19: passing *name to subroutine now forces filehandle and array creation
10 * patch19: `command` in array context now returns array of lines
11 * patch19: <handle> input is a little more efficient
13 * Revision 3.0.1.5 90/03/27 15:54:42 lwall
14 * patch16: MSDOS support
16 * Revision 3.0.1.4 90/02/28 17:38:37 lwall
17 * patch9: $#foo -= 2 didn't work
19 * Revision 3.0.1.3 89/11/17 15:25:07 lwall
20 * patch5: constant numeric subscripts disappeared in ?:
22 * Revision 3.0.1.2 89/11/11 04:33:05 lwall
23 * patch2: Configure now locates csh
25 * Revision 3.0.1.1 89/10/26 23:12:55 lwall
26 * patch1: glob didn't free a temporary string
28 * Revision 3.0 89/10/18 15:17:16 lwall
33 for (anum = 1; anum <= maxarg; anum++) {
34 argflags = arg[anum].arg_flags;
35 argtype = arg[anum].arg_type;
36 argptr = arg[anum].arg_ptr;
40 st[++sp] = &str_undef;
49 deb("%d.EXPR =>\n",anum);
52 sp = eval(argptr.arg_arg,
53 (argflags & AF_ARYOK) ? G_ARRAY : G_SCALAR, sp);
54 if (sp + (maxarg - anum) > stack->ary_max)
55 astore(stack, sp + (maxarg - anum), Nullstr);
56 st = stack->ary_array; /* possibly reallocated */
62 deb("%d.CMD (%lx) =>\n",anum,argptr.arg_cmd);
65 sp = cmd_exec(argptr.arg_cmd, gimme, sp);
66 if (sp + (maxarg - anum) > stack->ary_max)
67 astore(stack, sp + (maxarg - anum), Nullstr);
68 st = stack->ary_array; /* possibly reallocated */
73 case O_ITEM2: argtype = 2; break;
74 case O_ITEM3: argtype = 3; break;
75 default: argtype = anum; break;
77 str = afetch(stab_array(argptr.arg_stab),
78 arg[argtype].arg_len - arybase, TRUE);
81 (void)sprintf(buf,"LARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
82 arg[argtype].arg_len);
89 case O_ITEM2: argtype = 2; break;
90 case O_ITEM3: argtype = 3; break;
91 default: argtype = anum; break;
93 st[++sp] = afetch(stab_array(argptr.arg_stab),
94 arg[argtype].arg_len - arybase, FALSE);
99 (void)sprintf(buf,"ARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
100 arg[argtype].arg_len);
106 stab = argptr.arg_stab;
107 st[++sp] = (STR*)stab;
108 if (!stab_xarray(stab))
110 if (!stab_xhash(stab))
113 stab_io(stab) = stio_new();
116 (void)sprintf(buf,"STAR *%s",stab_name(argptr.arg_stab));
122 str = st[++sp] = (STR*)argptr.arg_stab;
125 (void)sprintf(buf,"LSTAR *%s",stab_name(argptr.arg_stab));
131 st[++sp] = STAB_STR(argptr.arg_stab);
134 (void)sprintf(buf,"STAB $%s",stab_name(argptr.arg_stab));
143 deb("%d.LEXPR =>\n",anum);
146 if (argflags & AF_ARYOK) {
147 sp = eval(argptr.arg_arg, G_ARRAY, sp);
148 if (sp + (maxarg - anum) > stack->ary_max)
149 astore(stack, sp + (maxarg - anum), Nullstr);
150 st = stack->ary_array; /* possibly reallocated */
153 sp = eval(argptr.arg_arg, G_SCALAR, sp);
154 st = stack->ary_array; /* possibly reallocated */
162 (void)sprintf(buf,"LVAL $%s",stab_name(argptr.arg_stab));
167 str = STAB_STR(argptr.arg_stab);
169 fatal("panic: A_LVAL");
172 if (argflags & AF_PRE) {
173 if (argflags & AF_UP)
179 str = arg->arg_ptr.arg_str;
181 else if (argflags & AF_POST) {
182 st[sp] = str_static(str);
183 if (argflags & AF_UP)
188 str = arg->arg_ptr.arg_str;
195 stab = argptr.arg_stab;
196 str = stab_array(argptr.arg_stab)->ary_magic;
197 if (optype != O_SASSIGN || argflags & (AF_PRE|AF_POST))
198 str_numset(str,(double)(stab_array(stab)->ary_fill+arybase));
203 fatal("panic: A_LEXPR");
206 stab = argptr.arg_stab;
207 st[++sp] = stab_array(stab)->ary_magic;
208 str_numset(st[sp],(double)(stab_array(stab)->ary_fill+arybase));
214 st[++sp] = argptr.arg_str;
220 (void) interp(str,argptr.arg_str,sp);
221 st = stack->ary_array;
228 tmps = str_get(interp(str,argptr.arg_str,sp));
229 st = stack->ary_array;
231 taintproper("Insecure dependency in ``");
233 fp = mypopen(tmps,"r");
236 if (gimme == G_SCALAR) {
237 while (str_gets(str,fp,str->str_cur) != Nullch)
242 if (++sp > stack->ary_max) {
243 astore(stack, sp, Nullstr);
244 st = stack->ary_array;
246 st[sp] = str_static(&str_undef);
247 if (str_gets(st[sp],fp,0) == Nullch) {
253 statusvalue = mypclose(fp);
258 if (gimme == G_SCALAR)
266 if (wantarray == G_ARRAY)
276 last_in_stab = stabent(str_get(STAB_STR(argptr.arg_stab)),TRUE);
277 old_record_separator = record_separator;
280 argflags |= AF_POST; /* enable newline chopping */
281 last_in_stab = argptr.arg_stab;
282 old_record_separator = record_separator;
284 record_separator = 0;
287 record_separator = 0;
289 record_separator = '\n';
294 last_in_stab = argptr.arg_stab;
295 old_record_separator = record_separator;
297 if (anum > 1) /* assign to scalar */
298 gimme = G_SCALAR; /* force context to scalar */
299 if (gimme == G_ARRAY)
300 str = str_static(&str_undef);
303 if (stab_io(last_in_stab)) {
304 fp = stab_io(last_in_stab)->ifp;
306 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
307 if (stab_io(last_in_stab)->flags & IOF_START) {
308 stab_io(last_in_stab)->flags &= ~IOF_START;
309 stab_io(last_in_stab)->lines = 0;
310 if (alen(stab_array(last_in_stab)) < 0) {
311 tmpstr = str_make("-",1); /* assume stdin */
312 (void)apush(stab_array(last_in_stab), tmpstr);
315 fp = nextargv(last_in_stab);
316 if (!fp) { /* Note: fp != stab_io(last_in_stab)->ifp */
317 (void)do_close(last_in_stab,FALSE); /* now it does*/
318 stab_io(last_in_stab)->flags |= IOF_START;
321 else if (argtype == A_GLOB) {
322 (void) interp(str,stab_val(last_in_stab),sp);
323 st = stack->ary_array;
324 tmpstr = Str_new(55,0);
326 str_set(tmpstr, "glob ");
327 str_scat(tmpstr,str);
328 str_cat(tmpstr," |");
331 str_nset(tmpstr,cshname,cshlen);
332 str_cat(tmpstr," -cf 'set nonomatch; glob ");
333 str_scat(tmpstr,str);
334 str_cat(tmpstr,"'|");
336 str_set(tmpstr, "echo ");
337 str_scat(tmpstr,str);
339 "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
342 (void)do_open(last_in_stab,tmpstr->str_ptr,
344 fp = stab_io(last_in_stab)->ifp;
350 warn("Read on closed filehandle <%s>",stab_name(last_in_stab));
354 else if (!str_gets(str,fp, optype == O_RCAT ? str->str_cur : 0)) {
356 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
357 fp = nextargv(last_in_stab);
360 (void)do_close(last_in_stab,FALSE);
361 stab_io(last_in_stab)->flags |= IOF_START;
363 else if (argflags & AF_POST) {
364 (void)do_close(last_in_stab,FALSE);
367 record_separator = old_record_separator;
368 if (gimme == G_ARRAY) {
375 stab_io(last_in_stab)->lines++;
378 str->str_tainted = 1; /* Anything from the outside world...*/
380 if (argflags & AF_POST) {
381 if (str->str_cur > 0)
383 if (str->str_ptr[str->str_cur] == record_separator)
384 str->str_ptr[str->str_cur] = '\0';
387 for (tmps = str->str_ptr; *tmps; tmps++)
388 if (!isalpha(*tmps) && !isdigit(*tmps) &&
389 index("$&*(){}[]'\";\\|?<>~`",*tmps))
391 if (*tmps && stat(str->str_ptr,&statbuf) < 0)
392 goto keepgoing; /* unmatched wildcard? */
394 if (gimme == G_ARRAY) {
395 if (++sp > stack->ary_max) {
396 astore(stack, sp, Nullstr);
397 st = stack->ary_array;
399 str = str_static(&str_undef);
403 record_separator = old_record_separator;
411 deb("%d.%s = '%s'\n",anum,tmps,str_peek(st[sp]));