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.5 90/03/27 15:54:42 lwall Locked $
7 * $Log: evalargs.xc,v $
8 * Revision 3.0.1.5 90/03/27 15:54:42 lwall
9 * patch16: MSDOS support
11 * Revision 3.0.1.4 90/02/28 17:38:37 lwall
12 * patch9: $#foo -= 2 didn't work
14 * Revision 3.0.1.3 89/11/17 15:25:07 lwall
15 * patch5: constant numeric subscripts disappeared in ?:
17 * Revision 3.0.1.2 89/11/11 04:33:05 lwall
18 * patch2: Configure now locates csh
20 * Revision 3.0.1.1 89/10/26 23:12:55 lwall
21 * patch1: glob didn't free a temporary string
23 * Revision 3.0 89/10/18 15:17:16 lwall
28 for (anum = 1; anum <= maxarg; anum++) {
29 argflags = arg[anum].arg_flags;
30 argtype = arg[anum].arg_type;
31 argptr = arg[anum].arg_ptr;
35 st[++sp] = &str_undef;
44 deb("%d.EXPR =>\n",anum);
47 sp = eval(argptr.arg_arg,
48 (argflags & AF_ARYOK) ? G_ARRAY : G_SCALAR, sp);
49 if (sp + (maxarg - anum) > stack->ary_max)
50 astore(stack, sp + (maxarg - anum), Nullstr);
51 st = stack->ary_array; /* possibly reallocated */
57 deb("%d.CMD (%lx) =>\n",anum,argptr.arg_cmd);
60 sp = cmd_exec(argptr.arg_cmd, gimme, sp);
61 if (sp + (maxarg - anum) > stack->ary_max)
62 astore(stack, sp + (maxarg - anum), Nullstr);
63 st = stack->ary_array; /* possibly reallocated */
68 case O_ITEM2: argtype = 2; break;
69 case O_ITEM3: argtype = 3; break;
70 default: argtype = anum; break;
72 str = afetch(stab_array(argptr.arg_stab),
73 arg[argtype].arg_len - arybase, TRUE);
76 (void)sprintf(buf,"LARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
77 arg[argtype].arg_len);
84 case O_ITEM2: argtype = 2; break;
85 case O_ITEM3: argtype = 3; break;
86 default: argtype = anum; break;
88 st[++sp] = afetch(stab_array(argptr.arg_stab),
89 arg[argtype].arg_len - arybase, FALSE);
94 (void)sprintf(buf,"ARYSTAB $%s[%d]",stab_name(argptr.arg_stab),
95 arg[argtype].arg_len);
101 st[++sp] = (STR*)argptr.arg_stab;
104 (void)sprintf(buf,"STAR *%s",stab_name(argptr.arg_stab));
110 str = st[++sp] = (STR*)argptr.arg_stab;
113 (void)sprintf(buf,"LSTAR *%s",stab_name(argptr.arg_stab));
119 st[++sp] = STAB_STR(argptr.arg_stab);
122 (void)sprintf(buf,"STAB $%s",stab_name(argptr.arg_stab));
131 deb("%d.LEXPR =>\n",anum);
134 if (argflags & AF_ARYOK) {
135 sp = eval(argptr.arg_arg, G_ARRAY, sp);
136 if (sp + (maxarg - anum) > stack->ary_max)
137 astore(stack, sp + (maxarg - anum), Nullstr);
138 st = stack->ary_array; /* possibly reallocated */
141 sp = eval(argptr.arg_arg, G_SCALAR, sp);
142 st = stack->ary_array; /* possibly reallocated */
150 (void)sprintf(buf,"LVAL $%s",stab_name(argptr.arg_stab));
155 str = STAB_STR(argptr.arg_stab);
157 fatal("panic: A_LVAL");
160 if (argflags & AF_PRE) {
161 if (argflags & AF_UP)
167 str = arg->arg_ptr.arg_str;
169 else if (argflags & AF_POST) {
170 st[sp] = str_static(str);
171 if (argflags & AF_UP)
176 str = arg->arg_ptr.arg_str;
183 stab = argptr.arg_stab;
184 str = stab_array(argptr.arg_stab)->ary_magic;
185 if (optype != O_SASSIGN || argflags & (AF_PRE|AF_POST))
186 str_numset(str,(double)(stab_array(stab)->ary_fill+arybase));
191 fatal("panic: A_LEXPR");
194 stab = argptr.arg_stab;
195 st[++sp] = stab_array(stab)->ary_magic;
196 str_numset(st[sp],(double)(stab_array(stab)->ary_fill+arybase));
202 st[++sp] = argptr.arg_str;
208 (void) interp(str,argptr.arg_str,sp);
209 st = stack->ary_array;
216 tmps = str_get(interp(str,argptr.arg_str,sp));
217 st = stack->ary_array;
219 taintproper("Insecure dependency in ``");
221 fp = mypopen(tmps,"r");
224 while (str_gets(str,fp,str->str_cur) != Nullch)
226 statusvalue = mypclose(fp);
238 if (wantarray == G_ARRAY)
248 last_in_stab = stabent(str_get(STAB_STR(argptr.arg_stab)),TRUE);
249 old_record_separator = record_separator;
252 argflags |= AF_POST; /* enable newline chopping */
253 last_in_stab = argptr.arg_stab;
254 old_record_separator = record_separator;
256 record_separator = 0;
259 record_separator = 0;
261 record_separator = '\n';
266 last_in_stab = argptr.arg_stab;
267 old_record_separator = record_separator;
269 if (anum > 1) /* assign to scalar */
270 gimme = G_SCALAR; /* force context to scalar */
273 if (stab_io(last_in_stab)) {
274 fp = stab_io(last_in_stab)->ifp;
276 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
277 if (stab_io(last_in_stab)->flags & IOF_START) {
278 stab_io(last_in_stab)->flags &= ~IOF_START;
279 stab_io(last_in_stab)->lines = 0;
280 if (alen(stab_array(last_in_stab)) < 0) {
281 tmpstr = str_make("-",1); /* assume stdin */
282 (void)apush(stab_array(last_in_stab), tmpstr);
285 fp = nextargv(last_in_stab);
286 if (!fp) { /* Note: fp != stab_io(last_in_stab)->ifp */
287 (void)do_close(last_in_stab,FALSE); /* now it does*/
288 stab_io(last_in_stab)->flags |= IOF_START;
291 else if (argtype == A_GLOB) {
292 (void) interp(str,stab_val(last_in_stab),sp);
293 st = stack->ary_array;
294 tmpstr = Str_new(55,0);
296 str_set(tmpstr, "glob ");
297 str_scat(tmpstr,str);
298 str_cat(tmpstr," |");
301 str_nset(tmpstr,cshname,cshlen);
302 str_cat(tmpstr," -cf 'set nonomatch; glob ");
303 str_scat(tmpstr,str);
304 str_cat(tmpstr,"'|");
306 str_set(tmpstr, "echo ");
307 str_scat(tmpstr,str);
309 "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
312 (void)do_open(last_in_stab,tmpstr->str_ptr,
314 fp = stab_io(last_in_stab)->ifp;
320 warn("Read on closed filehandle <%s>",stab_name(last_in_stab));
324 else if (!str_gets(str,fp, optype == O_RCAT ? str->str_cur : 0)) {
326 if (stab_io(last_in_stab)->flags & IOF_ARGV) {
327 fp = nextargv(last_in_stab);
330 (void)do_close(last_in_stab,FALSE);
331 stab_io(last_in_stab)->flags |= IOF_START;
333 else if (argflags & AF_POST) {
334 (void)do_close(last_in_stab,FALSE);
337 record_separator = old_record_separator;
338 if (gimme == G_ARRAY) {
345 stab_io(last_in_stab)->lines++;
348 str->str_tainted = 1; /* Anything from the outside world...*/
350 if (argflags & AF_POST) {
351 if (str->str_cur > 0)
353 if (str->str_ptr[str->str_cur] == record_separator)
354 str->str_ptr[str->str_cur] = '\0';
357 for (tmps = str->str_ptr; *tmps; tmps++)
358 if (!isalpha(*tmps) && !isdigit(*tmps) &&
359 index("$&*(){}[]'\";\\|?<>~`",*tmps))
361 if (*tmps && stat(str->str_ptr,&statbuf) < 0)
362 goto keepgoing; /* unmatched wildcard? */
364 if (gimme == G_ARRAY) {
365 st[sp] = str_static(st[sp]);
366 if (++sp > stack->ary_max) {
367 astore(stack, sp, Nullstr);
368 st = stack->ary_array;
373 record_separator = old_record_separator;
381 deb("%d.%s = '%s'\n",anum,tmps,str_peek(st[sp]));