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.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
32 #ident "@(#)rpc_util.c 1.14 93/07/05 SMI"
33 static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * rpc_util.c, Utility routines for the RPC protocol compiler
42 * Copyright (C) 1989, Sun Microsystems, Inc.
49 #include "rpc_parse.h"
53 #define ARGEXT "argument"
55 char curline[MAXLINESIZE]; /* current read line */
56 char *where = curline; /* current point in line */
57 int linenum = 0; /* current line number */
59 const char *infilename; /* input filename */
62 static const char *outfiles[NFILES]; /* output file names */
65 FILE *fout; /* file pointer of current output */
66 FILE *fin; /* file pointer of current input */
68 list *defined; /* list of defined things */
70 static void printwhere( void );
73 * Reinitialize the world
78 memset(curline, 0, MAXLINESIZE);
88 streq(const char *a, const char *b)
90 return (strcmp(a, b) == 0);
94 * find a value in a list
97 findval(list *lst, const char *val, int (*cmp)(definition *, const char *))
99 for (; lst != NULL; lst = lst->next) {
100 if ((*cmp) (lst->val, val)) {
108 * store a value in a list
111 storeval(list **lstp, definition *val)
116 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
124 findit(definition *def, const char *type)
126 return (streq(def->def_name, type));
130 fixit(const char *type, const char *orig)
134 def = (definition *) FINDVAL(defined, type, findit);
135 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
138 switch (def->def.ty.rel) {
140 if (streq(def->def.ty.old_type, "opaque"))
143 return (def->def.ty.old_type);
146 return (fixit(def->def.ty.old_type, orig));
153 fixtype(const char *type)
155 return (fixit(type, type));
159 stringfix(const char *type)
161 if (streq(type, "string")) {
162 return ("wrapstring");
169 ptype(const char *prefix, const char *type, int follow)
171 if (prefix != NULL) {
172 if (streq(prefix, "enum")) {
173 f_print(fout, "enum ");
175 f_print(fout, "struct ");
178 if (streq(type, "bool")) {
179 f_print(fout, "bool_t ");
180 } else if (streq(type, "string")) {
181 f_print(fout, "char *");
183 f_print(fout, "%s ", follow ? fixtype(type) : type);
188 typedefed(definition *def, const char *type)
190 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
193 return (streq(def->def_name, type));
198 isvectordef(const char *type, relation rel)
205 return (!streq(type, "string"));
211 def = (definition *) FINDVAL(defined, type, typedefed);
215 type = def->def.ty.old_type;
216 rel = def->def.ty.rel;
224 locase(const char *str)
227 static char buf[100];
230 while ( (c = *str++) ) {
231 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
238 pvname_svc(const char *pname, const char *vnum)
240 f_print(fout, "%s_%s_svc", locase(pname), vnum);
244 pvname(const char *pname, const char *vnum)
246 f_print(fout, "%s_%s", locase(pname), vnum);
250 * print a useful (?) error message, and then die
253 error(const char *msg)
256 warnx("%s, line %d: %s", infilename, linenum, msg);
261 * Something went wrong, unlink any files that we may have created and then
269 for (i = 0; i < nfiles; i++) {
270 (void) unlink(outfiles[i]);
276 record_open(const char *file)
278 if (nfiles < NFILES) {
279 outfiles[nfiles++] = file;
281 warnx("too many files");
286 static char expectbuf[100];
287 static const char *toktostr(tok_kind kind);
290 * error, token encountered was not the expected one
293 expected1(tok_kind exp1)
295 s_print(expectbuf, "expected '%s'",
301 * error, token encountered was not one of two expected ones
304 expected2(tok_kind exp1, tok_kind exp2)
306 s_print(expectbuf, "expected '%s' or '%s'",
313 * error, token encountered was not one of 3 expected ones
316 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
318 s_print(expectbuf, "expected '%s', '%s' or '%s'",
326 tabify(FILE *f, int tab)
329 (void) fputc('\t', f);
334 static token tokstrings[] = {
335 {TOK_IDENT, "identifier"},
336 {TOK_CONST, "const"},
347 {TOK_SEMICOLON, ";"},
348 {TOK_UNION, "union"},
349 {TOK_STRUCT, "struct"},
350 {TOK_SWITCH, "switch"},
352 {TOK_DEFAULT, "default"},
354 {TOK_TYPEDEF, "typedef"},
356 {TOK_SHORT, "short"},
358 {TOK_UNSIGNED, "unsigned"},
359 {TOK_DOUBLE, "double"},
360 {TOK_FLOAT, "float"},
362 {TOK_STRING, "string"},
363 {TOK_OPAQUE, "opaque"},
366 {TOK_PROGRAM, "program"},
367 {TOK_VERSION, "version"},
372 toktostr(tok_kind kind)
376 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
389 for (i = 0; (c = curline[i]); i++) {
391 cnt = 8 - (i % TABSIZE);
397 (void) fputc(c, stderr);
410 for (i = 0; i < where - curline; i++) {
413 cnt = 8 - (i % TABSIZE);
418 (void) fputc('^', stderr);
421 (void) fputc('\n', stderr);
425 make_argname(const char *pname, const char *vname)
429 name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
430 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
434 bas_type *typ_list_h;
435 bas_type *typ_list_t;
438 add_type(int len, const char *type)
442 ptr = XALLOC(bas_type);
447 if (typ_list_t == NULL)
455 typ_list_t->next = ptr;
462 find_type(const char *type)
469 if (strcmp(ptr->name, type) == 0)
482 if ((p = malloc(size)) == NULL) {
483 warnx("malloc failed");
490 xrealloc(void *ptr, size_t size)
494 if ((p = realloc(ptr, size)) == NULL) {
495 warnx("realloc failed");
502 xstrdup(const char *str)
506 if ((p = strdup(str)) == NULL) {
507 warnx("strdup failed");