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