home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp / src / devtools / rpcgen / rpc_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-09  |  9.6 KB  |  507 lines

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