17c478bd9Sstevel@tonic-gate %{
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate * CDDL HEADER START
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
67c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
77c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
87c478bd9Sstevel@tonic-gate * with the License.
97c478bd9Sstevel@tonic-gate *
107c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
117c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
127c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
137c478bd9Sstevel@tonic-gate * and limitations under the License.
147c478bd9Sstevel@tonic-gate *
157c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
167c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
177c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
187c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
197c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
207c478bd9Sstevel@tonic-gate *
217c478bd9Sstevel@tonic-gate * CDDL HEADER END
227c478bd9Sstevel@tonic-gate */
237c478bd9Sstevel@tonic-gate %}
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
29b390fe2cSmuffin /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
30b390fe2cSmuffin /* All Rights Reserved */
31b390fe2cSmuffin
327c478bd9Sstevel@tonic-gate %{
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdarg.h>
357c478bd9Sstevel@tonic-gate #include <limits.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <locale.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate
40b390fe2cSmuffin static void getout(int) __NORETURN;
417c478bd9Sstevel@tonic-gate static int *bundle(int, ...);
427c478bd9Sstevel@tonic-gate static void usage(void);
43b390fe2cSmuffin
44b390fe2cSmuffin int cpeek(char, int, char, int, char);
45*1a90c98dSToomas Soome int yyerror(const char *);
46b390fe2cSmuffin
477c478bd9Sstevel@tonic-gate #define STRING_SIZE (BC_STRING_MAX + 3) /* string plus quotes */
487c478bd9Sstevel@tonic-gate /* plus NULL */
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate FILE *in;
517c478bd9Sstevel@tonic-gate char cary[LINE_MAX+1];
527c478bd9Sstevel@tonic-gate char *cp = { cary };
537c478bd9Sstevel@tonic-gate char *cpend = &cary[LINE_MAX]; /* last address (not the null char) */
547c478bd9Sstevel@tonic-gate char string[STRING_SIZE];
557c478bd9Sstevel@tonic-gate char *str = { string };
567c478bd9Sstevel@tonic-gate int crs = '0';
577c478bd9Sstevel@tonic-gate int rcrs = '0'; /* reset crs */
587c478bd9Sstevel@tonic-gate int bindx = 0;
597c478bd9Sstevel@tonic-gate int lev = 0; /* current scope level */
607c478bd9Sstevel@tonic-gate int ln; /* line number of current file */
617c478bd9Sstevel@tonic-gate int *ttp;
627c478bd9Sstevel@tonic-gate char *ss; /* current input source */
637c478bd9Sstevel@tonic-gate int bstack[10] = { 0 };
647c478bd9Sstevel@tonic-gate char *numb[15] = {
657c478bd9Sstevel@tonic-gate " 0", " 1", " 2", " 3", " 4", " 5",
667c478bd9Sstevel@tonic-gate " 6", " 7", " 8", " 9", " 10", " 11",
677c478bd9Sstevel@tonic-gate " 12", " 13", " 14"
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate int *pre, *post;
707c478bd9Sstevel@tonic-gate int interact = 0; /* talking to a tty? */
717c478bd9Sstevel@tonic-gate %}
72724c5febSToomas Soome
73724c5febSToomas Soome %union {
74724c5febSToomas Soome int *iptr;
75724c5febSToomas Soome char *cptr;
76724c5febSToomas Soome int cc;
77724c5febSToomas Soome }
78724c5febSToomas Soome %start start;
79724c5febSToomas Soome %type <iptr> stat def slist dlets e
80724c5febSToomas Soome %type <iptr> re fprefix cargs eora cons constant lora
81724c5febSToomas Soome %right '='
82724c5febSToomas Soome %left '+' '-'
83724c5febSToomas Soome %left '*' '/' '%'
84724c5febSToomas Soome %right '^'
85724c5febSToomas Soome %left UMINUS
86724c5febSToomas Soome
87724c5febSToomas Soome %token <cptr> LETTER
88724c5febSToomas Soome %type <cptr> EQOP CRS
89724c5febSToomas Soome %token <cc> DIGIT SQRT LENGTH _IF FFF EQ
90724c5febSToomas Soome %token <cc> _WHILE _FOR NE LE GE INCR DECR
91724c5febSToomas Soome %token <cc> _RETURN _BREAK _DEFINE BASE OBASE SCALE
92724c5febSToomas Soome %token <cc> EQPL EQMI EQMUL EQDIV EQREM EQEXP
93724c5febSToomas Soome %token <cptr> _AUTO DOT
94724c5febSToomas Soome %token <cc> QSTR
95724c5febSToomas Soome
967c478bd9Sstevel@tonic-gate %%
977c478bd9Sstevel@tonic-gate start :
987c478bd9Sstevel@tonic-gate | start stat tail
99724c5febSToomas Soome {
1007c478bd9Sstevel@tonic-gate output($2);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate | start def dargs ')' '{' dlist slist '}'
103724c5febSToomas Soome {
1047c478bd9Sstevel@tonic-gate ttp = bundle(6, pre, $7, post, "0", numb[lev], "Q");
105b390fe2cSmuffin conout(ttp, (char *)$2);
1067c478bd9Sstevel@tonic-gate rcrs = crs;
107b390fe2cSmuffin output((int *)"");
1087c478bd9Sstevel@tonic-gate lev = bindx = 0;
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate ;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate dlist : tail
1137c478bd9Sstevel@tonic-gate | dlist _AUTO dlets tail
1147c478bd9Sstevel@tonic-gate ;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate stat : e
117724c5febSToomas Soome {
118724c5febSToomas Soome bundle(2, $1, "ps.");
119724c5febSToomas Soome }
1207c478bd9Sstevel@tonic-gate |
121724c5febSToomas Soome {
122724c5febSToomas Soome bundle(1, "");
123724c5febSToomas Soome }
1247c478bd9Sstevel@tonic-gate | QSTR
125724c5febSToomas Soome {
126724c5febSToomas Soome bundle(3, "[", $1, "]P");
127724c5febSToomas Soome }
1287c478bd9Sstevel@tonic-gate | LETTER '=' e
129724c5febSToomas Soome {
130724c5febSToomas Soome bundle(3, $3, "s", $1);
131724c5febSToomas Soome }
1327c478bd9Sstevel@tonic-gate | LETTER '[' e ']' '=' e
133724c5febSToomas Soome {
134724c5febSToomas Soome bundle(4, $6, $3, ":", geta($1));
135724c5febSToomas Soome }
1367c478bd9Sstevel@tonic-gate | LETTER EQOP e
137724c5febSToomas Soome {
138724c5febSToomas Soome bundle(6, "l", $1, $3, $2, "s", $1);
139724c5febSToomas Soome }
1407c478bd9Sstevel@tonic-gate | LETTER '[' e ']' EQOP e
141724c5febSToomas Soome {
142724c5febSToomas Soome bundle(8, $3, ";", geta($1), $6, $5, $3, ":", geta($1));
143724c5febSToomas Soome }
1447c478bd9Sstevel@tonic-gate | _BREAK
145724c5febSToomas Soome {
146724c5febSToomas Soome bundle(2, numb[lev-bstack[bindx-1]], "Q");
147724c5febSToomas Soome }
1487c478bd9Sstevel@tonic-gate | _RETURN '(' e ')'
149724c5febSToomas Soome {
150724c5febSToomas Soome bundle(4, $3, post, numb[lev], "Q");
151724c5febSToomas Soome }
1527c478bd9Sstevel@tonic-gate | _RETURN '(' ')'
153724c5febSToomas Soome {
154724c5febSToomas Soome bundle(4, "0", post, numb[lev], "Q");
155724c5febSToomas Soome }
1567c478bd9Sstevel@tonic-gate | _RETURN
157724c5febSToomas Soome {
158724c5febSToomas Soome bundle(4, "0", post, numb[lev], "Q");
159724c5febSToomas Soome }
1607c478bd9Sstevel@tonic-gate | SCALE '=' e
161724c5febSToomas Soome {
162724c5febSToomas Soome bundle(2, $3, "k");
163724c5febSToomas Soome }
1647c478bd9Sstevel@tonic-gate | SCALE EQOP e
165724c5febSToomas Soome {
166724c5febSToomas Soome bundle(4, "K", $3, $2, "k");
167724c5febSToomas Soome }
1687c478bd9Sstevel@tonic-gate | BASE '=' e
169724c5febSToomas Soome {
170724c5febSToomas Soome bundle(2, $3, "i");
171724c5febSToomas Soome }
1727c478bd9Sstevel@tonic-gate | BASE EQOP e
173724c5febSToomas Soome {
174724c5febSToomas Soome bundle(4, "I", $3, $2, "i");
175724c5febSToomas Soome }
1767c478bd9Sstevel@tonic-gate | OBASE '=' e
177724c5febSToomas Soome {
178724c5febSToomas Soome bundle(2, $3, "o");
179724c5febSToomas Soome }
1807c478bd9Sstevel@tonic-gate | OBASE EQOP e
181724c5febSToomas Soome {
182724c5febSToomas Soome bundle(4, "O", $3, $2, "o");
183724c5febSToomas Soome }
1847c478bd9Sstevel@tonic-gate | '{' slist '}'
185724c5febSToomas Soome {
1867c478bd9Sstevel@tonic-gate $$ = $2;
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate | FFF
189724c5febSToomas Soome {
190724c5febSToomas Soome bundle(1, "fY");
191724c5febSToomas Soome }
1927c478bd9Sstevel@tonic-gate | error
193724c5febSToomas Soome {
194724c5febSToomas Soome bundle(1, "c");
195724c5febSToomas Soome }
1967c478bd9Sstevel@tonic-gate | _IF CRS BLEV '(' re ')' stat
197724c5febSToomas Soome {
1987c478bd9Sstevel@tonic-gate conout($7, $2);
1997c478bd9Sstevel@tonic-gate bundle(3, $5, $2, " ");
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate | _WHILE CRS '(' re ')' stat BLEV
202724c5febSToomas Soome {
2037c478bd9Sstevel@tonic-gate bundle(3, $6, $4, $2);
2047c478bd9Sstevel@tonic-gate conout($$, $2);
2057c478bd9Sstevel@tonic-gate bundle(3, $4, $2, " ");
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate | fprefix CRS re ';' e ')' stat BLEV
208724c5febSToomas Soome {
2097c478bd9Sstevel@tonic-gate bundle(5, $7, $5, "s.", $3, $2);
2107c478bd9Sstevel@tonic-gate conout($$, $2);
2117c478bd9Sstevel@tonic-gate bundle(5, $1, "s.", $3, $2, " ");
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate | '~' LETTER '=' e
214724c5febSToomas Soome {
215724c5febSToomas Soome bundle(3, $4, "S", $2);
216724c5febSToomas Soome }
2177c478bd9Sstevel@tonic-gate ;
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate EQOP : EQPL
220724c5febSToomas Soome {
2217c478bd9Sstevel@tonic-gate $$ = "+";
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate | EQMI
224724c5febSToomas Soome {
2257c478bd9Sstevel@tonic-gate $$ = "-";
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate | EQMUL
228724c5febSToomas Soome {
2297c478bd9Sstevel@tonic-gate $$ = "*";
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate | EQDIV
232724c5febSToomas Soome {
2337c478bd9Sstevel@tonic-gate $$ = "/";
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate | EQREM
236724c5febSToomas Soome {
2377c478bd9Sstevel@tonic-gate $$ = "%%";
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate | EQEXP
240724c5febSToomas Soome {
2417c478bd9Sstevel@tonic-gate $$ = "^";
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate ;
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate fprefix : _FOR '(' e ';'
246724c5febSToomas Soome {
2477c478bd9Sstevel@tonic-gate $$ = $3;
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate ;
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate BLEV :
252724c5febSToomas Soome {
253724c5febSToomas Soome --bindx;
254724c5febSToomas Soome }
2557c478bd9Sstevel@tonic-gate ;
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate slist : stat
2587c478bd9Sstevel@tonic-gate | slist tail stat
259724c5febSToomas Soome {
260724c5febSToomas Soome bundle(2, $1, $3);
261724c5febSToomas Soome }
2627c478bd9Sstevel@tonic-gate ;
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate tail : '\n'
265724c5febSToomas Soome {
2667c478bd9Sstevel@tonic-gate ln++;
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate | ';'
2697c478bd9Sstevel@tonic-gate ;
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate re : e EQ e
272724c5febSToomas Soome {
2737c478bd9Sstevel@tonic-gate $$ = bundle(3, $1, $3, "=");
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate | e '<' e
276724c5febSToomas Soome {
277724c5febSToomas Soome bundle(3, $1, $3, ">");
278724c5febSToomas Soome }
2797c478bd9Sstevel@tonic-gate | e '>' e
280724c5febSToomas Soome {
281724c5febSToomas Soome bundle(3, $1, $3, "<");
282724c5febSToomas Soome }
2837c478bd9Sstevel@tonic-gate | e NE e
284724c5febSToomas Soome {
285724c5febSToomas Soome bundle(3, $1, $3, "!=");
286724c5febSToomas Soome }
2877c478bd9Sstevel@tonic-gate | e GE e
288724c5febSToomas Soome {
289724c5febSToomas Soome bundle(3, $1, $3, "!>");
290724c5febSToomas Soome }
2917c478bd9Sstevel@tonic-gate | e LE e
292724c5febSToomas Soome {
293724c5febSToomas Soome bundle(3, $1, $3, "!<");
294724c5febSToomas Soome }
2957c478bd9Sstevel@tonic-gate | e
296724c5febSToomas Soome {
297724c5febSToomas Soome bundle(2, $1, " 0!=");
298724c5febSToomas Soome }
2997c478bd9Sstevel@tonic-gate ;
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate e : e '+' e
302724c5febSToomas Soome {
303724c5febSToomas Soome bundle(3, $1, $3, "+");
304724c5febSToomas Soome }
3057c478bd9Sstevel@tonic-gate | e '-' e
306724c5febSToomas Soome {
307724c5febSToomas Soome bundle(3, $1, $3, "-");
308724c5febSToomas Soome }
3097c478bd9Sstevel@tonic-gate | '-' e %prec UMINUS
310724c5febSToomas Soome {
311724c5febSToomas Soome bundle(3, " 0", $2, "-");
312724c5febSToomas Soome }
3137c478bd9Sstevel@tonic-gate | e '*' e
314724c5febSToomas Soome {
315724c5febSToomas Soome bundle(3, $1, $3, "*");
316724c5febSToomas Soome }
3177c478bd9Sstevel@tonic-gate | e '/' e
318724c5febSToomas Soome {
319724c5febSToomas Soome bundle(3, $1, $3, "/");
320724c5febSToomas Soome }
3217c478bd9Sstevel@tonic-gate | e '%' e
322724c5febSToomas Soome {
323724c5febSToomas Soome bundle(3, $1, $3, "%%");
324724c5febSToomas Soome }
3257c478bd9Sstevel@tonic-gate | e '^' e
326724c5febSToomas Soome {
327724c5febSToomas Soome bundle(3, $1, $3, "^");
328724c5febSToomas Soome }
3297c478bd9Sstevel@tonic-gate | LETTER '[' e ']'
330724c5febSToomas Soome {
331724c5febSToomas Soome bundle(3, $3, ";", geta($1));
332724c5febSToomas Soome }
3337c478bd9Sstevel@tonic-gate | LETTER INCR
334724c5febSToomas Soome {
335724c5febSToomas Soome bundle(4, "l", $1, "d1+s", $1);
336724c5febSToomas Soome }
3377c478bd9Sstevel@tonic-gate | INCR LETTER
338724c5febSToomas Soome {
339724c5febSToomas Soome bundle(4, "l", $2, "1+ds", $2);
340724c5febSToomas Soome }
3417c478bd9Sstevel@tonic-gate | DECR LETTER
342724c5febSToomas Soome {
343724c5febSToomas Soome bundle(4, "l", $2, "1-ds", $2);
344724c5febSToomas Soome }
3457c478bd9Sstevel@tonic-gate | LETTER DECR
346724c5febSToomas Soome {
347724c5febSToomas Soome bundle(4, "l", $1, "d1-s", $1);
348724c5febSToomas Soome }
3497c478bd9Sstevel@tonic-gate | LETTER '[' e ']' INCR
350724c5febSToomas Soome {
351724c5febSToomas Soome bundle(7, $3, ";", geta($1), "d1+", $3, ":", geta($1));
352724c5febSToomas Soome }
3537c478bd9Sstevel@tonic-gate | INCR LETTER '[' e ']'
354724c5febSToomas Soome {
355724c5febSToomas Soome bundle(7, $4, ";", geta($2), "1+d", $4, ":", geta($2));
356724c5febSToomas Soome }
3577c478bd9Sstevel@tonic-gate | LETTER '[' e ']' DECR
358724c5febSToomas Soome {
359724c5febSToomas Soome bundle(7, $3, ";", geta($1), "d1-", $3, ":", geta($1));
360724c5febSToomas Soome }
3617c478bd9Sstevel@tonic-gate | DECR LETTER '[' e ']'
362724c5febSToomas Soome {
363724c5febSToomas Soome bundle(7, $4, ";", geta($2), "1-d", $4, ":", geta($2));
364724c5febSToomas Soome }
3657c478bd9Sstevel@tonic-gate | SCALE INCR
366724c5febSToomas Soome {
367724c5febSToomas Soome bundle(1, "Kd1+k");
368724c5febSToomas Soome }
3697c478bd9Sstevel@tonic-gate | INCR SCALE
370724c5febSToomas Soome {
371724c5febSToomas Soome bundle(1, "K1+dk");
372724c5febSToomas Soome }
3737c478bd9Sstevel@tonic-gate | SCALE DECR
374724c5febSToomas Soome {
375724c5febSToomas Soome bundle(1, "Kd1-k");
376724c5febSToomas Soome }
3777c478bd9Sstevel@tonic-gate | DECR SCALE
378724c5febSToomas Soome {
379724c5febSToomas Soome bundle(1, "K1-dk");
380724c5febSToomas Soome }
3817c478bd9Sstevel@tonic-gate | BASE INCR
382724c5febSToomas Soome {
383724c5febSToomas Soome bundle(1, "Id1+i");
384724c5febSToomas Soome }
3857c478bd9Sstevel@tonic-gate | INCR BASE
386724c5febSToomas Soome {
387724c5febSToomas Soome bundle(1, "I1+di");
388724c5febSToomas Soome }
3897c478bd9Sstevel@tonic-gate | BASE DECR
390724c5febSToomas Soome {
391724c5febSToomas Soome bundle(1, "Id1-i");
392724c5febSToomas Soome }
3937c478bd9Sstevel@tonic-gate | DECR BASE
394724c5febSToomas Soome {
395724c5febSToomas Soome bundle(1, "I1-di");
396724c5febSToomas Soome }
3977c478bd9Sstevel@tonic-gate | OBASE INCR
398724c5febSToomas Soome {
399724c5febSToomas Soome bundle(1, "Od1+o");
400724c5febSToomas Soome }
4017c478bd9Sstevel@tonic-gate | INCR OBASE
402724c5febSToomas Soome {
403724c5febSToomas Soome bundle(1, "O1+do");
404724c5febSToomas Soome }
4057c478bd9Sstevel@tonic-gate | OBASE DECR
406724c5febSToomas Soome {
407724c5febSToomas Soome bundle(1, "Od1-o");
408724c5febSToomas Soome }
4097c478bd9Sstevel@tonic-gate | DECR OBASE
410724c5febSToomas Soome {
411724c5febSToomas Soome bundle(1, "O1-do");
412724c5febSToomas Soome }
4137c478bd9Sstevel@tonic-gate | LETTER '(' cargs ')'
414724c5febSToomas Soome {
415724c5febSToomas Soome bundle(4, $3, "l", getf($1), "x");
416724c5febSToomas Soome }
4177c478bd9Sstevel@tonic-gate | LETTER '(' ')'
418724c5febSToomas Soome {
419724c5febSToomas Soome bundle(3, "l", getf($1), "x");
420724c5febSToomas Soome }
4217c478bd9Sstevel@tonic-gate | cons
422724c5febSToomas Soome {
423724c5febSToomas Soome bundle(2, " ", $1);
424724c5febSToomas Soome }
4257c478bd9Sstevel@tonic-gate | DOT cons
426724c5febSToomas Soome {
427724c5febSToomas Soome bundle(2, " .", $2);
428724c5febSToomas Soome }
4297c478bd9Sstevel@tonic-gate | cons DOT cons
430724c5febSToomas Soome {
431724c5febSToomas Soome bundle(4, " ", $1, ".", $3);
432724c5febSToomas Soome }
4337c478bd9Sstevel@tonic-gate | cons DOT
434724c5febSToomas Soome {
435724c5febSToomas Soome bundle(3, " ", $1, ".");
436724c5febSToomas Soome }
4377c478bd9Sstevel@tonic-gate | DOT
438724c5febSToomas Soome {
4397c478bd9Sstevel@tonic-gate $<cptr>$ = "l.";
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate | LETTER
442724c5febSToomas Soome {
443724c5febSToomas Soome bundle(2, "l", $1);
444724c5febSToomas Soome }
4457c478bd9Sstevel@tonic-gate | LETTER '=' e
446724c5febSToomas Soome {
447724c5febSToomas Soome bundle(3, $3, "ds", $1);
448724c5febSToomas Soome }
4497c478bd9Sstevel@tonic-gate | LETTER EQOP e %prec '='
450724c5febSToomas Soome {
451724c5febSToomas Soome bundle(6, "l", $1, $3, $2, "ds", $1);
452724c5febSToomas Soome }
4537c478bd9Sstevel@tonic-gate | LETTER '[' e ']' '=' e
454724c5febSToomas Soome {
455724c5febSToomas Soome bundle(5, $6, "d", $3, ":", geta($1));
456724c5febSToomas Soome }
4577c478bd9Sstevel@tonic-gate | LETTER '[' e ']' EQOP e
458724c5febSToomas Soome {
4597c478bd9Sstevel@tonic-gate bundle(9, $3, ";", geta($1), $6, $5, "d", $3, ":",
4607c478bd9Sstevel@tonic-gate geta($1));
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate | LENGTH '(' e ')'
463724c5febSToomas Soome {
464724c5febSToomas Soome bundle(2, $3, "Z");
465724c5febSToomas Soome }
4667c478bd9Sstevel@tonic-gate | SCALE '(' e ')'
467724c5febSToomas Soome {
468724c5febSToomas Soome bundle(2, $3, "X"); /* must be before '(' e ')' */
469724c5febSToomas Soome }
4707c478bd9Sstevel@tonic-gate | '(' e ')'
471724c5febSToomas Soome {
4727c478bd9Sstevel@tonic-gate $$ = $2;
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate | '?'
475724c5febSToomas Soome {
476724c5febSToomas Soome bundle(1, "?");
477724c5febSToomas Soome }
4787c478bd9Sstevel@tonic-gate | SQRT '(' e ')'
479724c5febSToomas Soome {
480724c5febSToomas Soome bundle(2, $3, "v");
481724c5febSToomas Soome }
4827c478bd9Sstevel@tonic-gate | '~' LETTER
483724c5febSToomas Soome {
484724c5febSToomas Soome bundle(2, "L", $2);
485724c5febSToomas Soome }
4867c478bd9Sstevel@tonic-gate | SCALE '=' e
487724c5febSToomas Soome {
488724c5febSToomas Soome bundle(2, $3, "dk");
489724c5febSToomas Soome }
4907c478bd9Sstevel@tonic-gate | SCALE EQOP e %prec '='
491724c5febSToomas Soome {
492724c5febSToomas Soome bundle(4, "K", $3, $2, "dk");
493724c5febSToomas Soome }
4947c478bd9Sstevel@tonic-gate | BASE '=' e
495724c5febSToomas Soome {
496724c5febSToomas Soome bundle(2, $3, "di");
497724c5febSToomas Soome }
4987c478bd9Sstevel@tonic-gate | BASE EQOP e %prec '='
499724c5febSToomas Soome {
500724c5febSToomas Soome bundle(4, "I", $3, $2, "di");
501724c5febSToomas Soome }
5027c478bd9Sstevel@tonic-gate | OBASE '=' e
503724c5febSToomas Soome {
504724c5febSToomas Soome bundle(2, $3, "do");
505724c5febSToomas Soome }
5067c478bd9Sstevel@tonic-gate | OBASE EQOP e %prec '='
507724c5febSToomas Soome {
508724c5febSToomas Soome bundle(4, "O", $3, $2, "do");
509724c5febSToomas Soome }
5107c478bd9Sstevel@tonic-gate | SCALE
511724c5febSToomas Soome {
512724c5febSToomas Soome bundle(1, "K");
513724c5febSToomas Soome }
5147c478bd9Sstevel@tonic-gate | BASE
515724c5febSToomas Soome {
516724c5febSToomas Soome bundle(1, "I");
517724c5febSToomas Soome }
5187c478bd9Sstevel@tonic-gate | OBASE
519724c5febSToomas Soome {
520724c5febSToomas Soome bundle(1, "O");
521724c5febSToomas Soome }
5227c478bd9Sstevel@tonic-gate ;
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate cargs : eora
5257c478bd9Sstevel@tonic-gate | cargs ',' eora
526724c5febSToomas Soome {
527724c5febSToomas Soome bundle(2, $1, $3);
528724c5febSToomas Soome }
5297c478bd9Sstevel@tonic-gate ;
5307c478bd9Sstevel@tonic-gate eora : e
5317c478bd9Sstevel@tonic-gate | LETTER '[' ']'
532724c5febSToomas Soome {
533724c5febSToomas Soome bundle(2, "l", geta($1));
534724c5febSToomas Soome }
5357c478bd9Sstevel@tonic-gate ;
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate cons : constant
538724c5febSToomas Soome {
5397c478bd9Sstevel@tonic-gate *cp++ = '\0';
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate constant: '_'
543724c5febSToomas Soome {
5447c478bd9Sstevel@tonic-gate checkbuffer();
5457c478bd9Sstevel@tonic-gate $<cptr>$ = cp;
5467c478bd9Sstevel@tonic-gate *cp++ = '_';
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate | DIGIT
549724c5febSToomas Soome {
5507c478bd9Sstevel@tonic-gate checkbuffer();
5517c478bd9Sstevel@tonic-gate $<cptr>$ = cp;
5527c478bd9Sstevel@tonic-gate *cp++ = $1;
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate | constant DIGIT
555724c5febSToomas Soome {
5567c478bd9Sstevel@tonic-gate checkbuffer();
5577c478bd9Sstevel@tonic-gate *cp++ = $2;
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate ;
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate CRS :
562724c5febSToomas Soome {
5637c478bd9Sstevel@tonic-gate checkbuffer();
5647c478bd9Sstevel@tonic-gate $$ = cp;
5657c478bd9Sstevel@tonic-gate *cp++ = crs++;
5667c478bd9Sstevel@tonic-gate *cp++ = '\0';
5677c478bd9Sstevel@tonic-gate if (crs == '[')
5687c478bd9Sstevel@tonic-gate crs += 3;
5697c478bd9Sstevel@tonic-gate if (crs == 'a')
5707c478bd9Sstevel@tonic-gate crs = '{';
5717c478bd9Sstevel@tonic-gate if (crs >= 0241) {
572*1a90c98dSToomas Soome (void) yyerror("program too big");
5737c478bd9Sstevel@tonic-gate getout(1);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate bstack[bindx++] = lev++;
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate ;
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate def : _DEFINE LETTER '('
580724c5febSToomas Soome {
5817c478bd9Sstevel@tonic-gate $$ = getf($2);
5827c478bd9Sstevel@tonic-gate pre = (int *)"";
5837c478bd9Sstevel@tonic-gate post = (int *)"";
5847c478bd9Sstevel@tonic-gate lev = 1;
5857c478bd9Sstevel@tonic-gate bstack[bindx = 0] = 0;
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate ;
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate dargs : /* empty */
5907c478bd9Sstevel@tonic-gate | lora
591724c5febSToomas Soome {
5927c478bd9Sstevel@tonic-gate pp($1);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate | dargs ',' lora
595724c5febSToomas Soome {
5967c478bd9Sstevel@tonic-gate pp($3);
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate ;
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate dlets : lora
601724c5febSToomas Soome {
602724c5febSToomas Soome tp($1);
603724c5febSToomas Soome }
6047c478bd9Sstevel@tonic-gate | dlets ',' lora
605724c5febSToomas Soome {
606724c5febSToomas Soome tp($3);
607724c5febSToomas Soome }
6087c478bd9Sstevel@tonic-gate ;
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate lora : LETTER
611724c5febSToomas Soome {
6127c478bd9Sstevel@tonic-gate $<cptr>$ = $1;
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate | LETTER '[' ']'
615724c5febSToomas Soome {
6167c478bd9Sstevel@tonic-gate $$ = geta($1);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate ;
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate %%
6217c478bd9Sstevel@tonic-gate #define error 256
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate int peekc = -1;
6247c478bd9Sstevel@tonic-gate int ifile; /* current index into sargv */
6257c478bd9Sstevel@tonic-gate int sargc; /* size of sargv[] */
6267c478bd9Sstevel@tonic-gate char **sargv; /* saved arg list without options */
6277c478bd9Sstevel@tonic-gate
6287c478bd9Sstevel@tonic-gate char funtab[52] = {
6297c478bd9Sstevel@tonic-gate 01, 0, 02, 0, 03, 0, 04, 0, 05, 0, 06, 0, 07, 0,
6307c478bd9Sstevel@tonic-gate 010, 0, 011, 0, 012, 0, 013, 0, 014, 0, 015, 0, 016, 0, 017, 0,
6317c478bd9Sstevel@tonic-gate 020, 0, 021, 0, 022, 0, 023, 0, 024, 0, 025, 0, 026, 0, 027, 0,
6327c478bd9Sstevel@tonic-gate 030, 0, 031, 0, 032, 0
6337c478bd9Sstevel@tonic-gate };
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate unsigned char atab[52] = {
6367c478bd9Sstevel@tonic-gate 0241, 0, 0242, 0, 0243, 0, 0244, 0, 0245, 0, 0246, 0, 0247, 0, 0250, 0,
6377c478bd9Sstevel@tonic-gate 0251, 0, 0252, 0, 0253, 0, 0254, 0, 0255, 0, 0256, 0, 0257, 0, 0260, 0,
6387c478bd9Sstevel@tonic-gate 0261, 0, 0262, 0, 0263, 0, 0264, 0, 0265, 0, 0266, 0, 0267, 0, 0270, 0,
6397c478bd9Sstevel@tonic-gate 0271, 0, 0272, 0
6407c478bd9Sstevel@tonic-gate };
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate char *letr[26] = {
6437c478bd9Sstevel@tonic-gate "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
6447c478bd9Sstevel@tonic-gate "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
6457c478bd9Sstevel@tonic-gate "u", "v", "w", "x", "y", "z"
6467c478bd9Sstevel@tonic-gate };
6477c478bd9Sstevel@tonic-gate
648b390fe2cSmuffin int
yylex(void)649b390fe2cSmuffin yylex(void)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate int c, ch;
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate restart:
6547c478bd9Sstevel@tonic-gate c = getch();
6557c478bd9Sstevel@tonic-gate peekc = -1;
6567c478bd9Sstevel@tonic-gate while (c == ' ' || c == '\t')
6577c478bd9Sstevel@tonic-gate c = getch();
6587c478bd9Sstevel@tonic-gate if (c == '\\') {
6597c478bd9Sstevel@tonic-gate (void) getch();
6607c478bd9Sstevel@tonic-gate goto restart;
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate if (c <= 'z' && c >= 'a') {
6637c478bd9Sstevel@tonic-gate /* look ahead to look for reserved words */
6647c478bd9Sstevel@tonic-gate peekc = getch();
6657c478bd9Sstevel@tonic-gate if (peekc >= 'a' && peekc <= 'z') {
6667c478bd9Sstevel@tonic-gate /* must be reserved word */
6677c478bd9Sstevel@tonic-gate if (c == 'i' && peekc == 'f') {
6687c478bd9Sstevel@tonic-gate c = _IF;
6697c478bd9Sstevel@tonic-gate goto skip;
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate if (c == 'w' && peekc == 'h') {
6727c478bd9Sstevel@tonic-gate c = _WHILE;
6737c478bd9Sstevel@tonic-gate goto skip;
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate if (c == 'f' && peekc == 'o') {
6767c478bd9Sstevel@tonic-gate c = _FOR;
6777c478bd9Sstevel@tonic-gate goto skip;
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate if (c == 's' && peekc == 'q') {
6807c478bd9Sstevel@tonic-gate c = SQRT;
6817c478bd9Sstevel@tonic-gate goto skip;
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate if (c == 'r' && peekc == 'e') {
6847c478bd9Sstevel@tonic-gate c = _RETURN;
6857c478bd9Sstevel@tonic-gate goto skip;
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate if (c == 'b' && peekc == 'r') {
6887c478bd9Sstevel@tonic-gate c = _BREAK;
6897c478bd9Sstevel@tonic-gate goto skip;
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate if (c == 'd' && peekc == 'e') {
6927c478bd9Sstevel@tonic-gate c = _DEFINE;
6937c478bd9Sstevel@tonic-gate goto skip;
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate if (c == 's' && peekc == 'c') {
6967c478bd9Sstevel@tonic-gate c = SCALE;
6977c478bd9Sstevel@tonic-gate goto skip;
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate if (c == 'b' && peekc == 'a') {
7007c478bd9Sstevel@tonic-gate c = BASE;
7017c478bd9Sstevel@tonic-gate goto skip;
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate if (c == 'i' && peekc == 'b') {
7047c478bd9Sstevel@tonic-gate c = BASE;
7057c478bd9Sstevel@tonic-gate goto skip;
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate if (c == 'o' && peekc == 'b') {
7087c478bd9Sstevel@tonic-gate c = OBASE;
7097c478bd9Sstevel@tonic-gate goto skip;
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate if (c == 'd' && peekc == 'i') {
7127c478bd9Sstevel@tonic-gate c = FFF;
7137c478bd9Sstevel@tonic-gate goto skip;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate if (c == 'a' && peekc == 'u') {
7167c478bd9Sstevel@tonic-gate c = _AUTO;
7177c478bd9Sstevel@tonic-gate goto skip;
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate if (c == 'l' && peekc == 'e') {
7207c478bd9Sstevel@tonic-gate c = LENGTH;
7217c478bd9Sstevel@tonic-gate goto skip;
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate if (c == 'q' && peekc == 'u') {
7247c478bd9Sstevel@tonic-gate getout(0);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate /* could not be found */
7277c478bd9Sstevel@tonic-gate return (error);
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate skip: /* skip over rest of word */
7307c478bd9Sstevel@tonic-gate peekc = -1;
7317c478bd9Sstevel@tonic-gate while ((ch = getch()) >= 'a' && ch <= 'z')
7327c478bd9Sstevel@tonic-gate ;
7337c478bd9Sstevel@tonic-gate peekc = ch;
7347c478bd9Sstevel@tonic-gate return (c);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate /* usual case; just one single letter */
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate yylval.cptr = letr[c-'a'];
7407c478bd9Sstevel@tonic-gate return (LETTER);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate
7437c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '9' || c >= 'A' && c <= 'F') {
7447c478bd9Sstevel@tonic-gate yylval.cc = c;
7457c478bd9Sstevel@tonic-gate return (DIGIT);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate
7487c478bd9Sstevel@tonic-gate switch (c) {
7497c478bd9Sstevel@tonic-gate case '.':
7507c478bd9Sstevel@tonic-gate return (DOT);
7517c478bd9Sstevel@tonic-gate
7527c478bd9Sstevel@tonic-gate case '=':
7537c478bd9Sstevel@tonic-gate switch ((peekc = getch())) {
7547c478bd9Sstevel@tonic-gate case '=':
7557c478bd9Sstevel@tonic-gate c = EQ;
7567c478bd9Sstevel@tonic-gate goto gotit;
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate case '+':
7597c478bd9Sstevel@tonic-gate c = EQPL;
7607c478bd9Sstevel@tonic-gate goto gotit;
7617c478bd9Sstevel@tonic-gate
7627c478bd9Sstevel@tonic-gate case '-':
7637c478bd9Sstevel@tonic-gate c = EQMI;
7647c478bd9Sstevel@tonic-gate goto gotit;
7657c478bd9Sstevel@tonic-gate
7667c478bd9Sstevel@tonic-gate case '*':
7677c478bd9Sstevel@tonic-gate c = EQMUL;
7687c478bd9Sstevel@tonic-gate goto gotit;
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate case '/':
7717c478bd9Sstevel@tonic-gate c = EQDIV;
7727c478bd9Sstevel@tonic-gate goto gotit;
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate case '%':
7757c478bd9Sstevel@tonic-gate c = EQREM;
7767c478bd9Sstevel@tonic-gate goto gotit;
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate case '^':
7797c478bd9Sstevel@tonic-gate c = EQEXP;
7807c478bd9Sstevel@tonic-gate goto gotit;
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate default:
7837c478bd9Sstevel@tonic-gate return ('=');
7847c478bd9Sstevel@tonic-gate gotit:
7857c478bd9Sstevel@tonic-gate peekc = -1;
7867c478bd9Sstevel@tonic-gate return (c);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate case '+':
7907c478bd9Sstevel@tonic-gate return (cpeek('+', INCR, '=', EQPL, '+'));
7917c478bd9Sstevel@tonic-gate
7927c478bd9Sstevel@tonic-gate case '-':
7937c478bd9Sstevel@tonic-gate return (cpeek('-', DECR, '=', EQMI, '-'));
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate case '*':
7967c478bd9Sstevel@tonic-gate return (cpeek('=', EQMUL, '\0', 0, '*'));
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate case '%':
7997c478bd9Sstevel@tonic-gate return (cpeek('=', EQREM, '\0', 0, '%'));
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate case '^':
8027c478bd9Sstevel@tonic-gate return (cpeek('=', EQEXP, '\0', 0, '^'));
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate case '<':
8057c478bd9Sstevel@tonic-gate return (cpeek('=', LE, '\0', 0, '<'));
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate case '>':
8087c478bd9Sstevel@tonic-gate return (cpeek('=', GE, '\0', 0, '>'));
8097c478bd9Sstevel@tonic-gate
8107c478bd9Sstevel@tonic-gate case '!':
8117c478bd9Sstevel@tonic-gate return (cpeek('=', NE, '\0', 0, '!'));
8127c478bd9Sstevel@tonic-gate
8137c478bd9Sstevel@tonic-gate case '/':
8147c478bd9Sstevel@tonic-gate if ((peekc = getch()) == '=') {
8157c478bd9Sstevel@tonic-gate peekc = -1;
8167c478bd9Sstevel@tonic-gate return (EQDIV);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate if (peekc == '*') {
8197c478bd9Sstevel@tonic-gate peekc = -1;
8207c478bd9Sstevel@tonic-gate while ((getch() != '*') || ((peekc = getch()) != '/'))
8217c478bd9Sstevel@tonic-gate ;
8227c478bd9Sstevel@tonic-gate peekc = -1;
8237c478bd9Sstevel@tonic-gate goto restart;
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate else
8267c478bd9Sstevel@tonic-gate return (c);
8277c478bd9Sstevel@tonic-gate
8287c478bd9Sstevel@tonic-gate case '"':
8297c478bd9Sstevel@tonic-gate yylval.cptr = str;
8307c478bd9Sstevel@tonic-gate while ((c = getch()) != '"') {
8317c478bd9Sstevel@tonic-gate *str++ = c;
8327c478bd9Sstevel@tonic-gate if (str >= &string[STRING_SIZE-1]) {
833*1a90c98dSToomas Soome (void) yyerror("string space exceeded");
8347c478bd9Sstevel@tonic-gate getout(1);
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate *str++ = '\0';
8387c478bd9Sstevel@tonic-gate return (QSTR);
8397c478bd9Sstevel@tonic-gate
8407c478bd9Sstevel@tonic-gate default:
8417c478bd9Sstevel@tonic-gate return (c);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate
845b390fe2cSmuffin int
cpeek(char c1,int yes1,char c2,int yes2,char none)846b390fe2cSmuffin cpeek(char c1, int yes1, char c2, int yes2, char none)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate int r;
8497c478bd9Sstevel@tonic-gate
8507c478bd9Sstevel@tonic-gate peekc = getch();
8517c478bd9Sstevel@tonic-gate if (peekc == c1)
8527c478bd9Sstevel@tonic-gate r = yes1;
8537c478bd9Sstevel@tonic-gate else if (peekc == c2)
8547c478bd9Sstevel@tonic-gate r = yes2;
8557c478bd9Sstevel@tonic-gate else
8567c478bd9Sstevel@tonic-gate return (none);
8577c478bd9Sstevel@tonic-gate peekc = -1;
8587c478bd9Sstevel@tonic-gate return (r);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate
861b390fe2cSmuffin
862b390fe2cSmuffin int
getch(void)863b390fe2cSmuffin getch(void)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate int ch;
8667c478bd9Sstevel@tonic-gate char mbuf[LINE_MAX];
8677c478bd9Sstevel@tonic-gate
8687c478bd9Sstevel@tonic-gate loop:
8697c478bd9Sstevel@tonic-gate ch = (peekc < 0) ? getc(in) : peekc;
8707c478bd9Sstevel@tonic-gate peekc = -1;
8717c478bd9Sstevel@tonic-gate if (ch != EOF)
8727c478bd9Sstevel@tonic-gate return (ch);
8737c478bd9Sstevel@tonic-gate
8747c478bd9Sstevel@tonic-gate if (++ifile >= sargc) {
8757c478bd9Sstevel@tonic-gate if (ifile >= sargc+1)
8767c478bd9Sstevel@tonic-gate getout(0);
8777c478bd9Sstevel@tonic-gate in = stdin;
8787c478bd9Sstevel@tonic-gate ln = 0;
8797c478bd9Sstevel@tonic-gate goto loop;
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate (void) fclose(in);
8837c478bd9Sstevel@tonic-gate if ((in = fopen(sargv[ifile], "r")) != NULL) {
8847c478bd9Sstevel@tonic-gate ln = 0;
8857c478bd9Sstevel@tonic-gate ss = sargv[ifile];
8867c478bd9Sstevel@tonic-gate goto loop;
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate (void) snprintf(mbuf, sizeof (mbuf), "can't open input file %s",
8897c478bd9Sstevel@tonic-gate sargv[ifile]);
8907c478bd9Sstevel@tonic-gate ln = -1;
8917c478bd9Sstevel@tonic-gate ss = "command line";
892*1a90c98dSToomas Soome (void) yyerror(mbuf);
8937c478bd9Sstevel@tonic-gate getout(1);
8947c478bd9Sstevel@tonic-gate /*NOTREACHED*/
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate
8977c478bd9Sstevel@tonic-gate #define b_sp_max 5000
8987c478bd9Sstevel@tonic-gate int b_space[b_sp_max];
8997c478bd9Sstevel@tonic-gate int *b_sp_nxt = { b_space };
9007c478bd9Sstevel@tonic-gate
9017c478bd9Sstevel@tonic-gate int bdebug = 0;
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate static int *
bundle(int i,...)9047c478bd9Sstevel@tonic-gate bundle(int i, ...)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate va_list ap;
9077c478bd9Sstevel@tonic-gate int *q;
9087c478bd9Sstevel@tonic-gate
9097c478bd9Sstevel@tonic-gate va_start(ap, i);
9107c478bd9Sstevel@tonic-gate q = b_sp_nxt;
9117c478bd9Sstevel@tonic-gate if (bdebug)
9127c478bd9Sstevel@tonic-gate printf("bundle %d elements at %o\n", i, q);
9137c478bd9Sstevel@tonic-gate while (i-- > 0) {
9147c478bd9Sstevel@tonic-gate if (b_sp_nxt >= & b_space[b_sp_max])
915*1a90c98dSToomas Soome (void) yyerror("bundling space exceeded");
9167c478bd9Sstevel@tonic-gate *b_sp_nxt++ = va_arg(ap, int);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate * b_sp_nxt++ = 0;
9197c478bd9Sstevel@tonic-gate yyval.iptr = q;
9207c478bd9Sstevel@tonic-gate va_end(ap);
9217c478bd9Sstevel@tonic-gate return (q);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate
924b390fe2cSmuffin void
routput(int * p)925b390fe2cSmuffin routput(int *p)
9267c478bd9Sstevel@tonic-gate {
9277c478bd9Sstevel@tonic-gate if (bdebug) printf("routput(%o)\n", p);
9287c478bd9Sstevel@tonic-gate if (p >= &b_space[0] && p < &b_space[b_sp_max]) {
9297c478bd9Sstevel@tonic-gate /* part of a bundle */
9307c478bd9Sstevel@tonic-gate while (*p != 0)
931b390fe2cSmuffin routput((int *)*p++);
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate else
9347c478bd9Sstevel@tonic-gate printf((char *)p); /* character string */
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate
937b390fe2cSmuffin void
output(int * p)938b390fe2cSmuffin output(int *p)
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate routput(p);
9417c478bd9Sstevel@tonic-gate b_sp_nxt = & b_space[0];
9427c478bd9Sstevel@tonic-gate printf("\n");
9437c478bd9Sstevel@tonic-gate (void) fflush(stdout);
9447c478bd9Sstevel@tonic-gate cp = cary;
9457c478bd9Sstevel@tonic-gate crs = rcrs;
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate
948b390fe2cSmuffin void
conout(int * p,char * s)949b390fe2cSmuffin conout(int *p, char *s)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate printf("[");
9527c478bd9Sstevel@tonic-gate routput(p);
9537c478bd9Sstevel@tonic-gate printf("]s%s\n", s);
9547c478bd9Sstevel@tonic-gate (void) fflush(stdout);
9557c478bd9Sstevel@tonic-gate lev--;
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate
958*1a90c98dSToomas Soome int
yyerror(const char * s)959*1a90c98dSToomas Soome yyerror(const char *s)
9607c478bd9Sstevel@tonic-gate {
9617c478bd9Sstevel@tonic-gate if (ifile >= sargc)
9627c478bd9Sstevel@tonic-gate ss = "teletype";
9637c478bd9Sstevel@tonic-gate
9647c478bd9Sstevel@tonic-gate if (ss == 0 || *ss == 0)
9657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s on line %d\n"), s, ln+1);
9667c478bd9Sstevel@tonic-gate else
9677c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s on line %d, %s\n"),
9687c478bd9Sstevel@tonic-gate s, ln+1, ss);
9697c478bd9Sstevel@tonic-gate (void) fflush(stderr);
9707c478bd9Sstevel@tonic-gate
9717c478bd9Sstevel@tonic-gate cp = cary;
9727c478bd9Sstevel@tonic-gate crs = rcrs;
9737c478bd9Sstevel@tonic-gate bindx = 0;
9747c478bd9Sstevel@tonic-gate lev = 0;
9757c478bd9Sstevel@tonic-gate b_sp_nxt = &b_space[0];
976*1a90c98dSToomas Soome return (0);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate
979b390fe2cSmuffin void
checkbuffer(void)980b390fe2cSmuffin checkbuffer(void)
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate /* Do not exceed the last char in input line buffer */
9837c478bd9Sstevel@tonic-gate if (cp >= cpend) {
984*1a90c98dSToomas Soome (void) yyerror("line too long\n");
9857c478bd9Sstevel@tonic-gate getout(1);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate
989b390fe2cSmuffin void
pp(int * s)990b390fe2cSmuffin pp(int *s)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate /* puts the relevant stuff on pre and post for the letter s */
9937c478bd9Sstevel@tonic-gate
9947c478bd9Sstevel@tonic-gate (void) bundle(3, "S", s, pre);
9957c478bd9Sstevel@tonic-gate pre = yyval.iptr;
9967c478bd9Sstevel@tonic-gate (void) bundle(4, post, "L", s, "s.");
9977c478bd9Sstevel@tonic-gate post = yyval.iptr;
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate
1000b390fe2cSmuffin void
tp(int * s)1001b390fe2cSmuffin tp(int *s)
10027c478bd9Sstevel@tonic-gate { /* same as pp, but for temps */
10037c478bd9Sstevel@tonic-gate bundle(3, "0S", s, pre);
10047c478bd9Sstevel@tonic-gate pre = yyval.iptr;
10057c478bd9Sstevel@tonic-gate bundle(4, post, "L", s, "s.");
10067c478bd9Sstevel@tonic-gate post = yyval.iptr;
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate
1009b390fe2cSmuffin void
yyinit(int argc,char ** argv)1010b390fe2cSmuffin yyinit(int argc, char **argv)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate char mbuf[LINE_MAX];
10137c478bd9Sstevel@tonic-gate
10147c478bd9Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); /* ignore all interrupts */
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate sargv = argv;
10177c478bd9Sstevel@tonic-gate sargc = argc;
10187c478bd9Sstevel@tonic-gate if (sargc == 0)
10197c478bd9Sstevel@tonic-gate in = stdin;
10207c478bd9Sstevel@tonic-gate else if ((in = fopen(sargv[0], "r")) == NULL) {
10217c478bd9Sstevel@tonic-gate (void) snprintf(mbuf, sizeof (mbuf), "can't open input file %s",
10227c478bd9Sstevel@tonic-gate sargv[0]);
10237c478bd9Sstevel@tonic-gate ln = -1;
10247c478bd9Sstevel@tonic-gate ss = "command line";
1025*1a90c98dSToomas Soome (void) yyerror(mbuf);
10267c478bd9Sstevel@tonic-gate getout(1);
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate ifile = 0;
10297c478bd9Sstevel@tonic-gate ln = 0;
10307c478bd9Sstevel@tonic-gate ss = sargv[0];
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate
1033b390fe2cSmuffin static void
getout(int code)1034b390fe2cSmuffin getout(int code)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate printf("q");
10377c478bd9Sstevel@tonic-gate (void) fflush(stdout);
10387c478bd9Sstevel@tonic-gate exit(code);
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate
10417c478bd9Sstevel@tonic-gate int *
getf(char * p)1042b390fe2cSmuffin getf(char *p)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate return ((int *) &funtab[2*(*p -0141)]);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate
10477c478bd9Sstevel@tonic-gate int *
geta(char * p)1048b390fe2cSmuffin geta(char *p)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate return ((int *) &atab[2*(*p - 0141)]);
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate
1053b390fe2cSmuffin int
main(int argc,char ** argv)1054b390fe2cSmuffin main(int argc, char **argv)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate int p[2];
10577c478bd9Sstevel@tonic-gate int cflag = 0;
10587c478bd9Sstevel@tonic-gate int lflag = 0;
10597c478bd9Sstevel@tonic-gate int flag = 0;
10607c478bd9Sstevel@tonic-gate char **av;
10617c478bd9Sstevel@tonic-gate int filecounter = 0;
10627c478bd9Sstevel@tonic-gate
10637c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
10647c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
10657c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
10667c478bd9Sstevel@tonic-gate #endif
10677c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
10687c478bd9Sstevel@tonic-gate
10697c478bd9Sstevel@tonic-gate while ((flag = getopt(argc, argv, "dcl")) != EOF) {
10707c478bd9Sstevel@tonic-gate switch (flag) {
10717c478bd9Sstevel@tonic-gate case 'd':
10727c478bd9Sstevel@tonic-gate case 'c':
10737c478bd9Sstevel@tonic-gate cflag++;
10747c478bd9Sstevel@tonic-gate break;
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate case 'l':
10777c478bd9Sstevel@tonic-gate lflag++;
10787c478bd9Sstevel@tonic-gate break;
10797c478bd9Sstevel@tonic-gate
10807c478bd9Sstevel@tonic-gate default:
10817c478bd9Sstevel@tonic-gate fflush(stdout);
10827c478bd9Sstevel@tonic-gate usage();
10837c478bd9Sstevel@tonic-gate break;
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate
10877c478bd9Sstevel@tonic-gate argc -= optind;
10887c478bd9Sstevel@tonic-gate av = &argv[optind];
10897c478bd9Sstevel@tonic-gate
10907c478bd9Sstevel@tonic-gate /*
10917c478bd9Sstevel@tonic-gate * argc is the count of arguments, which should be filenames,
10927c478bd9Sstevel@tonic-gate * remaining in argv. av is a pointer to the first of the
10937c478bd9Sstevel@tonic-gate * remaining arguments.
10947c478bd9Sstevel@tonic-gate */
10957c478bd9Sstevel@tonic-gate
10967c478bd9Sstevel@tonic-gate for (filecounter = 0; filecounter < argc; filecounter++) {
10977c478bd9Sstevel@tonic-gate if ((strlen(av[filecounter])) >= PATH_MAX) {
10987c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
10997c478bd9Sstevel@tonic-gate gettext("File argument too long\n"));
11007c478bd9Sstevel@tonic-gate exit(2);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate
11047c478bd9Sstevel@tonic-gate if (lflag) {
11057c478bd9Sstevel@tonic-gate /*
11067c478bd9Sstevel@tonic-gate * if the user wants to include the math library, prepend
11077c478bd9Sstevel@tonic-gate * the math library filename to the argument list by
11087c478bd9Sstevel@tonic-gate * overwriting the last option (there must be at least one
11097c478bd9Sstevel@tonic-gate * supplied option if this is being done).
11107c478bd9Sstevel@tonic-gate */
11117c478bd9Sstevel@tonic-gate av = &argv[optind-1];
11127c478bd9Sstevel@tonic-gate av[0] = "/usr/lib/lib.b";
11137c478bd9Sstevel@tonic-gate argc++;
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate
11167c478bd9Sstevel@tonic-gate if (cflag) {
11177c478bd9Sstevel@tonic-gate yyinit(argc, av);
11187c478bd9Sstevel@tonic-gate yyparse();
11197c478bd9Sstevel@tonic-gate exit(0);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate
11227c478bd9Sstevel@tonic-gate pipe(p);
11237c478bd9Sstevel@tonic-gate if (fork() == 0) {
11247c478bd9Sstevel@tonic-gate (void) close(1);
11257c478bd9Sstevel@tonic-gate dup(p[1]);
11267c478bd9Sstevel@tonic-gate (void) close(p[0]);
11277c478bd9Sstevel@tonic-gate (void) close(p[1]);
11287c478bd9Sstevel@tonic-gate yyinit(argc, av);
11297c478bd9Sstevel@tonic-gate yyparse();
11307c478bd9Sstevel@tonic-gate exit(0);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate (void) close(0);
11337c478bd9Sstevel@tonic-gate dup(p[0]);
11347c478bd9Sstevel@tonic-gate (void) close(p[0]);
11357c478bd9Sstevel@tonic-gate (void) close(p[1]);
11367c478bd9Sstevel@tonic-gate #ifdef XPG6
11377c478bd9Sstevel@tonic-gate execl("/usr/xpg6/bin/dc", "dc", "-", 0);
11387c478bd9Sstevel@tonic-gate #else
11397c478bd9Sstevel@tonic-gate execl("/usr/bin/dc", "dc", "-", 0);
11407c478bd9Sstevel@tonic-gate #endif
1141b390fe2cSmuffin
1142b390fe2cSmuffin return (1);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate
11457c478bd9Sstevel@tonic-gate static void
usage(void)11467c478bd9Sstevel@tonic-gate usage(void)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
11497c478bd9Sstevel@tonic-gate "usage: bc [ -c ] [ -l ] [ file ... ]\n"));
11507c478bd9Sstevel@tonic-gate exit(2);
11517c478bd9Sstevel@tonic-gate }
1152