parser.c revision 149026
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/parser.c 149026 2005-08-13 15:47:13Z stefanf $");
40
41#include <stdlib.h>
42#include <unistd.h>
43
44#include "shell.h"
45#include "parser.h"
46#include "nodes.h"
47#include "expand.h"	/* defines rmescapes() */
48#include "syntax.h"
49#include "options.h"
50#include "input.h"
51#include "output.h"
52#include "var.h"
53#include "error.h"
54#include "memalloc.h"
55#include "mystring.h"
56#include "alias.h"
57#include "show.h"
58#include "eval.h"
59#ifndef NO_HISTORY
60#include "myhistedit.h"
61#endif
62
63/*
64 * Shell command parser.
65 */
66
67#define	EOFMARKLEN	79
68#define	PROMPTLEN	128
69
70/* values returned by readtoken */
71#include "token.h"
72
73
74
75struct heredoc {
76	struct heredoc *next;	/* next here document in list */
77	union node *here;		/* redirection node */
78	char *eofmark;		/* string indicating end of input */
79	int striptabs;		/* if set, strip leading tabs */
80};
81
82
83
84STATIC struct heredoc *heredoclist;	/* list of here documents to read */
85STATIC int parsebackquote;	/* nonzero if we are inside backquotes */
86STATIC int doprompt;		/* if set, prompt the user */
87STATIC int needprompt;		/* true if interactive and at start of line */
88STATIC int lasttoken;		/* last token read */
89MKINIT int tokpushback;		/* last token pushed back */
90STATIC char *wordtext;		/* text of last word returned by readtoken */
91MKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
92STATIC struct nodelist *backquotelist;
93STATIC union node *redirnode;
94STATIC struct heredoc *heredoc;
95STATIC int quoteflag;		/* set if (part of) last token was quoted */
96STATIC int startlinno;		/* line # where last token started */
97
98/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
99static int noaliases = 0;
100
101#define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
102#ifdef GDB_HACK
103static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
104static const char types[] = "}-+?=";
105#endif
106
107
108STATIC union node *list(int);
109STATIC union node *andor(void);
110STATIC union node *pipeline(void);
111STATIC union node *command(void);
112STATIC union node *simplecmd(union node **, union node *);
113STATIC union node *makename(void);
114STATIC void parsefname(void);
115STATIC void parseheredoc(void);
116STATIC int peektoken(void);
117STATIC int readtoken(void);
118STATIC int xxreadtoken(void);
119STATIC int readtoken1(int, char const *, char *, int);
120STATIC int noexpand(char *);
121STATIC void synexpect(int);
122STATIC void synerror(char *);
123STATIC void setprompt(int);
124
125
126/*
127 * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
128 * valid parse tree indicating a blank line.)
129 */
130
131union node *
132parsecmd(int interact)
133{
134	int t;
135
136	tokpushback = 0;
137	doprompt = interact;
138	if (doprompt)
139		setprompt(1);
140	else
141		setprompt(0);
142	needprompt = 0;
143	t = readtoken();
144	if (t == TEOF)
145		return NEOF;
146	if (t == TNL)
147		return NULL;
148	tokpushback++;
149	return list(1);
150}
151
152
153STATIC union node *
154list(int nlflag)
155{
156	union node *n1, *n2, *n3;
157	int tok;
158
159	checkkwd = 2;
160	if (nlflag == 0 && tokendlist[peektoken()])
161		return NULL;
162	n1 = NULL;
163	for (;;) {
164		n2 = andor();
165		tok = readtoken();
166		if (tok == TBACKGND) {
167			if (n2->type == NCMD || n2->type == NPIPE) {
168				n2->ncmd.backgnd = 1;
169			} else if (n2->type == NREDIR) {
170				n2->type = NBACKGND;
171			} else {
172				n3 = (union node *)stalloc(sizeof (struct nredir));
173				n3->type = NBACKGND;
174				n3->nredir.n = n2;
175				n3->nredir.redirect = NULL;
176				n2 = n3;
177			}
178		}
179		if (n1 == NULL) {
180			n1 = n2;
181		}
182		else {
183			n3 = (union node *)stalloc(sizeof (struct nbinary));
184			n3->type = NSEMI;
185			n3->nbinary.ch1 = n1;
186			n3->nbinary.ch2 = n2;
187			n1 = n3;
188		}
189		switch (tok) {
190		case TBACKGND:
191		case TSEMI:
192			tok = readtoken();
193			/* FALLTHROUGH */
194		case TNL:
195			if (tok == TNL) {
196				parseheredoc();
197				if (nlflag)
198					return n1;
199			} else {
200				tokpushback++;
201			}
202			checkkwd = 2;
203			if (tokendlist[peektoken()])
204				return n1;
205			break;
206		case TEOF:
207			if (heredoclist)
208				parseheredoc();
209			else
210				pungetc();		/* push back EOF on input */
211			return n1;
212		default:
213			if (nlflag)
214				synexpect(-1);
215			tokpushback++;
216			return n1;
217		}
218	}
219}
220
221
222
223STATIC union node *
224andor(void)
225{
226	union node *n1, *n2, *n3;
227	int t;
228
229	n1 = pipeline();
230	for (;;) {
231		if ((t = readtoken()) == TAND) {
232			t = NAND;
233		} else if (t == TOR) {
234			t = NOR;
235		} else {
236			tokpushback++;
237			return n1;
238		}
239		n2 = pipeline();
240		n3 = (union node *)stalloc(sizeof (struct nbinary));
241		n3->type = t;
242		n3->nbinary.ch1 = n1;
243		n3->nbinary.ch2 = n2;
244		n1 = n3;
245	}
246}
247
248
249
250STATIC union node *
251pipeline(void)
252{
253	union node *n1, *n2, *pipenode;
254	struct nodelist *lp, *prev;
255	int negate;
256
257	negate = 0;
258	TRACE(("pipeline: entered\n"));
259	while (readtoken() == TNOT)
260		negate = !negate;
261	tokpushback++;
262	n1 = command();
263	if (readtoken() == TPIPE) {
264		pipenode = (union node *)stalloc(sizeof (struct npipe));
265		pipenode->type = NPIPE;
266		pipenode->npipe.backgnd = 0;
267		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
268		pipenode->npipe.cmdlist = lp;
269		lp->n = n1;
270		do {
271			prev = lp;
272			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
273			lp->n = command();
274			prev->next = lp;
275		} while (readtoken() == TPIPE);
276		lp->next = NULL;
277		n1 = pipenode;
278	}
279	tokpushback++;
280	if (negate) {
281		n2 = (union node *)stalloc(sizeof (struct nnot));
282		n2->type = NNOT;
283		n2->nnot.com = n1;
284		return n2;
285	} else
286		return n1;
287}
288
289
290
291STATIC union node *
292command(void)
293{
294	union node *n1, *n2;
295	union node *ap, **app;
296	union node *cp, **cpp;
297	union node *redir, **rpp;
298	int t, negate = 0;
299
300	checkkwd = 2;
301	redir = NULL;
302	n1 = NULL;
303	rpp = &redir;
304
305	/* Check for redirection which may precede command */
306	while (readtoken() == TREDIR) {
307		*rpp = n2 = redirnode;
308		rpp = &n2->nfile.next;
309		parsefname();
310	}
311	tokpushback++;
312
313	while (readtoken() == TNOT) {
314		TRACE(("command: TNOT recognized\n"));
315		negate = !negate;
316	}
317	tokpushback++;
318
319	switch (readtoken()) {
320	case TIF:
321		n1 = (union node *)stalloc(sizeof (struct nif));
322		n1->type = NIF;
323		if ((n1->nif.test = list(0)) == NULL)
324			synexpect(-1);
325		if (readtoken() != TTHEN)
326			synexpect(TTHEN);
327		n1->nif.ifpart = list(0);
328		n2 = n1;
329		while (readtoken() == TELIF) {
330			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
331			n2 = n2->nif.elsepart;
332			n2->type = NIF;
333			if ((n2->nif.test = list(0)) == NULL)
334				synexpect(-1);
335			if (readtoken() != TTHEN)
336				synexpect(TTHEN);
337			n2->nif.ifpart = list(0);
338		}
339		if (lasttoken == TELSE)
340			n2->nif.elsepart = list(0);
341		else {
342			n2->nif.elsepart = NULL;
343			tokpushback++;
344		}
345		if (readtoken() != TFI)
346			synexpect(TFI);
347		checkkwd = 1;
348		break;
349	case TWHILE:
350	case TUNTIL: {
351		int got;
352		n1 = (union node *)stalloc(sizeof (struct nbinary));
353		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
354		if ((n1->nbinary.ch1 = list(0)) == NULL)
355			synexpect(-1);
356		if ((got=readtoken()) != TDO) {
357TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
358			synexpect(TDO);
359		}
360		n1->nbinary.ch2 = list(0);
361		if (readtoken() != TDONE)
362			synexpect(TDONE);
363		checkkwd = 1;
364		break;
365	}
366	case TFOR:
367		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
368			synerror("Bad for loop variable");
369		n1 = (union node *)stalloc(sizeof (struct nfor));
370		n1->type = NFOR;
371		n1->nfor.var = wordtext;
372		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
373			app = &ap;
374			while (readtoken() == TWORD) {
375				n2 = (union node *)stalloc(sizeof (struct narg));
376				n2->type = NARG;
377				n2->narg.text = wordtext;
378				n2->narg.backquote = backquotelist;
379				*app = n2;
380				app = &n2->narg.next;
381			}
382			*app = NULL;
383			n1->nfor.args = ap;
384			if (lasttoken != TNL && lasttoken != TSEMI)
385				synexpect(-1);
386		} else {
387#ifndef GDB_HACK
388			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
389								   '@', '=', '\0'};
390#endif
391			n2 = (union node *)stalloc(sizeof (struct narg));
392			n2->type = NARG;
393			n2->narg.text = (char *)argvars;
394			n2->narg.backquote = NULL;
395			n2->narg.next = NULL;
396			n1->nfor.args = n2;
397			/*
398			 * Newline or semicolon here is optional (but note
399			 * that the original Bourne shell only allowed NL).
400			 */
401			if (lasttoken != TNL && lasttoken != TSEMI)
402				tokpushback++;
403		}
404		checkkwd = 2;
405		if ((t = readtoken()) == TDO)
406			t = TDONE;
407		else if (t == TBEGIN)
408			t = TEND;
409		else
410			synexpect(-1);
411		n1->nfor.body = list(0);
412		if (readtoken() != t)
413			synexpect(t);
414		checkkwd = 1;
415		break;
416	case TCASE:
417		n1 = (union node *)stalloc(sizeof (struct ncase));
418		n1->type = NCASE;
419		if (readtoken() != TWORD)
420			synexpect(TWORD);
421		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
422		n2->type = NARG;
423		n2->narg.text = wordtext;
424		n2->narg.backquote = backquotelist;
425		n2->narg.next = NULL;
426		while (readtoken() == TNL);
427		if (lasttoken != TWORD || ! equal(wordtext, "in"))
428			synerror("expecting \"in\"");
429		cpp = &n1->ncase.cases;
430		noaliases = 1;	/* turn off alias expansion */
431		checkkwd = 2, readtoken();
432		while (lasttoken != TESAC) {
433			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
434			cp->type = NCLIST;
435			app = &cp->nclist.pattern;
436			if (lasttoken == TLP)
437				readtoken();
438			for (;;) {
439				*app = ap = (union node *)stalloc(sizeof (struct narg));
440				ap->type = NARG;
441				ap->narg.text = wordtext;
442				ap->narg.backquote = backquotelist;
443				if (checkkwd = 2, readtoken() != TPIPE)
444					break;
445				app = &ap->narg.next;
446				readtoken();
447			}
448			ap->narg.next = NULL;
449			if (lasttoken != TRP)
450				noaliases = 0, synexpect(TRP);
451			cp->nclist.body = list(0);
452
453			checkkwd = 2;
454			if ((t = readtoken()) != TESAC) {
455				if (t != TENDCASE)
456					noaliases = 0, synexpect(TENDCASE);
457				else
458					checkkwd = 2, readtoken();
459			}
460			cpp = &cp->nclist.next;
461		}
462		noaliases = 0;	/* reset alias expansion */
463		*cpp = NULL;
464		checkkwd = 1;
465		break;
466	case TLP:
467		n1 = (union node *)stalloc(sizeof (struct nredir));
468		n1->type = NSUBSHELL;
469		n1->nredir.n = list(0);
470		n1->nredir.redirect = NULL;
471		if (readtoken() != TRP)
472			synexpect(TRP);
473		checkkwd = 1;
474		break;
475	case TBEGIN:
476		n1 = list(0);
477		if (readtoken() != TEND)
478			synexpect(TEND);
479		checkkwd = 1;
480		break;
481	/* Handle an empty command like other simple commands.  */
482	case TSEMI:
483	case TAND:
484	case TOR:
485		/*
486		 * An empty command before a ; doesn't make much sense, and
487		 * should certainly be disallowed in the case of `if ;'.
488		 */
489		if (!redir)
490			synexpect(-1);
491	case TNL:
492	case TEOF:
493	case TWORD:
494	case TRP:
495		tokpushback++;
496		n1 = simplecmd(rpp, redir);
497		goto checkneg;
498	default:
499		synexpect(-1);
500	}
501
502	/* Now check for redirection which may follow command */
503	while (readtoken() == TREDIR) {
504		*rpp = n2 = redirnode;
505		rpp = &n2->nfile.next;
506		parsefname();
507	}
508	tokpushback++;
509	*rpp = NULL;
510	if (redir) {
511		if (n1->type != NSUBSHELL) {
512			n2 = (union node *)stalloc(sizeof (struct nredir));
513			n2->type = NREDIR;
514			n2->nredir.n = n1;
515			n1 = n2;
516		}
517		n1->nredir.redirect = redir;
518	}
519
520checkneg:
521	if (negate) {
522		n2 = (union node *)stalloc(sizeof (struct nnot));
523		n2->type = NNOT;
524		n2->nnot.com = n1;
525		return n2;
526	}
527	else
528		return n1;
529}
530
531
532STATIC union node *
533simplecmd(union node **rpp, union node *redir)
534{
535	union node *args, **app;
536	union node **orig_rpp = rpp;
537	union node *n = NULL, *n2;
538	int negate = 0;
539
540	/* If we don't have any redirections already, then we must reset */
541	/* rpp to be the address of the local redir variable.  */
542	if (redir == 0)
543		rpp = &redir;
544
545	args = NULL;
546	app = &args;
547	/*
548	 * We save the incoming value, because we need this for shell
549	 * functions.  There can not be a redirect or an argument between
550	 * the function name and the open parenthesis.
551	 */
552	orig_rpp = rpp;
553
554	while (readtoken() == TNOT) {
555		TRACE(("command: TNOT recognized\n"));
556		negate = !negate;
557	}
558	tokpushback++;
559
560	for (;;) {
561		if (readtoken() == TWORD) {
562			n = (union node *)stalloc(sizeof (struct narg));
563			n->type = NARG;
564			n->narg.text = wordtext;
565			n->narg.backquote = backquotelist;
566			*app = n;
567			app = &n->narg.next;
568		} else if (lasttoken == TREDIR) {
569			*rpp = n = redirnode;
570			rpp = &n->nfile.next;
571			parsefname();	/* read name of redirection file */
572		} else if (lasttoken == TLP && app == &args->narg.next
573					    && rpp == orig_rpp) {
574			/* We have a function */
575			if (readtoken() != TRP)
576				synexpect(TRP);
577#ifdef notdef
578			if (! goodname(n->narg.text))
579				synerror("Bad function name");
580#endif
581			n->type = NDEFUN;
582			n->narg.next = command();
583			goto checkneg;
584		} else {
585			tokpushback++;
586			break;
587		}
588	}
589	*app = NULL;
590	*rpp = NULL;
591	n = (union node *)stalloc(sizeof (struct ncmd));
592	n->type = NCMD;
593	n->ncmd.backgnd = 0;
594	n->ncmd.args = args;
595	n->ncmd.redirect = redir;
596
597checkneg:
598	if (negate) {
599		n2 = (union node *)stalloc(sizeof (struct nnot));
600		n2->type = NNOT;
601		n2->nnot.com = n;
602		return n2;
603	}
604	else
605		return n;
606}
607
608STATIC union node *
609makename(void)
610{
611	union node *n;
612
613	n = (union node *)stalloc(sizeof (struct narg));
614	n->type = NARG;
615	n->narg.next = NULL;
616	n->narg.text = wordtext;
617	n->narg.backquote = backquotelist;
618	return n;
619}
620
621void fixredir(union node *n, const char *text, int err)
622{
623	TRACE(("Fix redir %s %d\n", text, err));
624	if (!err)
625		n->ndup.vname = NULL;
626
627	if (is_digit(text[0]) && text[1] == '\0')
628		n->ndup.dupfd = digit_val(text[0]);
629	else if (text[0] == '-' && text[1] == '\0')
630		n->ndup.dupfd = -1;
631	else {
632
633		if (err)
634			synerror("Bad fd number");
635		else
636			n->ndup.vname = makename();
637	}
638}
639
640
641STATIC void
642parsefname(void)
643{
644	union node *n = redirnode;
645
646	if (readtoken() != TWORD)
647		synexpect(-1);
648	if (n->type == NHERE) {
649		struct heredoc *here = heredoc;
650		struct heredoc *p;
651		int i;
652
653		if (quoteflag == 0)
654			n->type = NXHERE;
655		TRACE(("Here document %d\n", n->type));
656		if (here->striptabs) {
657			while (*wordtext == '\t')
658				wordtext++;
659		}
660		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
661			synerror("Illegal eof marker for << redirection");
662		rmescapes(wordtext);
663		here->eofmark = wordtext;
664		here->next = NULL;
665		if (heredoclist == NULL)
666			heredoclist = here;
667		else {
668			for (p = heredoclist ; p->next ; p = p->next);
669			p->next = here;
670		}
671	} else if (n->type == NTOFD || n->type == NFROMFD) {
672		fixredir(n, wordtext, 0);
673	} else {
674		n->nfile.fname = makename();
675	}
676}
677
678
679/*
680 * Input any here documents.
681 */
682
683STATIC void
684parseheredoc(void)
685{
686	struct heredoc *here;
687	union node *n;
688
689	while (heredoclist) {
690		here = heredoclist;
691		heredoclist = here->next;
692		if (needprompt) {
693			setprompt(2);
694			needprompt = 0;
695		}
696		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
697				here->eofmark, here->striptabs);
698		n = (union node *)stalloc(sizeof (struct narg));
699		n->narg.type = NARG;
700		n->narg.next = NULL;
701		n->narg.text = wordtext;
702		n->narg.backquote = backquotelist;
703		here->here->nhere.doc = n;
704	}
705}
706
707STATIC int
708peektoken(void)
709{
710	int t;
711
712	t = readtoken();
713	tokpushback++;
714	return (t);
715}
716
717STATIC int
718readtoken(void)
719{
720	int t;
721	int savecheckkwd = checkkwd;
722	struct alias *ap;
723#ifdef DEBUG
724	int alreadyseen = tokpushback;
725#endif
726
727	top:
728	t = xxreadtoken();
729
730	if (checkkwd) {
731		/*
732		 * eat newlines
733		 */
734		if (checkkwd == 2) {
735			checkkwd = 0;
736			while (t == TNL) {
737				parseheredoc();
738				t = xxreadtoken();
739			}
740		} else
741			checkkwd = 0;
742		/*
743		 * check for keywords and aliases
744		 */
745		if (t == TWORD && !quoteflag)
746		{
747			const char * const *pp;
748
749			for (pp = parsekwd; *pp; pp++) {
750				if (**pp == *wordtext && equal(*pp, wordtext))
751				{
752					lasttoken = t = pp - parsekwd + KWDOFFSET;
753					TRACE(("keyword %s recognized\n", tokname[t]));
754					goto out;
755				}
756			}
757			if (noaliases == 0 &&
758			    (ap = lookupalias(wordtext, 1)) != NULL) {
759				pushstring(ap->val, strlen(ap->val), ap);
760				checkkwd = savecheckkwd;
761				goto top;
762			}
763		}
764out:
765		checkkwd = (t == TNOT) ? savecheckkwd : 0;
766	}
767#ifdef DEBUG
768	if (!alreadyseen)
769	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
770	else
771	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
772#endif
773	return (t);
774}
775
776
777/*
778 * Read the next input token.
779 * If the token is a word, we set backquotelist to the list of cmds in
780 *	backquotes.  We set quoteflag to true if any part of the word was
781 *	quoted.
782 * If the token is TREDIR, then we set redirnode to a structure containing
783 *	the redirection.
784 * In all cases, the variable startlinno is set to the number of the line
785 *	on which the token starts.
786 *
787 * [Change comment:  here documents and internal procedures]
788 * [Readtoken shouldn't have any arguments.  Perhaps we should make the
789 *  word parsing code into a separate routine.  In this case, readtoken
790 *  doesn't need to have any internal procedures, but parseword does.
791 *  We could also make parseoperator in essence the main routine, and
792 *  have parseword (readtoken1?) handle both words and redirection.]
793 */
794
795#define RETURN(token)	return lasttoken = token
796
797STATIC int
798xxreadtoken(void)
799{
800	int c;
801
802	if (tokpushback) {
803		tokpushback = 0;
804		return lasttoken;
805	}
806	if (needprompt) {
807		setprompt(2);
808		needprompt = 0;
809	}
810	startlinno = plinno;
811	for (;;) {	/* until token or start of word found */
812		c = pgetc_macro();
813		if (c == ' ' || c == '\t')
814			continue;		/* quick check for white space first */
815		switch (c) {
816		case ' ': case '\t':
817			continue;
818		case '#':
819			while ((c = pgetc()) != '\n' && c != PEOF);
820			pungetc();
821			continue;
822		case '\\':
823			if (pgetc() == '\n') {
824				startlinno = ++plinno;
825				if (doprompt)
826					setprompt(2);
827				else
828					setprompt(0);
829				continue;
830			}
831			pungetc();
832			goto breakloop;
833		case '\n':
834			plinno++;
835			needprompt = doprompt;
836			RETURN(TNL);
837		case PEOF:
838			RETURN(TEOF);
839		case '&':
840			if (pgetc() == '&')
841				RETURN(TAND);
842			pungetc();
843			RETURN(TBACKGND);
844		case '|':
845			if (pgetc() == '|')
846				RETURN(TOR);
847			pungetc();
848			RETURN(TPIPE);
849		case ';':
850			if (pgetc() == ';')
851				RETURN(TENDCASE);
852			pungetc();
853			RETURN(TSEMI);
854		case '(':
855			RETURN(TLP);
856		case ')':
857			RETURN(TRP);
858		default:
859			goto breakloop;
860		}
861	}
862breakloop:
863	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
864#undef RETURN
865}
866
867
868
869/*
870 * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
871 * is not NULL, read a here document.  In the latter case, eofmark is the
872 * word which marks the end of the document and striptabs is true if
873 * leading tabs should be stripped from the document.  The argument firstc
874 * is the first character of the input token or document.
875 *
876 * Because C does not have internal subroutines, I have simulated them
877 * using goto's to implement the subroutine linkage.  The following macros
878 * will run code that appears at the end of readtoken1.
879 */
880
881#define CHECKEND()	{goto checkend; checkend_return:;}
882#define PARSEREDIR()	{goto parseredir; parseredir_return:;}
883#define PARSESUB()	{goto parsesub; parsesub_return:;}
884#define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
885#define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
886#define	PARSEARITH()	{goto parsearith; parsearith_return:;}
887
888STATIC int
889readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs)
890{
891	int c = firstc;
892	char *out;
893	int len;
894	char line[EOFMARKLEN + 1];
895	struct nodelist *bqlist;
896	int quotef;
897	int dblquote;
898	int varnest;	/* levels of variables expansion */
899	int arinest;	/* levels of arithmetic expansion */
900	int parenlevel;	/* levels of parens in arithmetic */
901	int oldstyle;
902	char const *prevsyntax;	/* syntax before arithmetic */
903	int synentry;
904#if __GNUC__
905	/* Avoid longjmp clobbering */
906	(void) &out;
907	(void) &quotef;
908	(void) &dblquote;
909	(void) &varnest;
910	(void) &arinest;
911	(void) &parenlevel;
912	(void) &oldstyle;
913	(void) &prevsyntax;
914	(void) &syntax;
915	(void) &synentry;
916#endif
917
918	startlinno = plinno;
919	dblquote = 0;
920	if (syntax == DQSYNTAX)
921		dblquote = 1;
922	quotef = 0;
923	bqlist = NULL;
924	varnest = 0;
925	arinest = 0;
926	parenlevel = 0;
927
928	STARTSTACKSTR(out);
929	loop: {	/* for each line, until end of word */
930		CHECKEND();	/* set c to PEOF if at end of here document */
931		for (;;) {	/* until end of line or end of word */
932			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
933
934			synentry = syntax[c];
935
936			switch(synentry) {
937			case CNL:	/* '\n' */
938				if (syntax == BASESYNTAX)
939					goto endword;	/* exit outer loop */
940				USTPUTC(c, out);
941				plinno++;
942				if (doprompt)
943					setprompt(2);
944				else
945					setprompt(0);
946				c = pgetc();
947				goto loop;		/* continue outer loop */
948			case CWORD:
949				USTPUTC(c, out);
950				break;
951			case CCTL:
952				if (eofmark == NULL || dblquote)
953					USTPUTC(CTLESC, out);
954				USTPUTC(c, out);
955				break;
956			case CBACK:	/* backslash */
957				c = pgetc();
958				if (c == PEOF) {
959					USTPUTC('\\', out);
960					pungetc();
961				} else if (c == '\n') {
962					if (doprompt)
963						setprompt(2);
964					else
965						setprompt(0);
966				} else {
967					if (dblquote && c != '\\' &&
968					    c != '`' && c != '$' &&
969					    (c != '"' || eofmark != NULL))
970						USTPUTC('\\', out);
971					if (SQSYNTAX[c] == CCTL)
972						USTPUTC(CTLESC, out);
973					else if (eofmark == NULL)
974						USTPUTC(CTLQUOTEMARK, out);
975					USTPUTC(c, out);
976					quotef++;
977				}
978				break;
979			case CSQUOTE:
980				if (eofmark == NULL)
981					USTPUTC(CTLQUOTEMARK, out);
982				syntax = SQSYNTAX;
983				break;
984			case CDQUOTE:
985				if (eofmark == NULL)
986					USTPUTC(CTLQUOTEMARK, out);
987				syntax = DQSYNTAX;
988				dblquote = 1;
989				break;
990			case CENDQUOTE:
991				if (eofmark != NULL && arinest == 0 &&
992				    varnest == 0) {
993					USTPUTC(c, out);
994				} else {
995					if (arinest) {
996						syntax = ARISYNTAX;
997						dblquote = 0;
998					} else if (eofmark == NULL) {
999						syntax = BASESYNTAX;
1000						dblquote = 0;
1001					}
1002					quotef++;
1003				}
1004				break;
1005			case CVAR:	/* '$' */
1006				PARSESUB();		/* parse substitution */
1007				break;
1008			case CENDVAR:	/* '}' */
1009				if (varnest > 0) {
1010					varnest--;
1011					USTPUTC(CTLENDVAR, out);
1012				} else {
1013					USTPUTC(c, out);
1014				}
1015				break;
1016			case CLP:	/* '(' in arithmetic */
1017				parenlevel++;
1018				USTPUTC(c, out);
1019				break;
1020			case CRP:	/* ')' in arithmetic */
1021				if (parenlevel > 0) {
1022					USTPUTC(c, out);
1023					--parenlevel;
1024				} else {
1025					if (pgetc() == ')') {
1026						if (--arinest == 0) {
1027							USTPUTC(CTLENDARI, out);
1028							syntax = prevsyntax;
1029							if (syntax == DQSYNTAX)
1030								dblquote = 1;
1031							else
1032								dblquote = 0;
1033						} else
1034							USTPUTC(')', out);
1035					} else {
1036						/*
1037						 * unbalanced parens
1038						 *  (don't 2nd guess - no error)
1039						 */
1040						pungetc();
1041						USTPUTC(')', out);
1042					}
1043				}
1044				break;
1045			case CBQUOTE:	/* '`' */
1046				PARSEBACKQOLD();
1047				break;
1048			case CEOF:
1049				goto endword;		/* exit outer loop */
1050			default:
1051				if (varnest == 0)
1052					goto endword;	/* exit outer loop */
1053				USTPUTC(c, out);
1054			}
1055			c = pgetc_macro();
1056		}
1057	}
1058endword:
1059	if (syntax == ARISYNTAX)
1060		synerror("Missing '))'");
1061	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
1062		synerror("Unterminated quoted string");
1063	if (varnest != 0) {
1064		startlinno = plinno;
1065		synerror("Missing '}'");
1066	}
1067	USTPUTC('\0', out);
1068	len = out - stackblock();
1069	out = stackblock();
1070	if (eofmark == NULL) {
1071		if ((c == '>' || c == '<')
1072		 && quotef == 0
1073		 && len <= 2
1074		 && (*out == '\0' || is_digit(*out))) {
1075			PARSEREDIR();
1076			return lasttoken = TREDIR;
1077		} else {
1078			pungetc();
1079		}
1080	}
1081	quoteflag = quotef;
1082	backquotelist = bqlist;
1083	grabstackblock(len);
1084	wordtext = out;
1085	return lasttoken = TWORD;
1086/* end of readtoken routine */
1087
1088
1089
1090/*
1091 * Check to see whether we are at the end of the here document.  When this
1092 * is called, c is set to the first character of the next input line.  If
1093 * we are at the end of the here document, this routine sets the c to PEOF.
1094 */
1095
1096checkend: {
1097	if (eofmark) {
1098		if (striptabs) {
1099			while (c == '\t')
1100				c = pgetc();
1101		}
1102		if (c == *eofmark) {
1103			if (pfgets(line, sizeof line) != NULL) {
1104				char *p, *q;
1105
1106				p = line;
1107				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1108				if (*p == '\n' && *q == '\0') {
1109					c = PEOF;
1110					plinno++;
1111					needprompt = doprompt;
1112				} else {
1113					pushstring(line, strlen(line), NULL);
1114				}
1115			}
1116		}
1117	}
1118	goto checkend_return;
1119}
1120
1121
1122/*
1123 * Parse a redirection operator.  The variable "out" points to a string
1124 * specifying the fd to be redirected.  The variable "c" contains the
1125 * first character of the redirection operator.
1126 */
1127
1128parseredir: {
1129	char fd = *out;
1130	union node *np;
1131
1132	np = (union node *)stalloc(sizeof (struct nfile));
1133	if (c == '>') {
1134		np->nfile.fd = 1;
1135		c = pgetc();
1136		if (c == '>')
1137			np->type = NAPPEND;
1138		else if (c == '&')
1139			np->type = NTOFD;
1140		else if (c == '|')
1141			np->type = NCLOBBER;
1142		else {
1143			np->type = NTO;
1144			pungetc();
1145		}
1146	} else {	/* c == '<' */
1147		np->nfile.fd = 0;
1148		c = pgetc();
1149		if (c == '<') {
1150			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1151				np = (union node *)stalloc(sizeof (struct nhere));
1152				np->nfile.fd = 0;
1153			}
1154			np->type = NHERE;
1155			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1156			heredoc->here = np;
1157			if ((c = pgetc()) == '-') {
1158				heredoc->striptabs = 1;
1159			} else {
1160				heredoc->striptabs = 0;
1161				pungetc();
1162			}
1163		} else if (c == '&')
1164			np->type = NFROMFD;
1165		else if (c == '>')
1166			np->type = NFROMTO;
1167		else {
1168			np->type = NFROM;
1169			pungetc();
1170		}
1171	}
1172	if (fd != '\0')
1173		np->nfile.fd = digit_val(fd);
1174	redirnode = np;
1175	goto parseredir_return;
1176}
1177
1178
1179/*
1180 * Parse a substitution.  At this point, we have read the dollar sign
1181 * and nothing else.
1182 */
1183
1184parsesub: {
1185	int subtype;
1186	int typeloc;
1187	int flags;
1188	char *p;
1189#ifndef GDB_HACK
1190	static const char types[] = "}-+?=";
1191#endif
1192       int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1193
1194	c = pgetc();
1195	if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1196	    !is_special(c)) {
1197		USTPUTC('$', out);
1198		pungetc();
1199	} else if (c == '(') {	/* $(command) or $((arith)) */
1200		if (pgetc() == '(') {
1201			PARSEARITH();
1202		} else {
1203			pungetc();
1204			PARSEBACKQNEW();
1205		}
1206	} else {
1207		USTPUTC(CTLVAR, out);
1208		typeloc = out - stackblock();
1209		USTPUTC(VSNORMAL, out);
1210		subtype = VSNORMAL;
1211		if (c == '{') {
1212			bracketed_name = 1;
1213			c = pgetc();
1214			if (c == '#') {
1215				if ((c = pgetc()) == '}')
1216					c = '#';
1217				else
1218					subtype = VSLENGTH;
1219			}
1220			else
1221				subtype = 0;
1222		}
1223		if (!is_eof(c) && is_name(c)) {
1224			do {
1225				STPUTC(c, out);
1226				c = pgetc();
1227			} while (!is_eof(c) && is_in_name(c));
1228		} else if (is_digit(c)) {
1229			if (bracketed_name) {
1230				do {
1231					STPUTC(c, out);
1232					c = pgetc();
1233				} while (is_digit(c));
1234			} else {
1235				STPUTC(c, out);
1236				c = pgetc();
1237			}
1238		} else {
1239			if (! is_special(c))
1240badsub:				synerror("Bad substitution");
1241			USTPUTC(c, out);
1242			c = pgetc();
1243		}
1244		STPUTC('=', out);
1245		flags = 0;
1246		if (subtype == 0) {
1247			switch (c) {
1248			case ':':
1249				flags = VSNUL;
1250				c = pgetc();
1251				/*FALLTHROUGH*/
1252			default:
1253				p = strchr(types, c);
1254				if (p == NULL)
1255					goto badsub;
1256				subtype = p - types + VSNORMAL;
1257				break;
1258			case '%':
1259			case '#':
1260				{
1261					int cc = c;
1262					subtype = c == '#' ? VSTRIMLEFT :
1263							     VSTRIMRIGHT;
1264					c = pgetc();
1265					if (c == cc)
1266						subtype++;
1267					else
1268						pungetc();
1269					break;
1270				}
1271			}
1272		} else {
1273			pungetc();
1274		}
1275		if (subtype != VSLENGTH && (dblquote || arinest))
1276			flags |= VSQUOTE;
1277		*(stackblock() + typeloc) = subtype | flags;
1278		if (subtype != VSNORMAL)
1279			varnest++;
1280	}
1281	goto parsesub_return;
1282}
1283
1284
1285/*
1286 * Called to parse command substitutions.  Newstyle is set if the command
1287 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1288 * list of commands (passed by reference), and savelen is the number of
1289 * characters on the top of the stack which must be preserved.
1290 */
1291
1292parsebackq: {
1293	struct nodelist **nlpp;
1294	int savepbq;
1295	union node *n;
1296	char *volatile str;
1297	struct jmploc jmploc;
1298	struct jmploc *volatile savehandler;
1299	int savelen;
1300	int saveprompt;
1301#if __GNUC__
1302	/* Avoid longjmp clobbering */
1303	(void) &saveprompt;
1304#endif
1305
1306	savepbq = parsebackquote;
1307	if (setjmp(jmploc.loc)) {
1308		if (str)
1309			ckfree(str);
1310		parsebackquote = 0;
1311		handler = savehandler;
1312		longjmp(handler->loc, 1);
1313	}
1314	INTOFF;
1315	str = NULL;
1316	savelen = out - stackblock();
1317	if (savelen > 0) {
1318		str = ckmalloc(savelen);
1319		memcpy(str, stackblock(), savelen);
1320	}
1321	savehandler = handler;
1322	handler = &jmploc;
1323	INTON;
1324        if (oldstyle) {
1325                /* We must read until the closing backquote, giving special
1326                   treatment to some slashes, and then push the string and
1327                   reread it as input, interpreting it normally.  */
1328                char *out;
1329                int c;
1330                int savelen;
1331                char *str;
1332
1333
1334                STARTSTACKSTR(out);
1335		for (;;) {
1336			if (needprompt) {
1337				setprompt(2);
1338				needprompt = 0;
1339			}
1340			switch (c = pgetc()) {
1341			case '`':
1342				goto done;
1343
1344			case '\\':
1345                                if ((c = pgetc()) == '\n') {
1346					plinno++;
1347					if (doprompt)
1348						setprompt(2);
1349					else
1350						setprompt(0);
1351					/*
1352					 * If eating a newline, avoid putting
1353					 * the newline into the new character
1354					 * stream (via the STPUTC after the
1355					 * switch).
1356					 */
1357					continue;
1358				}
1359                                if (c != '\\' && c != '`' && c != '$'
1360                                    && (!dblquote || c != '"'))
1361                                        STPUTC('\\', out);
1362				break;
1363
1364			case '\n':
1365				plinno++;
1366				needprompt = doprompt;
1367				break;
1368
1369			case PEOF:
1370			        startlinno = plinno;
1371				synerror("EOF in backquote substitution");
1372 				break;
1373
1374			default:
1375				break;
1376			}
1377			STPUTC(c, out);
1378                }
1379done:
1380                STPUTC('\0', out);
1381                savelen = out - stackblock();
1382                if (savelen > 0) {
1383                        str = ckmalloc(savelen);
1384                        memcpy(str, stackblock(), savelen);
1385			setinputstring(str, 1);
1386                }
1387        }
1388	nlpp = &bqlist;
1389	while (*nlpp)
1390		nlpp = &(*nlpp)->next;
1391	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1392	(*nlpp)->next = NULL;
1393	parsebackquote = oldstyle;
1394
1395	if (oldstyle) {
1396		saveprompt = doprompt;
1397		doprompt = 0;
1398	}
1399
1400	n = list(0);
1401
1402	if (oldstyle)
1403		doprompt = saveprompt;
1404	else {
1405		if (readtoken() != TRP)
1406			synexpect(TRP);
1407	}
1408
1409	(*nlpp)->n = n;
1410        if (oldstyle) {
1411		/*
1412		 * Start reading from old file again, ignoring any pushed back
1413		 * tokens left from the backquote parsing
1414		 */
1415                popfile();
1416		tokpushback = 0;
1417	}
1418	while (stackblocksize() <= savelen)
1419		growstackblock();
1420	STARTSTACKSTR(out);
1421	if (str) {
1422		memcpy(out, str, savelen);
1423		STADJUST(savelen, out);
1424		INTOFF;
1425		ckfree(str);
1426		str = NULL;
1427		INTON;
1428	}
1429	parsebackquote = savepbq;
1430	handler = savehandler;
1431	if (arinest || dblquote)
1432		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1433	else
1434		USTPUTC(CTLBACKQ, out);
1435	if (oldstyle)
1436		goto parsebackq_oldreturn;
1437	else
1438		goto parsebackq_newreturn;
1439}
1440
1441/*
1442 * Parse an arithmetic expansion (indicate start of one and set state)
1443 */
1444parsearith: {
1445
1446	if (++arinest == 1) {
1447		prevsyntax = syntax;
1448		syntax = ARISYNTAX;
1449		USTPUTC(CTLARI, out);
1450		if (dblquote)
1451			USTPUTC('"',out);
1452		else
1453			USTPUTC(' ',out);
1454	} else {
1455		/*
1456		 * we collapse embedded arithmetic expansion to
1457		 * parenthesis, which should be equivalent
1458		 */
1459		USTPUTC('(', out);
1460	}
1461	goto parsearith_return;
1462}
1463
1464} /* end of readtoken */
1465
1466
1467
1468#ifdef mkinit
1469RESET {
1470	tokpushback = 0;
1471	checkkwd = 0;
1472}
1473#endif
1474
1475/*
1476 * Returns true if the text contains nothing to expand (no dollar signs
1477 * or backquotes).
1478 */
1479
1480STATIC int
1481noexpand(char *text)
1482{
1483	char *p;
1484	char c;
1485
1486	p = text;
1487	while ((c = *p++) != '\0') {
1488		if ( c == CTLQUOTEMARK)
1489			continue;
1490		if (c == CTLESC)
1491			p++;
1492		else if (BASESYNTAX[(int)c] == CCTL)
1493			return 0;
1494	}
1495	return 1;
1496}
1497
1498
1499/*
1500 * Return true if the argument is a legal variable name (a letter or
1501 * underscore followed by zero or more letters, underscores, and digits).
1502 */
1503
1504int
1505goodname(char *name)
1506{
1507	char *p;
1508
1509	p = name;
1510	if (! is_name(*p))
1511		return 0;
1512	while (*++p) {
1513		if (! is_in_name(*p))
1514			return 0;
1515	}
1516	return 1;
1517}
1518
1519
1520/*
1521 * Called when an unexpected token is read during the parse.  The argument
1522 * is the token that is expected, or -1 if more than one type of token can
1523 * occur at this point.
1524 */
1525
1526STATIC void
1527synexpect(int token)
1528{
1529	char msg[64];
1530
1531	if (token >= 0) {
1532		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1533			tokname[lasttoken], tokname[token]);
1534	} else {
1535		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1536	}
1537	synerror(msg);
1538}
1539
1540
1541STATIC void
1542synerror(char *msg)
1543{
1544	if (commandname)
1545		outfmt(&errout, "%s: %d: ", commandname, startlinno);
1546	outfmt(&errout, "Syntax error: %s\n", msg);
1547	error((char *)NULL);
1548}
1549
1550STATIC void
1551setprompt(int which)
1552{
1553	whichprompt = which;
1554
1555#ifndef NO_HISTORY
1556	if (!el)
1557#endif
1558		out2str(getprompt(NULL));
1559}
1560
1561/*
1562 * called by editline -- any expansions to the prompt
1563 *    should be added here.
1564 */
1565char *
1566getprompt(void *unused __unused)
1567{
1568	static char ps[PROMPTLEN];
1569	char *fmt;
1570	int i, j, trim;
1571
1572	/*
1573	 * Select prompt format.
1574	 */
1575	switch (whichprompt) {
1576	case 0:
1577		fmt = "";
1578		break;
1579	case 1:
1580		fmt = ps1val();
1581		break;
1582	case 2:
1583		fmt = ps2val();
1584		break;
1585	default:
1586		return "<internal prompt error>";
1587	}
1588
1589	/*
1590	 * Format prompt string.
1591	 */
1592	for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1593		if (*fmt == '\\')
1594			switch (*++fmt) {
1595
1596				/*
1597				 * Hostname.
1598				 *
1599				 * \h specifies just the local hostname,
1600				 * \H specifies fully-qualified hostname.
1601				 */
1602			case 'h':
1603			case 'H':
1604				ps[i] = '\0';
1605				gethostname(&ps[i], PROMPTLEN - i);
1606				/* Skip to end of hostname. */
1607				trim = (*fmt == 'h') ? '.' : '\0';
1608				while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1609					i++;
1610				break;
1611
1612				/*
1613				 * Working directory.
1614				 *
1615				 * \W specifies just the final component,
1616				 * \w specifies the entire path.
1617				 */
1618			case 'W':
1619			case 'w':
1620				ps[i] = '\0';
1621				getcwd(&ps[i], PROMPTLEN - i);
1622				if (*fmt == 'W') {
1623					/* Final path component only. */
1624					trim = 1;
1625					for (j = i; ps[j] != '\0'; j++)
1626					  if (ps[j] == '/')
1627						trim = j + 1;
1628					memmove(&ps[i], &ps[trim],
1629					    j - trim + 1);
1630				}
1631				/* Skip to end of path. */
1632				while (ps[i + 1] != '\0')
1633					i++;
1634				break;
1635
1636				/*
1637				 * Superuser status.
1638				 *
1639				 * '$' for normal users, '#' for root.
1640				 */
1641			case '$':
1642				ps[i] = (geteuid() != 0) ? '$' : '#';
1643				break;
1644
1645				/*
1646				 * A literal \.
1647				 */
1648			case '\\':
1649				ps[i] = '\\';
1650				break;
1651
1652				/*
1653				 * Emit unrecognized formats verbatim.
1654				 */
1655			default:
1656				ps[i++] = '\\';
1657				ps[i] = *fmt;
1658				break;
1659			}
1660		else
1661			ps[i] = *fmt;
1662	ps[i] = '\0';
1663	return (ps);
1664}
1665