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