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 or with the express written consent of
8 * Sun Microsystems, Inc.
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.
28 * Mountain View, California 94043
32 * From: @(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char util_rcsid
[] =
40 * rpc_util.c, Utility routines for the RPC protocol compiler
47 #include "rpc_parse.h"
51 #define ARGEXT "argument"
53 char curline
[MAXLINESIZE
]; /* current read line */
54 const char *where
= curline
; /* current point in line */
55 int linenum
= 0; /* current line number */
57 const char *infilename
; /* input filename */
60 const char *outfiles
[NFILES
]; /* output file names */
63 FILE *fout
; /* file pointer of current output */
64 FILE *fin
; /* file pointer of current input */
66 list
*defined
; /* list of defined things */
68 static int findit (const definition
* def
, const char *type
);
69 static const char *fixit (const char *type
, const char *orig
);
70 static int typedefed (const definition
* def
, const char *type
);
71 static const char *toktostr (tok_kind kind
);
72 static void printbuf (void);
73 static void printwhere (void);
76 * Reinitialize the world
81 memset (curline
, 0, MAXLINESIZE
);
91 streq (const char *a
, const char *b
)
93 return strcmp (a
, b
) == 0;
97 * find a value in a list
100 findval (list
*lst
, const char *val
,
101 int (*cmp
) (const definition
*, const char *))
104 for (; lst
!= NULL
; lst
= lst
->next
)
106 if (cmp (lst
->val
, val
))
115 * store a value in a list
118 storeval (list
**lstp
, definition
*val
)
124 for (l
= lstp
; *l
!= NULL
; l
= (list
**) & (*l
)->next
);
132 findit (const definition
* def
, const char *type
)
134 return streq (def
->def_name
, type
);
138 fixit (const char *type
, const char *orig
)
142 def
= findval (defined
, type
, findit
);
143 if (def
== NULL
|| def
->def_kind
!= DEF_TYPEDEF
)
147 switch (def
->def
.ty
.rel
)
150 if (streq (def
->def
.ty
.old_type
, "opaque"))
153 return (def
->def
.ty
.old_type
);
155 return (fixit (def
->def
.ty
.old_type
, orig
));
162 fixtype (const char *type
)
164 return fixit (type
, type
);
168 stringfix (const char *type
)
170 if (streq (type
, "string"))
181 ptype (const char *prefix
, const char *type
, int follow
)
185 if (streq (prefix
, "enum"))
187 f_print (fout
, "enum ");
191 f_print (fout
, "struct ");
194 if (streq (type
, "bool"))
196 f_print (fout
, "bool_t ");
198 else if (streq (type
, "string"))
200 f_print (fout
, "char *");
204 f_print (fout
, "%s ", follow
? fixtype (type
) : type
);
209 typedefed (const definition
* def
, const char *type
)
211 if (def
->def_kind
!= DEF_TYPEDEF
|| def
->def
.ty
.old_prefix
!= NULL
)
217 return streq (def
->def_name
, type
);
222 isvectordef (const char *type
, relation rel
)
231 return !streq (type
, "string");
237 def
= findval (defined
, type
, typedefed
);
242 type
= def
->def
.ty
.old_type
;
243 rel
= def
->def
.ty
.rel
;
249 locase (const char *str
)
252 static char buf
[100];
255 while ((c
= *str
++) != 0)
257 *p
++ = (c
>= 'A' && c
<= 'Z') ? (c
- 'A' + 'a') : c
;
264 pvname_svc (const char *pname
, const char *vnum
)
266 f_print (fout
, "%s_%s_svc", locase (pname
), vnum
);
270 pvname (const char *pname
, const char *vnum
)
272 f_print (fout
, "%s_%s", locase (pname
), vnum
);
276 * print a useful (?) error message, and then die
279 error (const char *msg
)
282 f_print (stderr
, "%s, line %d: ", infilename
, linenum
);
283 f_print (stderr
, "%s\n", msg
);
288 * Something went wrong, unlink any files that we may have created and then
296 for (i
= 0; i
< nfiles
; i
++)
298 unlink (outfiles
[i
]);
304 record_open (const char *file
)
308 outfiles
[nfiles
++] = file
;
312 f_print (stderr
, "too many files!\n");
317 static char expectbuf
[100];
320 * error, token encountered was not the expected one
323 expected1 (tok_kind exp1
)
325 s_print (expectbuf
, "expected '%s'",
331 * error, token encountered was not one of two expected ones
334 expected2 (tok_kind exp1
, tok_kind exp2
)
336 s_print (expectbuf
, "expected '%s' or '%s'",
343 * error, token encountered was not one of 3 expected ones
346 expected3 (tok_kind exp1
, tok_kind exp2
, tok_kind exp3
)
348 s_print (expectbuf
, "expected '%s', '%s' or '%s'",
356 tabify (FILE * f
, int tab
)
360 (void) fputc ('\t', f
);
365 static const token tokstrings
[] =
367 {TOK_IDENT
, "identifier"},
368 {TOK_CONST
, "const"},
379 {TOK_SEMICOLON
, ";"},
380 {TOK_UNION
, "union"},
381 {TOK_STRUCT
, "struct"},
382 {TOK_SWITCH
, "switch"},
384 {TOK_DEFAULT
, "default"},
386 {TOK_TYPEDEF
, "typedef"},
388 {TOK_SHORT
, "short"},
390 {TOK_UNSIGNED
, "unsigned"},
391 {TOK_DOUBLE
, "double"},
392 {TOK_FLOAT
, "float"},
394 {TOK_STRING
, "string"},
395 {TOK_OPAQUE
, "opaque"},
398 {TOK_PROGRAM
, "program"},
399 {TOK_VERSION
, "version"},
404 toktostr (tok_kind kind
)
408 for (sp
= tokstrings
; sp
->kind
!= TOK_EOF
&& sp
->kind
!= kind
; sp
++);
421 for (i
= 0; (c
= curline
[i
]) != 0; i
++)
425 cnt
= 8 - (i
% TABSIZE
);
434 (void) fputc (c
, stderr
);
447 for (i
= 0; i
< where
- curline
; i
++)
452 cnt
= 8 - (i
% TABSIZE
);
460 (void) fputc ('^', stderr
);
463 (void) fputc ('\n', stderr
);
467 make_argname (const char *pname
, const char *vname
)
471 name
= malloc (strlen (pname
) + strlen (vname
) + strlen (ARGEXT
) + 3);
474 fprintf (stderr
, "failed in malloc");
477 sprintf (name
, "%s_%s_%s", locase (pname
), vname
, ARGEXT
);
481 bas_type
*typ_list_h
;
482 bas_type
*typ_list_t
;
485 add_type (int len
, const char *type
)
490 if ((ptr
= malloc (sizeof (bas_type
))) == NULL
)
492 fprintf (stderr
, "failed in malloc");
499 if (typ_list_t
== NULL
)
508 typ_list_t
->next
= ptr
;
516 find_type (const char *type
)
525 if (strcmp (ptr
->name
, type
) == 0)