Pull in patch that properly includes stdint.h
[pkg-k5-afs_openafs.git] / src / rxgen / rpc_util.c
blob2d5c1a1c1d47ae2f4c2bef3b4e9feb173dac029f
1 /* @(#)rpc_util.c 1.2 87/11/24 3.9 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
32 * rpc_util.c, Utility routines for the RPC protocol compiler
33 * Copyright (C) 1987, Sun Microsystems, Inc.
35 #include <afsconfig.h>
36 #include <afs/param.h>
39 #include <stdio.h>
40 #include <string.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #ifdef HAVE_STDLIB_H
45 #include <stdlib.h>
46 #endif
47 #include "rpc_scan.h"
48 #include "rpc_parse.h"
49 #include "rpc_util.h"
51 char curline[MAXLINESIZE]; /* current read line */
52 char *where = curline; /* current point in line */
53 int linenum = 0; /* current line number */
55 char *infilename; /* input filename */
57 #define NFILES 6
58 char *outfiles[NFILES]; /* output file names */
59 int nfiles;
61 FILE *fout; /* file pointer of current output */
62 FILE *fin; /* file pointer of current input */
64 list *defined; /* list of defined things */
66 /* static prototypes */
67 static int findit(definition * def, char *type);
68 static char *fixit(char *type, char *orig);
69 static int typedefed(definition * def, char *type);
70 static char *locase(char *str);
71 static char *toktostr(tok_kind kind);
72 static void printbuf(void);
73 static void printwhere(void);
77 * Reinitialize the world
79 void
80 reinitialize(void)
82 int i;
83 memset(curline, 0, MAXLINESIZE);
84 where = curline;
85 linenum = 0;
86 defined = NULL;
87 special_defined = typedef_defined = uniondef_defined = NULL;
88 PackageIndex = -1;
89 master_opcodenumber = 99999;
90 master_highest_opcode = 0;
91 no_of_stat_funcs = 0;
92 for (i = 0; i < MAX_PACKAGES; i++) {
93 no_of_stat_funcs_header[i] = 0;
98 * string equality
101 streq(char *a, char *b)
103 return (strcmp(a, b) == 0);
107 * find a value in a list
109 char *
110 findval(list * lst, char *val, int (*cmp) (definition * def, char *type))
112 for (; lst != NULL; lst = lst->next) {
113 if ((*cmp) ((definition *) lst->val, val)) {
114 return (lst->val);
117 return (NULL);
121 * store a value in a list
123 void
124 storeval(list ** lstp, char *val)
126 list **l;
127 list *lst;
129 for (l = lstp; *l != NULL; l = (list **) & (*l)->next);
130 lst = ALLOC(list);
131 lst->val = val;
132 lst->next = NULL;
133 *l = lst;
137 static int
138 findit(definition * def, char *type)
140 return (streq(def->def_name, type));
144 static char *
145 fixit(char *type, char *orig)
147 definition *def;
149 def = (definition *) FINDVAL(defined, type, findit);
150 if (def == NULL || def->def_kind != DEF_TYPEDEF) {
151 return (orig);
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);
163 char *
164 fixtype(char *type)
166 return (fixit(type, type));
169 char *
170 stringfix(char *type)
172 if (streq(type, "string")) {
173 return ("wrapstring");
174 } else {
175 return (type);
179 void
180 ptype(char *prefix, char *type, int follow)
182 if (prefix != NULL) {
183 if (streq(prefix, "enum")) {
184 f_print(fout, "enum ");
185 } else {
186 f_print(fout, "struct ");
189 if (streq(type, "bool")) {
190 f_print(fout, "bool_t ");
191 } else if (streq(type, "string")) {
192 f_print(fout, "char *");
193 } else {
194 f_print(fout, "%s ", follow ? fixtype(type) : type);
199 static int
200 typedefed(definition * def, char *type)
202 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) {
203 return (0);
204 } else {
205 return (streq(def->def_name, type));
210 isvectordef(char *type, relation rel)
212 definition *def;
214 for (;;) {
215 switch (rel) {
216 case REL_VECTOR:
217 return (!streq(type, "string"));
218 case REL_ARRAY:
219 return (0);
220 case REL_POINTER:
221 return (0);
222 case REL_ALIAS:
223 def = (definition *) FINDVAL(defined, type, typedefed);
224 if (def == NULL) {
225 return (0);
227 type = def->def.ty.old_type;
228 rel = def->def.ty.rel;
234 static char *
235 locase(char *str)
237 char c;
238 static char buf[100];
239 char *p = buf;
241 while ((c = *str++)) {
242 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
244 *p = 0;
245 return (buf);
249 void
250 pvname(char *pname, char *vnum)
252 f_print(fout, "%s_%s", locase(pname), vnum);
257 * print a useful (?) error message, and then die
259 void
260 error(char *msg)
262 printwhere();
263 f_print(stderr, "%s, line %d: ", infilename, linenum);
264 f_print(stderr, "%s\n", msg);
265 crash();
269 * Something went wrong, unlink any files that we may have created and then
270 * die.
272 void
273 crash(void)
275 int i;
277 for (i = 0; i < nfiles; i++) {
278 (void)unlink(outfiles[i]);
280 exit(1);
284 void
285 record_open(char *file)
287 if (nfiles < NFILES) {
288 outfiles[nfiles++] = file;
289 } else {
290 f_print(stderr, "too many files!\n");
291 crash();
295 /* buffer shared for all of the expected* routines */
296 static char expectbuf[100];
299 * error, token encountered was not the expected one
301 void
302 expected1(tok_kind exp1)
304 s_print(expectbuf, "expected '%s'", toktostr(exp1));
305 error(expectbuf);
309 * error, token encountered was not one of two expected ones
311 void
312 expected2(tok_kind exp1, tok_kind exp2)
314 s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1),
315 toktostr(exp2));
316 error(expectbuf);
320 * error, token encountered was not one of 3 expected ones
322 void
323 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
325 s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1),
326 toktostr(exp2), toktostr(exp3));
327 error(expectbuf);
332 * error, token encountered was not one of 4 expected ones
334 void
335 expected4(tok_kind exp1, tok_kind exp2, tok_kind exp3, tok_kind exp4)
337 sprintf(expectbuf, "expected '%s', '%s', '%s', or '%s'", toktostr(exp1),
338 toktostr(exp2), toktostr(exp3), toktostr(exp4));
339 error(expectbuf);
342 void
343 tabify(FILE * f, int tab)
345 if (scan_print)
346 while (tab--) {
347 (void)fputc('\t', f);
351 static token tokstrings[] = {
352 {TOK_IDENT, "identifier"},
353 {TOK_CONST, "const"},
354 {TOK_RPAREN, ")"},
355 {TOK_LPAREN, "("},
356 {TOK_RBRACE, "}"},
357 {TOK_LBRACE, "{"},
358 {TOK_LBRACKET, "["},
359 {TOK_RBRACKET, "]"},
360 {TOK_STAR, "*"},
361 {TOK_COMMA, ","},
362 {TOK_EQUAL, "="},
363 {TOK_COLON, ":"},
364 {TOK_SEMICOLON, ";"},
365 {TOK_UNION, "union"},
366 {TOK_STRUCT, "struct"},
367 {TOK_SWITCH, "switch"},
368 {TOK_CASE, "case"},
369 {TOK_DEFAULT, "default"},
370 {TOK_ENUM, "enum"},
371 {TOK_TYPEDEF, "typedef"},
372 {TOK_INT, "int"},
373 {TOK_SHORT, "short"},
374 {TOK_INT32, "afs_int32"}, /* XXX */
375 {TOK_UNSIGNED, "unsigned"},
376 {TOK_DOUBLE, "double"},
377 {TOK_FLOAT, "float"},
378 {TOK_CHAR, "char"},
379 {TOK_STRING, "string"},
380 {TOK_OPAQUE, "opaque"},
381 {TOK_BOOL, "bool"},
382 {TOK_VOID, "void"},
383 {TOK_PROGRAM, "program"},
384 {TOK_VERSION, "version"},
385 {TOK_PACKAGE, "package"},
386 {TOK_PREFIX, "prefix"},
387 {TOK_STATINDEX, "statindex"},
388 {TOK_SPECIAL, "special"},
389 {TOK_STARTINGOPCODE, "startingopcode"},
390 {TOK_CUSTOMIZED, "customized"},
391 {TOK_PROC, "proc"},
392 {TOK_SPLITPREFIX, "splitprefix"},
393 {TOK_SPLIT, "split"},
394 {TOK_MULTI, "multi"},
395 {TOK_IN, "IN"},
396 {TOK_OUT, "OUT"},
397 {TOK_INOUT, "INOUT"},
398 {TOK_AFSUUID, "afsUUID"},
399 {TOK_EOF, "??????"}
402 static char *
403 toktostr(tok_kind kind)
405 token *sp;
407 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++);
408 return (sp->str);
413 static void
414 printbuf(void)
416 char c;
417 int i;
418 int cnt;
420 # define TABSIZE 4
422 for (i = 0; (c = curline[i]); i++) {
423 if (c == '\t') {
424 cnt = 8 - (i % TABSIZE);
425 c = ' ';
426 } else {
427 cnt = 1;
429 while (cnt--) {
430 fputc(c, stderr);
436 static void
437 printwhere(void)
439 int i;
440 char c;
441 int cnt;
443 printbuf();
444 for (i = 0; i < where - curline; i++) {
445 c = curline[i];
446 if (c == '\t') {
447 cnt = 8 - (i % TABSIZE);
448 } else {
449 cnt = 1;
451 while (cnt--) {
452 fputc('^', stderr);
455 fputc('\n', stderr);