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