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