1 /* @(#)rpc_util.c 1.2 87/11/24 3.9 RPCSRC */
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * rpc_util.c, Utility routines for the RPC protocol compiler
33 * Copyright (C) 1987, Sun Microsystems, Inc.
35 #include <afsconfig.h>
36 #include <afs/param.h>
50 #include "rpc_parse.h"
53 char curline[MAXLINESIZE]; /* current read line */
54 char *where = curline; /* current point in line */
55 int linenum = 0; /* current line number */
57 char *infilename; /* input filename */
60 char *outfiles[NFILES]; /* output file names */
63 FILE *fout; /* file pointer of current output */
64 FILE *fin; /* file pointer of current input */
66 list *defined; /* list of defined things */
68 /* static prototypes */
69 static int findit(definition * def, char *type);
70 static char *fixit(char *type, char *orig);
71 static int typedefed(definition * def, char *type);
72 static char *locase(char *str);
73 static char *toktostr(tok_kind kind);
74 static void printbuf(void);
75 static void printwhere(void);
79 * Reinitialize the world
85 memset(curline, 0, MAXLINESIZE);
89 special_defined = typedef_defined = uniondef_defined = NULL;
91 master_opcodenumber = 99999;
92 master_highest_opcode = 0;
94 for (i = 0; i < MAX_PACKAGES; i++) {
95 no_of_stat_funcs_header[i] = 0;
103 streq(char *a, char *b)
105 return (strcmp(a, b) == 0);
109 * find a value in a list
112 findval(list * lst, char *val, int (*cmp) (definition * def, char *type))
114 for (; lst != NULL; lst = lst->next) {
115 if ((*cmp) ((definition *) lst->val, val)) {
123 * store a value in a list
126 storeval(list ** lstp, char *val)
131 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
140 findit(definition * def, char *type)
142 return (streq(def->def_name, type));
147 fixit(char *type, char *orig)
151 def = (definition *) FINDVAL(defined, type, findit);
152 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
155 switch (def->def.ty.rel) {
157 return (def->def.ty.old_type);
159 return (fixit(def->def.ty.old_type, orig));
168 return (fixit(type, type));
172 stringfix(char *type)
174 if (streq(type, "string")) {
175 return ("wrapstring");
182 ptype(char *prefix, char *type, int follow)
184 if (prefix != NULL) {
185 if (streq(prefix, "enum")) {
186 f_print(fout, "enum ");
188 f_print(fout, "struct ");
191 if (streq(type, "bool")) {
192 f_print(fout, "bool_t ");
193 } else if (streq(type, "string")) {
194 f_print(fout, "char *");
196 f_print(fout, "%s ", follow ? fixtype(type) : type);
202 typedefed(definition * def, char *type)
204 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
207 return (streq(def->def_name, type));
212 isvectordef(char *type, relation rel)
219 return (!streq(type, "string"));
225 def = (definition *) FINDVAL(defined, type, typedefed);
229 type = def->def.ty.old_type;
230 rel = def->def.ty.rel;
240 static char buf[100];
243 while ((c = *str++)) {
244 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
252 pvname(char *pname, char *vnum)
254 f_print(fout, "%s_%s", locase(pname), vnum);
259 * print a useful (?) error message, and then die
265 f_print(stderr, "%s, line %d: ", infilename, linenum);
266 f_print(stderr, "%s\n", msg);
271 * Something went wrong, unlink any files that we may have created and then
279 for (i = 0; i < nfiles; i++) {
280 (void)unlink(outfiles[i]);
287 record_open(char *file)
289 if (nfiles < NFILES) {
290 outfiles[nfiles++] = file;
292 f_print(stderr, "too many files!\n");
297 /* buffer shared for all of the expected* routines */
298 static char expectbuf[100];
301 * error, token encountered was not the expected one
304 expected1(tok_kind exp1)
306 s_print(expectbuf, "expected '%s'", toktostr(exp1));
311 * error, token encountered was not one of two expected ones
314 expected2(tok_kind exp1, tok_kind exp2)
316 s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1),
322 * error, token encountered was not one of 3 expected ones
325 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
327 s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1),
328 toktostr(exp2), toktostr(exp3));
334 * error, token encountered was not one of 4 expected ones
337 expected4(tok_kind exp1, tok_kind exp2, tok_kind exp3, tok_kind exp4)
339 sprintf(expectbuf, "expected '%s', '%s', '%s', or '%s'", toktostr(exp1),
340 toktostr(exp2), toktostr(exp3), toktostr(exp4));
345 tabify(FILE * f, int tab)
349 (void)fputc('\t', f);
353 static token tokstrings[] = {
354 {TOK_IDENT, "identifier"},
355 {TOK_CONST, "const"},
366 {TOK_SEMICOLON, ";"},
367 {TOK_UNION, "union"},
368 {TOK_STRUCT, "struct"},
369 {TOK_SWITCH, "switch"},
371 {TOK_DEFAULT, "default"},
373 {TOK_TYPEDEF, "typedef"},
375 {TOK_SHORT, "short"},
376 {TOK_INT32, "afs_int32"}, /* XXX */
377 {TOK_UNSIGNED, "unsigned"},
378 {TOK_DOUBLE, "double"},
379 {TOK_FLOAT, "float"},
381 {TOK_STRING, "string"},
382 {TOK_OPAQUE, "opaque"},
385 {TOK_PROGRAM, "program"},
386 {TOK_VERSION, "version"},
387 {TOK_PACKAGE, "package"},
388 {TOK_PREFIX, "prefix"},
389 {TOK_STATINDEX, "statindex"},
390 {TOK_SPECIAL, "special"},
391 {TOK_STARTINGOPCODE, "startingopcode"},
392 {TOK_CUSTOMIZED, "customized"},
394 {TOK_SPLITPREFIX, "splitprefix"},
395 {TOK_SPLIT, "split"},
396 {TOK_MULTI, "multi"},
399 {TOK_INOUT, "INOUT"},
400 {TOK_AFSUUID, "afsUUID"},
405 toktostr(tok_kind kind)
409 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
424 for (i = 0; (c = curline[i]); i++) {
426 cnt = 8 - (i % TABSIZE);
446 for (i = 0; i < where - curline; i++) {
449 cnt = 8 - (i % TABSIZE);