rpc_util.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * From: @(#)rpc_util.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_util.c, Utility routines for the RPC protocol compiler
  34. */
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include "rpc_scan.h"
  40. #include "rpc_parse.h"
  41. #include "rpc_util.h"
  42. #include "proto.h"
  43. #define ARGEXT "argument"
  44. char curline[MAXLINESIZE]; /* current read line */
  45. const char *where = curline; /* current point in line */
  46. int linenum = 0; /* current line number */
  47. const char *infilename; /* input filename */
  48. #define NFILES 7
  49. const char *outfiles[NFILES]; /* output file names */
  50. int nfiles;
  51. FILE *fout; /* file pointer of current output */
  52. FILE *fin; /* file pointer of current input */
  53. list *defined; /* list of defined things */
  54. static int findit (const definition * def, const char *type);
  55. static const char *fixit (const char *type, const char *orig);
  56. static int typedefed (const definition * def, const char *type);
  57. static const char *toktostr (tok_kind kind);
  58. static void printbuf (void);
  59. static void printwhere (void);
  60. /*
  61. * Reinitialize the world
  62. */
  63. void
  64. reinitialize (void)
  65. {
  66. memset (curline, 0, MAXLINESIZE);
  67. where = curline;
  68. linenum = 0;
  69. defined = NULL;
  70. }
  71. /*
  72. * string equality
  73. */
  74. int
  75. streq (const char *a, const char *b)
  76. {
  77. return strcmp (a, b) == 0;
  78. }
  79. /*
  80. * find a value in a list
  81. */
  82. definition *
  83. findval (list *lst, const char *val,
  84. int (*cmp) (const definition *, const char *))
  85. {
  86. for (; lst != NULL; lst = lst->next)
  87. {
  88. if (cmp (lst->val, val))
  89. {
  90. return lst->val;
  91. }
  92. }
  93. return NULL;
  94. }
  95. /*
  96. * store a value in a list
  97. */
  98. void
  99. storeval (list **lstp, definition *val)
  100. {
  101. list **l;
  102. list *lst;
  103. for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
  104. lst = ALLOC (list);
  105. lst->val = val;
  106. lst->next = NULL;
  107. *l = lst;
  108. }
  109. static int
  110. findit (const definition * def, const char *type)
  111. {
  112. return streq (def->def_name, type);
  113. }
  114. static const char *
  115. fixit (const char *type, const char *orig)
  116. {
  117. definition *def;
  118. def = findval (defined, type, findit);
  119. if (def == NULL || def->def_kind != DEF_TYPEDEF)
  120. {
  121. return orig;
  122. }
  123. switch (def->def.ty.rel)
  124. {
  125. case REL_VECTOR:
  126. if (streq (def->def.ty.old_type, "opaque"))
  127. return ("char");
  128. else
  129. return (def->def.ty.old_type);
  130. case REL_ALIAS:
  131. return (fixit (def->def.ty.old_type, orig));
  132. default:
  133. return orig;
  134. }
  135. }
  136. const char *
  137. fixtype (const char *type)
  138. {
  139. return fixit (type, type);
  140. }
  141. const char *
  142. stringfix (const char *type)
  143. {
  144. if (streq (type, "string"))
  145. {
  146. return "wrapstring";
  147. }
  148. else
  149. {
  150. return type;
  151. }
  152. }
  153. void
  154. ptype (const char *prefix, const char *type, int follow)
  155. {
  156. if (prefix != NULL)
  157. {
  158. if (streq (prefix, "enum"))
  159. {
  160. f_print (fout, "enum ");
  161. }
  162. else
  163. {
  164. f_print (fout, "struct ");
  165. }
  166. }
  167. if (streq (type, "bool"))
  168. {
  169. f_print (fout, "bool_t ");
  170. }
  171. else if (streq (type, "string"))
  172. {
  173. f_print (fout, "char *");
  174. }
  175. else
  176. {
  177. f_print (fout, "%s ", follow ? fixtype (type) : type);
  178. }
  179. }
  180. static int
  181. typedefed (const definition * def, const char *type)
  182. {
  183. if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
  184. {
  185. return 0;
  186. }
  187. else
  188. {
  189. return streq (def->def_name, type);
  190. }
  191. }
  192. int
  193. isvectordef (const char *type, relation rel)
  194. {
  195. definition *def;
  196. for (;;)
  197. {
  198. switch (rel)
  199. {
  200. case REL_VECTOR:
  201. return !streq (type, "string");
  202. case REL_ARRAY:
  203. return 0;
  204. case REL_POINTER:
  205. return 0;
  206. case REL_ALIAS:
  207. def = findval (defined, type, typedefed);
  208. if (def == NULL)
  209. {
  210. return 0;
  211. }
  212. type = def->def.ty.old_type;
  213. rel = def->def.ty.rel;
  214. }
  215. }
  216. }
  217. char *
  218. locase (const char *str)
  219. {
  220. char c;
  221. static char buf[100];
  222. char *p = buf;
  223. while ((c = *str++) != 0)
  224. {
  225. *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
  226. }
  227. *p = 0;
  228. return buf;
  229. }
  230. void
  231. pvname_svc (const char *pname, const char *vnum)
  232. {
  233. f_print (fout, "%s_%s_svc", locase (pname), vnum);
  234. }
  235. void
  236. pvname (const char *pname, const char *vnum)
  237. {
  238. f_print (fout, "%s_%s", locase (pname), vnum);
  239. }
  240. /*
  241. * print a useful (?) error message, and then die
  242. */
  243. void
  244. error (const char *msg)
  245. {
  246. printwhere ();
  247. f_print (stderr, "%s, line %d: ", infilename, linenum);
  248. f_print (stderr, "%s\n", msg);
  249. crash ();
  250. }
  251. /*
  252. * Something went wrong, unlink any files that we may have created and then
  253. * die.
  254. */
  255. void
  256. crash (void)
  257. {
  258. int i;
  259. for (i = 0; i < nfiles; i++)
  260. {
  261. unlink (outfiles[i]);
  262. }
  263. exit (1);
  264. }
  265. void
  266. record_open (const char *file)
  267. {
  268. if (nfiles < NFILES)
  269. {
  270. outfiles[nfiles++] = file;
  271. }
  272. else
  273. {
  274. f_print (stderr, "too many files!\n");
  275. crash ();
  276. }
  277. }
  278. static char expectbuf[100];
  279. /*
  280. * error, token encountered was not the expected one
  281. */
  282. void
  283. expected1 (tok_kind exp1)
  284. {
  285. s_print (expectbuf, "expected '%s'",
  286. toktostr (exp1));
  287. error (expectbuf);
  288. }
  289. /*
  290. * error, token encountered was not one of two expected ones
  291. */
  292. void
  293. expected2 (tok_kind exp1, tok_kind exp2)
  294. {
  295. s_print (expectbuf, "expected '%s' or '%s'",
  296. toktostr (exp1),
  297. toktostr (exp2));
  298. error (expectbuf);
  299. }
  300. /*
  301. * error, token encountered was not one of 3 expected ones
  302. */
  303. void
  304. expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
  305. {
  306. s_print (expectbuf, "expected '%s', '%s' or '%s'",
  307. toktostr (exp1),
  308. toktostr (exp2),
  309. toktostr (exp3));
  310. error (expectbuf);
  311. }
  312. void
  313. tabify (FILE * f, int tab)
  314. {
  315. while (tab--)
  316. {
  317. (void) fputc ('\t', f);
  318. }
  319. }
  320. static const token tokstrings[] =
  321. {
  322. {TOK_IDENT, "identifier"},
  323. {TOK_CONST, "const"},
  324. {TOK_RPAREN, ")"},
  325. {TOK_LPAREN, "("},
  326. {TOK_RBRACE, "}"},
  327. {TOK_LBRACE, "{"},
  328. {TOK_LBRACKET, "["},
  329. {TOK_RBRACKET, "]"},
  330. {TOK_STAR, "*"},
  331. {TOK_COMMA, ","},
  332. {TOK_EQUAL, "="},
  333. {TOK_COLON, ":"},
  334. {TOK_SEMICOLON, ";"},
  335. {TOK_UNION, "union"},
  336. {TOK_STRUCT, "struct"},
  337. {TOK_SWITCH, "switch"},
  338. {TOK_CASE, "case"},
  339. {TOK_DEFAULT, "default"},
  340. {TOK_ENUM, "enum"},
  341. {TOK_TYPEDEF, "typedef"},
  342. {TOK_INT, "int"},
  343. {TOK_SHORT, "short"},
  344. {TOK_LONG, "long"},
  345. {TOK_UNSIGNED, "unsigned"},
  346. {TOK_DOUBLE, "double"},
  347. {TOK_FLOAT, "float"},
  348. {TOK_CHAR, "char"},
  349. {TOK_STRING, "string"},
  350. {TOK_OPAQUE, "opaque"},
  351. {TOK_BOOL, "bool"},
  352. {TOK_VOID, "void"},
  353. {TOK_PROGRAM, "program"},
  354. {TOK_VERSION, "version"},
  355. {TOK_EOF, "??????"}
  356. };
  357. static const char *
  358. toktostr (tok_kind kind)
  359. {
  360. const token *sp;
  361. for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
  362. return sp->str;
  363. }
  364. static void
  365. printbuf (void)
  366. {
  367. char c;
  368. int i;
  369. int cnt;
  370. #define TABSIZE 4
  371. for (i = 0; (c = curline[i]) != 0; i++)
  372. {
  373. if (c == '\t')
  374. {
  375. cnt = 8 - (i % TABSIZE);
  376. c = ' ';
  377. }
  378. else
  379. {
  380. cnt = 1;
  381. }
  382. while (cnt--)
  383. {
  384. (void) fputc (c, stderr);
  385. }
  386. }
  387. }
  388. static void
  389. printwhere (void)
  390. {
  391. int i;
  392. char c;
  393. int cnt;
  394. printbuf ();
  395. for (i = 0; i < where - curline; i++)
  396. {
  397. c = curline[i];
  398. if (c == '\t')
  399. {
  400. cnt = 8 - (i % TABSIZE);
  401. }
  402. else
  403. {
  404. cnt = 1;
  405. }
  406. while (cnt--)
  407. {
  408. (void) fputc ('^', stderr);
  409. }
  410. }
  411. (void) fputc ('\n', stderr);
  412. }
  413. char *
  414. make_argname (const char *pname, const char *vname)
  415. {
  416. char *name;
  417. name = malloc (strlen (pname) + strlen (vname) + strlen (ARGEXT) + 3);
  418. if (!name)
  419. {
  420. fprintf (stderr, "failed in malloc");
  421. exit (1);
  422. }
  423. sprintf (name, "%s_%s_%s", locase (pname), vname, ARGEXT);
  424. return name;
  425. }
  426. bas_type *typ_list_h;
  427. bas_type *typ_list_t;
  428. void
  429. add_type (int len, const char *type)
  430. {
  431. bas_type *ptr;
  432. if ((ptr = malloc (sizeof (bas_type))) == NULL)
  433. {
  434. fprintf (stderr, "failed in malloc");
  435. exit (1);
  436. }
  437. ptr->name = type;
  438. ptr->length = len;
  439. ptr->next = NULL;
  440. if (typ_list_t == NULL)
  441. {
  442. typ_list_t = ptr;
  443. typ_list_h = ptr;
  444. }
  445. else
  446. {
  447. typ_list_t->next = ptr;
  448. typ_list_t = ptr;
  449. }
  450. }
  451. bas_type *
  452. find_type (const char *type)
  453. {
  454. bas_type *ptr;
  455. ptr = typ_list_h;
  456. while (ptr != NULL)
  457. {
  458. if (strcmp (ptr->name, type) == 0)
  459. return ptr;
  460. else
  461. ptr = ptr->next;
  462. };
  463. return NULL;
  464. }