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
38 * rpc_util.c, Utility routines for the RPC protocol compiler
45 #include "rpc_parse.h"
49 #define ARGEXT "argument"
51 char curline
[MAXLINESIZE
]; /* current read line */
52 const char *where
= curline
; /* current point in line */
53 int linenum
= 0; /* current line number */
55 const char *infilename
; /* input filename */
58 const char *outfiles
[NFILES
]; /* output file names */
61 FILE *fout
; /* file pointer of current output */
62 FILE *fin
; /* file pointer of current input */
64 list
*defined
; /* list of defined things */
66 static int findit (const definition
* def
, const char *type
);
67 static const char *fixit (const char *type
, const char *orig
);
68 static int typedefed (const definition
* def
, const char *type
);
69 static const char *toktostr (tok_kind kind
);
70 static void printbuf (void);
71 static void printwhere (void);
74 * Reinitialize the world
79 memset (curline
, 0, MAXLINESIZE
);
89 streq (const char *a
, const char *b
)
91 return strcmp (a
, b
) == 0;
95 * find a value in a list
98 findval (list
*lst
, const char *val
,
99 int (*cmp
) (const definition
*, const char *))
102 for (; lst
!= NULL
; lst
= lst
->next
)
104 if (cmp (lst
->val
, val
))
113 * store a value in a list
116 storeval (list
**lstp
, definition
*val
)
122 for (l
= lstp
; *l
!= NULL
; l
= (list
**) & (*l
)->next
);
130 findit (const definition
* def
, const char *type
)
132 return streq (def
->def_name
, type
);
136 fixit (const char *type
, const char *orig
)
140 def
= findval (defined
, type
, findit
);
141 if (def
== NULL
|| def
->def_kind
!= DEF_TYPEDEF
)
145 switch (def
->def
.ty
.rel
)
148 if (streq (def
->def
.ty
.old_type
, "opaque"))
151 return (def
->def
.ty
.old_type
);
153 return (fixit (def
->def
.ty
.old_type
, orig
));
160 fixtype (const char *type
)
162 return fixit (type
, type
);
166 stringfix (const char *type
)
168 if (streq (type
, "string"))
179 ptype (const char *prefix
, const char *type
, int follow
)
183 if (streq (prefix
, "enum"))
185 f_print (fout
, "enum ");
189 f_print (fout
, "struct ");
192 if (streq (type
, "bool"))
194 f_print (fout
, "bool_t ");
196 else if (streq (type
, "string"))
198 f_print (fout
, "char *");
202 f_print (fout
, "%s ", follow
? fixtype (type
) : type
);
207 typedefed (const definition
* def
, const char *type
)
209 if (def
->def_kind
!= DEF_TYPEDEF
|| def
->def
.ty
.old_prefix
!= NULL
)
215 return streq (def
->def_name
, type
);
220 isvectordef (const char *type
, relation rel
)
229 return !streq (type
, "string");
235 def
= findval (defined
, type
, typedefed
);
240 type
= def
->def
.ty
.old_type
;
241 rel
= def
->def
.ty
.rel
;
247 locase (const char *str
)
250 static char buf
[100];
253 while ((c
= *str
++) != 0)
255 *p
++ = (c
>= 'A' && c
<= 'Z') ? (c
- 'A' + 'a') : c
;
262 pvname_svc (const char *pname
, const char *vnum
)
264 f_print (fout
, "%s_%s_svc", locase (pname
), vnum
);
268 pvname (const char *pname
, const char *vnum
)
270 f_print (fout
, "%s_%s", locase (pname
), vnum
);
274 * print a useful (?) error message, and then die
277 error (const char *msg
)
280 f_print (stderr
, "%s, line %d: ", infilename
, linenum
);
281 f_print (stderr
, "%s\n", msg
);
286 * Something went wrong, unlink any files that we may have created and then
294 for (i
= 0; i
< nfiles
; i
++)
296 unlink (outfiles
[i
]);
302 record_open (const char *file
)
306 outfiles
[nfiles
++] = file
;
310 f_print (stderr
, "too many files!\n");
315 static char expectbuf
[100];
318 * error, token encountered was not the expected one
321 expected1 (tok_kind exp1
)
323 s_print (expectbuf
, "expected '%s'",
329 * error, token encountered was not one of two expected ones
332 expected2 (tok_kind exp1
, tok_kind exp2
)
334 s_print (expectbuf
, "expected '%s' or '%s'",
341 * error, token encountered was not one of 3 expected ones
344 expected3 (tok_kind exp1
, tok_kind exp2
, tok_kind exp3
)
346 s_print (expectbuf
, "expected '%s', '%s' or '%s'",
354 tabify (FILE * f
, int tab
)
358 (void) fputc ('\t', f
);
363 static const token tokstrings
[] =
365 {TOK_IDENT
, "identifier"},
366 {TOK_CONST
, "const"},
377 {TOK_SEMICOLON
, ";"},
378 {TOK_UNION
, "union"},
379 {TOK_STRUCT
, "struct"},
380 {TOK_SWITCH
, "switch"},
382 {TOK_DEFAULT
, "default"},
384 {TOK_TYPEDEF
, "typedef"},
386 {TOK_SHORT
, "short"},
388 {TOK_UNSIGNED
, "unsigned"},
389 {TOK_DOUBLE
, "double"},
390 {TOK_FLOAT
, "float"},
392 {TOK_STRING
, "string"},
393 {TOK_OPAQUE
, "opaque"},
396 {TOK_PROGRAM
, "program"},
397 {TOK_VERSION
, "version"},
402 toktostr (tok_kind kind
)
406 for (sp
= tokstrings
; sp
->kind
!= TOK_EOF
&& sp
->kind
!= kind
; sp
++);
419 for (i
= 0; (c
= curline
[i
]) != 0; i
++)
423 cnt
= 8 - (i
% TABSIZE
);
432 (void) fputc (c
, stderr
);
445 for (i
= 0; i
< where
- curline
; i
++)
450 cnt
= 8 - (i
% TABSIZE
);
458 (void) fputc ('^', stderr
);
461 (void) fputc ('\n', stderr
);
465 make_argname (const char *pname
, const char *vname
)
469 name
= malloc (strlen (pname
) + strlen (vname
) + strlen (ARGEXT
) + 3);
472 fprintf (stderr
, "failed in malloc");
475 sprintf (name
, "%s_%s_%s", locase (pname
), vname
, ARGEXT
);
479 bas_type
*typ_list_h
;
480 bas_type
*typ_list_t
;
483 add_type (int len
, const char *type
)
488 if ((ptr
= malloc (sizeof (bas_type
))) == NULL
)
490 fprintf (stderr
, "failed in malloc");
497 if (typ_list_t
== NULL
)
506 typ_list_t
->next
= ptr
;
514 find_type (const char *type
)
523 if (strcmp (ptr
->name
, type
) == 0)