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]
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
33 * University Acknowledgment- Portions of this document are derived from
34 * software developed by the University of California, Berkeley, and its
39 * rpc_util.c, Utility routines for the RPC protocol compiler
47 #include "rpc_parse.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 */
63 char *outfiles
[NFILES
]; /* output file names */
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
77 (void) memset(curline
, 0, MAXLINESIZE
);
87 streq(char *a
, char *b
)
89 return (strcmp(a
, b
) == 0);
93 * find a value in a list
96 findval(list
*lst
, char *val
, int (*cmp
)())
98 for (; lst
!= NULL
; lst
= lst
->next
) {
99 if ((*cmp
) (lst
->val
, val
))
106 * store a value in a list
109 storeval(list
**lstp
, definition
*val
)
114 for (l
= lstp
; *l
!= NULL
; l
= (list
**)&(*l
)->next
)
116 lst
= calloc(1, sizeof (list
));
123 findit(definition
*def
, char *type
)
125 return (streq(def
->def_name
, type
));
129 fixit(char *type
, char *orig
)
133 def
= (definition
*)FINDVAL(defined
, type
, findit
);
134 if (def
== NULL
|| def
->def_kind
!= DEF_TYPEDEF
)
136 switch (def
->def
.ty
.rel
) {
138 if (streq(def
->def
.ty
.old_type
, "opaque"))
140 return (def
->def
.ty
.old_type
);
143 return (fixit(def
->def
.ty
.old_type
, orig
));
152 return (fixit(type
, type
));
156 stringfix(char *type
)
158 if (streq(type
, "string"))
159 return ("wrapstring");
164 ptype(char *prefix
, char *type
, int follow
)
166 if (prefix
!= NULL
) {
167 if (streq(prefix
, "enum")) {
168 f_print(fout
, "enum ");
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 ");
180 f_print(fout
, "%s ", follow
? fixtype(type
) : type
);
185 typedefed(definition
*def
, char *type
)
187 if (def
->def_kind
!= DEF_TYPEDEF
|| def
->def
.ty
.old_prefix
!= NULL
)
189 return (streq(def
->def_name
, type
));
193 isvectordef(char *type
, relation rel
)
200 return (!streq(type
, "string"));
206 def
= (definition
*)FINDVAL(defined
, type
, typedefed
);
209 type
= def
->def
.ty
.old_type
;
210 rel
= def
->def
.ty
.rel
;
219 static char buf
[100];
223 *p
++ = (c
>= 'A' && c
<= 'Z') ? (c
- 'A' + 'a') : c
;
229 pvname_svc(char *pname
, char *vnum
)
231 f_print(fout
, "%s_%s_svc", locase(pname
), vnum
);
235 pvname(char *pname
, char *vnum
)
237 f_print(fout
, "%s_%s", locase(pname
), vnum
);
241 * print a useful (?) error message, and then die
247 f_print(stderr
, "%s, line %d: ", infilename
, linenum
);
248 f_print(stderr
, "%s\n", msg
);
253 * Something went wrong, unlink any files that we may have created and then
261 for (i
= 0; i
< nfiles
; i
++)
262 (void) unlink(outfiles
[i
]);
267 record_open(char *file
)
269 if (nfiles
< NFILES
) {
270 outfiles
[nfiles
++] = file
;
273 f_print(stderr
, "too many files!\n");
277 static char expectbuf
[100];
278 static char *toktostr();
281 * error, token encountered was not the expected one
284 expected1(tok_kind exp1
)
286 (void) snprintf(expectbuf
,
287 sizeof (expectbuf
), "expected '%s'", toktostr(exp1
));
292 * error, token encountered was not one of two expected ones
295 expected2(tok_kind exp1
, tok_kind exp2
)
297 (void) snprintf(expectbuf
,
298 sizeof (expectbuf
), "expected '%s' or '%s'", toktostr(exp1
),
304 * error, token encountered was not one of 3 expected ones
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
));
316 tabify(FILE *f
, int tab
)
319 (void) fputc('\t', f
);
322 static token tokstrings
[] = {
323 {TOK_IDENT
, "identifier"},
324 {TOK_CONST
, "const"},
335 {TOK_SEMICOLON
, ";"},
336 {TOK_UNION
, "union"},
337 {TOK_STRUCT
, "struct"},
338 {TOK_SWITCH
, "switch"},
340 {TOK_DEFAULT
, "default"},
342 {TOK_TYPEDEF
, "typedef"},
344 {TOK_SHORT
, "short"},
346 {TOK_UNSIGNED
, "unsigned"},
347 {TOK_DOUBLE
, "double"},
348 {TOK_FLOAT
, "float"},
350 {TOK_STRING
, "string"},
351 {TOK_OPAQUE
, "opaque"},
354 {TOK_PROGRAM
, "program"},
355 {TOK_VERSION
, "version"},
360 toktostr(tok_kind kind
)
364 for (sp
= tokstrings
; sp
->kind
!= TOK_EOF
&& sp
->kind
!= kind
; sp
++)
378 for (i
= 0; (c
= curline
[i
]) != '\0'; i
++) {
380 cnt
= 8 - (i
% TABSIZE
);
386 (void) fputc(c
, stderr
);
398 for (i
= 0; i
< where
- curline
; i
++) {
401 cnt
= 8 - (i
% TABSIZE
);
406 (void) fputc('^', stderr
);
408 (void) fputc('\n', stderr
);
412 make_argname(char *pname
, char *vname
)
417 nlen
= strlen(pname
) + strlen(vname
) + strlen(ARGEXT
) + 3;
420 (void) fprintf(stderr
, "failed in malloc");
423 (void) snprintf(name
, nlen
, "%s_%s_%s", locase(pname
), vname
, ARGEXT
);
427 bas_type
*typ_list_h
;
428 bas_type
*typ_list_t
;
431 add_type(int len
, char *type
)
435 if ((ptr
= malloc(sizeof (bas_type
))) == NULL
) {
436 (void) fprintf(stderr
, "failed in malloc");
443 if (typ_list_t
== NULL
) {
447 typ_list_t
->next
= ptr
;
454 find_type(char *type
)
459 while (ptr
!= NULL
) {
460 if (strcmp(ptr
->name
, type
) == 0)