rpc_scan.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * From: @(#)rpc_scan.c 1.11 89/02/22
  3. *
  4. * Copyright (c) 2010, Oracle America, Inc.
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * 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
  14. * provided with the distribution.
  15. * * Neither the name of the "Oracle America, Inc." nor the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * rpc_scan.c, Scanner for the RPC protocol compiler
  34. * Copyright (C) 1987, Sun Microsystems, Inc.
  35. */
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include <string.h>
  39. #include <libintl.h>
  40. #include "rpc_scan.h"
  41. #include "rpc_parse.h"
  42. #include "rpc_util.h"
  43. #include "proto.h"
  44. #define startcomment(where) (where[0] == '/' && where[1] == '*')
  45. #define endcomment(where) (where[-1] == '*' && where[0] == '/')
  46. static int pushed = 0; /* is a token pushed */
  47. static token lasttok; /* last token, if pushed */
  48. static void unget_token (token * tokp);
  49. static void findstrconst (const char **str, const char **val);
  50. static void findchrconst (const char **str, const char **val);
  51. static void findconst (const char **str, const char **val);
  52. static void findkind (const char **mark, token * tokp);
  53. static int cppline (const char *line);
  54. static int directive (const char *line);
  55. static void printdirective (const char *line);
  56. static void docppline (const char *line, int *lineno, const char **fname);
  57. /*
  58. * scan expecting 1 given token
  59. */
  60. void
  61. scan (tok_kind expect, token * tokp)
  62. {
  63. get_token (tokp);
  64. if (tokp->kind != expect)
  65. expected1 (expect);
  66. }
  67. /*
  68. * scan expecting any of the 2 given tokens
  69. */
  70. void
  71. scan2 (tok_kind expect1, tok_kind expect2, token * tokp)
  72. {
  73. get_token (tokp);
  74. if (tokp->kind != expect1 && tokp->kind != expect2)
  75. {
  76. expected2 (expect1, expect2);
  77. }
  78. }
  79. /*
  80. * scan expecting any of the 3 given token
  81. */
  82. void
  83. scan3 (tok_kind expect1, tok_kind expect2, tok_kind expect3, token * tokp)
  84. {
  85. get_token (tokp);
  86. if (tokp->kind != expect1 && tokp->kind != expect2
  87. && tokp->kind != expect3)
  88. {
  89. expected3 (expect1, expect2, expect3);
  90. }
  91. }
  92. /*
  93. * scan expecting a constant, possibly symbolic
  94. */
  95. void
  96. scan_num (token *tokp)
  97. {
  98. get_token (tokp);
  99. switch (tokp->kind)
  100. {
  101. case TOK_IDENT:
  102. break;
  103. default:
  104. error (_("constant or identifier expected"));
  105. }
  106. }
  107. /*
  108. * Peek at the next token
  109. */
  110. void
  111. peek (token *tokp)
  112. {
  113. get_token (tokp);
  114. unget_token (tokp);
  115. }
  116. /*
  117. * Peek at the next token and scan it if it matches what you expect
  118. */
  119. int
  120. peekscan (tok_kind expect, token *tokp)
  121. {
  122. peek (tokp);
  123. if (tokp->kind == expect)
  124. {
  125. get_token (tokp);
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. /*
  131. * Get the next token, printing out any directive that are encountered.
  132. */
  133. void
  134. get_token (token *tokp)
  135. {
  136. int commenting;
  137. if (pushed)
  138. {
  139. pushed = 0;
  140. *tokp = lasttok;
  141. return;
  142. }
  143. commenting = 0;
  144. for (;;)
  145. {
  146. if (*where == 0)
  147. {
  148. for (;;)
  149. {
  150. if (!fgets (curline, MAXLINESIZE, fin))
  151. {
  152. tokp->kind = TOK_EOF;
  153. *curline = 0;
  154. where = curline;
  155. return;
  156. }
  157. linenum++;
  158. if (commenting)
  159. {
  160. break;
  161. }
  162. else if (cppline (curline))
  163. {
  164. docppline (curline, &linenum,
  165. &infilename);
  166. }
  167. else if (directive (curline))
  168. {
  169. printdirective (curline);
  170. }
  171. else
  172. {
  173. break;
  174. }
  175. }
  176. where = curline;
  177. }
  178. else if (isspace (*where))
  179. {
  180. while (isspace (*where))
  181. {
  182. where++; /* eat */
  183. }
  184. }
  185. else if (commenting)
  186. {
  187. for (where++; *where; where++)
  188. {
  189. if (endcomment (where))
  190. {
  191. where++;
  192. commenting--;
  193. break;
  194. }
  195. }
  196. }
  197. else if (startcomment (where))
  198. {
  199. where += 2;
  200. commenting++;
  201. }
  202. else
  203. {
  204. break;
  205. }
  206. }
  207. /*
  208. * 'where' is not whitespace, comment or directive Must be a token!
  209. */
  210. switch (*where)
  211. {
  212. case ':':
  213. tokp->kind = TOK_COLON;
  214. where++;
  215. break;
  216. case ';':
  217. tokp->kind = TOK_SEMICOLON;
  218. where++;
  219. break;
  220. case ',':
  221. tokp->kind = TOK_COMMA;
  222. where++;
  223. break;
  224. case '=':
  225. tokp->kind = TOK_EQUAL;
  226. where++;
  227. break;
  228. case '*':
  229. tokp->kind = TOK_STAR;
  230. where++;
  231. break;
  232. case '[':
  233. tokp->kind = TOK_LBRACKET;
  234. where++;
  235. break;
  236. case ']':
  237. tokp->kind = TOK_RBRACKET;
  238. where++;
  239. break;
  240. case '{':
  241. tokp->kind = TOK_LBRACE;
  242. where++;
  243. break;
  244. case '}':
  245. tokp->kind = TOK_RBRACE;
  246. where++;
  247. break;
  248. case '(':
  249. tokp->kind = TOK_LPAREN;
  250. where++;
  251. break;
  252. case ')':
  253. tokp->kind = TOK_RPAREN;
  254. where++;
  255. break;
  256. case '<':
  257. tokp->kind = TOK_LANGLE;
  258. where++;
  259. break;
  260. case '>':
  261. tokp->kind = TOK_RANGLE;
  262. where++;
  263. break;
  264. case '"':
  265. tokp->kind = TOK_STRCONST;
  266. findstrconst (&where, &tokp->str);
  267. break;
  268. case '\'':
  269. tokp->kind = TOK_CHARCONST;
  270. findchrconst (&where, &tokp->str);
  271. break;
  272. case '-':
  273. case '0':
  274. case '1':
  275. case '2':
  276. case '3':
  277. case '4':
  278. case '5':
  279. case '6':
  280. case '7':
  281. case '8':
  282. case '9':
  283. tokp->kind = TOK_IDENT;
  284. findconst (&where, &tokp->str);
  285. break;
  286. default:
  287. if (!(isalpha (*where) || *where == '_'))
  288. {
  289. char buf[100];
  290. char *p;
  291. s_print (buf, _("illegal character in file: "));
  292. p = buf + strlen (buf);
  293. if (isprint (*where))
  294. {
  295. s_print (p, "%c", *where);
  296. }
  297. else
  298. {
  299. s_print (p, "%d", *where);
  300. }
  301. error (buf);
  302. }
  303. findkind (&where, tokp);
  304. break;
  305. }
  306. }
  307. static void
  308. unget_token (token * tokp)
  309. {
  310. lasttok = *tokp;
  311. pushed = 1;
  312. }
  313. static void
  314. findstrconst (const char **str, const char **val)
  315. {
  316. const char *p;
  317. char *tmp;
  318. int size;
  319. p = *str;
  320. do
  321. {
  322. p++;
  323. }
  324. while (*p && *p != '"');
  325. if (*p == 0)
  326. {
  327. error (_("unterminated string constant"));
  328. }
  329. p++;
  330. size = p - *str;
  331. tmp = alloc (size + 1);
  332. strncpy (tmp, *str, size);
  333. tmp[size] = 0;
  334. *val = tmp;
  335. *str = p;
  336. }
  337. static void
  338. findchrconst (const char **str, const char **val)
  339. {
  340. const char *p;
  341. char *tmp;
  342. int size;
  343. p = *str;
  344. do
  345. {
  346. p++;
  347. }
  348. while (*p && *p != '\'');
  349. if (*p == 0)
  350. {
  351. error (_("unterminated string constant"));
  352. }
  353. p++;
  354. size = p - *str;
  355. if (size != 3)
  356. {
  357. error (_("empty char string"));
  358. }
  359. tmp = alloc (size + 1);
  360. strncpy (tmp, *str, size);
  361. tmp[size] = 0;
  362. *val = tmp;
  363. *str = p;
  364. }
  365. static void
  366. findconst (const char **str, const char **val)
  367. {
  368. const char *p;
  369. char *tmp;
  370. int size;
  371. p = *str;
  372. if (*p == '0' && *(p + 1) == 'x')
  373. {
  374. p++;
  375. do
  376. {
  377. p++;
  378. }
  379. while (isxdigit (*p));
  380. }
  381. else
  382. {
  383. do
  384. {
  385. p++;
  386. }
  387. while (isdigit (*p));
  388. }
  389. size = p - *str;
  390. tmp = alloc (size + 1);
  391. strncpy (tmp, *str, size);
  392. tmp[size] = 0;
  393. *val = tmp;
  394. *str = p;
  395. }
  396. static const token symbols[] =
  397. {
  398. {TOK_CONST, "const"},
  399. {TOK_UNION, "union"},
  400. {TOK_SWITCH, "switch"},
  401. {TOK_CASE, "case"},
  402. {TOK_DEFAULT, "default"},
  403. {TOK_STRUCT, "struct"},
  404. {TOK_TYPEDEF, "typedef"},
  405. {TOK_ENUM, "enum"},
  406. {TOK_OPAQUE, "opaque"},
  407. {TOK_BOOL, "bool"},
  408. {TOK_VOID, "void"},
  409. {TOK_CHAR, "char"},
  410. {TOK_INT, "int"},
  411. {TOK_UNSIGNED, "unsigned"},
  412. {TOK_SHORT, "short"},
  413. {TOK_LONG, "long"},
  414. {TOK_HYPER, "hyper"},
  415. {TOK_FLOAT, "float"},
  416. {TOK_DOUBLE, "double"},
  417. {TOK_STRING, "string"},
  418. {TOK_PROGRAM, "program"},
  419. {TOK_VERSION, "version"},
  420. {TOK_EOF, "??????"},
  421. };
  422. static void
  423. findkind (const char **mark, token *tokp)
  424. {
  425. int len;
  426. const token *s;
  427. const char *str;
  428. char *tmp;
  429. str = *mark;
  430. for (s = symbols; s->kind != TOK_EOF; s++)
  431. {
  432. len = strlen (s->str);
  433. if (strncmp (str, s->str, len) == 0)
  434. {
  435. if (!isalnum (str[len]) && str[len] != '_')
  436. {
  437. tokp->kind = s->kind;
  438. tokp->str = s->str;
  439. *mark = str + len;
  440. return;
  441. }
  442. }
  443. }
  444. tokp->kind = TOK_IDENT;
  445. for (len = 0; isalnum (str[len]) || str[len] == '_'; len++);
  446. tmp = alloc (len + 1);
  447. strncpy (tmp, str, len);
  448. tmp[len] = 0;
  449. tokp->str = tmp;
  450. *mark = str + len;
  451. }
  452. static int
  453. cppline (const char *line)
  454. {
  455. return line == curline && *line == '#';
  456. }
  457. static int
  458. directive (const char *line)
  459. {
  460. return line == curline && *line == '%';
  461. }
  462. static void
  463. printdirective (const char *line)
  464. {
  465. f_print (fout, "%s", line + 1);
  466. }
  467. static void
  468. docppline (const char *line, int *lineno, const char **fname)
  469. {
  470. char *file;
  471. int num;
  472. char *p;
  473. line++;
  474. while (isspace (*line))
  475. {
  476. line++;
  477. }
  478. num = atoi (line);
  479. while (isdigit (*line))
  480. {
  481. line++;
  482. }
  483. while (isspace (*line))
  484. {
  485. line++;
  486. }
  487. if (*line != '"')
  488. {
  489. error (_("preprocessor error"));
  490. }
  491. line++;
  492. p = file = alloc (strlen (line) + 1);
  493. while (*line && *line != '"')
  494. {
  495. *p++ = *line++;
  496. }
  497. if (*line == 0)
  498. {
  499. error (_("preprocessor error"));
  500. }
  501. *p = 0;
  502. if (*file == 0)
  503. {
  504. free (file);
  505. *fname = NULL;
  506. }
  507. else
  508. {
  509. *fname = file;
  510. }
  511. *lineno = num - 1;
  512. }