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
36 * rpc_util.c, Utility routines for the RPC protocol compiler
43 #include "rpc_parse.h"
47 #define ARGEXT "argument"
49 char curline
[MAXLINESIZE
]; /* current read line */
50 const 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 int findit (const definition
* def
, const char *type
);
65 static const char *fixit (const char *type
, const char *orig
);
66 static int typedefed (const definition
* def
, const char *type
);
67 static const char *toktostr (tok_kind kind
);
68 static void printbuf (void);
69 static void printwhere (void);
72 * Reinitialize the world
77 memset (curline
, 0, MAXLINESIZE
);
87 streq (const char *a
, const char *b
)
89 return strcmp (a
, b
) == 0;
93 * find a value in a list
96 findval (list
*lst
, const char *val
,
97 int (*cmp
) (const definition
*, const char *))
100 for (; lst
!= NULL
; lst
= lst
->next
)
102 if (cmp (lst
->val
, val
))
111 * store a value in a list
114 storeval (list
**lstp
, definition
*val
)
120 for (l
= lstp
; *l
!= NULL
; l
= (list
**) & (*l
)->next
);
128 findit (const definition
* def
, const char *type
)
130 return streq (def
->def_name
, type
);
134 fixit (const char *type
, const char *orig
)
138 def
= findval (defined
, type
, findit
);
139 if (def
== NULL
|| def
->def_kind
!= DEF_TYPEDEF
)
143 switch (def
->def
.ty
.rel
)
146 if (streq (def
->def
.ty
.old_type
, "opaque"))
149 return (def
->def
.ty
.old_type
);
151 return (fixit (def
->def
.ty
.old_type
, orig
));
158 fixtype (const char *type
)
160 return fixit (type
, type
);
164 stringfix (const char *type
)
166 if (streq (type
, "string"))
177 ptype (const char *prefix
, const char *type
, int follow
)
181 if (streq (prefix
, "enum"))
183 f_print (fout
, "enum ");
187 f_print (fout
, "struct ");
190 if (streq (type
, "bool"))
192 f_print (fout
, "bool_t ");
194 else if (streq (type
, "string"))
196 f_print (fout
, "char *");
200 f_print (fout
, "%s ", follow
? fixtype (type
) : type
);
205 typedefed (const definition
* def
, const char *type
)
207 if (def
->def_kind
!= DEF_TYPEDEF
|| def
->def
.ty
.old_prefix
!= NULL
)
213 return streq (def
->def_name
, type
);
218 isvectordef (const char *type
, relation rel
)
227 return !streq (type
, "string");
233 def
= findval (defined
, type
, typedefed
);
238 type
= def
->def
.ty
.old_type
;
239 rel
= def
->def
.ty
.rel
;
245 locase (const char *str
)
248 static char buf
[100];
251 while ((c
= *str
++) != 0)
253 *p
++ = (c
>= 'A' && c
<= 'Z') ? (c
- 'A' + 'a') : c
;
260 pvname_svc (const char *pname
, const char *vnum
)
262 f_print (fout
, "%s_%s_svc", locase (pname
), vnum
);
266 pvname (const char *pname
, const char *vnum
)
268 f_print (fout
, "%s_%s", locase (pname
), vnum
);
272 * print a useful (?) error message, and then die
275 error (const char *msg
)
278 f_print (stderr
, "%s, line %d: ", infilename
, linenum
);
279 f_print (stderr
, "%s\n", msg
);
284 * Something went wrong, unlink any files that we may have created and then
292 for (i
= 0; i
< nfiles
; i
++)
294 unlink (outfiles
[i
]);
300 record_open (const char *file
)
304 outfiles
[nfiles
++] = file
;
308 f_print (stderr
, "too many files!\n");
313 static char expectbuf
[100];
316 * error, token encountered was not the expected one
319 expected1 (tok_kind exp1
)
321 s_print (expectbuf
, "expected '%s'",
327 * error, token encountered was not one of two expected ones
330 expected2 (tok_kind exp1
, tok_kind exp2
)
332 s_print (expectbuf
, "expected '%s' or '%s'",
339 * error, token encountered was not one of 3 expected ones
342 expected3 (tok_kind exp1
, tok_kind exp2
, tok_kind exp3
)
344 s_print (expectbuf
, "expected '%s', '%s' or '%s'",
352 tabify (FILE * f
, int tab
)
356 (void) fputc ('\t', f
);
361 static const token tokstrings
[] =
363 {TOK_IDENT
, "identifier"},
364 {TOK_CONST
, "const"},
375 {TOK_SEMICOLON
, ";"},
376 {TOK_UNION
, "union"},
377 {TOK_STRUCT
, "struct"},
378 {TOK_SWITCH
, "switch"},
380 {TOK_DEFAULT
, "default"},
382 {TOK_TYPEDEF
, "typedef"},
384 {TOK_SHORT
, "short"},
386 {TOK_UNSIGNED
, "unsigned"},
387 {TOK_DOUBLE
, "double"},
388 {TOK_FLOAT
, "float"},
390 {TOK_STRING
, "string"},
391 {TOK_OPAQUE
, "opaque"},
394 {TOK_PROGRAM
, "program"},
395 {TOK_VERSION
, "version"},
400 toktostr (tok_kind kind
)
404 for (sp
= tokstrings
; sp
->kind
!= TOK_EOF
&& sp
->kind
!= kind
; sp
++);
417 for (i
= 0; (c
= curline
[i
]) != 0; i
++)
421 cnt
= 8 - (i
% TABSIZE
);
430 (void) fputc (c
, stderr
);
443 for (i
= 0; i
< where
- curline
; i
++)
448 cnt
= 8 - (i
% TABSIZE
);
456 (void) fputc ('^', stderr
);
459 (void) fputc ('\n', stderr
);
463 make_argname (const char *pname
, const char *vname
)
467 name
= malloc (strlen (pname
) + strlen (vname
) + strlen (ARGEXT
) + 3);
470 fprintf (stderr
, "failed in malloc");
473 sprintf (name
, "%s_%s_%s", locase (pname
), vname
, ARGEXT
);
477 bas_type
*typ_list_h
;
478 bas_type
*typ_list_t
;
481 add_type (int len
, const char *type
)
486 if ((ptr
= malloc (sizeof (bas_type
))) == NULL
)
488 fprintf (stderr
, "failed in malloc");
495 if (typ_list_t
== NULL
)
504 typ_list_t
->next
= ptr
;
512 find_type (const char *type
)
521 if (strcmp (ptr
->name
, type
) == 0)