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