2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
34 * rpc_util.c, Utility routines for the RPC protocol compiler
41 #include "rpc_parse.h"
44 static void printwhere(void);
47 #define ARGEXT "argument"
49 char curline[MAXLINESIZE]; /* current read line */
50 char *where = curline; /* current point in line */
51 int linenum = 0; /* current line number */
53 char *infilename; /* input filename */
56 char *outfiles[NFILES]; /* output file names */
59 FILE *fout; /* file pointer of current output */
60 FILE *fin; /* file pointer of current input */
62 list *defined; /* list of defined things */
65 * Reinitialize the world
70 memset(curline, 0, MAXLINESIZE);
80 streq(char *a, char *b)
82 return (strcmp(a, b) == 0);
86 * find a value in a list
89 findval(list *lst, char *val, int (*cmp)(definition *, char *))
92 for (; lst != NULL; lst = lst->next) {
93 if ((*cmp) (lst->val, val)) {
101 * store a value in a list
112 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) {
134 switch (def->def.ty.rel) {
136 return (def->def.ty.old_type);
138 return (fixit(def->def.ty.old_type, orig));
147 return (fixit(type, type));
151 stringfix(char *type)
153 if (streq(type, "string")) {
154 return ("wrapstring");
161 ptype(char *prefix, char *type, int follow)
163 if (prefix != NULL) {
164 if (streq(prefix, "enum")) {
165 f_print(fout, "enum ");
167 f_print(fout, "struct ");
170 if (streq(type, "bool")) {
171 f_print(fout, "bool_t ");
172 } else if (streq(type, "string")) {
173 f_print(fout, "char *");
175 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));
190 isvectordef(char *type, relation rel)
197 return (!streq(type, "string"));
203 def = (definition *) FINDVAL(defined, type, typedefed);
207 type = def->def.ty.old_type;
208 rel = def->def.ty.rel;
217 static char buf[100];
220 while ((c = *str++) != '\0') {
221 *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 f_print(stderr, "%s, line %d: ", infilename, linenum);
247 f_print(stderr, "%s\n", msg);
252 * Something went wrong, unlink any files that we may have created and then
260 for (i = 0; i < nfiles; i++) {
261 (void) unlink(outfiles[i]);
267 record_open(char *file)
269 if (nfiles < NFILES) {
270 outfiles[nfiles++] = file;
272 f_print(stderr, "too many files!\n");
277 static char expectbuf[100];
278 static char *toktostr();
281 * error, token encountered was not the expected one
287 s_print(expectbuf, "expected '%s'",
293 * error, token encountered was not one of two expected ones
296 expected2(exp1, exp2)
299 s_print(expectbuf, "expected '%s' or '%s'",
306 * error, token encountered was not one of 3 expected ones
309 expected3(exp1, exp2, exp3)
310 tok_kind exp1, exp2, exp3;
312 s_print(expectbuf, "expected '%s', '%s' or '%s'",
325 (void) fputc('\t', f);
330 static token tokstrings[] = {
331 {TOK_IDENT, "identifier"},
332 {TOK_CONST, "const"},
343 {TOK_SEMICOLON, ";"},
344 {TOK_UNION, "union"},
345 {TOK_STRUCT, "struct"},
346 {TOK_SWITCH, "switch"},
348 {TOK_DEFAULT, "default"},
350 {TOK_TYPEDEF, "typedef"},
352 {TOK_SHORT, "short"},
353 {TOK_INT32, "int32"},
354 {TOK_UNSIGNED, "unsigned"},
355 {TOK_DOUBLE, "double"},
356 {TOK_FLOAT, "float"},
358 {TOK_STRING, "string"},
359 {TOK_OPAQUE, "opaque"},
362 {TOK_PROGRAM, "program"},
363 {TOK_VERSION, "version"},
373 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
386 for (i = 0; (c = curline[i]) != '\0'; i++) {
388 cnt = 8 - (i % TABSIZE);
394 (void) fputc(c, stderr);
407 for (i = 0; i < where - curline; i++) {
410 cnt = 8 - (i % TABSIZE);
415 (void) fputc('^', stderr);
418 (void) fputc('\n', stderr);
422 make_argname(char *pname, char *vname)
426 name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
428 fprintf(stderr, "failed in malloc");
431 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
435 bas_type *typ_list_h;
436 bas_type *typ_list_t;
439 add_type(int len, char *type)
444 if ((ptr = (bas_type *) malloc(sizeof(bas_type))) == (bas_type *) NULL) {
445 fprintf(stderr, "failed in malloc");
451 if (typ_list_t == NULL) {
457 typ_list_t->next = ptr;
464 find_type(char *type)
471 while (ptr != NULL) {
472 if (strcmp(ptr->name, type) == 0)