diff options
author | Larry Wall <larry@wall.org> | 1989-10-18 00:00:00 +0000 |
---|---|---|
committer | Larry Wall <larry@wall.org> | 1989-10-18 00:00:00 +0000 |
commit | a687059cbaf2c6fdccb5e0fae2aee80ec15625a8 (patch) | |
tree | 674c8533b7bd942204f23782934c72f8624dd308 /x2p | |
parent | 13281fa4f8547e0eb31d1986b865d9b7ec7d0dcc (diff) | |
download | perl-a687059cbaf2c6fdccb5e0fae2aee80ec15625a8.tar.gz |
perl 3.0: (no announcement message available)perl-3.000
A few of the new features: (18 Oct)
* Perl can now handle binary data correctly and has functions to pack and unpack binary structures into arrays or lists. You can now do arbitrary ioctl functions.
* You can now pass things to subroutines by reference.
* Debugger enhancements.
* An array or associative array may now appear in a local() list.
* Array values may now be interpolated into strings.
* Subroutine names are now distinguished by prefixing with &. You can call subroutines without using do, and without passing any argument list at all.
* You can use the new -u switch to cause perl to dump core so that you can run undump and produce a binary executable image. Alternately you can use the "dump" operator after initializing any variables and such.
* You can now chop lists.
* Perl now uses /bin/csh to do filename globbing, if available. This means that filenames with spaces or other strangenesses work right.
* New functions: mkdir and rmdir, getppid, getpgrp and setpgrp, getpriority and setpriority, chroot, ioctl and fcntl, flock, readlink, lstat, rindex, pack and unpack, read, warn, dbmopen and dbmclose, dump, reverse, defined, undef.
Diffstat (limited to 'x2p')
-rw-r--r-- | x2p/EXTERN.h | 11 | ||||
-rw-r--r-- | x2p/INTERN.h | 11 | ||||
-rw-r--r-- | x2p/Makefile.SH | 30 | ||||
-rw-r--r-- | x2p/a2p.h | 87 | ||||
-rw-r--r-- | x2p/a2p.man | 66 | ||||
-rw-r--r-- | x2p/a2p.y | 183 | ||||
-rw-r--r-- | x2p/a2py.c | 379 | ||||
-rw-r--r-- | x2p/handy.h | 11 | ||||
-rw-r--r-- | x2p/hash.c | 11 | ||||
-rw-r--r-- | x2p/hash.h | 11 | ||||
-rw-r--r-- | x2p/s2p.SH (renamed from x2p/s2p) | 190 | ||||
-rw-r--r-- | x2p/s2p.man | 5 | ||||
-rw-r--r-- | x2p/str.c | 11 | ||||
-rw-r--r-- | x2p/str.h | 11 | ||||
-rw-r--r-- | x2p/util.c | 18 | ||||
-rw-r--r-- | x2p/util.h | 11 | ||||
-rw-r--r-- | x2p/walk.c | 967 |
17 files changed, 1577 insertions, 436 deletions
diff --git a/x2p/EXTERN.h b/x2p/EXTERN.h index 66793aff0f..fc98380c94 100644 --- a/x2p/EXTERN.h +++ b/x2p/EXTERN.h @@ -1,8 +1,13 @@ -/* $Header: EXTERN.h,v 2.0 88/06/05 00:15:24 root Exp $ +/* $Header: EXTERN.h,v 3.0 89/10/18 15:33:37 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: EXTERN.h,v $ - * Revision 2.0 88/06/05 00:15:24 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:33:37 lwall + * 3.0 baseline * */ diff --git a/x2p/INTERN.h b/x2p/INTERN.h index 42e029d24f..d2a3033134 100644 --- a/x2p/INTERN.h +++ b/x2p/INTERN.h @@ -1,8 +1,13 @@ -/* $Header: INTERN.h,v 2.0 88/06/05 00:15:27 root Exp $ +/* $Header: INTERN.h,v 3.0 89/10/18 15:33:45 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: INTERN.h,v $ - * Revision 2.0 88/06/05 00:15:27 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:33:45 lwall + * 3.0 baseline * */ diff --git a/x2p/Makefile.SH b/x2p/Makefile.SH index a00ee89c79..c451965028 100644 --- a/x2p/Makefile.SH +++ b/x2p/Makefile.SH @@ -1,3 +1,6 @@ +case "$0" in +*/*) cd `expr X$0 : 'X\(.*\)/'` ;; +esac case $CONFIG in '') if test ! -f config.sh; then @@ -9,18 +12,24 @@ case $CONFIG in . ./config.sh ;; esac -case "$0" in -*/*) cd `expr X$0 : 'X\(.*\)/'` ;; -esac case "$mallocsrc" in '') ;; *) mallocsrc="../$mallocsrc";; esac echo "Extracting x2p/Makefile (with variable substitutions)" cat >Makefile <<!GROK!THIS! -# $Header: Makefile.SH,v 2.0 88/06/05 00:15:31 root Exp $ +# $Header: Makefile.SH,v 3.0 89/10/18 15:33:52 lwall Locked $ # # $Log: Makefile.SH,v $ +# Revision 3.0 89/10/18 15:33:52 lwall +# 3.0 baseline +# +# Revision 2.0.1.2 88/09/07 17:13:30 lwall +# patch14: added redirection of stderr to /dev/null +# +# Revision 2.0.1.1 88/07/11 23:13:39 root +# patch2: now expects more shift/reduce errors +# # Revision 2.0 88/06/05 00:15:31 root # Baseline version 2.0. # @@ -31,14 +40,14 @@ bin = $bin lib = $lib mansrc = $mansrc manext = $manext -CFLAGS = $ccflags -O +CFLAGS = $ccflags $optimize LDFLAGS = $ldflags SMALL = $small LARGE = $large $split mallocsrc = $mallocsrc mallocobj = $mallocobj -libs = $libnm -lm +libs = $libnm -lm $libs !GROK!THIS! cat >>Makefile <<'!NO!SUBS!' @@ -73,24 +82,21 @@ all: $(public) $(private) $(util) touch all a2p: $(obj) a2p.o - $(CC) $(LDFLAGS) $(LARGE) $(obj) a2p.o $(libs) -o a2p + $(CC) $(LARGE) $(LDFLAGS) $(obj) a2p.o $(libs) -o a2p a2p.c: a2p.y - @ echo Expect 103 shift/reduce errors... + @ echo Expect 208 shift/reduce conflicts... yacc a2p.y mv y.tab.c a2p.c a2p.o: a2p.c a2py.c a2p.h EXTERN.h util.h INTERN.h handy.h ../config.h $(CC) -c $(CFLAGS) $(LARGE) a2p.c -# if a .h file depends on another .h file... -$(h): - touch $@ install: a2p s2p # won't work with csh export PATH || exit 1 - mv $(bin)/a2p $(bin)/a2p.old 2>/dev/null - - mv $(bin)/s2p $(bin)/s2p.old + - mv $(bin)/s2p $(bin)/s2p.old 2>/dev/null - if test `pwd` != $(bin); then cp $(public) $(bin); fi cd $(bin); \ for pub in $(public); do \ @@ -1,8 +1,13 @@ -/* $Header: a2p.h,v 2.0 88/06/05 00:15:33 root Exp $ +/* $Header: a2p.h,v 3.0 89/10/18 15:34:14 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: a2p.h,v $ - * Revision 2.0 88/06/05 00:15:33 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:34:14 lwall + * 3.0 baseline * */ @@ -38,7 +43,7 @@ #define OCONCAT 19 #define OASSIGN 20 #define OADD 21 -#define OSUB 22 +#define OSUBTRACT 22 #define OMULT 23 #define ODIV 24 #define OMOD 25 @@ -86,6 +91,25 @@ #define OEXP 67 #define OSQRT 68 #define OINT 69 +#define ODO 70 +#define OPOW 71 +#define OSUB 72 +#define OGSUB 73 +#define OMATCH 74 +#define OUSERFUN 75 +#define OUSERDEF 76 +#define OCLOSE 77 +#define OATAN2 78 +#define OSIN 79 +#define OCOS 80 +#define ORAND 81 +#define OSRAND 82 +#define ODELETE 83 +#define OSYSTEM 84 +#define OCOND 85 +#define ORETURN 86 +#define ODEFINED 87 +#define OSTAR 88 #ifdef DOINIT char *opname[] = { @@ -111,7 +135,7 @@ char *opname[] = { "CONCAT", "ASSIGN", "ADD", - "SUB", + "SUBTRACT", "MULT", "DIV", "MOD", @@ -159,18 +183,38 @@ char *opname[] = { "EXP", "SQRT", "INT", - "70" + "DO", + "POW", + "SUB", + "GSUB", + "MATCH", + "USERFUN", + "USERDEF", + "CLOSE", + "ATAN2", + "SIN", + "COS", + "RAND", + "SRAND", + "DELETE", + "SYSTEM", + "COND", + "RETURN", + "DEFINED", + "STAR", + "89" }; #else extern char *opname[]; #endif +EXT int mop INIT(1); + +#define OPSMAX 50000 union { int ival; char *cval; -} ops[50000]; /* hope they have 200k to spare */ - -EXT int mop INIT(1); +} ops[OPSMAX]; /* hope they have 200k to spare */ #define DEBUGGING @@ -241,6 +285,7 @@ EXT bool lval_field INIT(FALSE); EXT bool do_chop INIT(FALSE); EXT bool need_entire INIT(FALSE); EXT bool absmaxfld INIT(FALSE); +EXT bool saw_altinput INIT(FALSE); EXT char const_FS INIT(0); EXT char *namelist INIT(Nullch); @@ -254,3 +299,27 @@ char *nameary[100]; EXT STR *opens; EXT HASH *symtab; +EXT HASH *curarghash; + +#define P_MIN 0 +#define P_LISTOP 5 +#define P_COMMA 10 +#define P_ASSIGN 15 +#define P_COND 20 +#define P_DOTDOT 25 +#define P_OROR 30 +#define P_ANDAND 35 +#define P_OR 40 +#define P_AND 45 +#define P_EQ 50 +#define P_REL 55 +#define P_UNI 60 +#define P_FILETEST 65 +#define P_SHIFT 70 +#define P_ADD 75 +#define P_MUL 80 +#define P_MATCH 85 +#define P_UNARY 90 +#define P_POW 95 +#define P_AUTO 100 +#define P_MAX 999 diff --git a/x2p/a2p.man b/x2p/a2p.man index 858ee53272..45d8ea93bb 100644 --- a/x2p/a2p.man +++ b/x2p/a2p.man @@ -1,7 +1,13 @@ .rn '' }` -''' $Header: a2p.man,v 2.0 88/06/05 00:15:36 root Exp $ +''' $Header: a2p.man,v 3.0 89/10/18 15:34:22 lwall Locked $ ''' ''' $Log: a2p.man,v $ +''' Revision 3.0 89/10/18 15:34:22 lwall +''' 3.0 baseline +''' +''' Revision 2.0.1.1 88/07/11 23:16:25 root +''' patch2: changes related to 1985 awk +''' ''' Revision 2.0 88/06/05 00:15:36 root ''' Baseline version 2.0. ''' @@ -74,7 +80,7 @@ might say: .sp a2p -7 -nlogin.password.uid.gid.gcos.shell.home .sp -Any delimiter will do to separate the field names. +Any delimiter can be used to separate the field names. .TP 5 .B \-<number> causes a2p to assume that input will always have that many fields. @@ -85,17 +91,6 @@ There are some areas where you may want to examine the perl script produced and tweak it some. Here are some of them, in no particular order. .PP -The split operator in perl always strips off all null fields from the end. -Awk does NOT do this, if you've set FS. -If the perl script splits to an array, the field count may not reflect -what you expect. -Ordinarily this isn't a problem, since nonexistent array elements have a null -value, but if you rely on NF in awk, you could be in for trouble. -Either force the number of fields with \-<number>, or count the number of -delimiters another way, e.g. with y/:/:/. -Or add something non-null to the end before you split, and then pop it off -the resulting array. -.PP There is an awk idiom of putting int() around a string expression to force numeric interpretation, even though the argument is always integer anyway. This is generally unneeded in perl, but a2p can't tell if the argument @@ -110,6 +105,8 @@ Instead it guesses which one you want. It's almost always right, but it can be spoofed. All such guesses are marked with the comment \*(L"#???\*(R". You should go through and check them. +You might want to run at least once with the \-w switch to perl, which +will warn you if you use == where you should have used eq. .PP Perl does not attempt to emulate the behavior of awk in which nonexistent array elements spring into existence simply by being referenced. @@ -133,20 +130,9 @@ Perl has two kinds of array, numerically-indexed and associative. Awk arrays are usually translated to associative arrays, but if you happen to know that the index is always going to be numeric you could change the {...} to [...]. -Iteration over an associative array is done with each(), but +Iteration over an associative array is done using the keys() function, but iteration over a numeric array is NOT. -You need a for loop, or while loop with a pop() or shift(), so you might -need to modify any loop that is iterating over the array in question. -.PP -Arrays which have been split into are assumed to be numerically indexed. -The usual perl idiom for iterating over such arrays is to use pop() or shift() -and assign the resulting value to a variable inside the conditional of the -while loop. -This is destructive to the array, however, so a2p can't assume this is -reasonable. -A2p will write a standard for loop with a scratch variable. -You may wish to change it to a pop() loop for more efficiency, presuming -you don't want to keep the array around. +You might need to modify any loop that is iterating over the array in question. .PP Awk starts by assuming OFMT has the value %.6g. Perl starts by assuming its equivalent, $#, to have the value %.20g. @@ -157,24 +143,40 @@ the awk script. There are times when you can move this down past some conditionals that test the entire record so that the split is not done as often. .PP -There may occasionally be extra parentheses that you can remove. -.PP For aesthetic reasons you may wish to change the array base $[ from 1 back -to the default of 0, but remember to change all array subscripts AND +to perl's default of 0, but remember to change all array subscripts AND all substr() and index() operations to match. .PP -Cute comments that say "# Here is a workaround because awk is dumb" are not -translated. +Cute comments that say "# Here is a workaround because awk is dumb" are passed +through unmodified. .PP Awk scripts are often embedded in a shell script that pipes stuff into and out of awk. Often the shell script wrapper can be incorporated into the perl script, since perl can start up pipes into and out of itself, and can do other things that awk can't do by itself. +.PP +Scripts that refer to the special variables RSTART and RLENGTH can often +be simplified by referring to the variables $`, $& and $', as long as they +are within the scope of the pattern match that sets them. +.PP +The produced perl script may have subroutines defined to deal with awk's +semantics regarding getline and print. +Since a2p usually picks correctness over efficiency. +it is almost always possible to rewrite such code to be more efficient by +discarding the semantic sugar. +.PP +For efficiency, you may wish to remove the keyword from any return statement +that is the last statement executed in a subroutine. +A2p catches the most common case, but doesn't analyze embedded blocks for +subtler cases. +.PP +ARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n]. +A loop that tries to iterate over ARGV[0] won't find it. .SH ENVIRONMENT A2p uses no environment variables. .SH AUTHOR -Larry Wall <lwall@devvax.Jpl.Nasa.Gov> +Larry Wall <lwall@jpl-devvax.Jpl.Nasa.Gov> .SH FILES .SH SEE ALSO perl The perl compiler/interpreter @@ -1,9 +1,14 @@ %{ -/* $Header: a2p.y,v 2.0 88/06/05 00:15:38 root Exp $ +/* $Header: a2p.y,v 3.0 89/10/18 15:34:29 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: a2p.y,v $ - * Revision 2.0 88/06/05 00:15:38 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:34:29 lwall + * 3.0 baseline * */ @@ -11,49 +16,53 @@ #include "a2p.h" int root; +int begins = Nullop; +int ends = Nullop; %} %token BEGIN END %token REGEX %token SEMINEW NEWLINE COMMENT -%token FUN1 GRGR +%token FUN1 FUNN GRGR %token PRINT PRINTF SPRINTF SPLIT %token IF ELSE WHILE FOR IN -%token EXIT NEXT BREAK CONTINUE +%token EXIT NEXT BREAK CONTINUE RET +%token GETLINE DO SUB GSUB MATCH +%token FUNCTION USERFUN DELETE %right ASGNOP +%right '?' ':' %left OROR %left ANDAND -%left NOT +%left IN %left NUMBER VAR SUBSTR INDEX -%left GETLINE -%nonassoc RELOP MATCHOP +%left MATCHOP +%left RELOP '<' '>' %left OR %left STRING %left '+' '-' %left '*' '/' '%' %right UMINUS +%left NOT +%right '^' %left INCR DECR %left FIELD VFIELD %% -program : junk begin hunks end - { root = oper4(OPROG,$1,$2,$3,$4); } +program : junk hunks + { root = oper4(OPROG,$1,begins,$2,ends); } ; begin : BEGIN '{' maybe states '}' junk - { $$ = oper3(OJUNK,$3,$4,$6); in_begin = FALSE; } - | /* NULL */ - { $$ = Nullop; } + { begins = oper4(OJUNK,begins,$3,$4,$6); in_begin = FALSE; + $$ = Nullop; } ; end : END '{' maybe states '}' - { $$ = oper2(OJUNK,$3,$4); } + { ends = oper3(OJUNK,ends,$3,$4); $$ = Nullop; } | end NEWLINE { $$ = $1; } - | /* NULL */ - { $$ = Nullop; } ; hunks : hunks hunk junk @@ -66,8 +75,16 @@ hunk : patpat { $$ = oper1(OHUNK,$1); need_entire = TRUE; } | patpat '{' maybe states '}' { $$ = oper2(OHUNK,$1,oper2(OJUNK,$3,$4)); } + | FUNCTION USERFUN '(' arg_list ')' maybe '{' maybe states '}' + { fixfargs($2,$4,0); $$ = oper5(OUSERDEF,$2,$4,$6,$8,$9); } | '{' maybe states '}' { $$ = oper2(OHUNK,Nullop,oper2(OJUNK,$2,$3)); } + | begin + | end + ; + +arg_list: expr_list + { $$ = rememberargs($$); } ; patpat : pat @@ -76,9 +93,7 @@ patpat : pat { $$ = oper2(ORANGE,$1,$3); } ; -pat : REGEX - { $$ = oper1(OREGEX,$1); } - | match +pat : match | rel | compound_pat ; @@ -86,10 +101,10 @@ pat : REGEX compound_pat : '(' compound_pat ')' { $$ = oper1(OPPAREN,$2); } - | pat ANDAND pat - { $$ = oper2(OPANDAND,$1,$3); } - | pat OROR pat - { $$ = oper2(OPOROR,$1,$3); } + | pat ANDAND maybe pat + { $$ = oper3(OPANDAND,$1,$3,$4); } + | pat OROR maybe pat + { $$ = oper3(OPOROR,$1,$3,$4); } | NOT pat { $$ = oper1(OPNOT,$2); } ; @@ -103,22 +118,30 @@ cond : expr compound_cond : '(' compound_cond ')' { $$ = oper1(OCPAREN,$2); } - | cond ANDAND cond - { $$ = oper2(OCANDAND,$1,$3); } - | cond OROR cond - { $$ = oper2(OCOROR,$1,$3); } + | cond ANDAND maybe cond + { $$ = oper3(OCANDAND,$1,$3,$4); } + | cond OROR maybe cond + { $$ = oper3(OCOROR,$1,$3,$4); } | NOT cond { $$ = oper1(OCNOT,$2); } ; rel : expr RELOP expr { $$ = oper3(ORELOP,$2,$1,$3); } + | expr '>' expr + { $$ = oper3(ORELOP,string(">",1),$1,$3); } + | expr '<' expr + { $$ = oper3(ORELOP,string("<",1),$1,$3); } | '(' rel ')' { $$ = oper1(ORPAREN,$2); } ; -match : expr MATCHOP REGEX +match : expr MATCHOP expr + { $$ = oper3(OMATCHOP,$2,$1,$3); } + | expr MATCHOP REGEX { $$ = oper3(OMATCHOP,$2,$1,oper1(OREGEX,$3)); } + | REGEX %prec MATCHOP + { $$ = oper1(OREGEX,$1); } | '(' match ')' { $$ = oper1(OMPAREN,$2); } ; @@ -138,16 +161,26 @@ expr : term term : variable { $$ = $1; } + | NUMBER + { $$ = oper1(ONUM,$1); } + | STRING + { $$ = oper1(OSTR,$1); } | term '+' term { $$ = oper2(OADD,$1,$3); } | term '-' term - { $$ = oper2(OSUB,$1,$3); } + { $$ = oper2(OSUBTRACT,$1,$3); } | term '*' term { $$ = oper2(OMULT,$1,$3); } | term '/' term { $$ = oper2(ODIV,$1,$3); } | term '%' term { $$ = oper2(OMOD,$1,$3); } + | term '^' term + { $$ = oper2(OPOW,$1,$3); } + | term IN VAR + { $$ = oper2(ODEFINED,aryrefarg($3),$1); } + | term '?' term ':' term + { $$ = oper2(OCOND,$1,$3,$5); } | variable INCR { $$ = oper1(OPOSTINCR,$1); } | variable DECR @@ -164,51 +197,85 @@ term : variable { $$ = oper1(OPAREN,$2); } | GETLINE { $$ = oper0(OGETLINE); } + | GETLINE VAR + { $$ = oper1(OGETLINE,$2); } + | GETLINE '<' expr + { $$ = oper3(OGETLINE,Nullop,string("<",1),$3); + if (ops[$3].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; } + | GETLINE VAR '<' expr + { $$ = oper3(OGETLINE,$2,string("<",1),$4); + if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; } + | term 'p' GETLINE + { $$ = oper3(OGETLINE,Nullop,string("|",1),$1); + if (ops[$1].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; } + | term 'p' GETLINE VAR + { $$ = oper3(OGETLINE,$4,string("|",1),$1); + if (ops[$1].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; } | FUN1 { $$ = oper0($1); need_entire = do_chop = TRUE; } | FUN1 '(' ')' { $$ = oper1($1,Nullop); need_entire = do_chop = TRUE; } | FUN1 '(' expr ')' { $$ = oper1($1,$3); } - | SPRINTF print_list + | FUNN '(' expr_list ')' + { $$ = oper1($1,$3); } + | USERFUN '(' expr_list ')' + { $$ = oper2(OUSERFUN,$1,$3); } + | SPRINTF expr_list { $$ = oper1(OSPRINTF,$2); } | SUBSTR '(' expr ',' expr ',' expr ')' { $$ = oper3(OSUBSTR,$3,$5,$7); } | SUBSTR '(' expr ',' expr ')' { $$ = oper2(OSUBSTR,$3,$5); } | SPLIT '(' expr ',' VAR ',' expr ')' - { $$ = oper3(OSPLIT,$3,numary($5),$7); } + { $$ = oper3(OSPLIT,$3,aryrefarg(numary($5)),$7); } | SPLIT '(' expr ',' VAR ')' - { $$ = oper2(OSPLIT,$3,numary($5)); } + { $$ = oper2(OSPLIT,$3,aryrefarg(numary($5))); } | INDEX '(' expr ',' expr ')' { $$ = oper2(OINDEX,$3,$5); } + | MATCH '(' expr ',' REGEX ')' + { $$ = oper2(OMATCH,$3,oper1(OREGEX,$5)); } + | MATCH '(' expr ',' expr ')' + { $$ = oper2(OMATCH,$3,$5); } + | SUB '(' expr ',' expr ')' + { $$ = oper2(OSUB,$3,$5); } + | SUB '(' REGEX ',' expr ')' + { $$ = oper2(OSUB,oper1(OREGEX,$3),$5); } + | GSUB '(' expr ',' expr ')' + { $$ = oper2(OGSUB,$3,$5); } + | GSUB '(' REGEX ',' expr ')' + { $$ = oper2(OGSUB,oper1(OREGEX,$3),$5); } + | SUB '(' expr ',' expr ',' expr ')' + { $$ = oper3(OSUB,$3,$5,$7); } + | SUB '(' REGEX ',' expr ',' expr ')' + { $$ = oper3(OSUB,oper1(OREGEX,$3),$5,$7); } + | GSUB '(' expr ',' expr ',' expr ')' + { $$ = oper3(OGSUB,$3,$5,$7); } + | GSUB '(' REGEX ',' expr ',' expr ')' + { $$ = oper3(OGSUB,oper1(OREGEX,$3),$5,$7); } ; -variable: NUMBER - { $$ = oper1(ONUM,$1); } - | STRING - { $$ = oper1(OSTR,$1); } - | VAR +variable: VAR { $$ = oper1(OVAR,$1); } - | VAR '[' expr ']' - { $$ = oper2(OVAR,$1,$3); } + | VAR '[' expr_list ']' + { $$ = oper2(OVAR,aryrefarg($1),$3); } | FIELD { $$ = oper1(OFLD,$1); } | VFIELD term { $$ = oper1(OVFLD,$2); } ; -print_list +expr_list : expr | clist | /* NULL */ { $$ = Nullop; } ; -clist : expr ',' expr - { $$ = oper2(OCOMMA,$1,$3); } - | clist ',' expr - { $$ = oper2(OCOMMA,$1,$3); } +clist : expr ',' maybe expr + { $$ = oper3(OCOMMA,$1,$3,$4); } + | clist ',' maybe expr + { $$ = oper3(OCOMMA,$1,$3,$4); } | '(' clist ')' /* these parens are invisible */ { $$ = $2; } ; @@ -220,9 +287,9 @@ junk : junk hunksep ; hunksep : ';' - { $$ = oper0(OSEMICOLON); } + { $$ = oper2(OJUNK,oper0(OSEMICOLON),oper0(ONEWLINE)); } | SEMINEW - { $$ = oper0(OSEMICOLON); } + { $$ = oper2(OJUNK,oper0(OSEMICOLON),oper0(ONEWLINE)); } | NEWLINE { $$ = oper0(ONEWLINE); } | COMMENT @@ -275,23 +342,23 @@ simpnull: simple simple : expr - | PRINT print_list redir expr + | PRINT expr_list redir expr { $$ = oper3(OPRINT,$2,$3,$4); do_opens = TRUE; saw_ORS = saw_OFS = TRUE; if (!$2) need_entire = TRUE; if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; } - | PRINT print_list + | PRINT expr_list { $$ = oper1(OPRINT,$2); if (!$2) need_entire = TRUE; saw_ORS = saw_OFS = TRUE; } - | PRINTF print_list redir expr + | PRINTF expr_list redir expr { $$ = oper3(OPRINTF,$2,$3,$4); do_opens = TRUE; if (!$2) need_entire = TRUE; if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; } - | PRINTF print_list + | PRINTF expr_list { $$ = oper1(OPRINTF,$2); if (!$2) need_entire = TRUE; } @@ -305,10 +372,16 @@ simple { $$ = oper1(OEXIT,$2); } | CONTINUE { $$ = oper0(OCONTINUE); } + | RET + { $$ = oper0(ORETURN); } + | RET expr + { $$ = oper1(ORETURN,$2); } + | DELETE VAR '[' expr ']' + { $$ = oper2(ODELETE,aryrefarg($2),$4); } ; -redir : RELOP - { $$ = oper1(OREDIR,string(">",1)); } +redir : '>' %prec FIELD + { $$ = oper1(OREDIR,$1); } | GRGR { $$ = oper1(OREDIR,string(">>",2)); } | '|' @@ -322,12 +395,14 @@ compound { $$ = oper3(OIF,$3,bl($6,$5),bl($9,$8)); } | WHILE '(' cond ')' maybe statement { $$ = oper2(OWHILE,$3,bl($6,$5)); } + | DO maybe statement WHILE '(' cond ')' + { $$ = oper2(ODO,bl($3,$2),$6); } | FOR '(' simpnull ';' cond ';' simpnull ')' maybe statement { $$ = oper4(OFOR,$3,$5,$7,bl($10,$9)); } | FOR '(' simpnull ';' ';' simpnull ')' maybe statement { $$ = oper4(OFOR,$3,string("",0),$6,bl($9,$8)); } - | FOR '(' VAR IN VAR ')' maybe statement - { $$ = oper3(OFORIN,$3,$5,bl($8,$7)); } + | FOR '(' expr ')' maybe statement + { $$ = oper2(OFORIN,$3,bl($6,$5)); } | '{' maybe states '}' maybe { $$ = oper3(OBLOCK,oper2(OJUNK,$2,$3),Nullop,$5); } ; diff --git a/x2p/a2py.c b/x2p/a2py.c index 3adbd65fd3..e17c542a4a 100644 --- a/x2p/a2py.c +++ b/x2p/a2py.c @@ -1,8 +1,13 @@ -/* $Header: a2py.c,v 2.0 88/06/05 00:15:41 root Exp $ +/* $Header: a2py.c,v 3.0 89/10/18 15:34:35 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: a2py.c,v $ - * Revision 2.0 88/06/05 00:15:41 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:34:35 lwall + * 3.0 baseline * */ @@ -12,6 +17,7 @@ char *index(); char *filename; int checkers = 0; +STR *walk(); main(argc,argv,env) register int argc; @@ -21,7 +27,6 @@ register char **env; register STR *str; register char *s; int i; - STR *walk(); STR *tmpstr; linestr = str_new(80); @@ -79,6 +84,7 @@ register char **env; bufptr = str_get(linestr); symtab = hnew(); + curarghash = hnew(); /* now parse the report spec */ @@ -114,8 +120,12 @@ register char **env; /* second pass to produce new program */ - tmpstr = walk(0,0,root,&i); - str = str_make("#!/usr/bin/perl\neval \"exec /usr/bin/perl -S $0 $*\"\n\ + tmpstr = walk(0,0,root,&i,P_MIN); + str = str_make("#!"); + str_cat(str, BIN); + str_cat(str, "/perl\neval \"exec "); + str_cat(str, BIN); + str_cat(str, "/perl -S $0 $*\"\n\ if $running_under_some_shell;\n\ # this emulates #! processing on NIH machines.\n\ # (remove #! line above if indigestible)\n\n"); @@ -148,7 +158,9 @@ register char **env; #define RETURN(retval) return (bufptr = s,retval) #define XTERM(retval) return (expectterm = TRUE,bufptr = s,retval) #define XOP(retval) return (expectterm = FALSE,bufptr = s,retval) -#define ID(x) return (yylval=string(x,0),expectterm = FALSE,bufptr = s,VAR) +#define ID(x) return (yylval=string(x,0),expectterm = FALSE,bufptr = s,idtype) + +int idtype; yylex() { @@ -203,10 +215,14 @@ yylex() } XTERM(tmp); case '(': + tmp = *s++; + XTERM(tmp); case '{': case '[': case ')': case ']': + case '?': + case ':': tmp = *s++; XOP(tmp); case 127: @@ -237,9 +253,13 @@ yylex() /* FALL THROUGH */ case '*': case '%': + case '^': tmp = *s++; if (*s == '=') { - yylval = string(s-1,2); + if (tmp == '^') + yylval = string("**=",3); + else + yylval = string(s-1,2); s++; XTERM(ASGNOP); } @@ -257,7 +277,12 @@ yylex() if (tmp == '|') XTERM(OROR); s--; - XTERM('|'); + while (*s == ' ' || *s == '\t') + s++; + if (strnEQ(s,"getline",7)) + XTERM('p'); + else + XTERM('|'); case '=': s++; tmp = *s++; @@ -289,8 +314,7 @@ yylex() XTERM(RELOP); } s--; - yylval = string("<",1); - XTERM(RELOP); + XTERM('<'); case '>': s++; tmp = *s++; @@ -303,15 +327,18 @@ yylex() XTERM(RELOP); } s--; - yylval = string(">",1); - XTERM(RELOP); + XTERM('>'); #define SNARFWORD \ d = tokenbuf; \ while (isalpha(*s) || isdigit(*s) || *s == '_') \ *d++ = *s++; \ *d = '\0'; \ - d = tokenbuf; + d = tokenbuf; \ + if (*s == '(') \ + idtype = USERFUN; \ + else \ + idtype = VAR; case '$': s++; @@ -319,6 +346,7 @@ yylex() s++; do_chop = TRUE; need_entire = TRUE; + idtype = VAR; ID("0"); } do_split = TRUE; @@ -347,7 +375,7 @@ yylex() XTERM(tmp); case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': + case '5': case '6': case '7': case '8': case '9': case '.': s = scannum(s); XOP(NUMBER); case '"': @@ -361,6 +389,16 @@ yylex() case 'a': case 'A': SNARFWORD; + if (strEQ(d,"ARGC")) + set_array_base = TRUE; + if (strEQ(d,"ARGV")) { + yylval=numary(string("ARGV",0)); + XOP(VAR); + } + if (strEQ(d,"atan2")) { + yylval = OATAN2; + XTERM(FUNN); + } ID(d); case 'b': case 'B': SNARFWORD; @@ -373,9 +411,34 @@ yylex() SNARFWORD; if (strEQ(d,"continue")) XTERM(CONTINUE); + if (strEQ(d,"cos")) { + yylval = OCOS; + XTERM(FUN1); + } + if (strEQ(d,"close")) { + do_fancy_opens = 1; + yylval = OCLOSE; + XTERM(FUN1); + } + if (strEQ(d,"chdir")) + *d = toupper(*d); + else if (strEQ(d,"crypt")) + *d = toupper(*d); + else if (strEQ(d,"chop")) + *d = toupper(*d); + else if (strEQ(d,"chmod")) + *d = toupper(*d); + else if (strEQ(d,"chown")) + *d = toupper(*d); ID(d); case 'd': case 'D': SNARFWORD; + if (strEQ(d,"do")) + XTERM(DO); + if (strEQ(d,"delete")) + XTERM(DELETE); + if (strEQ(d,"die")) + *d = toupper(*d); ID(d); case 'e': case 'E': SNARFWORD; @@ -391,6 +454,18 @@ yylex() yylval = OEXP; XTERM(FUN1); } + if (strEQ(d,"elsif")) + *d = toupper(*d); + else if (strEQ(d,"eq")) + *d = toupper(*d); + else if (strEQ(d,"eval")) + *d = toupper(*d); + else if (strEQ(d,"eof")) + *d = toupper(*d); + else if (strEQ(d,"each")) + *d = toupper(*d); + else if (strEQ(d,"exec")) + *d = toupper(*d); ID(d); case 'f': case 'F': SNARFWORD; @@ -406,18 +481,40 @@ yylex() } ID(tokenbuf); } - if (strEQ(d,"FILENAME")) - d = "ARGV"; if (strEQ(d,"for")) XTERM(FOR); + else if (strEQ(d,"function")) + XTERM(FUNCTION); + if (strEQ(d,"FILENAME")) + d = "ARGV"; + if (strEQ(d,"foreach")) + *d = toupper(*d); + else if (strEQ(d,"format")) + *d = toupper(*d); + else if (strEQ(d,"fork")) + *d = toupper(*d); + else if (strEQ(d,"fh")) + *d = toupper(*d); ID(d); case 'g': case 'G': SNARFWORD; if (strEQ(d,"getline")) XTERM(GETLINE); + if (strEQ(d,"gsub")) + XTERM(GSUB); + if (strEQ(d,"ge")) + *d = toupper(*d); + else if (strEQ(d,"gt")) + *d = toupper(*d); + else if (strEQ(d,"goto")) + *d = toupper(*d); + else if (strEQ(d,"gmtime")) + *d = toupper(*d); ID(d); case 'h': case 'H': SNARFWORD; + if (strEQ(d,"hex")) + *d = toupper(*d); ID(d); case 'i': case 'I': SNARFWORD; @@ -436,9 +533,15 @@ yylex() ID(d); case 'j': case 'J': SNARFWORD; + if (strEQ(d,"join")) + *d = toupper(*d); ID(d); case 'k': case 'K': SNARFWORD; + if (strEQ(d,"keys")) + *d = toupper(*d); + else if (strEQ(d,"kill")) + *d = toupper(*d); ID(d); case 'l': case 'L': SNARFWORD; @@ -450,9 +553,27 @@ yylex() yylval = OLOG; XTERM(FUN1); } + if (strEQ(d,"last")) + *d = toupper(*d); + else if (strEQ(d,"local")) + *d = toupper(*d); + else if (strEQ(d,"lt")) + *d = toupper(*d); + else if (strEQ(d,"le")) + *d = toupper(*d); + else if (strEQ(d,"locatime")) + *d = toupper(*d); + else if (strEQ(d,"link")) + *d = toupper(*d); ID(d); case 'm': case 'M': SNARFWORD; + if (strEQ(d,"match")) { + set_array_base = TRUE; + XTERM(MATCH); + } + if (strEQ(d,"m")) + *d = toupper(*d); ID(d); case 'n': case 'N': SNARFWORD; @@ -462,20 +583,28 @@ yylex() saw_line_op = TRUE; XTERM(NEXT); } + if (strEQ(d,"ne")) + *d = toupper(*d); ID(d); case 'o': case 'O': SNARFWORD; if (strEQ(d,"ORS")) { saw_ORS = TRUE; - d = "$\\"; + d = "\\"; } if (strEQ(d,"OFS")) { saw_OFS = TRUE; - d = "$,"; + d = ","; } if (strEQ(d,"OFMT")) { - d = "$#"; + d = "#"; } + if (strEQ(d,"open")) + *d = toupper(*d); + else if (strEQ(d,"ord")) + *d = toupper(*d); + else if (strEQ(d,"oct")) + *d = toupper(*d); ID(d); case 'p': case 'P': SNARFWORD; @@ -485,6 +614,10 @@ yylex() if (strEQ(d,"printf")) { XTERM(PRINTF); } + if (strEQ(d,"push")) + *d = toupper(*d); + else if (strEQ(d,"pop")) + *d = toupper(*d); ID(d); case 'q': case 'Q': SNARFWORD; @@ -492,9 +625,21 @@ yylex() case 'r': case 'R': SNARFWORD; if (strEQ(d,"RS")) { - d = "$/"; + d = "/"; saw_RS = TRUE; } + if (strEQ(d,"rand")) { + yylval = ORAND; + XTERM(FUN1); + } + if (strEQ(d,"return")) + XTERM(RET); + if (strEQ(d,"reset")) + *d = toupper(*d); + else if (strEQ(d,"redo")) + *d = toupper(*d); + else if (strEQ(d,"rename")) + *d = toupper(*d); ID(d); case 's': case 'S': SNARFWORD; @@ -506,32 +651,97 @@ yylex() set_array_base = TRUE; XTERM(SUBSTR); } + if (strEQ(d,"sub")) + XTERM(SUB); if (strEQ(d,"sprintf")) XTERM(SPRINTF); if (strEQ(d,"sqrt")) { yylval = OSQRT; XTERM(FUN1); } + if (strEQ(d,"SUBSEP")) { + d = ";"; + } + if (strEQ(d,"sin")) { + yylval = OSIN; + XTERM(FUN1); + } + if (strEQ(d,"srand")) { + yylval = OSRAND; + XTERM(FUN1); + } + if (strEQ(d,"system")) { + yylval = OSYSTEM; + XTERM(FUN1); + } + if (strEQ(d,"s")) + *d = toupper(*d); + else if (strEQ(d,"shift")) + *d = toupper(*d); + else if (strEQ(d,"select")) + *d = toupper(*d); + else if (strEQ(d,"seek")) + *d = toupper(*d); + else if (strEQ(d,"stat")) + *d = toupper(*d); + else if (strEQ(d,"study")) + *d = toupper(*d); + else if (strEQ(d,"sleep")) + *d = toupper(*d); + else if (strEQ(d,"symlink")) + *d = toupper(*d); + else if (strEQ(d,"sort")) + *d = toupper(*d); ID(d); case 't': case 'T': SNARFWORD; + if (strEQ(d,"tr")) + *d = toupper(*d); + else if (strEQ(d,"tell")) + *d = toupper(*d); + else if (strEQ(d,"time")) + *d = toupper(*d); + else if (strEQ(d,"times")) + *d = toupper(*d); ID(d); case 'u': case 'U': SNARFWORD; + if (strEQ(d,"until")) + *d = toupper(*d); + else if (strEQ(d,"unless")) + *d = toupper(*d); + else if (strEQ(d,"umask")) + *d = toupper(*d); + else if (strEQ(d,"unshift")) + *d = toupper(*d); + else if (strEQ(d,"unlink")) + *d = toupper(*d); + else if (strEQ(d,"utime")) + *d = toupper(*d); ID(d); case 'v': case 'V': SNARFWORD; + if (strEQ(d,"values")) + *d = toupper(*d); ID(d); case 'w': case 'W': SNARFWORD; if (strEQ(d,"while")) XTERM(WHILE); + if (strEQ(d,"write")) + *d = toupper(*d); + else if (strEQ(d,"wait")) + *d = toupper(*d); ID(d); case 'x': case 'X': SNARFWORD; + if (strEQ(d,"x")) + *d = toupper(*d); ID(d); case 'y': case 'Y': SNARFWORD; + if (strEQ(d,"y")) + *d = toupper(*d); ID(d); case 'z': case 'Z': SNARFWORD; @@ -634,6 +844,8 @@ char *ptr; ops[mop].cval = safemalloc(len+1); strncpy(ops[mop].cval,ptr,len); ops[mop++].cval[len] = '\0'; + if (mop >= OPSMAX) + fatal("Recompile a2p with larger OPSMAX\n"); return retval; } @@ -645,6 +857,8 @@ int type; if (type > 255) fatal("type > 255 (%d)\n",type); ops[mop++].ival = type; + if (mop >= OPSMAX) + fatal("Recompile a2p with larger OPSMAX\n"); return retval; } @@ -658,6 +872,8 @@ int arg1; fatal("type > 255 (%d)\n",type); ops[mop++].ival = type + (1<<8); ops[mop++].ival = arg1; + if (mop >= OPSMAX) + fatal("Recompile a2p with larger OPSMAX\n"); return retval; } @@ -673,6 +889,8 @@ int arg2; ops[mop++].ival = type + (2<<8); ops[mop++].ival = arg1; ops[mop++].ival = arg2; + if (mop >= OPSMAX) + fatal("Recompile a2p with larger OPSMAX\n"); return retval; } @@ -690,6 +908,8 @@ int arg3; ops[mop++].ival = arg1; ops[mop++].ival = arg2; ops[mop++].ival = arg3; + if (mop >= OPSMAX) + fatal("Recompile a2p with larger OPSMAX\n"); return retval; } @@ -709,6 +929,8 @@ int arg4; ops[mop++].ival = arg2; ops[mop++].ival = arg3; ops[mop++].ival = arg4; + if (mop >= OPSMAX) + fatal("Recompile a2p with larger OPSMAX\n"); return retval; } @@ -730,6 +952,8 @@ int arg5; ops[mop++].ival = arg3; ops[mop++].ival = arg4; ops[mop++].ival = arg5; + if (mop >= OPSMAX) + fatal("Recompile a2p with larger OPSMAX\n"); return retval; } @@ -902,10 +1126,121 @@ int arg; STR *key; int dummy; - key = walk(0,0,arg,&dummy); + key = walk(0,0,arg,&dummy,P_MIN); str_cat(key,"[]"); hstore(symtab,key->str_ptr,str_make("1")); str_free(key); set_array_base = TRUE; return arg; } + +rememberargs(arg) +int arg; +{ + int type; + STR *str; + + if (!arg) + return arg; + type = ops[arg].ival & 255; + if (type == OCOMMA) { + rememberargs(ops[arg+1].ival); + rememberargs(ops[arg+3].ival); + } + else if (type == OVAR) { + str = str_new(0); + hstore(curarghash,ops[ops[arg+1].ival+1].cval,str); + } + else + fatal("panic: unknown argument type %d, line %d\n",type,line); + return arg; +} + +aryrefarg(arg) +int arg; +{ + int type = ops[arg].ival & 255; + STR *str; + + if (type != OSTRING) + fatal("panic: aryrefarg %d, line %d\n",type,line); + str = hfetch(curarghash,ops[arg+1].cval); + if (str) + str_set(str,"*"); + return arg; +} + +fixfargs(name,arg,prevargs) +int name; +int arg; +int prevargs; +{ + int type; + STR *str; + int numargs; + + if (!arg) + return prevargs; + type = ops[arg].ival & 255; + if (type == OCOMMA) { + numargs = fixfargs(name,ops[arg+1].ival,prevargs); + numargs = fixfargs(name,ops[arg+3].ival,numargs); + } + else if (type == OVAR) { + str = hfetch(curarghash,ops[ops[arg+1].ival+1].cval); + if (strEQ(str_get(str),"*")) { + char tmpbuf[128]; + + str_set(str,""); /* in case another routine has this */ + ops[arg].ival &= ~255; + ops[arg].ival |= OSTAR; + sprintf(tmpbuf,"%s:%d",ops[name+1].cval,prevargs); + fprintf(stderr,"Adding %s\n",tmpbuf); + str = str_new(0); + str_set(str,"*"); + hstore(curarghash,tmpbuf,str); + } + numargs = prevargs + 1; + } + else + fatal("panic: unknown argument type %d, arg %d, line %d\n", + type,numargs+1,line); + return numargs; +} + +fixrargs(name,arg,prevargs) +char *name; +int arg; +int prevargs; +{ + int type; + STR *str; + int numargs; + + if (!arg) + return prevargs; + type = ops[arg].ival & 255; + if (type == OCOMMA) { + numargs = fixrargs(name,ops[arg+1].ival,prevargs); + numargs = fixrargs(name,ops[arg+3].ival,numargs); + } + else { + char tmpbuf[128]; + + sprintf(tmpbuf,"%s:%d",name,prevargs); + str = hfetch(curarghash,tmpbuf); + fprintf(stderr,"Looking for %s\n",tmpbuf); + if (str && strEQ(str->str_ptr,"*")) { + if (type == OVAR || type == OSTAR) { + ops[arg].ival &= ~255; + ops[arg].ival |= OSTAR; + } + else + fatal("Can't pass expression by reference as arg %d of %s\n", + prevargs+1, name); + } + numargs = prevargs + 1; + } + return numargs; +} + diff --git a/x2p/handy.h b/x2p/handy.h index bc0d0d590d..80a9afbbdf 100644 --- a/x2p/handy.h +++ b/x2p/handy.h @@ -1,8 +1,13 @@ -/* $Header: handy.h,v 2.0 88/06/05 00:15:47 root Exp $ +/* $Header: handy.h,v 3.0 89/10/18 15:34:44 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: handy.h,v $ - * Revision 2.0 88/06/05 00:15:47 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:34:44 lwall + * 3.0 baseline * */ diff --git a/x2p/hash.c b/x2p/hash.c index 38932efc49..a89b6511e4 100644 --- a/x2p/hash.c +++ b/x2p/hash.c @@ -1,8 +1,13 @@ -/* $Header: hash.c,v 2.0 88/06/05 00:15:50 root Exp $ +/* $Header: hash.c,v 3.0 89/10/18 15:34:50 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: hash.c,v $ - * Revision 2.0 88/06/05 00:15:50 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:34:50 lwall + * 3.0 baseline * */ diff --git a/x2p/hash.h b/x2p/hash.h index 8d9029515a..1a67ae8b73 100644 --- a/x2p/hash.h +++ b/x2p/hash.h @@ -1,8 +1,13 @@ -/* $Header: hash.h,v 2.0 88/06/05 00:15:52 root Exp $ +/* $Header: hash.h,v 3.0 89/10/18 15:34:57 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: hash.h,v $ - * Revision 2.0 88/06/05 00:15:52 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:34:57 lwall + * 3.0 baseline * */ @@ -1,8 +1,42 @@ -#!/usr/bin/perl - -# $Header: s2p,v 2.0 88/06/05 00:15:55 root Exp $ +: This forces SH files to create target in same directory as SH file. +: This is so that make depend always knows where to find SH derivatives. +case "$0" in +*/*) cd `expr X$0 : 'X\(.*\)/'` ;; +esac +case $CONFIG in +'') + if test ! -f config.sh; then + ln ../config.sh . || \ + ln ../../config.sh . || \ + ln ../../../config.sh . || \ + (echo "Can't find config.sh."; exit 1) + fi + . config.sh + ;; +esac +echo "Extracting s2p (with variable substitutions)" +: This section of the file will have variable substitutions done on it. +: Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!. +: Protect any dollar signs and backticks that you do not want interpreted +: by putting a backslash in front. You may delete these comments. +$spitshell >s2p <<!GROK!THIS! +#!$bin/perl + +\$bin = '$bin'; +!GROK!THIS! + +: In the following dollars and backticks do not need the extra backslash. +$spitshell >>s2p <<'!NO!SUBS!' + +# $Header: s2p.SH,v 3.0 89/10/18 15:35:02 lwall Locked $ # -# $Log: s2p,v $ +# $Log: s2p.SH,v $ +# Revision 3.0 89/10/18 15:35:02 lwall +# 3.0 baseline +# +# Revision 2.0.1.1 88/07/11 23:26:23 root +# patch2: s2p didn't put a proper prologue on output script +# # Revision 2.0 88/06/05 00:15:55 root # Baseline version 2.0. # @@ -89,25 +123,7 @@ line: while (<>) { $addr1 = 'eof()'; } elsif (s|^/||) { - $addr1 = '/'; - delim: while (s:^([^(|)\\/]*)([(|)\\/])::) { - $prefix = $1; - $delim = $2; - if ($delim eq '\\') { - s/(.)(.*)/$2/; - $ch = $1; - $delim = '' if index("(|)",$ch) >= 0; - $delim .= $1; - } - elsif ($delim ne '/') { - $delim = '\\' . $delim; - } - $addr1 .= $prefix; - $addr1 .= $delim; - if ($delim eq '/') { - last delim; - } - } + $addr1 = do fetchpat('/'); } if (s/^,//) { if (s/^([0-9]+)//) { @@ -115,25 +131,7 @@ line: while (<>) { } elsif (s/^\$//) { $addr2 = "eof()"; } elsif (s|^/||) { - $addr2 = '/'; - delim: while (s:^([^(|)\\/]*)([(|)\\/])::) { - $prefix = $1; - $delim = $2; - if ($delim eq '\\') { - s/(.)(.*)/$2/; - $ch = $1; - $delim = '' if index("(|)",$ch) >= 0; - $delim .= $1; - } - elsif ($delim ne '/') { - $delim = '\\' . $delim; - } - $addr2 .= $prefix; - $addr2 .= $delim; - if ($delim eq '/') { - last delim; - } - } + $addr2 = do fetchpat('/'); } else { do Die("Invalid second address at line $.\n"); } @@ -160,7 +158,7 @@ line: while (<>) { } else { $rmaybe = "\n$r"; if ($addr2 || $addr1) { - $space = substr(' ',0,$shiftwidth); + $space = ' ' x $shiftwidth; } else { $space = ''; } @@ -184,8 +182,7 @@ line: while (<>) { while ($#lines >= 0) { $_ = shift(lines); unless (s/^ *<<--//) { - print body substr("\t\t\t\t\t\t\t\t\t\t\t\t",0,$indent / 8), - substr(' ',0,$indent % 8); + print body "\t" x ($indent / 8), ' ' x ($indent % 8); } print body $_, "\n"; } @@ -243,7 +240,11 @@ unless ($debug) { } close head; - print "#!/bin/perl\n\n"; + print "#!$bin/perl +eval \"exec $bin/perl -S \$0 \$*\" + if \$running_under_some_shell; + +"; open(body,"cc -E /tmp/sperl2$$.c |") || do Die("Can't reopen temp file"); while (<body>) { @@ -281,10 +282,10 @@ sub make_label { $label =~ s/[^a-zA-Z0-9]/_/g; if ($label =~ /^[0-9_]/) { $label = 'L' . $label; } $label = substr($label,0,8); - if ($label =~ /^([a-z])([a-z]*)$/) { + if ($label =~ /^([a-z])([a-z]*)$/) { # could be reserved word $first = $1; $rest = $2; - $first =~ y/a-z/A-Z/; + $first =~ y/a-z/A-Z/; # so capitalize it $label = $first . $rest; } $label; @@ -377,9 +378,25 @@ ${space}next line;"; $delim = substr($_,1,1); $len = length($_); $repl = $end = 0; + $inbracket = 0; for ($i = 2; $i < $len; $i++) { $c = substr($_,$i,1); - if ($c eq '\\') { + if ($c eq $delim) { + if ($inbracket) { + $_ = substr($_,0,$i) . '\\' . substr($_,$i,10000); + $i++; + $len++; + } + else { + if ($repl) { + $end = $i; + last; + } else { + $repl = $i; + } + } + } + elsif ($c eq '\\') { $i++; if ($i >= $len) { $_ .= 'n'; @@ -387,21 +404,21 @@ ${space}next line;"; $len = length($_); $_ = substr($_,0,--$len); } - elsif (!$repl && index("(|)",substr($_,$i,1)) >= 0) { + elsif (!$repl && substr($_,$i,1) =~ /^[(){}\w]$/) { $i--; $len--; $_ = substr($_,0,$i) . substr($_,$i+1,10000); } } - elsif ($c eq $delim) { - if ($repl) { - $end = $i; - last; - } else { - $repl = $i; - } + elsif ($c eq '[' && !$repl) { + $i++ if substr($_,$i,1) eq '^'; + $i++ if substr($_,$i,1) eq ']'; + $inbracket = 1; } - elsif (!$repl && index("(|)",$c) >= 0) { + elsif ($c eq ']') { + $inbracket = 0; + } + elsif (!$repl && index("()",$c) >= 0) { $_ = substr($_,0,$i) . '\\' . substr($_,$i,10000); $i++; $len++; @@ -428,7 +445,12 @@ ${space}next line;"; } do Die("Unrecognized substitution command ($end) at line $.\n"); } - $_ = $subst . $cmd . ';'; + $_ = +"<<--#ifdef TSEEN +$subst && \$tflag++$cmd; +<<--#else +$subst$cmd; +<<--#endif"; next; } @@ -453,17 +475,14 @@ ${space}next line;"; } if (/^P/) { - $_ = -'if (/(^[^\n]*\n)/) { - print $1; -}'; + $_ = 'print $1 if /(^.*\n)/;'; next; } if (/^D/) { $_ = -'s/^[^\n]*\n//; -if ($_) {redo line;} +'s/^.*\n//; +redo line if $_; next line;'; next; } @@ -558,3 +577,44 @@ next line;'; $_; } +sub fetchpat { + local($outer) = @_; + local($addr) = $outer; + local($inbracket); + local($prefix,$delim,$ch); + + delim: while (s:^([^\](|)[\\/]*)([](|)[\\/])::) { + $prefix = $1; + $delim = $2; + print "$prefix\t$delim\t$_\n"; + if ($delim eq '\\') { + s/(.)//; + $ch = $1; + $delim = '' if $ch =~ /^[(){}\w]$/; + $delim .= $1; + } + elsif ($delim eq '[') { + $inbracket = 1; + s/^\^// && ($delim .= '^'); + s/^]// && ($delim .= ']'); + print "$prefix\t$delim\t$_\n"; + } + elsif ($delim eq ']') { + $inbracket = 0; + } + elsif ($inbracket || $delim ne $outer) { + print "Adding\n"; + $delim = '\\' . $delim; + } + $addr .= $prefix; + $addr .= $delim; + if ($delim eq $outer && !$inbracket) { + last delim; + } + } + $addr; +} + +!NO!SUBS! +chmod 755 s2p +$eunicefix s2p diff --git a/x2p/s2p.man b/x2p/s2p.man index a9d00aa93c..be5ef6130c 100644 --- a/x2p/s2p.man +++ b/x2p/s2p.man @@ -1,7 +1,10 @@ .rn '' }` -''' $Header: s2p.man,v 2.0 88/06/05 00:15:59 root Exp $ +''' $Header: s2p.man,v 3.0 89/10/18 15:35:09 lwall Locked $ ''' ''' $Log: s2p.man,v $ +''' Revision 3.0 89/10/18 15:35:09 lwall +''' 3.0 baseline +''' ''' Revision 2.0 88/06/05 00:15:59 root ''' Baseline version 2.0. ''' @@ -1,8 +1,13 @@ -/* $Header: str.c,v 2.0 88/06/05 00:16:02 root Exp $ +/* $Header: str.c,v 3.0 89/10/18 15:35:18 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: str.c,v $ - * Revision 2.0 88/06/05 00:16:02 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:35:18 lwall + * 3.0 baseline * */ @@ -1,8 +1,13 @@ -/* $Header: str.h,v 2.0 88/06/05 00:16:05 root Exp $ +/* $Header: str.h,v 3.0 89/10/18 15:35:27 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: str.h,v $ - * Revision 2.0 88/06/05 00:16:05 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:35:27 lwall + * 3.0 baseline * */ diff --git a/x2p/util.c b/x2p/util.c index 0d98de807c..27b08b0862 100644 --- a/x2p/util.c +++ b/x2p/util.c @@ -1,8 +1,13 @@ -/* $Header: util.c,v 2.0 88/06/05 00:16:07 root Exp $ +/* $Header: util.c,v 3.0 89/10/18 15:35:35 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: util.c,v $ - * Revision 2.0 88/06/05 00:16:07 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:35:35 lwall + * 3.0 baseline * */ @@ -228,6 +233,13 @@ char *pat; exit(1); } +/*VARARGS1*/ +warn(pat,a1,a2,a3,a4) +char *pat; +{ + fprintf(stderr,pat,a1,a2,a3,a4); +} + static bool firstsetenv = TRUE; extern char **environ; diff --git a/x2p/util.h b/x2p/util.h index 18cca4bb4e..f36c27cfa0 100644 --- a/x2p/util.h +++ b/x2p/util.h @@ -1,8 +1,13 @@ -/* $Header: util.h,v 2.0 88/06/05 00:16:10 root Exp $ +/* $Header: util.h,v 3.0 89/10/18 15:35:41 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: util.h,v $ - * Revision 2.0 88/06/05 00:16:10 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:35:41 lwall + * 3.0 baseline * */ diff --git a/x2p/walk.c b/x2p/walk.c index 344581ea03..959527de3a 100644 --- a/x2p/walk.c +++ b/x2p/walk.c @@ -1,8 +1,13 @@ -/* $Header: walk.c,v 2.0 88/06/05 00:16:12 root Exp $ +/* $Header: walk.c,v 3.0 89/10/18 15:35:48 lwall Locked $ + * + * Copyright (c) 1989, Larry Wall + * + * You may distribute under the terms of the GNU General Public License + * as specified in the README file that comes with the perl 3.0 kit. * * $Log: walk.c,v $ - * Revision 2.0 88/06/05 00:16:12 root - * Baseline version 2.0. + * Revision 3.0 89/10/18 15:35:48 lwall + * 3.0 baseline * */ @@ -14,16 +19,22 @@ bool exitval = FALSE; bool realexit = FALSE; bool saw_getline = FALSE; +bool subretnum = FALSE; +bool saw_FNR = FALSE; +bool saw_argv0 = FALSE; int maxtmp = 0; char *lparen; char *rparen; +STR *subs; +STR *curargs = Nullstr; STR * -walk(useval,level,node,numericptr) +walk(useval,level,node,numericptr,minprec) int useval; int level; register int node; int *numericptr; +int minprec; /* minimum precedence without parens */ { register int len; register STR *str; @@ -31,11 +42,13 @@ int *numericptr; register int i; register STR *tmpstr; STR *tmp2str; + STR *tmp3str; char *t; char *d, *s; int numarg; int numeric = FALSE; STR *fstr; + int prec = P_MAX; /* assume no parens needed */ char *index(); if (!node) { @@ -47,8 +60,9 @@ int *numericptr; type &= 255; switch (type) { case OPROG: - str = walk(0,level,ops[node+1].ival,&numarg); opens = str_new(0); + subs = str_new(0); + str = walk(0,level,ops[node+1].ival,&numarg,P_MIN); if (do_split && need_entire && !absmaxfld) split_to_array = TRUE; if (do_split && split_to_array) @@ -79,103 +93,140 @@ int *numericptr; if (saw_ORS) { str_cat(str,"$\\ = \"\\n\";\t\t# set output record separator\n"); } + if (saw_argv0) { + str_cat(str,"$ARGV0 = $0;\t\t# remember what we ran as\n"); + } if (str->str_cur > 20) str_cat(str,"\n"); if (ops[node+2].ival) { - str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,"\n\n"); } - if (saw_line_op) - str_cat(str,"line: "); - str_cat(str,"while (<>) {\n"); - tab(str,++level); - if (saw_FS && !const_FS) - do_chop = TRUE; - if (do_chop) { - str_cat(str,"chop;\t# strip record separator\n"); - tab(str,level); - } - arymax = 0; - if (namelist) { - while (isalpha(*namelist)) { - for (d = tokenbuf,s=namelist; - isalpha(*s) || isdigit(*s) || *s == '_'; - *d++ = *s++) ; - *d = '\0'; - while (*s && !isalpha(*s)) s++; - namelist = s; - nameary[++arymax] = savestr(tokenbuf); + fstr = walk(0,level+1,ops[node+3].ival,&numarg,P_MIN); + if (*fstr->str_ptr) { + if (saw_line_op) + str_cat(str,"line: "); + str_cat(str,"while (<>) {\n"); + tab(str,++level); + if (saw_FS && !const_FS) + do_chop = TRUE; + if (do_chop) { + str_cat(str,"chop;\t# strip record separator\n"); + tab(str,level); } + arymax = 0; + if (namelist) { + while (isalpha(*namelist)) { + for (d = tokenbuf,s=namelist; + isalpha(*s) || isdigit(*s) || *s == '_'; + *d++ = *s++) ; + *d = '\0'; + while (*s && !isalpha(*s)) s++; + namelist = s; + nameary[++arymax] = savestr(tokenbuf); + } + } + if (maxfld < arymax) + maxfld = arymax; + if (do_split) + emit_split(str,level); + str_scat(str,fstr); + str_free(fstr); + fixtab(str,--level); + str_cat(str,"}\n"); + if (saw_FNR) + str_cat(str,"continue {\n $FNRbase = $. if eof;\n}\n"); } - if (maxfld < arymax) - maxfld = arymax; - if (do_split) - emit_split(str,level); - str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg)); - str_free(fstr); - fixtab(str,--level); - str_cat(str,"}\n"); + else + str_cat(str,"# (no line actions)\n"); if (ops[node+4].ival) { realexit = TRUE; str_cat(str,"\n"); tab(str,level); - str_scat(str,fstr=walk(0,level,ops[node+4].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+4].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,"\n"); } if (exitval) str_cat(str,"exit ExitValue;\n"); + if (subs->str_ptr) { + str_cat(str,"\n"); + str_scat(str,subs); + } if (saw_getline) { - str_cat(str,"\nsub Getline {\n $_ = <>;\n"); - tab(str,++level); - if (do_chop) { - str_cat(str,"chop;\t# strip record separator\n"); - tab(str,level); + for (len = 0; len < 4; len++) { + if (saw_getline & (1 << len)) { + sprintf(tokenbuf,"\nsub Getline%d {\n",len); + str_cat(str, tokenbuf); + if (len & 2) { + if (do_fancy_opens) + str_cat(str," &Pick('',@_);\n"); + else + str_cat(str," ($fh) = @_;\n"); + } + else { + if (saw_FNR) + str_cat(str," $FNRbase = $. if eof;\n"); + } + if (len & 1) + str_cat(str," local($_)\n"); + if (len & 2) + str_cat(str, + " if ($getline_ok = (($_ = <$fh>) ne ''))"); + else + str_cat(str, + " if ($getline_ok = (($_ = <>) ne ''))"); + str_cat(str, " {\n"); + level += 2; + tab(str,level); + i = 0; + if (do_chop) { + i++; + str_cat(str,"chop;\t# strip record separator\n"); + tab(str,level); + } + if (do_split && !(len & 1)) { + i++; + emit_split(str,level); + } + if (!i) + str_cat(str,";\n"); + fixtab(str,--level); + str_cat(str,"}\n $_;\n}\n"); + --level; + } } - if (do_split) - emit_split(str,level); - fixtab(str,--level); - str_cat(str,"}\n"); } if (do_fancy_opens) { str_cat(str,"\n\ sub Pick {\n\ - ($name) = @_;\n\ + local($mode,$name,$pipe) = @_;\n\ $fh = $opened{$name};\n\ if (!$fh) {\n\ - $nextfh == 0 && open(fh_0,$name);\n\ - $nextfh == 1 && open(fh_1,$name);\n\ - $nextfh == 2 && open(fh_2,$name);\n\ - $nextfh == 3 && open(fh_3,$name);\n\ - $nextfh == 4 && open(fh_4,$name);\n\ - $nextfh == 5 && open(fh_5,$name);\n\ - $nextfh == 6 && open(fh_6,$name);\n\ - $nextfh == 7 && open(fh_7,$name);\n\ - $nextfh == 8 && open(fh_8,$name);\n\ - $nextfh == 9 && open(fh_9,$name);\n\ - $fh = $opened{$name} = 'fh_' . $nextfh++;\n\ + $fh = $opened{$name} = 'fh_' . ($nextfh++ + 0);\n\ + open($fh,$mode.$name.$pipe);\n\ }\n\ - select($fh);\n\ }\n\ "); } break; case OHUNKS: - str = walk(0,level,ops[node+1].ival,&numarg); - str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg)); + str = walk(0,level,ops[node+1].ival,&numarg,P_MIN); + str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN)); str_free(fstr); if (len == 3) { - str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg,P_MIN)); str_free(fstr); } else { } break; case ORANGE: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_DOTDOT; + str = walk(1,level,ops[node+1].ival,&numarg,prec+1); str_cat(str," .. "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); str_free(fstr); break; case OPAT: @@ -183,10 +234,10 @@ sub Pick {\n\ case OREGEX: str = str_new(0); str_set(str,"/"); - tmpstr=walk(0,level,ops[node+1].ival,&numarg); + tmpstr=walk(0,level,ops[node+1].ival,&numarg,P_MIN); /* translate \nnn to [\nnn] */ for (s = tmpstr->str_ptr, d = tokenbuf; *s; s++, d++) { - if (*s == '\\' && isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) { + if (*s == '\\' && isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])){ *d++ = '['; *d++ = *s++; *d++ = *s++; @@ -207,92 +258,108 @@ sub Pick {\n\ case OHUNK: if (len == 1) { str = str_new(0); - str = walk(0,level,oper1(OPRINT,0),&numarg); + str = walk(0,level,oper1(OPRINT,0),&numarg,P_MIN); str_cat(str," if "); - str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,";"); } else { - tmpstr = walk(0,level,ops[node+1].ival,&numarg); + tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN); if (*tmpstr->str_ptr) { str = str_new(0); str_set(str,"if ("); str_scat(str,tmpstr); str_cat(str,") {\n"); tab(str,++level); - str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN)); str_free(fstr); fixtab(str,--level); str_cat(str,"}\n"); tab(str,level); } else { - str = walk(0,level,ops[node+2].ival,&numarg); + str = walk(0,level,ops[node+2].ival,&numarg,P_MIN); } } break; case OPPAREN: str = str_new(0); str_set(str,"("); - str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,")"); break; case OPANDAND: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_ANDAND; + str = walk(1,level,ops[node+1].ival,&numarg,prec); str_cat(str," && "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); + str_free(fstr); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1)); str_free(fstr); break; case OPOROR: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_OROR; + str = walk(1,level,ops[node+1].ival,&numarg,prec); str_cat(str," || "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); + str_free(fstr); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1)); str_free(fstr); break; case OPNOT: + prec = P_UNARY; str = str_new(0); str_set(str,"!"); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec)); str_free(fstr); break; case OCPAREN: str = str_new(0); str_set(str,"("); - str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); numeric |= numarg; str_cat(str,")"); break; case OCANDAND: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_ANDAND; + str = walk(1,level,ops[node+1].ival,&numarg,prec); numeric = 1; str_cat(str," && "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); + str_free(fstr); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1)); str_free(fstr); break; case OCOROR: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_OROR; + str = walk(1,level,ops[node+1].ival,&numarg,prec); numeric = 1; str_cat(str," || "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); + str_free(fstr); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1)); str_free(fstr); break; case OCNOT: + prec = P_UNARY; str = str_new(0); str_set(str,"!"); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec)); str_free(fstr); numeric = 1; break; case ORELOP: - str = walk(1,level,ops[node+2].ival,&numarg); + prec = P_REL; + str = walk(1,level,ops[node+2].ival,&numarg,prec+1); numeric |= numarg; - tmpstr = walk(0,level,ops[node+1].ival,&numarg); - tmp2str = walk(1,level,ops[node+3].ival,&numarg); + tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN); + tmp2str = walk(1,level,ops[node+3].ival,&numarg,prec+1); numeric |= numarg; - if (!numeric) { + if (!numeric || + (!numarg && (*tmp2str->str_ptr == '"' || *tmp2str->str_ptr == '\''))) { t = tmpstr->str_ptr; if (strEQ(t,"==")) str_set(tmpstr,"eq"); @@ -329,15 +396,16 @@ sub Pick {\n\ case ORPAREN: str = str_new(0); str_set(str,"("); - str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); numeric |= numarg; str_cat(str,")"); break; case OMATCHOP: - str = walk(1,level,ops[node+2].ival,&numarg); + prec = P_MATCH; + str = walk(1,level,ops[node+2].ival,&numarg,prec+1); str_cat(str," "); - tmpstr = walk(0,level,ops[node+1].ival,&numarg); + tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN); if (strEQ(tmpstr->str_ptr,"~")) str_cat(str,"=~"); else { @@ -345,100 +413,124 @@ sub Pick {\n\ str_free(tmpstr); } str_cat(str," "); - str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1)); str_free(fstr); numeric = 1; break; case OMPAREN: str = str_new(0); str_set(str,"("); - str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg)); + str_scat(str, + fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); numeric |= numarg; str_cat(str,")"); break; case OCONCAT: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_ADD; + type = ops[ops[node+1].ival].ival & 255; + str = walk(1,level,ops[node+1].ival,&numarg,prec+(type != OCONCAT)); str_cat(str," . "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + type = ops[ops[node+2].ival].ival & 255; + str_scat(str, + fstr=walk(1,level,ops[node+2].ival,&numarg,prec+(type != OCONCAT))); str_free(fstr); break; case OASSIGN: - str = walk(0,level,ops[node+2].ival,&numarg); + prec = P_ASSIGN; + str = walk(0,level,ops[node+2].ival,&numarg,prec+1); str_cat(str," "); - tmpstr = walk(0,level,ops[node+1].ival,&numarg); + tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN); str_scat(str,tmpstr); if (str_len(tmpstr) > 1) numeric = 1; str_free(tmpstr); str_cat(str," "); - str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec)); str_free(fstr); numeric |= numarg; break; case OADD: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_ADD; + str = walk(1,level,ops[node+1].ival,&numarg,prec); str_cat(str," + "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); str_free(fstr); numeric = 1; break; - case OSUB: - str = walk(1,level,ops[node+1].ival,&numarg); + case OSUBTRACT: + prec = P_ADD; + str = walk(1,level,ops[node+1].ival,&numarg,prec); str_cat(str," - "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); str_free(fstr); numeric = 1; break; case OMULT: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_MUL; + str = walk(1,level,ops[node+1].ival,&numarg,prec); str_cat(str," * "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); str_free(fstr); numeric = 1; break; case ODIV: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_MUL; + str = walk(1,level,ops[node+1].ival,&numarg,prec); str_cat(str," / "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); + str_free(fstr); + numeric = 1; + break; + case OPOW: + prec = P_POW; + str = walk(1,level,ops[node+1].ival,&numarg,prec+1); + str_cat(str," ** "); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec)); str_free(fstr); numeric = 1; break; case OMOD: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_MUL; + str = walk(1,level,ops[node+1].ival,&numarg,prec); str_cat(str," % "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,prec+1)); str_free(fstr); numeric = 1; break; case OPOSTINCR: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_AUTO; + str = walk(1,level,ops[node+1].ival,&numarg,prec+1); str_cat(str,"++"); numeric = 1; break; case OPOSTDECR: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_AUTO; + str = walk(1,level,ops[node+1].ival,&numarg,prec+1); str_cat(str,"--"); numeric = 1; break; case OPREINCR: + prec = P_AUTO; str = str_new(0); str_set(str,"++"); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec+1)); str_free(fstr); numeric = 1; break; case OPREDECR: + prec = P_AUTO; str = str_new(0); str_set(str,"--"); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec+1)); str_free(fstr); numeric = 1; break; case OUMINUS: + prec = P_UNARY; str = str_new(0); str_set(str,"-"); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,prec)); str_free(fstr); numeric = 1; break; @@ -448,34 +540,107 @@ sub Pick {\n\ case OPAREN: str = str_new(0); str_set(str,"("); - str_scat(str,fstr=walk(useval != 0,level,ops[node+1].ival,&numarg)); + str_scat(str, + fstr=walk(useval != 0,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,")"); numeric |= numarg; break; case OGETLINE: str = str_new(0); - str_set(str,"do Getline()"); - saw_getline = TRUE; + if (useval) + str_cat(str,"("); + if (len > 0) { + str_cat(str,"$"); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); + if (!*fstr->str_ptr) { + str_cat(str,"_"); + len = 2; /* a legal fiction */ + } + str_free(fstr); + } + else + str_cat(str,"$_"); + if (len > 1) { + tmpstr=walk(1,level,ops[node+3].ival,&numarg,P_MIN); + fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN); + if (!do_fancy_opens) { + t = tmpstr->str_ptr; + if (*t == '"' || *t == '\'') + t = cpytill(tokenbuf,t+1,*t); + else + fatal("Internal error: OGETLINE %s", t); + d = savestr(t); + s = savestr(tokenbuf); + for (t = tokenbuf; *t; t++) { + *t &= 127; + if (!isalpha(*t) && !isdigit(*t)) + *t = '_'; + } + if (!index(tokenbuf,'_')) + strcpy(t,"_fh"); + tmp3str = hfetch(symtab,tokenbuf); + if (!tmp3str) { + do_opens = TRUE; + str_cat(opens,"open("); + str_cat(opens,tokenbuf); + str_cat(opens,", "); + d[1] = '\0'; + str_cat(opens,d); + str_cat(opens,tmpstr->str_ptr+1); + opens->str_cur--; + if (*fstr->str_ptr == '|') + str_cat(opens,"|"); + str_cat(opens,d); + if (*fstr->str_ptr == '|') + str_cat(opens,") || die 'Cannot pipe from \""); + else + str_cat(opens,") || die 'Cannot open file \""); + if (*d == '"') + str_cat(opens,"'.\""); + str_cat(opens,s); + if (*d == '"') + str_cat(opens,"\".'"); + str_cat(opens,"\".';\n"); + hstore(symtab,tokenbuf,str_make("x")); + } + safefree(s); + safefree(d); + str_set(tmpstr,"'"); + str_cat(tmpstr,tokenbuf); + str_cat(tmpstr,"'"); + } + if (*fstr->str_ptr == '|') + str_cat(tmpstr,", '|'"); + str_free(fstr); + } + else + tmpstr = str_make(""); + sprintf(tokenbuf," = &Getline%d(%s)",len,tmpstr->str_ptr); + str_cat(str,tokenbuf); + str_free(tmpstr); + if (useval) + str_cat(str,",$getline_ok)"); + saw_getline |= 1 << len; break; case OSPRINTF: str = str_new(0); str_set(str,"sprintf("); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,")"); break; case OSUBSTR: str = str_new(0); str_set(str,"substr("); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_COMMA+1)); str_free(fstr); str_cat(str,", "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_COMMA+1)); str_free(fstr); str_cat(str,", "); if (len == 3) { - str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,P_COMMA+1)); str_free(fstr); } else @@ -489,7 +654,7 @@ sub Pick {\n\ case OSPLIT: str = str_new(0); numeric = 1; - tmpstr = walk(1,level,ops[node+2].ival,&numarg); + tmpstr = walk(1,level,ops[node+2].ival,&numarg,P_MIN); if (useval) str_set(str,"(@"); else @@ -497,7 +662,7 @@ sub Pick {\n\ str_scat(str,tmpstr); str_cat(str," = split("); if (len == 3) { - fstr = walk(1,level,ops[node+3].ival,&numarg); + fstr = walk(1,level,ops[node+3].ival,&numarg,P_COMMA+1); if (str_len(fstr) == 3 && *fstr->str_ptr == '\'') { i = fstr->str_ptr[1] & 127; if (index("*+?.[]()|^$\\",i)) @@ -519,9 +684,9 @@ sub Pick {\n\ else str_cat(str,"' '"); str_cat(str,", "); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_COMMA+1)); str_free(fstr); - str_cat(str,")"); + str_cat(str,", 999)"); if (useval) { str_cat(str,")"); } @@ -530,20 +695,209 @@ sub Pick {\n\ case OINDEX: str = str_new(0); str_set(str,"index("); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_COMMA+1)); str_free(fstr); str_cat(str,", "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_COMMA+1)); str_free(fstr); str_cat(str,")"); numeric = 1; break; + case OMATCH: + str = str_new(0); + prec = P_ANDAND; + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MATCH+1)); + str_free(fstr); + str_cat(str," =~ "); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MATCH+1)); + str_free(fstr); + str_cat(str," && ($RLENGTH = length($&), $RSTART = length($`)+1)"); + numeric = 1; + break; + case OUSERDEF: + str = str_new(0); + subretnum = FALSE; + fstr=walk(1,level-1,ops[node+2].ival,&numarg,P_MIN); + curargs = str_new(0); + str_sset(curargs,fstr); + str_cat(curargs,","); + tmp2str=walk(1,level,ops[node+5].ival,&numarg,P_MIN); + str_free(curargs); + curargs = Nullstr; + level--; + subretnum |= numarg; + s = Nullch; + t = tmp2str->str_ptr; + while (t = instr(t,"return ")) + s = t++; + if (s) { + i = 0; + for (t = s+7; *t; t++) { + if (*t == ';' || *t == '}') + i++; + } + if (i == 1) { + strcpy(s,s+7); + tmp2str->str_cur -= 7; + } + } + str_set(str,"\n"); + tab(str,level); + str_cat(str,"sub "); + str_scat(str,tmpstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); + str_cat(str," {\n"); + tab(str,++level); + if (fstr->str_cur) { + str_cat(str,"local("); + str_scat(str,fstr); + str_cat(str,") = @_;"); + } + str_free(fstr); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,P_MIN)); + str_free(fstr); + fixtab(str,level); + str_scat(str,fstr=walk(1,level,ops[node+4].ival,&numarg,P_MIN)); + str_free(fstr); + fixtab(str,level); + str_scat(str,tmp2str); + str_free(tmp2str); + fixtab(str,--level); + str_cat(str,"}\n"); + tab(str,level); + str_scat(subs,str); + str_set(str,""); + str_cat(tmpstr,"("); + tmp2str = str_new(0); + if (subretnum) + str_set(tmp2str,"1"); + hstore(symtab,tmpstr->str_ptr,tmp2str); + str_free(tmpstr); + level++; + break; + case ORETURN: + str = str_new(0); + if (len > 0) { + str_cat(str,"return "); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_UNI+1)); + str_free(fstr); + if (numarg) + subretnum = TRUE; + } + else + str_cat(str,"return"); + break; + case OUSERFUN: + str = str_new(0); + str_set(str,"&"); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); + str_free(fstr); + str_cat(str,"("); + tmpstr = hfetch(symtab,str->str_ptr+3); + if (tmpstr && tmpstr->str_ptr) + numeric |= atoi(tmpstr->str_ptr); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN)); + str_free(fstr); + str_cat(str,")"); + break; + case OGSUB: + case OSUB: + if (type == OGSUB) + s = "g"; + else + s = ""; + str = str_new(0); + tmpstr = str_new(0); + i = 0; + if (len == 3) { + tmpstr = walk(1,level,ops[node+3].ival,&numarg,P_MATCH+1); + if (strNE(tmpstr->str_ptr,"$_")) { + str_cat(tmpstr, " =~ s"); + i++; + } + else + str_set(tmpstr, "s"); + } + else + str_set(tmpstr, "s"); + type = ops[ops[node+2].ival].ival; + len = type >> 8; + type &= 255; + tmp3str = str_new(0); + if (type == OSTR) { + tmp2str=walk(1,level,ops[ops[node+2].ival+1].ival,&numarg,P_MIN); + for (t = tmp2str->str_ptr, d=tokenbuf; *t; d++,t++) { + if (*t == '&') + *d++ = '$' + 128; + else if (*t == '$') + *d++ = '\\' + 128; + *d = *t + 128; + } + *d = '\0'; + str_set(tmp2str,tokenbuf); + } + else { + tmp2str=walk(1,level,ops[node+2].ival,&numarg,P_MIN); + str_set(tmp3str,"($s_ = '\"'.("); + str_scat(tmp3str,tmp2str); + str_cat(tmp3str,").'\"') =~ s/&/\\$&/g, "); + str_set(tmp2str,"eval $s_"); + s = (*s == 'g' ? "ge" : "e"); + i++; + } + type = ops[ops[node+1].ival].ival; + len = type >> 8; + type &= 255; + fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN); + if (type == OREGEX) { + if (useval && i) + str_cat(str,"("); + str_scat(str,tmp3str); + str_scat(str,tmpstr); + str_scat(str,fstr); + str_scat(str,tmp2str); + str_cat(str,"/"); + str_cat(str,s); + } + else if ((type == OFLD && !split_to_array) || (type == OVAR && len == 1)) { + if (useval && i) + str_cat(str,"("); + str_scat(str,tmp3str); + str_scat(str,tmpstr); + str_cat(str,"/"); + str_scat(str,fstr); + str_cat(str,"/"); + str_scat(str,tmp2str); + str_cat(str,"/"); + str_cat(str,s); + } + else { + i++; + if (useval) + str_cat(str,"("); + str_cat(str,"$s = "); + str_scat(str,fstr); + str_cat(str,", "); + str_scat(str,tmp3str); + str_scat(str,tmpstr); + str_cat(str,"/$s/"); + str_scat(str,tmp2str); + str_cat(str,"/"); + str_cat(str,s); + } + if (useval && i) + str_cat(str,")"); + str_free(fstr); + str_free(tmpstr); + str_free(tmp2str); + str_free(tmp3str); + numeric = 1; + break; case ONUM: - str = walk(1,level,ops[node+1].ival,&numarg); + str = walk(1,level,ops[node+1].ival,&numarg,P_MIN); numeric = 1; break; case OSTR: - tmpstr = walk(1,level,ops[node+1].ival,&numarg); + tmpstr = walk(1,level,ops[node+1].ival,&numarg,P_MIN); s = "'"; for (t = tmpstr->str_ptr, d=tokenbuf; *t; d++,t++) { if (*t == '\'') @@ -567,15 +921,34 @@ sub Pick {\n\ str_free(tmpstr); str_cat(str,s); break; + case ODEFINED: + prec = P_UNI; + str = str_new(0); + str_set(str,"defined $"); + goto addvar; + case ODELETE: + str = str_new(0); + str_set(str,"delete $"); + goto addvar; + case OSTAR: + str = str_new(0); + str_set(str,"*"); + goto addvar; case OVAR: str = str_new(0); str_set(str,"$"); - str_scat(str,tmpstr=walk(1,level,ops[node+1].ival,&numarg)); + addvar: + str_scat(str,tmpstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); if (len == 1) { tmp2str = hfetch(symtab,tmpstr->str_ptr); if (tmp2str && atoi(tmp2str->str_ptr)) numeric = 2; - if (strEQ(str->str_ptr,"$NR")) { + if (strEQ(str->str_ptr,"$FNR")) { + numeric = 1; + saw_FNR++; + str_set(str,"($.-$FNRbase)"); + } + else if (strEQ(str->str_ptr,"$NR")) { numeric = 1; str_set(str,"$."); } @@ -585,22 +958,37 @@ sub Pick {\n\ } else if (strEQ(str->str_ptr,"$0")) str_set(str,"$_"); + else if (strEQ(str->str_ptr,"$ARGC")) + str_set(str,"($#ARGV+1)"); } else { +#ifdef NOTDEF + if (curargs) { + sprintf(tokenbuf,"$%s,",tmpstr->str_ptr); + ??? if (instr(curargs->str_ptr,tokenbuf)) + str_cat(str,"\377"); /* can't translate yet */ + } +#endif str_cat(tmpstr,"[]"); tmp2str = hfetch(symtab,tmpstr->str_ptr); if (tmp2str && atoi(tmp2str->str_ptr)) str_cat(str,"["); else str_cat(str,"{"); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN)); str_free(fstr); - if (tmp2str && atoi(tmp2str->str_ptr)) - strcpy(tokenbuf,"]"); - else - strcpy(tokenbuf,"}"); - *tokenbuf += 128; - str_cat(str,tokenbuf); + if (strEQ(str->str_ptr,"$ARGV[0")) { + str_set(str,"$ARGV0"); + saw_argv0++; + } + else { + if (tmp2str && atoi(tmp2str->str_ptr)) + strcpy(tokenbuf,"]"); + else + strcpy(tokenbuf,"}"); + *tokenbuf += 128; + str_cat(str,tokenbuf); + } } str_free(tmpstr); break; @@ -609,12 +997,12 @@ sub Pick {\n\ if (split_to_array) { str_set(str,"$Fld"); str_cat(str,"["); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,"]"); } else { - i = atoi(walk(1,level,ops[node+1].ival,&numarg)->str_ptr); + i = atoi(walk(1,level,ops[node+1].ival,&numarg,P_MIN)->str_ptr); if (i <= arymax) sprintf(tokenbuf,"$%s",nameary[i]); else @@ -628,7 +1016,7 @@ sub Pick {\n\ i = ops[node+1].ival; if ((ops[i].ival & 255) == OPAREN) i = ops[i+1].ival; - tmpstr=walk(1,level,i,&numarg); + tmpstr=walk(1,level,i,&numarg,P_MIN); str_scat(str,tmpstr); str_free(tmpstr); str_cat(str,"]"); @@ -648,7 +1036,7 @@ sub Pick {\n\ case OSCOMMENT: str = str_new(0); str_set(str,";"); - tmpstr = walk(0,level,ops[node+1].ival,&numarg); + tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN); for (s = tmpstr->str_ptr; *s && *s != '\n'; s++) *s += 128; str_scat(str,tmpstr); @@ -657,7 +1045,7 @@ sub Pick {\n\ break; case OCOMMENT: str = str_new(0); - tmpstr = walk(0,level,ops[node+1].ival,&numarg); + tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN); for (s = tmpstr->str_ptr; *s && *s != '\n'; s++) *s += 128; str_scat(str,tmpstr); @@ -665,27 +1053,31 @@ sub Pick {\n\ tab(str,level); break; case OCOMMA: - str = walk(1,level,ops[node+1].ival,&numarg); + prec = P_COMMA; + str = walk(1,level,ops[node+1].ival,&numarg,prec); str_cat(str,", "); - str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN)); + str_free(fstr); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec+1)); str_free(fstr); break; case OSEMICOLON: str = str_new(1); - str_set(str,"; "); + str_set(str,";\n"); + tab(str,level); break; case OSTATES: - str = walk(0,level,ops[node+1].ival,&numarg); - str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg)); + str = walk(0,level,ops[node+1].ival,&numarg,P_MIN); + str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN)); str_free(fstr); break; case OSTATE: str = str_new(0); if (len >= 1) { - str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); if (len >= 2) { - tmpstr = walk(0,level,ops[node+2].ival,&numarg); + tmpstr = walk(0,level,ops[node+2].ival,&numarg,P_MIN); if (*tmpstr->str_ptr == ';') { addsemi(str); str_cat(str,tmpstr->str_ptr+1); @@ -694,14 +1086,43 @@ sub Pick {\n\ } } break; + case OCLOSE: + str = str_make("close("); + tmpstr = walk(1,level,ops[node+1].ival,&numarg,P_MIN); + if (!do_fancy_opens) { + t = tmpstr->str_ptr; + if (*t == '"' || *t == '\'') + t = cpytill(tokenbuf,t+1,*t); + else + fatal("Internal error: OCLOSE %s",t); + s = savestr(tokenbuf); + for (t = tokenbuf; *t; t++) { + *t &= 127; + if (!isalpha(*t) && !isdigit(*t)) + *t = '_'; + } + if (!index(tokenbuf,'_')) + strcpy(t,"_fh"); + str_free(tmpstr); + safefree(s); + str_set(str,"close "); + str_cat(str,tokenbuf); + } + else { + sprintf(tokenbuf,"$fh = delete $opened{%s} && close($fh)", + tmpstr->str_ptr); + str_free(tmpstr); + str_set(str,tokenbuf); + } + break; case OPRINTF: case OPRINT: lparen = ""; /* set to parens if necessary */ rparen = ""; str = str_new(0); if (len == 3) { /* output redirection */ - tmpstr = walk(1,level,ops[node+3].ival,&numarg); - tmp2str = walk(1,level,ops[node+2].ival,&numarg); + tmpstr = walk(1,level,ops[node+3].ival,&numarg,P_MIN); + tmp2str = walk(1,level,ops[node+2].ival,&numarg,P_MIN); if (!do_fancy_opens) { t = tmpstr->str_ptr; if (*t == '"' || *t == '\'') @@ -717,34 +1138,38 @@ sub Pick {\n\ } if (!index(tokenbuf,'_')) strcpy(t,"_fh"); - str_cat(opens,"open("); - str_cat(opens,tokenbuf); - str_cat(opens,", "); - d[1] = '\0'; - str_cat(opens,d); - str_scat(opens,tmp2str); - str_cat(opens,tmpstr->str_ptr+1); - if (*tmp2str->str_ptr == '|') - str_cat(opens,") || die 'Cannot pipe to \""); - else - str_cat(opens,") || die 'Cannot create file \""); - if (*d == '"') - str_cat(opens,"'.\""); - str_cat(opens,s); - if (*d == '"') - str_cat(opens,"\".'"); - str_cat(opens,"\".';\n"); + tmp3str = hfetch(symtab,tokenbuf); + if (!tmp3str) { + str_cat(opens,"open("); + str_cat(opens,tokenbuf); + str_cat(opens,", "); + d[1] = '\0'; + str_cat(opens,d); + str_scat(opens,tmp2str); + str_cat(opens,tmpstr->str_ptr+1); + if (*tmp2str->str_ptr == '|') + str_cat(opens,") || die 'Cannot pipe to \""); + else + str_cat(opens,") || die 'Cannot create file \""); + if (*d == '"') + str_cat(opens,"'.\""); + str_cat(opens,s); + if (*d == '"') + str_cat(opens,"\".'"); + str_cat(opens,"\".';\n"); + hstore(symtab,tokenbuf,str_make("x")); + } str_free(tmpstr); str_free(tmp2str); safefree(s); safefree(d); } else { - sprintf(tokenbuf,"do Pick('%s' . (%s)) &&\n", + sprintf(tokenbuf,"&Pick('%s', %s) &&\n", tmp2str->str_ptr, tmpstr->str_ptr); str_cat(str,tokenbuf); tab(str,level+1); - *tokenbuf = '\0'; + strcpy(tokenbuf,"$fh"); str_free(tmpstr); str_free(tmp2str); lparen = "("; @@ -752,7 +1177,7 @@ sub Pick {\n\ } } else - strcpy(tokenbuf,"stdout"); + strcpy(tokenbuf,""); str_cat(str,lparen); /* may be null */ if (type == OPRINTF) str_cat(str,"printf"); @@ -763,7 +1188,7 @@ sub Pick {\n\ str_cat(str," "); str_cat(str,tokenbuf); } - tmpstr = walk(1+(type==OPRINT),level,ops[node+1].ival,&numarg); + tmpstr = walk(1+(type==OPRINT),level,ops[node+1].ival,&numarg,P_MIN); if (!*tmpstr->str_ptr && lval_field) { t = saw_OFS ? "$," : "' '"; if (split_to_array) { @@ -795,6 +1220,24 @@ sub Pick {\n\ str_cat(str,rparen); /* may be null */ str_free(tmpstr); break; + case ORAND: + str = str_make("rand(1)"); + break; + case OSRAND: + str = str_make("srand("); + goto maybe0; + case OATAN2: + str = str_make("atan2("); + goto maybe0; + case OSIN: + str = str_make("sin("); + goto maybe0; + case OCOS: + str = str_make("cos("); + goto maybe0; + case OSYSTEM: + str = str_make("system("); + goto maybe0; case OLENGTH: str = str_make("length("); goto maybe0; @@ -812,7 +1255,7 @@ sub Pick {\n\ maybe0: numeric = 1; if (len > 0) - tmpstr = walk(1,level,ops[node+1].ival,&numarg); + tmpstr = walk(1,level,ops[node+1].ival,&numarg,P_MIN); else tmpstr = str_new(0);; if (!*tmpstr->str_ptr) { @@ -863,11 +1306,13 @@ sub Pick {\n\ case OEXIT: str = str_new(0); if (realexit) { + prec = P_UNI; str_set(str,"exit"); if (len == 1) { str_cat(str," "); exitval = TRUE; - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str, + fstr=walk(1,level,ops[node+1].ival,&numarg,prec+1)); str_free(fstr); } } @@ -875,7 +1320,8 @@ sub Pick {\n\ if (len == 1) { str_set(str,"ExitValue = "); exitval = TRUE; - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str, + fstr=walk(1,level,ops[node+1].ival,&numarg,P_ASSIGN)); str_free(fstr); str_cat(str,"; "); } @@ -891,10 +1337,10 @@ sub Pick {\n\ case OIF: str = str_new(0); str_set(str,"if ("); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,") "); - str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN)); str_free(fstr); if (len == 3) { i = ops[node+3].ival; @@ -911,12 +1357,12 @@ sub Pick {\n\ } if (i) { str_cat(str,"els"); - str_scat(str,fstr=walk(0,level,i,&numarg)); + str_scat(str,fstr=walk(0,level,i,&numarg,P_MIN)); str_free(fstr); } else { str_cat(str,"else "); - str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg,P_MIN)); str_free(fstr); } } @@ -924,16 +1370,16 @@ sub Pick {\n\ case OWHILE: str = str_new(0); str_set(str,"while ("); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,") "); - str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN)); str_free(fstr); break; case OFOR: str = str_new(0); str_set(str,"for ("); - str_scat(str,tmpstr=walk(1,level,ops[node+1].ival,&numarg)); + str_scat(str,tmpstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); i = numarg; if (i) { t = s = tmpstr->str_ptr; @@ -944,7 +1390,7 @@ sub Pick {\n\ i = 0; } str_cat(str,"; "); - fstr=walk(1,level,ops[node+2].ival,&numarg); + fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN); if (i && (t = index(fstr->str_ptr,0377))) { if (strnEQ(fstr->str_ptr,s,i)) *t = ' '; @@ -953,57 +1399,65 @@ sub Pick {\n\ str_free(fstr); str_free(tmpstr); str_cat(str,"; "); - str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg)); + str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,P_MIN)); str_free(fstr); str_cat(str,") "); - str_scat(str,fstr=walk(0,level,ops[node+4].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+4].ival,&numarg,P_MIN)); str_free(fstr); break; case OFORIN: - tmpstr=walk(0,level,ops[node+2].ival,&numarg); + tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN); + d = index(tmpstr->str_ptr,'$'); + if (!d) + fatal("Illegal for loop: %s",tmpstr->str_ptr); + s = index(d,'{'); + if (!s) + s = index(d,'['); + if (!s) + fatal("Illegal for loop: %s",d); + *s++ = '\0'; + t = index(s,'}' + 128); + if (!t) + t = index(s,']' + 128); + if (t) + *t = '\0'; str = str_new(0); - str_sset(str,tmpstr); + str_set(str,d+1); str_cat(str,"[]"); tmp2str = hfetch(symtab,str->str_ptr); if (tmp2str && atoi(tmp2str->str_ptr)) { - fstr=walk(1,level,ops[node+1].ival,&numarg); sprintf(tokenbuf, - "foreach $%s (@%s) ", - fstr->str_ptr, - tmpstr->str_ptr); - str_set(str,tokenbuf); - str_free(fstr); - str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg)); - str_free(fstr); + "foreach %s (@%s) ", + s, + d+1); } else { - str_set(str,"while (($"); - str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg)); - str_free(fstr); - str_cat(str,",$junkval) = each("); - str_scat(str,tmpstr); - str_cat(str,")) "); - str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg)); - str_free(fstr); + sprintf(tokenbuf, + "foreach %s (keys %%%s) ", + s, + d+1); } + str_set(str,tokenbuf); + str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN)); + str_free(fstr); str_free(tmpstr); break; case OBLOCK: str = str_new(0); str_set(str,"{"); if (len >= 2 && ops[node+2].ival) { - str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN)); str_free(fstr); } fixtab(str,++level); - str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+1].ival,&numarg,P_MIN)); str_free(fstr); addsemi(str); fixtab(str,--level); str_cat(str,"}\n"); tab(str,level); if (len >= 3) { - str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+3].ival,&numarg,P_MIN)); str_free(fstr); } break; @@ -1012,9 +1466,9 @@ sub Pick {\n\ if (len) { if (len > 5) fatal("Garbage length in walk"); - str = walk(0,level,ops[node+1].ival,&numarg); + str = walk(0,level,ops[node+1].ival,&numarg,P_MIN); for (i = 2; i<= len; i++) { - str_scat(str,fstr=walk(0,level,ops[node+i].ival,&numarg)); + str_scat(str,fstr=walk(0,level,ops[node+i].ival,&numarg,P_MIN)); str_free(fstr); } } @@ -1025,6 +1479,16 @@ sub Pick {\n\ } if (!str) str = str_new(0); + + if (useval && prec < minprec) { /* need parens? */ + fstr = str_new(str->str_cur+2); + str_nset(fstr,"(",1); + str_scat(fstr,str); + str_ncat(fstr,")",1); + str_free(str); + str = fstr; + } + *numericptr = numeric; #ifdef DEBUGGING if (debug & 4) { @@ -1063,7 +1527,7 @@ register int lvl; /* strip trailing white space */ s = str->str_ptr+str->str_cur - 1; - while (s >= str->str_ptr && (*s == ' ' || *s == '\t')) + while (s >= str->str_ptr && (*s == ' ' || *s == '\t' || *s == '\n')) s--; s[1] = '\0'; str->str_cur = s + 1 - str->str_ptr; @@ -1109,13 +1573,13 @@ int level; str_cat(str,tokenbuf); } if (const_FS) { - sprintf(tokenbuf," = split(/[%c\\n]/);\n",const_FS); + sprintf(tokenbuf," = split(/[%c\\n]/, $_, 999);\n",const_FS); str_cat(str,tokenbuf); } else if (saw_FS) - str_cat(str," = split($FS);\n"); + str_cat(str," = split($FS, $_, 999);\n"); else - str_cat(str," = split(' ');\n"); + str_cat(str," = split(' ', $_, 999);\n"); tab(str,level); } @@ -1132,6 +1596,8 @@ int *numericptr; char *d, *s; int numarg; int numeric = FALSE; + STR *tmpstr; + STR *tmp2str; if (!node) { *numericptr = 0; @@ -1259,7 +1725,7 @@ int *numericptr; prewalk(1,level,ops[node+2].ival,&numarg); numeric = 1; break; - case OSUB: + case OSUBTRACT: prewalk(1,level,ops[node+1].ival,&numarg); prewalk(1,level,ops[node+2].ival,&numarg); numeric = 1; @@ -1274,6 +1740,11 @@ int *numericptr; prewalk(1,level,ops[node+2].ival,&numarg); numeric = 1; break; + case OPOW: + prewalk(1,level,ops[node+1].ival,&numarg); + prewalk(1,level,ops[node+2].ival,&numarg); + numeric = 1; + break; case OMOD: prewalk(1,level,ops[node+1].ival,&numarg); prewalk(1,level,ops[node+2].ival,&numarg); @@ -1333,6 +1804,55 @@ int *numericptr; prewalk(0,level,ops[node+2].ival,&numarg); numeric = 1; break; + case OMATCH: + prewalk(0,level,ops[node+1].ival,&numarg); + prewalk(0,level,ops[node+2].ival,&numarg); + numeric = 1; + break; + case OUSERDEF: + subretnum = FALSE; + --level; + tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN); + ++level; + prewalk(0,level,ops[node+2].ival,&numarg); + prewalk(0,level,ops[node+4].ival,&numarg); + prewalk(0,level,ops[node+5].ival,&numarg); + --level; + str_cat(tmpstr,"("); + tmp2str = str_new(0); + if (subretnum || numarg) + str_set(tmp2str,"1"); + hstore(symtab,tmpstr->str_ptr,tmp2str); + str_free(tmpstr); + level++; + break; + case ORETURN: + if (len > 0) { + prewalk(0,level,ops[node+1].ival,&numarg); + if (numarg) + subretnum = TRUE; + } + break; + case OUSERFUN: + tmp2str = str_new(0); + str_scat(tmp2str,tmpstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN)); + fixrargs(tmpstr->str_ptr,ops[node+2],0); + str_free(tmpstr); + str_cat(tmp2str,"("); + tmpstr = hfetch(symtab,tmp2str->str_ptr); + if (tmpstr && tmpstr->str_ptr) + numeric |= atoi(tmpstr->str_ptr); + prewalk(0,level,ops[node+2].ival,&numarg); + str_free(tmp2str); + break; + case OGSUB: + case OSUB: + if (len >= 3) + prewalk(0,level,ops[node+3].ival,&numarg); + prewalk(0,level,ops[ops[node+2].ival+1].ival,&numarg); + prewalk(0,level,ops[node+1].ival,&numarg); + numeric = 1; + break; case ONUM: prewalk(0,level,ops[node+1].ival,&numarg); numeric = 1; @@ -1340,6 +1860,9 @@ int *numericptr; case OSTR: prewalk(0,level,ops[node+1].ival,&numarg); break; + case ODEFINED: + case ODELETE: + case OSTAR: case OVAR: prewalk(0,level,ops[node+1].ival,&numarg); if (len == 1) { @@ -1370,6 +1893,7 @@ int *numericptr; case OCOMMA: prewalk(0,level,ops[node+1].ival,&numarg); prewalk(0,level,ops[node+2].ival,&numarg); + prewalk(0,level,ops[node+3].ival,&numarg); break; case OSEMICOLON: break; @@ -1385,6 +1909,9 @@ int *numericptr; } } break; + case OCLOSE: + prewalk(0,level,ops[node+1].ival,&numarg); + break; case OPRINTF: case OPRINT: if (len == 3) { /* output redirection */ @@ -1393,6 +1920,18 @@ int *numericptr; } prewalk(0+(type==OPRINT),level,ops[node+1].ival,&numarg); break; + case ORAND: + break; + case OSRAND: + goto maybe0; + case OATAN2: + goto maybe0; + case OSIN: + goto maybe0; + case OCOS: + goto maybe0; + case OSYSTEM: + goto maybe0; case OLENGTH: goto maybe0; case OLOG: @@ -1405,7 +1944,8 @@ int *numericptr; maybe0: numeric = 1; if (len > 0) - prewalk(type != OLENGTH,level,ops[node+1].ival,&numarg); + prewalk(type != OLENGTH && type != OSYSTEM, + level,ops[node+1].ival,&numarg); break; case OBREAK: break; @@ -1440,7 +1980,6 @@ int *numericptr; case OFORIN: prewalk(0,level,ops[node+2].ival,&numarg); prewalk(0,level,ops[node+1].ival,&numarg); - prewalk(0,level,ops[node+3].ival,&numarg); break; case OBLOCK: if (len == 2) { @@ -1480,7 +2019,7 @@ register int node; len = type >> 8; type &= 255; if (type == OVAR && len == 1) { - tmpstr=walk(0,0,ops[node+1].ival,&numarg); + tmpstr=walk(0,0,ops[node+1].ival,&numarg,P_MIN); tmp2str = str_make("1"); hstore(symtab,tmpstr->str_ptr,tmp2str); } |