8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libshell / common / sh / tdump.c
blob444ed340dbafafaefe685f8891c28488ed30d906
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
22 * David Korn
23 * AT&T Labs
25 * shell parse tree dump
29 #include "defs.h"
30 #include "shnodes.h"
31 #include "path.h"
32 #include "io.h"
33 #include <ccode.h>
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*);
43 static Sfio_t *outfile;
45 int sh_tdump(Sfio_t *out, const Shnode_t *t)
47 outfile = out;
48 return(p_tree(t));
52 * convert to ASCII to write and back again if needed
54 static int outstring(Sfio_t *out, const char *string, int n)
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);
65 * print script corresponding to shell tree <t>
67 static int p_tree(register const Shnode_t *t)
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)
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)
96 if(p_tree((Shnode_t*)(t->wh.whinc))<0)
97 return(-1);
99 else
101 if(sfputl(outfile,-1)<0)
102 return(-1);
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
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);
154 return(-1);
157 static int p_arg(register const struct argnod *arg)
159 register int n;
160 struct fornod *fp;
161 while(arg)
163 if((n = strlen(arg->argval)) || (arg->argflag&~(ARG_APPEND|ARG_MESSAGE|ARG_QUOTED)))
164 fp=0;
165 else
167 fp=(struct fornod*)arg->argchn.ap;
168 n = strlen(fp->fornam)+1;
170 sfputu(outfile,n+1);
171 if(fp)
173 sfputc(outfile,0);
174 outstring(outfile,fp->fornam,n-1);
176 else
177 outstring(outfile,arg->argval,n);
178 sfputc(outfile,arg->argflag);
179 if(fp)
181 sfputu(outfile,fp->fortyp);
182 p_tree(fp->fortre);
184 arg = arg->argnxt.ap;
186 return(sfputu(outfile,0));
189 static int p_redirect(register const struct ionod *iop)
191 while(iop)
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)
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);
205 else
206 sfputu(outfile,0);
207 if(iop->iovname)
208 p_string(iop->iovname);
209 iop = iop->ionxt;
211 return(sfputl(outfile,-1));
214 static int p_comarg(register const struct comnod *com)
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));
227 static int p_comlist(const struct dolnod *dol)
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));
242 static int p_switch(register const struct regnod *reg)
244 while(reg)
246 sfputl(outfile,reg->regflag);
247 p_arg(reg->regptr);
248 p_tree(reg->regcom);
249 reg = reg->regnxt;
251 return(sfputl(outfile,-1));
254 static int p_string(register const char *string)
256 register size_t n=strlen(string);
257 if(sfputu(outfile,n+1)<0)
258 return(-1);
259 return(outstring(outfile,string,n));