MFC:
[dragonfly.git] / usr.bin / rpcgen / rpc_util.c
blob62c7464fd1ef7c6d027b8b7b4e9fe142b435c1f8
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.
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.11 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_util.c,v 1.6 1999/08/28 01:05:17 peter Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_util.c,v 1.4 2004/06/19 16:40:36 joerg Exp $
34 #ident "@(#)rpc_util.c 1.14 93/07/05 SMI"
37 * rpc_util.c, Utility routines for the RPC protocol compiler
38 * Copyright (C) 1989, Sun Microsystems, Inc.
40 #include <err.h>
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include "rpc_scan.h"
46 #include "rpc_parse.h"
47 #include "rpc_util.h"
49 #define ARGEXT "argument"
51 char curline[MAXLINESIZE]; /* current read line */
52 char *where = curline; /* current point in line */
53 int linenum = 0; /* current line number */
55 char *infilename; /* input filename */
57 #define NFILES 7
58 char *outfiles[NFILES]; /* output file names */
59 int nfiles;
61 FILE *fout; /* file pointer of current output */
62 FILE *fin; /* file pointer of current input */
64 list *defined; /* list of defined things */
66 static void printwhere(void);
69 * Reinitialize the world
71 void
72 reinitialize(void)
74 memset(curline, 0, MAXLINESIZE);
75 where = curline;
76 linenum = 0;
77 defined = NULL;
81 * string equality
83 int
84 streq(char *a, char *b)
86 return(strcmp(a, b) == 0);
90 * find a value in a list
92 definition *
93 findval(list *lst, char *val, int (*cmp) (definition *, char *))
95 for (; lst != NULL; lst = lst->next) {
96 if ((*cmp) (lst->val, val))
97 return(lst->val);
99 return(NULL);
103 * store a value in a list
105 void
106 storeval(list **lstp, definition *val)
108 list **l;
109 list *lst;
111 for (l = lstp; *l != NULL; l = (list **) & (*l)->next)
113 lst = ALLOC(list);
114 lst->val = val;
115 lst->next = NULL;
116 *l = lst;
119 static int
120 findit(definition *def, char *type)
122 return(streq(def->def_name, type));
125 static char *
126 fixit(char *type, char *orig)
128 definition *def;
130 def = (definition *) FINDVAL(defined, type, findit);
131 if (def == NULL || def->def_kind != DEF_TYPEDEF)
132 return(orig);
133 switch (def->def.ty.rel) {
134 case REL_VECTOR:
135 if (streq(def->def.ty.old_type, "opaque"))
136 return("char");
137 else
138 return(def->def.ty.old_type);
140 case REL_ALIAS:
141 return(fixit(def->def.ty.old_type, orig));
142 default:
143 return(orig);
147 char *
148 fixtype(char *type)
150 return(fixit(type, type));
153 char *
154 stringfix(char *type)
156 if (streq(type, "string"))
157 return("wrapstring");
158 else
159 return(type);
162 void
163 ptype(char *prefix, char *type, int follow)
165 if (prefix != NULL) {
166 if (streq(prefix, "enum"))
167 f_print(fout, "enum ");
168 else
169 f_print(fout, "struct ");
171 if (streq(type, "bool"))
172 f_print(fout, "bool_t ");
173 else if (streq(type, "string"))
174 f_print(fout, "char *");
175 else
176 f_print(fout, "%s ", follow ? fixtype(type) : type);
179 static int
180 typedefed(definition *def, char *type)
182 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
183 return(0);
184 else
185 return(streq(def->def_name, type));
189 isvectordef(char *type, relation rel)
191 definition *def;
193 for (;;) {
194 switch (rel) {
195 case REL_VECTOR:
196 return(!streq(type, "string"));
197 case REL_ARRAY:
198 return(0);
199 case REL_POINTER:
200 return(0);
201 case REL_ALIAS:
202 def = (definition *) FINDVAL(defined, type, typedefed);
203 if (def == NULL) {
204 return(0);
206 type = def->def.ty.old_type;
207 rel = def->def.ty.rel;
211 return(0);
214 char *
215 locase(char *str)
217 char c;
218 static char buf[100];
219 char *p = buf;
221 while ((c = *str++) != 0)
222 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
223 *p = 0;
224 return(buf);
227 void
228 pvname_svc(char *pname, char *vnum)
230 f_print(fout, "%s_%s_svc", locase(pname), vnum);
233 void
234 pvname(char *pname, char *vnum)
236 f_print(fout, "%s_%s", locase(pname), vnum);
240 * print a useful (?) error message, and then die
242 void
243 error(char *msg)
245 printwhere();
246 warnx("%s, line %d: %s", infilename, linenum, msg);
247 crash();
251 * Something went wrong, unlink any files that we may have created and then
252 * die.
254 void
255 crash(void)
257 int i;
259 for (i = 0; i < nfiles; i++)
260 unlink(outfiles[i]);
261 exit(1);
264 void
265 record_open(char *file)
267 if (nfiles < NFILES) {
268 outfiles[nfiles++] = file;
269 } else {
270 warnx("too many files");
271 crash();
275 static char expectbuf[100];
276 static char *toktostr();
279 * error, token encountered was not the expected one
281 void
282 expected1(tok_kind exp1)
284 s_print(expectbuf, "expected '%s'", toktostr(exp1));
285 error(expectbuf);
289 * error, token encountered was not one of two expected ones
291 void
292 expected2(tok_kind exp1, tok_kind exp2)
294 s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1),
295 toktostr(exp2));
296 error(expectbuf);
300 * error, token encountered was not one of 3 expected ones
302 void
303 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
305 s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1),
306 toktostr(exp2), toktostr(exp3));
307 error(expectbuf);
310 void
311 tabify(FILE *f, int tab)
313 while (tab--)
314 fputc('\t', f);
318 static token tokstrings[] = {
319 {TOK_IDENT, "identifier"},
320 {TOK_CONST, "const"},
321 {TOK_RPAREN, ")"},
322 {TOK_LPAREN, "("},
323 {TOK_RBRACE, "}"},
324 {TOK_LBRACE, "{"},
325 {TOK_LBRACKET, "["},
326 {TOK_RBRACKET, "]"},
327 {TOK_STAR, "*"},
328 {TOK_COMMA, ","},
329 {TOK_EQUAL, "="},
330 {TOK_COLON, ":"},
331 {TOK_SEMICOLON, ";"},
332 {TOK_UNION, "union"},
333 {TOK_STRUCT, "struct"},
334 {TOK_SWITCH, "switch"},
335 {TOK_CASE, "case"},
336 {TOK_DEFAULT, "default"},
337 {TOK_ENUM, "enum"},
338 {TOK_TYPEDEF, "typedef"},
339 {TOK_INT, "int"},
340 {TOK_SHORT, "short"},
341 {TOK_LONG, "long"},
342 {TOK_UNSIGNED, "unsigned"},
343 {TOK_DOUBLE, "double"},
344 {TOK_FLOAT, "float"},
345 {TOK_CHAR, "char"},
346 {TOK_STRING, "string"},
347 {TOK_OPAQUE, "opaque"},
348 {TOK_BOOL, "bool"},
349 {TOK_VOID, "void"},
350 {TOK_PROGRAM, "program"},
351 {TOK_VERSION, "version"},
352 {TOK_EOF, "??????"}
355 static char *
356 toktostr(tok_kind kind)
358 token *sp;
360 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++)
362 return(sp->str);
365 static void
366 printbuf(void)
368 char c;
369 int i;
370 int cnt;
372 # define TABSIZE 4
374 for (i = 0; (c = curline[i]); i++) {
375 if (c == '\t') {
376 cnt = 8 - (i % TABSIZE);
377 c = ' ';
378 } else {
379 cnt = 1;
381 while (cnt--)
382 fputc(c, stderr);
386 static void
387 printwhere(void)
389 int i;
390 char c;
391 int cnt;
393 printbuf();
394 for (i = 0; i < where - curline; i++) {
395 c = curline[i];
396 if (c == '\t')
397 cnt = 8 - (i % TABSIZE);
398 else
399 cnt = 1;
400 while (cnt--) {
401 fputc('^', stderr);
404 fputc('\n', stderr);
407 char *
408 make_argname(char *pname, char *vname)
410 char *name;
412 name = malloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3);
413 if (!name)
414 errx(1, "failed in malloc");
415 sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT);
416 return(name);
419 bas_type *typ_list_h;
420 bas_type *typ_list_t;
422 void
423 add_type(int len, char *type)
425 bas_type *ptr;
427 if ((ptr = (bas_type *) malloc(sizeof (bas_type))) == NULL)
428 errx(1, "failed in malloc");
430 ptr->name = type;
431 ptr->length = len;
432 ptr->next = NULL;
433 if (typ_list_t == NULL) {
434 typ_list_t = ptr;
435 typ_list_h = ptr;
436 } else {
437 typ_list_t->next = ptr;
438 typ_list_t = ptr;
443 bas_type *
444 find_type(char *type)
446 bas_type * ptr;
448 ptr = typ_list_h;
449 while (ptr != NULL)
451 if (strcmp(ptr->name, type) == 0)
452 return(ptr);
453 else
454 ptr = ptr->next;
456 return(NULL);