Update.
[glibc.git] / sunrpc / rpc_util.c
blobcb4561ef395a4d8d024d73eb80543b06caa1d427
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
31 /*
32 * From: @(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI
34 char util_rcsid[] =
35 "$Id$";
38 * rpc_util.c, Utility routines for the RPC protocol compiler
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"
49 #define ARGEXT "argument"
51 char curline[MAXLINESIZE]; /* current read line */
52 const char *where = curline; /* current point in line */
53 int linenum = 0; /* current line number */
55 const char *infilename; /* input filename */
57 #define NFILES 7
58 const char *outfiles[NFILES]; /* output file names */
59 int nfiles;
61 FILE *fout; /* file pointer of current output */
62 FILE *fin; /* file pointer of current input */
64 list *defined; /* list of defined things */
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);
74 * Reinitialize the world
76 void
77 reinitialize(void)
79 memset(curline, 0, MAXLINESIZE);
80 where = curline;
81 linenum = 0;
82 defined = NULL;
86 * string equality
88 int
89 streq(const char *a, const char *b)
91 return (strcmp(a, b) == 0);
95 * find a value in a list
97 definition *
98 findval(list *lst, const char *val,
99 int (*cmp)(const definition *, const char *))
102 for (; lst != NULL; lst = lst->next) {
103 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) {
140 return (orig);
142 switch (def->def.ty.rel) {
143 case REL_VECTOR:
144 return (def->def.ty.old_type);
145 case REL_ALIAS:
146 return (fixit(def->def.ty.old_type, orig));
147 default:
148 return (orig);
152 const char *
153 fixtype(const char *type)
155 return (fixit(type, type));
158 const char *
159 stringfix(const char *type)
161 if (streq(type, "string")) {
162 return "wrapstring";
164 else {
165 return type;
169 void
170 ptype(const char *prefix, const char *type, int follow)
172 if (prefix != NULL) {
173 if (streq(prefix, "enum")) {
174 f_print(fout, "enum ");
175 } else {
176 f_print(fout, "struct ");
179 if (streq(type, "bool")) {
180 f_print(fout, "bool_t ");
181 } else if (streq(type, "string")) {
182 f_print(fout, "char *");
183 } else {
184 f_print(fout, "%s ", follow ? fixtype(type) : type);
188 static int
189 typedefed(const definition *def, const char *type)
191 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
192 return (0);
193 } else {
194 return (streq(def->def_name, type));
199 isvectordef(const char *type, relation rel)
201 definition *def;
203 for (;;) {
204 switch (rel) {
205 case REL_VECTOR:
206 return (!streq(type, "string"));
207 case REL_ARRAY:
208 return (0);
209 case REL_POINTER:
210 return (0);
211 case REL_ALIAS:
212 def = findval(defined, type, typedefed);
213 if (def == NULL) {
214 return (0);
216 type = def->def.ty.old_type;
217 rel = def->def.ty.rel;
222 char *
223 locase(const char *str)
225 char c;
226 static char buf[100];
227 char *p = buf;
229 while ((c = *str++)!=0) {
230 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
232 *p = 0;
233 return (buf);
236 void
237 pvname_svc(const char *pname, const char *vnum)
239 f_print(fout, "%s_%s_svc", locase(pname), vnum);
242 void
243 pvname(const char *pname, const char *vnum)
245 f_print(fout, "%s_%s", locase(pname), vnum);
249 * print a useful (?) error message, and then die
251 void
252 error(const char *msg)
254 printwhere();
255 f_print(stderr, "%s, line %d: ", infilename, linenum);
256 f_print(stderr, "%s\n", msg);
257 crash();
261 * Something went wrong, unlink any files that we may have created and then
262 * die.
264 void
265 crash(void)
267 int i;
269 for (i = 0; i < nfiles; i++) {
270 (void) unlink(outfiles[i]);
272 exit(1);
275 void
276 record_open(const char *file)
278 if (nfiles < NFILES) {
279 outfiles[nfiles++] = file;
281 else {
282 f_print(stderr, "too many files!\n");
283 crash();
287 static char expectbuf[100];
290 * error, token encountered was not the expected one
292 void
293 expected1(tok_kind exp1)
295 s_print(expectbuf, "expected '%s'",
296 toktostr(exp1));
297 error(expectbuf);
301 * error, token encountered was not one of two expected ones
303 void
304 expected2(tok_kind exp1, tok_kind exp2)
306 s_print(expectbuf, "expected '%s' or '%s'",
307 toktostr(exp1),
308 toktostr(exp2));
309 error(expectbuf);
313 * error, token encountered was not one of 3 expected ones
315 void
316 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
318 s_print(expectbuf, "expected '%s', '%s' or '%s'",
319 toktostr(exp1),
320 toktostr(exp2),
321 toktostr(exp3));
322 error(expectbuf);
325 void
326 tabify(FILE *f, int tab)
328 while (tab--) {
329 (void) fputc('\t', f);
334 static token tokstrings[] = {
335 {TOK_IDENT, "identifier"},
336 {TOK_CONST, "const"},
337 {TOK_RPAREN, ")"},
338 {TOK_LPAREN, "("},
339 {TOK_RBRACE, "}"},
340 {TOK_LBRACE, "{"},
341 {TOK_LBRACKET, "["},
342 {TOK_RBRACKET, "]"},
343 {TOK_STAR, "*"},
344 {TOK_COMMA, ","},
345 {TOK_EQUAL, "="},
346 {TOK_COLON, ":"},
347 {TOK_SEMICOLON, ";"},
348 {TOK_UNION, "union"},
349 {TOK_STRUCT, "struct"},
350 {TOK_SWITCH, "switch"},
351 {TOK_CASE, "case"},
352 {TOK_DEFAULT, "default"},
353 {TOK_ENUM, "enum"},
354 {TOK_TYPEDEF, "typedef"},
355 {TOK_INT, "int"},
356 {TOK_SHORT, "short"},
357 {TOK_LONG, "long"},
358 {TOK_UNSIGNED, "unsigned"},
359 {TOK_DOUBLE, "double"},
360 {TOK_FLOAT, "float"},
361 {TOK_CHAR, "char"},
362 {TOK_STRING, "string"},
363 {TOK_OPAQUE, "opaque"},
364 {TOK_BOOL, "bool"},
365 {TOK_VOID, "void"},
366 {TOK_PROGRAM, "program"},
367 {TOK_VERSION, "version"},
368 {TOK_EOF, "??????"}
371 static const char *
372 toktostr(tok_kind kind)
374 token *sp;
376 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
377 return (sp->str);
380 static void
381 printbuf(void)
383 char c;
384 int i;
385 int cnt;
387 # define TABSIZE 4
389 for (i = 0; (c = curline[i])!=0; i++) {
390 if (c == '\t') {
391 cnt = 8 - (i % TABSIZE);
392 c = ' ';
393 } else {
394 cnt = 1;
396 while (cnt--) {
397 (void) fputc(c, stderr);
402 static void
403 printwhere(void)
405 int i;
406 char c;
407 int cnt;
409 printbuf();
410 for (i = 0; i < where - curline; i++) {
411 c = curline[i];
412 if (c == '\t') {
413 cnt = 8 - (i % TABSIZE);
414 } else {
415 cnt = 1;
417 while (cnt--) {
418 (void) fputc('^', stderr);
421 (void) fputc('\n', stderr);
424 char *
425 make_argname(const char *pname, const char *vname)
427 char *name;
429 name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
430 if (!name) {
431 fprintf(stderr, "failed in malloc");
432 exit(1);
434 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
435 return name;
438 bas_type *typ_list_h;
439 bas_type *typ_list_t;
441 void
442 add_type(int len, const char *type)
444 bas_type *ptr;
447 if ((ptr = malloc(sizeof(bas_type))) == NULL) {
448 fprintf(stderr, "failed in malloc");
449 exit(1);
452 ptr->name=type;
453 ptr->length=len;
454 ptr->next=NULL;
455 if(typ_list_t == NULL)
458 typ_list_t=ptr;
459 typ_list_h=ptr;
461 else
464 typ_list_t->next=ptr;
465 typ_list_t=ptr;
471 bas_type *find_type(const char *type)
473 bas_type *ptr;
475 ptr=typ_list_h;
478 while(ptr != NULL)
480 if(strcmp(ptr->name,type) == 0)
481 return(ptr);
482 else
483 ptr=ptr->next;
485 return(NULL);