1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1982-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * David Korn <dgk@research.att.com> *
18 * *
19 ***********************************************************************/
20 #pragma prototyped
21 /*
22 * David Korn
23 * AT&T Labs
24 *
25 * shell parse tree dump
26 *
27 */
28
29 #include "defs.h"
30 #include "shnodes.h"
31 #include "path.h"
32 #include "io.h"
33 #include <ccode.h>
34
35 static int p_comlist(const struct dolnod*);
36 static int p_arg(const struct argnod*);
37 static int p_comarg(const struct comnod*);
38 static int p_redirect(const struct ionod*);
39 static int p_switch(const struct regnod*);
40 static int p_tree(const Shnode_t*);
41 static int p_string(const char*);
42
43 static Sfio_t *outfile;
44
sh_tdump(Sfio_t * out,const Shnode_t * t)45 int sh_tdump(Sfio_t *out, const Shnode_t *t)
46 {
47 outfile = out;
48 return(p_tree(t));
49 }
50
51 /*
52 * convert to ASCII to write and back again if needed
53 */
outstring(Sfio_t * out,const char * string,int n)54 static int outstring(Sfio_t *out, const char *string, int n)
55 {
56 int r;
57 char *cp = (char*)string;
58 ccmaps(cp, n, CC_NATIVE, CC_ASCII);
59 r = sfwrite(out,cp,n);
60 ccmaps(cp, n, CC_ASCII, CC_NATIVE);
61 return(r);
62 }
63
64 /*
65 * print script corresponding to shell tree <t>
66 */
p_tree(register const Shnode_t * t)67 static int p_tree(register const Shnode_t *t)
68 {
69 if(!t)
70 return(sfputl(outfile,-1));
71 if(sfputl(outfile,t->tre.tretyp)<0)
72 return(-1);
73 switch(t->tre.tretyp&COMMSK)
74 {
75 case TTIME:
76 case TPAR:
77 return(p_tree(t->par.partre));
78 case TCOM:
79 return(p_comarg((struct comnod*)t));
80 case TSETIO:
81 case TFORK:
82 if(sfputu(outfile,t->fork.forkline)<0)
83 return(-1);
84 if(p_tree(t->fork.forktre)<0)
85 return(-1);
86 return(p_redirect(t->fork.forkio));
87 case TIF:
88 if(p_tree(t->if_.iftre)<0)
89 return(-1);
90 if(p_tree(t->if_.thtre)<0)
91 return(-1);
92 return(p_tree(t->if_.eltre));
93 case TWH:
94 if(t->wh.whinc)
95 {
96 if(p_tree((Shnode_t*)(t->wh.whinc))<0)
97 return(-1);
98 }
99 else
100 {
101 if(sfputl(outfile,-1)<0)
102 return(-1);
103 }
104 if(p_tree(t->wh.whtre)<0)
105 return(-1);
106 return(p_tree(t->wh.dotre));
107 case TLST:
108 case TAND:
109 case TORF:
110 case TFIL:
111 if(p_tree(t->lst.lstlef)<0)
112 return(-1);
113 return(p_tree(t->lst.lstrit));
114 case TARITH:
115 if(sfputu(outfile,t->ar.arline)<0)
116 return(-1);
117 return(p_arg(t->ar.arexpr));
118 case TFOR:
119 if(sfputu(outfile,t->for_.forline)<0)
120 return(-1);
121 if(p_tree(t->for_.fortre)<0)
122 return(-1);
123 if(p_string(t->for_.fornam)<0)
124 return(-1);
125 return(p_tree((Shnode_t*)t->for_.forlst));
126 case TSW:
127 if(sfputu(outfile,t->sw.swline)<0)
128 return(-1);
129 if(p_arg(t->sw.swarg)<0)
130 return(-1);
131 return(p_switch(t->sw.swlst));
132 case TFUN:
133 if(sfputu(outfile,t->funct.functline)<0)
134 return(-1);
135 if(p_string(t->funct.functnam)<0)
136 return(-1);
137 if(p_tree(t->funct.functtre)<0)
138 return(-1);
139 return(p_tree((Shnode_t*)t->funct.functargs));
140 case TTST:
141 if(sfputu(outfile,t->tst.tstline)<0)
142 return(-1);
143 if((t->tre.tretyp&TPAREN)==TPAREN)
144 return(p_tree(t->lst.lstlef));
145 else
146 {
147 if(p_arg(&(t->lst.lstlef->arg))<0)
148 return(-1);
149 if((t->tre.tretyp&TBINARY))
150 return(p_arg(&(t->lst.lstrit->arg)));
151 return(0);
152 }
153 }
154 return(-1);
155 }
156
p_arg(register const struct argnod * arg)157 static int p_arg(register const struct argnod *arg)
158 {
159 register int n;
160 struct fornod *fp;
161 while(arg)
162 {
163 if((n = strlen(arg->argval)) || (arg->argflag&~(ARG_APPEND|ARG_MESSAGE|ARG_QUOTED)))
164 fp=0;
165 else
166 {
167 fp=(struct fornod*)arg->argchn.ap;
168 n = strlen(fp->fornam)+1;
169 }
170 sfputu(outfile,n+1);
171 if(fp)
172 {
173 sfputc(outfile,0);
174 outstring(outfile,fp->fornam,n-1);
175 }
176 else
177 outstring(outfile,arg->argval,n);
178 sfputc(outfile,arg->argflag);
179 if(fp)
180 {
181 sfputu(outfile,fp->fortyp);
182 p_tree(fp->fortre);
183 }
184 arg = arg->argnxt.ap;
185 }
186 return(sfputu(outfile,0));
187 }
188
p_redirect(register const struct ionod * iop)189 static int p_redirect(register const struct ionod *iop)
190 {
191 while(iop)
192 {
193 if(iop->iovname)
194 sfputl(outfile,iop->iofile|IOVNM);
195 else
196 sfputl(outfile,iop->iofile);
197 p_string(iop->ioname);
198 if(iop->iodelim)
199 {
200 p_string(iop->iodelim);
201 sfputl(outfile,iop->iosize);
202 sfseek(sh.heredocs,iop->iooffset,SEEK_SET);
203 sfmove(sh.heredocs,outfile, iop->iosize,-1);
204 }
205 else
206 sfputu(outfile,0);
207 if(iop->iovname)
208 p_string(iop->iovname);
209 iop = iop->ionxt;
210 }
211 return(sfputl(outfile,-1));
212 }
213
p_comarg(register const struct comnod * com)214 static int p_comarg(register const struct comnod *com)
215 {
216 p_redirect(com->comio);
217 p_arg(com->comset);
218 if(!com->comarg)
219 sfputl(outfile,-1);
220 else if(com->comtyp&COMSCAN)
221 p_arg(com->comarg);
222 else
223 p_comlist((struct dolnod*)com->comarg);
224 return(sfputu(outfile,com->comline));
225 }
226
p_comlist(const struct dolnod * dol)227 static int p_comlist(const struct dolnod *dol)
228 {
229 register char *cp, *const*argv;
230 register int n;
231 argv = dol->dolval+ARG_SPARE;
232 while(cp = *argv)
233 argv++;
234 n = argv - (dol->dolval+1);
235 sfputl(outfile,n);
236 argv = dol->dolval+ARG_SPARE;
237 while(cp = *argv++)
238 p_string(cp);
239 return(sfputu(outfile,0));
240 }
241
p_switch(register const struct regnod * reg)242 static int p_switch(register const struct regnod *reg)
243 {
244 while(reg)
245 {
246 sfputl(outfile,reg->regflag);
247 p_arg(reg->regptr);
248 p_tree(reg->regcom);
249 reg = reg->regnxt;
250 }
251 return(sfputl(outfile,-1));
252 }
253
p_string(register const char * string)254 static int p_string(register const char *string)
255 {
256 register size_t n=strlen(string);
257 if(sfputu(outfile,n+1)<0)
258 return(-1);
259 return(outstring(outfile,string,n));
260 }
261