lex.c 9.8 KB

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