rxgen: Always pass aliases (typedefs) as pointers
[openafs.git] / src / rxgen / rpc_util.c
1 /* @(#)rpc_util.c       1.2 87/11/24 3.9 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
31 /*
32  * rpc_util.c, Utility routines for the RPC protocol compiler
33  * Copyright (C) 1987, Sun Microsystems, Inc.
34  */
35 #include <afsconfig.h>
36 #include <afs/param.h>
37
38 #include <roken.h>
39
40 #include "rpc_scan.h"
41 #include "rpc_parse.h"
42 #include "rpc_util.h"
43
44 char curline[MAXLINESIZE];      /* current read line */
45 char *where = curline;          /* current point in line */
46 int linenum = 0;                /* current line number */
47
48 char *infilename;               /* input filename */
49
50 #define NFILES 6
51 char *outfiles[NFILES];         /* output file names */
52 int nfiles;
53
54 FILE *fout;                     /* file pointer of current output */
55 FILE *fin;                      /* file pointer of current input */
56
57 list *defined;                  /* list of defined things */
58
59 /* static prototypes */
60 static int findit(definition * def, char *type);
61 static char *fixit(char *type, char *orig);
62 static char *locase(char *str);
63 static char *toktostr(tok_kind kind);
64 static void printbuf(void);
65 static void printwhere(void);
66
67
68 /*
69  * Reinitialize the world
70  */
71 void
72 reinitialize(void)
73 {
74     int i;
75     memset(curline, 0, MAXLINESIZE);
76     where = curline;
77     linenum = 0;
78     defined = NULL;
79     special_defined = typedef_defined = uniondef_defined = NULL;
80     PackageIndex = -1;
81     master_opcodenumber = 99999;
82     master_highest_opcode = 0;
83     no_of_stat_funcs = 0;
84     for (i = 0; i < MAX_PACKAGES; i++) {
85         no_of_stat_funcs_header[i] = 0;
86     }
87 }
88
89 /*
90  * string equality
91  */
92 int
93 streq(char *a, char *b)
94 {
95     return (strcmp(a, b) == 0);
96 }
97
98 /*
99  * find a value in a list
100  */
101 char *
102 findval(list * lst, char *val, int (*cmp) (definition * def, char *type))
103 {
104     for (; lst != NULL; lst = lst->next) {
105         if ((*cmp) ((definition *) lst->val, val)) {
106             return (lst->val);
107         }
108     }
109     return (NULL);
110 }
111
112 /*
113  * store a value in a list
114  */
115 void
116 storeval(list ** lstp, char *val)
117 {
118     list **l;
119     list *lst;
120
121     for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
122     lst = ALLOC(list);
123     lst->val = val;
124     lst->next = NULL;
125     *l = lst;
126 }
127
128
129 static int
130 findit(definition * def, char *type)
131 {
132     return (streq(def->def_name, type));
133 }
134
135
136 static char *
137 fixit(char *type, char *orig)
138 {
139     definition *def;
140
141     def = (definition *) FINDVAL(defined, type, findit);
142     if (def == NULL || def->def_kind != DEF_TYPEDEF) {
143         return (orig);
144     }
145     switch (def->def.ty.rel) {
146     case REL_VECTOR:
147         return (def->def.ty.old_type);
148     case REL_ALIAS:
149         return (fixit(def->def.ty.old_type, orig));
150     default:
151         return (orig);
152     }
153 }
154
155 char *
156 fixtype(char *type)
157 {
158     return (fixit(type, type));
159 }
160
161 char *
162 stringfix(char *type)
163 {
164     if (streq(type, "string")) {
165         return ("wrapstring");
166     } else {
167         return (type);
168     }
169 }
170
171 void
172 ptype(char *prefix, char *type, 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 int
192 isvectordef(char *type, relation rel)
193 {
194     for (;;) {
195         switch (rel) {
196         case REL_VECTOR:
197             return (!streq(type, "string"));
198         case REL_ARRAY:
199             return (0);
200         case REL_POINTER:
201             return (0);
202         case REL_ALIAS:
203             return (0);
204         }
205     }
206 }
207
208
209 static char *
210 locase(char *str)
211 {
212     char c;
213     static char buf[100];
214     char *p = buf;
215
216     while ((c = *str++)) {
217         *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
218     }
219     *p = 0;
220     return (buf);
221 }
222
223
224 void
225 pvname(char *pname, char *vnum)
226 {
227     f_print(fout, "%s_%s", locase(pname), vnum);
228 }
229
230
231 /*
232  * print a useful (?) error message, and then die
233  */
234 void
235 error(char *msg)
236 {
237     printwhere();
238     f_print(stderr, "%s, line %d: ", infilename, linenum);
239     f_print(stderr, "%s\n", msg);
240     crash();
241 }
242
243 /*
244  * Something went wrong, unlink any files that we may have created and then
245  * die.
246  */
247 void
248 crash(void)
249 {
250     int i;
251
252     for (i = 0; i < nfiles; i++) {
253         (void)unlink(outfiles[i]);
254     }
255     exit(1);
256 }
257
258
259 void
260 record_open(char *file)
261 {
262     if (nfiles < NFILES) {
263         outfiles[nfiles++] = file;
264     } else {
265         f_print(stderr, "too many files!\n");
266         crash();
267     }
268 }
269
270 /* buffer shared for all of the expected* routines */
271 static char expectbuf[100];
272
273 /*
274  * error, token encountered was not the expected one
275  */
276 void
277 expected1(tok_kind exp1)
278 {
279     s_print(expectbuf, "expected '%s'", toktostr(exp1));
280     error(expectbuf);
281 }
282
283 /*
284  * error, token encountered was not one of two expected ones
285  */
286 void
287 expected2(tok_kind exp1, tok_kind exp2)
288 {
289     s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1),
290             toktostr(exp2));
291     error(expectbuf);
292 }
293
294 /*
295  * error, token encountered was not one of 3 expected ones
296  */
297 void
298 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
299 {
300     s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1),
301             toktostr(exp2), toktostr(exp3));
302     error(expectbuf);
303 }
304
305
306 /*
307  * error, token encountered was not one of 4 expected ones
308  */
309 void
310 expected4(tok_kind exp1, tok_kind exp2, tok_kind exp3, tok_kind exp4)
311 {
312     sprintf(expectbuf, "expected '%s', '%s', '%s', or '%s'", toktostr(exp1),
313             toktostr(exp2), toktostr(exp3), toktostr(exp4));
314     error(expectbuf);
315 }
316
317 void
318 tabify(FILE * f, int tab)
319 {
320     if (scan_print)
321         while (tab--) {
322             (void)fputc('\t', f);
323         }
324 }
325
326 static token tokstrings[] = {
327     {TOK_IDENT, "identifier"},
328     {TOK_CONST, "const"},
329     {TOK_RPAREN, ")"},
330     {TOK_LPAREN, "("},
331     {TOK_RBRACE, "}"},
332     {TOK_LBRACE, "{"},
333     {TOK_LBRACKET, "["},
334     {TOK_RBRACKET, "]"},
335     {TOK_STAR, "*"},
336     {TOK_COMMA, ","},
337     {TOK_EQUAL, "="},
338     {TOK_COLON, ":"},
339     {TOK_SEMICOLON, ";"},
340     {TOK_UNION, "union"},
341     {TOK_STRUCT, "struct"},
342     {TOK_SWITCH, "switch"},
343     {TOK_CASE, "case"},
344     {TOK_DEFAULT, "default"},
345     {TOK_ENUM, "enum"},
346     {TOK_TYPEDEF, "typedef"},
347     {TOK_INT, "int"},
348     {TOK_SHORT, "short"},
349     {TOK_INT32, "afs_int32"},   /* XXX */
350     {TOK_UNSIGNED, "unsigned"},
351     {TOK_DOUBLE, "double"},
352     {TOK_FLOAT, "float"},
353     {TOK_CHAR, "char"},
354     {TOK_STRING, "string"},
355     {TOK_OPAQUE, "opaque"},
356     {TOK_BOOL, "bool"},
357     {TOK_VOID, "void"},
358     {TOK_PACKAGE, "package"},
359     {TOK_PREFIX, "prefix"},
360     {TOK_STATINDEX, "statindex"},
361     {TOK_SPECIAL, "special"},
362     {TOK_STARTINGOPCODE, "startingopcode"},
363     {TOK_CUSTOMIZED, "customized"},
364     {TOK_PROC, "proc"},
365     {TOK_SPLITPREFIX, "splitprefix"},
366     {TOK_SPLIT, "split"},
367     {TOK_MULTI, "multi"},
368     {TOK_IN, "IN"},
369     {TOK_OUT, "OUT"},
370     {TOK_INOUT, "INOUT"},
371     {TOK_AFSUUID, "afsUUID"},
372     {TOK_EOF, "??????"}
373 };
374
375 static char *
376 toktostr(tok_kind kind)
377 {
378     token *sp;
379
380     for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
381     return (sp->str);
382 }
383
384
385
386 static void
387 printbuf(void)
388 {
389     char c;
390     int i;
391     int cnt;
392
393 #       define TABSIZE 4
394
395     for (i = 0; (c = curline[i]); i++) {
396         if (c == '\t') {
397             cnt = 8 - (i % TABSIZE);
398             c = ' ';
399         } else {
400             cnt = 1;
401         }
402         while (cnt--) {
403             fputc(c, stderr);
404         }
405     }
406 }
407
408
409 static void
410 printwhere(void)
411 {
412     int i;
413     char c;
414     int cnt;
415
416     printbuf();
417     for (i = 0; i < where - curline; i++) {
418         c = curline[i];
419         if (c == '\t') {
420             cnt = 8 - (i % TABSIZE);
421         } else {
422             cnt = 1;
423         }
424         while (cnt--) {
425             fputc('^', stderr);
426         }
427     }
428     fputc('\n', stderr);
429 }