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.4 2004/06/19 16:40:36 joerg 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);
84 streq(char *a, char *b)
86 return(strcmp(a, b) == 0);
90 * find a value in a list
93 findval(list *lst, char *val, int (*cmp) (definition *, char *))
95 for (; lst != NULL; lst = lst->next) {
96 if ((*cmp) (lst->val, val))
103 * store a value in a list
106 storeval(list **lstp, definition *val)
111 for (l = lstp; *l != NULL; l = (list **) & (*l)->next)
120 findit(definition *def, char *type)
122 return(streq(def->def_name, type));
126 fixit(char *type, char *orig)
130 def = (definition *) FINDVAL(defined, type, findit);
131 if (def == NULL || def->def_kind != DEF_TYPEDEF)
133 switch (def->def.ty.rel) {
135 if (streq(def->def.ty.old_type, "opaque"))
138 return(def->def.ty.old_type);
141 return(fixit(def->def.ty.old_type, orig));
150 return(fixit(type, type));
154 stringfix(char *type)
156 if (streq(type, "string"))
157 return("wrapstring");
163 ptype(char *prefix, char *type, int follow)
165 if (prefix != NULL) {
166 if (streq(prefix, "enum"))
167 f_print(fout, "enum ");
169 f_print(fout, "struct ");
171 if (streq(type, "bool"))
172 f_print(fout, "bool_t ");
173 else if (streq(type, "string"))
174 f_print(fout, "char *");
176 f_print(fout, "%s ", follow ? fixtype(type) : type);
180 typedefed(definition *def, char *type)
182 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
185 return(streq(def->def_name, type));
189 isvectordef(char *type, relation rel)
196 return(!streq(type, "string"));
202 def = (definition *) FINDVAL(defined, type, typedefed);
206 type = def->def.ty.old_type;
207 rel = def->def.ty.rel;
218 static char buf[100];
221 while ((c = *str++) != 0)
222 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
228 pvname_svc(char *pname, char *vnum)
230 f_print(fout, "%s_%s_svc", locase(pname), vnum);
234 pvname(char *pname, char *vnum)
236 f_print(fout, "%s_%s", locase(pname), vnum);
240 * print a useful (?) error message, and then die
246 warnx("%s, line %d: %s", infilename, linenum, msg);
251 * Something went wrong, unlink any files that we may have created and then
259 for (i = 0; i < nfiles; i++)
265 record_open(char *file)
267 if (nfiles < NFILES) {
268 outfiles[nfiles++] = file;
270 warnx("too many files");
275 static char expectbuf[100];
276 static char *toktostr();
279 * error, token encountered was not the expected one
282 expected1(tok_kind exp1)
284 s_print(expectbuf, "expected '%s'", toktostr(exp1));
289 * error, token encountered was not one of two expected ones
292 expected2(tok_kind exp1, tok_kind exp2)
294 s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1),
300 * error, token encountered was not one of 3 expected ones
303 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
305 s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1),
306 toktostr(exp2), toktostr(exp3));
311 tabify(FILE *f, int tab)
318 static token tokstrings[] = {
319 {TOK_IDENT, "identifier"},
320 {TOK_CONST, "const"},
331 {TOK_SEMICOLON, ";"},
332 {TOK_UNION, "union"},
333 {TOK_STRUCT, "struct"},
334 {TOK_SWITCH, "switch"},
336 {TOK_DEFAULT, "default"},
338 {TOK_TYPEDEF, "typedef"},
340 {TOK_SHORT, "short"},
342 {TOK_UNSIGNED, "unsigned"},
343 {TOK_DOUBLE, "double"},
344 {TOK_FLOAT, "float"},
346 {TOK_STRING, "string"},
347 {TOK_OPAQUE, "opaque"},
350 {TOK_PROGRAM, "program"},
351 {TOK_VERSION, "version"},
356 toktostr(tok_kind kind)
360 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++)
374 for (i = 0; (c = curline[i]); i++) {
376 cnt = 8 - (i % TABSIZE);
394 for (i = 0; i < where - curline; i++) {
397 cnt = 8 - (i % TABSIZE);
408 make_argname(char *pname, char *vname)
412 name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
414 errx(1, "failed in malloc");
415 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
419 bas_type *typ_list_h;
420 bas_type *typ_list_t;
423 add_type(int len, char *type)
427 if ((ptr = (bas_type *) malloc(sizeof (bas_type))) == NULL)
428 errx(1, "failed in malloc");
433 if (typ_list_t == NULL) {
437 typ_list_t->next = ptr;
444 find_type(char *type)
451 if (strcmp(ptr->name, type) == 0)