2 * From: @(#)rpc_util.c 1.11 89/02/22
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_util.c, Utility routines for the RPC protocol compiler
41 #include "rpc_parse.h"
45 #define ARGEXT "argument"
47 char curline
[MAXLINESIZE
]; /* current read line */
48 const char *where
= curline
; /* current point in line */
49 int linenum
= 0; /* current line number */
51 const char *infilename
; /* input filename */
54 const char *outfiles
[NFILES
]; /* output file names */
57 FILE *fout
; /* file pointer of current output */
58 FILE *fin
; /* file pointer of current input */
60 list
*defined
; /* list of defined things */
62 static int findit (const definition
* def
, const char *type
);
63 static const char *fixit (const char *type
, const char *orig
);
64 static int typedefed (const definition
* def
, const char *type
);
65 static const char *toktostr (tok_kind kind
);
66 static void printbuf (void);
67 static void printwhere (void);
70 * Reinitialize the world
75 memset (curline
, 0, MAXLINESIZE
);
85 streq (const char *a
, const char *b
)
87 return strcmp (a
, b
) == 0;
91 * find a value in a list
94 findval (list
*lst
, const char *val
,
95 int (*cmp
) (const definition
*, const char *))
98 for (; lst
!= NULL
; lst
= lst
->next
)
100 if (cmp (lst
->val
, val
))
109 * store a value in a list
112 storeval (list
**lstp
, definition
*val
)
118 for (l
= lstp
; *l
!= NULL
; l
= (list
**) & (*l
)->next
);
126 findit (const definition
* def
, const char *type
)
128 return streq (def
->def_name
, type
);
132 fixit (const char *type
, const char *orig
)
136 def
= findval (defined
, type
, findit
);
137 if (def
== NULL
|| def
->def_kind
!= DEF_TYPEDEF
)
141 switch (def
->def
.ty
.rel
)
144 if (streq (def
->def
.ty
.old_type
, "opaque"))
147 return (def
->def
.ty
.old_type
);
149 return (fixit (def
->def
.ty
.old_type
, orig
));
156 fixtype (const char *type
)
158 return fixit (type
, type
);
162 stringfix (const char *type
)
164 if (streq (type
, "string"))
175 ptype (const char *prefix
, const char *type
, int follow
)
179 if (streq (prefix
, "enum"))
181 f_print (fout
, "enum ");
185 f_print (fout
, "struct ");
188 if (streq (type
, "bool"))
190 f_print (fout
, "bool_t ");
192 else if (streq (type
, "string"))
194 f_print (fout
, "char *");
198 f_print (fout
, "%s ", follow
? fixtype (type
) : type
);
203 typedefed (const definition
* def
, const char *type
)
205 if (def
->def_kind
!= DEF_TYPEDEF
|| def
->def
.ty
.old_prefix
!= NULL
)
211 return streq (def
->def_name
, type
);
216 isvectordef (const char *type
, relation rel
)
225 return !streq (type
, "string");
231 def
= findval (defined
, type
, typedefed
);
236 type
= def
->def
.ty
.old_type
;
237 rel
= def
->def
.ty
.rel
;
243 locase (const char *str
)
246 static char buf
[100];
249 while ((c
= *str
++) != 0)
251 *p
++ = (c
>= 'A' && c
<= 'Z') ? (c
- 'A' + 'a') : c
;
258 pvname_svc (const char *pname
, const char *vnum
)
260 f_print (fout
, "%s_%s_svc", locase (pname
), vnum
);
264 pvname (const char *pname
, const char *vnum
)
266 f_print (fout
, "%s_%s", locase (pname
), vnum
);
270 * print a useful (?) error message, and then die
273 error (const char *msg
)
276 f_print (stderr
, "%s, line %d: ", infilename
, linenum
);
277 f_print (stderr
, "%s\n", msg
);
282 * Something went wrong, unlink any files that we may have created and then
290 for (i
= 0; i
< nfiles
; i
++)
292 unlink (outfiles
[i
]);
298 record_open (const char *file
)
302 outfiles
[nfiles
++] = file
;
306 f_print (stderr
, "too many files!\n");
311 static char expectbuf
[100];
314 * error, token encountered was not the expected one
317 expected1 (tok_kind exp1
)
319 s_print (expectbuf
, "expected '%s'",
325 * error, token encountered was not one of two expected ones
328 expected2 (tok_kind exp1
, tok_kind exp2
)
330 s_print (expectbuf
, "expected '%s' or '%s'",
337 * error, token encountered was not one of 3 expected ones
340 expected3 (tok_kind exp1
, tok_kind exp2
, tok_kind exp3
)
342 s_print (expectbuf
, "expected '%s', '%s' or '%s'",
350 tabify (FILE * f
, int tab
)
354 (void) fputc ('\t', f
);
359 static const token tokstrings
[] =
361 {TOK_IDENT
, "identifier"},
362 {TOK_CONST
, "const"},
373 {TOK_SEMICOLON
, ";"},
374 {TOK_UNION
, "union"},
375 {TOK_STRUCT
, "struct"},
376 {TOK_SWITCH
, "switch"},
378 {TOK_DEFAULT
, "default"},
380 {TOK_TYPEDEF
, "typedef"},
382 {TOK_SHORT
, "short"},
384 {TOK_UNSIGNED
, "unsigned"},
385 {TOK_DOUBLE
, "double"},
386 {TOK_FLOAT
, "float"},
388 {TOK_STRING
, "string"},
389 {TOK_OPAQUE
, "opaque"},
392 {TOK_PROGRAM
, "program"},
393 {TOK_VERSION
, "version"},
398 toktostr (tok_kind kind
)
402 for (sp
= tokstrings
; sp
->kind
!= TOK_EOF
&& sp
->kind
!= kind
; sp
++);
415 for (i
= 0; (c
= curline
[i
]) != 0; i
++)
419 cnt
= 8 - (i
% TABSIZE
);
428 (void) fputc (c
, stderr
);
441 for (i
= 0; i
< where
- curline
; i
++)
446 cnt
= 8 - (i
% TABSIZE
);
454 (void) fputc ('^', stderr
);
457 (void) fputc ('\n', stderr
);
461 make_argname (const char *pname
, const char *vname
)
465 name
= 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
;
479 add_type (int len
, const char *type
)
484 if ((ptr
= malloc (sizeof (bas_type
))) == NULL
)
486 fprintf (stderr
, "failed in malloc");
493 if (typ_list_t
== NULL
)
502 typ_list_t
->next
= ptr
;
510 find_type (const char *type
)
519 if (strcmp (ptr
->name
, type
) == 0)