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
31 * rpc_util.c, Utility routines for the RPC protocol compiler
32 * Copyright (C) 1989, Sun Microsystems, Inc.
39 #include "rpc_parse.h"
43 #define ARGEXT "argument"
45 char curline[MAXLINESIZE]; /* current read line */
46 char *where = curline; /* current point in line */
47 int linenum = 0; /* current line number */
49 const char *infilename; /* input filename */
52 static const char *outfiles[NFILES]; /* output file names */
55 FILE *fout; /* file pointer of current output */
56 FILE *fin; /* file pointer of current input */
58 list *defined; /* list of defined things */
60 static void printwhere( void );
63 * Reinitialize the world
68 memset(curline, 0, MAXLINESIZE);
78 streq(const char *a, const char *b)
80 return (strcmp(a, b) == 0);
84 * find a value in a list
87 findval(list *lst, const char *val, int (*cmp)(definition *, const char *))
89 for (; lst != NULL; lst = lst->next) {
90 if ((*cmp) (lst->val, val)) {
98 * store a value in a list
101 storeval(list **lstp, definition *val)
106 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
114 findit(definition *def, const char *type)
116 return (streq(def->def_name, type));
120 fixit(const char *type, const char *orig)
124 def = (definition *) FINDVAL(defined, type, findit);
125 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
128 switch (def->def.ty.rel) {
130 if (streq(def->def.ty.old_type, "opaque"))
133 return (def->def.ty.old_type);
136 return (fixit(def->def.ty.old_type, orig));
143 fixtype(const char *type)
145 return (fixit(type, type));
149 stringfix(const char *type)
151 if (streq(type, "string")) {
152 return ("wrapstring");
159 ptype(const char *prefix, const char *type, int follow)
161 if (prefix != NULL) {
162 if (streq(prefix, "enum")) {
163 f_print(fout, "enum ");
165 f_print(fout, "struct ");
168 if (streq(type, "bool")) {
169 f_print(fout, "bool_t ");
170 } else if (streq(type, "string")) {
171 f_print(fout, "char *");
173 f_print(fout, "%s ", follow ? fixtype(type) : type);
178 typedefed(definition *def, const char *type)
180 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
183 return (streq(def->def_name, type));
188 isvectordef(const char *type, relation rel)
195 return (!streq(type, "string"));
201 def = (definition *) FINDVAL(defined, type, typedefed);
205 type = def->def.ty.old_type;
206 rel = def->def.ty.rel;
214 locase(const char *str)
217 static char buf[100];
220 while ( (c = *str++) ) {
221 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
228 pvname_svc(const char *pname, const char *vnum)
230 f_print(fout, "%s_%s_svc", locase(pname), vnum);
234 pvname(const char *pname, const char *vnum)
236 f_print(fout, "%s_%s", locase(pname), vnum);
240 * print a useful (?) error message, and then die
243 error(const char *msg)
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++) {
260 (void) unlink(outfiles[i]);
266 record_open(const char *file)
268 if (nfiles < NFILES) {
269 outfiles[nfiles++] = file;
271 warnx("too many files");
276 static char expectbuf[100];
277 static const char *toktostr(tok_kind kind);
280 * error, token encountered was not the expected one
283 expected1(tok_kind exp1)
285 s_print(expectbuf, "expected '%s'",
291 * error, token encountered was not one of two expected ones
294 expected2(tok_kind exp1, tok_kind exp2)
296 s_print(expectbuf, "expected '%s' or '%s'",
303 * error, token encountered was not one of 3 expected ones
306 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
308 s_print(expectbuf, "expected '%s', '%s' or '%s'",
316 tabify(FILE *f, int tab)
319 (void) fputc('\t', f);
324 static token tokstrings[] = {
325 {TOK_IDENT, "identifier"},
326 {TOK_CONST, "const"},
337 {TOK_SEMICOLON, ";"},
338 {TOK_UNION, "union"},
339 {TOK_STRUCT, "struct"},
340 {TOK_SWITCH, "switch"},
342 {TOK_DEFAULT, "default"},
344 {TOK_TYPEDEF, "typedef"},
346 {TOK_SHORT, "short"},
348 {TOK_UNSIGNED, "unsigned"},
349 {TOK_DOUBLE, "double"},
350 {TOK_FLOAT, "float"},
352 {TOK_STRING, "string"},
353 {TOK_OPAQUE, "opaque"},
356 {TOK_PROGRAM, "program"},
357 {TOK_VERSION, "version"},
362 toktostr(tok_kind kind)
366 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
379 for (i = 0; (c = curline[i]); i++) {
381 cnt = 8 - (i % TABSIZE);
387 (void) fputc(c, stderr);
400 for (i = 0; i < where - curline; i++) {
403 cnt = 8 - (i % TABSIZE);
408 (void) fputc('^', stderr);
411 (void) fputc('\n', stderr);
415 make_argname(const char *pname, const char *vname)
419 name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
420 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
424 bas_type *typ_list_h;
425 bas_type *typ_list_t;
428 add_type(int len, const char *type)
432 ptr = XALLOC(bas_type);
437 if (typ_list_t == NULL)
445 typ_list_t->next = ptr;
452 find_type(const char *type)
459 if (strcmp(ptr->name, type) == 0)
472 if ((p = malloc(size)) == NULL) {
473 warnx("malloc failed");
480 xrealloc(void *ptr, size_t size)
484 if ((p = realloc(ptr, size)) == NULL) {
485 warnx("realloc failed");
492 xstrdup(const char *str)
496 if ((p = strdup(str)) == NULL) {
497 warnx("strdup failed");