(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sunrpc / rpc_util.c
blob31e1d3143c19e9235018f12e0af58bc9a02d557e
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 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char util_rcsid[] =
36 "$Id$";
37 #endif
40 * rpc_util.c, Utility routines for the RPC protocol compiler
42 #include <stdio.h>
43 #include <ctype.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include "rpc_scan.h"
47 #include "rpc_parse.h"
48 #include "rpc_util.h"
49 #include "proto.h"
51 #define ARGEXT "argument"
53 char curline[MAXLINESIZE]; /* current read line */
54 const char *where = curline; /* current point in line */
55 int linenum = 0; /* current line number */
57 const char *infilename; /* input filename */
59 #define NFILES 7
60 const char *outfiles[NFILES]; /* output file names */
61 int nfiles;
63 FILE *fout; /* file pointer of current output */
64 FILE *fin; /* file pointer of current input */
66 list *defined; /* list of defined things */
68 static int findit (const definition * def, const char *type);
69 static const char *fixit (const char *type, const char *orig);
70 static int typedefed (const definition * def, const char *type);
71 static const char *toktostr (tok_kind kind);
72 static void printbuf (void);
73 static void printwhere (void);
76 * Reinitialize the world
78 void
79 reinitialize (void)
81 memset (curline, 0, MAXLINESIZE);
82 where = curline;
83 linenum = 0;
84 defined = NULL;
88 * string equality
90 int
91 streq (const char *a, const char *b)
93 return strcmp (a, b) == 0;
97 * find a value in a list
99 definition *
100 findval (list *lst, const char *val,
101 int (*cmp) (const definition *, const char *))
104 for (; lst != NULL; lst = lst->next)
106 if (cmp (lst->val, val))
108 return lst->val;
111 return NULL;
115 * store a value in a list
117 void
118 storeval (list **lstp, definition *val)
120 list **l;
121 list *lst;
124 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
125 lst = ALLOC (list);
126 lst->val = val;
127 lst->next = NULL;
128 *l = lst;
131 static int
132 findit (const definition * def, const char *type)
134 return streq (def->def_name, type);
137 static const char *
138 fixit (const char *type, const char *orig)
140 definition *def;
142 def = findval (defined, type, findit);
143 if (def == NULL || def->def_kind != DEF_TYPEDEF)
145 return orig;
147 switch (def->def.ty.rel)
149 case REL_VECTOR:
150 if (streq (def->def.ty.old_type, "opaque"))
151 return ("char");
152 else
153 return (def->def.ty.old_type);
154 case REL_ALIAS:
155 return (fixit (def->def.ty.old_type, orig));
156 default:
157 return orig;
161 const char *
162 fixtype (const char *type)
164 return fixit (type, type);
167 const char *
168 stringfix (const char *type)
170 if (streq (type, "string"))
172 return "wrapstring";
174 else
176 return type;
180 void
181 ptype (const char *prefix, const char *type, int follow)
183 if (prefix != NULL)
185 if (streq (prefix, "enum"))
187 f_print (fout, "enum ");
189 else
191 f_print (fout, "struct ");
194 if (streq (type, "bool"))
196 f_print (fout, "bool_t ");
198 else if (streq (type, "string"))
200 f_print (fout, "char *");
202 else
204 f_print (fout, "%s ", follow ? fixtype (type) : type);
208 static int
209 typedefed (const definition * def, const char *type)
211 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
213 return 0;
215 else
217 return streq (def->def_name, type);
222 isvectordef (const char *type, relation rel)
224 definition *def;
226 for (;;)
228 switch (rel)
230 case REL_VECTOR:
231 return !streq (type, "string");
232 case REL_ARRAY:
233 return 0;
234 case REL_POINTER:
235 return 0;
236 case REL_ALIAS:
237 def = findval (defined, type, typedefed);
238 if (def == NULL)
240 return 0;
242 type = def->def.ty.old_type;
243 rel = def->def.ty.rel;
248 char *
249 locase (const char *str)
251 char c;
252 static char buf[100];
253 char *p = buf;
255 while ((c = *str++) != 0)
257 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
259 *p = 0;
260 return buf;
263 void
264 pvname_svc (const char *pname, const char *vnum)
266 f_print (fout, "%s_%s_svc", locase (pname), vnum);
269 void
270 pvname (const char *pname, const char *vnum)
272 f_print (fout, "%s_%s", locase (pname), vnum);
276 * print a useful (?) error message, and then die
278 void
279 error (const char *msg)
281 printwhere ();
282 f_print (stderr, "%s, line %d: ", infilename, linenum);
283 f_print (stderr, "%s\n", msg);
284 crash ();
288 * Something went wrong, unlink any files that we may have created and then
289 * die.
291 void
292 crash (void)
294 int i;
296 for (i = 0; i < nfiles; i++)
298 unlink (outfiles[i]);
300 exit (1);
303 void
304 record_open (const char *file)
306 if (nfiles < NFILES)
308 outfiles[nfiles++] = file;
310 else
312 f_print (stderr, "too many files!\n");
313 crash ();
317 static char expectbuf[100];
320 * error, token encountered was not the expected one
322 void
323 expected1 (tok_kind exp1)
325 s_print (expectbuf, "expected '%s'",
326 toktostr (exp1));
327 error (expectbuf);
331 * error, token encountered was not one of two expected ones
333 void
334 expected2 (tok_kind exp1, tok_kind exp2)
336 s_print (expectbuf, "expected '%s' or '%s'",
337 toktostr (exp1),
338 toktostr (exp2));
339 error (expectbuf);
343 * error, token encountered was not one of 3 expected ones
345 void
346 expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
348 s_print (expectbuf, "expected '%s', '%s' or '%s'",
349 toktostr (exp1),
350 toktostr (exp2),
351 toktostr (exp3));
352 error (expectbuf);
355 void
356 tabify (FILE * f, int tab)
358 while (tab--)
360 (void) fputc ('\t', f);
365 static const token tokstrings[] =
367 {TOK_IDENT, "identifier"},
368 {TOK_CONST, "const"},
369 {TOK_RPAREN, ")"},
370 {TOK_LPAREN, "("},
371 {TOK_RBRACE, "}"},
372 {TOK_LBRACE, "{"},
373 {TOK_LBRACKET, "["},
374 {TOK_RBRACKET, "]"},
375 {TOK_STAR, "*"},
376 {TOK_COMMA, ","},
377 {TOK_EQUAL, "="},
378 {TOK_COLON, ":"},
379 {TOK_SEMICOLON, ";"},
380 {TOK_UNION, "union"},
381 {TOK_STRUCT, "struct"},
382 {TOK_SWITCH, "switch"},
383 {TOK_CASE, "case"},
384 {TOK_DEFAULT, "default"},
385 {TOK_ENUM, "enum"},
386 {TOK_TYPEDEF, "typedef"},
387 {TOK_INT, "int"},
388 {TOK_SHORT, "short"},
389 {TOK_LONG, "long"},
390 {TOK_UNSIGNED, "unsigned"},
391 {TOK_DOUBLE, "double"},
392 {TOK_FLOAT, "float"},
393 {TOK_CHAR, "char"},
394 {TOK_STRING, "string"},
395 {TOK_OPAQUE, "opaque"},
396 {TOK_BOOL, "bool"},
397 {TOK_VOID, "void"},
398 {TOK_PROGRAM, "program"},
399 {TOK_VERSION, "version"},
400 {TOK_EOF, "??????"}
403 static const char *
404 toktostr (tok_kind kind)
406 const token *sp;
408 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
409 return sp->str;
412 static void
413 printbuf (void)
415 char c;
416 int i;
417 int cnt;
419 #define TABSIZE 4
421 for (i = 0; (c = curline[i]) != 0; i++)
423 if (c == '\t')
425 cnt = 8 - (i % TABSIZE);
426 c = ' ';
428 else
430 cnt = 1;
432 while (cnt--)
434 (void) fputc (c, stderr);
439 static void
440 printwhere (void)
442 int i;
443 char c;
444 int cnt;
446 printbuf ();
447 for (i = 0; i < where - curline; i++)
449 c = curline[i];
450 if (c == '\t')
452 cnt = 8 - (i % TABSIZE);
454 else
456 cnt = 1;
458 while (cnt--)
460 (void) fputc ('^', stderr);
463 (void) fputc ('\n', stderr);
466 char *
467 make_argname (const char *pname, const char *vname)
469 char *name;
471 name = malloc (strlen (pname) + strlen (vname) + strlen (ARGEXT) + 3);
472 if (!name)
474 fprintf (stderr, "failed in malloc");
475 exit (1);
477 sprintf (name, "%s_%s_%s", locase (pname), vname, ARGEXT);
478 return name;
481 bas_type *typ_list_h;
482 bas_type *typ_list_t;
484 void
485 add_type (int len, const char *type)
487 bas_type *ptr;
490 if ((ptr = malloc (sizeof (bas_type))) == NULL)
492 fprintf (stderr, "failed in malloc");
493 exit (1);
496 ptr->name = type;
497 ptr->length = len;
498 ptr->next = NULL;
499 if (typ_list_t == NULL)
502 typ_list_t = ptr;
503 typ_list_h = ptr;
505 else
508 typ_list_t->next = ptr;
509 typ_list_t = ptr;
515 bas_type *
516 find_type (const char *type)
518 bas_type *ptr;
520 ptr = typ_list_h;
523 while (ptr != NULL)
525 if (strcmp (ptr->name, type) == 0)
526 return ptr;
527 else
528 ptr = ptr->next;
530 return NULL;