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
29 * @(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_util.c,v 1.6 1999/08/28 01:05:17 peter Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_util.c,v 1.3 2003/11/03 19:31:32 eirikn Exp $
34 #ident "@(#)rpc_util.c 1.14 93/07/05 SMI"
37 * rpc_util.c, Utility routines for the RPC protocol compiler
38 * Copyright (C) 1989, Sun Microsystems, Inc.
46 #include "rpc_parse.h"
49 #define ARGEXT "argument"
51 char curline[MAXLINESIZE]; /* current read line */
52 char *where = curline; /* current point in line */
53 int linenum = 0; /* current line number */
55 char *infilename; /* input filename */
58 char *outfiles[NFILES]; /* output file names */
61 FILE *fout; /* file pointer of current output */
62 FILE *fin; /* file pointer of current input */
64 list *defined; /* list of defined things */
66 static void printwhere( void );
69 * Reinitialize the world
74 memset(curline, 0, MAXLINESIZE);
88 return (strcmp(a, b) == 0);
92 * find a value in a list
95 findval(lst, val, cmp)
101 for (; lst != NULL; lst = lst->next) {
102 if ((*cmp) (lst->val, val)) {
110 * store a value in a list
120 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
132 return (streq(def->def_name, type));
142 def = (definition *) FINDVAL(defined, type, findit);
143 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
146 switch (def->def.ty.rel) {
148 if (streq(def->def.ty.old_type, "opaque"))
151 return (def->def.ty.old_type);
154 return (fixit(def->def.ty.old_type, orig));
164 return (fixit(type, type));
171 if (streq(type, "string")) {
172 return ("wrapstring");
179 ptype(prefix, type, follow)
184 if (prefix != NULL) {
185 if (streq(prefix, "enum")) {
186 f_print(fout, "enum ");
188 f_print(fout, "struct ");
191 if (streq(type, "bool")) {
192 f_print(fout, "bool_t ");
193 } else if (streq(type, "string")) {
194 f_print(fout, "char *");
196 f_print(fout, "%s ", follow ? fixtype(type) : type);
205 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
208 return (streq(def->def_name, type));
213 isvectordef(type, rel)
222 return (!streq(type, "string"));
228 def = (definition *) FINDVAL(defined, type, typedefed);
232 type = def->def.ty.old_type;
233 rel = def->def.ty.rel;
245 static char buf[100];
248 while ( (c = *str++) ) {
249 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
256 pvname_svc(pname, vnum)
260 f_print(fout, "%s_%s_svc", locase(pname), vnum);
268 f_print(fout, "%s_%s", locase(pname), vnum);
272 * print a useful (?) error message, and then die
279 warnx("%s, line %d: %s", infilename, linenum, msg);
284 * Something went wrong, unlink any files that we may have created and then
292 for (i = 0; i < nfiles; i++) {
293 (void) unlink(outfiles[i]);
302 if (nfiles < NFILES) {
303 outfiles[nfiles++] = file;
305 warnx("too many files");
310 static char expectbuf[100];
311 static char *toktostr();
314 * error, token encountered was not the expected one
320 s_print(expectbuf, "expected '%s'",
326 * error, token encountered was not one of two expected ones
329 expected2(exp1, exp2)
332 s_print(expectbuf, "expected '%s' or '%s'",
339 * error, token encountered was not one of 3 expected ones
342 expected3(exp1, exp2, exp3)
343 tok_kind exp1, exp2, exp3;
345 s_print(expectbuf, "expected '%s', '%s' or '%s'",
358 (void) fputc('\t', f);
363 static token tokstrings[] = {
364 {TOK_IDENT, "identifier"},
365 {TOK_CONST, "const"},
376 {TOK_SEMICOLON, ";"},
377 {TOK_UNION, "union"},
378 {TOK_STRUCT, "struct"},
379 {TOK_SWITCH, "switch"},
381 {TOK_DEFAULT, "default"},
383 {TOK_TYPEDEF, "typedef"},
385 {TOK_SHORT, "short"},
387 {TOK_UNSIGNED, "unsigned"},
388 {TOK_DOUBLE, "double"},
389 {TOK_FLOAT, "float"},
391 {TOK_STRING, "string"},
392 {TOK_OPAQUE, "opaque"},
395 {TOK_PROGRAM, "program"},
396 {TOK_VERSION, "version"},
406 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
419 for (i = 0; (c = curline[i]); i++) {
421 cnt = 8 - (i % TABSIZE);
427 (void) fputc(c, stderr);
440 for (i = 0; i < where - curline; i++) {
443 cnt = 8 - (i % TABSIZE);
448 (void) fputc('^', stderr);
451 (void) fputc('\n', stderr);
455 make_argname(pname, vname)
461 name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
463 errx(1, "failed in malloc");
464 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
468 bas_type *typ_list_h;
469 bas_type *typ_list_t;
478 if ((ptr = (bas_type *) malloc(sizeof (bas_type))) == (bas_type *)NULL)
479 errx(1, "failed in malloc");
484 if (typ_list_t == NULL)
492 typ_list_t->next = ptr;
498 bas_type *find_type(type)
506 if (strcmp(ptr->name, type) == 0)