2 * rpcgen - Generate XDR bindings automatically.
3 * Copyright (C) 2008 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "rpcgen_int.h"
29 static void gen_line (void);
30 static void gen_decl (int indent, const struct decl *);
31 static void gen_type (const struct type *);
38 for (i = 0; i < n; ++i)
42 /* This generates a #line directive referring back to the
43 * original source file.
49 fprintf (yyout, "#line %d \"%s\"\n", yylineno, input_filename);
53 write_header_def (void)
57 p = strrchr (output_filename, '/') ? : output_filename;
59 fputs ("RPCGEN_HEADER_", yyout);
63 fputc (toupper (*p), yyout);
71 gen_prologue (const char *filename)
77 "/* This file was generated by PortableXDR rpcgen %s\n"
78 " * ANY CHANGES YOU MAKE TO THIS FILE MAY BE LOST!\n"
79 " * The input file was %s\n"
82 PACKAGE_VERSION, filename);
87 len = strlen (filename);
88 basename = malloc (len + 1);
89 strcpy (basename, filename);
90 if (len >= 2 && basename[len-2] == '.')
91 basename[len-2] = '\0';
92 fprintf (yyout, "#include \"%s.h\"\n", basename);
97 fprintf (yyout, "#ifndef ");
99 fprintf (yyout, "\n#define ");
104 "#ifdef __cplusplus\n"
108 "#include <stdint.h>\n"
109 "#include <rpc/rpc.h>\n"
128 "#ifdef __cplusplus\n"
134 fprintf (yyout, " */\n");
138 fprintf (yyout, "\n/* EOF */\n");
142 gen_const (const char *name, const char *value)
144 if (output_mode == output_h) {
147 fprintf (yyout, "#define %s %s\n", name, value);
152 gen_enum (const char *name, const struct cons *enum_values)
159 fprintf (yyout, "enum %s {\n", name);
160 while (enum_values) {
161 struct enum_value *enum_value = (struct enum_value *) enum_values->ptr;
162 if (enum_value->value)
163 fprintf (yyout, " %s = %s,\n",
164 enum_value->ident, enum_value->value);
166 fprintf (yyout, " %s,\n", enum_value->ident);
167 enum_values = enum_values->next;
171 "typedef enum %s %s;\n"
172 "extern bool_t xdr_%s (XDR *, %s *);\n"
174 name, name, name, name);
184 gen_struct (const char *name, const struct cons *decls)
191 fprintf (yyout, "struct %s {\n", name);
193 gen_decl (2, (struct decl *) decls->ptr);
198 "typedef struct %s %s;\n"
199 "extern bool_t xdr_%s (XDR *, %s *);\n"
201 name, name, name, name);
211 gen_union (const char *name, const struct decl *discrim,
212 const struct cons *union_cases)
219 fprintf (yyout, "struct %s {\n", name);
220 gen_decl (2, discrim);
221 fprintf (yyout, " union {\n");
223 while (union_cases) {
224 struct decl *decl = ((struct union_case *) union_cases->ptr)->decl;
225 if (decl) gen_decl (4, decl);
226 union_cases = union_cases->next;
231 "typedef struct %s %s;\n"
232 "extern bool_t xdr_%s (XDR *, %s *);\n"
234 name, name, name, name, name);
244 gen_typedef (const struct decl *decl)
246 if (output_mode == output_h) {
248 fputs ("typedef ", yyout);
251 "extern bool_t xdr_%s (XDR *, %s *);\n"
253 decl->ident, decl->ident);
258 gen_decl (int indent, const struct decl *decl)
262 switch (decl->decl_type)
264 case decl_type_simple:
265 gen_type (decl->type);
266 fprintf (yyout, " %s;\n", decl->ident);
269 case decl_type_fixed_array:
270 gen_type (decl->type);
271 fprintf (yyout, " %s[%s];\n", decl->ident, decl->len);
274 case decl_type_variable_array:
275 fprintf (yyout, "struct {\n");
277 fprintf (yyout, "uint32_t %s_len;\n", decl->ident);
279 gen_type (decl->type);
280 fprintf (yyout, " *%s_val;\n", decl->ident);
282 fprintf (yyout, "} %s;\n", decl->ident);
285 case decl_type_pointer:
286 gen_type (decl->type);
287 fprintf (yyout, " *%s;\n", decl->ident);
293 gen_type (const struct type *type)
298 if (type->sgn) fputs ("int8_t", yyout);
299 else fputs ("uint8_t", yyout);
303 if (type->sgn) fputs ("int16_t", yyout);
304 else fputs ("uint16_t", yyout);
308 if (type->sgn) fputs ("int32_t", yyout);
309 else fputs ("uint32_t", yyout);
313 if (type->sgn) fputs ("int64_t", yyout);
314 else fputs ("uint64_t", yyout);
318 fputs ("double", yyout);
322 fputs ("char *", yyout);
326 fputs ("char", yyout);
330 fputs ("bool_t", yyout);
334 fputs (type->ident, yyout);