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]
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 */
37 static struct ionod
* inout();
38 static void chkword(void);
39 static void chksym(int);
40 static struct trenod
* term();
41 static struct trenod
* makelist();
42 static struct trenod
* list();
43 static struct regnod
* syncase();
44 static struct trenod
* item();
46 static void prsym(int);
47 static void synbad(void);
50 /* ======== storage allocation for functions ======== */
57 return((unsigned char *)alloc(asize
));
59 return(getstak(asize
));
63 /* ======== command line decoding ========*/
75 t
= (struct forknod
*)getstor(sizeof(struct forknod
));
76 t
->forktyp
= flgs
|TFORK
;
79 return((struct trenod
*)t
);
82 static struct trenod
*
93 t
= (struct lstnod
*)getstor(sizeof(struct lstnod
));
98 return((struct trenod
*)t
);
113 struct trenod
*i
, *e
;
123 else if (i
== 0 && (flg
& MTFLG
) == 0)
130 i
= makefork(FAMP
, i
);
135 if (e
= cmd(sym
, flg
| MTFLG
))
136 i
= makelist(TLST
, i
, e
);
158 static struct trenod
*
164 while (r
&& ((b
= (wdval
== ANDFSYM
)) || wdval
== ORFSYM
))
165 r
= makelist((b
? TAND
: TORF
), r
, term(NLFLG
));
174 static struct trenod
*
184 if ((t
= item(TRUE
)) && (wdval
== '^' || wdval
== '|'))
187 struct trenod
*right
;
189 left
= makefork(FPOU
, t
);
190 right
= makefork(FPIN
, term(NLFLG
));
191 return(makefork(0, makelist(TFIL
, left
, right
)));
198 static struct regnod
*
208 (struct regnod
*)getstor(sizeof (struct regnod
));
217 wdarg
= (struct argnod
*)alloc(length(argp
->argval
) + BYTESPERWORD
);
218 movstr(argp
->argval
, wdarg
->argval
);
221 wdarg
->argnxt
= r
->regptr
;
224 /* 'in' is not a reserved word in this case */
228 if (wdval
|| (word() != ')' && wdval
!= '|'))
235 r
->regcom
= cmd(0, NLFLG
| MTFLG
);
237 r
->regnxt
= syncase(esym
);
250 * ( cmd ) [ < in ] [ > out ]
251 * word word* [ < in ] [ > out ]
252 * if ... then ... else ... fi
253 * for ... while ... do ... done
254 * case ... in ... esac
257 static struct trenod
*
265 io
= inout((struct ionod
*)0);
274 t
= (struct swnod
*)getstor(sizeof(struct swnod
));
275 r
= (struct trenod
*)t
;
279 t
->swarg
= make(wdarg
->argval
);
281 t
->swarg
= wdarg
->argval
;
283 chksym(INSYM
| BRSYM
);
284 t
->swlst
= syncase(wdval
== INSYM
? ESSYM
: KTSYM
);
294 t
= (struct ifnod
*)getstor(sizeof(struct ifnod
));
295 r
= (struct trenod
*)t
;
298 t
->iftre
= cmd(THSYM
, NLFLG
);
299 t
->thtre
= cmd(ELSYM
| FISYM
| EFSYM
, NLFLG
);
300 t
->eltre
= ((w
= wdval
) == ELSYM
? cmd(FISYM
, NLFLG
) : (w
== EFSYM
? (wdval
= IFSYM
, item(0)) : 0));
310 t
= (struct fornod
*)getstor(sizeof(struct fornod
));
311 r
= (struct trenod
*)t
;
317 t
->fornam
= make(wdarg
->argval
);
319 t
->fornam
= wdarg
->argval
;
320 if (skipnl() == INSYM
)
325 t
->forlst
= (struct comnod
*)item(0);
328 if (wdval
!= NL
&& wdval
!= ';')
334 chksym(DOSYM
| BRSYM
);
335 t
->fortre
= cmd(wdval
== DOSYM
? ODSYM
: KTSYM
, NLFLG
);
344 t
= (struct whnod
*)getstor(sizeof(struct whnod
));
345 r
= (struct trenod
*)t
;
347 t
->whtyp
= (wdval
== WHSYM
? TWH
: TUN
);
348 t
->whtre
= cmd(DOSYM
, NLFLG
);
349 t
->dotre
= cmd(ODSYM
, NLFLG
);
354 r
= cmd(KTSYM
, NLFLG
);
361 p
= (struct parnod
*)getstor(sizeof(struct parnod
));
362 p
->partre
= cmd(')', NLFLG
);
376 struct argnod
**argtail
;
377 struct argnod
**argset
= 0;
381 if ((wdval
!= NL
) && ((peekn
= skipwc()) == '('))
384 struct ionod
*saveio
;
392 * We increase fndef before calling getstor(),
393 * so that getstor() uses malloc to allocate
394 * memory instead of stack. This is necessary
395 * since fndnod will be hung on np->namenv,
396 * which persists over command executions.
399 f
= (struct fndnod
*)getstor(sizeof(struct fndnod
));
400 r
= (struct trenod
*)f
;
403 f
->fndnam
= make(wdarg
->argval
);
407 f
->fndval
= (struct trenod
*)item(0);
410 if (iotemp
!= saveio
)
412 struct ionod
*ioptr
= iotemp
;
414 while (ioptr
->iolst
!= saveio
)
415 ioptr
= ioptr
->iolst
;
417 ioptr
->iolst
= fiotemp
;
425 t
= (struct comnod
*)getstor(sizeof(struct comnod
));
426 r
= (struct trenod
*)t
;
428 t
->comio
= io
; /*initial io chain*/
429 argtail
= &(t
->comarg
);
436 wdarg
= (struct argnod
*)alloc(length(argp
->argval
) + BYTESPERWORD
);
437 movstr(argp
->argval
, wdarg
->argval
);
443 argp
->argnxt
= (struct argnod
*)argset
;
444 argset
= (struct argnod
**)argp
;
449 argtail
= &(argp
->argnxt
);
450 keywd
= flags
& keyflg
;
459 io
->ionxt
= inout((struct ionod
*)0);
462 t
->comio
= io
= inout((struct ionod
*)0);
467 t
->comset
= (struct argnod
*)argset
;
470 if (nohash
== 0 && (fndef
== 0 || (flags
& hashflg
)))
474 com
= t
->comarg
->argval
;
475 if (*com
&& *com
!= DOLLAR
)
476 pathlook(com
, 0, t
->comset
);
499 while ((reserv
++, word() == NL
))
504 static struct ionod
*
506 struct ionod
*lastio
;
515 case DOCSYM
: /* << */
516 iof
|= IODOC
|IODOC_SUBST
;
519 case APPSYM
: /* >> */
531 if ((c
= nextwc()) == '&')
544 iop
= (struct ionod
*)getstor(sizeof(struct ionod
));
547 iop
->ioname
= (char *) make(wdarg
->argval
);
549 iop
->ioname
= (char *) (wdarg
->argval
);
559 iop
->ionxt
= inout(lastio
);
575 if (((x
& SYMFLG
) ? x
: sym
) != wdval
)
584 const struct sysnod
*sp
= reserved
;
586 while (sp
->sysval
&& sp
->sysval
!= sym
)
590 else if (sym
== EOFSYM
)
591 prs(_gettext(endoffile
));
597 prs(_gettext(nlorsemi
));
607 prs(_gettext(synmsg
));
608 if ((flags
& ttyflg
) == 0)
610 prs(_gettext(atline
));
618 prs_cntl(wdarg
->argval
);
620 prs(_gettext(unexpected
));