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
30 #ident "@(#)rpc_util.c 1.14 93/07/05 SMI"
34 static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * rpc_util.c, Utility routines for the RPC protocol compiler
43 * Copyright (C) 1989, Sun Microsystems, Inc.
51 #include "rpc_parse.h"
54 #define ARGEXT "argument"
56 char curline[MAXLINESIZE]; /* current read line */
57 char *where = curline; /* current point in line */
58 int linenum = 0; /* current line number */
60 char *infilename; /* input filename */
63 char *outfiles[NFILES]; /* output file names */
66 FILE *fout; /* file pointer of current output */
67 FILE *fin; /* file pointer of current input */
69 list *defined; /* list of defined things */
71 static void printwhere( void );
74 * Reinitialize the world
79 memset(curline, 0, MAXLINESIZE);
93 return (strcmp(a, b) == 0);
97 * find a value in a list
100 findval(lst, val, cmp)
106 for (; lst != NULL; lst = lst->next) {
107 if ((*cmp) (lst->val, val)) {
115 * store a value in a list
125 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
137 return (streq(def->def_name, type));
147 def = (definition *) FINDVAL(defined, type, findit);
148 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
151 switch (def->def.ty.rel) {
153 if (streq(def->def.ty.old_type, "opaque"))
156 return (def->def.ty.old_type);
159 return (fixit(def->def.ty.old_type, orig));
169 return (fixit(type, type));
176 if (streq(type, "string")) {
177 return ("wrapstring");
184 ptype(prefix, type, follow)
189 if (prefix != NULL) {
190 if (streq(prefix, "enum")) {
191 f_print(fout, "enum ");
193 f_print(fout, "struct ");
196 if (streq(type, "bool")) {
197 f_print(fout, "bool_t ");
198 } else if (streq(type, "string")) {
199 f_print(fout, "char *");
201 f_print(fout, "%s ", follow ? fixtype(type) : type);
210 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
213 return (streq(def->def_name, type));
218 isvectordef(type, rel)
227 return (!streq(type, "string"));
233 def = (definition *) FINDVAL(defined, type, typedefed);
237 type = def->def.ty.old_type;
238 rel = def->def.ty.rel;
250 static char buf[100];
253 while ( (c = *str++) ) {
254 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
261 pvname_svc(pname, vnum)
265 f_print(fout, "%s_%s_svc", locase(pname), vnum);
273 f_print(fout, "%s_%s", locase(pname), vnum);
277 * print a useful (?) error message, and then die
284 warnx("%s, line %d: %s", infilename, linenum, msg);
289 * Something went wrong, unlink any files that we may have created and then
297 for (i = 0; i < nfiles; i++) {
298 (void) unlink(outfiles[i]);
307 if (nfiles < NFILES) {
308 outfiles[nfiles++] = file;
310 warnx("too many files");
315 static char expectbuf[100];
316 static char *toktostr();
319 * error, token encountered was not the expected one
325 s_print(expectbuf, "expected '%s'",
331 * error, token encountered was not one of two expected ones
334 expected2(exp1, exp2)
337 s_print(expectbuf, "expected '%s' or '%s'",
344 * error, token encountered was not one of 3 expected ones
347 expected3(exp1, exp2, exp3)
348 tok_kind exp1, exp2, exp3;
350 s_print(expectbuf, "expected '%s', '%s' or '%s'",
363 (void) fputc('\t', f);
368 static token tokstrings[] = {
369 {TOK_IDENT, "identifier"},
370 {TOK_CONST, "const"},
381 {TOK_SEMICOLON, ";"},
382 {TOK_UNION, "union"},
383 {TOK_STRUCT, "struct"},
384 {TOK_SWITCH, "switch"},
386 {TOK_DEFAULT, "default"},
388 {TOK_TYPEDEF, "typedef"},
390 {TOK_SHORT, "short"},
392 {TOK_UNSIGNED, "unsigned"},
393 {TOK_DOUBLE, "double"},
394 {TOK_FLOAT, "float"},
396 {TOK_STRING, "string"},
397 {TOK_OPAQUE, "opaque"},
400 {TOK_PROGRAM, "program"},
401 {TOK_VERSION, "version"},
411 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
424 for (i = 0; (c = curline[i]); i++) {
426 cnt = 8 - (i % TABSIZE);
432 (void) fputc(c, stderr);
445 for (i = 0; i < where - curline; i++) {
448 cnt = 8 - (i % TABSIZE);
453 (void) fputc('^', stderr);
456 (void) fputc('\n', stderr);
460 make_argname(pname, vname)
466 name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
467 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
471 bas_type *typ_list_h;
472 bas_type *typ_list_t;
481 ptr = XALLOC(bas_type);
486 if (typ_list_t == NULL)
494 typ_list_t->next = ptr;
500 bas_type *find_type(type)
508 if (strcmp(ptr->name, type) == 0)
521 if ((p = malloc(size)) == NULL) {
522 warnx("malloc failed");
529 xrealloc(void *ptr, size_t size)
533 if ((p = realloc(ptr, size)) == NULL) {
534 warnx("realloc failed");
541 xstrdup(const char *str)
545 if ((p = strdup(str)) == NULL) {
546 warnx("strdup failed");