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