Unleashed v1.4
[unleashed.git] / usr / src / cmd / rpcgen / rpc_util.c
blob837f92d1929cfbfb6c0b867aa80b0aedbb266a20
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
29 * University Copyright- Copyright (c) 1982, 1986, 1988
30 * The Regents of the University of California
31 * All Rights Reserved
33 * University Acknowledgment- Portions of this document are derived from
34 * software developed by the University of California, Berkeley, and its
35 * contributors.
39 * rpc_util.c, Utility routines for the RPC protocol compiler
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include <string.h>
46 #include "rpc_scan.h"
47 #include "rpc_parse.h"
48 #include "rpc_util.h"
50 extern void crash(void);
52 static void printwhere(void);
54 #define ARGEXT "argument"
56 char curline[MAXLINESIZE]; /* current read line */
57 char *where = curline; /* current point in line */
58 int linenum = 0; /* current line number */
60 char *infilename; /* input filename */
62 #define NFILES 15
63 char *outfiles[NFILES]; /* output file names */
64 int nfiles;
66 FILE *fout; /* file pointer of current output */
67 FILE *fin; /* file pointer of current input */
69 list *defined; /* list of defined things */
72 * Reinitialize the world
74 void
75 reinitialize(void)
77 (void) memset(curline, 0, MAXLINESIZE);
78 where = curline;
79 linenum = 0;
80 defined = NULL;
84 * string equality
86 int
87 streq(char *a, char *b)
89 return (strcmp(a, b) == 0);
93 * find a value in a list
95 definition *
96 findval(list *lst, char *val, int (*cmp)())
98 for (; lst != NULL; lst = lst->next) {
99 if ((*cmp) (lst->val, val))
100 return (lst->val);
102 return (NULL);
106 * store a value in a list
108 void
109 storeval(list **lstp, definition *val)
111 list **l;
112 list *lst;
114 for (l = lstp; *l != NULL; l = (list **)&(*l)->next)
115 /* LOOP */;
116 lst = calloc(1, sizeof (list));
117 lst->val = val;
118 lst->next = NULL;
119 *l = lst;
122 static int
123 findit(definition *def, char *type)
125 return (streq(def->def_name, type));
128 static char *
129 fixit(char *type, char *orig)
131 definition *def;
133 def = (definition *)FINDVAL(defined, type, findit);
134 if (def == NULL || def->def_kind != DEF_TYPEDEF)
135 return (orig);
136 switch (def->def.ty.rel) {
137 case REL_VECTOR:
138 if (streq(def->def.ty.old_type, "opaque"))
139 return ("char");
140 return (def->def.ty.old_type);
142 case REL_ALIAS:
143 return (fixit(def->def.ty.old_type, orig));
144 default:
145 return (orig);
149 char *
150 fixtype(char *type)
152 return (fixit(type, type));
155 char *
156 stringfix(char *type)
158 if (streq(type, "string"))
159 return ("wrapstring");
160 return (type);
163 void
164 ptype(char *prefix, char *type, int follow)
166 if (prefix != NULL) {
167 if (streq(prefix, "enum")) {
168 f_print(fout, "enum ");
169 } else {
170 f_print(fout, "struct ");
173 if (streq(type, "bool")) {
174 f_print(fout, "bool_t ");
175 } else if (streq(type, "string")) {
176 f_print(fout, "char *");
177 } else if (streq(type, "oneway")) {
178 f_print(fout, "void ");
179 } else {
180 f_print(fout, "%s ", follow ? fixtype(type) : type);
184 static int
185 typedefed(definition *def, char *type)
187 if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL)
188 return (0);
189 return (streq(def->def_name, type));
193 isvectordef(char *type, relation rel)
195 definition *def;
197 for (;;) {
198 switch (rel) {
199 case REL_VECTOR:
200 return (!streq(type, "string"));
201 case REL_ARRAY:
202 return (0);
203 case REL_POINTER:
204 return (0);
205 case REL_ALIAS:
206 def = (definition *)FINDVAL(defined, type, typedefed);
207 if (def == NULL)
208 return (0);
209 type = def->def.ty.old_type;
210 rel = def->def.ty.rel;
215 char *
216 locase(char *str)
218 char c;
219 static char buf[100];
220 char *p = buf;
222 while (c = *str++)
223 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
224 *p = 0;
225 return (buf);
228 void
229 pvname_svc(char *pname, char *vnum)
231 f_print(fout, "%s_%s_svc", locase(pname), vnum);
234 void
235 pvname(char *pname, char *vnum)
237 f_print(fout, "%s_%s", locase(pname), vnum);
241 * print a useful (?) error message, and then die
243 void
244 error(char *msg)
246 printwhere();
247 f_print(stderr, "%s, line %d: ", infilename, linenum);
248 f_print(stderr, "%s\n", msg);
249 crash();
253 * Something went wrong, unlink any files that we may have created and then
254 * die.
256 void
257 crash(void)
259 int i;
261 for (i = 0; i < nfiles; i++)
262 (void) unlink(outfiles[i]);
263 exit(1);
266 void
267 record_open(char *file)
269 if (nfiles < NFILES) {
270 outfiles[nfiles++] = file;
271 return;
273 f_print(stderr, "too many files!\n");
274 crash();
277 static char expectbuf[100];
278 static char *toktostr();
281 * error, token encountered was not the expected one
283 void
284 expected1(tok_kind exp1)
286 (void) snprintf(expectbuf,
287 sizeof (expectbuf), "expected '%s'", toktostr(exp1));
288 error(expectbuf);
292 * error, token encountered was not one of two expected ones
294 void
295 expected2(tok_kind exp1, tok_kind exp2)
297 (void) snprintf(expectbuf,
298 sizeof (expectbuf), "expected '%s' or '%s'", toktostr(exp1),
299 toktostr(exp2));
300 error(expectbuf);
304 * error, token encountered was not one of 3 expected ones
306 void
307 expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3)
309 (void) snprintf(expectbuf,
310 sizeof (expectbuf), "expected '%s', '%s' or '%s'",
311 toktostr(exp1), toktostr(exp2), toktostr(exp3));
312 error(expectbuf);
315 void
316 tabify(FILE *f, int tab)
318 while (tab--)
319 (void) fputc('\t', f);
322 static token tokstrings[] = {
323 {TOK_IDENT, "identifier"},
324 {TOK_CONST, "const"},
325 {TOK_RPAREN, ")"},
326 {TOK_LPAREN, "("},
327 {TOK_RBRACE, "}"},
328 {TOK_LBRACE, "{"},
329 {TOK_LBRACKET, "["},
330 {TOK_RBRACKET, "]"},
331 {TOK_STAR, "*"},
332 {TOK_COMMA, ","},
333 {TOK_EQUAL, "="},
334 {TOK_COLON, ":"},
335 {TOK_SEMICOLON, ";"},
336 {TOK_UNION, "union"},
337 {TOK_STRUCT, "struct"},
338 {TOK_SWITCH, "switch"},
339 {TOK_CASE, "case"},
340 {TOK_DEFAULT, "default"},
341 {TOK_ENUM, "enum"},
342 {TOK_TYPEDEF, "typedef"},
343 {TOK_INT, "int"},
344 {TOK_SHORT, "short"},
345 {TOK_LONG, "long"},
346 {TOK_UNSIGNED, "unsigned"},
347 {TOK_DOUBLE, "double"},
348 {TOK_FLOAT, "float"},
349 {TOK_CHAR, "char"},
350 {TOK_STRING, "string"},
351 {TOK_OPAQUE, "opaque"},
352 {TOK_BOOL, "bool"},
353 {TOK_VOID, "void"},
354 {TOK_PROGRAM, "program"},
355 {TOK_VERSION, "version"},
356 {TOK_EOF, "??????"}
359 static char *
360 toktostr(tok_kind kind)
362 token *sp;
364 for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++)
365 /* LOOP */;
366 return (sp->str);
369 static void
370 printbuf(void)
372 char c;
373 int i;
374 int cnt;
376 #define TABSIZE 4
378 for (i = 0; (c = curline[i]) != '\0'; i++) {
379 if (c == '\t') {
380 cnt = 8 - (i % TABSIZE);
381 c = ' ';
382 } else {
383 cnt = 1;
385 while (cnt--)
386 (void) fputc(c, stderr);
390 static void
391 printwhere(void)
393 int i;
394 char c;
395 int cnt;
397 printbuf();
398 for (i = 0; i < where - curline; i++) {
399 c = curline[i];
400 if (c == '\t') {
401 cnt = 8 - (i % TABSIZE);
402 } else {
403 cnt = 1;
405 while (cnt--)
406 (void) fputc('^', stderr);
408 (void) fputc('\n', stderr);
411 char *
412 make_argname(char *pname, char *vname)
414 char *name;
415 size_t nlen;
417 nlen = strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3;
418 name = malloc(nlen);
419 if (name == NULL) {
420 (void) fprintf(stderr, "failed in malloc");
421 exit(1);
423 (void) snprintf(name, nlen, "%s_%s_%s", locase(pname), vname, ARGEXT);
424 return (name);
427 bas_type *typ_list_h;
428 bas_type *typ_list_t;
430 void
431 add_type(int len, char *type)
433 bas_type *ptr;
435 if ((ptr = malloc(sizeof (bas_type))) == NULL) {
436 (void) fprintf(stderr, "failed in malloc");
437 exit(1);
440 ptr->name = type;
441 ptr->length = len;
442 ptr->next = NULL;
443 if (typ_list_t == NULL) {
444 typ_list_t = ptr;
445 typ_list_h = ptr;
446 } else {
447 typ_list_t->next = ptr;
448 typ_list_t = ptr;
453 bas_type *
454 find_type(char *type)
456 bas_type *ptr;
458 ptr = typ_list_h;
459 while (ptr != NULL) {
460 if (strcmp(ptr->name, type) == 0)
461 return (ptr);
462 ptr = ptr->next;
464 return (NULL);