2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
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 static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
36 * rpc_util.c, Utility routines for the RPC protocol compiler
43 #include "rpc_parse.h"
46 static void printwhere(void);
49 #define ARGEXT "argument"
51 char curline[MAXLINESIZE]; /* current read line */
52 char *where = curline; /* current point in line */
53 int linenum = 0; /* current line number */
55 char *infilename; /* input filename */
58 char *outfiles[NFILES]; /* output file names */
61 FILE *fout; /* file pointer of current output */
62 FILE *fin; /* file pointer of current input */
64 list *defined; /* list of defined things */
67 * Reinitialize the world
72 memset(curline, 0, MAXLINESIZE);
82 streq(char *a, char *b)
84 return (strcmp(a, b) == 0);
88 * find a value in a list
91 findval(list *lst, char *val, int (*cmp)(definition *, char *))
94 for (; lst != NULL; lst = lst->next) {
95 if ((*cmp) (lst->val, val)) {
103 * store a value in a list
114 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
122 findit(definition *def, char *type)
124 return (streq(def->def_name, type));
128 fixit(char *type, char *orig)
132 def = (definition *) FINDVAL(defined, type, findit);
133 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
136 switch (def->def.ty.rel) {
138 return (def->def.ty.old_type);
140 return (fixit(def->def.ty.old_type, orig));
149 return (fixit(type, type));
153 stringfix(char *type)
155 if (streq(type, "string")) {
156 return ("wrapstring");
163 ptype(char *prefix, char *type, int follow)
165 if (prefix != NULL) {
166 if (streq(prefix, "enum")) {
167 f_print(fout, "enum ");
169 f_print(fout, "struct ");
172 if (streq(type, "bool")) {
173 f_print(fout, "bool_t ");
174 } else if (streq(type, "string")) {
175 f_print(fout, "char *");
177 f_print(fout, "%s ", follow ? fixtype(type) : type);
182 typedefed(definition *def, char *type)
184 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
187 return (streq(def->def_name, type));
192 isvectordef(char *type, relation rel)
199 return (!streq(type, "string"));
205 def = (definition *) FINDVAL(defined, type, typedefed);
209 type = def->def.ty.old_type;
210 rel = def->def.ty.rel;
219 static char buf[100];
222 while ((c = *str++) != '\0') {
223 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
230 pvname_svc(char *pname, char *vnum)
232 f_print(fout, "%s_%s_svc", locase(pname), vnum);
236 pvname(char *pname, char *vnum)
238 f_print(fout, "%s_%s", locase(pname), vnum);
242 * print a useful (?) error message, and then die
248 f_print(stderr, "%s, line %d: ", infilename, linenum);
249 f_print(stderr, "%s\n", msg);
254 * Something went wrong, unlink any files that we may have created and then
262 for (i = 0; i < nfiles; i++) {
263 (void) unlink(outfiles[i]);
269 record_open(char *file)
271 if (nfiles < NFILES) {
272 outfiles[nfiles++] = file;
274 f_print(stderr, "too many files!\n");
279 static char expectbuf[100];
280 static char *toktostr();
283 * error, token encountered was not the expected one
289 s_print(expectbuf, "expected '%s'",
295 * error, token encountered was not one of two expected ones
298 expected2(exp1, exp2)
301 s_print(expectbuf, "expected '%s' or '%s'",
308 * error, token encountered was not one of 3 expected ones
311 expected3(exp1, exp2, exp3)
312 tok_kind exp1, exp2, exp3;
314 s_print(expectbuf, "expected '%s', '%s' or '%s'",
327 (void) fputc('\t', f);
332 static token tokstrings[] = {
333 {TOK_IDENT, "identifier"},
334 {TOK_CONST, "const"},
345 {TOK_SEMICOLON, ";"},
346 {TOK_UNION, "union"},
347 {TOK_STRUCT, "struct"},
348 {TOK_SWITCH, "switch"},
350 {TOK_DEFAULT, "default"},
352 {TOK_TYPEDEF, "typedef"},
354 {TOK_SHORT, "short"},
355 {TOK_INT32, "int32"},
356 {TOK_UNSIGNED, "unsigned"},
357 {TOK_DOUBLE, "double"},
358 {TOK_FLOAT, "float"},
360 {TOK_STRING, "string"},
361 {TOK_OPAQUE, "opaque"},
364 {TOK_PROGRAM, "program"},
365 {TOK_VERSION, "version"},
375 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
388 for (i = 0; (c = curline[i]) != '\0'; i++) {
390 cnt = 8 - (i % TABSIZE);
396 (void) fputc(c, stderr);
409 for (i = 0; i < where - curline; i++) {
412 cnt = 8 - (i % TABSIZE);
417 (void) fputc('^', stderr);
420 (void) fputc('\n', stderr);
424 make_argname(char *pname, char *vname)
428 name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
430 fprintf(stderr, "failed in malloc");
433 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
437 bas_type *typ_list_h;
438 bas_type *typ_list_t;
441 add_type(int len, char *type)
446 if ((ptr = (bas_type *) malloc(sizeof(bas_type))) == (bas_type *) NULL) {
447 fprintf(stderr, "failed in malloc");
453 if (typ_list_t == NULL) {
459 typ_list_t->next = ptr;
466 find_type(char *type)
473 while (ptr != NULL) {
474 if (strcmp(ptr->name, type) == 0)