parse.c revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2007 AT&T Knowledge Ventures *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Knowledge Ventures *
* *
* A copy of the License is available at *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* UNIX shell
*
* S. R. Bourne
* Rewritten by David Korn
* AT&T Labs
*
* This is the parser for a shell language
*/
#if KSHELL
#include "defs.h"
#else
#include <shell.h>
#endif
#include <ctype.h>
#include <fcin.h>
#include <error.h>
#include "shlex.h"
#include "history.h"
#include "builtins.h"
#include "test.h"
#include "history.h"
/* These routines are local to this module */
static int skipnl(int);
static Shnode_t *test_primary(void);
#ifndef NIL
#endif /* NIL */
#define CNTL(x) ((x)&037)
#if !KSHELL
static struct stdata
{
int cmdline;
} st;
#endif
static int loop_level;
static struct argnod *label_list;
static struct argnod *label_last;
#if SHOPT_KIA
#include "path.h"
/*
* write out entities for each item in the list
* type=='V' for variable assignment lists
* Otherwise type is determined by the command */
{
register char *cp;
register int n,eline;
int width=0;
unsigned long r=0;
static char atbuff[20];
int justify=0;
if(type==0)
{
type = 'v';
{
case 'a':
type='p';
justify = 'a';
break;
case 'e':
*attribute++ = 'x';
break;
case 'r':
*attribute++ = 'r';
break;
case 'l':
break;
}
{
break;
if(cp[1]==n)
break;
while((n= *++cp))
{
if(isdigit(n))
else if(n=='L' || n=='R' || n =='Z')
justify=n;
else
*attribute++ = n;
}
}
}
else if(cmd)
*attribute = 0;
while(argp)
{
else
}
return(r);
}
#endif /* SHOPT_KIA */
/*
* Make a parent node for fork() or io-redirection
*/
{
return(par);
}
{
else
return(t);
}
/*
* Make a node corresponding to a command list
*/
{
register Shnode_t *t;
if(!l || !r)
sh_syntax();
else
{
else
}
return(t);
}
/*
* entry to shell parser
* Flag can be the union of SH_EOF|SH_NL
*/
{
register Shnode_t *t;
return((void*)sh_trestore(iop));
loop_level = 0;
label_list = label_last = 0;
if(sh_isoption(SH_VERBOSE))
return(NIL(void*));
if(fcfile())
{
{
int version;
fcseek(4);
fcclose();
if(version > 3)
return((void*)sh_trestore(iop));
}
}
#if KSHELL
#endif
fcclose();
/* unstack any completed alias expansions */
{
if(sp)
}
{
}
stakseek(0);
return((void*)t);
}
/*
* This routine parses up the matching right parenthesis and returns
* the parse tree
*/
Shnode_t *sh_dolparen(void)
{
register Shnode_t *t=0;
switch(sh_lex())
{
/* ((...)) arithmetic expression */
case EXPRSYM:
break;
case LPAREN:
break;
}
{
/*
* This code handles the case where string has been converted
* to a file by an alias setup
*/
register int c;
char *cp;
if(fcgetc(c) > 0)
fcseek(-1);
fcclose();
}
return(t);
}
/*
* remove temporary files and stacks
*/
void sh_freeup(void)
{
}
/*
* increase reference count for each stack in function list when flag>0
* decrease reference count for each stack in function list when flag<=0
* stack is freed when reference count is zero
*/
{
{
if(flag<=0)
else
}
}
/*
* cmd
* empty
* list
* list & [ cmd ]
* list [ ; cmd ]
*/
{
{
}
sh_syntax();
{
case COOPSYM: /* set up a cooperating process */
/* FALL THRU */
case '&':
if(left)
{
/* (...)& -> {...;} & */
}
/* FALL THRU */
case ';':
if(!left)
sh_syntax();
break;
case EOFSYM:
break;
default:
{
sh_syntax();
}
}
return(left);
}
/*
* list
* term
* list && term
* list || term
* unfortunately, these are equal precedence
*/
{
register int token;
return(t);
}
/*
* term
* item
* item | term
*/
{
register Shnode_t *t;
register int token;
else
/* check to see if pipeline is to be timed */
{
}
{
{
{
case TFORK:
break;
case TFIL:
break;
default:
}
}
sh_syntax();
}
return(t);
}
/*
* case statement
*/
{
register struct regnod *r;
r->regptr=0;
r->regflag=0;
skipnl(0);
while(1)
{
sh_syntax();
break;
else if(tok=='|')
sh_lex();
else
sh_syntax();
}
else if(tok==FALLTHRUSYM)
{
r->regflag++;
}
else
{
sh_syntax();
r->regnxt=0;
}
return(r);
}
/*
* This routine creates the parse tree for the arithmetic for
* When called, shlex.arg contains the string inside ((...))
* When the first argument is missing, a while node is returned
* Otherise a list containing an arithmetic command and a while
* is returned.
*/
{
register int offset;
register int n;
/* save current input */
/* split ((...)) into three expressions */
for(n=0; ; n++)
{
register int c;
if(n==2)
break;
/* copy up to ; onto the stack */
break;
/* remove trailing white space */
offset--;
/* check for empty initialization expression */
continue;
/* check for empty condition and treat as while((1)) */
stakputc('1');
if(n==0)
else
}
fcseek(1);
if(n<2)
{
sh_syntax();
}
/* check whether the increment is present */
{
}
else
n = skipnl(0);
else if(n==';')
n = sh_lex();
sh_syntax();
return(tf);
}
{
register Shnode_t *t;
register int flag;
#if SHOPT_KIA
#endif /* SHOPT_KIA */
else if(sh_lex())
sh_syntax();
{
fcclose();
}
{
if(fcfill() >= 0)
fcseek(-1);
if(sh_isstate(SH_HISTORY))
else
{
/* copy source to temporary file */
else
}
}
#if SHOPT_KIA
#endif /* SHOPT_KIA */
if(flag)
{
#if SHOPT_BASH
{
else
sh_syntax();
}
#endif
}
skipnl(0);
else
{
}
sh_syntax();
if(jmpval == 0)
{
/* create a new stak frame to compile the command */
/*
* store the pathname of function definition file on stack
* in name field of fake for node
*/
loop_level = 0;
{
/* copy current word token to current stak frame */
}
}
/* restore the old stack */
if(slp)
{
}
#if SHOPT_KIA
#endif /* SHOPT_KIA */
if(jmpval)
{
{
}
}
{
if(fcfill()>0)
fcseek(-1);
}
#if SHOPT_KIA
#endif /* SHOPT_KIA */
return(t);
}
/*
* Compound assignment
*/
{
register int n;
int array=0;
sh_syntax();
{
array = ARG_APPEND;
}
/* shift right */
while(n > 0)
{
n--;
}
array=0;
{
int index= 0;
while(n==LPAREN)
{
n = skipnl(0);
}
}
else if(n)
sh_syntax();
else if(!(shlex.arg->argflag&ARG_ASSIGN) && !((np=nv_search(shlex.arg->argval,sh.fun_tree,0)) && nv_isattr(np,BLT_DCL)))
while(1)
{
break;
else
break;
if(n!=NL && n!=';')
sh_syntax();
{
if(n==RPAREN)
break;
sh_syntax();
}
if((n!=FUNCTSYM) && !(shlex.arg->argflag&ARG_ASSIGN) && !((np=nv_search(shlex.arg->argval,sh.fun_tree,0)) && nv_isattr(np,BLT_DCL)))
{
if(n!=0)
sh_syntax();
/* check for sys5 style function */
{
sh_syntax();
}
}
*tp = t;
}
return(ap);
}
/*
* item
*
* ( cmd ) [ < in ] [ > out ]
* word word* [ < in ] [ > out ]
* if ... then ... else ... fi
* for ... while ... do ... done
* case ... in ... esac
* begin ... end
*/
{
register Shnode_t *t;
int showme=0;
else
io=0;
{
}
switch(tok)
{
/* [[ ... ]] test expression */
case BTESTSYM:
break;
/* ((...)) arithmetic expression */
case EXPRSYM:
sh_lex();
goto done;
/* case statement */
case CASESYM:
{
if(sh_lex())
sh_syntax();
sh_syntax();
{
sh_syntax();
}
break;
}
/* if statement */
case IFSYM:
{
{
goto done;
t = tt;
goto done;
}
break;
}
/* for and select statement */
case FORSYM:
case SELECTSYM:
{
if(sh_lex())
{
sh_syntax();
/* arithmetic for */
t = arithfor(t);
break;
}
#if SHOPT_KIA
#endif /* SHOPT_KIA */
{
if(sh_lex())
{
sh_syntax();
/* some Linux scripts assume this */
if(sh_isoption(SH_NOEXEC))
}
else
sh_syntax();
}
/* 'for i;do cmd' is valid syntax */
else if(tok==';')
sh_syntax();
loop_level++;
if(--loop_level==0)
break;
}
/* This is the code for parsing function definitions */
case FUNCTSYM:
return(funct());
#if SHOPT_NAMESPACE
case NSPACESYM:
if(sh_lex())
sh_syntax();
sh_syntax();
break;
#endif /* SHOPT_NAMESPACE */
/* while and until */
case WHILESYM:
case UNTILSYM:
loop_level++;
if(--loop_level==0)
break;
case LABLSYM:
{
while(argp)
{
}
sh_syntax();
return(t);
}
/* command group with {...} */
case LBRACE:
break;
case LPAREN:
break;
default:
if(io==0)
return(0);
case ';':
if(io==0)
{
return(0);
if(sh_lex()==';')
sh_syntax();
}
/* simple command */
case 0:
return(t);
}
sh_lex();
{
t=makeparent(tok,t);
}
done:
return(t);
}
/*
* This is for a simple command, for list, or compound assignment
*/
{
register struct comnod *t;
register int tok;
int argno = 0;
int assignment = 0;
int associative=0;
{
associative = 1;
}
/* set command line number for error messages */
t->comline = sh_getlineno();
t->comset = 0;
t->comnamp = 0;
t->comnamq = 0;
t->comstate = 0;
{
{
break;
}
sh_syntax();
/* check for assignment argument */
{
if(assignment)
{
if(assignment==1)
{
}
if(argno>=0)
argno++;
}
else /* alias substitutions allowed */
}
else
{
argno = -1;
{
/* check for builtin command */
{
key_on = 1;
}
}
}
#if SHOPT_DEVFD
{
Shnode_t *t;
argno = -1;
goto retry;
}
#endif /* SHOPT_DEVFD */
{
{
if(associative)
goto retry;
}
{
/* SVR2 style function */
{
return(funct());
}
}
}
{
break;
goto retry;
}
{
if(io)
{
}
else
}
}
*argtail = 0;
#if SHOPT_KIA
{
unsigned long r=0;
if(np)
else if(argp)
if(r>0)
#if 0
#endif
{
}
}
#endif /* SHOPT_KIA */
{
{
tok = 0;
{
continue;
if(tok>=1)
{
if(tok>9)
{
tok /= 10;
}
else
}
break;
}
}
}
/* expand argument list if possible */
if(argno>0)
else if(t->comarg)
return((Shnode_t*)t);
}
/*
* skip past newlines but issue prompt if interactive
*/
{
register int token;
sh_syntax();
return(token);
}
/*
* check for and process and i/o redirections
* if flag>0 then an alias can be in the next word
* if flag<0 only one redirection will be processed
*/
{
char *iovname=0;
#if SHOPT_BASH
register int errout=0;
#endif
{
iof = 0;
}
switch(token&0xff)
{
case '<':
{
int n;
if(fcgetc(n)=='#')
else if(n>0)
fcseek(-1);
}
break;
case '>':
#if SHOPT_BASH
if(iof<0)
{
errout = 1;
iof = 1;
}
#endif
break;
default:
return(lastio);
}
{
{
fcseek(-1);
}
else
sh_syntax();
}
{
{
}
else
{
}
}
else
{
}
if(flag>0)
/* allow alias substitutions and parameter assignments */
#if SHOPT_KIA
{
{
unsigned long r=kiaentity((iof&IORAW)?sh_fmtq(iop->ioname):iop->ioname,-1,'f',0,0,shlex.script,'f',0,"");
sfprintf(shlex.kiatmp,"p;%..64d;f;%..64d;%d;%d;%c;%d\n",shlex.current,r,n,n,(iof&IOPUT)?((iof&IOAPP)?'a':'w'):((iof&IODOC)?'h':'r'),iof&IOUFD);
}
}
#endif /* SHOPT_KIA */
if(flag>=0)
{
sh_lex();
#if SHOPT_BASH
if(errout)
{
/* redirect standard output to standard error */
}
#endif
}
else
return(iop);
}
/*
* convert argument chain to argument list when no special arguments
*/
{
register char **cp;
register int special=0;
/* special hack for test -t compatibility */
special = 2;
special = 3;
if(special)
{
special=0;
}
if(special)
{
const char *message;
{
message = "line %d: Invariant test";
special=0;
}
else
{
message = "line %d: -t requires argument";
argn++;
}
if(sh_isoption(SH_NOEXEC))
}
/* leave space for an extra argument at the front */
dp = (struct dolnod*)stakalloc((unsigned)sizeof(struct dolnod) + ARG_SPARE*sizeof(char*) + argn*sizeof(char*));
while(ap)
{
}
if(special==3)
{
cp++;
}
else if(special)
*cp++ = "1";
*cp = 0;
}
{
sh_syntax();
return(t);
}
{
return(t);
}
{
register Shnode_t *t = test_primary();
return(t);
}
/*
* convert =~ into == ~(E)
*/
static void ere_match(void)
{
register int c;
if(c)
fcseek(-1);
fcclose();
}
static Shnode_t *test_primary(void)
{
register Shnode_t *t;
switch(token)
{
case '(':
t = test_expr(')');
break;
case '!':
if(!(t = test_primary()))
sh_syntax();
return(t);
case TESTUNOP:
if(sh_lex())
sh_syntax();
#if SHOPT_KIA
{
unsigned long r;
}
#endif /* SHOPT_KIA */
break;
/* binary test operators */
case 0:
{
{
ere_match();
}
}
else if(token=='<')
else if(token=='>')
{
return(t);
}
else
sh_syntax();
#if SHOPT_KIA
{
unsigned long r;
}
#endif /* SHOPT_KIA */
if(sh_lex())
sh_syntax();
if(num&TEST_PATTERN)
{
num &= ~TEST_PATTERN;
}
#if SHOPT_KIA
{
unsigned long r;
}
#endif /* SHOPT_KIA */
break;
default:
return(0);
}
skipnl(0);
return(t);
}
#if SHOPT_KIA
/*
* return an entity checksum
* The entity is created if it doesn't exist
*/
unsigned long kiaentity(const char *name,int len,int type,int first,int last,unsigned long parent, int pkind, int width, const char *attr)
{
if(len>0)
else
{
if(type=='p')
else
}
stakputc(0);
{
if(!pkind)
pkind = '0';
if(len>0)
sfprintf(shlex.kiafile,"%..64d;%c;%.*s;%d;%d;%..64d;%..64d;%c;%d;%s\n",np->hash,type,len,name,first,last,parent,shlex.fscript,pkind,width,attr);
else
sfprintf(shlex.kiafile,"%..64d;%c;%s;%d;%d;%..64d;%..64d;%c;%d;%s\n",np->hash,type,name,first,last,parent,shlex.fscript,pkind,width,attr);
}
}
{
kiaentity(name+1,-1,*name,0,-1,(*name=='p'?shlex.unknown:shlex.script),np->nvalue.i,nv_size(np),"");
}
int kiaclose(void)
{
register int n;
{
#ifdef SF_BUFCONST
n= sfprintf(shlex.kiafile,"DIRECTORY\nENTITY;%lld;%d\nDIRECTORY;",(Sflong_t)shlex.kiabegin,(size_t)(off1-shlex.kiabegin));
else
n= sfprintf(shlex.kiafile,"DIRECTORY\nENTITY;%lld;%d\nRELATIONSHIP;%lld;%d\nDIRECTORY;",(Sflong_t)shlex.kiabegin,(size_t)(off1-shlex.kiabegin),(Sflong_t)off1,(size_t)(off2-off1));
off2 = -(n+12);
#else
n= sfprintf(shlex.kiafile,"DIRECTORY\nENTITY;%d;%d\nDIRECTORY;",shlex.kiabegin,off1-shlex.kiabegin);
else
n= sfprintf(shlex.kiafile,"DIRECTORY\nENTITY;%d;%d\nRELATIONSHIP;%d;%d\nDIRECTORY;",shlex.kiabegin,off1-shlex.kiabegin,off1,off2-off1);
#endif
}
}
#endif /* SHOPT_KIA */