(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[platform/upstream/glibc.git] / sunrpc / rpc_util.c
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.
9  *
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.
13  *
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.
17  *
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.
21  *
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.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 /*
32  * From: @(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI
33  */
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char util_rcsid[] =
36   "$Id$";
37 #endif
38
39 /*
40  * rpc_util.c, Utility routines for the RPC protocol compiler
41  */
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"
50
51 #define ARGEXT "argument"
52
53 char curline[MAXLINESIZE];      /* current read line */
54 const char *where = curline;    /* current point in line */
55 int linenum = 0;                /* current line number */
56
57 const char *infilename;         /* input filename */
58
59 #define NFILES 7
60 const char *outfiles[NFILES];   /* output file names */
61 int nfiles;
62
63 FILE *fout;                     /* file pointer of current output */
64 FILE *fin;                      /* file pointer of current input */
65
66 list *defined;                  /* list of defined things */
67
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);
74
75 /*
76  * Reinitialize the world
77  */
78 void
79 reinitialize (void)
80 {
81   memset (curline, 0, MAXLINESIZE);
82   where = curline;
83   linenum = 0;
84   defined = NULL;
85 }
86
87 /*
88  * string equality
89  */
90 int
91 streq (const char *a, const char *b)
92 {
93   return strcmp (a, b) == 0;
94 }
95
96 /*
97  * find a value in a list
98  */
99 definition *
100 findval (list *lst, const char *val,
101          int (*cmp) (const definition *, const char *))
102 {
103
104   for (; lst != NULL; lst = lst->next)
105     {
106       if (cmp (lst->val, val))
107         {
108           return lst->val;
109         }
110     }
111   return NULL;
112 }
113
114 /*
115  * store a value in a list
116  */
117 void
118 storeval (list **lstp, definition *val)
119 {
120   list **l;
121   list *lst;
122
123
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;
129 }
130
131 static int
132 findit (const definition * def, const char *type)
133 {
134   return streq (def->def_name, type);
135 }
136
137 static const char *
138 fixit (const char *type, const char *orig)
139 {
140   definition *def;
141
142   def = findval (defined, type, findit);
143   if (def == NULL || def->def_kind != DEF_TYPEDEF)
144     {
145       return orig;
146     }
147   switch (def->def.ty.rel)
148     {
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;
158     }
159 }
160
161 const char *
162 fixtype (const char *type)
163 {
164   return fixit (type, type);
165 }
166
167 const char *
168 stringfix (const char *type)
169 {
170   if (streq (type, "string"))
171     {
172       return "wrapstring";
173     }
174   else
175     {
176       return type;
177     }
178 }
179
180 void
181 ptype (const char *prefix, const char *type, int follow)
182 {
183   if (prefix != NULL)
184     {
185       if (streq (prefix, "enum"))
186         {
187           f_print (fout, "enum ");
188         }
189       else
190         {
191           f_print (fout, "struct ");
192         }
193     }
194   if (streq (type, "bool"))
195     {
196       f_print (fout, "bool_t ");
197     }
198   else if (streq (type, "string"))
199     {
200       f_print (fout, "char *");
201     }
202   else
203     {
204       f_print (fout, "%s ", follow ? fixtype (type) : type);
205     }
206 }
207
208 static int
209 typedefed (const definition * def, const char *type)
210 {
211   if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
212     {
213       return 0;
214     }
215   else
216     {
217       return streq (def->def_name, type);
218     }
219 }
220
221 int
222 isvectordef (const char *type, relation rel)
223 {
224   definition *def;
225
226   for (;;)
227     {
228       switch (rel)
229         {
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)
239             {
240               return 0;
241             }
242           type = def->def.ty.old_type;
243           rel = def->def.ty.rel;
244         }
245     }
246 }
247
248 char *
249 locase (const char *str)
250 {
251   char c;
252   static char buf[100];
253   char *p = buf;
254
255   while ((c = *str++) != 0)
256     {
257       *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
258     }
259   *p = 0;
260   return buf;
261 }
262
263 void
264 pvname_svc (const char *pname, const char *vnum)
265 {
266   f_print (fout, "%s_%s_svc", locase (pname), vnum);
267 }
268
269 void
270 pvname (const char *pname, const char *vnum)
271 {
272   f_print (fout, "%s_%s", locase (pname), vnum);
273 }
274
275 /*
276  * print a useful (?) error message, and then die
277  */
278 void
279 error (const char *msg)
280 {
281   printwhere ();
282   f_print (stderr, "%s, line %d: ", infilename, linenum);
283   f_print (stderr, "%s\n", msg);
284   crash ();
285 }
286
287 /*
288  * Something went wrong, unlink any files that we may have created and then
289  * die.
290  */
291 void
292 crash (void)
293 {
294   int i;
295
296   for (i = 0; i < nfiles; i++)
297     {
298       unlink (outfiles[i]);
299     }
300   exit (1);
301 }
302
303 void
304 record_open (const char *file)
305 {
306   if (nfiles < NFILES)
307     {
308       outfiles[nfiles++] = file;
309     }
310   else
311     {
312       f_print (stderr, "too many files!\n");
313       crash ();
314     }
315 }
316
317 static char expectbuf[100];
318
319 /*
320  * error, token encountered was not the expected one
321  */
322 void
323 expected1 (tok_kind exp1)
324 {
325   s_print (expectbuf, "expected '%s'",
326            toktostr (exp1));
327   error (expectbuf);
328 }
329
330 /*
331  * error, token encountered was not one of two expected ones
332  */
333 void
334 expected2 (tok_kind exp1, tok_kind exp2)
335 {
336   s_print (expectbuf, "expected '%s' or '%s'",
337            toktostr (exp1),
338            toktostr (exp2));
339   error (expectbuf);
340 }
341
342 /*
343  * error, token encountered was not one of 3 expected ones
344  */
345 void
346 expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
347 {
348   s_print (expectbuf, "expected '%s', '%s' or '%s'",
349            toktostr (exp1),
350            toktostr (exp2),
351            toktostr (exp3));
352   error (expectbuf);
353 }
354
355 void
356 tabify (FILE * f, int tab)
357 {
358   while (tab--)
359     {
360       (void) fputc ('\t', f);
361     }
362 }
363
364
365 static const token tokstrings[] =
366 {
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, "??????"}
401 };
402
403 static const char *
404 toktostr (tok_kind kind)
405 {
406   const token *sp;
407
408   for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
409   return sp->str;
410 }
411
412 static void
413 printbuf (void)
414 {
415   char c;
416   int i;
417   int cnt;
418
419 #define TABSIZE 4
420
421   for (i = 0; (c = curline[i]) != 0; i++)
422     {
423       if (c == '\t')
424         {
425           cnt = 8 - (i % TABSIZE);
426           c = ' ';
427         }
428       else
429         {
430           cnt = 1;
431         }
432       while (cnt--)
433         {
434           (void) fputc (c, stderr);
435         }
436     }
437 }
438
439 static void
440 printwhere (void)
441 {
442   int i;
443   char c;
444   int cnt;
445
446   printbuf ();
447   for (i = 0; i < where - curline; i++)
448     {
449       c = curline[i];
450       if (c == '\t')
451         {
452           cnt = 8 - (i % TABSIZE);
453         }
454       else
455         {
456           cnt = 1;
457         }
458       while (cnt--)
459         {
460           (void) fputc ('^', stderr);
461         }
462     }
463   (void) fputc ('\n', stderr);
464 }
465
466 char *
467 make_argname (const char *pname, const char *vname)
468 {
469   char *name;
470
471   name = malloc (strlen (pname) + strlen (vname) + strlen (ARGEXT) + 3);
472   if (!name)
473     {
474       fprintf (stderr, "failed in malloc");
475       exit (1);
476     }
477   sprintf (name, "%s_%s_%s", locase (pname), vname, ARGEXT);
478   return name;
479 }
480
481 bas_type *typ_list_h;
482 bas_type *typ_list_t;
483
484 void
485 add_type (int len, const char *type)
486 {
487   bas_type *ptr;
488
489
490   if ((ptr = malloc (sizeof (bas_type))) == NULL)
491     {
492       fprintf (stderr, "failed in malloc");
493       exit (1);
494     }
495
496   ptr->name = type;
497   ptr->length = len;
498   ptr->next = NULL;
499   if (typ_list_t == NULL)
500     {
501
502       typ_list_t = ptr;
503       typ_list_h = ptr;
504     }
505   else
506     {
507
508       typ_list_t->next = ptr;
509       typ_list_t = ptr;
510     }
511
512 }
513
514
515 bas_type *
516 find_type (const char *type)
517 {
518   bas_type *ptr;
519
520   ptr = typ_list_h;
521
522
523   while (ptr != NULL)
524     {
525       if (strcmp (ptr->name, type) == 0)
526         return ptr;
527       else
528         ptr = ptr->next;
529     };
530   return NULL;
531 }