lex.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* $Id: lex.c,v 1.15 2002/12/15 14:27:43 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /*
  32. * This lexical analyser was written to be smaller than flex and with less
  33. * features. Its attributes in random order: capable of multiple instances, one
  34. * token lookahead, strings delimited by ' or ", comments can start anywhere
  35. * with # and last until eol, max token size = _POSIX2_LINE_LENGTH. Tokens are
  36. * defined in lex.h, the mapping of tokens to ascii happens here.
  37. *
  38. * Usage:
  39. *
  40. * l = open_lex(filename);
  41. * while (lex_nexttoken(l)) {
  42. * use l->token, l->op, l->value
  43. * }
  44. * close_lex(l);
  45. */
  46. #include <sys/types.h>
  47. #include <errno.h>
  48. #include <limits.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include "xmalloc.h"
  53. #include "lex.h"
  54. #include "error.h"
  55. static struct {
  56. const char *name;
  57. int opcode;
  58. } keywords[] = {
  59. { "{", LXT_BEGIN },
  60. { "}", LXT_END },
  61. { "(", LXT_OPEN },
  62. { ")", LXT_CLOSE },
  63. { ",", LXT_COMMA },
  64. { "accept", LXT_ACCEPT },
  65. { "cpu", LXT_CPU },
  66. { "datadir", LXT_DATADIR },
  67. { "debug", LXT_DEBUG },
  68. { "if", LXT_IF },
  69. { "in", LXT_IN },
  70. { "io", LXT_IO },
  71. { "mbuf", LXT_MBUF },
  72. { "mem", LXT_MEM },
  73. { "monitor", LXT_MONITOR },
  74. { "mux", LXT_MUX },
  75. { "pf", LXT_PF },
  76. { "port", LXT_PORT },
  77. { "proc", LXT_PROC },
  78. { "source", LXT_SOURCE },
  79. { "stream", LXT_STREAM },
  80. { "to", LXT_TO },
  81. { "write", LXT_WRITE },
  82. { NULL, 0 }
  83. };
  84. #define KW_OPS "{},()"
  85. /* Return the number of the token pointed to by cp or LXT_BADTOKEN */
  86. int
  87. parse_token(const char *cp)
  88. {
  89. u_int i;
  90. for (i = 0; keywords[i].name; i++)
  91. if (strcasecmp(cp, keywords[i].name) == 0)
  92. return keywords[i].opcode;
  93. return LXT_BADTOKEN;
  94. }
  95. /* Return the ascii representation of an opcode */
  96. const char *
  97. parse_opcode(const int op)
  98. {
  99. u_int i;
  100. for (i = 0; keywords[i].name; i++)
  101. if (keywords[i].opcode == op)
  102. return keywords[i].name;
  103. return NULL;
  104. }
  105. /* Read a line and increase buffer if needed */
  106. int
  107. lex_readline(struct lex * l)
  108. {
  109. char *bp;
  110. bp = l->buffer;
  111. if (l->buffer) {
  112. if ((l->curpos < l->endpos) &&
  113. ((l->bsize - l->endpos) < _POSIX2_LINE_MAX)) {
  114. l->bsize += _POSIX2_LINE_MAX;
  115. l->buffer = xrealloc(l->buffer, l->bsize);
  116. bp = l->buffer;
  117. bp += l->endpos;
  118. }
  119. else {
  120. l->curpos = 0;
  121. l->endpos = 0;
  122. }
  123. }
  124. else {
  125. l->bsize = _POSIX2_LINE_MAX;
  126. l->buffer = xmalloc(l->bsize);
  127. bp = l->buffer;
  128. }
  129. if (!fgets(bp, (l->buffer + l->bsize) - bp, l->fh))
  130. return 0;
  131. else {
  132. l->endpos += strlen(bp) - 1;
  133. return 1;
  134. }
  135. }
  136. /* Copy char out of input stream */
  137. void
  138. lex_copychar(struct lex * l)
  139. {
  140. l->token[l->tokpos] = l->buffer[l->curpos];
  141. if (++l->tokpos >= _POSIX2_LINE_MAX) {
  142. l->token[_POSIX2_LINE_MAX - 1] = '\0';
  143. fatal("%s:%d: parse error at '%s'", l->filename, l->cline, l->token);
  144. /* NOT REACHED */
  145. }
  146. }
  147. /* Get next char, read next line if needed */
  148. int
  149. lex_nextchar(struct lex * l)
  150. {
  151. l->curpos++;
  152. if (l->curpos > l->endpos)
  153. if (!lex_readline(l))
  154. return 0;
  155. if (l->buffer[l->curpos] == '\n')
  156. l->cline++;
  157. return 1;
  158. }
  159. /* Close of current token with a '\0' */
  160. void
  161. lex_termtoken(struct lex * l)
  162. {
  163. l->token[l->tokpos] = l->token[_POSIX2_LINE_MAX - 1] = '\0';
  164. l->tokpos = 0;
  165. }
  166. /* Unget token; the lexer allows 1 look a head. */
  167. void
  168. lex_ungettoken(struct lex * l)
  169. {
  170. l->unget = 1;
  171. }
  172. /* Get the next token in lex->token. return 0 if no more tokens found. */
  173. int
  174. lex_nexttoken(struct lex * l)
  175. {
  176. /* return same token as last time if it has been pushed back */
  177. if (l->unget) {
  178. l->unget = 0;
  179. return 1;
  180. }
  181. l->op = LXT_BADTOKEN;
  182. l->value = 0;
  183. l->type = LXY_UNKNOWN;
  184. /* find first non whitespace */
  185. while (l->buffer[l->curpos] == ' ' ||
  186. l->buffer[l->curpos] == '\t' ||
  187. l->buffer[l->curpos] == '\r' ||
  188. l->buffer[l->curpos] == '\n' ||
  189. l->buffer[l->curpos] == '\0' ||
  190. l->buffer[l->curpos] == '#') {
  191. /* flush rest of line if comment */
  192. if (l->buffer[l->curpos] == '#') {
  193. while (l->buffer[l->curpos] != '\n')
  194. if (!lex_nextchar(l))
  195. return 0;
  196. }
  197. else if (!lex_nextchar(l))
  198. return 0;
  199. }
  200. l->type = LXY_STRING;
  201. /* "delimited string" */
  202. if (l->buffer[l->curpos] == '"') {
  203. if (!lex_nextchar(l)) {
  204. warning("%s:%d: unbalanced '\"'", l->filename, l->cline);
  205. return 0;
  206. }
  207. while (l->buffer[l->curpos] != '"') {
  208. lex_copychar(l);
  209. if (!lex_nextchar(l)) {
  210. warning("%s:%d: unbalanced '\"'", l->filename, l->cline);
  211. return 0;
  212. }
  213. }
  214. lex_termtoken(l);
  215. lex_nextchar(l);
  216. return 1;
  217. }
  218. /* 'delimited string' */
  219. if (l->buffer[l->curpos] == '\'') {
  220. if (!lex_nextchar(l)) {
  221. warning("%s:%d: unbalanced \"\'\"", l->filename, l->cline);
  222. return 0;
  223. }
  224. while (l->buffer[l->curpos] != '\'') {
  225. lex_copychar(l);
  226. if (!lex_nextchar(l)) {
  227. warning("%s:%d: unbalanced \"\'\"", l->filename, l->cline);
  228. return 0;
  229. }
  230. }
  231. lex_termtoken(l);
  232. lex_nextchar(l);
  233. return 1;
  234. }
  235. /* one char keyword */
  236. if (strchr(KW_OPS, l->buffer[l->curpos])) {
  237. lex_copychar(l);
  238. lex_termtoken(l);
  239. l->op = parse_token(l->token);
  240. lex_nextchar(l);
  241. return 1;
  242. }
  243. /* single keyword */
  244. while (l->buffer[l->curpos] != ' ' &&
  245. l->buffer[l->curpos] != '\t' &&
  246. l->buffer[l->curpos] != '\r' &&
  247. l->buffer[l->curpos] != '\n' &&
  248. l->buffer[l->curpos] != '\0' &&
  249. l->buffer[l->curpos] != '#' &&
  250. (strchr(KW_OPS, l->buffer[l->curpos]) == NULL)) {
  251. lex_copychar(l);
  252. if (!lex_nextchar(l))
  253. break;
  254. }
  255. lex_termtoken(l);
  256. l->op = parse_token(l->token);
  257. /* number */
  258. if (l->token[0] >= '0' && l->token[0] <= '9') {
  259. if (strlen(l->token) == strspn(l->token, "0123456789")) {
  260. l->type = LXY_NUMBER;
  261. l->value = strtol(l->token, NULL, 10);
  262. }
  263. }
  264. return 1;
  265. }
  266. /* Create and initialize a lexical analyser */
  267. struct lex *
  268. open_lex(const char *filename)
  269. {
  270. struct lex *l;
  271. l = xmalloc(sizeof(struct lex));
  272. l->buffer = NULL;
  273. l->cline = 1;
  274. l->curpos = 0;
  275. l->endpos = 0;
  276. l->filename = filename;
  277. l->op = LXT_BADTOKEN;
  278. l->token = xmalloc(_POSIX2_LINE_MAX);
  279. l->tokpos = 0;
  280. l->type = LXY_UNKNOWN;
  281. l->unget = 0;
  282. l->value = 0;
  283. if ((l->fh = fopen(l->filename, "r")) == NULL) {
  284. warning("could not open file '%s':%s",
  285. l->filename, strerror(errno));
  286. xfree(l);
  287. return NULL;
  288. }
  289. lex_readline(l);
  290. return l;
  291. }
  292. /* Destroy a lexical analyser */
  293. void
  294. close_lex(struct lex * l)
  295. {
  296. if (l == NULL)
  297. return;
  298. if (l->fh)
  299. fclose(l->fh);
  300. if (l->buffer)
  301. xfree(l->buffer);
  302. if (l->token)
  303. xfree(l->token);
  304. xfree(l);
  305. }
  306. /* Signal a parse error */
  307. void
  308. parse_error(struct lex * l, const char *s)
  309. {
  310. warning("%s:%d: expected %s (found '%.8s')",
  311. l->filename, l->cline, s, l->token);
  312. }