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>
56 #include "rpc_parse.h"
59 char curline[MAXLINESIZE]; /* current read line */
60 char *where = curline; /* current point in line */
61 int linenum = 0; /* current line number */
63 char *infilename; /* input filename */
66 char *outfiles[NFILES]; /* output file names */
69 FILE *fout; /* file pointer of current output */
70 FILE *fin; /* file pointer of current input */
72 list *defined; /* list of defined things */
74 /* static prototypes */
75 static int findit(definition * def, char *type);
76 static char *fixit(char *type, char *orig);
77 static int typedefed(definition * def, char *type);
78 static char *locase(char *str);
79 static char *toktostr(tok_kind kind);
80 static void printbuf(void);
81 static void printwhere(void);
85 * Reinitialize the world
91 memset(curline, 0, MAXLINESIZE);
95 special_defined = typedef_defined = uniondef_defined = NULL;
97 master_opcodenumber = 99999;
98 master_highest_opcode = 0;
100 for (i = 0; i < MAX_PACKAGES; i++) {
101 no_of_stat_funcs_header[i] = 0;
109 streq(char *a, char *b)
111 return (strcmp(a, b) == 0);
115 * find a value in a list
118 findval(list * lst, char *val, int (*cmp) (definition * def, char *type))
120 for (; lst != NULL; lst = lst->next) {
121 if ((*cmp) ((definition *) lst->val, val)) {
129 * store a value in a list
132 storeval(list ** lstp, char *val)
137 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
146 findit(definition * def, char *type)
148 return (streq(def->def_name, type));
153 fixit(char *type, char *orig)
157 def = (definition *) FINDVAL(defined, type, findit);
158 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
161 switch (def->def.ty.rel) {
163 return (def->def.ty.old_type);
165 return (fixit(def->def.ty.old_type, orig));
174 return (fixit(type, type));
178 stringfix(char *type)
180 if (streq(type, "string")) {
181 return ("wrapstring");
188 ptype(char *prefix, char *type, int follow)
190 if (prefix != NULL) {
191 if (streq(prefix, "enum")) {
192 f_print(fout, "enum ");
194 f_print(fout, "struct ");
197 if (streq(type, "bool")) {
198 f_print(fout, "bool_t ");
199 } else if (streq(type, "string")) {
200 f_print(fout, "char *");
202 f_print(fout, "%s ", follow ? fixtype(type) : type);
208 typedefed(definition * def, char *type)
210 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
213 return (streq(def->def_name, type));
218 isvectordef(char *type, relation rel)
225 return (!streq(type, "string"));
231 def = (definition *) FINDVAL(defined, type, typedefed);
235 type = def->def.ty.old_type;
236 rel = def->def.ty.rel;
246 static char buf[100];
249 while ((c = *str++)) {
250 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
258 pvname(char *pname, char *vnum)
260 f_print(fout, "%s_%s", locase(pname), vnum);
265 * print a useful (?) error message, and then die
271 f_print(stderr, "%s, line %d: ", infilename, linenum);
272 f_print(stderr, "%s\n", msg);
277 * Something went wrong, unlink any files that we may have created and then
285 for (i = 0; i < nfiles; i++) {
286 (void)unlink(outfiles[i]);
293 record_open(char *file)
295 if (nfiles < NFILES) {
296 outfiles[nfiles++] = file;
298 f_print(stderr, "too many files!\n");
303 /* buffer shared for all of the expected* routines */
304 static char expectbuf[100];
307 * error, token encountered was not the expected one
310 expected1(tok_kind exp1)
312 s_print(expectbuf, "expected '%s'", toktostr(exp1));
317 * error, token encountered was not one of two expected ones
320 expected2(tok_kind exp1, tok_kind exp2)
322 s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1),
328 * error, token encountered was not one of 3 expected ones
331 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
333 s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1),
334 toktostr(exp2), toktostr(exp3));
340 * error, token encountered was not one of 4 expected ones
343 expected4(tok_kind exp1, tok_kind exp2, tok_kind exp3, tok_kind exp4)
345 sprintf(expectbuf, "expected '%s', '%s', '%s', or '%s'", toktostr(exp1),
346 toktostr(exp2), toktostr(exp3), toktostr(exp4));
351 tabify(FILE * f, int tab)
355 (void)fputc('\t', f);
359 static token tokstrings[] = {
360 {TOK_IDENT, "identifier"},
361 {TOK_CONST, "const"},
372 {TOK_SEMICOLON, ";"},
373 {TOK_UNION, "union"},
374 {TOK_STRUCT, "struct"},
375 {TOK_SWITCH, "switch"},
377 {TOK_DEFAULT, "default"},
379 {TOK_TYPEDEF, "typedef"},
381 {TOK_SHORT, "short"},
382 {TOK_INT32, "afs_int32"}, /* XXX */
383 {TOK_UNSIGNED, "unsigned"},
384 {TOK_DOUBLE, "double"},
385 {TOK_FLOAT, "float"},
387 {TOK_STRING, "string"},
388 {TOK_OPAQUE, "opaque"},
391 {TOK_PROGRAM, "program"},
392 {TOK_VERSION, "version"},
393 {TOK_PACKAGE, "package"},
394 {TOK_PREFIX, "prefix"},
395 {TOK_STATINDEX, "statindex"},
396 {TOK_SPECIAL, "special"},
397 {TOK_STARTINGOPCODE, "startingopcode"},
398 {TOK_CUSTOMIZED, "customized"},
400 {TOK_SPLITPREFIX, "splitprefix"},
401 {TOK_SPLIT, "split"},
402 {TOK_MULTI, "multi"},
405 {TOK_INOUT, "INOUT"},
406 {TOK_AFSUUID, "afsUUID"},
411 toktostr(tok_kind kind)
415 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
430 for (i = 0; (c = curline[i]); i++) {
432 cnt = 8 - (i % TABSIZE);
452 for (i = 0; i < where - curline; i++) {
455 cnt = 8 - (i % TABSIZE);