lex.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* $Id: lex.c,v 1.21 2004/02/27 09:50:18 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2004 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 <fcntl.h>
  49. #include <limits.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <unistd.h>
  53. #include "xmalloc.h"
  54. #include "lex.h"
  55. #include "error.h"
  56. static struct {
  57. const char *name;
  58. int opcode;
  59. } keywords[] = {
  60. { "{", LXT_BEGIN },
  61. { "}", LXT_END },
  62. { "(", LXT_OPEN },
  63. { ")", LXT_CLOSE },
  64. { ",", LXT_COMMA },
  65. { "accept", LXT_ACCEPT },
  66. { "cpu", LXT_CPU },
  67. { "datadir", LXT_DATADIR },
  68. { "debug", LXT_DEBUG },
  69. { "every", LXT_EVERY },
  70. { "if", LXT_IF },
  71. { "in", LXT_IN },
  72. { "io", LXT_IO },
  73. { "io1", LXT_IO1 },
  74. { "io2", LXT_IO },
  75. { "mbuf", LXT_MBUF },
  76. { "mem", LXT_MEM },
  77. { "monitor", LXT_MONITOR },
  78. { "mux", LXT_MUX },
  79. { "pf", LXT_PF },
  80. { "port", LXT_PORT },
  81. { "proc", LXT_PROC },
  82. { "second", LXT_SECOND },
  83. { "seconds", LXT_SECONDS },
  84. { "sensor", LXT_SENSOR },
  85. { "source", LXT_SOURCE },
  86. { "stream", LXT_STREAM },
  87. { "to", LXT_TO },
  88. { "write", LXT_WRITE },
  89. { NULL, 0 }
  90. };
  91. #define KW_OPS "{},()"
  92. /* Return the number of the token pointed to by cp or LXT_BADTOKEN */
  93. int
  94. parse_token(const char *cp)
  95. {
  96. u_int i;
  97. for (i = 0; keywords[i].name; i++)
  98. if (strcasecmp(cp, keywords[i].name) == 0)
  99. return keywords[i].opcode;
  100. return LXT_BADTOKEN;
  101. }
  102. /* Return the ascii representation of an opcode */
  103. const char *
  104. parse_opcode(const int op)
  105. {
  106. u_int i;
  107. for (i = 0; keywords[i].name; i++)
  108. if (keywords[i].opcode == op)
  109. return keywords[i].name;
  110. return NULL;
  111. }
  112. /* Read a line and increase buffer if needed */
  113. int
  114. lex_readline(struct lex *l)
  115. {
  116. char *bp;
  117. bp = l->buffer;
  118. if (l->buffer) {
  119. if ((l->curpos < l->endpos) &&
  120. ((l->bsize - l->endpos) < _POSIX2_LINE_MAX)) {
  121. l->bsize += _POSIX2_LINE_MAX;
  122. l->buffer = xrealloc(l->buffer, l->bsize);
  123. bp = l->buffer;
  124. bp += l->endpos;
  125. } else {
  126. l->curpos = 0;
  127. l->endpos = 0;
  128. }
  129. } else {
  130. l->bsize = _POSIX2_LINE_MAX;
  131. l->buffer = xmalloc(l->bsize);
  132. bp = l->buffer;
  133. }
  134. if (!read(l->fh, bp, (l->buffer + l->bsize) - bp))
  135. return 0;
  136. else {
  137. l->endpos += strlen(bp) - 1;
  138. return 1;
  139. }
  140. }
  141. /* Copy char out of input stream */
  142. void
  143. lex_copychar(struct lex *l)
  144. {
  145. l->token[l->tokpos] = l->buffer[l->curpos];
  146. if (++l->tokpos >= _POSIX2_LINE_MAX) {
  147. l->token[_POSIX2_LINE_MAX - 1] = '\0';
  148. fatal("%.200s:%d: parse error at '%.200s'", l->filename, l->cline, l->token);
  149. /* NOT REACHED */
  150. }
  151. }
  152. /* Get next char, read next line if needed */
  153. int
  154. lex_nextchar(struct lex *l)
  155. {
  156. l->curpos++;
  157. if (l->curpos > l->endpos)
  158. if (!lex_readline(l))
  159. return 0;
  160. if (l->buffer[l->curpos] == '\n')
  161. l->cline++;
  162. return 1;
  163. }
  164. /* Close of current token with a '\0' */
  165. void
  166. lex_termtoken(struct lex *l)
  167. {
  168. l->token[l->tokpos] = l->token[_POSIX2_LINE_MAX - 1] = '\0';
  169. l->tokpos = 0;
  170. }
  171. /* Unget token; the lexer allows 1 look a head. */
  172. void
  173. lex_ungettoken(struct lex *l)
  174. {
  175. l->unget = 1;
  176. }
  177. /* Get the next token in lex->token. return 0 if no more tokens found. */
  178. int
  179. lex_nexttoken(struct lex *l)
  180. {
  181. /* return same token as last time if it has been pushed back */
  182. if (l->unget) {
  183. l->unget = 0;
  184. return 1;
  185. }
  186. l->op = LXT_BADTOKEN;
  187. l->value = 0;
  188. l->type = LXY_UNKNOWN;
  189. /* find first non whitespace */
  190. while (l->buffer[l->curpos] == ' ' ||
  191. l->buffer[l->curpos] == '\t' ||
  192. l->buffer[l->curpos] == '\r' ||
  193. l->buffer[l->curpos] == '\n' ||
  194. l->buffer[l->curpos] == '\0' ||
  195. l->buffer[l->curpos] == '#') {
  196. /* flush rest of line if comment */
  197. if (l->buffer[l->curpos] == '#') {
  198. while (l->buffer[l->curpos] != '\n')
  199. if (!lex_nextchar(l))
  200. return 0;
  201. } else if (!lex_nextchar(l))
  202. return 0;
  203. }
  204. l->type = LXY_STRING;
  205. /* "delimited string" */
  206. if (l->buffer[l->curpos] == '"') {
  207. if (!lex_nextchar(l)) {
  208. warning("%.200s:%d: unbalanced '\"'", l->filename, l->cline);
  209. return 0;
  210. }
  211. while (l->buffer[l->curpos] != '"') {
  212. lex_copychar(l);
  213. if (!lex_nextchar(l)) {
  214. warning("%.200s:%d: unbalanced '\"'", l->filename, l->cline);
  215. return 0;
  216. }
  217. }
  218. lex_termtoken(l);
  219. lex_nextchar(l);
  220. return 1;
  221. }
  222. /* 'delimited string' */
  223. if (l->buffer[l->curpos] == '\'') {
  224. if (!lex_nextchar(l)) {
  225. warning("%.200s:%d: unbalanced \"\'\"", l->filename, l->cline);
  226. return 0;
  227. }
  228. while (l->buffer[l->curpos] != '\'') {
  229. lex_copychar(l);
  230. if (!lex_nextchar(l)) {
  231. warning("%.200s:%d: unbalanced \"\'\"", l->filename, l->cline);
  232. return 0;
  233. }
  234. }
  235. lex_termtoken(l);
  236. lex_nextchar(l);
  237. return 1;
  238. }
  239. /* one char keyword */
  240. if (strchr(KW_OPS, l->buffer[l->curpos])) {
  241. lex_copychar(l);
  242. lex_termtoken(l);
  243. l->op = parse_token(l->token);
  244. lex_nextchar(l);
  245. return 1;
  246. }
  247. /* single keyword */
  248. while (l->buffer[l->curpos] != ' ' &&
  249. l->buffer[l->curpos] != '\t' &&
  250. l->buffer[l->curpos] != '\r' &&
  251. l->buffer[l->curpos] != '\n' &&
  252. l->buffer[l->curpos] != '\0' &&
  253. l->buffer[l->curpos] != '#' &&
  254. (strchr(KW_OPS, l->buffer[l->curpos]) == NULL)) {
  255. lex_copychar(l);
  256. if (!lex_nextchar(l))
  257. break;
  258. }
  259. lex_termtoken(l);
  260. l->op = parse_token(l->token);
  261. /* number */
  262. if (l->token[0] >= '0' && l->token[0] <= '9') {
  263. if (strlen(l->token) == strspn(l->token, "0123456789")) {
  264. l->type = LXY_NUMBER;
  265. l->value = strtol(l->token, NULL, 10);
  266. }
  267. }
  268. return 1;
  269. }
  270. /* Create and initialize a lexical analyser */
  271. struct lex *
  272. open_lex(const char *filename)
  273. {
  274. struct lex *l;
  275. l = xmalloc(sizeof(struct lex));
  276. reset_lex(l);
  277. l->buffer = NULL;
  278. l->filename = filename;
  279. l->token = xmalloc(_POSIX2_LINE_MAX);
  280. if ((l->fh = open(l->filename, O_RDONLY)) < 0) {
  281. warning("could not open file '%.200s':%.200s",
  282. l->filename, strerror(errno));
  283. close_lex(l);
  284. return NULL;
  285. }
  286. lex_readline(l);
  287. return l;
  288. }
  289. /* Prepare file for another lexer run */
  290. void
  291. rewind_lex(struct lex *l)
  292. {
  293. off_t filepos;
  294. reset_lex(l);
  295. if ((filepos = lseek(l->fh, (off_t)0, SEEK_SET)) == -1) {
  296. warning("could not rewind file '%.200s':%.200s",
  297. l->filename, strerror(errno));
  298. }
  299. }
  300. /* Reset lexer to start of file defaults */
  301. void
  302. reset_lex(struct lex *l)
  303. {
  304. l->cline = 1;
  305. l->curpos = 0;
  306. l->endpos = 0;
  307. l->op = LXT_BADTOKEN;
  308. l->tokpos = 0;
  309. l->type = LXY_UNKNOWN;
  310. l->unget = 0;
  311. l->value = 0;
  312. }
  313. /* Destroy a lexical analyser */
  314. void
  315. close_lex(struct lex *l)
  316. {
  317. if (l == NULL)
  318. return;
  319. if (l->fh)
  320. close(l->fh);
  321. if (l->buffer)
  322. xfree(l->buffer);
  323. if (l->token)
  324. xfree(l->token);
  325. xfree(l);
  326. }
  327. /* Signal a parse error */
  328. void
  329. parse_error(struct lex *l, const char *s)
  330. {
  331. warning("%.200s:%d: expected '%.200s' found '%.8s'",
  332. l->filename, l->cline, s, l->token);
  333. }