2 * From: @(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * rpc_util.c, Utility routines for the RPC protocol compiler
40 #include "rpc_parse.h"
44 #define ARGEXT "argument"
46 char curline[MAXLINESIZE]; /* current read line */
47 const char *where = curline; /* current point in line */
48 int linenum = 0; /* current line number */
50 const char *infilename; /* input filename */
53 const char *outfiles[NFILES]; /* output file names */
56 FILE *fout; /* file pointer of current output */
57 FILE *fin; /* file pointer of current input */
59 list *defined; /* list of defined things */
61 static int findit (const definition * def, const char *type);
62 static const char *fixit (const char *type, const char *orig);
63 static int typedefed (const definition * def, const char *type);
64 static const char *toktostr (tok_kind kind);
65 static void printbuf (void);
66 static void printwhere (void);
69 * Reinitialize the world
74 memset (curline, 0, MAXLINESIZE);
84 streq (const char *a, const char *b)
86 return strcmp (a, b) == 0;
90 * find a value in a list
93 findval (list *lst, const char *val,
94 int (*cmp) (const definition *, const char *))
97 for (; lst != NULL; lst = lst->next)
99 if (cmp (lst->val, val))
108 * store a value in a list
111 storeval (list **lstp, definition *val)
117 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
125 findit (const definition * def, const char *type)
127 return streq (def->def_name, type);
131 fixit (const char *type, const char *orig)
135 def = findval (defined, type, findit);
136 if (def == NULL || def->def_kind != DEF_TYPEDEF)
140 switch (def->def.ty.rel)
143 if (streq (def->def.ty.old_type, "opaque"))
146 return (def->def.ty.old_type);
148 return (fixit (def->def.ty.old_type, orig));
155 fixtype (const char *type)
157 return fixit (type, type);
161 stringfix (const char *type)
163 if (streq (type, "string"))
174 ptype (const char *prefix, const char *type, int follow)
178 if (streq (prefix, "enum"))
180 f_print (fout, "enum ");
184 f_print (fout, "struct ");
187 if (streq (type, "bool"))
189 f_print (fout, "bool_t ");
191 else if (streq (type, "string"))
193 f_print (fout, "char *");
197 f_print (fout, "%s ", follow ? fixtype (type) : type);
202 typedefed (const definition * def, const char *type)
204 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
210 return streq (def->def_name, type);
215 isvectordef (const char *type, relation rel)
224 return !streq (type, "string");
230 def = findval (defined, type, typedefed);
235 type = def->def.ty.old_type;
236 rel = def->def.ty.rel;
242 locase (const char *str)
245 static char buf[100];
248 while ((c = *str++) != 0)
250 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
257 pvname_svc (const char *pname, const char *vnum)
259 f_print (fout, "%s_%s_svc", locase (pname), vnum);
263 pvname (const char *pname, const char *vnum)
265 f_print (fout, "%s_%s", locase (pname), vnum);
269 * print a useful (?) error message, and then die
272 error (const char *msg)
275 f_print (stderr, "%s, line %d: ", infilename, linenum);
276 f_print (stderr, "%s\n", msg);
281 * Something went wrong, unlink any files that we may have created and then
289 for (i = 0; i < nfiles; i++)
291 unlink (outfiles[i]);
297 record_open (const char *file)
301 outfiles[nfiles++] = file;
305 f_print (stderr, "too many files!\n");
310 static char expectbuf[100];
313 * error, token encountered was not the expected one
316 expected1 (tok_kind exp1)
318 s_print (expectbuf, "expected '%s'",
324 * error, token encountered was not one of two expected ones
327 expected2 (tok_kind exp1, tok_kind exp2)
329 s_print (expectbuf, "expected '%s' or '%s'",
336 * error, token encountered was not one of 3 expected ones
339 expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
341 s_print (expectbuf, "expected '%s', '%s' or '%s'",
349 tabify (FILE * f, int tab)
353 (void) fputc ('\t', f);
358 static const token tokstrings[] =
360 {TOK_IDENT, "identifier"},
361 {TOK_CONST, "const"},
372 {TOK_SEMICOLON, ";"},
373 {TOK_UNION, "union"},
374 {TOK_STRUCT, "struct"},
375 {TOK_SWITCH, "switch"},
377 {TOK_DEFAULT, "default"},
379 {TOK_TYPEDEF, "typedef"},
381 {TOK_SHORT, "short"},
383 {TOK_UNSIGNED, "unsigned"},
384 {TOK_DOUBLE, "double"},
385 {TOK_FLOAT, "float"},
387 {TOK_STRING, "string"},
388 {TOK_OPAQUE, "opaque"},
391 {TOK_PROGRAM, "program"},
392 {TOK_VERSION, "version"},
397 toktostr (tok_kind kind)
401 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
414 for (i = 0; (c = curline[i]) != 0; i++)
418 cnt = 8 - (i % TABSIZE);
427 (void) fputc (c, stderr);
440 for (i = 0; i < where - curline; i++)
445 cnt = 8 - (i % TABSIZE);
453 (void) fputc ('^', stderr);
456 (void) fputc ('\n', stderr);
460 make_argname (const char *pname, const char *vname)
464 name = malloc (strlen (pname) + strlen (vname) + strlen (ARGEXT) + 3);
467 fprintf (stderr, "failed in malloc");
470 sprintf (name, "%s_%s_%s", locase (pname), vname, ARGEXT);
474 bas_type *typ_list_h;
475 bas_type *typ_list_t;
478 add_type (int len, const char *type)
483 if ((ptr = malloc (sizeof (bas_type))) == NULL)
485 fprintf (stderr, "failed in malloc");
492 if (typ_list_t == NULL)
501 typ_list_t->next = ptr;
509 find_type (const char *type)
518 if (strcmp (ptr->name, type) == 0)