Merge from vendor branch LESS:
[dragonfly.git] / usr.bin / rpcgen / rpc_util.c
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.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 $
32  */
33
34 #ident  "@(#)rpc_util.c 1.14    93/07/05 SMI"
35
36 /*
37  * rpc_util.c, Utility routines for the RPC protocol compiler
38  * Copyright (C) 1989, Sun Microsystems, Inc.
39  */
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"
48
49 #define ARGEXT "argument"
50
51 char curline[MAXLINESIZE];      /* current read line */
52 char *where = curline;          /* current point in line */
53 int linenum = 0;                /* current line number */
54
55 char *infilename;               /* input filename */
56
57 #define NFILES   7
58 char *outfiles[NFILES];         /* output file names */
59 int nfiles;
60
61 FILE *fout;                     /* file pointer of current output */
62 FILE *fin;                      /* file pointer of current input */
63
64 list *defined;                  /* list of defined things */
65
66 static void     printwhere(void);
67
68 /*
69  * Reinitialize the world
70  */
71 void
72 reinitialize(void)
73 {
74         memset(curline, 0, MAXLINESIZE);
75         where = curline;
76         linenum = 0;
77         defined = NULL;
78 }
79
80 /*
81  * string equality
82  */
83 int
84 streq(char *a, char *b)
85 {
86         return(strcmp(a, b) == 0);
87 }
88
89 /*
90  * find a value in a list
91  */
92 definition *
93 findval(list *lst, char *val, int (*cmp) (definition *, char *))
94 {
95         for (; lst != NULL; lst = lst->next) {
96                 if ((*cmp) (lst->val, val))
97                         return(lst->val);
98         }
99         return(NULL);
100 }
101
102 /*
103  * store a value in a list
104  */
105 void
106 storeval(list **lstp, definition *val)
107 {
108         list **l;
109         list *lst;
110
111         for (l = lstp; *l != NULL; l = (list **) & (*l)->next)
112                 ;
113         lst = ALLOC(list);
114         lst->val = val;
115         lst->next = NULL;
116         *l = lst;
117 }
118
119 static int
120 findit(definition *def, char *type)
121 {
122         return(streq(def->def_name, type));
123 }
124
125 static char *
126 fixit(char *type, char *orig)
127 {
128         definition *def;
129
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);
139
140         case REL_ALIAS:
141                 return(fixit(def->def.ty.old_type, orig));
142         default:
143                 return(orig);
144         }
145 }
146
147 char *
148 fixtype(char *type)
149 {
150         return(fixit(type, type));
151 }
152
153 char *
154 stringfix(char *type)
155 {
156         if (streq(type, "string"))
157                 return("wrapstring");
158         else
159                 return(type);
160 }
161
162 void
163 ptype(char *prefix, char *type, int follow)
164 {
165         if (prefix != NULL) {
166                 if (streq(prefix, "enum"))
167                         f_print(fout, "enum ");
168                 else
169                         f_print(fout, "struct ");
170         }
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);
177 }
178
179 static int
180 typedefed(definition *def, char *type)
181 {
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));
186 }
187
188 int
189 isvectordef(char *type, relation rel)
190 {
191         definition *def;
192
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);
205                         }
206                         type = def->def.ty.old_type;
207                         rel = def->def.ty.rel;
208                 }
209         }
210
211         return(0);
212 }
213
214 char *
215 locase(char *str)
216 {
217         char c;
218         static char buf[100];
219         char *p = buf;
220
221         while ((c = *str++) != 0)
222                 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
223         *p = 0;
224         return(buf);
225 }
226
227 void
228 pvname_svc(char *pname, char *vnum)
229 {
230         f_print(fout, "%s_%s_svc", locase(pname), vnum);
231 }
232
233 void
234 pvname(char *pname, char *vnum)
235 {
236         f_print(fout, "%s_%s", locase(pname), vnum);
237 }
238
239 /*
240  * print a useful (?) error message, and then die
241  */
242 void
243 error(char *msg)
244 {
245         printwhere();
246         warnx("%s, line %d: %s", infilename, linenum, msg);
247         crash();
248 }
249
250 /*
251  * Something went wrong, unlink any files that we may have created and then
252  * die.
253  */
254 void
255 crash(void)
256 {
257         int i;
258
259         for (i = 0; i < nfiles; i++)
260                 unlink(outfiles[i]);
261         exit(1);
262 }
263
264 void
265 record_open(char *file)
266 {
267         if (nfiles < NFILES) {
268                 outfiles[nfiles++] = file;
269         } else {
270                 warnx("too many files");
271                 crash();
272         }
273 }
274
275 static char expectbuf[100];
276 static char *toktostr();
277
278 /*
279  * error, token encountered was not the expected one
280  */
281 void
282 expected1(tok_kind exp1)
283 {
284         s_print(expectbuf, "expected '%s'", toktostr(exp1));
285         error(expectbuf);
286 }
287
288 /*
289  * error, token encountered was not one of two expected ones
290  */
291 void
292 expected2(tok_kind exp1, tok_kind exp2)
293 {
294         s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1),
295                 toktostr(exp2));
296         error(expectbuf);
297 }
298
299 /*
300  * error, token encountered was not one of 3 expected ones
301  */
302 void
303 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
304 {
305         s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1),
306                 toktostr(exp2), toktostr(exp3));
307         error(expectbuf);
308 }
309
310 void
311 tabify(FILE *f, int tab)
312 {
313         while (tab--)
314                 fputc('\t', f);
315 }
316
317
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, "??????"}
353 };
354
355 static char *
356 toktostr(tok_kind kind)
357 {
358         token *sp;
359
360         for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++)
361                 ;
362         return(sp->str);
363 }
364
365 static void
366 printbuf(void)
367 {
368         char c;
369         int i;
370         int cnt;
371
372 #       define TABSIZE 4
373
374         for (i = 0; (c = curline[i]); i++) {
375                 if (c == '\t') {
376                         cnt = 8 - (i % TABSIZE);
377                         c = ' ';
378                 } else {
379                         cnt = 1;
380                 }
381                 while (cnt--)
382                         fputc(c, stderr);
383         }
384 }
385
386 static void
387 printwhere(void)
388 {
389         int i;
390         char c;
391         int cnt;
392
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);
402                 }
403         }
404         fputc('\n', stderr);
405 }
406
407 char *
408 make_argname(char *pname, char *vname)
409 {
410         char *name;
411
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);
417 }
418
419 bas_type *typ_list_h;
420 bas_type *typ_list_t;
421
422 void
423 add_type(int len, char *type)
424 {
425         bas_type *ptr;
426
427         if ((ptr = (bas_type *) malloc(sizeof (bas_type))) == NULL)
428                 errx(1, "failed in malloc");
429
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;
439         }
440 }
441
442
443 bas_type *
444 find_type(char *type)
445 {
446         bas_type * ptr;
447
448         ptr = typ_list_h;
449         while (ptr != NULL)
450         {
451                 if (strcmp(ptr->name, type) == 0)
452                         return(ptr);
453                 else
454                         ptr = ptr->next;
455         }
456         return(NULL);
457 }