lex.c 9.7 KB

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