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