Mon Jul 29 20:33:42 1996 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[glibc.git] / sunrpc / rpc_util.c
blobebb037a05eb1ed32c5927005e4b83004bcb7893f
1 /* @(#)rpc_util.c 2.1 88/08/01 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #ifndef lint
31 static char sccsid[] = "@(#)rpc_util.c 1.5 87/06/24 (C) 1987 SMI";
32 #endif
35 * rpc_util.c, Utility routines for the RPC protocol compiler
36 * Copyright (C) 1987, Sun Microsystems, Inc.
38 #include <stdio.h>
39 #include "rpc_scan.h"
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
43 char curline[MAXLINESIZE]; /* current read line */
44 char *where = curline; /* current point in line */
45 int linenum = 0; /* current line number */
47 char *infilename; /* input filename */
49 #define NFILES 4
50 char *outfiles[NFILES]; /* output file names */
51 int nfiles;
53 FILE *fout; /* file pointer of current output */
54 FILE *fin; /* file pointer of current input */
56 list *defined; /* list of defined things */
59 * Reinitialize the world
61 reinitialize()
63 bzero(curline, MAXLINESIZE);
64 where = curline;
65 linenum = 0;
66 defined = NULL;
70 * string equality
72 streq(a, b)
73 char *a;
74 char *b;
76 return (strcmp(a, b) == 0);
80 * find a value in a list
82 char *
83 findval(lst, val, cmp)
84 list *lst;
85 char *val;
86 int (*cmp) ();
89 for (; lst != NULL; lst = lst->next) {
90 if ((*cmp) (lst->val, val)) {
91 return (lst->val);
94 return (NULL);
98 * store a value in a list
100 void
101 storeval(lstp, val)
102 list **lstp;
103 char *val;
105 list **l;
106 list *lst;
108 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
109 lst = ALLOC(list);
110 lst->val = val;
111 lst->next = NULL;
112 *l = lst;
116 static
117 findit(def, type)
118 definition *def;
119 char *type;
121 return (streq(def->def_name, type));
125 static char *
126 fixit(type, orig)
127 char *type;
128 char *orig;
130 definition *def;
132 def = (definition *) FINDVAL(defined, type, findit);
133 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
134 return (orig);
136 switch (def->def.ty.rel) {
137 case REL_VECTOR:
138 return (def->def.ty.old_type);
139 case REL_ALIAS:
140 return (fixit(def->def.ty.old_type, orig));
141 default:
142 return (orig);
146 char *
147 fixtype(type)
148 char *type;
150 return (fixit(type, type));
153 char *
154 stringfix(type)
155 char *type;
157 if (streq(type, "string")) {
158 return ("wrapstring");
159 } else {
160 return (type);
164 void
165 ptype(prefix, type, follow)
166 char *prefix;
167 char *type;
168 int follow;
170 if (prefix != NULL) {
171 if (streq(prefix, "enum")) {
172 f_print(fout, "enum ");
173 } else {
174 f_print(fout, "struct ");
177 if (streq(type, "bool")) {
178 f_print(fout, "bool_t ");
179 } else if (streq(type, "string")) {
180 f_print(fout, "char *");
181 } else {
182 f_print(fout, "%s ", follow ? fixtype(type) : type);
187 static
188 typedefed(def, type)
189 definition *def;
190 char *type;
192 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
193 return (0);
194 } else {
195 return (streq(def->def_name, type));
199 isvectordef(type, rel)
200 char *type;
201 relation rel;
203 definition *def;
205 for (;;) {
206 switch (rel) {
207 case REL_VECTOR:
208 return (!streq(type, "string"));
209 case REL_ARRAY:
210 return (0);
211 case REL_POINTER:
212 return (0);
213 case REL_ALIAS:
214 def = (definition *) FINDVAL(defined, type, typedefed);
215 if (def == NULL) {
216 return (0);
218 type = def->def.ty.old_type;
219 rel = def->def.ty.rel;
225 static char *
226 locase(str)
227 char *str;
229 char c;
230 static char buf[100];
231 char *p = buf;
233 while (c = *str++) {
234 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
236 *p = 0;
237 return (buf);
241 void
242 pvname(pname, vnum)
243 char *pname;
244 char *vnum;
246 f_print(fout, "%s_%s", locase(pname), vnum);
251 * print a useful (?) error message, and then die
253 void
254 error(msg)
255 char *msg;
257 printwhere();
258 f_print(stderr, _("%s, line %d: "), infilename, linenum);
259 f_print(stderr, "%s\n", msg);
260 crash();
264 * Something went wrong, unlink any files that we may have created and then
265 * die.
267 crash()
269 int i;
271 for (i = 0; i < nfiles; i++) {
272 (void) unlink(outfiles[i]);
274 exit(1);
278 void
279 record_open(file)
280 char *file;
282 if (nfiles < NFILES) {
283 outfiles[nfiles++] = file;
284 } else {
285 f_print(stderr, _("too many files!\n"));
286 crash();
290 static char expectbuf[100];
291 static char *toktostr();
294 * error, token encountered was not the expected one
296 void
297 expected1(exp1)
298 tok_kind exp1;
300 s_print(expectbuf, _("expected '%s'"),
301 toktostr(exp1));
302 error(expectbuf);
306 * error, token encountered was not one of two expected ones
308 void
309 expected2(exp1, exp2)
310 tok_kind exp1, exp2;
312 s_print(expectbuf, _("expected '%s' or '%s'"),
313 toktostr(exp1),
314 toktostr(exp2));
315 error(expectbuf);
319 * error, token encountered was not one of 3 expected ones
321 void
322 expected3(exp1, exp2, exp3)
323 tok_kind exp1, exp2, exp3;
325 s_print(expectbuf, _("expected '%s', '%s' or '%s'"),
326 toktostr(exp1),
327 toktostr(exp2),
328 toktostr(exp3));
329 error(expectbuf);
332 void
333 tabify(f, tab)
334 FILE *f;
335 int tab;
337 while (tab--) {
338 (void) fputc('\t', f);
344 static token tokstrings[] = {
345 {TOK_IDENT, "identifier"},
346 {TOK_CONST, "const"},
347 {TOK_RPAREN, ")"},
348 {TOK_LPAREN, "("},
349 {TOK_RBRACE, "}"},
350 {TOK_LBRACE, "{"},
351 {TOK_LBRACKET, "["},
352 {TOK_RBRACKET, "]"},
353 {TOK_STAR, "*"},
354 {TOK_COMMA, ","},
355 {TOK_EQUAL, "="},
356 {TOK_COLON, ":"},
357 {TOK_SEMICOLON, ";"},
358 {TOK_UNION, "union"},
359 {TOK_STRUCT, "struct"},
360 {TOK_SWITCH, "switch"},
361 {TOK_CASE, "case"},
362 {TOK_DEFAULT, "default"},
363 {TOK_ENUM, "enum"},
364 {TOK_TYPEDEF, "typedef"},
365 {TOK_INT, "int"},
366 {TOK_SHORT, "short"},
367 {TOK_LONG, "long"},
368 {TOK_UNSIGNED, "unsigned"},
369 {TOK_DOUBLE, "double"},
370 {TOK_FLOAT, "float"},
371 {TOK_CHAR, "char"},
372 {TOK_STRING, "string"},
373 {TOK_OPAQUE, "opaque"},
374 {TOK_BOOL, "bool"},
375 {TOK_VOID, "void"},
376 {TOK_PROGRAM, "program"},
377 {TOK_VERSION, "version"},
378 {TOK_EOF, "??????"}
381 static char *
382 toktostr(kind)
383 tok_kind kind;
385 token *sp;
387 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
388 return (sp->str);
393 static
394 printbuf()
396 char c;
397 int i;
398 int cnt;
400 # define TABSIZE 4
402 for (i = 0; c = curline[i]; i++) {
403 if (c == '\t') {
404 cnt = 8 - (i % TABSIZE);
405 c = ' ';
406 } else {
407 cnt = 1;
409 while (cnt--) {
410 (void) fputc(c, stderr);
416 static
417 printwhere()
419 int i;
420 char c;
421 int cnt;
423 printbuf();
424 for (i = 0; i < where - curline; i++) {
425 c = curline[i];
426 if (c == '\t') {
427 cnt = 8 - (i % TABSIZE);
428 } else {
429 cnt = 1;
431 while (cnt--) {
432 (void) fputc('^', stderr);
435 (void) fputc('\n', stderr);