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>
39 * rpc_util.c, Utility routines for the RPC protocol compiler
40 * Copyright (C) 1989, Sun Microsystems, Inc.
47 #include "rpc_parse.h"
51 #define ARGEXT "argument"
53 char curline[MAXLINESIZE]; /* current read line */
54 char *where = curline; /* current point in line */
55 int linenum = 0; /* current line number */
57 const char *infilename; /* input filename */
60 static const 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 void printwhere( void );
71 * Reinitialize the world
76 memset(curline, 0, MAXLINESIZE);
86 streq(const char *a, const char *b)
88 return (strcmp(a, b) == 0);
92 * find a value in a list
95 findval(list *lst, const char *val, int (*cmp)(definition *, const char *))
97 for (; lst != NULL; lst = lst->next) {
98 if ((*cmp) (lst->val, val)) {
106 * store a value in a list
109 storeval(list **lstp, definition *val)
114 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
122 findit(definition *def, const char *type)
124 return (streq(def->def_name, type));
128 fixit(const char *type, const 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 if (streq(def->def.ty.old_type, "opaque"))
141 return (def->def.ty.old_type);
144 return (fixit(def->def.ty.old_type, orig));
151 fixtype(const char *type)
153 return (fixit(type, type));
157 stringfix(const char *type)
159 if (streq(type, "string")) {
160 return ("wrapstring");
167 ptype(const char *prefix, const char *type, int follow)
169 if (prefix != NULL) {
170 if (streq(prefix, "enum")) {
171 f_print(fout, "enum ");
173 f_print(fout, "struct ");
176 if (streq(type, "bool")) {
177 f_print(fout, "bool_t ");
178 } else if (streq(type, "string")) {
179 f_print(fout, "char *");
181 f_print(fout, "%s ", follow ? fixtype(type) : type);
186 typedefed(definition *def, const char *type)
188 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
191 return (streq(def->def_name, type));
196 isvectordef(const char *type, relation rel)
203 return (!streq(type, "string"));
209 def = (definition *) FINDVAL(defined, type, typedefed);
213 type = def->def.ty.old_type;
214 rel = def->def.ty.rel;
222 locase(const char *str)
225 static char buf[100];
228 while ( (c = *str++) ) {
229 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
236 pvname_svc(const char *pname, const char *vnum)
238 f_print(fout, "%s_%s_svc", locase(pname), vnum);
242 pvname(const char *pname, const char *vnum)
244 f_print(fout, "%s_%s", locase(pname), vnum);
248 * print a useful (?) error message, and then die
251 error(const char *msg)
254 warnx("%s, line %d: %s", infilename, linenum, msg);
259 * Something went wrong, unlink any files that we may have created and then
267 for (i = 0; i < nfiles; i++) {
268 (void) unlink(outfiles[i]);
274 record_open(const char *file)
276 if (nfiles < NFILES) {
277 outfiles[nfiles++] = file;
279 warnx("too many files");
284 static char expectbuf[100];
285 static const char *toktostr(tok_kind kind);
288 * error, token encountered was not the expected one
291 expected1(tok_kind exp1)
293 s_print(expectbuf, "expected '%s'",
299 * error, token encountered was not one of two expected ones
302 expected2(tok_kind exp1, tok_kind exp2)
304 s_print(expectbuf, "expected '%s' or '%s'",
311 * error, token encountered was not one of 3 expected ones
314 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
316 s_print(expectbuf, "expected '%s', '%s' or '%s'",
324 tabify(FILE *f, int tab)
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"},
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"},
370 toktostr(tok_kind kind)
374 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
387 for (i = 0; (c = curline[i]); i++) {
389 cnt = 8 - (i % TABSIZE);
395 (void) fputc(c, stderr);
408 for (i = 0; i < where - curline; i++) {
411 cnt = 8 - (i % TABSIZE);
416 (void) fputc('^', stderr);
419 (void) fputc('\n', stderr);
423 make_argname(const char *pname, const char *vname)
427 name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
428 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
432 bas_type *typ_list_h;
433 bas_type *typ_list_t;
436 add_type(int len, const char *type)
440 ptr = XALLOC(bas_type);
445 if (typ_list_t == NULL)
453 typ_list_t->next = ptr;
460 find_type(const char *type)
467 if (strcmp(ptr->name, type) == 0)
480 if ((p = malloc(size)) == NULL) {
481 warnx("malloc failed");
488 xrealloc(void *ptr, size_t size)
492 if ((p = realloc(ptr, size)) == NULL) {
493 warnx("realloc failed");
500 xstrdup(const char *str)
504 if ((p = strdup(str)) == NULL) {
505 warnx("strdup failed");