lex.c 9.4 KB

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