get mxge to build, stage 29/many
[dragonfly.git] / usr.bin / rpcgen / rpc_util.c
blobbce553618c3851e36de04a53fdf75e7b4ac51728
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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
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 $
35 * rpc_util.c, Utility routines for the RPC protocol compiler
36 * Copyright (C) 1989, Sun Microsystems, Inc.
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"
47 #define ARGEXT "argument"
49 char curline[MAXLINESIZE]; /* current read line */
50 char *where = curline; /* current point in line */
51 int linenum = 0; /* current line number */
53 const char *infilename; /* input filename */
55 #define NFILES 7
56 const char *outfiles[NFILES]; /* output file names */
57 int nfiles;
59 FILE *fout; /* file pointer of current output */
60 FILE *fin; /* file pointer of current input */
62 list *defined; /* list of defined things */
64 static void printwhere(void);
67 * Reinitialize the world
69 void
70 reinitialize(void)
72 memset(curline, 0, MAXLINESIZE);
73 where = curline;
74 linenum = 0;
75 defined = NULL;
79 * string equality
81 int
82 streq(const char *a, const char *b)
84 return(strcmp(a, b) == 0);
88 * find a value in a list
90 definition *
91 findval(list *lst, const char *val, int (*cmp)(definition *, const char *))
93 for (; lst != NULL; lst = lst->next) {
94 if ((*cmp) (lst->val, val))
95 return(lst->val);
97 return(NULL);
101 * store a value in a list
103 void
104 storeval(list **lstp, definition *val)
106 list **l;
107 list *lst;
109 for (l = lstp; *l != NULL; l = (list **) & (*l)->next)
111 lst = XALLOC(list);
112 lst->val = val;
113 lst->next = NULL;
114 *l = lst;
117 static int
118 findit(definition *def, const char *type)
120 return(streq(def->def_name, type));
123 static const char *
124 fixit(const char *type, const char *orig)
126 definition *def;
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);
138 case REL_ALIAS:
139 return(fixit(def->def.ty.old_type, orig));
140 default:
141 return(orig);
145 const char *
146 fixtype(const char *type)
148 return(fixit(type, type));
151 const char *
152 stringfix(const char *type)
154 if (streq(type, "string"))
155 return("wrapstring");
156 else
157 return(type);
160 void
161 ptype(const char *prefix, const char *type, int follow)
163 if (prefix != NULL) {
164 if (streq(prefix, "enum"))
165 f_print(fout, "enum ");
166 else
167 f_print(fout, "struct ");
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);
177 static int
178 typedefed(definition *def, const char *type)
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));
187 isvectordef(const char *type, relation rel)
189 definition *def;
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);
204 type = def->def.ty.old_type;
205 rel = def->def.ty.rel;
209 return(0);
212 char *
213 locase(const char *str)
215 char c;
216 static char buf[100];
217 char *p = buf;
219 while ((c = *str++) != 0)
220 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
221 *p = 0;
222 return(buf);
225 void
226 pvname_svc(const char *pname, const char *vnum)
228 f_print(fout, "%s_%s_svc", locase(pname), vnum);
231 void
232 pvname(const char *pname, const char *vnum)
234 f_print(fout, "%s_%s", locase(pname), vnum);
238 * print a useful (?) error message, and then die
240 void
241 error(const char *msg)
243 printwhere();
244 warnx("%s, line %d: %s", infilename, linenum, msg);
245 crash();
249 * Something went wrong, unlink any files that we may have created and then
250 * die.
252 void
253 crash(void)
255 int i;
257 for (i = 0; i < nfiles; i++)
258 unlink(outfiles[i]);
259 exit(1);
262 void
263 record_open(const char *file)
265 if (nfiles < NFILES) {
266 outfiles[nfiles++] = file;
267 } else {
268 warnx("too many files");
269 crash();
273 static char expectbuf[100];
274 static const char *toktostr(tok_kind);
277 * error, token encountered was not the expected one
279 void
280 expected1(tok_kind exp1)
282 s_print(expectbuf, "expected '%s'", toktostr(exp1));
283 error(expectbuf);
287 * error, token encountered was not one of two expected ones
289 void
290 expected2(tok_kind exp1, tok_kind exp2)
292 s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1),
293 toktostr(exp2));
294 error(expectbuf);
298 * error, token encountered was not one of 3 expected ones
300 void
301 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
303 s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1),
304 toktostr(exp2), toktostr(exp3));
305 error(expectbuf);
308 void
309 tabify(FILE *f, int tab)
311 while (tab--)
312 fputc('\t', f);
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, "??????"}
353 static const char *
354 toktostr(tok_kind kind)
356 token *sp;
358 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++)
360 return(sp->str);
363 static void
364 printbuf(void)
366 char c;
367 int i;
368 int cnt;
370 # define TABSIZE 4
372 for (i = 0; (c = curline[i]); i++) {
373 if (c == '\t') {
374 cnt = 8 - (i % TABSIZE);
375 c = ' ';
376 } else {
377 cnt = 1;
379 while (cnt--)
380 fputc(c, stderr);
384 static void
385 printwhere(void)
387 int i;
388 char c;
389 int cnt;
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);
402 fputc('\n', stderr);
405 char *
406 make_argname(const char *pname, const char *vname)
408 char *name;
410 name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
411 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
412 return(name);
415 bas_type *typ_list_h;
416 bas_type *typ_list_t;
418 void
419 add_type(int len, const char *type)
421 bas_type *ptr;
423 ptr = XALLOC(bas_type);
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;
438 bas_type *
439 find_type(const char *type)
441 bas_type * ptr;
443 ptr = typ_list_h;
444 while (ptr != NULL)
446 if (strcmp(ptr->name, type) == 0)
447 return(ptr);
448 else
449 ptr = ptr->next;
451 return(NULL);
454 void *
455 xmalloc(size_t size)
457 void *p;
459 if ((p = malloc(size)) == NULL) {
460 warnx("malloc failed");
461 crash();
463 return (p);
466 void *
467 xrealloc(void *ptr, size_t size)
469 void *p;
471 if ((p = realloc(ptr, size)) == NULL) {
472 warnx("realloc failed");
473 crash();
475 return (p);
478 char *
479 xstrdup(const char *str)
481 char *p;
483 if ((p = strdup(str)) == NULL) {
484 warnx("strdup failed");
485 crash();
487 return (p);