2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpc_util.c 1.14 93/07/05 SMI; 1.11 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_util.c,v 1.10 2005/11/13 21:17:24 dwmalone Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_util.c,v 1.4 2004/06/19 16:40:36 joerg Exp $
35 * rpc_util.c, Utility routines for the RPC protocol compiler
36 * Copyright (C) 1989, Sun Microsystems, Inc.
43 #include "rpc_parse.h"
47 #define ARGEXT "argument"
49 char curline
[MAXLINESIZE
]; /* current read line */
50 char *where
= curline
; /* current point in line */
51 int linenum
= 0; /* current line number */
53 const char *infilename
; /* input filename */
56 const char *outfiles
[NFILES
]; /* output file names */
59 FILE *fout
; /* file pointer of current output */
60 FILE *fin
; /* file pointer of current input */
62 list
*defined
; /* list of defined things */
64 static void printwhere(void);
67 * Reinitialize the world
72 memset(curline
, 0, MAXLINESIZE
);
82 streq(const char *a
, const char *b
)
84 return(strcmp(a
, b
) == 0);
88 * find a value in a list
91 findval(list
*lst
, const char *val
, int (*cmp
)(definition
*, const char *))
93 for (; lst
!= NULL
; lst
= lst
->next
) {
94 if ((*cmp
) (lst
->val
, val
))
101 * store a value in a list
104 storeval(list
**lstp
, definition
*val
)
109 for (l
= lstp
; *l
!= NULL
; l
= (list
**) & (*l
)->next
)
118 findit(definition
*def
, const char *type
)
120 return(streq(def
->def_name
, type
));
124 fixit(const char *type
, const char *orig
)
128 def
= (definition
*) FINDVAL(defined
, type
, findit
);
129 if (def
== NULL
|| def
->def_kind
!= DEF_TYPEDEF
)
131 switch (def
->def
.ty
.rel
) {
133 if (streq(def
->def
.ty
.old_type
, "opaque"))
136 return(def
->def
.ty
.old_type
);
139 return(fixit(def
->def
.ty
.old_type
, orig
));
146 fixtype(const char *type
)
148 return(fixit(type
, type
));
152 stringfix(const char *type
)
154 if (streq(type
, "string"))
155 return("wrapstring");
161 ptype(const char *prefix
, const char *type
, int follow
)
163 if (prefix
!= NULL
) {
164 if (streq(prefix
, "enum"))
165 f_print(fout
, "enum ");
167 f_print(fout
, "struct ");
169 if (streq(type
, "bool"))
170 f_print(fout
, "bool_t ");
171 else if (streq(type
, "string"))
172 f_print(fout
, "char *");
174 f_print(fout
, "%s ", follow
? fixtype(type
) : type
);
178 typedefed(definition
*def
, const char *type
)
180 if (def
->def_kind
!= DEF_TYPEDEF
|| def
->def
.ty
.old_prefix
!= NULL
)
183 return(streq(def
->def_name
, type
));
187 isvectordef(const char *type
, relation rel
)
194 return(!streq(type
, "string"));
200 def
= (definition
*) FINDVAL(defined
, type
, typedefed
);
204 type
= def
->def
.ty
.old_type
;
205 rel
= def
->def
.ty
.rel
;
213 locase(const char *str
)
216 static char buf
[100];
219 while ((c
= *str
++) != 0)
220 *p
++ = (c
>= 'A' && c
<= 'Z') ? (c
- 'A' + 'a') : c
;
226 pvname_svc(const char *pname
, const char *vnum
)
228 f_print(fout
, "%s_%s_svc", locase(pname
), vnum
);
232 pvname(const char *pname
, const char *vnum
)
234 f_print(fout
, "%s_%s", locase(pname
), vnum
);
238 * print a useful (?) error message, and then die
241 error(const char *msg
)
244 warnx("%s, line %d: %s", infilename
, linenum
, msg
);
249 * Something went wrong, unlink any files that we may have created and then
257 for (i
= 0; i
< nfiles
; i
++)
263 record_open(const char *file
)
265 if (nfiles
< NFILES
) {
266 outfiles
[nfiles
++] = file
;
268 warnx("too many files");
273 static char expectbuf
[100];
274 static const char *toktostr(tok_kind
);
277 * error, token encountered was not the expected one
280 expected1(tok_kind exp1
)
282 s_print(expectbuf
, "expected '%s'", toktostr(exp1
));
287 * error, token encountered was not one of two expected ones
290 expected2(tok_kind exp1
, tok_kind exp2
)
292 s_print(expectbuf
, "expected '%s' or '%s'", toktostr(exp1
),
298 * error, token encountered was not one of 3 expected ones
301 expected3(tok_kind exp1
, tok_kind exp2
, tok_kind exp3
)
303 s_print(expectbuf
, "expected '%s', '%s' or '%s'", toktostr(exp1
),
304 toktostr(exp2
), toktostr(exp3
));
309 tabify(FILE *f
, int tab
)
316 static token tokstrings
[] = {
317 {TOK_IDENT
, "identifier"},
318 {TOK_CONST
, "const"},
329 {TOK_SEMICOLON
, ";"},
330 {TOK_UNION
, "union"},
331 {TOK_STRUCT
, "struct"},
332 {TOK_SWITCH
, "switch"},
334 {TOK_DEFAULT
, "default"},
336 {TOK_TYPEDEF
, "typedef"},
338 {TOK_SHORT
, "short"},
340 {TOK_UNSIGNED
, "unsigned"},
341 {TOK_DOUBLE
, "double"},
342 {TOK_FLOAT
, "float"},
344 {TOK_STRING
, "string"},
345 {TOK_OPAQUE
, "opaque"},
348 {TOK_PROGRAM
, "program"},
349 {TOK_VERSION
, "version"},
354 toktostr(tok_kind kind
)
358 for (sp
= tokstrings
; sp
->kind
!= TOK_EOF
&& sp
->kind
!= kind
; sp
++)
372 for (i
= 0; (c
= curline
[i
]); i
++) {
374 cnt
= 8 - (i
% TABSIZE
);
392 for (i
= 0; i
< where
- curline
; i
++) {
395 cnt
= 8 - (i
% TABSIZE
);
406 make_argname(const char *pname
, const char *vname
)
410 name
= xmalloc(strlen(pname
) + strlen(vname
) + strlen(ARGEXT
) + 3);
411 sprintf(name
, "%s_%s_%s", locase(pname
), vname
, ARGEXT
);
415 bas_type
*typ_list_h
;
416 bas_type
*typ_list_t
;
419 add_type(int len
, const char *type
)
423 ptr
= XALLOC(bas_type
);
428 if (typ_list_t
== NULL
) {
432 typ_list_t
->next
= ptr
;
439 find_type(const char *type
)
446 if (strcmp(ptr
->name
, type
) == 0)
459 if ((p
= malloc(size
)) == NULL
) {
460 warnx("malloc failed");
467 xrealloc(void *ptr
, size_t size
)
471 if ((p
= realloc(ptr
, size
)) == NULL
) {
472 warnx("realloc failed");
479 xstrdup(const char *str
)
483 if ((p
= strdup(str
)) == NULL
) {
484 warnx("strdup failed");