1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1994, by Sun Microsytems, Inc.
24*0Sstevel@tonic-gate */
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate * Includes
30*0Sstevel@tonic-gate */
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <stdarg.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <libintl.h>
38*0Sstevel@tonic-gate #include <sys/stat.h>
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/param.h>
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #include "new.h"
43*0Sstevel@tonic-gate #include "queue.h"
44*0Sstevel@tonic-gate #include "source.h"
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate * Typedefs
49*0Sstevel@tonic-gate */
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate typedef struct source {
52*0Sstevel@tonic-gate queue_node_t qn;
53*0Sstevel@tonic-gate char *path;
54*0Sstevel@tonic-gate FILE *instream;
55*0Sstevel@tonic-gate int linenum;
56*0Sstevel@tonic-gate boolean_t isatty;
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate } source_t;
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate * Defines
63*0Sstevel@tonic-gate */
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate #define HOME "HOME"
66*0Sstevel@tonic-gate #define PREXRC ".prexrc"
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate /*
70*0Sstevel@tonic-gate * Globals
71*0Sstevel@tonic-gate */
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate static queue_node_t stack;
74*0Sstevel@tonic-gate static source_t *top;
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate * source_init() - initializes the source stack
79*0Sstevel@tonic-gate */
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate void
source_init(void)82*0Sstevel@tonic-gate source_init(void)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate source_t *new_p;
85*0Sstevel@tonic-gate struct stat statbuf;
86*0Sstevel@tonic-gate char *home;
87*0Sstevel@tonic-gate int retval;
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate /* initialize the stack queue head */
91*0Sstevel@tonic-gate queue_init(&stack);
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate /* stick the standard input on the bottom of the stack */
94*0Sstevel@tonic-gate new_p = new(source_t);
95*0Sstevel@tonic-gate queue_init(&new_p->qn);
96*0Sstevel@tonic-gate new_p->path = strdup("<STDIN>");
97*0Sstevel@tonic-gate new_p->instream = stdin;
98*0Sstevel@tonic-gate new_p->linenum = 1;
99*0Sstevel@tonic-gate new_p->isatty = isatty(fileno(new_p->instream));
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate (void) queue_prepend(&stack, &new_p->qn);
102*0Sstevel@tonic-gate top = new_p;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate * since we are pushing onto a stack, we invert the search order *
106*0Sstevel@tonic-gate * and push the prexrc in the current directory on next.
107*0Sstevel@tonic-gate */
108*0Sstevel@tonic-gate retval = stat(PREXRC, &statbuf);
109*0Sstevel@tonic-gate if (retval != -1) {
110*0Sstevel@tonic-gate source_file(PREXRC);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate home = getenv(HOME);
113*0Sstevel@tonic-gate if (home) {
114*0Sstevel@tonic-gate char path[MAXPATHLEN];
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate if ((strlen(home) + strlen(PREXRC) + 2) < (size_t) MAXPATHLEN) {
117*0Sstevel@tonic-gate (void) sprintf(path, "%s/%s", home, PREXRC);
118*0Sstevel@tonic-gate retval = stat(path, &statbuf);
119*0Sstevel@tonic-gate if (retval != -1) {
120*0Sstevel@tonic-gate source_file(path);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate } /* end source_init */
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate * source_file() - pushes a new source onto the stack
129*0Sstevel@tonic-gate */
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate void
source_file(char * path)132*0Sstevel@tonic-gate source_file(char *path)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate FILE *newfile;
135*0Sstevel@tonic-gate source_t *new_p;
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gate newfile = fopen(path, "r");
138*0Sstevel@tonic-gate if (!newfile) {
139*0Sstevel@tonic-gate semantic_err(gettext("cannot open \"%s\""), path);
140*0Sstevel@tonic-gate return;
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate new_p = new(source_t);
143*0Sstevel@tonic-gate queue_init(&new_p->qn);
144*0Sstevel@tonic-gate new_p->path = strdup(path);
145*0Sstevel@tonic-gate new_p->instream = newfile;
146*0Sstevel@tonic-gate new_p->linenum = 1;
147*0Sstevel@tonic-gate new_p->isatty = isatty(fileno(new_p->instream));
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate (void) queue_prepend(&stack, &new_p->qn);
150*0Sstevel@tonic-gate top = new_p;
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate } /* end source_file */
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate * source_input() - lexical analyzer input routine
157*0Sstevel@tonic-gate */
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate extern void quit(boolean_t, boolean_t);
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate int
source_input(void)162*0Sstevel@tonic-gate source_input(void)
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate int c;
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate if (!top)
167*0Sstevel@tonic-gate return (0);
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate c = getc(top->instream);
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate if (c == EOF) {
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate * If we get an EOF at the top level, we quit if we are *
174*0Sstevel@tonic-gate * non-interactive, pretend we saw a new-line if we are *
175*0Sstevel@tonic-gate * interactive.
176*0Sstevel@tonic-gate */
177*0Sstevel@tonic-gate if (top->instream == stdin) {
178*0Sstevel@tonic-gate if (top->isatty) {
179*0Sstevel@tonic-gate source_output('\n');
180*0Sstevel@tonic-gate return ('\n');
181*0Sstevel@tonic-gate } else
182*0Sstevel@tonic-gate quit(B_TRUE, B_TRUE);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate /* we've exhausted the current stream, pop it, delete it ... */
185*0Sstevel@tonic-gate if (top->path)
186*0Sstevel@tonic-gate free(top->path);
187*0Sstevel@tonic-gate (void) fclose(top->instream);
188*0Sstevel@tonic-gate (void) queue_remove(&top->qn);
189*0Sstevel@tonic-gate free(top);
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate /* point to the new top level i/o stream */
192*0Sstevel@tonic-gate top = (source_t *) queue_next(&stack, &stack);
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate if (!top)
195*0Sstevel@tonic-gate return (0);
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate /* trigger a prompt if neccessary */
198*0Sstevel@tonic-gate prompt();
199*0Sstevel@tonic-gate return (source_input());
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate return (c);
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate } /* end source_input */
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate * source_unput() - lexical analyzer unput routine
208*0Sstevel@tonic-gate */
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate void
source_unput(int c)211*0Sstevel@tonic-gate source_unput(int c)
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate if (top)
214*0Sstevel@tonic-gate (void) ungetc(c, top->instream);
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate } /* end source_unput */
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gate /*
220*0Sstevel@tonic-gate * source_output() - lexical analyzer output routine
221*0Sstevel@tonic-gate */
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate void
source_output(int c)224*0Sstevel@tonic-gate source_output(int c)
225*0Sstevel@tonic-gate {
226*0Sstevel@tonic-gate (void) putc(c, stdout);
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate } /* end source_output */
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate /*
232*0Sstevel@tonic-gate * source_nl() - increment the line counter
233*0Sstevel@tonic-gate */
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gate void
source_nl(void)236*0Sstevel@tonic-gate source_nl(void)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate if (top)
239*0Sstevel@tonic-gate top->linenum++;
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate } /* end source_nl */
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate * yyerror() -
246*0Sstevel@tonic-gate */
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate extern char yytext[];
249*0Sstevel@tonic-gate extern int g_linenum;
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate void
yyerror(char * s)252*0Sstevel@tonic-gate yyerror(char *s)
253*0Sstevel@tonic-gate {
254*0Sstevel@tonic-gate (void) fprintf(stderr,
255*0Sstevel@tonic-gate gettext("\"%s\", line %d: %s on or before \"%s\"\n"),
256*0Sstevel@tonic-gate top->path, top->linenum, s, yytext);
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate /*
262*0Sstevel@tonic-gate * yywrap() -
263*0Sstevel@tonic-gate */
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate int
yywrap()266*0Sstevel@tonic-gate yywrap()
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate return (1);
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate } /* end yywrap */
271*0Sstevel@tonic-gate
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate /*
274*0Sstevel@tonic-gate * prompt() -
275*0Sstevel@tonic-gate */
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate extern char **g_argv;
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate void
prompt(void)280*0Sstevel@tonic-gate prompt(void)
281*0Sstevel@tonic-gate {
282*0Sstevel@tonic-gate if (top && top->isatty)
283*0Sstevel@tonic-gate (void) printf("%s> ", g_argv[0]);
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate } /* end g_prompt */
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate /*
289*0Sstevel@tonic-gate * semantic_err() - reports a semantic error
290*0Sstevel@tonic-gate */
291*0Sstevel@tonic-gate
292*0Sstevel@tonic-gate void
semantic_err(char * format,...)293*0Sstevel@tonic-gate semantic_err(char *format, ...)
294*0Sstevel@tonic-gate {
295*0Sstevel@tonic-gate va_list ap;
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate va_start(ap, format);
298*0Sstevel@tonic-gate
299*0Sstevel@tonic-gate if (!top)
300*0Sstevel@tonic-gate return;
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("\"%s\", line %d: semantic error: "),
303*0Sstevel@tonic-gate top->path, top->linenum);
304*0Sstevel@tonic-gate (void) vfprintf(stderr, format, ap);
305*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("\n"));
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate } /* end semantic_err */
308