1 /* $Header: eval.c,v 3.0 89/10/18 15:17:04 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 89/10/18 15:17:04 lwall
27 static void (*ihand)();
28 static void (*qhand)();
30 static int (*ihand)();
31 static int (*qhand)();
38 static struct lstring *lstr;
39 static char old_record_separator;
41 double sin(), cos(), atan2(), pow();
46 extern char *sys_errlist[];
65 int arglast[8]; /* highest sp for arg--valid only for non-O_LIST args */
66 unsigned long tmplong;
73 bool assigning = FALSE;
74 double exp(), log(), sqrt(), modf();
75 char *crypt(), *getenv();
76 extern void grow_dlevel();
80 optype = arg->arg_type;
81 maxarg = arg->arg_len;
83 str = arg->arg_ptr.arg_str;
84 if (sp + maxarg > stack->ary_max)
85 astore(stack, sp + maxarg, Nullstr);
86 st = stack->ary_array;
91 deb("%s (%lx) %d args:\n",opname[optype],arg,maxarg);
93 debname[dlevel] = opname[optype][0];
94 debdelim[dlevel] = ':';
95 if (++dlevel >= dlmax)
100 #include "evalargs.xc"
108 if (gimme == G_ARRAY)
114 if (gimme == G_ARRAY)
117 STR_SSET(str,st[arglast[anum]-arglast[0]]);
121 if (gimme == G_ARRAY)
124 STR_SSET(str,st[arglast[anum]-arglast[0]]);
134 anum = (int)str_gnum(st[2]);
136 tmpstr = Str_new(50,0);
137 str_sset(tmpstr,str);
139 str_scat(str,tmpstr);
142 str_sset(str,&str_no);
146 sp = do_match(str,arg,
148 if (gimme == G_ARRAY)
153 sp = do_match(str,arg,
155 if (gimme == G_ARRAY)
157 str_sset(str, str_true(str) ? &str_no : &str_yes);
161 sp = do_subst(str,arg,arglast[0]);
164 sp = do_subst(str,arg,arglast[0]);
165 str = arg->arg_ptr.arg_str;
166 str_set(str, str_true(str) ? No : Yes);
169 if (arg[1].arg_flags & AF_ARYOK) {
170 if (arg->arg_len == 1) {
171 arg->arg_type = O_LOCAL;
172 arg->arg_flags |= AF_LOCAL;
176 arg->arg_type = O_AASSIGN;
181 arg->arg_type = O_SASSIGN;
186 arglast[2] = arglast[1]; /* push a null array */
195 STR_SSET(str, st[2]);
200 str = arg->arg_ptr.arg_str;
201 for (sp = arglast[0] + 1; sp <= arglast[1]; sp++)
206 if (arg[1].arg_type & A_DONT) {
207 sp = do_defined(str,arg,
211 else if (str->str_pok || str->str_nok)
215 if (arg[1].arg_type & A_DONT) {
216 sp = do_undef(str,arg,
220 else if (str != stab_val(defstab)) {
221 str->str_pok = str->str_nok = 0;
226 sp = do_study(str,arg,
230 value = str_gnum(st[1]);
231 value = pow(value,str_gnum(st[2]));
234 value = str_gnum(st[1]);
235 value *= str_gnum(st[2]);
238 if ((value = str_gnum(st[2])) == 0.0)
239 fatal("Illegal division by zero");
240 value = str_gnum(st[1]) / value;
243 tmplong = (long) str_gnum(st[2]);
245 fatal("Illegal modulus zero");
246 when = (long)str_gnum(st[1]);
249 value = (double)(when % tmplong);
251 value = (double)(tmplong - (-when % tmplong));
255 value = str_gnum(st[1]);
256 value += str_gnum(st[2]);
259 value = str_gnum(st[1]);
260 value -= str_gnum(st[2]);
263 value = str_gnum(st[1]);
264 anum = (int)str_gnum(st[2]);
266 value = (double)(((long)value) << anum);
270 value = str_gnum(st[1]);
271 anum = (int)str_gnum(st[2]);
273 value = (double)(((long)value) >> anum);
277 value = str_gnum(st[1]);
278 value = (value < str_gnum(st[2])) ? 1.0 : 0.0;
281 value = str_gnum(st[1]);
282 value = (value > str_gnum(st[2])) ? 1.0 : 0.0;
285 value = str_gnum(st[1]);
286 value = (value <= str_gnum(st[2])) ? 1.0 : 0.0;
289 value = str_gnum(st[1]);
290 value = (value >= str_gnum(st[2])) ? 1.0 : 0.0;
294 if ((!st[1]->str_nok && !looks_like_number(st[1])) ||
295 (!st[2]->str_nok && !looks_like_number(st[2])) )
296 warn("Possible use of == on string value");
298 value = str_gnum(st[1]);
299 value = (value == str_gnum(st[2])) ? 1.0 : 0.0;
302 value = str_gnum(st[1]);
303 value = (value != str_gnum(st[2])) ? 1.0 : 0.0;
306 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
307 value = str_gnum(st[1]);
309 value = (double)(((long)value) & (long)str_gnum(st[2]));
314 do_vop(optype,str,st[1],st[2]);
317 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
318 value = str_gnum(st[1]);
320 value = (double)(((long)value) ^ (long)str_gnum(st[2]));
325 do_vop(optype,str,st[1],st[2]);
328 if (!sawvec || st[1]->str_nok || st[2]->str_nok) {
329 value = str_gnum(st[1]);
331 value = (double)(((long)value) | (long)str_gnum(st[2]));
336 do_vop(optype,str,st[1],st[2]);
338 /* use register in evaluating str_true() */
340 if (str_true(st[1])) {
343 argflags = arg[anum].arg_flags;
344 if (gimme == G_ARRAY)
345 argflags |= AF_ARYOK;
346 argtype = arg[anum].arg_type & A_MASK;
347 argptr = arg[anum].arg_ptr;
355 str_sset(str, st[1]);
363 if (str_true(st[1])) {
365 str_sset(str, st[1]);
375 argflags = arg[anum].arg_flags;
376 if (gimme == G_ARRAY)
377 argflags |= AF_ARYOK;
378 argtype = arg[anum].arg_type & A_MASK;
379 argptr = arg[anum].arg_ptr;
386 anum = (str_true(st[1]) ? 2 : 3);
387 optype = (anum == 2 ? O_ITEM2 : O_ITEM3);
388 argflags = arg[anum].arg_flags;
389 if (gimme == G_ARRAY)
390 argflags |= AF_ARYOK;
391 argtype = arg[anum].arg_type & A_MASK;
392 argptr = arg[anum].arg_ptr;
398 if (gimme == G_ARRAY)
403 value = -str_gnum(st[1]);
406 value = (double) !str_true(st[1]);
410 value = (double) ~(long)str_gnum(st[1]);
414 tmps = stab_name(defoutstab);
416 if ((arg[1].arg_type & A_MASK) == A_WORD)
417 defoutstab = arg[1].arg_ptr.arg_stab;
419 defoutstab = stabent(str_get(st[1]),TRUE);
420 if (!stab_io(defoutstab))
421 stab_io(defoutstab) = stio_new();
422 curoutstab = defoutstab;
430 else if ((arg[1].arg_type & A_MASK) == A_WORD) {
431 if (!(stab = arg[1].arg_ptr.arg_stab))
435 stab = stabent(str_get(st[1]),TRUE);
436 if (!stab_io(stab)) {
442 fp = stab_io(stab)->ofp;
444 if (stab_io(stab)->fmt_stab)
445 form = stab_form(stab_io(stab)->fmt_stab);
447 form = stab_form(stab);
451 warn("No format for filehandle");
453 if (stab_io(stab)->ifp)
454 warn("Filehandle only opened for input");
456 warn("Write on closed filehandle");
463 format(&outrec,form,sp);
464 do_write(&outrec,stab_io(stab),sp);
465 if (stab_io(stab)->flags & IOF_FLUSH)
472 if ((arg[1].arg_type & A_MASK) == A_WORD)
473 stab = arg[1].arg_ptr.arg_stab;
475 stab = stabent(str_get(st[1]),TRUE);
476 anum = (int)str_gnum(st[3]);
477 value = (double)hdbmopen(stab_hash(stab),str_get(st[2]),anum);
480 fatal("No dbm or ndbm on this machine");
484 if ((arg[1].arg_type & A_MASK) == A_WORD)
485 stab = arg[1].arg_ptr.arg_stab;
487 stab = stabent(str_get(st[1]),TRUE);
488 hdbmclose(stab_hash(stab));
491 fatal("No dbm or ndbm on this machine");
494 if ((arg[1].arg_type & A_MASK) == A_WORD)
495 stab = arg[1].arg_ptr.arg_stab;
497 stab = stabent(str_get(st[1]),TRUE);
498 if (do_open(stab,str_get(st[2]))) {
499 value = (double)forkprocess;
500 stab_io(stab)->lines = 0;
507 value = (double) do_trans(str,arg);
508 str = arg->arg_ptr.arg_str;
511 str_set(arg->arg_ptr.arg_str, do_trans(str,arg) == 0 ? Yes : No);
512 str = arg->arg_ptr.arg_str;
517 else if ((arg[1].arg_type & A_MASK) == A_WORD)
518 stab = arg[1].arg_ptr.arg_stab;
520 stab = stabent(str_get(st[1]),TRUE);
521 str_set(str, do_close(stab,TRUE) ? Yes : No );
525 sp = do_each(str,stab_hash(arg[1].arg_ptr.arg_stab),
530 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
534 str->str_nok = str->str_pok = 0;
535 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
536 str->str_state = SS_ARY;
539 ary = stab_array(arg[1].arg_ptr.arg_stab);
540 maxarg = ary->ary_fill + 1;
541 if (gimme == G_ARRAY) { /* array wanted */
544 if (maxarg > 0 && sp + maxarg > stack->ary_max) {
545 astore(stack,sp + maxarg, Nullstr);
546 st = stack->ary_array;
548 Copy(ary->ary_array, &st[sp+1], maxarg, STR*);
553 str = afetch(ary,maxarg - 1,FALSE);
556 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),
557 ((int)str_gnum(st[2])) - arybase,FALSE);
562 tmpstab = arg[1].arg_ptr.arg_stab;
563 tmps = str_get(st[2]);
564 str = hdelete(stab_hash(tmpstab),tmps,st[2]->str_cur);
565 if (tmpstab == envstab)
571 str->str_nok = str->str_pok = 0;
572 str->str_u.str_stab = arg[1].arg_ptr.arg_stab;
573 str->str_state = SS_HASH;
576 if (gimme == G_ARRAY) { /* array wanted */
577 sp = do_kv(str,stab_hash(arg[1].arg_ptr.arg_stab), optype,
582 tmpstab = arg[1].arg_ptr.arg_stab;
583 sprintf(buf,"%d/%d",stab_hash(tmpstab)->tbl_fill,
584 stab_hash(tmpstab)->tbl_max+1);
589 tmpstab = arg[1].arg_ptr.arg_stab;
590 tmps = str_get(st[2]);
591 str = hfetch(stab_hash(tmpstab),tmps,st[2]->str_cur,FALSE);
596 anum = ((int)str_gnum(st[2])) - arybase;
597 str = afetch(stab_array(arg[1].arg_ptr.arg_stab),anum,TRUE);
599 fatal("Assignment to non-creatable value, subscript %d",anum);
602 tmpstab = arg[1].arg_ptr.arg_stab;
603 tmps = str_get(st[2]);
604 anum = st[2]->str_cur;
605 str = hfetch(stab_hash(tmpstab),tmps,anum,TRUE);
607 fatal("Assignment to non-creatable value, subscript \"%s\"",tmps);
608 if (tmpstab == envstab) /* heavy wizardry going on here */
609 str_magic(str, tmpstab, 'E', tmps, anum); /* str is now magic */
610 /* he threw the brick up into the air */
611 else if (tmpstab == sigstab)
612 str_magic(str, tmpstab, 'S', tmps, anum);
614 else if (stab_hash(tmpstab)->tbl_dbm)
615 str_magic(str, tmpstab, 'D', tmps, anum);
621 goto do_slice_already;
625 goto do_slice_already;
629 goto do_slice_already;
634 sp = do_slice(arg[1].arg_ptr.arg_stab,anum,argtype,
638 if (arglast[2] - arglast[1] != 1)
639 str = do_push(stab_array(arg[1].arg_ptr.arg_stab),arglast);
641 str = Str_new(51,0); /* must copy the STR */
643 (void)apush(stab_array(arg[1].arg_ptr.arg_stab),str);
647 str = apop(ary = stab_array(arg[1].arg_ptr.arg_stab));
648 goto staticalization;
650 str = ashift(ary = stab_array(arg[1].arg_ptr.arg_stab));
654 if (ary->ary_flags & ARF_REAL)
655 (void)str_2static(str);
658 sp = do_unpack(str,gimme,arglast);
661 value = str_gnum(st[3]);
662 sp = do_split(str, arg[2].arg_ptr.arg_spat, (int)value,
667 value = (double)str_len(stab_val(defstab));
669 value = (double)str_len(st[1]);
672 do_sprintf(str, sp-arglast[0], st+1);
675 anum = ((int)str_gnum(st[2])) - arybase; /* anum=where to start*/
676 tmps = str_get(st[1]); /* force conversion to string */
677 if (argtype = (str == st[1]))
678 str = arg->arg_ptr.arg_str;
680 anum += st[1]->str_cur + arybase;
681 if (anum < 0 || anum > st[1]->str_cur)
684 optype = (int)str_gnum(st[3]);
688 anum = st[1]->str_cur - anum; /* anum=how many bytes left*/
691 str_nset(str, tmps, anum);
692 if (argtype) { /* it's an lvalue! */
693 lstr = (struct lstring*)str;
694 str->str_magic = st[1];
695 st[1]->str_rare = 's';
696 lstr->lstr_offset = tmps - str_get(st[1]);
697 lstr->lstr_len = anum;
702 (void)do_pack(str,arglast);
705 sp = do_grep(arg,str,gimme,arglast);
708 do_join(str,arglast);
711 tmps = str_get(st[1]);
712 value = (double) (str_cmp(st[1],st[2]) < 0);
715 tmps = str_get(st[1]);
716 value = (double) (str_cmp(st[1],st[2]) > 0);
719 tmps = str_get(st[1]);
720 value = (double) (str_cmp(st[1],st[2]) <= 0);
723 tmps = str_get(st[1]);
724 value = (double) (str_cmp(st[1],st[2]) >= 0);
727 tmps = str_get(st[1]);
728 value = (double) str_eq(st[1],st[2]);
731 tmps = str_get(st[1]);
732 value = (double) !str_eq(st[1],st[2]);
735 sp = do_subr(arg,gimme,arglast);
736 st = stack->ary_array + arglast[0]; /* maybe realloced */
739 sp = do_dbsubr(arg,gimme,arglast);
740 st = stack->ary_array + arglast[0]; /* maybe realloced */
743 if ((arg[1].arg_type & A_MASK) == A_WORD)
744 stab = arg[1].arg_ptr.arg_stab;
746 stab = stabent(str_get(st[1]),TRUE);
749 sp = do_sort(str,stab,
757 if (arglast[2] - arglast[1] != 1) {
758 do_join(str,arglast);
759 tmps = str_get(st[1]);
763 tmps = str_get(st[2]);
766 tmps = "Warning: something's wrong";
770 if (arglast[2] - arglast[1] != 1) {
771 do_join(str,arglast);
772 tmps = str_get(st[1]);
776 tmps = str_get(st[2]);
784 if ((arg[1].arg_type & A_MASK) == A_WORD)
785 stab = arg[1].arg_ptr.arg_stab;
787 stab = stabent(str_get(st[1]),TRUE);
790 if (!stab_io(stab)) {
792 warn("Filehandle never opened");
795 if (!(fp = stab_io(stab)->ofp)) {
797 if (stab_io(stab)->ifp)
798 warn("Filehandle opened only for input");
800 warn("Print on closed filehandle");
805 if (optype == O_PRTF || arglast[2] - arglast[1] != 1)
806 value = (double)do_aprint(arg,fp,arglast);
808 value = (double)do_print(st[2],fp);
809 if (orslen && optype == O_PRINT)
810 if (fwrite(ors, 1, orslen, fp) == 0)
813 if (stab_io(stab)->flags & IOF_FLUSH)
814 if (fflush(fp) == EOF)
820 tmps = str_get(stab_val(defstab));
822 tmps = str_get(st[1]);
823 if (!tmps || !*tmps) {
824 tmpstr = hfetch(stab_hash(envstab),"HOME",4,FALSE);
826 tmps = str_get(tmpstr);
828 if (!tmps || !*tmps) {
829 tmpstr = hfetch(stab_hash(envstab),"LOGDIR",6,FALSE);
831 tmps = str_get(tmpstr);
834 taintproper("Insecure dependency in chdir");
836 value = (double)(chdir(tmps) >= 0);
842 anum = (int)str_gnum(st[1]);
849 tmps = str_get(st[1]);
850 str_reset(tmps,arg[2].arg_ptr.arg_hash);
854 if (gimme == G_ARRAY)
857 str = st[sp - arglast[0]]; /* unwanted list, return last item */
864 else if ((arg[1].arg_type & A_MASK) == A_WORD)
865 stab = arg[1].arg_ptr.arg_stab;
867 stab = stabent(str_get(st[1]),TRUE);
868 str_set(str, do_eof(stab) ? Yes : No);
874 else if ((arg[1].arg_type & A_MASK) == A_WORD)
875 stab = arg[1].arg_ptr.arg_stab;
877 stab = stabent(str_get(st[1]),TRUE);
878 if (do_eof(stab)) /* make sure we have fp with something */
885 *str->str_ptr = getc(stab_io(stab)->ifp); /* should never be EOF */
892 else if ((arg[1].arg_type & A_MASK) == A_WORD)
893 stab = arg[1].arg_ptr.arg_stab;
895 stab = stabent(str_get(st[1]),TRUE);
897 value = (double)do_tell(stab);
904 if ((arg[1].arg_type & A_MASK) == A_WORD)
905 stab = arg[1].arg_ptr.arg_stab;
907 stab = stabent(str_get(st[1]),TRUE);
908 tmps = str_get(st[2]);
909 anum = (int)str_gnum(st[3]);
910 STR_GROW(st[2], anum+1), (tmps = str_get(st[2])); /* sneaky */
912 if (!stab_io(stab) || !stab_io(stab)->ifp)
915 else if (optype == O_RECV) {
916 argtype = sizeof buf;
917 optype = (int)str_gnum(st[4]);
918 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, optype,
921 st[2]->str_cur = anum;
922 st[2]->str_ptr[anum] = '\0';
923 str_nset(str,buf,argtype);
926 str_sset(str,&str_undef);
929 else if (stab_io(stab)->type == 's') {
930 argtype = sizeof buf;
931 anum = recvfrom(fileno(stab_io(stab)->ifp), tmps, anum, 0,
935 else if (optype == O_RECV)
939 anum = fread(tmps, 1, anum, stab_io(stab)->ifp);
942 st[2]->str_cur = anum;
943 st[2]->str_ptr[anum] = '\0';
944 value = (double)anum;
948 if ((arg[1].arg_type & A_MASK) == A_WORD)
949 stab = arg[1].arg_ptr.arg_stab;
951 stab = stabent(str_get(st[1]),TRUE);
952 tmps = str_get(st[2]);
953 anum = (int)str_gnum(st[3]);
954 optype = sp - arglast[0];
957 warn("Too many args on send");
959 tmps2 = str_get(st[4]);
960 anum = sendto(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur,
961 anum, tmps2, st[4]->str_cur);
964 anum = send(fileno(stab_io(stab)->ifp), tmps, st[2]->str_cur, anum);
967 value = (double)anum;
973 if ((arg[1].arg_type & A_MASK) == A_WORD)
974 stab = arg[1].arg_ptr.arg_stab;
976 stab = stabent(str_get(st[1]),TRUE);
977 value = str_gnum(st[2]);
978 str_set(str, do_seek(stab,
979 (long)value, (int)str_gnum(st[3]) ) ? Yes : No);
983 tmps = "SUB"; /* just fake up a "last SUB" */
985 if (gimme == G_ARRAY) {
986 lastretstr = Nullstr;
987 lastspbase = arglast[1];
988 lastsize = arglast[2] - arglast[1];
991 lastretstr = str_static(st[arglast[2] - arglast[0]]);
997 tmps = str_get(arg[1].arg_ptr.arg_str);
999 while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
1000 strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
1003 deb("(Skipping label #%d %s)\n",loop_ptr,
1004 loop_stack[loop_ptr].loop_label);
1011 deb("(Found label #%d %s)\n",loop_ptr,
1012 loop_stack[loop_ptr].loop_label);
1017 fatal("Bad label: %s", maxarg > 0 ? tmps : "<null>");
1018 if (!lastretstr && optype == O_LAST && lastsize) {
1020 st += lastspbase + 1;
1021 optype = loop_stack[loop_ptr].loop_sp - lastspbase; /* negative */
1023 for (anum = lastsize; anum > 0; anum--,st++)
1024 st[optype] = str_static(st[0]);
1026 longjmp(loop_stack[loop_ptr].loop_env, O_LAST);
1028 longjmp(loop_stack[loop_ptr].loop_env, optype);
1030 case O_GOTO:/* shudder */
1031 goto_targ = str_get(arg[1].arg_ptr.arg_str);
1033 goto_targ = Nullch; /* just restart from top */
1034 if (optype == O_DUMP) {
1038 longjmp(top_env, 1);
1040 tmps = str_get(st[1]);
1042 if (!(tmps2 = fbminstr((unsigned char*)tmps,
1043 (unsigned char*)tmps + st[1]->str_cur, st[2])))
1045 if (tmps2 = fbminstr(Null(unsigned char*),Null(unsigned char*),Nullstr))
1047 value = (double)(-1 + arybase);
1049 value = (double)(tmps2 - tmps + arybase);
1052 tmps = str_get(st[1]);
1053 tmps2 = str_get(st[2]);
1055 if (!(tmps2 = rninstr(tmps, tmps + st[1]->str_cur,
1056 tmps2, tmps2 + st[2]->str_cur)))
1058 if (tmps2 = rninstr(Nullch,Nullch,Nullch,Nullch))
1060 value = (double)(-1 + arybase);
1062 value = (double)(tmps2 - tmps + arybase);
1066 value = (double) time(Null(long*));
1070 sp = do_tms(str,gimme,arglast);
1076 when = (long)str_gnum(st[1]);
1077 sp = do_time(str,localtime(&when),
1084 when = (long)str_gnum(st[1]);
1085 sp = do_time(str,gmtime(&when),
1090 sp = do_stat(str,arg,
1095 tmps = str_get(st[1]);
1097 str_set(str,fcrypt(tmps,str_get(st[2])));
1099 str_set(str,crypt(tmps,str_get(st[2])));
1103 "The crypt() function is unimplemented due to excessive paranoia.");
1107 value = str_gnum(st[1]);
1108 value = atan2(value,str_gnum(st[2]));
1112 value = str_gnum(stab_val(defstab));
1114 value = str_gnum(st[1]);
1119 value = str_gnum(stab_val(defstab));
1121 value = str_gnum(st[1]);
1128 value = str_gnum(st[1]);
1132 value = rand() * value / 2147483648.0;
1135 value = rand() * value / 65536.0;
1138 value = rand() * value / 32768.0;
1140 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1151 anum = (int)str_gnum(st[1]);
1156 value = str_gnum(stab_val(defstab));
1158 value = str_gnum(st[1]);
1163 value = str_gnum(stab_val(defstab));
1165 value = str_gnum(st[1]);
1170 value = str_gnum(stab_val(defstab));
1172 value = str_gnum(st[1]);
1173 value = sqrt(value);
1177 value = str_gnum(stab_val(defstab));
1179 value = str_gnum(st[1]);
1181 (void)modf(value,&value);
1183 (void)modf(-value,&value);
1189 tmps = str_get(stab_val(defstab));
1191 tmps = str_get(st[1]);
1193 value = (double) *tmps;
1196 value = (double) anum;
1203 tmps = str_get(st[1]);
1205 if (!tmps || !*tmps)
1206 sleep((32767<<16)+32767);
1208 sleep((unsigned int)atoi(tmps));
1210 value = (double)when;
1212 value = ((double)when) - value;
1216 sp = do_range(gimme,arglast);
1219 if (gimme == G_ARRAY) { /* it's a range */
1220 /* can we optimize to constant array? */
1221 if ((arg[1].arg_type & A_MASK) == A_SINGLE &&
1222 (arg[2].arg_type & A_MASK) == A_SINGLE) {
1223 st[2] = arg[2].arg_ptr.arg_str;
1224 sp = do_range(gimme,arglast);
1225 st = stack->ary_array;
1226 maxarg = sp - arglast[0];
1227 str_free(arg[1].arg_ptr.arg_str);
1228 str_free(arg[2].arg_ptr.arg_str);
1229 arg->arg_type = O_ARRAY;
1230 arg[1].arg_type = A_STAB|A_DONT;
1232 stab = arg[1].arg_ptr.arg_stab = aadd(genstab());
1233 ary = stab_array(stab);
1234 afill(ary,maxarg - 1);
1236 while (maxarg-- > 0)
1237 ary->ary_array[maxarg] = str_smake(st[maxarg]);
1240 arg->arg_type = optype = O_RANGE;
1241 maxarg = arg->arg_len = 2;
1243 arg[anum].arg_flags &= ~AF_ARYOK;
1244 argflags = arg[anum].arg_flags;
1245 argtype = arg[anum].arg_type & A_MASK;
1246 arg[anum].arg_type = argtype;
1247 argptr = arg[anum].arg_ptr;
1253 arg->arg_type = O_FLIP;
1256 if ((arg[1].arg_type & A_MASK) == A_SINGLE ?
1257 last_in_stab && (int)str_gnum(st[1]) == stab_io(last_in_stab)->lines
1260 str_numset(str,0.0);
1262 arg->arg_type = optype = O_FLOP;
1263 arg[2].arg_type &= ~A_DONT;
1264 arg[1].arg_type |= A_DONT;
1265 argflags = arg[2].arg_flags;
1266 argtype = arg[2].arg_type & A_MASK;
1267 argptr = arg[2].arg_ptr;
1276 if ((arg[2].arg_type & A_MASK) == A_SINGLE ?
1277 last_in_stab && (int)str_gnum(st[2]) == stab_io(last_in_stab)->lines
1280 arg->arg_type = O_FLIP;
1281 arg[1].arg_type &= ~A_DONT;
1282 arg[2].arg_type |= A_DONT;
1288 if (!anum && (tmpstab = stabent("$",allstabs)))
1289 str_numset(STAB_STR(tmpstab),(double)getpid());
1290 value = (double)anum;
1294 ihand = signal(SIGINT, SIG_IGN);
1295 qhand = signal(SIGQUIT, SIG_IGN);
1296 anum = wait(&argflags);
1298 pidgone(anum,argflags);
1299 value = (double)anum;
1303 (void)signal(SIGINT, ihand);
1304 (void)signal(SIGQUIT, qhand);
1305 statusvalue = (unsigned short)argflags;
1309 if (arglast[2] - arglast[1] == 1) {
1311 tainted |= st[2]->str_tainted;
1312 taintproper("Insecure dependency in system");
1315 while ((anum = vfork()) == -1) {
1316 if (errno != EAGAIN) {
1324 ihand = signal(SIGINT, SIG_IGN);
1325 qhand = signal(SIGQUIT, SIG_IGN);
1326 while ((argtype = wait(&argflags)) != anum && argtype >= 0)
1327 pidgone(argtype,argflags);
1331 (void)signal(SIGINT, ihand);
1332 (void)signal(SIGQUIT, qhand);
1333 statusvalue = (unsigned short)argflags;
1337 value = (double)((unsigned int)argflags & 0xffff);
1341 if ((arg[1].arg_type & A_MASK) == A_STAB)
1342 value = (double)do_aexec(st[1],arglast);
1343 else if (arglast[2] - arglast[1] != 1)
1344 value = (double)do_aexec(Nullstr,arglast);
1346 value = (double)do_exec(str_get(str_static(st[2])));
1350 if ((arg[1].arg_type & A_MASK) == A_STAB)
1351 value = (double)do_aexec(st[1],arglast);
1352 else if (arglast[2] - arglast[1] != 1)
1353 value = (double)do_aexec(Nullstr,arglast);
1355 value = (double)do_exec(str_get(str_static(st[2])));
1368 tmps = str_get(stab_val(defstab));
1370 tmps = str_get(st[1]);
1379 case '0': case '1': case '2': case '3': case '4':
1380 case '5': case '6': case '7':
1382 anum += *tmps++ & 15;
1384 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1385 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1389 anum += (*tmps++ & 7) + 9;
1398 value = (double)anum;
1405 value = (double)apply(optype,arglast);
1413 anum = umask((int)str_gnum(st[1]));
1414 value = (double)anum;
1416 taintproper("Insecure dependency in umask");
1420 tmps = str_get(st[1]);
1421 tmps2 = str_get(st[2]);
1423 taintproper("Insecure dependency in rename");
1426 value = (double)(rename(tmps,tmps2) >= 0);
1428 if (euid || stat(tmps2,&statbuf) < 0 ||
1429 (statbuf.st_mode & S_IFMT) != S_IFDIR )
1430 (void)UNLINK(tmps2); /* avoid unlinking a directory */
1431 if (!(anum = link(tmps,tmps2)))
1432 anum = UNLINK(tmps);
1433 value = (double)(anum >= 0);
1437 tmps = str_get(st[1]);
1438 tmps2 = str_get(st[2]);
1440 taintproper("Insecure dependency in link");
1442 value = (double)(link(tmps,tmps2) >= 0);
1445 tmps = str_get(st[1]);
1446 anum = (int)str_gnum(st[2]);
1448 taintproper("Insecure dependency in mkdir");
1451 value = (double)(mkdir(tmps,anum) >= 0);
1453 (void)sprintf(buf,"mkdir %s 2>&1",tmps);
1455 rsfp = mypopen(buf,"r");
1458 tmps2 = fgets(buf,sizeof buf,rsfp);
1459 (void)mypclose(rsfp);
1460 if (tmps2 != Nullch) {
1461 for (errno = 1; errno <= sys_nerr; errno++) {
1462 if (instr(buf,sys_errlist[errno])) /* you don't see this */
1477 tmps = str_get(stab_val(defstab));
1479 tmps = str_get(st[1]);
1481 taintproper("Insecure dependency in rmdir");
1484 value = (double)(rmdir(tmps) >= 0);
1487 (void)sprintf(buf,"rmdir %s 2>&1",tmps);
1488 goto one_liner; /* see above in MKDIR */
1491 value = (double)getppid();
1498 anum = (int)str_gnum(st[1]);
1499 value = (double)getpgrp(anum);
1502 fatal("The getpgrp() function is unimplemented on this machine");
1507 argtype = (int)str_gnum(st[1]);
1508 anum = (int)str_gnum(st[2]);
1510 taintproper("Insecure dependency in setpgrp");
1512 value = (double)(setpgrp(argtype,anum) >= 0);
1515 fatal("The setpgrp() function is unimplemented on this machine");
1520 argtype = (int)str_gnum(st[1]);
1521 anum = (int)str_gnum(st[2]);
1522 value = (double)getpriority(argtype,anum);
1525 fatal("The getpriority() function is unimplemented on this machine");
1530 argtype = (int)str_gnum(st[1]);
1531 anum = (int)str_gnum(st[2]);
1532 optype = (int)str_gnum(st[3]);
1534 taintproper("Insecure dependency in setpriority");
1536 value = (double)(setpriority(argtype,anum,optype) >= 0);
1539 fatal("The setpriority() function is unimplemented on this machine");
1544 tmps = str_get(stab_val(defstab));
1546 tmps = str_get(st[1]);
1548 taintproper("Insecure dependency in chroot");
1550 value = (double)(chroot(tmps) >= 0);
1555 stab = last_in_stab;
1556 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1557 stab = arg[1].arg_ptr.arg_stab;
1559 stab = stabent(str_get(st[1]),TRUE);
1560 argtype = (int)str_gnum(st[2]);
1562 taintproper("Insecure dependency in ioctl");
1564 anum = do_ctl(optype,stab,argtype,st[3]);
1569 str_set(str,"0 but true");
1575 stab = last_in_stab;
1576 else if ((arg[1].arg_type & A_MASK) == A_WORD)
1577 stab = arg[1].arg_ptr.arg_stab;
1579 stab = stabent(str_get(st[1]),TRUE);
1580 if (stab && stab_io(stab))
1581 fp = stab_io(stab)->ifp;
1585 argtype = (int)str_gnum(st[2]);
1586 value = (double)(flock(fileno(fp),argtype) >= 0);
1592 fatal("The flock() function is unimplemented on this machine");
1596 ary = stab_array(arg[1].arg_ptr.arg_stab);
1597 if (arglast[2] - arglast[1] != 1)
1598 do_unshift(ary,arglast);
1600 str = Str_new(52,0); /* must copy the STR */
1601 str_sset(str,st[2]);
1603 (void)astore(ary,0,str);
1605 value = (double)(ary->ary_fill + 1);
1610 tmpstr = stab_val(defstab);
1613 (arg[1].arg_type & A_MASK) != A_NULL ? st[1] : stab_val(defstab);
1615 tainted |= tmpstr->str_tainted;
1616 taintproper("Insecure dependency in eval");
1618 sp = do_eval(tmpstr, optype, arg[2].arg_ptr.arg_hash,
1646 if (mystat(arg,st[1]) < 0)
1648 if (cando(anum,argtype,&statcache))
1653 if (mystat(arg,st[1]) < 0)
1658 if (mystat(arg,st[1]) < 0)
1660 if (statcache.st_uid == (optype == O_FTEOWNED ? euid : uid) )
1664 if (mystat(arg,st[1]) < 0)
1666 if (!statcache.st_size)
1670 if (mystat(arg,st[1]) < 0)
1672 if (statcache.st_size)
1679 goto check_file_type;
1685 goto check_file_type;
1688 goto check_file_type;
1691 goto check_file_type;
1695 if (mystat(arg,st[1]) < 0)
1697 if ((statcache.st_mode & S_IFMT) == anum )
1703 goto check_file_type;
1709 if (lstat(str_get(st[1]),&statcache) < 0)
1711 if ((statcache.st_mode & S_IFMT) == S_IFLNK )
1717 tmps = str_get(st[1]);
1718 tmps2 = str_get(st[2]);
1720 taintproper("Insecure dependency in symlink");
1722 value = (double)(symlink(tmps,tmps2) >= 0);
1725 fatal("Unsupported function symlink()");
1730 tmps = str_get(stab_val(defstab));
1732 tmps = str_get(st[1]);
1733 anum = readlink(tmps,buf,sizeof buf);
1736 str_nset(str,buf,anum);
1739 fatal("Unsupported function readlink()");
1750 if (mystat(arg,st[1]) < 0)
1752 if (statcache.st_mode & anum)
1756 if (arg[1].arg_type & A_DONT) {
1757 stab = arg[1].arg_ptr.arg_stab;
1761 stab = stabent(tmps = str_get(st[1]),FALSE);
1762 if (stab && stab_io(stab) && stab_io(stab)->ifp)
1763 anum = fileno(stab_io(stab)->ifp);
1764 else if (isdigit(*tmps))
1773 str = do_fttext(arg,st[1]);
1777 if ((arg[1].arg_type & A_MASK) == A_WORD)
1778 stab = arg[1].arg_ptr.arg_stab;
1780 stab = stabent(str_get(st[1]),TRUE);
1782 value = (double)do_socket(stab,arglast);
1784 (void)do_socket(stab,arglast);
1788 if ((arg[1].arg_type & A_MASK) == A_WORD)
1789 stab = arg[1].arg_ptr.arg_stab;
1791 stab = stabent(str_get(st[1]),TRUE);
1793 value = (double)do_bind(stab,arglast);
1795 (void)do_bind(stab,arglast);
1799 if ((arg[1].arg_type & A_MASK) == A_WORD)
1800 stab = arg[1].arg_ptr.arg_stab;
1802 stab = stabent(str_get(st[1]),TRUE);
1804 value = (double)do_connect(stab,arglast);
1806 (void)do_connect(stab,arglast);
1810 if ((arg[1].arg_type & A_MASK) == A_WORD)
1811 stab = arg[1].arg_ptr.arg_stab;
1813 stab = stabent(str_get(st[1]),TRUE);
1815 value = (double)do_listen(stab,arglast);
1817 (void)do_listen(stab,arglast);
1821 if ((arg[1].arg_type & A_MASK) == A_WORD)
1822 stab = arg[1].arg_ptr.arg_stab;
1824 stab = stabent(str_get(st[1]),TRUE);
1825 if ((arg[2].arg_type & A_MASK) == A_WORD)
1826 stab2 = arg[2].arg_ptr.arg_stab;
1828 stab2 = stabent(str_get(st[2]),TRUE);
1829 do_accept(str,stab,stab2);
1837 sp = do_ghent(optype,
1845 sp = do_gnent(optype,
1853 sp = do_gpent(optype,
1861 sp = do_gsent(optype,
1865 value = (double) sethostent((int)str_gnum(st[1]));
1868 value = (double) setnetent((int)str_gnum(st[1]));
1871 value = (double) setprotoent((int)str_gnum(st[1]));
1874 value = (double) setservent((int)str_gnum(st[1]));
1877 value = (double) endhostent();
1880 value = (double) endnetent();
1883 value = (double) endprotoent();
1886 value = (double) endservent();
1889 sp = do_select(gimme,arglast);
1892 if ((arg[1].arg_type & A_MASK) == A_WORD)
1893 stab = arg[1].arg_ptr.arg_stab;
1895 stab = stabent(str_get(st[1]),TRUE);
1896 if ((arg[2].arg_type & A_MASK) == A_WORD)
1897 stab2 = arg[2].arg_ptr.arg_stab;
1899 stab2 = stabent(str_get(st[2]),TRUE);
1901 value = (double)do_spair(stab,stab2,arglast);
1903 (void)do_spair(stab,stab2,arglast);
1907 if ((arg[1].arg_type & A_MASK) == A_WORD)
1908 stab = arg[1].arg_ptr.arg_stab;
1910 stab = stabent(str_get(st[1]),TRUE);
1912 value = (double)do_shutdown(stab,arglast);
1914 (void)do_shutdown(stab,arglast);
1919 if ((arg[1].arg_type & A_MASK) == A_WORD)
1920 stab = arg[1].arg_ptr.arg_stab;
1922 stab = stabent(str_get(st[1]),TRUE);
1923 sp = do_sopt(optype,stab,arglast);
1927 if ((arg[1].arg_type & A_MASK) == A_WORD)
1928 stab = arg[1].arg_ptr.arg_stab;
1930 stab = stabent(str_get(st[1]),TRUE);
1931 sp = do_getsockname(optype,stab,arglast);
1934 #else /* SOCKET not defined */
1968 fatal("Unsupported socket function");
1971 if ((arg[1].arg_type & A_MASK) == A_WORD)
1972 stab = arg[1].arg_ptr.arg_stab;
1974 stab = stabent(str_get(st[1]),TRUE);
1975 if (!stab || !(stio = stab_io(stab)) || !(fp = stio->ifp))
1980 sp = do_vec(str == st[1], arg->arg_ptr.arg_str, arglast);
1985 sp = do_gpwent(optype,
1989 value = (double) setpwent();
1992 value = (double) endpwent();
1997 sp = do_ggrent(optype,
2001 value = (double) setgrent();
2004 value = (double) endgrent();
2007 if (!(tmps = getlogin()))
2017 if ((arg[1].arg_type & A_MASK) == A_WORD)
2018 stab = arg[1].arg_ptr.arg_stab;
2020 stab = stabent(str_get(st[1]),TRUE);
2021 sp = do_dirop(optype,stab,gimme,arglast);
2024 value = (double)do_syscall(arglast);
2034 deb("%s RETURNS \"%s\"\n",opname[optype],str_get(str));
2037 return arglast[0] + 1;
2044 deb("%s RETURNS ARRAY OF %d ARGS\n",opname[optype],sp - arglast[0]);
2066 str_numset(str,value);
2073 deb("%s RETURNS \"%f\"\n",opname[optype],value);
2076 return arglast[0] + 1;