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 static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
36 #include <sys/cdefs.h>
38 * rpc_util.c, Utility routines for the RPC protocol compiler
39 * Copyright (C) 1989, Sun Microsystems, Inc.
46 #include "rpc_parse.h"
50 #define ARGEXT "argument"
52 char curline[MAXLINESIZE]; /* current read line */
53 char *where = curline; /* current point in line */
54 int linenum = 0; /* current line number */
56 const char *infilename; /* input filename */
59 static const char *outfiles[NFILES]; /* output file names */
62 FILE *fout; /* file pointer of current output */
63 FILE *fin; /* file pointer of current input */
65 list *defined; /* list of defined things */
67 static void printwhere( void );
70 * Reinitialize the world
75 memset(curline, 0, MAXLINESIZE);
85 streq(const char *a, const char *b)
87 return (strcmp(a, b) == 0);
91 * find a value in a list
94 findval(list *lst, const char *val, int (*cmp)(definition *, const char *))
96 for (; lst != NULL; lst = lst->next) {
97 if ((*cmp) (lst->val, val)) {
105 * store a value in a list
108 storeval(list **lstp, definition *val)
113 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
121 findit(definition *def, const char *type)
123 return (streq(def->def_name, type));
127 fixit(const char *type, const char *orig)
131 def = (definition *) FINDVAL(defined, type, findit);
132 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
135 switch (def->def.ty.rel) {
137 if (streq(def->def.ty.old_type, "opaque"))
140 return (def->def.ty.old_type);
143 return (fixit(def->def.ty.old_type, orig));
150 fixtype(const char *type)
152 return (fixit(type, type));
156 stringfix(const char *type)
158 if (streq(type, "string")) {
159 return ("wrapstring");
166 ptype(const char *prefix, const char *type, int follow)
168 if (prefix != NULL) {
169 if (streq(prefix, "enum")) {
170 f_print(fout, "enum ");
172 f_print(fout, "struct ");
175 if (streq(type, "bool")) {
176 f_print(fout, "bool_t ");
177 } else if (streq(type, "string")) {
178 f_print(fout, "char *");
180 f_print(fout, "%s ", follow ? fixtype(type) : type);
185 typedefed(definition *def, const char *type)
187 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
190 return (streq(def->def_name, type));
195 isvectordef(const char *type, relation rel)
202 return (!streq(type, "string"));
208 def = (definition *) FINDVAL(defined, type, typedefed);
212 type = def->def.ty.old_type;
213 rel = def->def.ty.rel;
221 locase(const char *str)
224 static char buf[100];
227 while ( (c = *str++) ) {
228 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
235 pvname_svc(const char *pname, const char *vnum)
237 f_print(fout, "%s_%s_svc", locase(pname), vnum);
241 pvname(const char *pname, const char *vnum)
243 f_print(fout, "%s_%s", locase(pname), vnum);
247 * print a useful (?) error message, and then die
250 error(const char *msg)
253 warnx("%s, line %d: %s", infilename, linenum, msg);
258 * Something went wrong, unlink any files that we may have created and then
266 for (i = 0; i < nfiles; i++) {
267 (void) unlink(outfiles[i]);
273 record_open(const char *file)
275 if (nfiles < NFILES) {
276 outfiles[nfiles++] = file;
278 warnx("too many files");
283 static char expectbuf[100];
284 static const char *toktostr(tok_kind kind);
287 * error, token encountered was not the expected one
290 expected1(tok_kind exp1)
292 s_print(expectbuf, "expected '%s'",
298 * error, token encountered was not one of two expected ones
301 expected2(tok_kind exp1, tok_kind exp2)
303 s_print(expectbuf, "expected '%s' or '%s'",
310 * error, token encountered was not one of 3 expected ones
313 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
315 s_print(expectbuf, "expected '%s', '%s' or '%s'",
323 tabify(FILE *f, int tab)
326 (void) fputc('\t', f);
331 static token tokstrings[] = {
332 {TOK_IDENT, "identifier"},
333 {TOK_CONST, "const"},
344 {TOK_SEMICOLON, ";"},
345 {TOK_UNION, "union"},
346 {TOK_STRUCT, "struct"},
347 {TOK_SWITCH, "switch"},
349 {TOK_DEFAULT, "default"},
351 {TOK_TYPEDEF, "typedef"},
353 {TOK_SHORT, "short"},
355 {TOK_UNSIGNED, "unsigned"},
356 {TOK_DOUBLE, "double"},
357 {TOK_FLOAT, "float"},
359 {TOK_STRING, "string"},
360 {TOK_OPAQUE, "opaque"},
363 {TOK_PROGRAM, "program"},
364 {TOK_VERSION, "version"},
369 toktostr(tok_kind kind)
373 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
386 for (i = 0; (c = curline[i]); i++) {
388 cnt = 8 - (i % TABSIZE);
394 (void) fputc(c, stderr);
407 for (i = 0; i < where - curline; i++) {
410 cnt = 8 - (i % TABSIZE);
415 (void) fputc('^', stderr);
418 (void) fputc('\n', stderr);
422 make_argname(const char *pname, const char *vname)
426 name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
427 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
431 bas_type *typ_list_h;
432 bas_type *typ_list_t;
435 add_type(int len, const char *type)
439 ptr = XALLOC(bas_type);
444 if (typ_list_t == NULL)
452 typ_list_t->next = ptr;
459 find_type(const char *type)
466 if (strcmp(ptr->name, type) == 0)
479 if ((p = malloc(size)) == NULL) {
480 warnx("malloc failed");
487 xrealloc(void *ptr, size_t size)
491 if ((p = realloc(ptr, size)) == NULL) {
492 warnx("realloc failed");
499 xstrdup(const char *str)
503 if ((p = strdup(str)) == NULL) {
504 warnx("strdup failed");