rpc_util.c revision 1.15 1 /* $NetBSD: rpc_util.c,v 1.15 2015/05/09 23:12:57 dholland Exp $ */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user or with the express written consent of
9 * Sun Microsystems, Inc.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
40 #else
41 __RCSID("$NetBSD: rpc_util.c,v 1.15 2015/05/09 23:12:57 dholland Exp $");
42 #endif
43 #endif
44
45 /*
46 * rpc_util.c, Utility routines for the RPC protocol compiler
47 */
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <err.h>
53 #include <ctype.h>
54 #include "rpc_scan.h"
55 #include "rpc_parse.h"
56 #include "rpc_util.h"
57
58 #define ARGEXT "argument"
59
60 static void printwhere(void);
61
62 char curline[MAXLINESIZE]; /* current read line */
63 char *where = curline; /* current point in line */
64 int linenum = 0; /* current line number */
65
66 const char *infilename; /* input filename */
67
68 #define NFILES 7
69 static const char *outfiles[NFILES]; /* output file names */
70 int nfiles;
71
72 FILE *fout; /* file pointer of current output */
73 FILE *fin; /* file pointer of current input */
74
75 list *defined; /* list of defined things */
76
77 static const char *toktostr(tok_kind);
78 static void printbuf(void);
79 static void printwhere(void);
80 static int findit(definition *, const char *);
81 static const char *fixit(const char *, const char *);
82 static int typedefed(definition *, const char *);
83
84 /*
85 * Reinitialize the world
86 */
87 void
88 reinitialize(void)
89 {
90 memset(curline, 0, MAXLINESIZE);
91 where = curline;
92 linenum = 0;
93 defined = NULL;
94 }
95 /*
96 * string equality
97 */
98 int
99 streq(const char *a, const char *b)
100 {
101 return (strcmp(a, b) == 0);
102 }
103 /*
104 * find a value in a list
105 */
106 definition *
107 findval(list *lst, const char *val, int (*cmp)(definition *, const char *))
108 {
109
110 for (; lst != NULL; lst = lst->next) {
111 if ((*cmp) (lst->val, val)) {
112 return (lst->val);
113 }
114 }
115 return (NULL);
116 }
117 /*
118 * store a value in a list
119 */
120 void
121 storeval(list **lstp, definition *val)
122 {
123 list **l;
124 list *lst;
125
126
127 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
128 lst = ALLOC(list);
129 lst->val = val;
130 lst->next = NULL;
131 *l = lst;
132 }
133
134 static int
135 findit(definition *def, const char *type)
136 {
137 return (streq(def->def_name, type));
138 }
139
140 static const char *
141 fixit(const char *type, const char *orig)
142 {
143 definition *def;
144
145 def = (definition *) FINDVAL(defined, type, findit);
146 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
147 return (orig);
148 }
149 switch (def->def.ty.rel) {
150 case REL_VECTOR:
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 return ("wrapstring");
170 } else {
171 return (type);
172 }
173 }
174
175 void
176 ptype(const char *prefix, const char *type, int follow)
177 {
178 if (prefix != NULL) {
179 if (streq(prefix, "enum")) {
180 f_print(fout, "enum ");
181 } else {
182 f_print(fout, "struct ");
183 }
184 }
185 if (streq(type, "bool")) {
186 f_print(fout, "bool_t ");
187 } else
188 if (streq(type, "string")) {
189 f_print(fout, "char *");
190 } else {
191 f_print(fout, "%s ", follow ? fixtype(type) : type);
192 }
193 }
194
195 static int
196 typedefed(definition *def, const char *type)
197 {
198 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
199 return (0);
200 } else {
201 return (streq(def->def_name, type));
202 }
203 }
204
205 int
206 isvectordef(const char *type, relation rel)
207 {
208 definition *def;
209
210 for (;;) {
211 switch (rel) {
212 case REL_VECTOR:
213 return (!streq(type, "string"));
214 case REL_ARRAY:
215 return (0);
216 case REL_POINTER:
217 return (0);
218 case REL_ALIAS:
219 def = (definition *) FINDVAL(defined, type, typedefed);
220 if (def == NULL) {
221 return (0);
222 }
223 type = def->def.ty.old_type;
224 rel = def->def.ty.rel;
225 }
226 }
227 }
228
229 char *
230 locase(const char *str)
231 {
232 char c;
233 static char buf[100];
234 char *p = buf;
235
236 while ((c = *str++) != '\0') {
237 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
238 }
239 *p = 0;
240 return (buf);
241 }
242
243 void
244 pvname_svc(const char *pname, const char *vnum)
245 {
246 f_print(fout, "%s_%s_svc", locase(pname), vnum);
247 }
248
249 void
250 pvname(const char *pname, const char *vnum)
251 {
252 f_print(fout, "%s_%s", locase(pname), vnum);
253 }
254 /*
255 * print a useful (?) error message, and then die
256 */
257 void
258 error(const char *msg)
259 {
260 printwhere();
261 f_print(stderr, "%s:%d: %s", infilename, linenum, msg);
262 errx(EXIT_FAILURE, "Cannot recover from this error");
263 }
264 /*
265 * Something went wrong, unlink any files that we may have created and then
266 * die.
267 */
268 void
269 crash(void)
270 {
271 int i;
272
273 if (!docleanup)
274 return;
275
276 for (i = 0; i < nfiles; i++) {
277 (void) unlink(outfiles[i]);
278 }
279 }
280
281 void
282 record_open(const char *file)
283 {
284 if (nfiles < NFILES) {
285 outfiles[nfiles++] = file;
286 } else {
287 errx(EXIT_FAILURE, "Too many files!");
288 }
289 }
290
291 static char expectbuf[100];
292
293 /*
294 * error, token encountered was not the expected one
295 */
296 void
297 expected1(tok_kind exp1)
298 {
299 s_print(expectbuf, "Expected '%s'",
300 toktostr(exp1));
301 error(expectbuf);
302 }
303 /*
304 * error, token encountered was not one of two expected ones
305 */
306 void
307 expected2(tok_kind exp1, tok_kind exp2)
308 {
309 s_print(expectbuf, "Expected '%s' or '%s'",
310 toktostr(exp1),
311 toktostr(exp2));
312 error(expectbuf);
313 }
314 /*
315 * error, token encountered was not one of 3 expected ones
316 */
317 void
318 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
319 {
320 s_print(expectbuf, "Expected '%s', '%s', or '%s'",
321 toktostr(exp1),
322 toktostr(exp2),
323 toktostr(exp3));
324 error(expectbuf);
325 }
326
327 void
328 tabify(FILE *f, int tab)
329 {
330 while (tab--) {
331 (void) fputc('\t', f);
332 }
333 }
334
335
336 static token tokstrings[] = {
337 {TOK_IDENT, "identifier"},
338 {TOK_CONST, "const"},
339 {TOK_RPAREN, ")"},
340 {TOK_LPAREN, "("},
341 {TOK_RBRACE, "}"},
342 {TOK_LBRACE, "{"},
343 {TOK_LBRACKET, "["},
344 {TOK_RBRACKET, "]"},
345 {TOK_STAR, "*"},
346 {TOK_COMMA, ","},
347 {TOK_EQUAL, "="},
348 {TOK_COLON, ":"},
349 {TOK_SEMICOLON, ";"},
350 {TOK_UNION, "union"},
351 {TOK_STRUCT, "struct"},
352 {TOK_SWITCH, "switch"},
353 {TOK_CASE, "case"},
354 {TOK_DEFAULT, "default"},
355 {TOK_ENUM, "enum"},
356 {TOK_TYPEDEF, "typedef"},
357 {TOK_INT, "int"},
358 {TOK_SHORT, "short"},
359 {TOK_LONG, "long"},
360 {TOK_UNSIGNED, "unsigned"},
361 {TOK_DOUBLE, "double"},
362 {TOK_FLOAT, "float"},
363 {TOK_CHAR, "char"},
364 {TOK_STRING, "string"},
365 {TOK_OPAQUE, "opaque"},
366 {TOK_BOOL, "bool"},
367 {TOK_VOID, "void"},
368 {TOK_PROGRAM, "program"},
369 {TOK_VERSION, "version"},
370 {TOK_EOF, "??????"}
371 };
372
373 static const char *
374 toktostr(tok_kind kind)
375 {
376 token *sp;
377
378 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
379 return (sp->str);
380 }
381
382 static void
383 printbuf(void)
384 {
385 char c;
386 int i;
387 int cnt;
388
389 #define TABSIZE 4
390
391 for (i = 0; (c = curline[i]) != '\0'; i++) {
392 if (c == '\t') {
393 cnt = 8 - (i % TABSIZE);
394 c = ' ';
395 } else {
396 cnt = 1;
397 }
398 while (cnt--) {
399 (void) fputc(c, stderr);
400 }
401 }
402 }
403
404 static void
405 printwhere(void)
406 {
407 int i;
408 char c;
409 int cnt;
410
411 printbuf();
412 for (i = 0; i < where - curline; i++) {
413 c = curline[i];
414 if (c == '\t') {
415 cnt = 8 - (i % TABSIZE);
416 } else {
417 cnt = 1;
418 }
419 while (cnt--) {
420 (void) fputc('^', stderr);
421 }
422 }
423 (void) fputc('\n', stderr);
424 }
425
426 char *
427 make_argname(const char *pname, const char *vname)
428 {
429 char *name;
430 size_t len;
431
432 len = strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3;
433 name = malloc(len);
434 if (!name) {
435 fprintf(stderr, "failed in malloc");
436 exit(1);
437 }
438 snprintf(name, len, "%s_%s_%s", locase(pname), vname, ARGEXT);
439 return (name);
440 }
441
442 bas_type *typ_list_h;
443 bas_type *typ_list_t;
444
445 void
446 add_type(int len, const char *type)
447 {
448 bas_type *ptr;
449
450 if ((ptr = malloc(sizeof(bas_type))) == NULL) {
451 fprintf(stderr, "failed in malloc");
452 exit(1);
453 }
454 ptr->name = type;
455 ptr->length = len;
456 ptr->next = NULL;
457 if (typ_list_t == NULL) {
458 typ_list_t = ptr;
459 typ_list_h = ptr;
460 } else {
461 typ_list_t->next = ptr;
462 typ_list_t = ptr;
463 }
464 }
465
466 bas_type *
467 find_type(const char *type)
468 {
469 bas_type *ptr;
470
471 ptr = typ_list_h;
472
473
474 while (ptr != NULL) {
475 if (strcmp(ptr->name, type) == 0)
476 return (ptr);
477 else
478 ptr = ptr->next;
479 }
480 return (NULL);
481 }
482