Update.
[glibc/history.git] / sunrpc / rpc_util.c
bloba5fa727afe14afd13a23a858f6f0b38aa82c90ee
1 /*
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 or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From: @(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI
34 char util_rcsid[] =
35 "$Id$";
38 * rpc_util.c, Utility routines for the RPC protocol compiler
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include "rpc_scan.h"
45 #include "rpc_parse.h"
46 #include "rpc_util.h"
47 #include "proto.h"
49 #define ARGEXT "argument"
51 char curline[MAXLINESIZE]; /* current read line */
52 const char *where = curline; /* current point in line */
53 int linenum = 0; /* current line number */
55 const char *infilename; /* input filename */
57 #define NFILES 7
58 const char *outfiles[NFILES]; /* output file names */
59 int nfiles;
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 int findit (const definition * def, const char *type);
67 static const char *fixit (const char *type, const char *orig);
68 static int typedefed (const definition * def, const char *type);
69 static const char *toktostr (tok_kind kind);
70 static void printbuf (void);
71 static void printwhere (void);
74 * Reinitialize the world
76 void
77 reinitialize (void)
79 memset (curline, 0, MAXLINESIZE);
80 where = curline;
81 linenum = 0;
82 defined = NULL;
86 * string equality
88 int
89 streq (const char *a, const char *b)
91 return strcmp (a, b) == 0;
95 * find a value in a list
97 definition *
98 findval (list * lst, const char *val,
99 int (*cmp) (const definition *, const char *))
102 for (; lst != NULL; lst = lst->next)
104 if (cmp (lst->val, val))
106 return lst->val;
109 return NULL;
113 * store a value in a list
115 void
116 storeval (list ** lstp, definition * val)
118 list **l;
119 list *lst;
122 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
123 lst = ALLOC (list);
124 lst->val = val;
125 lst->next = NULL;
126 *l = lst;
129 static int
130 findit (const definition * def, const char *type)
132 return streq (def->def_name, type);
135 static const char *
136 fixit (const char *type, const char *orig)
138 definition *def;
140 def = findval (defined, type, findit);
141 if (def == NULL || def->def_kind != DEF_TYPEDEF)
143 return orig;
145 switch (def->def.ty.rel)
147 case REL_VECTOR:
148 return (def->def.ty.old_type);
149 case REL_ALIAS:
150 return (fixit (def->def.ty.old_type, orig));
151 default:
152 return orig;
156 const char *
157 fixtype (const char *type)
159 return fixit (type, type);
162 const char *
163 stringfix (const char *type)
165 if (streq (type, "string"))
167 return "wrapstring";
169 else
171 return type;
175 void
176 ptype (const char *prefix, const char *type, int follow)
178 if (prefix != NULL)
180 if (streq (prefix, "enum"))
182 f_print (fout, "enum ");
184 else
186 f_print (fout, "struct ");
189 if (streq (type, "bool"))
191 f_print (fout, "bool_t ");
193 else if (streq (type, "string"))
195 f_print (fout, "char *");
197 else
199 f_print (fout, "%s ", follow ? fixtype (type) : type);
203 static int
204 typedefed (const definition * def, const char *type)
206 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
208 return 0;
210 else
212 return streq (def->def_name, type);
217 isvectordef (const char *type, relation rel)
219 definition *def;
221 for (;;)
223 switch (rel)
225 case REL_VECTOR:
226 return !streq (type, "string");
227 case REL_ARRAY:
228 return 0;
229 case REL_POINTER:
230 return (0);
231 case REL_ALIAS:
232 def = findval (defined, type, typedefed);
233 if (def == NULL)
235 return 0;
237 type = def->def.ty.old_type;
238 rel = def->def.ty.rel;
243 char *
244 locase (const char *str)
246 char c;
247 static char buf[100];
248 char *p = buf;
250 while ((c = *str++) != 0)
252 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
254 *p = 0;
255 return buf;
258 void
259 pvname_svc (const char *pname, const char *vnum)
261 f_print (fout, "%s_%s_svc", locase (pname), vnum);
264 void
265 pvname (const char *pname, const char *vnum)
267 f_print (fout, "%s_%s", locase (pname), vnum);
271 * print a useful (?) error message, and then die
273 void
274 error (const char *msg)
276 printwhere ();
277 f_print (stderr, "%s, line %d: ", infilename, linenum);
278 f_print (stderr, "%s\n", msg);
279 crash ();
283 * Something went wrong, unlink any files that we may have created and then
284 * die.
286 void
287 crash (void)
289 int i;
291 for (i = 0; i < nfiles; i++)
293 (void) unlink (outfiles[i]);
295 exit (1);
298 void
299 record_open (const char *file)
301 if (nfiles < NFILES)
303 outfiles[nfiles++] = file;
305 else
307 f_print (stderr, "too many files!\n");
308 crash ();
312 static char expectbuf[100];
315 * error, token encountered was not the expected one
317 void
318 expected1 (tok_kind exp1)
320 s_print (expectbuf, "expected '%s'",
321 toktostr (exp1));
322 error (expectbuf);
326 * error, token encountered was not one of two expected ones
328 void
329 expected2 (tok_kind exp1, tok_kind exp2)
331 s_print (expectbuf, "expected '%s' or '%s'",
332 toktostr (exp1),
333 toktostr (exp2));
334 error (expectbuf);
338 * error, token encountered was not one of 3 expected ones
340 void
341 expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
343 s_print (expectbuf, "expected '%s', '%s' or '%s'",
344 toktostr (exp1),
345 toktostr (exp2),
346 toktostr (exp3));
347 error (expectbuf);
350 void
351 tabify (FILE * f, int tab)
353 while (tab--)
355 (void) fputc ('\t', f);
360 static const token tokstrings[] =
362 {TOK_IDENT, "identifier"},
363 {TOK_CONST, "const"},
364 {TOK_RPAREN, ")"},
365 {TOK_LPAREN, "("},
366 {TOK_RBRACE, "}"},
367 {TOK_LBRACE, "{"},
368 {TOK_LBRACKET, "["},
369 {TOK_RBRACKET, "]"},
370 {TOK_STAR, "*"},
371 {TOK_COMMA, ","},
372 {TOK_EQUAL, "="},
373 {TOK_COLON, ":"},
374 {TOK_SEMICOLON, ";"},
375 {TOK_UNION, "union"},
376 {TOK_STRUCT, "struct"},
377 {TOK_SWITCH, "switch"},
378 {TOK_CASE, "case"},
379 {TOK_DEFAULT, "default"},
380 {TOK_ENUM, "enum"},
381 {TOK_TYPEDEF, "typedef"},
382 {TOK_INT, "int"},
383 {TOK_SHORT, "short"},
384 {TOK_LONG, "long"},
385 {TOK_UNSIGNED, "unsigned"},
386 {TOK_DOUBLE, "double"},
387 {TOK_FLOAT, "float"},
388 {TOK_CHAR, "char"},
389 {TOK_STRING, "string"},
390 {TOK_OPAQUE, "opaque"},
391 {TOK_BOOL, "bool"},
392 {TOK_VOID, "void"},
393 {TOK_PROGRAM, "program"},
394 {TOK_VERSION, "version"},
395 {TOK_EOF, "??????"}
398 static const char *
399 toktostr (tok_kind kind)
401 token *sp;
403 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
404 return (sp->str);
407 static void
408 printbuf (void)
410 char c;
411 int i;
412 int cnt;
414 #define TABSIZE 4
416 for (i = 0; (c = curline[i]) != 0; i++)
418 if (c == '\t')
420 cnt = 8 - (i % TABSIZE);
421 c = ' ';
423 else
425 cnt = 1;
427 while (cnt--)
429 (void) fputc (c, stderr);
434 static void
435 printwhere (void)
437 int i;
438 char c;
439 int cnt;
441 printbuf ();
442 for (i = 0; i < where - curline; i++)
444 c = curline[i];
445 if (c == '\t')
447 cnt = 8 - (i % TABSIZE);
449 else
451 cnt = 1;
453 while (cnt--)
455 (void) fputc ('^', stderr);
458 (void) fputc ('\n', stderr);
461 char *
462 make_argname (const char *pname, const char *vname)
464 char *name;
466 name = malloc (strlen (pname) + strlen (vname) + strlen (ARGEXT) + 3);
467 if (!name)
469 fprintf (stderr, "failed in malloc");
470 exit (1);
472 sprintf (name, "%s_%s_%s", locase (pname), vname, ARGEXT);
473 return name;
476 bas_type *typ_list_h;
477 bas_type *typ_list_t;
479 void
480 add_type (int len, const char *type)
482 bas_type *ptr;
485 if ((ptr = malloc (sizeof (bas_type))) == NULL)
487 fprintf (stderr, "failed in malloc");
488 exit (1);
491 ptr->name = type;
492 ptr->length = len;
493 ptr->next = NULL;
494 if (typ_list_t == NULL)
497 typ_list_t = ptr;
498 typ_list_h = ptr;
500 else
503 typ_list_t->next = ptr;
504 typ_list_t = ptr;
510 bas_type *
511 find_type (const char *type)
513 bas_type *ptr;
515 ptr = typ_list_h;
518 while (ptr != NULL)
520 if (strcmp (ptr->name, type) == 0)
521 return ptr;
522 else
523 ptr = ptr->next;
525 return NULL;