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