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 * David Korn
1N/A * AT&T Labs
1N/A *
1N/A * shell intermediate code reader
1N/A *
1N/A */
1N/A
1N/A#include "defs.h"
1N/A#include "shnodes.h"
1N/A#include "path.h"
1N/A#include "io.h"
1N/A#include <ccode.h>
1N/A
1N/Astatic struct dolnod *r_comlist(Shell_t*);
1N/Astatic struct argnod *r_arg(Shell_t*);
1N/Astatic struct ionod *r_redirect(Shell_t*);
1N/Astatic struct regnod *r_switch(Shell_t*);
1N/Astatic Shnode_t *r_tree(Shell_t*);
1N/Astatic char *r_string(Stk_t*);
1N/Astatic void r_comarg(Shell_t*,struct comnod*);
1N/A
1N/Astatic Sfio_t *infile;
1N/A
1N/A#define getnode(s,type) ((Shnode_t*)stkalloc((s),sizeof(struct type)))
1N/A
1N/AShnode_t *sh_trestore(Shell_t *shp,Sfio_t *in)
1N/A{
1N/A Shnode_t *t;
1N/A infile = in;
1N/A t = r_tree(shp);
1N/A return(t);
1N/A}
1N/A/*
1N/A * read in a shell tree
1N/A */
1N/Astatic Shnode_t *r_tree(Shell_t *shp)
1N/A{
1N/A long l = sfgetl(infile);
1N/A register int type;
1N/A register Shnode_t *t=0;
1N/A if(l<0)
1N/A return(t);
1N/A type = l;
1N/A switch(type&COMMSK)
1N/A {
1N/A case TTIME:
1N/A case TPAR:
1N/A t = getnode(shp->stk,parnod);
1N/A t->par.partre = r_tree(shp);
1N/A break;
1N/A case TCOM:
1N/A t = getnode(shp->stk,comnod);
1N/A t->tre.tretyp = type;
1N/A r_comarg(shp,(struct comnod*)t);
1N/A break;
1N/A case TSETIO:
1N/A case TFORK:
1N/A t = getnode(shp->stk,forknod);
1N/A t->fork.forkline = sfgetu(infile);
1N/A t->fork.forktre = r_tree(shp);
1N/A t->fork.forkio = r_redirect(shp);
1N/A break;
1N/A case TIF:
1N/A t = getnode(shp->stk,ifnod);
1N/A t->if_.iftre = r_tree(shp);
1N/A t->if_.thtre = r_tree(shp);
1N/A t->if_.eltre = r_tree(shp);
1N/A break;
1N/A case TWH:
1N/A t = getnode(shp->stk,whnod);
1N/A t->wh.whinc = (struct arithnod*)r_tree(shp);
1N/A t->wh.whtre = r_tree(shp);
1N/A t->wh.dotre = r_tree(shp);
1N/A break;
1N/A case TLST:
1N/A case TAND:
1N/A case TORF:
1N/A case TFIL:
1N/A t = getnode(shp->stk,lstnod);
1N/A t->lst.lstlef = r_tree(shp);
1N/A t->lst.lstrit = r_tree(shp);
1N/A break;
1N/A case TARITH:
1N/A t = getnode(shp->stk,arithnod);
1N/A t->ar.arline = sfgetu(infile);
1N/A t->ar.arexpr = r_arg(shp);
1N/A t->ar.arcomp = 0;
1N/A if((t->ar.arexpr)->argflag&ARG_RAW)
1N/A t->ar.arcomp = sh_arithcomp(shp,(t->ar.arexpr)->argval);
1N/A break;
1N/A case TFOR:
1N/A t = getnode(shp->stk,fornod);
1N/A t->for_.forline = 0;
1N/A if(type&FLINENO)
1N/A t->for_.forline = sfgetu(infile);
1N/A t->for_.fortre = r_tree(shp);
1N/A t->for_.fornam = r_string(shp->stk);
1N/A t->for_.forlst = (struct comnod*)r_tree(shp);
1N/A break;
1N/A case TSW:
1N/A t = getnode(shp->stk,swnod);
1N/A t->sw.swline = 0;
1N/A if(type&FLINENO)
1N/A t->sw.swline = sfgetu(infile);
1N/A t->sw.swarg = r_arg(shp);
1N/A if(type&COMSCAN)
1N/A t->sw.swio = r_redirect(shp);
1N/A else
1N/A t->sw.swio = 0;
1N/A t->sw.swlst = r_switch(shp);
1N/A break;
1N/A case TFUN:
1N/A {
1N/A Stak_t *savstak;
1N/A struct slnod *slp;
1N/A struct functnod *fp;
1N/A t = getnode(shp->stk,functnod);
1N/A t->funct.functloc = -1;
1N/A t->funct.functline = sfgetu(infile);
1N/A t->funct.functnam = r_string(shp->stk);
1N/A savstak = stakcreate(STAK_SMALL);
1N/A savstak = stakinstall(savstak, 0);
1N/A slp = (struct slnod*)stkalloc(shp->stk,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 fp = (struct functnod*)(slp+1);
1N/A memset(fp, 0, sizeof(*fp));
1N/A fp->functtyp = TFUN|FAMP;
1N/A if(shp->st.filename)
1N/A fp->functnam = stkcopy(shp->stk,shp->st.filename);
1N/A t->funct.functtre = r_tree(shp);
1N/A t->funct.functstak = slp;
1N/A t->funct.functargs = (struct comnod*)r_tree(shp);
1N/A slp->slptr = stakinstall(savstak,0);
1N/A slp->slchild = shp->st.staklist;
1N/A break;
1N/A }
1N/A case TTST:
1N/A t = getnode(shp->stk,tstnod);
1N/A t->tst.tstline = sfgetu(infile);
1N/A if((type&TPAREN)==TPAREN)
1N/A t->lst.lstlef = r_tree(shp);
1N/A else
1N/A {
1N/A t->lst.lstlef = (Shnode_t*)r_arg(shp);
1N/A if((type&TBINARY))
1N/A t->lst.lstrit = (Shnode_t*)r_arg(shp);
1N/A }
1N/A }
1N/A if(t)
1N/A t->tre.tretyp = type;
1N/A return(t);
1N/A}
1N/A
1N/Astatic struct argnod *r_arg(Shell_t *shp)
1N/A{
1N/A register struct argnod *ap=0, *apold, *aptop=0;
1N/A register long l;
1N/A Stk_t *stkp=shp->stk;
1N/A while((l=sfgetu(infile))>0)
1N/A {
1N/A ap = (struct argnod*)stkseek(stkp,(unsigned)l+ARGVAL);
1N/A if(!aptop)
1N/A aptop = ap;
1N/A else
1N/A apold->argnxt.ap = ap;
1N/A if(--l > 0)
1N/A {
1N/A sfread(infile,ap->argval,(size_t)l);
1N/A ccmaps(ap->argval, l, CC_ASCII, CC_NATIVE);
1N/A }
1N/A ap->argval[l] = 0;
1N/A ap->argchn.cp = 0;
1N/A ap->argflag = sfgetc(infile);
1N/A#if 0
1N/A if((ap->argflag&ARG_MESSAGE) && *ap->argval)
1N/A {
1N/A /* replace international messages */
1N/A sh_endword(shp,1);
1N/A ap->argflag &= ~ARG_MESSAGE;
1N/A if(!(ap->argflag&(ARG_MAC|ARG_EXP)))
1N/A ap = sh_endword(shp,0);
1N/A else
1N/A {
1N/A ap = (struct argnod*)stkfreeze(stkp,0);
1N/A if(ap->argflag==0)
1N/A ap->argflag = ARG_RAW;
1N/A }
1N/A }
1N/A else
1N/A#endif
1N/A ap = (struct argnod*)stkfreeze(stkp,0);
1N/A if(*ap->argval==0 && (ap->argflag&~(ARG_APPEND|ARG_MESSAGE|ARG_QUOTED))==0)
1N/A {
1N/A struct fornod *fp = (struct fornod*)getnode(shp->stk,fornod);
1N/A fp->fortyp = sfgetu(infile);
1N/A fp->fortre = r_tree(shp);
1N/A fp->fornam = ap->argval+1;
1N/A ap->argchn.ap = (struct argnod*)fp;
1N/A }
1N/A apold = ap;
1N/A }
1N/A if(ap)
1N/A ap->argnxt.ap = 0;
1N/A return(aptop);
1N/A}
1N/A
1N/Astatic struct ionod *r_redirect(Shell_t* shp)
1N/A{
1N/A register long l;
1N/A register struct ionod *iop=0, *iopold, *ioptop=0;
1N/A while((l=sfgetl(infile))>=0)
1N/A {
1N/A iop = (struct ionod*)getnode(shp->stk,ionod);
1N/A if(!ioptop)
1N/A ioptop = iop;
1N/A else
1N/A iopold->ionxt = iop;
1N/A iop->iofile = l;
1N/A iop->ioname = r_string(shp->stk);
1N/A if(iop->iodelim = r_string(shp->stk))
1N/A {
1N/A iop->iosize = sfgetl(infile);
1N/A if(shp->heredocs)
1N/A iop->iooffset = sfseek(shp->heredocs,(off_t)0,SEEK_END);
1N/A else
1N/A {
1N/A shp->heredocs = sftmp(512);
1N/A iop->iooffset = 0;
1N/A }
1N/A sfmove(infile,shp->heredocs, iop->iosize, -1);
1N/A }
1N/A iopold = iop;
1N/A if(iop->iofile&IOVNM)
1N/A iop->iovname = r_string(shp->stk);
1N/A else
1N/A iop->iovname = 0;
1N/A iop->iofile &= ~IOVNM;
1N/A }
1N/A if(iop)
1N/A iop->ionxt = 0;
1N/A return(ioptop);
1N/A}
1N/A
1N/Astatic void r_comarg(Shell_t *shp,struct comnod *com)
1N/A{
1N/A char *cmdname=0;
1N/A com->comio = r_redirect(shp);
1N/A com->comset = r_arg(shp);
1N/A com->comstate = 0;
1N/A if(com->comtyp&COMSCAN)
1N/A {
1N/A com->comarg = r_arg(shp);
1N/A if(com->comarg->argflag==ARG_RAW)
1N/A cmdname = com->comarg->argval;
1N/A }
1N/A else if(com->comarg = (struct argnod*)r_comlist(shp))
1N/A cmdname = ((struct dolnod*)(com->comarg))->dolval[ARG_SPARE];
1N/A com->comline = sfgetu(infile);
1N/A com->comnamq = 0;
1N/A if(cmdname)
1N/A {
1N/A char *cp;
1N/A com->comnamp = (void*)nv_search(cmdname,shp->fun_tree,0);
1N/A if(com->comnamp && (cp =strrchr(cmdname+1,'.')))
1N/A {
1N/A *cp = 0;
1N/A com->comnamp = (void*)nv_open(cmdname,shp->var_tree,NV_VARNAME|NV_NOADD|NV_NOARRAY);
1N/A *cp = '.';
1N/A }
1N/A }
1N/A else
1N/A com->comnamp = 0;
1N/A}
1N/A
1N/Astatic struct dolnod *r_comlist(Shell_t *shp)
1N/A{
1N/A register struct dolnod *dol=0;
1N/A register long l;
1N/A register char **argv;
1N/A if((l=sfgetl(infile))>0)
1N/A {
1N/A dol = (struct dolnod*)stkalloc(shp->stk,sizeof(struct dolnod) + sizeof(char*)*(l+ARG_SPARE));
1N/A dol->dolnum = l;
1N/A dol->dolbot = ARG_SPARE;
1N/A argv = dol->dolval+ARG_SPARE;
1N/A while(*argv++ = r_string(shp->stk));
1N/A }
1N/A return(dol);
1N/A}
1N/A
1N/Astatic struct regnod *r_switch(Shell_t *shp)
1N/A{
1N/A register long l;
1N/A struct regnod *reg=0,*regold,*regtop=0;
1N/A while((l=sfgetl(infile))>=0)
1N/A {
1N/A reg = (struct regnod*)getnode(shp->stk,regnod);
1N/A if(!regtop)
1N/A regtop = reg;
1N/A else
1N/A regold->regnxt = reg;
1N/A reg->regflag = l;
1N/A reg->regptr = r_arg(shp);
1N/A reg->regcom = r_tree(shp);
1N/A regold = reg;
1N/A }
1N/A if(reg)
1N/A reg->regnxt = 0;
1N/A return(regtop);
1N/A}
1N/A
1N/Astatic char *r_string(Stk_t *stkp)
1N/A{
1N/A register Sfio_t *in = infile;
1N/A register unsigned long l = sfgetu(in);
1N/A register char *ptr;
1N/A if(l == 0)
1N/A return(NIL(char*));
1N/A ptr = stkalloc(stkp,(unsigned)l);
1N/A if(--l > 0)
1N/A {
1N/A if(sfread(in,ptr,(size_t)l)!=(size_t)l)
1N/A return(NIL(char*));
1N/A ccmaps(ptr, l, CC_ASCII, CC_NATIVE);
1N/A }
1N/A ptr[l] = 0;
1N/A return(ptr);
1N/A}