1779fc935Sceastha /*
2779fc935Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3779fc935Sceastha * Use is subject to license terms.
4779fc935Sceastha */
5779fc935Sceastha
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
15779fc935Sceastha #pragma ident "%Z%%M% %I% %E% SMI"
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate #include "e.h"
187c478bd9Sstevel@tonic-gate #include "e.def"
197c478bd9Sstevel@tonic-gate #include <locale.h>
207c478bd9Sstevel@tonic-gate
217c478bd9Sstevel@tonic-gate #define SSIZE 400
227c478bd9Sstevel@tonic-gate char token[SSIZE];
237c478bd9Sstevel@tonic-gate int sp;
247c478bd9Sstevel@tonic-gate #define putbak(c) *ip++ = c;
257c478bd9Sstevel@tonic-gate #define PUSHBACK 300 /* maximum pushback characters */
267c478bd9Sstevel@tonic-gate char ibuf[PUSHBACK+SSIZE]; /* pushback buffer for definitions, etc. */
277c478bd9Sstevel@tonic-gate char *ip = ibuf;
287c478bd9Sstevel@tonic-gate
29*b5cda42eSceastha extern tbl *keytbl[];
30*b5cda42eSceastha extern tbl *deftbl[];
31*b5cda42eSceastha
32779fc935Sceastha void define(int);
33779fc935Sceastha void delim(void);
34779fc935Sceastha void getstr(char *, int);
35779fc935Sceastha void include(void);
36779fc935Sceastha int openinfile(void);
37779fc935Sceastha void pbstr(char *);
38779fc935Sceastha void space(void);
39779fc935Sceastha
40779fc935Sceastha int
gtc(void)41779fc935Sceastha gtc(void)
42779fc935Sceastha {
437c478bd9Sstevel@tonic-gate loop:
447c478bd9Sstevel@tonic-gate if (ip > ibuf)
457c478bd9Sstevel@tonic-gate return (*--ip); /* already present */
467c478bd9Sstevel@tonic-gate lastchar = getc(curfile);
477c478bd9Sstevel@tonic-gate if (lastchar == '\n')
487c478bd9Sstevel@tonic-gate linect++;
497c478bd9Sstevel@tonic-gate if (lastchar != EOF)
507c478bd9Sstevel@tonic-gate return (lastchar);
517c478bd9Sstevel@tonic-gate if (++ifile > svargc) {
527c478bd9Sstevel@tonic-gate return (EOF);
537c478bd9Sstevel@tonic-gate }
54779fc935Sceastha (void) fclose(curfile);
557c478bd9Sstevel@tonic-gate linect = 1;
567c478bd9Sstevel@tonic-gate if (openinfile() == 0)
577c478bd9Sstevel@tonic-gate goto loop;
587c478bd9Sstevel@tonic-gate return (EOF);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * open file indexed by ifile in svargv, return non zero if fail
627c478bd9Sstevel@tonic-gate */
63779fc935Sceastha int
openinfile(void)64779fc935Sceastha openinfile(void)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate if (strcmp(svargv[ifile], "-") == 0) {
677c478bd9Sstevel@tonic-gate curfile = stdin;
687c478bd9Sstevel@tonic-gate return (0);
697c478bd9Sstevel@tonic-gate } else if ((curfile = fopen(svargv[ifile], "r")) != NULL) {
707c478bd9Sstevel@tonic-gate return (0);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate error(FATAL, gettext("can't open file %s"), svargv[ifile]);
737c478bd9Sstevel@tonic-gate return (1);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
76779fc935Sceastha void
pbstr(char * str)77779fc935Sceastha pbstr(char *str)
787c478bd9Sstevel@tonic-gate {
79779fc935Sceastha char *p;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate p = str;
82779fc935Sceastha while (*p++)
83779fc935Sceastha ;
847c478bd9Sstevel@tonic-gate --p;
857c478bd9Sstevel@tonic-gate if (ip >= &ibuf[PUSHBACK])
867c478bd9Sstevel@tonic-gate error(FATAL, gettext("pushback overflow"));
877c478bd9Sstevel@tonic-gate while (p > str)
887c478bd9Sstevel@tonic-gate putbak(*--p);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
91779fc935Sceastha int
yylex(void)92779fc935Sceastha yylex(void)
93779fc935Sceastha {
94779fc935Sceastha int c;
957c478bd9Sstevel@tonic-gate tbl *tp, *lookup();
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate beg:
987c478bd9Sstevel@tonic-gate while ((c = gtc()) == ' ' || c == '\n')
997c478bd9Sstevel@tonic-gate ;
1007c478bd9Sstevel@tonic-gate yylval = c;
1017c478bd9Sstevel@tonic-gate switch (c) {
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate case EOF:
1047c478bd9Sstevel@tonic-gate return (EOF);
1057c478bd9Sstevel@tonic-gate case '~':
1067c478bd9Sstevel@tonic-gate return (SPACE);
1077c478bd9Sstevel@tonic-gate case '^':
1087c478bd9Sstevel@tonic-gate return (THIN);
1097c478bd9Sstevel@tonic-gate case '\t':
1107c478bd9Sstevel@tonic-gate return (TAB);
1117c478bd9Sstevel@tonic-gate case '{':
1127c478bd9Sstevel@tonic-gate return ('{');
1137c478bd9Sstevel@tonic-gate case '}':
1147c478bd9Sstevel@tonic-gate return ('}');
1157c478bd9Sstevel@tonic-gate case '"':
1167c478bd9Sstevel@tonic-gate for (sp = 0; (c = gtc()) != '"' && c != '\n'; ) {
1177c478bd9Sstevel@tonic-gate if (c == '\\')
1187c478bd9Sstevel@tonic-gate if ((c = gtc()) != '"')
1197c478bd9Sstevel@tonic-gate token[sp++] = '\\';
1207c478bd9Sstevel@tonic-gate token[sp++] = c;
1217c478bd9Sstevel@tonic-gate if (sp >= SSIZE)
122779fc935Sceastha error(FATAL, gettext(
123779fc935Sceastha "quoted string %.20s... too long"), token);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate token[sp] = '\0';
1267c478bd9Sstevel@tonic-gate yylval = (int)&token[0];
1277c478bd9Sstevel@tonic-gate if (c == '\n')
1287c478bd9Sstevel@tonic-gate error(!FATAL, gettext("missing \" in %.20s"), token);
1297c478bd9Sstevel@tonic-gate return (QTEXT);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate if (c == righteq)
1327c478bd9Sstevel@tonic-gate return (EOF);
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate putbak(c);
1357c478bd9Sstevel@tonic-gate getstr(token, SSIZE);
1367c478bd9Sstevel@tonic-gate if (dbg) printf(".\tlex token = |%s|\n", token);
137779fc935Sceastha if ((tp = lookup(deftbl, token, NULL)) != NULL) {
1387c478bd9Sstevel@tonic-gate putbak(' ');
1397c478bd9Sstevel@tonic-gate pbstr(tp->defn);
1407c478bd9Sstevel@tonic-gate putbak(' ');
1417c478bd9Sstevel@tonic-gate if (dbg)
1427c478bd9Sstevel@tonic-gate printf(".\tfound %s|=%s|\n", token, tp->defn);
143779fc935Sceastha } else if ((tp = lookup(keytbl, token, NULL)) == NULL) {
1447c478bd9Sstevel@tonic-gate if (dbg) printf(".\t%s is not a keyword\n", token);
1457c478bd9Sstevel@tonic-gate return (CONTIG);
146779fc935Sceastha } else if (tp->defn == (char *)DEFINE ||
147779fc935Sceastha tp->defn == (char *)NDEFINE || tp->defn == (char *)TDEFINE)
148779fc935Sceastha define((int)tp->defn);
1497c478bd9Sstevel@tonic-gate else if (tp->defn == (char *)DELIM)
1507c478bd9Sstevel@tonic-gate delim();
1517c478bd9Sstevel@tonic-gate else if (tp->defn == (char *)GSIZE)
1527c478bd9Sstevel@tonic-gate globsize();
1537c478bd9Sstevel@tonic-gate else if (tp->defn == (char *)GFONT)
1547c478bd9Sstevel@tonic-gate globfont();
1557c478bd9Sstevel@tonic-gate else if (tp->defn == (char *)INCLUDE)
1567c478bd9Sstevel@tonic-gate include();
1577c478bd9Sstevel@tonic-gate else if (tp->defn == (char *)SPACE)
1587c478bd9Sstevel@tonic-gate space();
1597c478bd9Sstevel@tonic-gate else {
1607c478bd9Sstevel@tonic-gate return ((int)tp->defn);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate goto beg;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
165779fc935Sceastha void
getstr(char * s,int n)166779fc935Sceastha getstr(char *s, int n)
167779fc935Sceastha {
168779fc935Sceastha int c;
169779fc935Sceastha char *p;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate p = s;
1727c478bd9Sstevel@tonic-gate while ((c = gtc()) == ' ' || c == '\n')
1737c478bd9Sstevel@tonic-gate ;
1747c478bd9Sstevel@tonic-gate if (c == EOF) {
1757c478bd9Sstevel@tonic-gate *s = 0;
1767c478bd9Sstevel@tonic-gate return;
1777c478bd9Sstevel@tonic-gate }
178779fc935Sceastha while (c != ' ' && c != '\t' && c != '\n' && c != '{' && c != '}' &&
179779fc935Sceastha c != '"' && c != '~' && c != '^' && c != righteq) {
1807c478bd9Sstevel@tonic-gate if (c == '\\')
1817c478bd9Sstevel@tonic-gate if ((c = gtc()) != '"')
1827c478bd9Sstevel@tonic-gate *p++ = '\\';
1837c478bd9Sstevel@tonic-gate *p++ = c;
1847c478bd9Sstevel@tonic-gate if (--n <= 0)
1857c478bd9Sstevel@tonic-gate error(FATAL, gettext("token %.20s... too long"), s);
1867c478bd9Sstevel@tonic-gate c = gtc();
1877c478bd9Sstevel@tonic-gate }
188779fc935Sceastha if (c == '{' || c == '}' || c == '"' || c == '~' || c == '^' ||
189779fc935Sceastha c == '\t' || c == righteq)
1907c478bd9Sstevel@tonic-gate putbak(c);
1917c478bd9Sstevel@tonic-gate *p = '\0';
1927c478bd9Sstevel@tonic-gate yylval = (int)s;
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate
195779fc935Sceastha int
cstr(char * s,int quote,int maxs)196779fc935Sceastha cstr(char *s, int quote, int maxs)
197779fc935Sceastha {
1987c478bd9Sstevel@tonic-gate int del, c, i;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate s[0] = 0;
201779fc935Sceastha while ((del = gtc()) == ' ' || del == '\t')
202779fc935Sceastha ;
203779fc935Sceastha if (quote) {
2047c478bd9Sstevel@tonic-gate for (i = 0; (c = gtc()) != del && c != EOF; ) {
2057c478bd9Sstevel@tonic-gate s[i++] = c;
2067c478bd9Sstevel@tonic-gate if (i >= maxs)
2077c478bd9Sstevel@tonic-gate return (1); /* disaster */
2087c478bd9Sstevel@tonic-gate }
209779fc935Sceastha } else {
2107c478bd9Sstevel@tonic-gate if (del == '\n')
2117c478bd9Sstevel@tonic-gate return (1);
2127c478bd9Sstevel@tonic-gate s[0] = del;
213779fc935Sceastha for (i = 1; (c = gtc()) != ' ' && c != '\t' &&
214779fc935Sceastha c != '\n' && c != EOF; /* empty */) {
2157c478bd9Sstevel@tonic-gate s[i++] = c;
2167c478bd9Sstevel@tonic-gate if (i >= maxs)
2177c478bd9Sstevel@tonic-gate return (1); /* disaster */
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate s[i] = '\0';
2217c478bd9Sstevel@tonic-gate if (c == EOF)
2227c478bd9Sstevel@tonic-gate error(FATAL, gettext("Unexpected end of input at %.20s"), s);
2237c478bd9Sstevel@tonic-gate return (0);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
226779fc935Sceastha void
define(int type)227779fc935Sceastha define(int type)
228779fc935Sceastha {
2297c478bd9Sstevel@tonic-gate char *strsave(), *p1, *p2;
2307c478bd9Sstevel@tonic-gate tbl *lookup();
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate getstr(token, SSIZE); /* get name */
2337c478bd9Sstevel@tonic-gate if (type != DEFINE) {
234779fc935Sceastha (void) cstr(token, 1, SSIZE); /* skip the definition too */
2357c478bd9Sstevel@tonic-gate return;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate p1 = strsave(token);
2387c478bd9Sstevel@tonic-gate if (cstr(token, 1, SSIZE))
239779fc935Sceastha error(FATAL, gettext(
240779fc935Sceastha "Unterminated definition at %.20s"), token);
2417c478bd9Sstevel@tonic-gate p2 = strsave(token);
242779fc935Sceastha lookup(deftbl, p1, p2);
2437c478bd9Sstevel@tonic-gate if (dbg) printf(".\tname %s defined as %s\n", p1, p2);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate char *spaceval = NULL;
2477c478bd9Sstevel@tonic-gate
248779fc935Sceastha void
space(void)249779fc935Sceastha space(void) /* collect line of form "space amt" to replace \x in output */
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate char *strsave();
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate getstr(token, SSIZE);
2547c478bd9Sstevel@tonic-gate spaceval = strsave(token);
2557c478bd9Sstevel@tonic-gate if (dbg) printf(".\tsetting space to %s\n", token);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate
259779fc935Sceastha char *
strsave(char * s)260779fc935Sceastha strsave(char *s)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate char *malloc();
263779fc935Sceastha char *q;
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate q = malloc(strlen(s)+1);
2667c478bd9Sstevel@tonic-gate if (q == NULL)
2677c478bd9Sstevel@tonic-gate error(FATAL, gettext("out of space in strsave on %s"), s);
2687c478bd9Sstevel@tonic-gate strcpy(q, s);
2697c478bd9Sstevel@tonic-gate return (q);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate
272779fc935Sceastha void
include(void)273779fc935Sceastha include(void)
274779fc935Sceastha {
2757c478bd9Sstevel@tonic-gate error(!FATAL, gettext("Include not yet implemented"));
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
278779fc935Sceastha void
delim(void)279779fc935Sceastha delim(void)
280779fc935Sceastha {
2817c478bd9Sstevel@tonic-gate yyval = eqnreg = 0;
2827c478bd9Sstevel@tonic-gate if (cstr(token, 0, SSIZE))
2837c478bd9Sstevel@tonic-gate error(FATAL, gettext("Bizarre delimiters at %.20s"), token);
2847c478bd9Sstevel@tonic-gate lefteq = token[0];
2857c478bd9Sstevel@tonic-gate righteq = token[1];
2867c478bd9Sstevel@tonic-gate if (lefteq == 'o' && righteq == 'f')
2877c478bd9Sstevel@tonic-gate lefteq = righteq = '\0';
2887c478bd9Sstevel@tonic-gate }
289