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