lex.c 8.6 KB

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