home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / rpc2 / part11 / rpc / rpcgen / rpc_util.c next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.0 KB  |  256 lines

  1. #ifndef lint 
  2. static char sccsid[] = "@(#)rpc_util.c 1.1 86/03/26 (C) 1986 SMI";
  3. #endif
  4.  
  5. /*
  6.  * rpc_util.c, Utility routines for the RPC protocol compiler
  7.  * Copyright (C) 1986, Sun Microsystems, Inc.
  8.  */
  9. #include <stdio.h>
  10. #include "rpc_scan.h"
  11. #include "rpc_util.h"
  12.  
  13. char curline[MAXLINESIZE];        /* current read line */
  14. char *where = curline;            /* current point in line */
  15. int linenum = 0;                /* current line number */
  16.  
  17. char *infile;    /* input filename */
  18. char *outfile;  /* output filename */
  19. char *outfile2; /* possible other output filename */
  20.  
  21. FILE *fout;        /* file pointer of current output */
  22. FILE *fin;        /* file pointer of current input */
  23.  
  24. list *printed;    /* list of printed routines */
  25. list *defined;  /* list of defined things */
  26.  
  27. /*
  28.  * Reinitialize the world
  29.  */
  30. reinitialize()
  31. {
  32.     bzero(curline,MAXLINESIZE);
  33.     where = curline;
  34.     linenum = 0;
  35.     printed = NULL;
  36.     defined = NULL;
  37. }
  38.  
  39. /*
  40.  * string equality
  41.  */
  42. streq(a,b)
  43.     char *a;
  44.     char *b;
  45. {
  46.     return(strcmp(a,b) == 0);
  47. }
  48.  
  49. /*
  50.  * find a value in a list
  51.  */
  52. char *
  53. findval(lst,val,cmp)
  54.     list *lst;
  55.     char *val;
  56.     int (*cmp)();
  57. {
  58.     for (; lst != NULL; lst = lst->next) {
  59.         if ((*cmp)(lst->val,val)) {
  60.             return(lst->val);
  61.         }
  62.     }    
  63.     return(NULL);
  64. }
  65.  
  66. /*
  67.  * store a value in a list
  68.  */
  69. void
  70. storeval(lstp,val)
  71.     list **lstp;
  72.     char *val;
  73. {
  74.     list **l;
  75.     list *lst;
  76.  
  77.     for (l = lstp; *l != NULL; l = (list **) &(*l)->next) 
  78.         ;
  79.     lst = ALLOC(list);
  80.     lst->val = val;
  81.     lst->next = NULL;
  82.     *l = lst;
  83. }
  84.  
  85.  
  86. /*
  87.  * print a useful (?) error message, and then die
  88.  */
  89. void
  90. error(msg)
  91.     char *msg;
  92. {
  93.     extern char *outfile;
  94.  
  95.     printwhere();
  96.     fprintf(stderr,"%s, line %d: ",infile ? infile : "<stdin>", linenum);
  97.     fprintf(stderr,"%s\n",msg);
  98.     crash();
  99. }
  100.  
  101. /*
  102.  * Something went wrong, unlink any files
  103.  * that we may have created and then die.
  104.  */
  105. crash()
  106. {
  107.     if (outfile) {
  108.         unlink(outfile);
  109.     }    
  110.     if (outfile2) {
  111.         unlink(outfile2);
  112.     }
  113.     exit(1);
  114. }
  115.  
  116.  
  117.  
  118. static char expectbuf[100];
  119. static char *toktostr();
  120.  
  121. /*
  122.  * error, token encountered was not the expected one
  123.  */
  124. void
  125. expected1(exp1)
  126.     tok_kind exp1;
  127. {
  128.     sprintf(expectbuf,"expected '%s'",
  129.         toktostr(exp1));
  130.     error(expectbuf);
  131. }
  132.  
  133. /*
  134.  * error, token encountered was not one of two expected ones
  135.  */
  136. void
  137. expected2(exp1,exp2)
  138.     tok_kind exp1,exp2;
  139. {
  140.     sprintf(expectbuf,"expected '%s' or '%s'",
  141.         toktostr(exp1),
  142.         toktostr(exp2));
  143.     error(expectbuf);
  144. }
  145.  
  146. /*
  147.  * error, token encountered was not one of 3 expected ones
  148.  */
  149. void
  150. expected3(exp1,exp2,exp3)
  151.     tok_kind exp1,exp2,exp3;
  152. {
  153.     sprintf(expectbuf,"expected '%s', '%s' or '%s'",
  154.         toktostr(exp1),
  155.         toktostr(exp2),
  156.         toktostr(exp3));
  157.     error(expectbuf);
  158. }
  159.  
  160.  
  161.  
  162. static token tokstrings[] = {
  163.     { TOK_IDENT, "identifier" },
  164.     { TOK_CONST, "constant" },
  165.     { TOK_RPAREN, ")" },
  166.     { TOK_LPAREN, "(" },
  167.     { TOK_RBRACE, "}" },
  168.     { TOK_LBRACE, "{" },
  169.     { TOK_LBRACKET, "[" },
  170.     { TOK_RBRACKET, "]" },
  171.     { TOK_STAR, "*" },
  172.     { TOK_COMMA, "," },
  173.     { TOK_EQUAL, "=" },
  174.     { TOK_COLON, ":" },
  175.     { TOK_SEMICOLON, ";" },
  176.     { TOK_UNION, "union" },
  177.     { TOK_STRUCT, "struct" },
  178.     { TOK_SWITCH, "switch" },
  179.     { TOK_CASE,    "case" },
  180.     { TOK_DEFAULT, "default" },
  181.     { TOK_ENUM, "enum" },
  182.     { TOK_ARRAY, "array" },
  183.     { TOK_TYPEDEF, "typedef" },
  184.     { TOK_INT, "int" },
  185.     { TOK_SHORT, "short" },
  186.     { TOK_LONG, "long" },
  187.     { TOK_UNSIGNED, "unsigned" },
  188.     { TOK_DOUBLE, "double" },
  189.     { TOK_FLOAT, "float" },
  190.     { TOK_CHAR, "char" },
  191.     { TOK_STRING, "string" },
  192.     { TOK_OPAQUE, "opaque" },
  193.     { TOK_BOOL, "bool" },
  194.     { TOK_VOID, "void" },
  195.     { TOK_PROGRAM, "program" },
  196.     { TOK_VERSION, "version" },
  197.     { TOK_EOF, "??????" }
  198. };
  199.  
  200. static char * 
  201. toktostr(kind) 
  202.     tok_kind kind; 
  203.     token *sp;
  204.  
  205.     for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++)
  206.         ; 
  207.     return(sp->str); 
  208. }
  209.  
  210.  
  211. static 
  212. printbuf()
  213. {
  214.     char c;
  215.     int i;
  216.     int cnt;
  217.  
  218. #    define TABSIZE 4
  219.  
  220.     for (i = 0; c = curline[i]; i++) {
  221.         if (c == '\t') {
  222.             cnt = 8 - (i % TABSIZE);
  223.             c = ' ';
  224.         } else {
  225.             cnt = 1;
  226.         }
  227.         while (cnt--) {    
  228.             putc(c,stderr);
  229.         } 
  230.     }
  231. }
  232.  
  233.  
  234. static
  235. printwhere()
  236. {
  237.     int i;
  238.     char c;
  239.     int cnt;
  240.  
  241.     printbuf();
  242.     for (i = 0; i < where - curline; i++) {
  243.         c = curline[i];
  244.         if (c == '\t') {
  245.             cnt = 8 - (i % TABSIZE);
  246.         } else {
  247.             cnt = 1;
  248.         }
  249.         while (cnt--) {
  250.             putc('^',stderr);
  251.         }
  252.     }
  253.     putc('\n',stderr);
  254. }
  255.