1 /* $Header: eval.c,v 3.0.1.8 90/08/13 22:17:14 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 3.0.1.8 90/08/13 22:17:14 lwall
10 * patch28: the NSIG hack didn't work right on Xenix
11 * patch28: defined(@array) and defined(%array) didn't work right
12 * patch28: rename was busted on systems without rename system call
14 * Revision 3.0.1.7 90/08/09 03:33:44 lwall
15 * patch19: made ~ do vector operation on strings like &, | and ^
16 * patch19: dbmopen(%name...) didn't work right
17 * patch19: dbmopen(name, 'filename', undef) now refrains from creating
18 * patch19: empty %array now returns 0 in scalar context
19 * patch19: die with no arguments no longer exits unconditionally
20 * patch19: return outside a subroutine now returns a reasonable message
21 * patch19: rename done with unlink()/link()/unlink() now checks for clobbering
22 * patch19: -s now returns size of file
24 * Revision 3.0.1.6 90/03/27 15:53:51 lwall
25 * patch16: MSDOS support
26 * patch16: support for machines that can't cast negative floats to unsigned ints
27 * patch16: ioctl didn't return values correctly
29 * Revision 3.0.1.5 90/03/12 16:37:40 lwall
30 * patch13: undef $/ didn't work as advertised
31 * patch13: added list slice operator (LIST)[LIST]
32 * patch13: added splice operator: @oldelems = splice(@array,$offset,$len,LIST)
34 * Revision 3.0.1.4 90/02/28 17:36:59 lwall
35 * patch9: added pipe function
36 * patch9: a return in scalar context wouldn't return array
37 * patch9: !~ now always returns scalar even in array context
38 * patch9: some machines can't cast float to long with high bit set
39 * patch9: piped opens returned undef in child
40 * patch9: @array in scalar context now returns length of array
41 * patch9: chdir; coredumped
42 * patch9: wait no longer ignores signals
43 * patch9: mkdir now handles odd versions of /bin/mkdir
44 * patch9: -l FILEHANDLE now disallowed
46 * Revision 3.0.1.3 89/12/21 20:03:05 lwall
47 * patch7: errno may now be a macro with an lvalue
48 * patch7: ANSI strerror() is now supported
49 * patch7: send() didn't allow a TO argument
50 * patch7: ord() now always returns positive even on signed char machines
52 * Revision 3.0.1.2 89/11/17 15:19:34 lwall
53 * patch5: constant numeric subscripts get lost inside ?:
55 * Revision 3.0.1.1 89/11/11 04:31:51 lwall
56 * patch2: mkdir and rmdir needed to quote argument when passed to shell
57 * patch2: mkdir and rmdir now return better error codes
58 * patch2: fileno, seekdir, rewinddir and closedir now disallow defaults
60 * Revision 3.0 89/10/18 15:17:04 lwall
68 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
80 static void (*ihand)();
81 static void (*qhand)();
83 static int (*ihand)();
84 static int (*qhand)();
91 static struct lstring *lstr;
92 static int old_record_separator;
95 double sin(), cos(), atan2(), pow();
116 int arglast[8]; /* highest sp for arg--valid only for non-O_LIST args */
117 unsigned long tmplong;
124 bool assigning = FALSE;
125 double exp(), log(), sqrt(), modf();
126 char *crypt(), *getenv();
127 extern void grow_dlevel();
131 optype = arg->arg_type;
132 maxarg = arg->arg_len;
134 str = arg->arg_ptr.arg_str;
135 if (sp + maxarg > stack->ary_max)
136 astore(stack, sp + maxarg, Nullstr);
137 st = stack->ary_array;
142 deb("%s (%lx) %d args:\n",opname[optype],arg,maxarg);
144 debname[dlevel] = opname[optype][0];
145 debdelim[dlevel] = ':';
146 if (++dlevel >= dlmax)
151 #include "evalargs.xc"
159 if (gimme == G_ARRAY)
165 if (gimme == G_ARRAY)
168 STR_SSET(str,st[arglast[anum]-arglast[0]]);
172 if (gimme == G_ARRAY)
175 STR_SSET(str,st[arglast[anum]-arglast[0]]);
185 anum = (int)str_gnum(st[2]);
187 tmpstr = Str_new(50, 0);
188 str_sset(tmpstr,str);
189 tmps = str_get(tmpstr); /* force to be string */
190 STR_GROW(str, (anum * str->str_cur) + 1);
191 repeatcpy(str->str_ptr, tmps, tmpstr->str_cur, anum);
192 str->str_cur *= anum;
193 str->str_ptr[str->str_cur] = '\0';
196 str_sset(str,&str_no);
200 sp = do_match(str,arg,
202 if (gimme == G_ARRAY)
207 sp = do_match(str,arg,
209 str_sset(str, str_true(str) ? &str_no : &str_yes);
213 sp = do_subst(str,arg,arglast[0]);
216 sp = do_subst(str,arg,arglast[0]);
217 str = arg->arg_ptr.arg_str;
218 str_set(str, str_true(str) ? No : Yes);
221 if (arg[1].arg_flags & AF_ARYOK) {
222 if (arg->arg_len == 1) {
223 arg->arg_type = O_LOCAL;
227 arg->arg_type = O_AASSIGN;
232 arg->arg_type = O_SASSIGN;
237 arglast[2] = arglast[1]; /* push a null array */
246 STR_SSET(str, st[2]);
251 str = arg->arg_ptr.arg_str;
252 for (sp = arglast[0] + 1; sp <= arglast[1]; sp++)
257 if (arg[1].arg_type & A_DONT) {
258 sp = do_defined(str,arg,
262 else if (str->str_pok || str->str_nok)
266 if (arg[1].arg_type & A_DONT) {
267 sp = do_undef(str,arg,
271 else if (str != stab_val(defstab)) {
272 str->str_pok = str->str_nok = 0;
277 sp = do_study(str,arg,
281 value = str_gnum(st[1]);
282 value = pow(value,str_gnum(st[2]));
285 value = str_gnum(st[1]);
286 value *= str_gnum(st[2]);
289 if ((value = str_gnum(st[2])) == 0.0)
290 fatal("Illegal division by zero");
291 value = str_gnum(st[1]) / value;
294 tmplong = (long) str_gnum(st[2]);
296 fatal("Illegal modulus zero");
297 when = (long)str_gnum(st[1]);
300 value = (double)(when % tmplong);
302 value = (double)(tmplong - ((-when - 1) % tmplong)) - 1;
306 value = str_gnum(st[1]);
307 value += str_gnum(st[2]);
310 value = str_gnum(st[1]);
311 value -= str_gnum(st[2]);
314 value = str_gnum(st[1]);
315 anum = (int)str_gnum(st[2]);
317 value = (double)(U_L(value) << anum);
321 value = str_gnum(st[1]);
322 anum = (int)str_gnum(st[2]);
324 value = (double)(U_L(value) >> anum);
328 value = str_gnum(st[1]);
329 value = (value < str_gnum(st[2])) ? 1.0 : 0.0;
332 value = str_gnum(st[1]);
333 value = (value > str_gnum(st[2])) ? 1.0 : 0.0;
336 value = str_gnum(st[1]);
337 value = (value <= str_gnum(st[2])) ? 1.0 : 0.0;
340 value = str_gnum(st[1]);
341 value = (value >= str_gnum(st[2])) ? 1.0 : 0.0;
345 if ((!st[1]->str_nok && !looks_like_number(st[1])) ||
346 (!st[2]->str_nok && !looks_like_number(st[2])) )
347 warn("Possible use of == on string value");
349 value = str_gnum(st[1]);
350 value = (value == str_gnum(st[2])) ? 1.0 : 0.0;
353 value = str_gnum(st[1]);
354 value = (value != str_gnum(st[2])) ? 1.0 : 0.0;
357 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
358 value = str_gnum(st[1]);
360 value = (double)(U_L(value) & U_L(str_gnum(st[2])));
365 do_vop(optype,str,st[1],st[2]);
368 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
369 value = str_gnum(st[1]);
371 value = (double)(U_L(value) ^ U_L(str_gnum(st[2])));
376 do_vop(optype,str,st[1],st[2]);
379 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
380 value = str_gnum(st[1]);
382 value = (double)(U_L(value) | U_L(str_gnum(st[2])));
387 do_vop(optype,str,st[1],st[2]);
389 /* use register in evaluating str_true() */
391 if (str_true(st[1])) {
394 argflags = arg[anum].arg_flags;
395 if (gimme == G_ARRAY)
396 argflags |= AF_ARYOK;
397 argtype = arg[anum].arg_type & A_MASK;
398 argptr = arg[anum].arg_ptr;
406 str_sset(str, st[1]);
414 if (str_true(st[1])) {
416 str_sset(str, st[1]);
426 argflags = arg[anum].arg_flags;
427 if (gimme == G_ARRAY)
428 argflags |= AF_ARYOK;
429 argtype = arg[anum].arg_type & A_MASK;
430 argptr = arg[anum].arg_ptr;
437 anum = (str_true(st[1]) ? 2 : 3);
438 optype = (anum == 2 ? O_ITEM2 : O_ITEM3);
439 argflags = arg[anum].arg_flags;
440 if (gimme == G_ARRAY)
441 argflags |= AF_ARYOK;
442 argtype = arg[anum].arg_type & A_MASK;
443 argptr = arg[anum].arg_ptr;
449 if (gimme == G_ARRAY)
454 value = -str_gnum(st[1]);
457 value = (double) !str_true(st[1]);
460 if (!sawvec || st[1]->str_nok) {
462 value = (double) ~U_L(str_gnum(st[1]));
469 for (anum = str->str_cur; anum; anum--)
474 tmps = stab_name(defoutstab);
476 if ((arg[1].arg_type & A_MASK) == A_WORD)
477 defoutstab = arg[1].arg_ptr.arg_stab;
479 defoutstab = stabent(str_get(st[1]),TRUE);
480 if (!stab_io(defoutstab))
481 stab_io(defoutstab) = stio_new();
482 curoutstab = defoutstab;
490 else if ((arg[1].arg_type & A_MASK) == A_WORD) {
491 if (!(stab = arg[1].arg_ptr.arg_stab))
495 stab = stabent(str_get(st[1]),TRUE);
496 if (!stab_io(stab)) {
502 fp = stab_io(stab)->ofp;
504 if (stab_io(stab)->fmt_stab)
505 form = stab_form(stab_io(stab)->fmt_stab);
507 form = stab_form(stab);
511 warn("No format for filehandle");
513 if (stab_io(stab)->ifp)
514 warn("Filehandle only opened for input");
516 warn("Write on closed filehandle");
523 format(&outrec,form,sp);
524 do_write(&outrec,stab_io(stab),sp);
525 if (stab_io(stab)->flags & IOF_FLUSH)
532 stab = arg[1].arg_ptr.arg_stab;
533 if (st[3]->str_nok || st[3]->str_pok)
534 anum = (int)str_gnum(st[3]);
537 value = (double)hdbmopen(stab_hash(stab),str_get(st[2]),anum);
540 fatal("No dbm or ndbm on this machine");
544 stab = arg[1].arg_ptr.arg_stab;
545 hdbmclose(stab_hash(stab));
548 fatal("No dbm or ndbm on this machine");
551 if ((arg[1].arg_type & A_MASK) == A_WORD)
552 stab = arg[1].arg_ptr.arg_stab;
554 stab = stabent(str_get(st[1]),TRUE);
555 tmps = str_get(st[2]);
556 if (do_open(stab,tmps,st[2]->str_cur)) {
557 value = (double)forkprocess;
558 stab_io(stab)->lines = 0;
561 else if (forkprocess == 0) /* we are a new child */
567 value = (double) do_trans(str,arg);
568 str = arg->arg_ptr.arg_str;
571 str_set(arg->arg_ptr.arg_str, do_trans(str,arg) == 0 ? Yes : No);
572 str = arg->arg_ptr.arg_str;
577 else if ((arg[1].arg_type & A_MASK) == A_WORD)
578 stab = arg[1].arg_ptr.arg_stab;
580 stab = stabent(str_get(st[1]),TRUE);
581 str_set(str, do_close(stab,TRUE) ? Yes : No );
585 sp = do_each(str,stab_hash(arg[1].arg_ptr.arg_stab),
590 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
594 str->str_nok = str->str_pok = 0;
595 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
596 str->str_state = SS_ARY;
599 ary = stab_array(arg[1].arg_ptr.arg_stab);
600 maxarg = ary->ary_fill + 1;
601 if (gimme == G_ARRAY) { /* array wanted */
604 if (maxarg > 0 && sp + maxarg > stack->ary_max) {
605 astore(stack,sp + maxarg, Nullstr);
606 st = stack->ary_array;
609 Copy(ary->ary_array, &st[1], maxarg, STR*);
614 value = (double)maxarg;
618 anum = ((int)str_gnum(st[2])) - arybase;
619 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,FALSE);
624 tmpstab = arg[1].arg_ptr.arg_stab;
625 tmps = str_get(st[2]);
626 str = hdelete(stab_hash(tmpstab),tmps,st[2]->str_cur);
627 if (tmpstab == envstab)
633 str->str_nok = str->str_pok = 0;
634 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
635 str->str_state = SS_HASH;
638 if (gimme == G_ARRAY) { /* array wanted */
639 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
644 tmpstab = arg[1].arg_ptr.arg_stab;
645 if (!stab_hash(tmpstab)->tbl_fill)
647 sprintf(buf,"%d/%d",stab_hash(tmpstab)->tbl_fill,
648 stab_hash(tmpstab)->tbl_max+1);
653 tmpstab = arg[1].arg_ptr.arg_stab;
654 tmps = str_get(st[2]);
655 str = hfetch(stab_hash(tmpstab),tmps,st[2]->str_cur,FALSE);
660 anum = ((int)str_gnum(st[2])) - arybase;
661 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,TRUE);
663 fatal("Assignment to non-creatable value, subscript %d",anum);
666 tmpstab = arg[1].arg_ptr.arg_stab;
667 tmps = str_get(st[2]);
668 anum = st[2]->str_cur;
669 str = hfetch(stab_hash(tmpstab),tmps,anum,TRUE);
671 fatal("Assignment to non-creatable value, subscript \"%s\"",tmps);
672 if (tmpstab == envstab) /* heavy wizardry going on here */
673 str_magic(str, tmpstab, 'E', tmps, anum); /* str is now magic */
674 /* he threw the brick up into the air */
675 else if (tmpstab == sigstab)
676 str_magic(str, tmpstab, 'S', tmps, anum);
678 else if (stab_hash(tmpstab)->tbl_dbm)
679 str_magic(str, tmpstab, 'D', tmps, anum);
685 goto do_slice_already;
689 goto do_slice_already;
693 goto do_slice_already;
697 goto do_slice_already;
702 sp = do_slice(arg[1].arg_ptr.arg_stab,str,anum,argtype,
706 sp = do_splice(stab_array(arg[1].arg_ptr.arg_stab),gimme,arglast);
709 if (arglast[2] - arglast[1] != 1)
710 str = do_push(stab_array(arg[1].arg_ptr.arg_stab),arglast);
712 str = Str_new(51,0); /* must copy the STR */
714 (void)apush(stab_array(arg[1].arg_ptr.arg_stab),str);
718 str = apop(ary = stab_array(arg[1].arg_ptr.arg_stab));
719 goto staticalization;
721 str = ashift(ary = stab_array(arg[1].arg_ptr.arg_stab));
725 if (ary->ary_flags & ARF_REAL)
726 (void)str_2static(str);
729 sp = do_unpack(str,gimme,arglast);
732 value = str_gnum(st[3]);
733 sp = do_split(str, arg[2].arg_ptr.arg_spat, (int)value,
738 value = (double)str_len(stab_val(defstab));
740 value = (double)str_len(st[1]);
743 do_sprintf(str, sp-arglast[0], st+1);
746 anum = ((int)str_gnum(st[2])) - arybase; /* anum=where to start*/
747 tmps = str_get(st[1]); /* force conversion to string */
748 if (argtype = (str == st[1]))
749 str = arg->arg_ptr.arg_str;
751 anum += st[1]->str_cur + arybase;
752 if (anum < 0 || anum > st[1]->str_cur)
755 optype = (int)str_gnum(st[3]);
759 anum = st[1]->str_cur - anum; /* anum=how many bytes left*/
762 str_nset(str, tmps, anum);
763 if (argtype) { /* it's an lvalue! */
764 lstr = (struct lstring*)str;
765 str->str_magic = st[1];
766 st[1]->str_rare = 's';
767 lstr->lstr_offset = tmps - str_get(st[1]);
768 lstr->lstr_len = anum;
773 (void)do_pack(str,arglast);
776 sp = do_grep(arg,str,gimme,arglast);
779 do_join(str,arglast);
782 tmps = str_get(st[1]);
783 value = (double) (str_cmp(st[1],st[2]) < 0);
786 tmps = str_get(st[1]);
787 value = (double) (str_cmp(st[1],st[2]) > 0);
790 tmps = str_get(st[1]);
791 value = (double) (str_cmp(st[1],st[2]) <= 0);
794 tmps = str_get(st[1]);
795 value = (double) (str_cmp(st[1],st[2]) >= 0);
798 tmps = str_get(st[1]);
799 value = (double) str_eq(st[1],st[2]);
802 tmps = str_get(st[1]);
803 value = (double) !str_eq(st[1],st[2]);
806 sp = do_subr(arg,gimme,arglast);
807 st = stack->ary_array + arglast[0]; /* maybe realloced */
810 sp = do_dbsubr(arg,gimme,arglast);
811 st = stack->ary_array + arglast[0]; /* maybe realloced */
814 if ((arg[1].arg_type & A_MASK) == A_WORD)
815 stab = arg[1].arg_ptr.arg_stab;
817 stab = stabent(str_get(st[1]),TRUE);
820 sp = do_sort(str,stab,
828 if (arglast[2] - arglast[1] != 1) {
829 do_join(str,arglast);
830 tmps = str_get(st[1]);
834 tmps = str_get(st[2]);
837 tmps = "Warning: something's wrong";
841 if (arglast[2] - arglast[1] != 1) {
842 do_join(str,arglast);
843 tmps = str_get(st[1]);
847 tmps = str_get(st[2]);
855 if ((arg[1].arg_type & A_MASK) == A_WORD)
856 stab = arg[1].arg_ptr.arg_stab;
858 stab = stabent(str_get(st[1]),TRUE);
861 if (!stab_io(stab)) {
863 warn("Filehandle never opened");
866 if (!(fp = stab_io(stab)->ofp)) {
868 if (stab_io(stab)->ifp)
869 warn("Filehandle opened only for input");
871 warn("Print on closed filehandle");
876 if (optype == O_PRTF || arglast[2] - arglast[1] != 1)
877 value = (double)do_aprint(arg,fp,arglast);
879 value = (double)do_print(st[2],fp);
880 if (orslen && optype == O_PRINT)
881 if (fwrite(ors, 1, orslen, fp) == 0)
884 if (stab_io(stab)->flags & IOF_FLUSH)
885 if (fflush(fp) == EOF)
893 tmps = str_get(st[1]);
894 if (!tmps || !*tmps) {
895 tmpstr = hfetch(stab_hash(envstab),"HOME",4,FALSE);
897 tmps = str_get(tmpstr);
899 if (!tmps || !*tmps) {
900 tmpstr = hfetch(stab_hash(envstab),"LOGDIR",6,FALSE);
902 tmps = str_get(tmpstr);
905 taintproper("Insecure dependency in chdir");
907 value = (double)(chdir(tmps) >= 0);
913 anum = (int)str_gnum(st[1]);
920 tmps = str_get(st[1]);
921 str_reset(tmps,arg[2].arg_ptr.arg_hash);
925 if (gimme == G_ARRAY)
928 str = st[sp - arglast[0]]; /* unwanted list, return last item */
935 else if ((arg[1].arg_type & A_MASK) == A_WORD)
936 stab = arg[1].arg_ptr.arg_stab;
938 stab = stabent(str_get(st[1]),TRUE);
939 str_set(str, do_eof(stab) ? Yes : No);
945 else if ((arg[1].arg_type & A_MASK) == A_WORD)
946 stab = arg[1].arg_ptr.arg_stab;
948 stab = stabent(str_get(st[1]),TRUE);
949 if (do_eof(stab)) /* make sure we have fp with something */
956 *str->str_ptr = getc(stab_io(stab)->ifp); /* should never be EOF */
963 else if ((arg[1].arg_type & A_MASK) == A_WORD)
964 stab = arg[1].arg_ptr.arg_stab;
966 stab = stabent(str_get(st[1]),TRUE);
968 value = (double)do_tell(stab);
975 if ((arg[1].arg_type & A_MASK) == A_WORD)
976 stab = arg[1].arg_ptr.arg_stab;
978 stab = stabent(str_get(st[1]),TRUE);
979 tmps = str_get(st[2]);
980 anum = (int)str_gnum(st[3]);
981 STR_GROW(st[2], anum+1), (tmps = str_get(st[2])); /* sneaky */
983 if (!stab_io(stab) || !stab_io(stab)->ifp)
986 else if (optype == O_RECV) {
987 argtype = sizeof buf;
988 optype = (int)str_gnum(st[4]);
989 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, optype,
992 st[2]->str_cur = anum;
993 st[2]->str_ptr[anum] = '\0';
994 str_nset(str,buf,argtype);
997 str_sset(str,&str_undef);
1000 else if (stab_io(stab)->type == 's') {
1001 argtype = sizeof buf;
1002 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, 0,
1006 else if (optype == O_RECV)
1010 anum = fread(tmps, 1, anum, stab_io(stab)->ifp);
1013 st[2]->str_cur = anum;
1014 st[2]->str_ptr[anum] = '\0';
1015 value = (double)anum;
1019 if ((arg[1].arg_type & A_MASK) == A_WORD)
1020 stab = arg[1].arg_ptr.arg_stab;
1022 stab = stabent(str_get(st[1]),TRUE);
1023 tmps = str_get(st[2]);
1024 anum = (int)str_gnum(st[3]);
1025 optype = sp - arglast[0];
1028 warn("Too many args on send");
1029 stio = stab_io(stab);
1030 if (!stio || !stio->ifp) {
1033 warn("Send on closed socket");
1035 else if (optype >= 4) {
1036 tmps2 = str_get(st[4]);
1037 anum = sendto(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur,
1038 anum, tmps2, st[4]->str_cur);
1041 anum = send(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur, anum);
1044 value = (double)anum;
1050 if ((arg[1].arg_type & A_MASK) == A_WORD)
1051 stab = arg[1].arg_ptr.arg_stab;
1053 stab = stabent(str_get(st[1]),TRUE);
1054 value = str_gnum(st[2]);
1055 str_set(str, do_seek(stab,
1056 (long)value, (int)str_gnum(st[3]) ) ? Yes : No);
1060 tmps = "_SUB_"; /* just fake up a "last _SUB_" */
1062 if (wantarray == G_ARRAY) {
1063 lastretstr = Nullstr;
1064 lastspbase = arglast[1];
1065 lastsize = arglast[2] - arglast[1];
1068 lastretstr = str_static(st[arglast[2] - arglast[0]]);
1074 tmps = str_get(arg[1].arg_ptr.arg_str);
1076 while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
1077 strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
1080 deb("(Skipping label #%d %s)\n",loop_ptr,
1081 loop_stack[loop_ptr].loop_label);
1088 deb("(Found label #%d %s)\n",loop_ptr,
1089 loop_stack[loop_ptr].loop_label);
1094 if (tmps && strEQ(tmps, "_SUB_"))
1095 fatal("Can't return outside a subroutine");
1096 fatal("Bad label: %s", maxarg > 0 ? tmps : "<null>");
1098 if (!lastretstr && optype == O_LAST && lastsize) {
1100 st += lastspbase + 1;
1101 optype = loop_stack[loop_ptr].loop_sp - lastspbase; /* negative */
1103 for (anum = lastsize; anum > 0; anum--,st++)
1104 st[optype] = str_static(st[0]);
1106 longjmp(loop_stack[loop_ptr].loop_env, O_LAST);
1108 longjmp(loop_stack[loop_ptr].loop_env, optype);
1110 case O_GOTO:/* shudder */
1111 goto_targ = str_get(arg[1].arg_ptr.arg_str);
1113 goto_targ = Nullch; /* just restart from top */
1114 if (optype == O_DUMP) {
1118 longjmp(top_env, 1);
1120 tmps = str_get(st[1]);
1122 if (!(tmps2 = fbminstr((unsigned char*)tmps,
1123 (unsigned char*)tmps + st[1]->str_cur, st[2])))
1125 if (tmps2 = fbminstr(Null(unsigned char*),Null(unsigned char*),Nullstr))
1127 value = (double)(-1 + arybase);
1129 value = (double)(tmps2 - tmps + arybase);
1132 tmps = str_get(st[1]);
1133 tmps2 = str_get(st[2]);
1135 if (!(tmps2 = rninstr(tmps, tmps + st[1]->str_cur,
1136 tmps2, tmps2 + st[2]->str_cur)))
1138 if (tmps2 = rninstr(Nullch,Nullch,Nullch,Nullch))
1140 value = (double)(-1 + arybase);
1142 value = (double)(tmps2 - tmps + arybase);
1146 value = (double) time(Null(long*));
1150 sp = do_tms(str,gimme,arglast);
1156 when = (long)str_gnum(st[1]);
1157 sp = do_time(str,localtime(&when),
1164 when = (long)str_gnum(st[1]);
1165 sp = do_time(str,gmtime(&when),
1169 sp = do_truncate(str,arg,
1174 sp = do_stat(str,arg,
1179 tmps = str_get(st[1]);
1181 str_set(str,fcrypt(tmps,str_get(st[2])));
1183 str_set(str,crypt(tmps,str_get(st[2])));
1187 "The crypt() function is unimplemented due to excessive paranoia.");
1191 value = str_gnum(st[1]);
1192 value = atan2(value,str_gnum(st[2]));
1196 value = str_gnum(stab_val(defstab));
1198 value = str_gnum(st[1]);
1203 value = str_gnum(stab_val(defstab));
1205 value = str_gnum(st[1]);
1212 value = str_gnum(st[1]);
1216 value = rand() * value / 2147483648.0;
1219 value = rand() * value / 65536.0;
1222 value = rand() * value / 32768.0;
1224 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1235 anum = (int)str_gnum(st[1]);
1240 value = str_gnum(stab_val(defstab));
1242 value = str_gnum(st[1]);
1247 value = str_gnum(stab_val(defstab));
1249 value = str_gnum(st[1]);
1254 value = str_gnum(stab_val(defstab));
1256 value = str_gnum(st[1]);
1257 value = sqrt(value);
1261 value = str_gnum(stab_val(defstab));
1263 value = str_gnum(st[1]);
1265 (void)modf(value,&value);
1267 (void)modf(-value,&value);
1273 tmps = str_get(stab_val(defstab));
1275 tmps = str_get(st[1]);
1277 value = (double) (*tmps & 255);
1280 value = (double) (anum & 255);
1287 tmps = str_get(st[1]);
1289 if (!tmps || !*tmps)
1290 sleep((32767<<16)+32767);
1292 sleep((unsigned int)atoi(tmps));
1294 value = (double)when;
1296 value = ((double)when) - value;
1300 sp = do_range(gimme,arglast);
1303 if (gimme == G_ARRAY) { /* it's a range */
1304 /* can we optimize to constant array? */
1305 if ((arg[1].arg_type & A_MASK) == A_SINGLE &&
1306 (arg[2].arg_type & A_MASK) == A_SINGLE) {
1307 st[2] = arg[2].arg_ptr.arg_str;
1308 sp = do_range(gimme,arglast);
1309 st = stack->ary_array;
1310 maxarg = sp - arglast[0];
1311 str_free(arg[1].arg_ptr.arg_str);
1312 str_free(arg[2].arg_ptr.arg_str);
1313 arg->arg_type = O_ARRAY;
1314 arg[1].arg_type = A_STAB|A_DONT;
1316 stab = arg[1].arg_ptr.arg_stab = aadd(genstab());
1317 ary = stab_array(stab);
1318 afill(ary,maxarg - 1);
1320 while (maxarg-- > 0)
1321 ary->ary_array[maxarg] = str_smake(st[maxarg]);
1324 arg->arg_type = optype = O_RANGE;
1325 maxarg = arg->arg_len = 2;
1327 arg[anum].arg_flags &= ~AF_ARYOK;
1328 argflags = arg[anum].arg_flags;
1329 argtype = arg[anum].arg_type & A_MASK;
1330 arg[anum].arg_type = argtype;
1331 argptr = arg[anum].arg_ptr;
1337 arg->arg_type = O_FLIP;
1340 if ((arg[1].arg_type & A_MASK) == A_SINGLE ?
1341 last_in_stab && (int)str_gnum(st[1]) == stab_io(last_in_stab)->lines
1344 str_numset(str,0.0);
1346 arg->arg_type = optype = O_FLOP;
1347 arg[2].arg_type &= ~A_DONT;
1348 arg[1].arg_type |= A_DONT;
1349 argflags = arg[2].arg_flags;
1350 argtype = arg[2].arg_type & A_MASK;
1351 argptr = arg[2].arg_ptr;
1360 if ((arg[2].arg_type & A_MASK) == A_SINGLE ?
1361 last_in_stab && (int)str_gnum(st[2]) == stab_io(last_in_stab)->lines
1364 arg->arg_type = O_FLIP;
1365 arg[1].arg_type &= ~A_DONT;
1366 arg[2].arg_type |= A_DONT;
1373 if (!anum && (tmpstab = stabent("$",allstabs)))
1374 str_numset(STAB_STR(tmpstab),(double)getpid());
1375 value = (double)anum;
1378 fatal("Unsupported function fork");
1384 anum = wait(&argflags);
1386 pidgone(anum,argflags);
1387 value = (double)anum;
1389 statusvalue = (unsigned short)argflags;
1392 fatal("Unsupported function wait");
1398 if (arglast[2] - arglast[1] == 1) {
1400 tainted |= st[2]->str_tainted;
1401 taintproper("Insecure dependency in system");
1404 while ((anum = vfork()) == -1) {
1405 if (errno != EAGAIN) {
1413 ihand = signal(SIGINT, SIG_IGN);
1414 qhand = signal(SIGQUIT, SIG_IGN);
1415 while ((argtype = wait(&argflags)) != anum && argtype >= 0)
1416 pidgone(argtype,argflags);
1420 (void)signal(SIGINT, ihand);
1421 (void)signal(SIGQUIT, qhand);
1422 statusvalue = (unsigned short)argflags;
1426 value = (double)((unsigned int)argflags & 0xffff);
1428 do_execfree(); /* free any memory child malloced on vfork */
1431 if ((arg[1].arg_type & A_MASK) == A_STAB)
1432 value = (double)do_aexec(st[1],arglast);
1433 else if (arglast[2] - arglast[1] != 1)
1434 value = (double)do_aexec(Nullstr,arglast);
1436 value = (double)do_exec(str_get(str_static(st[2])));
1440 if ((arg[1].arg_type & A_MASK) == A_STAB)
1441 value = (double)do_aspawn(st[1],arglast);
1442 else if (arglast[2] - arglast[1] != 1)
1443 value = (double)do_aspawn(Nullstr,arglast);
1445 value = (double)do_spawn(str_get(str_static(st[2])));
1450 if ((arg[1].arg_type & A_MASK) == A_STAB)
1451 value = (double)do_aexec(st[1],arglast);
1452 else if (arglast[2] - arglast[1] != 1)
1453 value = (double)do_aexec(Nullstr,arglast);
1455 value = (double)do_exec(str_get(str_static(st[2])));
1468 tmps = str_get(stab_val(defstab));
1470 tmps = str_get(st[1]);
1479 case '0': case '1': case '2': case '3': case '4':
1480 case '5': case '6': case '7':
1482 anum += *tmps++ & 15;
1484 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1485 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1489 anum += (*tmps++ & 7) + 9;
1498 value = (double)anum;
1502 value = (double)apply(optype,arglast);
1505 fatal("Unsupported function chown");
1510 value = (double)apply(optype,arglast);
1513 fatal("Unsupported function kill");
1519 value = (double)apply(optype,arglast);
1528 anum = umask((int)str_gnum(st[1]));
1529 value = (double)anum;
1531 taintproper("Insecure dependency in umask");
1535 fatal("Unsupported function umask");
1539 tmps = str_get(st[1]);
1540 tmps2 = str_get(st[2]);
1542 taintproper("Insecure dependency in rename");
1545 value = (double)(rename(tmps,tmps2) >= 0);
1547 if (same_dirent(tmps2, tmps)) /* can always rename to same name */
1550 if (euid || stat(tmps2,&statbuf) < 0 ||
1551 (statbuf.st_mode & S_IFMT) != S_IFDIR )
1552 (void)UNLINK(tmps2);
1553 if (!(anum = link(tmps,tmps2)))
1554 anum = UNLINK(tmps);
1556 value = (double)(anum >= 0);
1561 tmps = str_get(st[1]);
1562 tmps2 = str_get(st[2]);
1564 taintproper("Insecure dependency in link");
1566 value = (double)(link(tmps,tmps2) >= 0);
1569 fatal("Unsupported function link");
1573 tmps = str_get(st[1]);
1574 anum = (int)str_gnum(st[2]);
1576 taintproper("Insecure dependency in mkdir");
1579 value = (double)(mkdir(tmps,anum) >= 0);
1582 (void)strcpy(buf,"mkdir ");
1584 #if !defined(MKDIR) || !defined(RMDIR)
1586 for (tmps2 = buf+6; *tmps; ) {
1590 (void)strcpy(tmps2," 2>&1");
1591 rsfp = mypopen(buf,"r");
1594 tmps2 = fgets(buf,sizeof buf,rsfp);
1595 (void)mypclose(rsfp);
1596 if (tmps2 != Nullch) {
1597 for (errno = 1; errno < sys_nerr; errno++) {
1598 if (instr(buf,sys_errlist[errno])) /* you don't see this */
1603 #define EACCES EPERM
1605 if (instr(buf,"cannot make"))
1607 else if (instr(buf,"non-exist"))
1609 else if (instr(buf,"does not exist"))
1611 else if (instr(buf,"not empty"))
1613 else if (instr(buf,"cannot access"))
1619 else { /* some mkdirs return no failure indication */
1620 tmps = str_get(st[1]);
1621 anum = (stat(tmps,&statbuf) >= 0);
1622 if (optype == O_RMDIR)
1627 errno = EACCES; /* a guess */
1628 value = (double)anum;
1637 tmps = str_get(stab_val(defstab));
1639 tmps = str_get(st[1]);
1641 taintproper("Insecure dependency in rmdir");
1644 value = (double)(rmdir(tmps) >= 0);
1647 (void)strcpy(buf,"rmdir ");
1648 goto one_liner; /* see above in MKDIR */
1652 value = (double)getppid();
1655 fatal("Unsupported function getppid");
1663 anum = (int)str_gnum(st[1]);
1664 value = (double)getpgrp(anum);
1667 fatal("The getpgrp() function is unimplemented on this machine");
1672 argtype = (int)str_gnum(st[1]);
1673 anum = (int)str_gnum(st[2]);
1675 taintproper("Insecure dependency in setpgrp");
1677 value = (double)(setpgrp(argtype,anum) >= 0);
1680 fatal("The setpgrp() function is unimplemented on this machine");
1685 argtype = (int)str_gnum(st[1]);
1686 anum = (int)str_gnum(st[2]);
1687 value = (double)getpriority(argtype,anum);
1690 fatal("The getpriority() function is unimplemented on this machine");
1695 argtype = (int)str_gnum(st[1]);
1696 anum = (int)str_gnum(st[2]);
1697 optype = (int)str_gnum(st[3]);
1699 taintproper("Insecure dependency in setpriority");
1701 value = (double)(setpriority(argtype,anum,optype) >= 0);
1704 fatal("The setpriority() function is unimplemented on this machine");
1710 tmps = str_get(stab_val(defstab));
1712 tmps = str_get(st[1]);
1714 taintproper("Insecure dependency in chroot");
1716 value = (double)(chroot(tmps) >= 0);
1719 fatal("Unsupported function chroot");
1725 stab = last_in_stab;
1726 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1727 stab = arg[1].arg_ptr.arg_stab;
1729 stab = stabent(str_get(st[1]),TRUE);
1730 argtype = U_I(str_gnum(st[2]));
1732 taintproper("Insecure dependency in ioctl");
1734 anum = do_ctl(optype,stab,argtype,st[3]);
1738 value = (double)anum;
1741 str_set(str,"0 but true");
1747 stab = last_in_stab;
1748 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1749 stab = arg[1].arg_ptr.arg_stab;
1751 stab = stabent(str_get(st[1]),TRUE);
1752 if (stab && stab_io(stab))
1753 fp = stab_io(stab)->ifp;
1757 argtype = (int)str_gnum(st[2]);
1758 value = (double)(flock(fileno(fp),argtype) >= 0);
1764 fatal("The flock() function is unimplemented on this machine");
1768 ary = stab_array(arg[1].arg_ptr.arg_stab);
1769 if (arglast[2] - arglast[1] != 1)
1770 do_unshift(ary,arglast);
1772 str = Str_new(52,0); /* must copy the STR */
1773 str_sset(str,st[2]);
1775 (void)astore(ary,0,str);
1777 value = (double)(ary->ary_fill + 1);
1784 tmpstr = stab_val(defstab);
1787 (arg[1].arg_type & A_MASK) != A_NULL ? st[1] : stab_val(defstab);
1789 tainted |= tmpstr->str_tainted;
1790 taintproper("Insecure dependency in eval");
1792 sp = do_eval(tmpstr, optype, arg[2].arg_ptr.arg_hash,
1820 if (mystat(arg,st[1]) < 0)
1822 if (cando(anum,argtype,&statcache))
1827 if (mystat(arg,st[1]) < 0)
1832 if (mystat(arg,st[1]) < 0)
1834 if (statcache.st_uid == (optype == O_FTEOWNED ? euid : uid) )
1838 if (mystat(arg,st[1]) < 0)
1840 if (!statcache.st_size)
1844 if (mystat(arg,st[1]) < 0)
1846 value = (double)statcache.st_size;
1852 goto check_file_type;
1858 goto check_file_type;
1862 goto check_file_type;
1868 goto check_file_type;
1872 if (mystat(arg,st[1]) < 0)
1874 if ((statcache.st_mode & S_IFMT) == anum )
1880 goto check_file_type;
1885 if (arg[1].arg_type & A_DONT)
1886 fatal("You must supply explicit filename with -l");
1888 if (lstat(str_get(st[1]),&statcache) < 0)
1890 if ((statcache.st_mode & S_IFMT) == S_IFLNK )
1896 tmps = str_get(st[1]);
1897 tmps2 = str_get(st[2]);
1899 taintproper("Insecure dependency in symlink");
1901 value = (double)(symlink(tmps,tmps2) >= 0);
1904 fatal("Unsupported function symlink");
1909 tmps = str_get(stab_val(defstab));
1911 tmps = str_get(st[1]);
1912 anum = readlink(tmps,buf,sizeof buf);
1915 str_nset(str,buf,anum);
1918 fatal("Unsupported function readlink");
1941 if (mystat(arg,st[1]) < 0)
1943 if (statcache.st_mode & anum)
1947 if (arg[1].arg_type & A_DONT) {
1948 stab = arg[1].arg_ptr.arg_stab;
1952 stab = stabent(tmps = str_get(st[1]),FALSE);
1953 if (stab && stab_io(stab) && stab_io(stab)->ifp)
1954 anum = fileno(stab_io(stab)->ifp);
1955 else if (isdigit(*tmps))
1964 str = do_fttext(arg,st[1]);
1968 if ((arg[1].arg_type & A_MASK) == A_WORD)
1969 stab = arg[1].arg_ptr.arg_stab;
1971 stab = stabent(str_get(st[1]),TRUE);
1973 value = (double)do_socket(stab,arglast);
1975 (void)do_socket(stab,arglast);
1979 if ((arg[1].arg_type & A_MASK) == A_WORD)
1980 stab = arg[1].arg_ptr.arg_stab;
1982 stab = stabent(str_get(st[1]),TRUE);
1984 value = (double)do_bind(stab,arglast);
1986 (void)do_bind(stab,arglast);
1990 if ((arg[1].arg_type & A_MASK) == A_WORD)
1991 stab = arg[1].arg_ptr.arg_stab;
1993 stab = stabent(str_get(st[1]),TRUE);
1995 value = (double)do_connect(stab,arglast);
1997 (void)do_connect(stab,arglast);
2001 if ((arg[1].arg_type & A_MASK) == A_WORD)
2002 stab = arg[1].arg_ptr.arg_stab;
2004 stab = stabent(str_get(st[1]),TRUE);
2006 value = (double)do_listen(stab,arglast);
2008 (void)do_listen(stab,arglast);
2012 if ((arg[1].arg_type & A_MASK) == A_WORD)
2013 stab = arg[1].arg_ptr.arg_stab;
2015 stab = stabent(str_get(st[1]),TRUE);
2016 if ((arg[2].arg_type & A_MASK) == A_WORD)
2017 stab2 = arg[2].arg_ptr.arg_stab;
2019 stab2 = stabent(str_get(st[2]),TRUE);
2020 do_accept(str,stab,stab2);
2028 sp = do_ghent(optype,
2036 sp = do_gnent(optype,
2044 sp = do_gpent(optype,
2052 sp = do_gsent(optype,
2056 value = (double) sethostent((int)str_gnum(st[1]));
2059 value = (double) setnetent((int)str_gnum(st[1]));
2062 value = (double) setprotoent((int)str_gnum(st[1]));
2065 value = (double) setservent((int)str_gnum(st[1]));
2068 value = (double) endhostent();
2071 value = (double) endnetent();
2074 value = (double) endprotoent();
2077 value = (double) endservent();
2080 if ((arg[1].arg_type & A_MASK) == A_WORD)
2081 stab = arg[1].arg_ptr.arg_stab;
2083 stab = stabent(str_get(st[1]),TRUE);
2084 if ((arg[2].arg_type & A_MASK) == A_WORD)
2085 stab2 = arg[2].arg_ptr.arg_stab;
2087 stab2 = stabent(str_get(st[2]),TRUE);
2089 value = (double)do_spair(stab,stab2,arglast);
2091 (void)do_spair(stab,stab2,arglast);
2095 if ((arg[1].arg_type & A_MASK) == A_WORD)
2096 stab = arg[1].arg_ptr.arg_stab;
2098 stab = stabent(str_get(st[1]),TRUE);
2100 value = (double)do_shutdown(stab,arglast);
2102 (void)do_shutdown(stab,arglast);
2107 if ((arg[1].arg_type & A_MASK) == A_WORD)
2108 stab = arg[1].arg_ptr.arg_stab;
2110 stab = stabent(str_get(st[1]),TRUE);
2111 sp = do_sopt(optype,stab,arglast);
2115 if ((arg[1].arg_type & A_MASK) == A_WORD)
2116 stab = arg[1].arg_ptr.arg_stab;
2118 stab = stabent(str_get(st[1]),TRUE);
2119 sp = do_getsockname(optype,stab,arglast);
2122 #else /* SOCKET not defined */
2155 fatal("Unsupported socket function");
2159 sp = do_select(gimme,arglast);
2162 fatal("select not implemented");
2167 if ((arg[1].arg_type & A_MASK) == A_WORD)
2168 stab = arg[1].arg_ptr.arg_stab;
2170 stab = stabent(str_get(st[1]),TRUE);
2171 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2178 if ((arg[1].arg_type & A_MASK) == A_WORD)
2179 stab = arg[1].arg_ptr.arg_stab;
2181 stab = stabent(str_get(st[1]),TRUE);
2182 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
2185 str_set(str, (setmode(fileno(fp), O_BINARY) != -1) ? Yes : No);
2192 sp = do_vec(str == st[1], arg->arg_ptr.arg_str, arglast);
2198 sp = do_gpwent(optype,
2202 value = (double) setpwent();
2205 value = (double) endpwent();
2210 fatal("Unsupported password function");
2217 sp = do_ggrent(optype,
2221 value = (double) setgrent();
2224 value = (double) endgrent();
2229 fatal("Unsupported group function");
2234 if (!(tmps = getlogin()))
2238 fatal("Unsupported function getlogin");
2249 if ((arg[1].arg_type & A_MASK) == A_WORD)
2250 stab = arg[1].arg_ptr.arg_stab;
2252 stab = stabent(str_get(st[1]),TRUE);
2253 sp = do_dirop(optype,stab,gimme,arglast);
2256 value = (double)do_syscall(arglast);
2260 if ((arg[1].arg_type & A_MASK) == A_WORD)
2261 stab = arg[1].arg_ptr.arg_stab;
2263 stab = stabent(str_get(st[1]),TRUE);
2264 if ((arg[2].arg_type & A_MASK) == A_WORD)
2265 stab2 = arg[2].arg_ptr.arg_stab;
2267 stab2 = stabent(str_get(st[2]),TRUE);
2268 do_pipe(str,stab,stab2);
2271 fatal("Unsupported function pipe");
2282 deb("%s RETURNS \"%s\"\n",opname[optype],str_get(str));
2285 return arglast[0] + 1;
2292 anum = sp - arglast[0];
2295 deb("%s RETURNS ()\n",opname[optype]);
2298 deb("%s RETURNS (\"%s\")\n",opname[optype],str_get(st[1]));
2301 tmps = str_get(st[1]);
2302 deb("%s RETURNS %d ARGS (\"%s\",%s\"%s\")\n",opname[optype],
2303 anum,tmps,anum==2?"":"...,",str_get(st[anum]));
2328 str_numset(str,value);
2335 deb("%s RETURNS \"%f\"\n",opname[optype],value);
2338 return arglast[0] + 1;