1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1982-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * UNIX shell
1N/A *
1N/A * S. R. Bourne
1N/A * Rewritten by David Korn
1N/A * AT&T Labs
1N/A *
1N/A * This is the parser for a shell language
1N/A */
1N/A
1N/A#if KSHELL
1N/A#include "defs.h"
1N/A#else
1N/A#include <shell.h>
1N/A#include <ctype.h>
1N/A#endif
1N/A#include <fcin.h>
1N/A#include <error.h>
1N/A#include "shlex.h"
1N/A#include "history.h"
1N/A#include "builtins.h"
1N/A#include "test.h"
1N/A#include "history.h"
1N/A
1N/A#define HERE_MEM 1024 /* size of here-docs kept in memory */
1N/A
1N/A#define hash nvlink.hl._hash
1N/A
1N/A/* These routines are local to this module */
1N/A
1N/Astatic Shnode_t *makeparent(Lex_t*, int, Shnode_t*);
1N/Astatic Shnode_t *makelist(Lex_t*, int, Shnode_t*, Shnode_t*);
1N/Astatic struct argnod *qscan(struct comnod*, int);
1N/Astatic struct ionod *inout(Lex_t*,struct ionod*, int);
1N/Astatic Shnode_t *sh_cmd(Lex_t*,int,int);
1N/Astatic Shnode_t *term(Lex_t*,int);
1N/Astatic Shnode_t *list(Lex_t*,int);
1N/Astatic struct regnod *syncase(Lex_t*,int);
1N/Astatic Shnode_t *item(Lex_t*,int);
1N/Astatic Shnode_t *simple(Lex_t*,int, struct ionod*);
1N/Astatic int skipnl(Lex_t*,int);
1N/Astatic Shnode_t *test_expr(Lex_t*,int);
1N/Astatic Shnode_t *test_and(Lex_t*);
1N/Astatic Shnode_t *test_or(Lex_t*);
1N/Astatic Shnode_t *test_primary(Lex_t*);
1N/A
1N/A#define sh_getlineno(lp) (lp->lastline)
1N/A
1N/A#ifndef NIL
1N/A# define NIL(type) ((type)0)
1N/A#endif /* NIL */
1N/A#define CNTL(x) ((x)&037)
1N/A
1N/A
1N/A#if !KSHELL
1N/Astatic struct stdata
1N/A{
1N/A struct slnod *staklist;
1N/A int cmdline;
1N/A} st;
1N/A#endif
1N/A
1N/Astatic int opt_get;
1N/Astatic int loop_level;
1N/Astatic struct argnod *label_list;
1N/Astatic struct argnod *label_last;
1N/A
1N/A#define getnode(type) ((Shnode_t*)stakalloc(sizeof(struct type)))
1N/A
1N/A#if SHOPT_KIA
1N/A#include "path.h"
1N/A/*
1N/A * write out entities for each item in the list
1N/A * type=='V' for variable assignment lists
1N/A * Otherwise type is determined by the command */
1N/Astatic unsigned long writedefs(Lex_t *lexp,struct argnod *arglist, int line, int type, struct argnod *cmd)
1N/A{
1N/A register struct argnod *argp = arglist;
1N/A register char *cp;
1N/A register int n,eline;
1N/A int width=0;
1N/A unsigned long r=0;
1N/A static char atbuff[20];
1N/A int justify=0;
1N/A char *attribute = atbuff;
1N/A unsigned long parent=lexp->script;
1N/A if(type==0)
1N/A {
1N/A parent = lexp->current;
1N/A type = 'v';
1N/A switch(*argp->argval)
1N/A {
1N/A case 'a':
1N/A type='p';
1N/A justify = 'a';
1N/A break;
1N/A case 'e':
1N/A *attribute++ = 'x';
1N/A break;
1N/A case 'r':
1N/A *attribute++ = 'r';
1N/A break;
1N/A case 'l':
1N/A break;
1N/A }
1N/A while(argp = argp->argnxt.ap)
1N/A {
1N/A if((n= *(cp=argp->argval))!='-' && n!='+')
1N/A break;
1N/A if(cp[1]==n)
1N/A break;
1N/A while((n= *++cp))
1N/A {
1N/A if(isdigit(n))
1N/A width = 10*width + n-'0';
1N/A else if(n=='L' || n=='R' || n =='Z')
1N/A justify=n;
1N/A else
1N/A *attribute++ = n;
1N/A }
1N/A }
1N/A }
1N/A else if(cmd)
1N/A parent=kiaentity(lexp,sh_argstr(cmd),-1,'p',-1,-1,lexp->unknown,'b',0,"");
1N/A *attribute = 0;
1N/A while(argp)
1N/A {
1N/A if((cp=strchr(argp->argval,'='))||(cp=strchr(argp->argval,'?')))
1N/A n = cp-argp->argval;
1N/A else
1N/A n = strlen(argp->argval);
1N/A eline = lexp->sh->inlineno-(lexp->token==NL);
1N/A r=kiaentity(lexp,argp->argval,n,type,line,eline,parent,justify,width,atbuff);
1N/A sfprintf(lexp->kiatmp,"p;%..64d;v;%..64d;%d;%d;s;\n",lexp->current,r,line,eline);
1N/A argp = argp->argnxt.ap;
1N/A }
1N/A return(r);
1N/A}
1N/A#endif /* SHOPT_KIA */
1N/A
1N/Astatic void typeset_order(const char *str,int line)
1N/A{
1N/A register int c,n=0;
1N/A unsigned const char *cp=(unsigned char*)str;
1N/A static unsigned char *table;
1N/A if(*cp!='+' && *cp!='-')
1N/A return;
1N/A if(!table)
1N/A {
1N/A table = calloc(1,256);
1N/A for(cp=(unsigned char*)"bflmnprstuxACHS";c = *cp; cp++)
1N/A table[c] = 1;
1N/A for(cp=(unsigned char*)"aiEFLRXhTZ";c = *cp; cp++)
1N/A table[c] = 2;
1N/A for(c='0'; c <='9'; c++)
1N/A table[c] = 3;
1N/A }
1N/A for(cp=(unsigned char*)str; c= *cp++; n=table[c])
1N/A {
1N/A if(table[c] < n)
1N/A errormsg(SH_DICT,ERROR_warn(0),e_lextypeset,line,str);
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * add type definitions when compiling with -n
1N/A */
1N/Astatic void check_typedef(struct comnod *tp)
1N/A{
1N/A char *cp=0;
1N/A if(tp->comtyp&COMSCAN)
1N/A {
1N/A struct argnod *ap = tp->comarg;
1N/A while(ap = ap->argnxt.ap)
1N/A {
1N/A if(!(ap->argflag&ARG_RAW) || memcmp(ap->argval,"--",2))
1N/A break;
1N/A if(sh_isoption(SH_NOEXEC))
1N/A typeset_order(ap->argval,tp->comline);
1N/A if(memcmp(ap->argval,"-T",2)==0)
1N/A {
1N/A if(ap->argval[2])
1N/A cp = ap->argval+2;
1N/A else if((ap->argnxt.ap)->argflag&ARG_RAW)
1N/A cp = (ap->argnxt.ap)->argval;
1N/A if(cp)
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A else
1N/A {
1N/A struct dolnod *dp = (struct dolnod*)tp->comarg;
1N/A char **argv = dp->dolval + dp->dolbot+1;
1N/A while((cp= *argv++) && memcmp(cp,"--",2))
1N/A {
1N/A if(sh_isoption(SH_NOEXEC))
1N/A typeset_order(cp,tp->comline);
1N/A if(memcmp(cp,"-T",2)==0)
1N/A {
1N/A if(cp[2])
1N/A cp = cp+2;
1N/A else
1N/A cp = *argv;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A if(cp)
1N/A {
1N/A Namval_t *mp=(Namval_t*)tp->comnamp ,*bp;
1N/A bp = sh_addbuiltin(cp,mp->nvalue.bfp, (void*)0);
1N/A nv_onattr(bp,nv_isattr(mp,NV_PUBLIC));
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * Make a parent node for fork() or io-redirection
1N/A */
1N/Astatic Shnode_t *makeparent(Lex_t *lp, int flag, Shnode_t *child)
1N/A{
1N/A register Shnode_t *par = getnode(forknod);
1N/A par->fork.forktyp = flag;
1N/A par->fork.forktre = child;
1N/A par->fork.forkio = 0;
1N/A par->fork.forkline = sh_getlineno(lp)-1;
1N/A return(par);
1N/A}
1N/A
1N/Astatic int paramsub(const char *str)
1N/A{
1N/A register int c,sub=0,lit=0;
1N/A while(c= *str++)
1N/A {
1N/A if(c=='$' && !lit)
1N/A {
1N/A if(*str=='(')
1N/A return(0);
1N/A if(sub)
1N/A continue;
1N/A if(*str=='{')
1N/A str++;
1N/A if(!isdigit(*str) && strchr("?#@*!$ ",*str)==0)
1N/A return(1);
1N/A }
1N/A else if(c=='`')
1N/A return(0);
1N/A else if(c=='[' && !lit)
1N/A sub++;
1N/A else if(c==']' && !lit)
1N/A sub--;
1N/A else if(c=='\'')
1N/A lit = !lit;
1N/A }
1N/A return(0);
1N/A}
1N/A
1N/Astatic Shnode_t *getanode(Lex_t *lp, struct argnod *ap)
1N/A{
1N/A register Shnode_t *t = getnode(arithnod);
1N/A t->ar.artyp = TARITH;
1N/A t->ar.arline = sh_getlineno(lp);
1N/A t->ar.arexpr = ap;
1N/A if(ap->argflag&ARG_RAW)
1N/A t->ar.arcomp = sh_arithcomp(lp->sh,ap->argval);
1N/A else
1N/A {
1N/A if(sh_isoption(SH_NOEXEC) && (ap->argflag&ARG_MAC) && paramsub(ap->argval))
1N/A errormsg(SH_DICT,ERROR_warn(0),e_lexwarnvar,lp->sh->inlineno);
1N/A t->ar.arcomp = 0;
1N/A }
1N/A return(t);
1N/A}
1N/A
1N/A/*
1N/A * Make a node corresponding to a command list
1N/A */
1N/Astatic Shnode_t *makelist(Lex_t *lexp, int type, Shnode_t *l, Shnode_t *r)
1N/A{
1N/A register Shnode_t *t;
1N/A if(!l || !r)
1N/A sh_syntax(lexp);
1N/A else
1N/A {
1N/A if((type&COMMSK) == TTST)
1N/A t = getnode(tstnod);
1N/A else
1N/A t = getnode(lstnod);
1N/A t->lst.lsttyp = type;
1N/A t->lst.lstlef = l;
1N/A t->lst.lstrit = r;
1N/A }
1N/A return(t);
1N/A}
1N/A
1N/A/*
1N/A * entry to shell parser
1N/A * Flag can be the union of SH_EOF|SH_NL
1N/A */
1N/A
1N/Avoid *sh_parse(Shell_t *shp, Sfio_t *iop, int flag)
1N/A{
1N/A register Shnode_t *t;
1N/A Lex_t *lexp = (Lex_t*)shp->lex_context;
1N/A Fcin_t sav_input;
1N/A struct argnod *sav_arg = lexp->arg;
1N/A int sav_prompt = shp->nextprompt;
1N/A if(shp->binscript && (sffileno(iop)==shp->infd || (flag&SH_FUNEVAL)))
1N/A return((void*)sh_trestore(shp,iop));
1N/A fcsave(&sav_input);
1N/A shp->st.staklist = 0;
1N/A lexp->noreserv = 0;
1N/A lexp->heredoc = 0;
1N/A lexp->inlineno = shp->inlineno;
1N/A lexp->firstline = shp->st.firstline;
1N/A shp->nextprompt = 1;
1N/A loop_level = 0;
1N/A label_list = label_last = 0;
1N/A if(sh_isoption(SH_INTERACTIVE))
1N/A sh_onstate(SH_INTERACTIVE);
1N/A if(sh_isoption(SH_VERBOSE))
1N/A sh_onstate(SH_VERBOSE);
1N/A sh_lexopen(lexp,shp,0);
1N/A if(fcfopen(iop) < 0)
1N/A return(NIL(void*));
1N/A if(fcfile())
1N/A {
1N/A char *cp = fcfirst();
1N/A if( cp[0]==CNTL('k') && cp[1]==CNTL('s') && cp[2]==CNTL('h') && cp[3]==0)
1N/A {
1N/A int version;
1N/A fcseek(4);
1N/A fcgetc(version);
1N/A fcclose();
1N/A fcrestore(&sav_input);
1N/A lexp->arg = sav_arg;
1N/A if(version > 3)
1N/A errormsg(SH_DICT,ERROR_exit(1),e_lexversion);
1N/A if(sffileno(iop)==shp->infd || (flag&SH_FUNEVAL))
1N/A shp->binscript = 1;
1N/A sfgetc(iop);
1N/A t = sh_trestore(shp,iop);
1N/A if(flag&SH_NL)
1N/A {
1N/A Shnode_t *tt;
1N/A while(1)
1N/A {
1N/A if(!(tt = sh_trestore(shp,iop)))
1N/A break;
1N/A t =makelist(lexp,TLST, t, tt);
1N/A }
1N/A }
1N/A return((void*)t);
1N/A }
1N/A }
1N/A flag &= ~SH_FUNEVAL;
1N/A if((flag&SH_NL) && (shp->inlineno=error_info.line+shp->st.firstline)==0)
1N/A shp->inlineno=1;
1N/A#if KSHELL
1N/A shp->nextprompt = 2;
1N/A#endif
1N/A t = sh_cmd(lexp,(flag&SH_EOF)?EOFSYM:'\n',SH_SEMI|SH_EMPTY|(flag&SH_NL));
1N/A fcclose();
1N/A fcrestore(&sav_input);
1N/A lexp->arg = sav_arg;
1N/A /* unstack any completed alias expansions */
1N/A if((sfset(iop,0,0)&SF_STRING) && !sfreserve(iop,0,-1))
1N/A {
1N/A Sfio_t *sp = sfstack(iop,NULL);
1N/A if(sp)
1N/A sfclose(sp);
1N/A }
1N/A shp->nextprompt = sav_prompt;
1N/A if(flag&SH_NL)
1N/A {
1N/A shp->st.firstline = lexp->firstline;
1N/A shp->inlineno = lexp->inlineno;
1N/A }
1N/A stkseek(shp->stk,0);
1N/A return((void*)t);
1N/A}
1N/A
1N/A/*
1N/A * This routine parses up the matching right parenthesis and returns
1N/A * the parse tree
1N/A */
1N/AShnode_t *sh_dolparen(Lex_t* lp)
1N/A{
1N/A register Shnode_t *t=0;
1N/A Sfio_t *sp = fcfile();
1N/A int line = lp->sh->inlineno;
1N/A lp->sh->inlineno = error_info.line+lp->sh->st.firstline;
1N/A sh_lexopen(lp,lp->sh,1);
1N/A lp->comsub = 1;
1N/A switch(sh_lex(lp))
1N/A {
1N/A /* ((...)) arithmetic expression */
1N/A case EXPRSYM:
1N/A t = getanode(lp,lp->arg);
1N/A break;
1N/A case LPAREN:
1N/A t = sh_cmd(lp,RPAREN,SH_NL|SH_EMPTY);
1N/A break;
1N/A case LBRACE:
1N/A t = sh_cmd(lp,RBRACE,SH_NL|SH_EMPTY);
1N/A break;
1N/A }
1N/A lp->comsub = 0;
1N/A if(!sp && (sp=fcfile()))
1N/A {
1N/A /*
1N/A * This code handles the case where string has been converted
1N/A * to a file by an alias setup
1N/A */
1N/A register int c;
1N/A char *cp;
1N/A if(fcgetc(c) > 0)
1N/A fcseek(-1);
1N/A cp = fcseek(0);
1N/A fcclose();
1N/A fcsopen(cp);
1N/A sfclose(sp);
1N/A }
1N/A lp->sh->inlineno = line;
1N/A return(t);
1N/A}
1N/A
1N/A/*
1N/A * remove temporary files and stacks
1N/A */
1N/A
1N/Avoid sh_freeup(Shell_t *shp)
1N/A{
1N/A if(shp->st.staklist)
1N/A sh_funstaks(shp->st.staklist,-1);
1N/A shp->st.staklist = 0;
1N/A}
1N/A
1N/A/*
1N/A * increase reference count for each stack in function list when flag>0
1N/A * decrease reference count for each stack in function list when flag<=0
1N/A * stack is freed when reference count is zero
1N/A */
1N/A
1N/Avoid sh_funstaks(register struct slnod *slp,int flag)
1N/A{
1N/A register struct slnod *slpold;
1N/A while(slpold=slp)
1N/A {
1N/A if(slp->slchild)
1N/A sh_funstaks(slp->slchild,flag);
1N/A slp = slp->slnext;
1N/A if(flag<=0)
1N/A stakdelete(slpold->slptr);
1N/A else
1N/A staklink(slpold->slptr);
1N/A }
1N/A}
1N/A/*
1N/A * cmd
1N/A * empty
1N/A * list
1N/A * list & [ cmd ]
1N/A * list [ ; cmd ]
1N/A */
1N/A
1N/Astatic Shnode_t *sh_cmd(Lex_t *lexp, register int sym, int flag)
1N/A{
1N/A register Shnode_t *left, *right;
1N/A register int type = FINT|FAMP;
1N/A if(sym==NL)
1N/A lexp->lasttok = 0;
1N/A left = list(lexp,flag);
1N/A if(lexp->token==NL)
1N/A {
1N/A if(flag&SH_NL)
1N/A lexp->token=';';
1N/A }
1N/A else if(!left && !(flag&SH_EMPTY))
1N/A sh_syntax(lexp);
1N/A switch(lexp->token)
1N/A {
1N/A case COOPSYM: /* set up a cooperating process */
1N/A type |= (FPIN|FPOU|FPCL|FCOOP);
1N/A /* FALL THRU */
1N/A case '&':
1N/A if(left)
1N/A {
1N/A /* (...)& -> {...;} & */
1N/A if(left->tre.tretyp==TPAR)
1N/A left = left->par.partre;
1N/A left = makeparent(lexp,TFORK|type, left);
1N/A }
1N/A /* FALL THRU */
1N/A case ';':
1N/A if(!left)
1N/A sh_syntax(lexp);
1N/A if(right=sh_cmd(lexp,sym,flag|SH_EMPTY))
1N/A left=makelist(lexp,TLST, left, right);
1N/A break;
1N/A case EOFSYM:
1N/A if(sym==NL)
1N/A break;
1N/A default:
1N/A if(sym && sym!=lexp->token)
1N/A {
1N/A if(sym!=ELSESYM || (lexp->token!=ELIFSYM && lexp->token!=FISYM))
1N/A sh_syntax(lexp);
1N/A }
1N/A }
1N/A return(left);
1N/A}
1N/A
1N/A/*
1N/A * list
1N/A * term
1N/A * list && term
1N/A * list || term
1N/A * unfortunately, these are equal precedence
1N/A */
1N/Astatic Shnode_t *list(Lex_t *lexp, register int flag)
1N/A{
1N/A register Shnode_t *t = term(lexp,flag);
1N/A register int token;
1N/A while(t && ((token=lexp->token)==ANDFSYM || token==ORFSYM))
1N/A t = makelist(lexp,(token==ANDFSYM?TAND:TORF), t, term(lexp,SH_NL|SH_SEMI));
1N/A return(t);
1N/A}
1N/A
1N/A/*
1N/A * term
1N/A * item
1N/A * item | term
1N/A */
1N/Astatic Shnode_t *term(Lex_t *lexp,register int flag)
1N/A{
1N/A register Shnode_t *t;
1N/A register int token;
1N/A if(flag&SH_NL)
1N/A token = skipnl(lexp,flag);
1N/A else
1N/A token = sh_lex(lexp);
1N/A /* check to see if pipeline is to be timed */
1N/A if(token==TIMESYM || token==NOTSYM)
1N/A {
1N/A t = getnode(parnod);
1N/A t->par.partyp=TTIME;
1N/A if(lexp->token==NOTSYM)
1N/A t->par.partyp |= COMSCAN;
1N/A t->par.partre = term(lexp,0);
1N/A }
1N/A#if SHOPT_COSHELL
1N/A else if((t=item(lexp,SH_NL|SH_EMPTY|(flag&SH_SEMI))) && (lexp->token=='|' || lexp->token==PIPESYM2))
1N/A#else
1N/A else if((t=item(lexp,SH_NL|SH_EMPTY|(flag&SH_SEMI))) && lexp->token=='|')
1N/A#endif /* SHOPT_COSHELL */
1N/A {
1N/A register Shnode_t *tt;
1N/A int showme = t->tre.tretyp&FSHOWME;
1N/A t = makeparent(lexp,TFORK|FPOU,t);
1N/A#if SHOPT_COSHELL
1N/A if(lexp->token==PIPESYM2)
1N/A t->tre.tretyp |= FALTPIPE;
1N/A#endif /* SHOPT_COSHELL */
1N/A if(tt=term(lexp,SH_NL))
1N/A {
1N/A switch(tt->tre.tretyp&COMMSK)
1N/A {
1N/A case TFORK:
1N/A tt->tre.tretyp |= FPIN|FPCL;
1N/A break;
1N/A case TFIL:
1N/A tt->lst.lstlef->tre.tretyp |= FPIN|FPCL;
1N/A break;
1N/A default:
1N/A tt= makeparent(lexp,TSETIO|FPIN|FPCL,tt);
1N/A }
1N/A t=makelist(lexp,TFIL,t,tt);
1N/A t->tre.tretyp |= showme;
1N/A }
1N/A else if(lexp->token)
1N/A sh_syntax(lexp);
1N/A }
1N/A return(t);
1N/A}
1N/A
1N/A/*
1N/A * case statement
1N/A */
1N/Astatic struct regnod* syncase(Lex_t *lexp,register int esym)
1N/A{
1N/A register int tok = skipnl(lexp,0);
1N/A register struct regnod *r;
1N/A if(tok==esym)
1N/A return(NIL(struct regnod*));
1N/A r = (struct regnod*)stakalloc(sizeof(struct regnod));
1N/A r->regptr=0;
1N/A r->regflag=0;
1N/A if(tok==LPAREN)
1N/A skipnl(lexp,0);
1N/A while(1)
1N/A {
1N/A if(!lexp->arg)
1N/A sh_syntax(lexp);
1N/A lexp->arg->argnxt.ap=r->regptr;
1N/A r->regptr = lexp->arg;
1N/A if((tok=sh_lex(lexp))==RPAREN)
1N/A break;
1N/A else if(tok=='|')
1N/A sh_lex(lexp);
1N/A else
1N/A sh_syntax(lexp);
1N/A }
1N/A r->regcom=sh_cmd(lexp,0,SH_NL|SH_EMPTY|SH_SEMI);
1N/A if((tok=lexp->token)==BREAKCASESYM)
1N/A r->regnxt=syncase(lexp,esym);
1N/A else if(tok==FALLTHRUSYM)
1N/A {
1N/A r->regflag++;
1N/A r->regnxt=syncase(lexp,esym);
1N/A }
1N/A else
1N/A {
1N/A if(tok!=esym && tok!=EOFSYM)
1N/A sh_syntax(lexp);
1N/A r->regnxt=0;
1N/A }
1N/A if(lexp->token==EOFSYM)
1N/A return(NIL(struct regnod*));
1N/A return(r);
1N/A}
1N/A
1N/A/*
1N/A * This routine creates the parse tree for the arithmetic for
1N/A * When called, shlex.arg contains the string inside ((...))
1N/A * When the first argument is missing, a while node is returned
1N/A * Otherise a list containing an arithmetic command and a while
1N/A * is returned.
1N/A */
1N/Astatic Shnode_t *arithfor(Lex_t *lexp,register Shnode_t *tf)
1N/A{
1N/A register Shnode_t *t, *tw = tf;
1N/A register int offset;
1N/A register struct argnod *argp;
1N/A register int n;
1N/A Stk_t *stkp = lexp->sh->stk;
1N/A int argflag = lexp->arg->argflag;
1N/A /* save current input */
1N/A Fcin_t sav_input;
1N/A fcsave(&sav_input);
1N/A fcsopen(lexp->arg->argval);
1N/A /* split ((...)) into three expressions */
1N/A for(n=0; ; n++)
1N/A {
1N/A register int c;
1N/A argp = (struct argnod*)stkseek(stkp,ARGVAL);
1N/A argp->argnxt.ap = 0;
1N/A argp->argchn.cp = 0;
1N/A argp->argflag = argflag;
1N/A if(n==2)
1N/A break;
1N/A /* copy up to ; onto the stack */
1N/A sh_lexskip(lexp,';',1,ST_NESTED);
1N/A offset = stktell(stkp)-1;
1N/A if((c=fcpeek(-1))!=';')
1N/A break;
1N/A /* remove trailing white space */
1N/A while(offset>ARGVAL && ((c= *stkptr(stkp,offset-1)),isspace(c)))
1N/A offset--;
1N/A /* check for empty initialization expression */
1N/A if(offset==ARGVAL && n==0)
1N/A continue;
1N/A stkseek(stkp,offset);
1N/A /* check for empty condition and treat as while((1)) */
1N/A if(offset==ARGVAL)
1N/A sfputc(stkp,'1');
1N/A argp = (struct argnod*)stkfreeze(stkp,1);
1N/A t = getanode(lexp,argp);
1N/A if(n==0)
1N/A tf = makelist(lexp,TLST,t,tw);
1N/A else
1N/A tw->wh.whtre = t;
1N/A }
1N/A while((offset=fcpeek(0)) && isspace(offset))
1N/A fcseek(1);
1N/A stakputs(fcseek(0));
1N/A argp = (struct argnod*)stakfreeze(1);
1N/A fcrestore(&sav_input);
1N/A if(n<2)
1N/A {
1N/A lexp->token = RPAREN|SYMREP;
1N/A sh_syntax(lexp);
1N/A }
1N/A /* check whether the increment is present */
1N/A if(*argp->argval)
1N/A {
1N/A t = getanode(lexp,argp);
1N/A tw->wh.whinc = (struct arithnod*)t;
1N/A }
1N/A else
1N/A tw->wh.whinc = 0;
1N/A sh_lexopen(lexp, lexp->sh,1);
1N/A if((n=sh_lex(lexp))==NL)
1N/A n = skipnl(lexp,0);
1N/A else if(n==';')
1N/A n = sh_lex(lexp);
1N/A if(n!=DOSYM && n!=LBRACE)
1N/A sh_syntax(lexp);
1N/A tw->wh.dotre = sh_cmd(lexp,n==DOSYM?DONESYM:RBRACE,SH_NL);
1N/A tw->wh.whtyp = TWH;
1N/A return(tf);
1N/A
1N/A}
1N/A
1N/Astatic Shnode_t *funct(Lex_t *lexp)
1N/A{
1N/A Shell_t *shp = lexp->sh;
1N/A register Shnode_t *t;
1N/A register int flag;
1N/A struct slnod *volatile slp=0;
1N/A Stak_t *savstak;
1N/A Sfoff_t first, last;
1N/A struct functnod *volatile fp;
1N/A Sfio_t *iop;
1N/A#if SHOPT_KIA
1N/A unsigned long current = lexp->current;
1N/A#endif /* SHOPT_KIA */
1N/A int nargs=0,size=0,jmpval, saveloop=loop_level;
1N/A struct argnod *savelabel = label_last;
1N/A struct checkpt buff;
1N/A int save_optget = opt_get;
1N/A void *in_mktype = shp->mktype;
1N/A shp->mktype = 0;
1N/A opt_get = 0;
1N/A t = getnode(functnod);
1N/A t->funct.functline = shp->inlineno;
1N/A t->funct.functtyp=TFUN;
1N/A t->funct.functargs = 0;
1N/A if(!(flag = (lexp->token==FUNCTSYM)))
1N/A t->funct.functtyp |= FPOSIX;
1N/A else if(sh_lex(lexp))
1N/A sh_syntax(lexp);
1N/A if(!(iop=fcfile()))
1N/A {
1N/A iop = sfopen(NIL(Sfio_t*),fcseek(0),"s");
1N/A fcclose();
1N/A fcfopen(iop);
1N/A }
1N/A t->funct.functloc = first = fctell();
1N/A if(!shp->st.filename || sffileno(iop)<0)
1N/A {
1N/A if(fcfill() >= 0)
1N/A fcseek(-1);
1N/A if(sh_isstate(SH_HISTORY) && shp->gd->hist_ptr)
1N/A t->funct.functloc = sfseek(shp->gd->hist_ptr->histfp,(off_t)0,SEEK_CUR);
1N/A else
1N/A {
1N/A /* copy source to temporary file */
1N/A t->funct.functloc = 0;
1N/A if(lexp->sh->heredocs)
1N/A t->funct.functloc = sfseek(lexp->sh->heredocs,(Sfoff_t)0, SEEK_END);
1N/A else
1N/A lexp->sh->heredocs = sftmp(HERE_MEM);
1N/A lexp->sh->funlog = lexp->sh->heredocs;
1N/A t->funct.functtyp |= FPIN;
1N/A }
1N/A }
1N/A t->funct.functnam= (char*)lexp->arg->argval;
1N/A#if SHOPT_KIA
1N/A if(lexp->kiafile)
1N/A lexp->current = kiaentity(lexp,t->funct.functnam,-1,'p',-1,-1,lexp->script,'p',0,"");
1N/A#endif /* SHOPT_KIA */
1N/A if(flag)
1N/A {
1N/A lexp->token = sh_lex(lexp);
1N/A#if SHOPT_BASH
1N/A if(lexp->token == LPAREN)
1N/A {
1N/A if((lexp->token = sh_lex(lexp)) == RPAREN)
1N/A t->funct.functtyp |= FPOSIX;
1N/A else
1N/A sh_syntax(lexp);
1N/A }
1N/A#endif
1N/A }
1N/A if(t->funct.functtyp&FPOSIX)
1N/A skipnl(lexp,0);
1N/A else
1N/A {
1N/A if(lexp->token==0)
1N/A {
1N/A struct comnod *ac;
1N/A char *cp, **argv, **argv0;
1N/A int c;
1N/A t->funct.functargs = ac = (struct comnod*)simple(lexp,SH_NOIO|SH_FUNDEF,NIL(struct ionod*));
1N/A if(ac->comset || (ac->comtyp&COMSCAN))
1N/A errormsg(SH_DICT,ERROR_exit(3),e_lexsyntax4,lexp->sh->inlineno);
1N/A argv0 = argv = ((struct dolnod*)ac->comarg)->dolval+ARG_SPARE;
1N/A while(cp= *argv++)
1N/A {
1N/A size += strlen(cp)+1;
1N/A if((c = mbchar(cp)) && isaletter(c))
1N/A while(c=mbchar(cp), isaname(c));
1N/A }
1N/A if(c)
1N/A errormsg(SH_DICT,ERROR_exit(3),e_lexsyntax4,lexp->sh->inlineno);
1N/A nargs = argv-argv0;
1N/A size += sizeof(struct dolnod)+(nargs+ARG_SPARE)*sizeof(char*);
1N/A if(shp->shcomp && memcmp(".sh.math.",t->funct.functnam,9)==0)
1N/A {
1N/A Namval_t *np= nv_open(t->funct.functnam,shp->fun_tree,NV_ADD|NV_VARNAME);
1N/A np->nvalue.rp = new_of(struct Ufunction,shp->funload?sizeof(Dtlink_t):0);
1N/A memset((void*)np->nvalue.rp,0,sizeof(struct Ufunction));
1N/A np->nvalue.rp->argc = ((struct dolnod*)ac->comarg)->dolnum;
1N/A }
1N/A }
1N/A while(lexp->token==NL)
1N/A lexp->token = sh_lex(lexp);
1N/A }
1N/A if((flag && lexp->token!=LBRACE) || lexp->token==EOFSYM)
1N/A sh_syntax(lexp);
1N/A sh_pushcontext(shp,&buff,1);
1N/A jmpval = sigsetjmp(buff.buff,0);
1N/A if(jmpval == 0)
1N/A {
1N/A /* create a new stak frame to compile the command */
1N/A savstak = stakcreate(STAK_SMALL);
1N/A savstak = stakinstall(savstak, 0);
1N/A slp = (struct slnod*)stakalloc(sizeof(struct slnod)+sizeof(struct functnod));
1N/A slp->slchild = 0;
1N/A slp->slnext = shp->st.staklist;
1N/A shp->st.staklist = 0;
1N/A t->funct.functstak = (struct slnod*)slp;
1N/A /*
1N/A * store the pathname of function definition file on stack
1N/A * in name field of fake for node
1N/A */
1N/A fp = (struct functnod*)(slp+1);
1N/A fp->functtyp = TFUN|FAMP;
1N/A fp->functnam = 0;
1N/A fp->functline = t->funct.functline;
1N/A if(shp->st.filename)
1N/A fp->functnam = stakcopy(shp->st.filename);
1N/A loop_level = 0;
1N/A label_last = label_list;
1N/A if(size)
1N/A {
1N/A struct dolnod *dp = (struct dolnod*)stakalloc(size);
1N/A char *cp, *sp, **argv, **old = ((struct dolnod*)t->funct.functargs->comarg)->dolval+1;
1N/A argv = ((char**)(dp->dolval))+1;
1N/A dp->dolnum = ((struct dolnod*)t->funct.functargs->comarg)->dolnum;
1N/A t->funct.functargs->comarg = (struct argnod*)dp;
1N/A for(cp=(char*)&argv[nargs]; sp= *old++; cp++)
1N/A {
1N/A *argv++ = cp;
1N/A cp = strcopy(cp,sp);
1N/A }
1N/A *argv = 0;
1N/A }
1N/A if(!flag && lexp->token==0)
1N/A {
1N/A /* copy current word token to current stak frame */
1N/A struct argnod *ap;
1N/A flag = ARGVAL + strlen(lexp->arg->argval);
1N/A ap = (struct argnod*)stakalloc(flag);
1N/A memcpy(ap,lexp->arg,flag);
1N/A lexp->arg = ap;
1N/A }
1N/A t->funct.functtre = item(lexp,SH_NOIO);
1N/A }
1N/A else if(shp->shcomp)
1N/A exit(1);
1N/A sh_popcontext(shp,&buff);
1N/A loop_level = saveloop;
1N/A label_last = savelabel;
1N/A /* restore the old stack */
1N/A if(slp)
1N/A {
1N/A slp->slptr = stakinstall(savstak,0);
1N/A slp->slchild = shp->st.staklist;
1N/A }
1N/A#if SHOPT_KIA
1N/A lexp->current = current;
1N/A#endif /* SHOPT_KIA */
1N/A if(jmpval)
1N/A {
1N/A if(slp && slp->slptr)
1N/A {
1N/A shp->st.staklist = slp->slnext;
1N/A stakdelete(slp->slptr);
1N/A }
1N/A siglongjmp(*shp->jmplist,jmpval);
1N/A }
1N/A shp->st.staklist = (struct slnod*)slp;
1N/A last = fctell();
1N/A fp->functline = (last-first);
1N/A fp->functtre = t;
1N/A shp->mktype = in_mktype;
1N/A if(lexp->sh->funlog)
1N/A {
1N/A if(fcfill()>0)
1N/A fcseek(-1);
1N/A lexp->sh->funlog = 0;
1N/A }
1N/A#if SHOPT_KIA
1N/A if(lexp->kiafile)
1N/A kiaentity(lexp,t->funct.functnam,-1,'p',t->funct.functline,shp->inlineno-1,lexp->current,'p',0,"");
1N/A#endif /* SHOPT_KIA */
1N/A t->funct.functtyp |= opt_get;
1N/A opt_get = save_optget;
1N/A return(t);
1N/A}
1N/A
1N/A/*
1N/A * Compound assignment
1N/A */
1N/Astatic struct argnod *assign(Lex_t *lexp, register struct argnod *ap, int type)
1N/A{
1N/A register int n;
1N/A register Shnode_t *t, **tp;
1N/A register struct comnod *ac;
1N/A Stk_t *stkp = lexp->sh->stk;
1N/A int array=0, index=0;
1N/A Namval_t *np;
1N/A n = strlen(ap->argval)-1;
1N/A if(ap->argval[n]!='=')
1N/A sh_syntax(lexp);
1N/A if(ap->argval[n-1]=='+')
1N/A {
1N/A ap->argval[n--]=0;
1N/A array = ARG_APPEND;
1N/A }
1N/A /* shift right */
1N/A while(n > 0)
1N/A {
1N/A ap->argval[n] = ap->argval[n-1];
1N/A n--;
1N/A }
1N/A *ap->argval=0;
1N/A t = getnode(fornod);
1N/A t->for_.fornam = (char*)(ap->argval+1);
1N/A t->for_.fortyp = sh_getlineno(lexp);
1N/A tp = &t->for_.fortre;
1N/A ap->argchn.ap = (struct argnod*)t;
1N/A ap->argflag &= ARG_QUOTED;
1N/A ap->argflag |= array;
1N/A lexp->assignok = SH_ASSIGN;
1N/A if(type==NV_ARRAY)
1N/A lexp->noreserv = 1;
1N/A else
1N/A lexp->aliasok = 1;
1N/A array= (type==NV_ARRAY)?SH_ARRAY:0;
1N/A if((n=skipnl(lexp,0))==RPAREN || n==LPAREN)
1N/A {
1N/A struct argnod *ar,*aq,**settail;
1N/A ac = (struct comnod*)getnode(comnod);
1N/A memset((void*)ac,0,sizeof(*ac));
1N/A comarray:
1N/A settail= &ac->comset;
1N/A ac->comline = sh_getlineno(lexp);
1N/A while(n==LPAREN)
1N/A {
1N/A ar = (struct argnod*)stkseek(stkp,ARGVAL);
1N/A ar->argflag= ARG_ASSIGN;
1N/A sfprintf(stkp,"[%d]=",index++);
1N/A if(aq=ac->comarg)
1N/A {
1N/A ac->comarg = aq->argnxt.ap;
1N/A sfprintf(stkp,"%s",aq->argval);
1N/A ar->argflag |= aq->argflag;
1N/A }
1N/A ar = (struct argnod*)stkfreeze(stkp,1);
1N/A ar->argnxt.ap = 0;
1N/A if(!aq)
1N/A ar = assign(lexp,ar,0);
1N/A ar->argflag |= ARG_MESSAGE;
1N/A *settail = ar;
1N/A settail = &(ar->argnxt.ap);
1N/A if(aq)
1N/A continue;
1N/A while((n = skipnl(lexp,0))==0)
1N/A {
1N/A ar = (struct argnod*)stkseek(stkp,ARGVAL);
1N/A ar->argflag= ARG_ASSIGN;
1N/A sfprintf(stkp,"[%d]=",index++);
1N/A stakputs(lexp->arg->argval);
1N/A ar = (struct argnod*)stkfreeze(stkp,1);
1N/A ar->argnxt.ap = 0;
1N/A ar->argflag = lexp->arg->argflag;
1N/A *settail = ar;
1N/A settail = &(ar->argnxt.ap);
1N/A }
1N/A }
1N/A }
1N/A else if(n && n!=FUNCTSYM)
1N/A sh_syntax(lexp);
1N/A else if(type!=NV_ARRAY && n!=FUNCTSYM && !(lexp->arg->argflag&ARG_ASSIGN) && !((np=nv_search(lexp->arg->argval,lexp->sh->fun_tree,0)) && (nv_isattr(np,BLT_DCL)|| np==SYSDOT)))
1N/A {
1N/A array=SH_ARRAY;
1N/A if(fcgetc(n)==LPAREN)
1N/A {
1N/A int c;
1N/A if(fcgetc(c)==RPAREN)
1N/A {
1N/A lexp->token = SYMRES;
1N/A array = 0;
1N/A }
1N/A else
1N/A fcseek(-2);
1N/A }
1N/A else if(n>0)
1N/A fcseek(-1);
1N/A if(array && type==NV_TYPE)
1N/A {
1N/A struct argnod *arg = lexp->arg;
1N/A n = lexp->token;
1N/A if(path_search(lexp->sh,lexp->arg->argval,NIL(Pathcomp_t**),1) && (np=nv_search(lexp->arg->argval,lexp->sh->fun_tree,0)) && nv_isattr(np,BLT_DCL))
1N/A {
1N/A lexp->token = n;
1N/A lexp->arg = arg;
1N/A array = 0;
1N/A }
1N/A else
1N/A sh_syntax(lexp);
1N/A }
1N/A }
1N/A lexp->noreserv = 0;
1N/A while(1)
1N/A {
1N/A if((n=lexp->token)==RPAREN)
1N/A break;
1N/A if(n==FUNCTSYM || n==SYMRES)
1N/A ac = (struct comnod*)funct(lexp);
1N/A else
1N/A ac = (struct comnod*)simple(lexp,SH_NOIO|SH_ASSIGN|array,NIL(struct ionod*));
1N/A if((n=lexp->token)==RPAREN)
1N/A break;
1N/A if(n!=NL && n!=';')
1N/A {
1N/A if(array && n==LPAREN)
1N/A goto comarray;
1N/A sh_syntax(lexp);
1N/A }
1N/A lexp->assignok = SH_ASSIGN;
1N/A if((n=skipnl(lexp,0)) || array)
1N/A {
1N/A if(n==RPAREN)
1N/A break;
1N/A if(array || n!=FUNCTSYM)
1N/A sh_syntax(lexp);
1N/A }
1N/A if((n!=FUNCTSYM) && !(lexp->arg->argflag&ARG_ASSIGN) && !((np=nv_search(lexp->arg->argval,lexp->sh->fun_tree,0)) && (nv_isattr(np,BLT_DCL)||np==SYSDOT)))
1N/A {
1N/A struct argnod *arg = lexp->arg;
1N/A if(n!=0)
1N/A sh_syntax(lexp);
1N/A /* check for sys5 style function */
1N/A if(sh_lex(lexp)!=LPAREN || sh_lex(lexp)!=RPAREN)
1N/A {
1N/A lexp->arg = arg;
1N/A lexp->token = 0;
1N/A sh_syntax(lexp);
1N/A }
1N/A lexp->arg = arg;
1N/A lexp->token = SYMRES;
1N/A }
1N/A t = makelist(lexp,TLST,(Shnode_t*)ac,t);
1N/A *tp = t;
1N/A tp = &t->lst.lstrit;
1N/A }
1N/A *tp = (Shnode_t*)ac;
1N/A lexp->assignok = 0;
1N/A return(ap);
1N/A}
1N/A
1N/A/*
1N/A * item
1N/A *
1N/A * ( cmd ) [ < in ] [ > out ]
1N/A * word word* [ < in ] [ > out ]
1N/A * if ... then ... else ... fi
1N/A * for ... while ... do ... done
1N/A * case ... in ... esac
1N/A * begin ... end
1N/A */
1N/A
1N/Astatic Shnode_t *item(Lex_t *lexp,int flag)
1N/A{
1N/A register Shnode_t *t;
1N/A register struct ionod *io;
1N/A register int tok = (lexp->token&0xff);
1N/A int savwdval = lexp->lasttok;
1N/A int savline = lexp->lastline;
1N/A int showme=0, comsub;
1N/A if(!(flag&SH_NOIO) && (tok=='<' || tok=='>' || lexp->token==IOVNAME))
1N/A io=inout(lexp,NIL(struct ionod*),1);
1N/A else
1N/A io=0;
1N/A if((tok=lexp->token) && tok!=EOFSYM && tok!=FUNCTSYM)
1N/A {
1N/A lexp->lastline = sh_getlineno(lexp);
1N/A lexp->lasttok = lexp->token;
1N/A }
1N/A switch(tok)
1N/A {
1N/A /* [[ ... ]] test expression */
1N/A case BTESTSYM:
1N/A t = test_expr(lexp,ETESTSYM);
1N/A t->tre.tretyp &= ~TTEST;
1N/A break;
1N/A /* ((...)) arithmetic expression */
1N/A case EXPRSYM:
1N/A t = getanode(lexp,lexp->arg);
1N/A sh_lex(lexp);
1N/A goto done;
1N/A
1N/A /* case statement */
1N/A case CASESYM:
1N/A {
1N/A int savetok = lexp->lasttok;
1N/A int saveline = lexp->lastline;
1N/A t = getnode(swnod);
1N/A if(sh_lex(lexp))
1N/A sh_syntax(lexp);
1N/A t->sw.swarg=lexp->arg;
1N/A t->sw.swtyp=TSW;
1N/A t->sw.swio = 0;
1N/A t->sw.swtyp |= FLINENO;
1N/A t->sw.swline = lexp->sh->inlineno;
1N/A if((tok=skipnl(lexp,0))!=INSYM && tok!=LBRACE)
1N/A sh_syntax(lexp);
1N/A if(!(t->sw.swlst=syncase(lexp,tok==INSYM?ESACSYM:RBRACE)) && lexp->token==EOFSYM)
1N/A {
1N/A lexp->lasttok = savetok;
1N/A lexp->lastline = saveline;
1N/A sh_syntax(lexp);
1N/A }
1N/A break;
1N/A }
1N/A
1N/A /* if statement */
1N/A case IFSYM:
1N/A {
1N/A register Shnode_t *tt;
1N/A t = getnode(ifnod);
1N/A t->if_.iftyp=TIF;
1N/A t->if_.iftre=sh_cmd(lexp,THENSYM,SH_NL);
1N/A t->if_.thtre=sh_cmd(lexp,ELSESYM,SH_NL|SH_SEMI);
1N/A tok = lexp->token;
1N/A t->if_.eltre=(tok==ELSESYM?sh_cmd(lexp,FISYM,SH_NL|SH_SEMI):
1N/A (tok==ELIFSYM?(lexp->token=IFSYM, tt=item(lexp,SH_NOIO)):0));
1N/A if(tok==ELIFSYM)
1N/A {
1N/A if(!tt || tt->tre.tretyp!=TSETIO)
1N/A goto done;
1N/A t->if_.eltre = tt->fork.forktre;
1N/A tt->fork.forktre = t;
1N/A t = tt;
1N/A goto done;
1N/A }
1N/A break;
1N/A }
1N/A
1N/A /* for and select statement */
1N/A case FORSYM:
1N/A case SELECTSYM:
1N/A {
1N/A t = getnode(fornod);
1N/A t->for_.fortyp=(lexp->token==FORSYM?TFOR:TSELECT);
1N/A t->for_.forlst=0;
1N/A t->for_.forline = lexp->sh->inlineno;
1N/A if(sh_lex(lexp))
1N/A {
1N/A if(lexp->token!=EXPRSYM || t->for_.fortyp!=TFOR)
1N/A sh_syntax(lexp);
1N/A /* arithmetic for */
1N/A t = arithfor(lexp,t);
1N/A break;
1N/A }
1N/A t->for_.fornam=(char*) lexp->arg->argval;
1N/A t->for_.fortyp |= FLINENO;
1N/A#if SHOPT_KIA
1N/A if(lexp->kiafile)
1N/A writedefs(lexp,lexp->arg,lexp->sh->inlineno,'v',NIL(struct argnod*));
1N/A#endif /* SHOPT_KIA */
1N/A while((tok=sh_lex(lexp))==NL);
1N/A if(tok==INSYM)
1N/A {
1N/A if(sh_lex(lexp))
1N/A {
1N/A if(lexp->token != NL && lexp->token !=';')
1N/A sh_syntax(lexp);
1N/A /* some Linux scripts assume this */
1N/A if(sh_isoption(SH_NOEXEC))
1N/A errormsg(SH_DICT,ERROR_warn(0),e_lexemptyfor,lexp->sh->inlineno-(lexp->token=='\n'));
1N/A t->for_.forlst = (struct comnod*)getnode(comnod);
1N/A (t->for_.forlst)->comarg = 0;
1N/A (t->for_.forlst)->comset = 0;
1N/A (t->for_.forlst)->comnamp = 0;
1N/A (t->for_.forlst)->comnamq = 0;
1N/A (t->for_.forlst)->comstate = 0;
1N/A (t->for_.forlst)->comio = 0;
1N/A (t->for_.forlst)->comtyp = 0;
1N/A }
1N/A else
1N/A t->for_.forlst=(struct comnod*)simple(lexp,SH_NOIO,NIL(struct ionod*));
1N/A if(lexp->token != NL && lexp->token !=';')
1N/A sh_syntax(lexp);
1N/A tok = skipnl(lexp,0);
1N/A }
1N/A /* 'for i;do cmd' is valid syntax */
1N/A else if(tok==';')
1N/A tok=sh_lex(lexp);
1N/A if(tok!=DOSYM && tok!=LBRACE)
1N/A sh_syntax(lexp);
1N/A loop_level++;
1N/A t->for_.fortre=sh_cmd(lexp,tok==DOSYM?DONESYM:RBRACE,SH_NL|SH_SEMI);
1N/A if(--loop_level==0)
1N/A label_last = label_list;
1N/A break;
1N/A }
1N/A
1N/A /* This is the code for parsing function definitions */
1N/A case FUNCTSYM:
1N/A return(funct(lexp));
1N/A
1N/A#if SHOPT_NAMESPACE
1N/A case NSPACESYM:
1N/A t = getnode(fornod);
1N/A t->for_.fortyp=TNSPACE;
1N/A t->for_.forlst=0;
1N/A if(sh_lex(lexp))
1N/A sh_syntax(lexp);
1N/A t->for_.fornam=(char*) lexp->arg->argval;
1N/A while((tok=sh_lex(lexp))==NL);
1N/A if(tok!=LBRACE)
1N/A sh_syntax(lexp);
1N/A t->for_.fortre = sh_cmd(lexp,RBRACE,SH_NL);
1N/A break;
1N/A#endif /* SHOPT_NAMESPACE */
1N/A
1N/A /* while and until */
1N/A case WHILESYM:
1N/A case UNTILSYM:
1N/A t = getnode(whnod);
1N/A t->wh.whtyp=(lexp->token==WHILESYM ? TWH : TUN);
1N/A loop_level++;
1N/A t->wh.whtre = sh_cmd(lexp,DOSYM,SH_NL);
1N/A t->wh.dotre = sh_cmd(lexp,DONESYM,SH_NL|SH_SEMI);
1N/A if(--loop_level==0)
1N/A label_last = label_list;
1N/A t->wh.whinc = 0;
1N/A break;
1N/A
1N/A case LABLSYM:
1N/A {
1N/A register struct argnod *argp = label_list;
1N/A while(argp)
1N/A {
1N/A if(strcmp(argp->argval,lexp->arg->argval)==0)
1N/A errormsg(SH_DICT,ERROR_exit(3),e_lexsyntax3,lexp->sh->inlineno,argp->argval);
1N/A argp = argp->argnxt.ap;
1N/A }
1N/A lexp->arg->argnxt.ap = label_list;
1N/A label_list = lexp->arg;
1N/A label_list->argchn.len = sh_getlineno(lexp);
1N/A label_list->argflag = loop_level;
1N/A skipnl(lexp,flag);
1N/A if(!(t = item(lexp,SH_NL)))
1N/A sh_syntax(lexp);
1N/A tok = (t->tre.tretyp&(COMSCAN|COMSCAN-1));
1N/A if(sh_isoption(SH_NOEXEC) && tok!=TWH && tok!=TUN && tok!=TFOR && tok!=TSELECT)
1N/A errormsg(SH_DICT,ERROR_warn(0),e_lexlabignore,label_list->argchn.len,label_list->argval);
1N/A return(t);
1N/A }
1N/A
1N/A /* command group with {...} */
1N/A case LBRACE:
1N/A comsub = lexp->comsub;
1N/A lexp->comsub = 0;
1N/A t = sh_cmd(lexp,RBRACE,SH_NL|SH_SEMI);
1N/A lexp->comsub = comsub;
1N/A break;
1N/A
1N/A case LPAREN:
1N/A t = getnode(parnod);
1N/A t->par.partre=sh_cmd(lexp,RPAREN,SH_NL|SH_SEMI);
1N/A t->par.partyp=TPAR;
1N/A break;
1N/A
1N/A#if SHOPT_COSHELL
1N/A case '&':
1N/A if(tok=sh_lex(lexp))
1N/A {
1N/A if(tok!=NL)
1N/A sh_syntax(lexp);
1N/A t = getnode(comnod);
1N/A memset(t,0,sizeof(struct comnod));
1N/A t->com.comline = sh_getlineno(lexp);
1N/A }
1N/A else
1N/A t = (Shnode_t*)simple(lexp,SH_NOIO,NIL(struct ionod*));
1N/A t->com.comtyp |= FAMP;
1N/A if(lexp->token=='&' || lexp->token=='|')
1N/A sh_syntax(lexp);
1N/A return(t);
1N/A break;
1N/A#endif /* SHOPT_COSHELL */
1N/A default:
1N/A if(io==0)
1N/A return(0);
1N/A
1N/A case ';':
1N/A if(io==0)
1N/A {
1N/A if(!(flag&SH_SEMI))
1N/A return(0);
1N/A if(sh_lex(lexp)==';')
1N/A sh_syntax(lexp);
1N/A showme = FSHOWME;
1N/A }
1N/A /* simple command */
1N/A case 0:
1N/A t = (Shnode_t*)simple(lexp,flag,io);
1N/A if(t->com.comarg && lexp->intypeset)
1N/A check_typedef(&t->com);
1N/A lexp->intypeset = 0;
1N/A lexp->inexec = 0;
1N/A t->tre.tretyp |= showme;
1N/A return(t);
1N/A }
1N/A sh_lex(lexp);
1N/A if(io=inout(lexp,io,0))
1N/A {
1N/A if((tok=t->tre.tretyp&COMMSK) != TFORK)
1N/A tok = TSETIO;
1N/A t=makeparent(lexp,tok,t);
1N/A t->tre.treio=io;
1N/A }
1N/Adone:
1N/A lexp->lasttok = savwdval;
1N/A lexp->lastline = savline;
1N/A return(t);
1N/A}
1N/A
1N/Astatic struct argnod *process_sub(Lex_t *lexp,int tok)
1N/A{
1N/A struct argnod *argp;
1N/A Shnode_t *t;
1N/A int mode = (tok==OPROCSYM);
1N/A t = sh_cmd(lexp,RPAREN,SH_NL);
1N/A argp = (struct argnod*)stkalloc(lexp->sh->stk,sizeof(struct argnod));
1N/A *argp->argval = 0;
1N/A argp->argchn.ap = (struct argnod*)makeparent(lexp,mode?TFORK|FPIN|FAMP|FPCL:TFORK|FPOU,t);
1N/A argp->argflag = (ARG_EXP|mode);
1N/A return(argp);
1N/A}
1N/A
1N/A
1N/A/*
1N/A * This is for a simple command, for list, or compound assignment
1N/A */
1N/Astatic Shnode_t *simple(Lex_t *lexp,int flag, struct ionod *io)
1N/A{
1N/A register struct comnod *t;
1N/A register struct argnod *argp;
1N/A register int tok;
1N/A Stk_t *stkp = lexp->sh->stk;
1N/A struct argnod **argtail;
1N/A struct argnod **settail;
1N/A int cmdarg=0;
1N/A int argno = 0;
1N/A int assignment = 0;
1N/A int key_on = (!(flag&SH_NOIO) && sh_isoption(SH_KEYWORD));
1N/A int associative=0;
1N/A if((argp=lexp->arg) && (argp->argflag&ARG_ASSIGN) && argp->argval[0]=='[')
1N/A {
1N/A flag |= SH_ARRAY;
1N/A associative = 1;
1N/A }
1N/A t = (struct comnod*)getnode(comnod);
1N/A t->comio=io; /*initial io chain*/
1N/A /* set command line number for error messages */
1N/A t->comline = sh_getlineno(lexp);
1N/A argtail = &(t->comarg);
1N/A t->comset = 0;
1N/A t->comnamp = 0;
1N/A t->comnamq = 0;
1N/A t->comstate = 0;
1N/A settail = &(t->comset);
1N/A while(lexp->token==0)
1N/A {
1N/A argp = lexp->arg;
1N/A if(*argp->argval==LBRACE && (flag&SH_FUNDEF) && argp->argval[1]==0)
1N/A {
1N/A lexp->token = LBRACE;
1N/A break;
1N/A }
1N/A if(associative && argp->argval[0]!='[')
1N/A sh_syntax(lexp);
1N/A /* check for assignment argument */
1N/A if((argp->argflag&ARG_ASSIGN) && assignment!=2)
1N/A {
1N/A *settail = argp;
1N/A settail = &(argp->argnxt.ap);
1N/A lexp->assignok = (flag&SH_ASSIGN)?SH_ASSIGN:1;
1N/A if(assignment)
1N/A {
1N/A struct argnod *ap=argp;
1N/A char *last, *cp;
1N/A if(assignment==1)
1N/A {
1N/A last = strchr(argp->argval,'=');
1N/A if(last && (last[-1]==']'|| (last[-1]=='+' && last[-2]==']')) && (cp=strchr(argp->argval,'[')) && (cp < last))
1N/A last = cp;
1N/A stkseek(stkp,ARGVAL);
1N/A sfwrite(stkp,argp->argval,last-argp->argval);
1N/A ap=(struct argnod*)stkfreeze(stkp,1);
1N/A ap->argflag = ARG_RAW;
1N/A ap->argchn.ap = 0;
1N/A }
1N/A *argtail = ap;
1N/A argtail = &(ap->argnxt.ap);
1N/A if(argno>=0)
1N/A argno++;
1N/A }
1N/A else /* alias substitutions allowed */
1N/A lexp->aliasok = 1;
1N/A }
1N/A else
1N/A {
1N/A if(!(argp->argflag&ARG_RAW))
1N/A argno = -1;
1N/A if(argno>=0 && argno++==cmdarg && !(flag&SH_ARRAY) && *argp->argval!='/')
1N/A {
1N/A /* check for builtin command */
1N/A Namval_t *np=nv_bfsearch(argp->argval,lexp->sh->fun_tree, (Namval_t**)&t->comnamq,(char**)0);
1N/A if(cmdarg==0)
1N/A t->comnamp = (void*)np;
1N/A if(np && is_abuiltin(np))
1N/A {
1N/A if(nv_isattr(np,BLT_DCL))
1N/A {
1N/A assignment = 1+(*argp->argval=='a');
1N/A if(np==SYSTYPESET)
1N/A lexp->intypeset = 1;
1N/A key_on = 1;
1N/A }
1N/A else if(np==SYSCOMMAND)
1N/A cmdarg++;
1N/A else if(np==SYSEXEC)
1N/A lexp->inexec = 1;
1N/A else if(np->nvalue.bfp==b_getopts)
1N/A opt_get |= FOPTGET;
1N/A }
1N/A }
1N/A *argtail = argp;
1N/A argtail = &(argp->argnxt.ap);
1N/A if(!(lexp->assignok=key_on) && !(flag&SH_NOIO) && sh_isoption(SH_NOEXEC))
1N/A lexp->assignok = SH_COMPASSIGN;
1N/A lexp->aliasok = 0;
1N/A }
1N/A retry:
1N/A tok = sh_lex(lexp);
1N/A if(tok==LABLSYM && (flag&SH_ASSIGN))
1N/A lexp->token = tok = 0;
1N/A#if SHOPT_DEVFD
1N/A if((tok==IPROCSYM || tok==OPROCSYM))
1N/A {
1N/A argp = process_sub(lexp,tok);
1N/A argno = -1;
1N/A *argtail = argp;
1N/A argtail = &(argp->argnxt.ap);
1N/A goto retry;
1N/A }
1N/A#endif /* SHOPT_DEVFD */
1N/A if(tok==LPAREN)
1N/A {
1N/A if(argp->argflag&ARG_ASSIGN)
1N/A {
1N/A int intypeset = lexp->intypeset;
1N/A int type = 0;
1N/A lexp->intypeset = 0;
1N/A if(t->comnamp==SYSTYPESET)
1N/A {
1N/A struct argnod *ap;
1N/A for(ap=t->comarg->argnxt.ap;ap;ap=ap->argnxt.ap)
1N/A {
1N/A if(*ap->argval!='-')
1N/A break;
1N/A if(strchr(ap->argval,'T'))
1N/A type = NV_TYPE;
1N/A else if(strchr(ap->argval,'a'))
1N/A type = NV_ARRAY;
1N/A else
1N/A continue;
1N/A break;
1N/A }
1N/A }
1N/A argp = assign(lexp,argp,type);
1N/A lexp->intypeset = intypeset;
1N/A if(associative)
1N/A lexp->assignok |= SH_ASSIGN;
1N/A goto retry;
1N/A }
1N/A else if(argno==1 && !t->comset)
1N/A {
1N/A /* SVR2 style function */
1N/A if(!(flag&SH_ARRAY) && sh_lex(lexp) == RPAREN)
1N/A {
1N/A lexp->arg = argp;
1N/A return(funct(lexp));
1N/A }
1N/A lexp->token = LPAREN;
1N/A }
1N/A }
1N/A else if(flag&SH_ASSIGN)
1N/A {
1N/A if(tok==RPAREN)
1N/A break;
1N/A else if(tok==NL && (flag&SH_ARRAY))
1N/A {
1N/A lexp->comp_assign = 2;
1N/A goto retry;
1N/A }
1N/A
1N/A }
1N/A if(!(flag&SH_NOIO))
1N/A {
1N/A if(io)
1N/A {
1N/A while(io->ionxt)
1N/A io = io->ionxt;
1N/A io->ionxt = inout(lexp,(struct ionod*)0,0);
1N/A }
1N/A else
1N/A t->comio = io = inout(lexp,(struct ionod*)0,0);
1N/A }
1N/A }
1N/A *argtail = 0;
1N/A t->comtyp = TCOM;
1N/A#if SHOPT_KIA
1N/A if(lexp->kiafile && !(flag&SH_NOIO))
1N/A {
1N/A register Namval_t *np=(Namval_t*)t->comnamp;
1N/A unsigned long r=0;
1N/A int line = t->comline;
1N/A argp = t->comarg;
1N/A if(np)
1N/A r = kiaentity(lexp,nv_name(np),-1,'p',-1,0,lexp->unknown,'b',0,"");
1N/A else if(argp)
1N/A r = kiaentity(lexp,sh_argstr(argp),-1,'p',-1,0,lexp->unknown,'c',0,"");
1N/A if(r>0)
1N/A sfprintf(lexp->kiatmp,"p;%..64d;p;%..64d;%d;%d;c;\n",lexp->current,r,line,line);
1N/A if(t->comset && argno==0)
1N/A writedefs(lexp,t->comset,line,'v',t->comarg);
1N/A else if(np && nv_isattr(np,BLT_DCL))
1N/A writedefs(lexp,argp,line,0,NIL(struct argnod*));
1N/A else if(argp && strcmp(argp->argval,"read")==0)
1N/A writedefs(lexp,argp,line,0,NIL(struct argnod*));
1N/A#if 0
1N/A else if(argp && strcmp(argp->argval,"unset")==0)
1N/A writedefs(lexp,argp,line,'u',NIL(struct argnod*));
1N/A#endif
1N/A else if(argp && *argp->argval=='.' && argp->argval[1]==0 && (argp=argp->argnxt.ap))
1N/A {
1N/A r = kiaentity(lexp,sh_argstr(argp),-1,'p',0,0,lexp->script,'d',0,"");
1N/A sfprintf(lexp->kiatmp,"p;%..64d;p;%..64d;%d;%d;d;\n",lexp->current,r,line,line);
1N/A }
1N/A }
1N/A#endif /* SHOPT_KIA */
1N/A if(t->comnamp && (argp=t->comarg->argnxt.ap))
1N/A {
1N/A Namval_t *np=(Namval_t*)t->comnamp;
1N/A if((np==SYSBREAK || np==SYSCONT) && (argp->argflag&ARG_RAW) && !isdigit(*argp->argval))
1N/A {
1N/A register char *cp = argp->argval;
1N/A /* convert break/continue labels to numbers */
1N/A tok = 0;
1N/A for(argp=label_list;argp!=label_last;argp=argp->argnxt.ap)
1N/A {
1N/A if(strcmp(cp,argp->argval))
1N/A continue;
1N/A tok = loop_level-argp->argflag;
1N/A if(tok>=1)
1N/A {
1N/A argp = t->comarg->argnxt.ap;
1N/A if(tok>9)
1N/A {
1N/A argp->argval[1] = '0'+tok%10;
1N/A argp->argval[2] = 0;
1N/A tok /= 10;
1N/A }
1N/A else
1N/A argp->argval[1] = 0;
1N/A *argp->argval = '0'+tok;
1N/A }
1N/A break;
1N/A }
1N/A if(sh_isoption(SH_NOEXEC) && tok==0)
1N/A errormsg(SH_DICT,ERROR_warn(0),e_lexlabunknown,lexp->sh->inlineno-(lexp->token=='\n'),cp);
1N/A }
1N/A else if(sh_isoption(SH_NOEXEC) && np==SYSSET && ((tok= *argp->argval)=='-'||tok=='+') &&
1N/A (argp->argval[1]==0||strchr(argp->argval,'k')))
1N/A errormsg(SH_DICT,ERROR_warn(0),e_lexobsolete5,lexp->sh->inlineno-(lexp->token=='\n'),argp->argval);
1N/A }
1N/A /* expand argument list if possible */
1N/A if(argno>0 && !(flag&SH_ARRAY))
1N/A t->comarg = qscan(t,argno);
1N/A else if(t->comarg)
1N/A t->comtyp |= COMSCAN;
1N/A lexp->aliasok = 0;
1N/A return((Shnode_t*)t);
1N/A}
1N/A
1N/A/*
1N/A * skip past newlines but issue prompt if interactive
1N/A */
1N/Astatic int skipnl(Lex_t *lexp,int flag)
1N/A{
1N/A register int token;
1N/A while((token=sh_lex(lexp))==NL);
1N/A if(token==';' && !(flag&SH_SEMI))
1N/A sh_syntax(lexp);
1N/A return(token);
1N/A}
1N/A
1N/A/*
1N/A * check for and process and i/o redirections
1N/A * if flag>0 then an alias can be in the next word
1N/A * if flag<0 only one redirection will be processed
1N/A */
1N/Astatic struct ionod *inout(Lex_t *lexp,struct ionod *lastio,int flag)
1N/A{
1N/A register int iof = lexp->digits, token=lexp->token;
1N/A register struct ionod *iop;
1N/A Stk_t *stkp = lexp->sh->stk;
1N/A char *iovname=0;
1N/A register int errout=0;
1N/A if(token==IOVNAME)
1N/A {
1N/A iovname=lexp->arg->argval+1;
1N/A token= sh_lex(lexp);
1N/A iof = 0;
1N/A }
1N/A switch(token&0xff)
1N/A {
1N/A case '<':
1N/A if(token==IODOCSYM)
1N/A iof |= (IODOC|IORAW);
1N/A else if(token==IOMOV0SYM)
1N/A iof |= IOMOV;
1N/A else if(token==IORDWRSYMT)
1N/A iof |= IORDW|IOREWRITE;
1N/A else if(token==IORDWRSYM)
1N/A iof |= IORDW;
1N/A else if((token&SYMSHARP) == SYMSHARP)
1N/A {
1N/A int n;
1N/A iof |= IOLSEEK;
1N/A if(fcgetc(n)=='#')
1N/A iof |= IOCOPY;
1N/A else if(n>0)
1N/A fcseek(-1);
1N/A }
1N/A break;
1N/A
1N/A case '>':
1N/A if(iof<0)
1N/A {
1N/A errout = 1;
1N/A iof = 1;
1N/A }
1N/A iof |= IOPUT;
1N/A if(token==IOAPPSYM)
1N/A iof |= IOAPP;
1N/A else if(token==IOMOV1SYM)
1N/A iof |= IOMOV;
1N/A else if(token==IOCLOBSYM)
1N/A iof |= IOCLOB;
1N/A else if((token&SYMSHARP) == SYMSHARP)
1N/A iof |= IOLSEEK;
1N/A else if((token&SYMSEMI) == SYMSEMI)
1N/A iof |= IOREWRITE;
1N/A break;
1N/A
1N/A default:
1N/A return(lastio);
1N/A }
1N/A lexp->digits=0;
1N/A iop=(struct ionod*) stkalloc(stkp,sizeof(struct ionod));
1N/A iop->iodelim = 0;
1N/A if(token=sh_lex(lexp))
1N/A {
1N/A if(token==RPAREN && (iof&IOLSEEK) && lexp->comsub)
1N/A {
1N/A lexp->arg = (struct argnod*)stkalloc(stkp,sizeof(struct argnod)+3);
1N/A strcpy(lexp->arg->argval,"CUR");
1N/A lexp->arg->argflag = ARG_RAW;
1N/A iof |= IOARITH;
1N/A fcseek(-1);
1N/A }
1N/A else if(token==EXPRSYM && (iof&IOLSEEK))
1N/A iof |= IOARITH;
1N/A else if(((token==IPROCSYM && !(iof&IOPUT)) || (token==OPROCSYM && (iof&IOPUT))) && !(iof&(IOLSEEK|IOREWRITE|IOMOV|IODOC)))
1N/A {
1N/A lexp->arg = process_sub(lexp,token);
1N/A iof |= IOPROCSUB;
1N/A }
1N/A else
1N/A sh_syntax(lexp);
1N/A }
1N/A if( (iof&IOPROCSUB) && !(iof&IOLSEEK))
1N/A iop->ioname= (char*)lexp->arg->argchn.ap;
1N/A else
1N/A iop->ioname=lexp->arg->argval;
1N/A iop->iovname = iovname;
1N/A if(iof&IODOC)
1N/A {
1N/A if(lexp->digits==2)
1N/A {
1N/A iof |= IOSTRG;
1N/A if(!(lexp->arg->argflag&ARG_RAW))
1N/A iof &= ~IORAW;
1N/A }
1N/A else
1N/A {
1N/A if(!lexp->sh->heredocs)
1N/A lexp->sh->heredocs = sftmp(HERE_MEM);
1N/A iop->iolst=lexp->heredoc;
1N/A lexp->heredoc=iop;
1N/A if(lexp->arg->argflag&ARG_QUOTED)
1N/A iof |= IOQUOTE;
1N/A if(lexp->digits==3)
1N/A iof |= IOLSEEK;
1N/A if(lexp->digits)
1N/A iof |= IOSTRIP;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A iop->iolst = 0;
1N/A if(lexp->arg->argflag&ARG_RAW)
1N/A iof |= IORAW;
1N/A }
1N/A iop->iofile=iof;
1N/A if(flag>0)
1N/A /* allow alias substitutions and parameter assignments */
1N/A lexp->aliasok = lexp->assignok = 1;
1N/A#if SHOPT_KIA
1N/A if(lexp->kiafile)
1N/A {
1N/A int n = lexp->sh->inlineno-(lexp->token=='\n');
1N/A if(!(iof&IOMOV))
1N/A {
1N/A unsigned long r=kiaentity(lexp,(iof&IORAW)?sh_fmtq(iop->ioname):iop->ioname,-1,'f',0,0,lexp->script,'f',0,"");
1N/A sfprintf(lexp->kiatmp,"p;%..64d;f;%..64d;%d;%d;%c;%d\n",lexp->current,r,n,n,(iof&IOPUT)?((iof&IOAPP)?'a':'w'):((iof&IODOC)?'h':'r'),iof&IOUFD);
1N/A }
1N/A }
1N/A#endif /* SHOPT_KIA */
1N/A if(flag>=0)
1N/A {
1N/A struct ionod *ioq=iop;
1N/A sh_lex(lexp);
1N/A if(errout)
1N/A {
1N/A /* redirect standard output to standard error */
1N/A ioq = (struct ionod*)stkalloc(stkp,sizeof(struct ionod));
1N/A memset(ioq,0,sizeof(*ioq));
1N/A ioq->ioname = "1";
1N/A ioq->iolst = 0;
1N/A ioq->iodelim = 0;
1N/A ioq->iofile = IORAW|IOPUT|IOMOV|2;
1N/A iop->ionxt=ioq;
1N/A }
1N/A ioq->ionxt=inout(lexp,lastio,flag);
1N/A }
1N/A else
1N/A iop->ionxt=0;
1N/A return(iop);
1N/A}
1N/A
1N/A/*
1N/A * convert argument chain to argument list when no special arguments
1N/A */
1N/A
1N/Astatic struct argnod *qscan(struct comnod *ac,int argn)
1N/A{
1N/A register char **cp;
1N/A register struct argnod *ap;
1N/A register struct dolnod* dp;
1N/A register int special=0;
1N/A /* special hack for test -t compatibility */
1N/A if((Namval_t*)ac->comnamp==SYSTEST)
1N/A special = 2;
1N/A else if(*(ac->comarg->argval)=='[' && ac->comarg->argval[1]==0)
1N/A special = 3;
1N/A if(special)
1N/A {
1N/A ap = ac->comarg->argnxt.ap;
1N/A if(argn==(special+1) && ap->argval[1]==0 && *ap->argval=='!')
1N/A ap = ap->argnxt.ap;
1N/A else if(argn!=special)
1N/A special=0;
1N/A }
1N/A if(special)
1N/A {
1N/A const char *message;
1N/A if(strcmp(ap->argval,"-t"))
1N/A {
1N/A message = "line %d: Invariant test";
1N/A special=0;
1N/A }
1N/A else
1N/A {
1N/A message = "line %d: -t requires argument";
1N/A argn++;
1N/A }
1N/A if(sh_isoption(SH_NOEXEC))
1N/A errormsg(SH_DICT,ERROR_warn(0),message,ac->comline);
1N/A }
1N/A /* leave space for an extra argument at the front */
1N/A dp = (struct dolnod*)stakalloc((unsigned)sizeof(struct dolnod) + ARG_SPARE*sizeof(char*) + argn*sizeof(char*));
1N/A cp = dp->dolval+ARG_SPARE;
1N/A dp->dolnum = argn;
1N/A dp->dolbot = ARG_SPARE;
1N/A ap = ac->comarg;
1N/A while(ap)
1N/A {
1N/A *cp++ = ap->argval;
1N/A ap = ap->argnxt.ap;
1N/A }
1N/A if(special==3)
1N/A {
1N/A cp[0] = cp[-1];
1N/A cp[-1] = "1";
1N/A cp++;
1N/A }
1N/A else if(special)
1N/A *cp++ = "1";
1N/A *cp = 0;
1N/A return((struct argnod*)dp);
1N/A}
1N/A
1N/Astatic Shnode_t *test_expr(Lex_t *lp,int sym)
1N/A{
1N/A register Shnode_t *t = test_or(lp);
1N/A if(lp->token!=sym)
1N/A sh_syntax(lp);
1N/A return(t);
1N/A}
1N/A
1N/Astatic Shnode_t *test_or(Lex_t *lp)
1N/A{
1N/A register Shnode_t *t = test_and(lp);
1N/A while(lp->token==ORFSYM)
1N/A t = makelist(lp,TORF|TTEST,t,test_and(lp));
1N/A return(t);
1N/A}
1N/A
1N/Astatic Shnode_t *test_and(Lex_t *lp)
1N/A{
1N/A register Shnode_t *t = test_primary(lp);
1N/A while(lp->token==ANDFSYM)
1N/A t = makelist(lp,TAND|TTEST,t,test_primary(lp));
1N/A return(t);
1N/A}
1N/A
1N/A/*
1N/A * convert =~ into == ~(E)
1N/A */
1N/Astatic void ere_match(void)
1N/A{
1N/A Sfio_t *base, *iop = sfopen((Sfio_t*)0," ~(E)","s");
1N/A register int c;
1N/A while( fcgetc(c),(c==' ' || c=='\t'));
1N/A if(c)
1N/A fcseek(-1);
1N/A if(!(base=fcfile()))
1N/A base = sfopen(NIL(Sfio_t*),fcseek(0),"s");
1N/A fcclose();
1N/A sfstack(base,iop);
1N/A fcfopen(base);
1N/A}
1N/A
1N/Astatic Shnode_t *test_primary(Lex_t *lexp)
1N/A{
1N/A register struct argnod *arg;
1N/A register Shnode_t *t;
1N/A register int num,token;
1N/A token = skipnl(lexp,0);
1N/A num = lexp->digits;
1N/A switch(token)
1N/A {
1N/A case '(':
1N/A t = test_expr(lexp,')');
1N/A t = makelist(lexp,TTST|TTEST|TPAREN ,t, (Shnode_t*)pointerof(lexp->sh->inlineno));
1N/A break;
1N/A case '!':
1N/A if(!(t = test_primary(lexp)))
1N/A sh_syntax(lexp);
1N/A t->tre.tretyp |= TNEGATE;
1N/A return(t);
1N/A case TESTUNOP:
1N/A if(sh_lex(lexp))
1N/A sh_syntax(lexp);
1N/A#if SHOPT_KIA
1N/A if(lexp->kiafile && !strchr("sntzoOG",num))
1N/A {
1N/A int line = lexp->sh->inlineno- (lexp->token==NL);
1N/A unsigned long r;
1N/A r=kiaentity(lexp,sh_argstr(lexp->arg),-1,'f',0,0,lexp->script,'t',0,"");
1N/A sfprintf(lexp->kiatmp,"p;%..64d;f;%..64d;%d;%d;t;\n",lexp->current,r,line,line);
1N/A }
1N/A#endif /* SHOPT_KIA */
1N/A t = makelist(lexp,TTST|TTEST|TUNARY|(num<<TSHIFT),
1N/A (Shnode_t*)lexp->arg,(Shnode_t*)lexp->arg);
1N/A t->tst.tstline = lexp->sh->inlineno;
1N/A break;
1N/A /* binary test operators */
1N/A case 0:
1N/A arg = lexp->arg;
1N/A if((token=sh_lex(lexp))==TESTBINOP)
1N/A {
1N/A num = lexp->digits;
1N/A if(num==TEST_REP)
1N/A {
1N/A ere_match();
1N/A num = TEST_PEQ;
1N/A }
1N/A }
1N/A else if(token=='<')
1N/A num = TEST_SLT;
1N/A else if(token=='>')
1N/A num = TEST_SGT;
1N/A else if(token==ANDFSYM||token==ORFSYM||token==ETESTSYM||token==RPAREN)
1N/A {
1N/A t = makelist(lexp,TTST|TTEST|TUNARY|('n'<<TSHIFT),
1N/A (Shnode_t*)arg,(Shnode_t*)arg);
1N/A t->tst.tstline = lexp->sh->inlineno;
1N/A return(t);
1N/A }
1N/A else
1N/A sh_syntax(lexp);
1N/A#if SHOPT_KIA
1N/A if(lexp->kiafile && (num==TEST_EF||num==TEST_NT||num==TEST_OT))
1N/A {
1N/A int line = lexp->sh->inlineno- (lexp->token==NL);
1N/A unsigned long r;
1N/A r=kiaentity(lexp,sh_argstr(lexp->arg),-1,'f',0,0,lexp->current,'t',0,"");
1N/A sfprintf(lexp->kiatmp,"p;%..64d;f;%..64d;%d;%d;t;\n",lexp->current,r,line,line);
1N/A }
1N/A#endif /* SHOPT_KIA */
1N/A if(sh_lex(lexp))
1N/A sh_syntax(lexp);
1N/A if(num&TEST_PATTERN)
1N/A {
1N/A if(lexp->arg->argflag&(ARG_EXP|ARG_MAC))
1N/A num &= ~TEST_PATTERN;
1N/A }
1N/A t = getnode(tstnod);
1N/A t->lst.lsttyp = TTST|TTEST|TBINARY|(num<<TSHIFT);
1N/A t->lst.lstlef = (Shnode_t*)arg;
1N/A t->lst.lstrit = (Shnode_t*)lexp->arg;
1N/A t->tst.tstline = lexp->sh->inlineno;
1N/A#if SHOPT_KIA
1N/A if(lexp->kiafile && (num==TEST_EF||num==TEST_NT||num==TEST_OT))
1N/A {
1N/A int line = lexp->sh->inlineno-(lexp->token==NL);
1N/A unsigned long r;
1N/A r=kiaentity(lexp,sh_argstr(lexp->arg),-1,'f',0,0,lexp->current,'t',0,"");
1N/A sfprintf(lexp->kiatmp,"p;%..64d;f;%..64d;%d;%d;t;\n",lexp->current,r,line,line);
1N/A }
1N/A#endif /* SHOPT_KIA */
1N/A break;
1N/A default:
1N/A return(0);
1N/A }
1N/A skipnl(lexp,0);
1N/A return(t);
1N/A}
1N/A
1N/A#if SHOPT_KIA
1N/A/*
1N/A * return an entity checksum
1N/A * The entity is created if it doesn't exist
1N/A */
1N/Aunsigned long kiaentity(Lex_t *lexp,const char *name,int len,int type,int first,int last,unsigned long parent, int pkind, int width, const char *attr)
1N/A{
1N/A Stk_t *stkp = lexp->sh->stk;
1N/A Namval_t *np;
1N/A long offset = stktell(stkp);
1N/A sfputc(stkp,type);
1N/A if(len>0)
1N/A sfwrite(stkp,name,len);
1N/A else
1N/A {
1N/A if(type=='p')
1N/A sfputr(stkp,path_basename(name),0);
1N/A else
1N/A sfputr(stkp,name,0);
1N/A }
1N/A np = nv_search(stakptr(offset),lexp->entity_tree,NV_ADD);
1N/A stkseek(stkp,offset);
1N/A np->nvalue.i = pkind;
1N/A nv_setsize(np,width);
1N/A if(!nv_isattr(np,NV_TAGGED) && first>=0)
1N/A {
1N/A nv_onattr(np,NV_TAGGED);
1N/A if(!pkind)
1N/A pkind = '0';
1N/A if(len>0)
1N/A sfprintf(lexp->kiafile,"%..64d;%c;%.*s;%d;%d;%..64d;%..64d;%c;%d;%s\n",np->hash,type,len,name,first,last,parent,lexp->fscript,pkind,width,attr);
1N/A else
1N/A sfprintf(lexp->kiafile,"%..64d;%c;%s;%d;%d;%..64d;%..64d;%c;%d;%s\n",np->hash,type,name,first,last,parent,lexp->fscript,pkind,width,attr);
1N/A }
1N/A return(np->hash);
1N/A}
1N/A
1N/Astatic void kia_add(register Namval_t *np, void *data)
1N/A{
1N/A char *name = nv_name(np);
1N/A Lex_t *lp = (Lex_t*)data;
1N/A NOT_USED(data);
1N/A kiaentity(lp,name+1,-1,*name,0,-1,(*name=='p'?lp->unknown:lp->script),np->nvalue.i,nv_size(np),"");
1N/A}
1N/A
1N/Aint kiaclose(Lex_t *lexp)
1N/A{
1N/A register off_t off1,off2;
1N/A register int n;
1N/A if(lexp->kiafile)
1N/A {
1N/A unsigned long r = kiaentity(lexp,lexp->scriptname,-1,'p',-1,lexp->sh->inlineno-1,0,'s',0,"");
1N/A kiaentity(lexp,lexp->scriptname,-1,'p',1,lexp->sh->inlineno-1,r,'s',0,"");
1N/A kiaentity(lexp,lexp->scriptname,-1,'f',1,lexp->sh->inlineno-1,r,'s',0,"");
1N/A nv_scan(lexp->entity_tree,kia_add,(void*)lexp,NV_TAGGED,0);
1N/A off1 = sfseek(lexp->kiafile,(off_t)0,SEEK_END);
1N/A sfseek(lexp->kiatmp,(off_t)0,SEEK_SET);
1N/A sfmove(lexp->kiatmp,lexp->kiafile,SF_UNBOUND,-1);
1N/A off2 = sfseek(lexp->kiafile,(off_t)0,SEEK_END);
1N/A#ifdef SF_BUFCONST
1N/A if(off2==off1)
1N/A n= sfprintf(lexp->kiafile,"DIRECTORY\nENTITY;%lld;%d\nDIRECTORY;",(Sflong_t)lexp->kiabegin,(size_t)(off1-lexp->kiabegin));
1N/A else
1N/A n= sfprintf(lexp->kiafile,"DIRECTORY\nENTITY;%lld;%d\nRELATIONSHIP;%lld;%d\nDIRECTORY;",(Sflong_t)lexp->kiabegin,(size_t)(off1-lexp->kiabegin),(Sflong_t)off1,(size_t)(off2-off1));
1N/A if(off2 >= INT_MAX)
1N/A off2 = -(n+12);
1N/A sfprintf(lexp->kiafile,"%010.10lld;%010d\n",(Sflong_t)off2+10, n+12);
1N/A#else
1N/A if(off2==off1)
1N/A n= sfprintf(lexp->kiafile,"DIRECTORY\nENTITY;%d;%d\nDIRECTORY;",lexp->kiabegin,off1-lexp->kiabegin);
1N/A else
1N/A n= sfprintf(lexp->kiafile,"DIRECTORY\nENTITY;%d;%d\nRELATIONSHIP;%d;%d\nDIRECTORY;",lexp->kiabegin,off1-lexp->kiabegin,off1,off2-off1);
1N/A sfprintf(lexp->kiafile,"%010d;%010d\n",off2+10, n+12);
1N/A#endif
1N/A }
1N/A return(sfclose(lexp->kiafile));
1N/A}
1N/A#endif /* SHOPT_KIA */