lex.c 9.5 KB

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