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";
36 static const char rcsid[] =
37 "$FreeBSD: src/usr.bin/rpcgen/rpc_util.c,v 1.6 1999/08/28 01:05:17 peter Exp $";
41 * rpc_util.c, Utility routines for the RPC protocol compiler
42 * Copyright (C) 1989, Sun Microsystems, Inc.
50 #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 char *infilename; /* input filename */
62 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 __P(( void ));
73 * Reinitialize the world
78 memset(curline, 0, MAXLINESIZE);
92 return (strcmp(a, b) == 0);
96 * find a value in a list
99 findval(lst, val, cmp)
105 for (; lst != NULL; lst = lst->next) {
106 if ((*cmp) (lst->val, val)) {
114 * store a value in a list
124 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
136 return (streq(def->def_name, type));
146 def = (definition *) FINDVAL(defined, type, findit);
147 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
150 switch (def->def.ty.rel) {
152 if (streq(def->def.ty.old_type, "opaque"))
155 return (def->def.ty.old_type);
158 return (fixit(def->def.ty.old_type, orig));
168 return (fixit(type, type));
175 if (streq(type, "string")) {
176 return ("wrapstring");
183 ptype(prefix, type, follow)
188 if (prefix != NULL) {
189 if (streq(prefix, "enum")) {
190 f_print(fout, "enum ");
192 f_print(fout, "struct ");
195 if (streq(type, "bool")) {
196 f_print(fout, "bool_t ");
197 } else if (streq(type, "string")) {
198 f_print(fout, "char *");
200 f_print(fout, "%s ", follow ? fixtype(type) : type);
209 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
212 return (streq(def->def_name, type));
217 isvectordef(type, rel)
226 return (!streq(type, "string"));
232 def = (definition *) FINDVAL(defined, type, typedefed);
236 type = def->def.ty.old_type;
237 rel = def->def.ty.rel;
249 static char buf[100];
252 while ( (c = *str++) ) {
253 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
260 pvname_svc(pname, vnum)
264 f_print(fout, "%s_%s_svc", locase(pname), vnum);
272 f_print(fout, "%s_%s", locase(pname), vnum);
276 * print a useful (?) error message, and then die
283 warnx("%s, line %d: %s", infilename, linenum, msg);
288 * Something went wrong, unlink any files that we may have created and then
296 for (i = 0; i < nfiles; i++) {
297 (void) unlink(outfiles[i]);
306 if (nfiles < NFILES) {
307 outfiles[nfiles++] = file;
309 warnx("too many files");
314 static char expectbuf[100];
315 static char *toktostr();
318 * error, token encountered was not the expected one
324 s_print(expectbuf, "expected '%s'",
330 * error, token encountered was not one of two expected ones
333 expected2(exp1, exp2)
336 s_print(expectbuf, "expected '%s' or '%s'",
343 * error, token encountered was not one of 3 expected ones
346 expected3(exp1, exp2, exp3)
347 tok_kind exp1, exp2, exp3;
349 s_print(expectbuf, "expected '%s', '%s' or '%s'",
362 (void) fputc('\t', f);
367 static token tokstrings[] = {
368 {TOK_IDENT, "identifier"},
369 {TOK_CONST, "const"},
380 {TOK_SEMICOLON, ";"},
381 {TOK_UNION, "union"},
382 {TOK_STRUCT, "struct"},
383 {TOK_SWITCH, "switch"},
385 {TOK_DEFAULT, "default"},
387 {TOK_TYPEDEF, "typedef"},
389 {TOK_SHORT, "short"},
391 {TOK_UNSIGNED, "unsigned"},
392 {TOK_DOUBLE, "double"},
393 {TOK_FLOAT, "float"},
395 {TOK_STRING, "string"},
396 {TOK_OPAQUE, "opaque"},
399 {TOK_PROGRAM, "program"},
400 {TOK_VERSION, "version"},
410 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
423 for (i = 0; (c = curline[i]); i++) {
425 cnt = 8 - (i % TABSIZE);
431 (void) fputc(c, stderr);
444 for (i = 0; i < where - curline; i++) {
447 cnt = 8 - (i % TABSIZE);
452 (void) fputc('^', stderr);
455 (void) fputc('\n', stderr);
459 make_argname(pname, vname)
465 name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
467 errx(1, "failed in malloc");
468 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
472 bas_type *typ_list_h;
473 bas_type *typ_list_t;
482 if ((ptr = (bas_type *) malloc(sizeof (bas_type))) == (bas_type *)NULL)
483 errx(1, "failed in malloc");
488 if (typ_list_t == NULL)
496 typ_list_t->next = ptr;
502 bas_type *find_type(type)
510 if (strcmp(ptr->name, type) == 0)