8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sh / func.c
blob81890b662c41ace3b5b5ef8703936c6f115acea4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * UNIX shell
34 #include "defs.h"
36 static void free_arg(struct argnod *);
37 static void freeio(struct ionod *);
38 static void freereg(struct regnod *);
39 static void prarg(struct argnod *argp);
40 static void prio(struct ionod *iop);
42 void
43 freefunc(struct namnod *n)
45 freetree((struct trenod *)(n->namenv));
48 void
49 freetree(struct trenod *t)
51 if (t)
53 int type;
55 type = t->tretyp & COMMSK;
57 switch (type)
59 case TFND: {
60 struct fndnod *f = fndptr(t);
62 if (f->fndref > 0) {
63 f->fndref--;
64 return;
66 free(f->fndnam);
67 freetree(f->fndval);
68 break;
71 case TCOM:
72 freeio(comptr(t)->comio);
73 free_arg(comptr(t)->comarg);
74 free_arg(comptr(t)->comset);
75 break;
77 case TFORK:
78 freeio(forkptr(t)->forkio);
79 freetree(forkptr(t)->forktre);
80 break;
82 case TPAR:
83 freetree(parptr(t)->partre);
84 break;
86 case TFIL:
87 case TLST:
88 case TAND:
89 case TORF:
90 freetree(lstptr(t)->lstlef);
91 freetree(lstptr(t)->lstrit);
92 break;
94 case TFOR:
96 struct fornod *f = (struct fornod *)t;
98 free(f->fornam);
99 freetree(f->fortre);
100 if (f->forlst)
102 freeio(f->forlst->comio);
103 free_arg(f->forlst->comarg);
104 free_arg(f->forlst->comset);
105 free(f->forlst);
108 break;
110 case TWH:
111 case TUN:
112 freetree(whptr(t)->whtre);
113 freetree(whptr(t)->dotre);
114 break;
116 case TIF:
117 freetree(ifptr(t)->iftre);
118 freetree(ifptr(t)->thtre);
119 freetree(ifptr(t)->eltre);
120 break;
122 case TSW:
123 free(swptr(t)->swarg);
124 freereg(swptr(t)->swlst);
125 break;
127 free(t);
131 static void
132 free_arg(struct argnod *argp)
134 struct argnod *sav;
136 while (argp)
138 sav = argp->argnxt;
139 free(argp);
140 argp = sav;
144 void
145 freeio(struct ionod *iop)
147 struct ionod *sav;
149 while (iop)
151 if (iop->iofile & IODOC)
154 #ifdef DEBUG
155 prs("unlinking ");
156 prs(iop->ioname);
157 newline();
158 #endif
160 unlink(iop->ioname);
162 if (fiotemp == iop)
163 fiotemp = iop->iolst;
164 else
166 struct ionod *fiop = fiotemp;
168 while (fiop->iolst != iop)
169 fiop = fiop->iolst;
171 fiop->iolst = iop->iolst;
174 free(iop->ioname);
175 free(iop->iolink);
176 sav = iop->ionxt;
177 free(iop);
178 iop = sav;
182 static void
183 freereg(struct regnod *regp)
185 struct regnod *sav;
187 while (regp)
189 free_arg(regp->regptr);
190 freetree(regp->regcom);
191 sav = regp->regnxt;
192 free(regp);
193 regp = sav;
198 static int nonl = 0;
200 void
201 prbgnlst(void)
203 if (nonl)
204 prc_buff(SPACE);
205 else
206 prc_buff(NL);
209 void
210 prendlst(void)
212 if (nonl) {
213 prc_buff(';');
214 prc_buff(SPACE);
216 else
217 prc_buff(NL);
220 void
221 prcmd(struct trenod *t)
223 nonl++;
224 prf(t);
225 nonl = 0;
228 void
229 prf(struct trenod *t)
231 sigchk();
233 if (t)
235 int type;
237 type = t->tretyp & COMMSK;
239 switch(type)
241 case TFND:
243 struct fndnod *f = (struct fndnod *)t;
245 prs_buff(f->fndnam);
246 prs_buff("(){");
247 prbgnlst();
248 prf(f->fndval);
249 prbgnlst();
250 prs_buff("}");
251 break;
254 case TCOM:
255 if (comptr(t)->comset) {
256 prarg(comptr(t)->comset);
257 prc_buff(SPACE);
259 prarg(comptr(t)->comarg);
260 prio(comptr(t)->comio);
261 break;
263 case TFORK:
264 prf(forkptr(t)->forktre);
265 prio(forkptr(t)->forkio);
266 if (forkptr(t)->forktyp & FAMP)
267 prs_buff(" &");
268 break;
270 case TPAR:
271 prs_buff("(");
272 prf(parptr(t)->partre);
273 prs_buff(")");
274 break;
276 case TFIL:
277 prf(lstptr(t)->lstlef);
278 prs_buff(" | ");
279 prf(lstptr(t)->lstrit);
280 break;
282 case TLST:
283 prf(lstptr(t)->lstlef);
284 prendlst();
285 prf(lstptr(t)->lstrit);
286 break;
288 case TAND:
289 prf(lstptr(t)->lstlef);
290 prs_buff(" && ");
291 prf(lstptr(t)->lstrit);
292 break;
294 case TORF:
295 prf(lstptr(t)->lstlef);
296 prs_buff(" || ");
297 prf(lstptr(t)->lstrit);
298 break;
300 case TFOR:
302 struct argnod *arg;
303 struct fornod *f = (struct fornod *)t;
305 prs_buff("for ");
306 prs_buff(f->fornam);
308 if (f->forlst)
310 arg = f->forlst->comarg;
311 prs_buff(" in");
313 while(arg != ENDARGS)
315 prc_buff(SPACE);
316 prs_buff(arg->argval);
317 arg = arg->argnxt;
321 prendlst();
322 prs_buff("do");
323 prbgnlst();
324 prf(f->fortre);
325 prendlst();
326 prs_buff("done");
328 break;
330 case TWH:
331 case TUN:
332 if (type == TWH)
333 prs_buff("while ");
334 else
335 prs_buff("until ");
336 prf(whptr(t)->whtre);
337 prendlst();
338 prs_buff("do");
339 prbgnlst();
340 prf(whptr(t)->dotre);
341 prendlst();
342 prs_buff("done");
343 break;
345 case TIF:
347 struct ifnod *f = (struct ifnod *)t;
349 prs_buff("if ");
350 prf(f->iftre);
351 prendlst();
352 prs_buff("then");
353 prendlst();
354 prf(f->thtre);
356 if (f->eltre)
358 prendlst();
359 prs_buff("else");
360 prendlst();
361 prf(f->eltre);
364 prendlst();
365 prs_buff("fi");
366 break;
369 case TSW:
371 struct regnod *swl;
373 prs_buff("case ");
374 prs_buff(swptr(t)->swarg);
376 swl = swptr(t)->swlst;
377 while(swl)
379 struct argnod *arg = swl->regptr;
381 if (arg)
383 prs_buff(arg->argval);
384 arg = arg->argnxt;
387 while(arg)
389 prs_buff(" | ");
390 prs_buff(arg->argval);
391 arg = arg->argnxt;
394 prs_buff(")");
395 prf(swl->regcom);
396 prs_buff(";;");
397 swl = swl->regnxt;
400 break;
404 sigchk();
407 static void
408 prarg(struct argnod *argp)
410 while (argp)
412 prs_buff(argp->argval);
413 argp=argp->argnxt;
414 if (argp)
415 prc_buff(SPACE);
419 static void
420 prio(struct ionod *iop)
422 int iof;
423 unsigned char *ion;
425 while (iop)
427 iof = iop->iofile;
428 ion = (unsigned char *) iop->ioname;
430 if (*ion)
432 prc_buff(SPACE);
434 prn_buff(iof & IOUFD);
436 if (iof & IODOC)
437 prs_buff("<<");
438 else if (iof & IOMOV)
440 if (iof & IOPUT)
441 prs_buff(">&");
442 else
443 prs_buff("<&");
446 else if ((iof & IOPUT) == 0)
447 prc_buff('<');
448 else if (iof & IOAPP)
449 prs_buff(">>");
450 else
451 prc_buff('>');
453 prs_buff(ion);
455 iop = iop->ionxt;