home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SATAN11.ZIP / SRC / RPCGEN / RPC_UTIL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-08  |  8.1 KB  |  441 lines

  1. /* @(#)rpc_util.c    2.1 88/08/01 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #ifndef lint
  31. static char sccsid[] = "@(#)rpc_util.c 1.5 87/06/24 (C) 1987 SMI";
  32. #endif
  33.  
  34. /*
  35.  * rpc_util.c, Utility routines for the RPC protocol compiler 
  36.  * Copyright (C) 1987, Sun Microsystems, Inc. 
  37.  */
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <stdio.h>
  41. #include "rpc_scan.h"
  42. #include "rpc_parse.h"
  43. #include "rpc_util.h"
  44.  
  45. char curline[MAXLINESIZE];    /* current read line */
  46. char *where = curline;    /* current point in line */
  47. int linenum = 0;    /* current line number */
  48.  
  49. char *infilename;    /* input filename */
  50.  
  51. #define NFILES 4
  52. char *outfiles[NFILES];    /* output file names */
  53. int nfiles;
  54.  
  55. FILE *fout;    /* file pointer of current output */
  56. FILE *fin;    /* file pointer of current input */
  57.  
  58. list *defined;    /* list of defined things */
  59.  
  60. static printwhere();
  61.  
  62. /*
  63.  * Reinitialize the world 
  64.  */
  65. reinitialize()
  66. {
  67.     memset(curline, 0, MAXLINESIZE);
  68.     where = curline;
  69.     linenum = 0;
  70.     defined = NULL;
  71. }
  72.  
  73. /*
  74.  * string equality 
  75.  */
  76. streq(a, b)
  77.     char *a;
  78.     char *b;
  79. {
  80.     return (strcmp(a, b) == 0);
  81. }
  82.  
  83. /*
  84.  * find a value in a list 
  85.  */
  86. char *
  87. findval(lst, val, cmp)
  88.     list *lst;
  89.     char *val;
  90.     int (*cmp) ();
  91.  
  92. {
  93.     for (; lst != NULL; lst = lst->next) {
  94.         if ((*cmp) (lst->val, val)) {
  95.             return (lst->val);
  96.         }
  97.     }
  98.     return (NULL);
  99. }
  100.  
  101. /*
  102.  * store a value in a list 
  103.  */
  104. void
  105. storeval(lstp, val)
  106.     list **lstp;
  107.     char *val;
  108. {
  109.     list **l;
  110.     list *lst;
  111.  
  112.     for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
  113.     lst = ALLOC(list);
  114.     lst->val = val;
  115.     lst->next = NULL;
  116.     *l = lst;
  117. }
  118.  
  119.  
  120. static
  121. findit(def, type)
  122.     definition *def;
  123.     char *type;
  124. {
  125.     return (streq(def->def_name, type));
  126. }
  127.  
  128.  
  129. static char *
  130. fixit(type, orig)
  131.     char *type;
  132.     char *orig;
  133. {
  134.     definition *def;
  135.  
  136.     def = (definition *) FINDVAL(defined, type, findit);
  137.     if (def == NULL || def->def_kind != DEF_TYPEDEF) {
  138.         return (orig);
  139.     }
  140.     switch (def->def.ty.rel) {
  141.     case REL_VECTOR:
  142.         return (def->def.ty.old_type);
  143.     case REL_ALIAS:
  144.         return (fixit(def->def.ty.old_type, orig));
  145.     default:
  146.         return (orig);
  147.     }
  148. }
  149.  
  150. char *
  151. fixtype(type)
  152.     char *type;
  153. {
  154.     return (fixit(type, type));
  155. }
  156.  
  157. char *
  158. stringfix(type)
  159.     char *type;
  160. {
  161.     if (streq(type, "string")) {
  162.         return ("wrapstring");
  163.     } else {
  164.         return (type);
  165.     }
  166. }
  167.  
  168. void
  169. ptype(prefix, type, follow)
  170.     char *prefix;
  171.     char *type;
  172.     int follow;
  173. {
  174.     if (prefix != NULL) {
  175.         if (streq(prefix, "enum")) {
  176.             f_print(fout, "enum ");
  177.         } else {
  178.             f_print(fout, "struct ");
  179.         }
  180.     }
  181.     if (streq(type, "bool")) {
  182.         f_print(fout, "bool_t ");
  183.     } else if (streq(type, "string")) {
  184.         f_print(fout, "char *");
  185.     } else {
  186.         f_print(fout, "%s ", follow ? fixtype(type) : type);
  187.     }
  188. }
  189.  
  190.  
  191. static
  192. typedefed(def, type)
  193.     definition *def;
  194.     char *type;
  195. {
  196.     if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
  197.         return (0);
  198.     } else {
  199.         return (streq(def->def_name, type));
  200.     }
  201. }
  202.  
  203. isvectordef(type, rel)
  204.     char *type;
  205.     relation rel;
  206. {
  207.     definition *def;
  208.  
  209.     for (;;) {
  210.         switch (rel) {
  211.         case REL_VECTOR:
  212.             return (!streq(type, "string"));
  213.         case REL_ARRAY:
  214.             return (0);
  215.         case REL_POINTER:
  216.             return (0);
  217.         case REL_ALIAS:
  218.             def = (definition *) FINDVAL(defined, type, typedefed);
  219.             if (def == NULL) {
  220.                 return (0);
  221.             }
  222.             type = def->def.ty.old_type;
  223.             rel = def->def.ty.rel;
  224.         }
  225.     }
  226. }
  227.  
  228.  
  229. static char *
  230. locase(str)
  231.     char *str;
  232. {
  233.     char c;
  234.     static char buf[100];
  235.     char *p = buf;
  236.  
  237.     while (c = *str++) {
  238.         *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
  239.     }
  240.     *p = 0;
  241.     return (buf);
  242. }
  243.  
  244.  
  245. void
  246. pvname(pname, vnum)
  247.     char *pname;
  248.     char *vnum;
  249. {
  250.     f_print(fout, "%s_%s", locase(pname), vnum);
  251. }
  252.  
  253.  
  254. /*
  255.  * print a useful (?) error message, and then die 
  256.  */
  257. void
  258. error(msg)
  259.     char *msg;
  260. {
  261.     printwhere();
  262.     f_print(stderr, "%s, line %d: ", infilename, linenum);
  263.     f_print(stderr, "%s\n", msg);
  264.     crash();
  265. }
  266.  
  267. /*
  268.  * Something went wrong, unlink any files that we may have created and then
  269.  * die. 
  270.  */
  271. crash()
  272. {
  273.     int i;
  274.  
  275.     for (i = 0; i < nfiles; i++) {
  276.         (void) unlink(outfiles[i]);
  277.     }
  278.     exit(1);
  279. }
  280.  
  281.  
  282. void
  283. record_open(file)
  284.     char *file;
  285. {
  286.     if (nfiles < NFILES) {
  287.         outfiles[nfiles++] = file;
  288.     } else {
  289.         f_print(stderr, "too many files!\n");
  290.         crash();
  291.     }
  292. }
  293.  
  294. static char expectbuf[100];
  295. static char *toktostr();
  296.  
  297. /*
  298.  * error, token encountered was not the expected one 
  299.  */
  300. void
  301. expected1(exp1)
  302.     tok_kind exp1;
  303. {
  304.     s_print(expectbuf, "expected '%s'",
  305.         toktostr(exp1));
  306.     error(expectbuf);
  307. }
  308.  
  309. /*
  310.  * error, token encountered was not one of two expected ones 
  311.  */
  312. void
  313. expected2(exp1, exp2)
  314.     tok_kind exp1, exp2;
  315. {
  316.     s_print(expectbuf, "expected '%s' or '%s'",
  317.         toktostr(exp1),
  318.         toktostr(exp2));
  319.     error(expectbuf);
  320. }
  321.  
  322. /*
  323.  * error, token encountered was not one of 3 expected ones 
  324.  */
  325. void
  326. expected3(exp1, exp2, exp3)
  327.     tok_kind exp1, exp2, exp3;
  328. {
  329.     s_print(expectbuf, "expected '%s', '%s' or '%s'",
  330.         toktostr(exp1),
  331.         toktostr(exp2),
  332.         toktostr(exp3));
  333.     error(expectbuf);
  334. }
  335.  
  336. void
  337. tabify(f, tab)
  338.     FILE *f;
  339.     int tab;
  340. {
  341.     while (tab--) {
  342.         (void) fputc('\t', f);
  343.     }
  344. }
  345.  
  346.  
  347.  
  348. static token tokstrings[] = {
  349.                  {TOK_IDENT, "identifier"},
  350.                  {TOK_CONST, "const"},
  351.                  {TOK_RPAREN, ")"},
  352.                  {TOK_LPAREN, "("},
  353.                  {TOK_RBRACE, "}"},
  354.                  {TOK_LBRACE, "{"},
  355.                  {TOK_LBRACKET, "["},
  356.                  {TOK_RBRACKET, "]"},
  357.                  {TOK_STAR, "*"},
  358.                  {TOK_COMMA, ","},
  359.                  {TOK_EQUAL, "="},
  360.                  {TOK_COLON, ":"},
  361.                  {TOK_SEMICOLON, ";"},
  362.                  {TOK_UNION, "union"},
  363.                  {TOK_STRUCT, "struct"},
  364.                  {TOK_SWITCH, "switch"},
  365.                  {TOK_CASE, "case"},
  366.                  {TOK_DEFAULT, "default"},
  367.                  {TOK_ENUM, "enum"},
  368.                  {TOK_TYPEDEF, "typedef"},
  369.                  {TOK_INT, "int"},
  370.                  {TOK_SHORT, "short"},
  371.                  {TOK_LONG, "long"},
  372.                  {TOK_UNSIGNED, "unsigned"},
  373.                  {TOK_DOUBLE, "double"},
  374.                  {TOK_FLOAT, "float"},
  375.                  {TOK_CHAR, "char"},
  376.                  {TOK_STRING, "string"},
  377.                  {TOK_OPAQUE, "opaque"},
  378.                  {TOK_BOOL, "bool"},
  379.                  {TOK_VOID, "void"},
  380.                  {TOK_PROGRAM, "program"},
  381.                  {TOK_VERSION, "version"},
  382.                  {TOK_EOF, "??????"}
  383. };
  384.  
  385. static char *
  386. toktostr(kind)
  387.     tok_kind kind;
  388. {
  389.     token *sp;
  390.  
  391.     for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
  392.     return (sp->str);
  393. }
  394.  
  395.  
  396.  
  397. static
  398. printbuf()
  399. {
  400.     char c;
  401.     int i;
  402.     int cnt;
  403.  
  404. #    define TABSIZE 4
  405.  
  406.     for (i = 0; c = curline[i]; i++) {
  407.         if (c == '\t') {
  408.             cnt = 8 - (i % TABSIZE);
  409.             c = ' ';
  410.         } else {
  411.             cnt = 1;
  412.         }
  413.         while (cnt--) {
  414.             (void) fputc(c, stderr);
  415.         }
  416.     }
  417. }
  418.  
  419.  
  420. static
  421. printwhere()
  422. {
  423.     int i;
  424.     char c;
  425.     int cnt;
  426.  
  427.     printbuf();
  428.     for (i = 0; i < where - curline; i++) {
  429.         c = curline[i];
  430.         if (c == '\t') {
  431.             cnt = 8 - (i % TABSIZE);
  432.         } else {
  433.             cnt = 1;
  434.         }
  435.         while (cnt--) {
  436.             (void) fputc('^', stderr);
  437.         }
  438.     }
  439.     (void) fputc('\n', stderr);
  440. }
  441.