1 /* $NetBSD: rpc_util.c,v 1.9 2002/01/31 19:36:50 tv Exp $ */
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 or with the express written consent of
9 * Sun Microsystems, Inc.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid
[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI";
41 __RCSID("$NetBSD: rpc_util.c,v 1.9 2002/01/31 19:36:50 tv Exp $");
46 * rpc_util.c, Utility routines for the RPC protocol compiler
54 #include "rpc_parse.h"
57 #define ARGEXT "argument"
59 static void printwhere
__P((void));
61 char curline
[MAXLINESIZE
]; /* current read line */
62 char *where
= curline
; /* current point in line */
63 int linenum
= 0; /* current line number */
65 char *infilename
; /* input filename */
68 char *outfiles
[NFILES
]; /* output file names */
71 FILE *fout
; /* file pointer of current output */
72 FILE *fin
; /* file pointer of current input */
74 list
*defined
; /* list of defined things */
76 static char *toktostr
__P((tok_kind
));
77 static void printbuf
__P((void));
78 static void printwhere
__P((void));
79 static int findit
__P((definition
*, char *));
80 static char *fixit
__P((char *, char *));
81 static int typedefed
__P((definition
*, char *));
84 * Reinitialize the world
89 memset(curline
, 0, MAXLINESIZE
);
102 return (strcmp(a
, b
) == 0);
105 * find a value in a list
108 findval(lst
, val
, cmp
)
111 int (*cmp
) __P((definition
*, char *));
115 for (; lst
!= NULL
; lst
= lst
->next
) {
116 if ((*cmp
) (lst
->val
, val
)) {
123 * store a value in a list
134 for (l
= lstp
; *l
!= NULL
; l
= (list
**) & (*l
)->next
);
146 return (streq(def
->def_name
, type
));
156 def
= (definition
*) FINDVAL(defined
, type
, findit
);
157 if (def
== NULL
|| def
->def_kind
!= DEF_TYPEDEF
) {
160 switch (def
->def
.ty
.rel
) {
162 return (def
->def
.ty
.old_type
);
164 return (fixit(def
->def
.ty
.old_type
, orig
));
174 return (fixit(type
, type
));
181 if (streq(type
, "string")) {
182 return ("wrapstring");
189 ptype(prefix
, type
, follow
)
194 if (prefix
!= NULL
) {
195 if (streq(prefix
, "enum")) {
196 f_print(fout
, "enum ");
198 f_print(fout
, "struct ");
201 if (streq(type
, "bool")) {
202 f_print(fout
, "bool_t ");
204 if (streq(type
, "string")) {
205 f_print(fout
, "char *");
207 f_print(fout
, "%s ", follow
? fixtype(type
) : type
);
216 if (def
->def_kind
!= DEF_TYPEDEF
|| def
->def
.ty
.old_prefix
!= NULL
) {
219 return (streq(def
->def_name
, type
));
224 isvectordef(type
, rel
)
233 return (!streq(type
, "string"));
239 def
= (definition
*) FINDVAL(defined
, type
, typedefed
);
243 type
= def
->def
.ty
.old_type
;
244 rel
= def
->def
.ty
.rel
;
254 static char buf
[100];
257 while ((c
= *str
++) != '\0') {
258 *p
++ = (c
>= 'A' && c
<= 'Z') ? (c
- 'A' + 'a') : c
;
265 pvname_svc(pname
, vnum
)
269 f_print(fout
, "%s_%s_svc", locase(pname
), vnum
);
277 f_print(fout
, "%s_%s", locase(pname
), vnum
);
280 * print a useful (?) error message, and then die
287 f_print(stderr
, "%s, line %d: ", infilename
, linenum
);
288 f_print(stderr
, "%s\n", msg
);
292 * Something went wrong, unlink any files that we may have created and then
300 for (i
= 0; i
< nfiles
; i
++) {
301 (void) unlink(outfiles
[i
]);
310 if (nfiles
< NFILES
) {
311 outfiles
[nfiles
++] = file
;
313 f_print(stderr
, "too many files!\n");
318 static char expectbuf
[100];
321 * error, token encountered was not the expected one
327 s_print(expectbuf
, "expected '%s'",
332 * error, token encountered was not one of two expected ones
335 expected2(exp1
, exp2
)
338 s_print(expectbuf
, "expected '%s' or '%s'",
344 * error, token encountered was not one of 3 expected ones
347 expected3(exp1
, exp2
, exp3
)
348 tok_kind exp1
, exp2
, exp3
;
350 s_print(expectbuf
, "expected '%s', '%s' or '%s'",
363 (void) fputc('\t', f
);
368 static token tokstrings
[] = {
369 {TOK_IDENT
, "identifier"},
370 {TOK_CONST
, "const"},
381 {TOK_SEMICOLON
, ";"},
382 {TOK_UNION
, "union"},
383 {TOK_STRUCT
, "struct"},
384 {TOK_SWITCH
, "switch"},
386 {TOK_DEFAULT
, "default"},
388 {TOK_TYPEDEF
, "typedef"},
390 {TOK_SHORT
, "short"},
392 {TOK_UNSIGNED
, "unsigned"},
393 {TOK_DOUBLE
, "double"},
394 {TOK_FLOAT
, "float"},
396 {TOK_STRING
, "string"},
397 {TOK_OPAQUE
, "opaque"},
400 {TOK_PROGRAM
, "program"},
401 {TOK_VERSION
, "version"},
411 for (sp
= tokstrings
; sp
->kind
!= TOK_EOF
&& sp
->kind
!= kind
; sp
++);
424 for (i
= 0; (c
= curline
[i
]) != '\0'; i
++) {
426 cnt
= 8 - (i
% TABSIZE
);
432 (void) fputc(c
, stderr
);
445 for (i
= 0; i
< where
- curline
; i
++) {
448 cnt
= 8 - (i
% TABSIZE
);
453 (void) fputc('^', stderr
);
456 (void) fputc('\n', stderr
);
460 make_argname(pname
, vname
)
466 name
= (char *) malloc(strlen(pname
) + strlen(vname
) + strlen(ARGEXT
) + 3);
468 fprintf(stderr
, "failed in malloc");
471 sprintf(name
, "%s_%s_%s", locase(pname
), vname
, ARGEXT
);
475 bas_type
*typ_list_h
;
476 bas_type
*typ_list_t
;
485 if ((ptr
= (bas_type
*) malloc(sizeof(bas_type
))) == (bas_type
*) NULL
) {
486 fprintf(stderr
, "failed in malloc");
492 if (typ_list_t
== NULL
) {
496 typ_list_t
->next
= ptr
;
510 while (ptr
!= NULL
) {
511 if (strcmp(ptr
->name
, type
) == 0)