lex.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "e.h"
  10. #include "y.tab.h"
  11. #include <ctype.h>
  12. #define SSIZE 1000
  13. char token[SSIZE];
  14. int sp;
  15. void space(void);
  16. void dodef(tbl *);
  17. void define(int);
  18. void ifdef(void);
  19. void include(void);
  20. void delim(void);
  21. yylex(void)
  22. {
  23. register int c;
  24. tbl *tp;
  25. begin:
  26. while ((c = input()) == ' ' || c == '\n' || c == '\t')
  27. ;
  28. yylval = c;
  29. switch (c) {
  30. case EOF:
  31. ERROR "unexpected end of input inside equation" WARNING;
  32. return(EOF);
  33. case '~':
  34. return(SPACE);
  35. case '^':
  36. return(THIN);
  37. /* case '\t':
  38. return(TAB);
  39. */
  40. case '{':
  41. return('{');
  42. case '}':
  43. return('}');
  44. case '"':
  45. for (sp = 0; (c=input())!='"' && c != '\n'; ) {
  46. if (c == '\\')
  47. if ((c = input()) != '"')
  48. token[sp++] = '\\';
  49. token[sp++] = c;
  50. if (sp >= SSIZE)
  51. ERROR "quoted string %.20s... too long", token FATAL;
  52. }
  53. token[sp] = '\0';
  54. yylval = (int) &token[0];
  55. if (c == '\n')
  56. ERROR "missing \" in %.20s", token WARNING;
  57. return(QTEXT);
  58. }
  59. if (!display && c == righteq)
  60. return(EOF);
  61. unput(c);
  62. getstr(token, SSIZE);
  63. dprintf(".\tlex token = |%s|\n", token);
  64. if ((tp = lookup(deftbl, token)) != NULL) { /* defined term */
  65. c = input();
  66. unput(c);
  67. if (c == '(') /* macro with args */
  68. dodef(tp);
  69. else { /* no args */
  70. unput(' ');
  71. pbstr(tp->cval);
  72. dprintf(".\tfound %s|=%s|\n", token, tp->cval);
  73. }
  74. goto begin;
  75. }
  76. if ((tp = lookup(keytbl, token)) == NULL) /* not a keyword */
  77. return CONTIG;
  78. switch (tp->ival) { /* some kind of keyword */
  79. case DEFINE: case TDEFINE: case NDEFINE:
  80. define(tp->ival);
  81. break;
  82. case IFDEF:
  83. ifdef();
  84. break;
  85. case DELIM:
  86. delim();
  87. break;
  88. case GSIZE:
  89. globsize();
  90. break;
  91. case GFONT:
  92. globfont();
  93. break;
  94. case INCLUDE:
  95. include();
  96. break;
  97. case SPACE:
  98. space();
  99. break;
  100. case DOTEQ:
  101. /* .EQ inside equation -- should warn if at bottom level */
  102. break;
  103. case DOTEN:
  104. if (curfile == infile)
  105. return EOF;
  106. /* else ignore nested .EN */
  107. break;
  108. default:
  109. return tp->ival;
  110. }
  111. goto begin;
  112. }
  113. void getstr(char *s, int n)
  114. {
  115. register int c;
  116. register char *p;
  117. p = s;
  118. while ((c = input()) == ' ' || c == '\n')
  119. ;
  120. if (c == EOF) {
  121. *s = 0;
  122. return;
  123. }
  124. while (c != ' ' && c != '\t' && c != '\n' && c != '{' && c != '}'
  125. && c != '"' && c != '~' && c != '^') {
  126. if (!display && c == righteq)
  127. break;
  128. if (c == '(' && p > s) { /* might be defined(...) */
  129. *p = '\0';
  130. if (lookup(deftbl, s) != NULL)
  131. break;
  132. }
  133. if (c == '\\')
  134. if ((c = input()) != '"')
  135. *p++ = '\\';
  136. *p++ = c;
  137. if (--n <= 0)
  138. ERROR "token %.20s... too long", s FATAL;
  139. c = input();
  140. }
  141. unput(c);
  142. *p = '\0';
  143. yylval = (int) s;
  144. }
  145. cstr(char *s, int quote, int maxs)
  146. {
  147. int del, c, i;
  148. s[0] = 0;
  149. while ((del=input()) == ' ' || del == '\t')
  150. ;
  151. if (quote)
  152. for (i=0; (c=input()) != del && c != EOF;) {
  153. s[i++] = c;
  154. if (i >= maxs)
  155. return(1); /* disaster */
  156. }
  157. else {
  158. if (del == '\n')
  159. return(1);
  160. s[0] = del;
  161. for (i=1; (c=input())!=' ' && c!= '\t' && c!='\n' && c!=EOF;) {
  162. s[i++] = c;
  163. if (i >= maxs)
  164. return(1); /* disaster */
  165. }
  166. }
  167. s[i] = '\0';
  168. if (c == EOF)
  169. ERROR "Unexpected end of input at %.20s", s FATAL;
  170. return(0);
  171. }
  172. void define(int type)
  173. {
  174. char *p1, *p2;
  175. extern int ftune(char *, char *);
  176. getstr(token, SSIZE); /* get name */
  177. if (type != DEFINE) {
  178. cstr(token, 1, SSIZE); /* skip the definition too */
  179. return;
  180. }
  181. p1 = strsave(token);
  182. if (cstr(token, 1, SSIZE))
  183. ERROR "Unterminated definition at %.20s", token FATAL;
  184. if (lookup(ftunetbl, p1) != NULL) { /* double tuning param */
  185. dprintf(".\ttune %s %s\n", p1, token);
  186. ftune(p1, token);
  187. } else {
  188. p2 = strsave(token);
  189. install(deftbl, p1, p2, 0);
  190. dprintf(".\tname %s defined as %s\n", p1, p2);
  191. }
  192. }
  193. void ifdef(void) /* do body if name is defined */
  194. {
  195. char name[100], *p;
  196. getstr(name, sizeof(name)); /* get name */
  197. cstr(token, 1, SSIZE); /* and body */
  198. if (lookup(deftbl, name) != NULL) { /* found it */
  199. p = strsave(token);
  200. pushsrc(Free, p);
  201. pushsrc(String, p);
  202. }
  203. }
  204. char *spaceval = NULL;
  205. void space(void) /* collect line of form "space amt" to replace \x in output */
  206. {
  207. getstr(token, SSIZE);
  208. spaceval = strsave(token);
  209. dprintf(".\tsetting spaceval to %s\n", token);
  210. }
  211. char *strsave(char *s)
  212. {
  213. register char *q;
  214. q = malloc(strlen(s)+1);
  215. if (q == NULL)
  216. ERROR "out of space in strsave on %s", s FATAL;
  217. strcpy(q, s);
  218. return(q);
  219. }
  220. void include(void)
  221. {
  222. char name[100];
  223. FILE *fin;
  224. int c;
  225. extern int errno;
  226. while ((c = input()) == ' ')
  227. ;
  228. unput(c);
  229. cstr(name, c == '"', sizeof(name)); /* gets it quoted or not */
  230. if ((fin = fopen(name, "r")) == NULL)
  231. ERROR "can't open file %s", name FATAL;
  232. errno = 0;
  233. curfile++;
  234. curfile->fin = fin;
  235. curfile->fname = strsave(name);
  236. curfile->lineno = 0;
  237. printf(".lf 1 %s\n", curfile->fname);
  238. pushsrc(File, curfile->fname);
  239. }
  240. void delim(void)
  241. {
  242. yyval = eqnreg = 0;
  243. if (cstr(token, 0, SSIZE))
  244. ERROR "Bizarre delimiters" FATAL;
  245. lefteq = token[0];
  246. righteq = token[1];
  247. if (!isprint(lefteq) || !isprint(righteq))
  248. ERROR "Bizarre delimiters" FATAL;
  249. if (lefteq == 'o' && righteq == 'f')
  250. lefteq = righteq = '\0';
  251. }