* sysdeps/posix/getaddrinfo.c (rfc3484_sort): Store result of
[glibc.git] / sunrpc / rpc_util.c
blobb910401a313cfa0a842ca785381815363c7ad7de
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
36 * rpc_util.c, Utility routines for the RPC protocol compiler
38 #include <stdio.h>
39 #include <ctype.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "rpc_scan.h"
43 #include "rpc_parse.h"
44 #include "rpc_util.h"
45 #include "proto.h"
47 #define ARGEXT "argument"
49 char curline[MAXLINESIZE]; /* current read line */
50 const char *where = curline; /* current point in line */
51 int linenum = 0; /* current line number */
53 const char *infilename; /* input filename */
55 #define NFILES 7
56 const char *outfiles[NFILES]; /* output file names */
57 int nfiles;
59 FILE *fout; /* file pointer of current output */
60 FILE *fin; /* file pointer of current input */
62 list *defined; /* list of defined things */
64 static int findit (const definition * def, const char *type);
65 static const char *fixit (const char *type, const char *orig);
66 static int typedefed (const definition * def, const char *type);
67 static const char *toktostr (tok_kind kind);
68 static void printbuf (void);
69 static void printwhere (void);
72 * Reinitialize the world
74 void
75 reinitialize (void)
77 memset (curline, 0, MAXLINESIZE);
78 where = curline;
79 linenum = 0;
80 defined = NULL;
84 * string equality
86 int
87 streq (const char *a, const char *b)
89 return strcmp (a, b) == 0;
93 * find a value in a list
95 definition *
96 findval (list *lst, const char *val,
97 int (*cmp) (const definition *, const char *))
100 for (; lst != NULL; lst = lst->next)
102 if (cmp (lst->val, val))
104 return lst->val;
107 return NULL;
111 * store a value in a list
113 void
114 storeval (list **lstp, definition *val)
116 list **l;
117 list *lst;
120 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
121 lst = ALLOC (list);
122 lst->val = val;
123 lst->next = NULL;
124 *l = lst;
127 static int
128 findit (const definition * def, const char *type)
130 return streq (def->def_name, type);
133 static const char *
134 fixit (const char *type, const char *orig)
136 definition *def;
138 def = findval (defined, type, findit);
139 if (def == NULL || def->def_kind != DEF_TYPEDEF)
141 return orig;
143 switch (def->def.ty.rel)
145 case REL_VECTOR:
146 if (streq (def->def.ty.old_type, "opaque"))
147 return ("char");
148 else
149 return (def->def.ty.old_type);
150 case REL_ALIAS:
151 return (fixit (def->def.ty.old_type, orig));
152 default:
153 return orig;
157 const char *
158 fixtype (const char *type)
160 return fixit (type, type);
163 const char *
164 stringfix (const char *type)
166 if (streq (type, "string"))
168 return "wrapstring";
170 else
172 return type;
176 void
177 ptype (const char *prefix, const char *type, int follow)
179 if (prefix != NULL)
181 if (streq (prefix, "enum"))
183 f_print (fout, "enum ");
185 else
187 f_print (fout, "struct ");
190 if (streq (type, "bool"))
192 f_print (fout, "bool_t ");
194 else if (streq (type, "string"))
196 f_print (fout, "char *");
198 else
200 f_print (fout, "%s ", follow ? fixtype (type) : type);
204 static int
205 typedefed (const definition * def, const char *type)
207 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
209 return 0;
211 else
213 return streq (def->def_name, type);
218 isvectordef (const char *type, relation rel)
220 definition *def;
222 for (;;)
224 switch (rel)
226 case REL_VECTOR:
227 return !streq (type, "string");
228 case REL_ARRAY:
229 return 0;
230 case REL_POINTER:
231 return 0;
232 case REL_ALIAS:
233 def = findval (defined, type, typedefed);
234 if (def == NULL)
236 return 0;
238 type = def->def.ty.old_type;
239 rel = def->def.ty.rel;
244 char *
245 locase (const char *str)
247 char c;
248 static char buf[100];
249 char *p = buf;
251 while ((c = *str++) != 0)
253 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
255 *p = 0;
256 return buf;
259 void
260 pvname_svc (const char *pname, const char *vnum)
262 f_print (fout, "%s_%s_svc", locase (pname), vnum);
265 void
266 pvname (const char *pname, const char *vnum)
268 f_print (fout, "%s_%s", locase (pname), vnum);
272 * print a useful (?) error message, and then die
274 void
275 error (const char *msg)
277 printwhere ();
278 f_print (stderr, "%s, line %d: ", infilename, linenum);
279 f_print (stderr, "%s\n", msg);
280 crash ();
284 * Something went wrong, unlink any files that we may have created and then
285 * die.
287 void
288 crash (void)
290 int i;
292 for (i = 0; i < nfiles; i++)
294 unlink (outfiles[i]);
296 exit (1);
299 void
300 record_open (const char *file)
302 if (nfiles < NFILES)
304 outfiles[nfiles++] = file;
306 else
308 f_print (stderr, "too many files!\n");
309 crash ();
313 static char expectbuf[100];
316 * error, token encountered was not the expected one
318 void
319 expected1 (tok_kind exp1)
321 s_print (expectbuf, "expected '%s'",
322 toktostr (exp1));
323 error (expectbuf);
327 * error, token encountered was not one of two expected ones
329 void
330 expected2 (tok_kind exp1, tok_kind exp2)
332 s_print (expectbuf, "expected '%s' or '%s'",
333 toktostr (exp1),
334 toktostr (exp2));
335 error (expectbuf);
339 * error, token encountered was not one of 3 expected ones
341 void
342 expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
344 s_print (expectbuf, "expected '%s', '%s' or '%s'",
345 toktostr (exp1),
346 toktostr (exp2),
347 toktostr (exp3));
348 error (expectbuf);
351 void
352 tabify (FILE * f, int tab)
354 while (tab--)
356 (void) fputc ('\t', f);
361 static const token tokstrings[] =
363 {TOK_IDENT, "identifier"},
364 {TOK_CONST, "const"},
365 {TOK_RPAREN, ")"},
366 {TOK_LPAREN, "("},
367 {TOK_RBRACE, "}"},
368 {TOK_LBRACE, "{"},
369 {TOK_LBRACKET, "["},
370 {TOK_RBRACKET, "]"},
371 {TOK_STAR, "*"},
372 {TOK_COMMA, ","},
373 {TOK_EQUAL, "="},
374 {TOK_COLON, ":"},
375 {TOK_SEMICOLON, ";"},
376 {TOK_UNION, "union"},
377 {TOK_STRUCT, "struct"},
378 {TOK_SWITCH, "switch"},
379 {TOK_CASE, "case"},
380 {TOK_DEFAULT, "default"},
381 {TOK_ENUM, "enum"},
382 {TOK_TYPEDEF, "typedef"},
383 {TOK_INT, "int"},
384 {TOK_SHORT, "short"},
385 {TOK_LONG, "long"},
386 {TOK_UNSIGNED, "unsigned"},
387 {TOK_DOUBLE, "double"},
388 {TOK_FLOAT, "float"},
389 {TOK_CHAR, "char"},
390 {TOK_STRING, "string"},
391 {TOK_OPAQUE, "opaque"},
392 {TOK_BOOL, "bool"},
393 {TOK_VOID, "void"},
394 {TOK_PROGRAM, "program"},
395 {TOK_VERSION, "version"},
396 {TOK_EOF, "??????"}
399 static const char *
400 toktostr (tok_kind kind)
402 const token *sp;
404 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
405 return sp->str;
408 static void
409 printbuf (void)
411 char c;
412 int i;
413 int cnt;
415 #define TABSIZE 4
417 for (i = 0; (c = curline[i]) != 0; i++)
419 if (c == '\t')
421 cnt = 8 - (i % TABSIZE);
422 c = ' ';
424 else
426 cnt = 1;
428 while (cnt--)
430 (void) fputc (c, stderr);
435 static void
436 printwhere (void)
438 int i;
439 char c;
440 int cnt;
442 printbuf ();
443 for (i = 0; i < where - curline; i++)
445 c = curline[i];
446 if (c == '\t')
448 cnt = 8 - (i % TABSIZE);
450 else
452 cnt = 1;
454 while (cnt--)
456 (void) fputc ('^', stderr);
459 (void) fputc ('\n', stderr);
462 char *
463 make_argname (const char *pname, const char *vname)
465 char *name;
467 name = malloc (strlen (pname) + strlen (vname) + strlen (ARGEXT) + 3);
468 if (!name)
470 fprintf (stderr, "failed in malloc");
471 exit (1);
473 sprintf (name, "%s_%s_%s", locase (pname), vname, ARGEXT);
474 return name;
477 bas_type *typ_list_h;
478 bas_type *typ_list_t;
480 void
481 add_type (int len, const char *type)
483 bas_type *ptr;
486 if ((ptr = malloc (sizeof (bas_type))) == NULL)
488 fprintf (stderr, "failed in malloc");
489 exit (1);
492 ptr->name = type;
493 ptr->length = len;
494 ptr->next = NULL;
495 if (typ_list_t == NULL)
498 typ_list_t = ptr;
499 typ_list_h = ptr;
501 else
504 typ_list_t->next = ptr;
505 typ_list_t = ptr;
511 bas_type *
512 find_type (const char *type)
514 bas_type *ptr;
516 ptr = typ_list_h;
519 while (ptr != NULL)
521 if (strcmp (ptr->name, type) == 0)
522 return ptr;
523 else
524 ptr = ptr->next;
526 return NULL;