lex.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* $Id: lex.c,v 1.30 2008/01/30 12:06:50 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2008 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. { "cpuiow", LXT_CPUIOW },
  68. { "datadir", LXT_DATADIR },
  69. { "debug", LXT_DEBUG },
  70. { "df", LXT_DF },
  71. { "every", LXT_EVERY },
  72. { "from", LXT_FROM },
  73. { "if", LXT_IF },
  74. { "if1", LXT_IF1 },
  75. { "if2", LXT_IF },
  76. { "in", LXT_IN },
  77. { "io", LXT_IO },
  78. { "io1", LXT_IO1 },
  79. { "io2", LXT_IO },
  80. { "mbuf", LXT_MBUF },
  81. { "mem", LXT_MEM },
  82. { "mem1", LXT_MEM1 },
  83. { "mem2", LXT_MEM },
  84. { "monitor", LXT_MONITOR },
  85. { "mux", LXT_MUX },
  86. { "pf", LXT_PF },
  87. { "pfq", LXT_PFQ },
  88. { "port", LXT_PORT },
  89. { "proc", LXT_PROC },
  90. { "second", LXT_SECOND },
  91. { "seconds", LXT_SECONDS },
  92. { "sensor", LXT_SENSOR },
  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)
  157. return 0;
  158. l->curpos++;
  159. if (l->curpos >= l->endpos)
  160. if (!lex_readline(l))
  161. return 0;
  162. if (l->buffer[l->curpos] == '\n')
  163. l->cline++;
  164. return 1;
  165. }
  166. /* Close of current token with a '\0' */
  167. void
  168. lex_termtoken(struct lex *l)
  169. {
  170. if (l == NULL)
  171. return;
  172. l->token[l->tokpos] = l->token[_POSIX2_LINE_MAX - 1] = '\0';
  173. l->tokpos = 0;
  174. }
  175. /* Unget token; the lexer allows 1 look a head. */
  176. void
  177. lex_ungettoken(struct lex *l)
  178. {
  179. if (l == NULL)
  180. return;
  181. l->unget = 1;
  182. }
  183. /* Get the next token in lex->token. return 0 if no more tokens found. */
  184. int
  185. lex_nexttoken(struct lex *l)
  186. {
  187. if (l == NULL)
  188. return 0;
  189. /* return same token as last time if it has been pushed back */
  190. if (l->unget) {
  191. l->unget = 0;
  192. return 1;
  193. }
  194. l->op = LXT_BADTOKEN;
  195. l->value = 0;
  196. l->type = LXY_UNKNOWN;
  197. /* find first non whitespace */
  198. while (l->buffer[l->curpos] == ' ' ||
  199. l->buffer[l->curpos] == '\t' ||
  200. l->buffer[l->curpos] == '\r' ||
  201. l->buffer[l->curpos] == '\n' ||
  202. l->buffer[l->curpos] == '\0' ||
  203. l->buffer[l->curpos] == '#') {
  204. /* flush rest of line if comment */
  205. if (l->buffer[l->curpos] == '#') {
  206. while (l->buffer[l->curpos] != '\n')
  207. if (!lex_nextchar(l))
  208. return 0;
  209. } else if (!lex_nextchar(l))
  210. return 0;
  211. }
  212. l->type = LXY_STRING;
  213. /* "delimited string" */
  214. if (l->buffer[l->curpos] == '"') {
  215. if (!lex_nextchar(l)) {
  216. warning("%.200s:%d: unbalanced '\"'", l->filename, l->cline);
  217. return 0;
  218. }
  219. while (l->buffer[l->curpos] != '"') {
  220. lex_copychar(l);
  221. if (!lex_nextchar(l)) {
  222. warning("%.200s:%d: unbalanced '\"'", l->filename, l->cline);
  223. return 0;
  224. }
  225. }
  226. lex_termtoken(l);
  227. lex_nextchar(l);
  228. return 1;
  229. }
  230. /* 'delimited string' */
  231. if (l->buffer[l->curpos] == '\'') {
  232. if (!lex_nextchar(l)) {
  233. warning("%.200s:%d: unbalanced \"\'\"", l->filename, l->cline);
  234. return 0;
  235. }
  236. while (l->buffer[l->curpos] != '\'') {
  237. lex_copychar(l);
  238. if (!lex_nextchar(l)) {
  239. warning("%.200s:%d: unbalanced \"\'\"", l->filename, l->cline);
  240. return 0;
  241. }
  242. }
  243. lex_termtoken(l);
  244. lex_nextchar(l);
  245. return 1;
  246. }
  247. /* one char keyword */
  248. if (strchr(KW_OPS, l->buffer[l->curpos])) {
  249. lex_copychar(l);
  250. lex_termtoken(l);
  251. l->op = parse_token(l->token);
  252. lex_nextchar(l);
  253. return 1;
  254. }
  255. /* single keyword */
  256. while (l->buffer[l->curpos] != ' ' &&
  257. l->buffer[l->curpos] != '\t' &&
  258. l->buffer[l->curpos] != '\r' &&
  259. l->buffer[l->curpos] != '\n' &&
  260. l->buffer[l->curpos] != '\0' &&
  261. l->buffer[l->curpos] != '#' &&
  262. (strchr(KW_OPS, l->buffer[l->curpos]) == NULL)) {
  263. lex_copychar(l);
  264. if (!lex_nextchar(l))
  265. break;
  266. }
  267. lex_termtoken(l);
  268. l->op = parse_token(l->token);
  269. /* number */
  270. if (l->token[0] >= '0' && l->token[0] <= '9') {
  271. if (strlen(l->token) == strspn(l->token, "0123456789")) {
  272. l->type = LXY_NUMBER;
  273. l->value = strtol(l->token, NULL, 10);
  274. }
  275. }
  276. return 1;
  277. }
  278. /* Create and initialize a lexical analyser */
  279. struct lex *
  280. open_lex(const char *filename)
  281. {
  282. struct lex *l;
  283. l = xmalloc(sizeof(struct lex));
  284. reset_lex(l);
  285. l->buffer = NULL;
  286. l->filename = filename;
  287. l->token = xmalloc(_POSIX2_LINE_MAX);
  288. if ((l->fh = open(l->filename, O_RDONLY)) < 0) {
  289. warning("could not open file \"%.200s\":%.200s",
  290. l->filename, strerror(errno));
  291. close_lex(l);
  292. return NULL;
  293. }
  294. lex_readline(l);
  295. return l;
  296. }
  297. /* Prepare file for another lexer run */
  298. void
  299. rewind_lex(struct lex *l)
  300. {
  301. off_t filepos;
  302. if (l == NULL)
  303. return;
  304. reset_lex(l);
  305. if ((filepos = lseek(l->fh, (off_t)0, SEEK_SET)) == -1) {
  306. warning("could not rewind file '%.200s':%.200s",
  307. l->filename, strerror(errno));
  308. }
  309. }
  310. /* Reset lexer to start of file defaults */
  311. void
  312. reset_lex(struct lex *l)
  313. {
  314. if (l == NULL)
  315. return;
  316. l->cline = 1;
  317. l->curpos = 0;
  318. l->endpos = 0;
  319. l->op = LXT_BADTOKEN;
  320. l->tokpos = 0;
  321. l->type = LXY_UNKNOWN;
  322. l->unget = 0;
  323. l->value = 0;
  324. }
  325. /* Destroy a lexical analyser */
  326. void
  327. close_lex(struct lex *l)
  328. {
  329. if (l == NULL)
  330. return;
  331. if (l->fh)
  332. close(l->fh);
  333. if (l->buffer)
  334. xfree(l->buffer);
  335. if (l->token)
  336. xfree(l->token);
  337. xfree(l);
  338. }
  339. /* Signal a parse error */
  340. void
  341. parse_error(struct lex *l, const char *s)
  342. {
  343. warning("%.200s:%d: expected '%.200s' found '%.8s'",
  344. l->filename, l->cline, s, l->token);
  345. }