nsgenbind
Loading...
Searching...
No Matches
nsgenbind.c
Go to the documentation of this file.
1/* binding generator main and command line parsing
2 *
3 * This file is part of nsgenbind.
4 * Licensed under the MIT License,
5 * http://www.opensource.org/licenses/mit-license.php
6 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
7 */
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <stdbool.h>
12#include <string.h>
13#include <unistd.h>
14#include <getopt.h>
15#include <errno.h>
16
17#include "options.h"
18#include "nsgenbind-ast.h"
19#include "webidl-ast.h"
20#include "ir.h"
21#include "output.h"
22#include "jsapi-libdom.h"
23#include "duk-libdom.h"
24
26
32
33static struct options* process_cmdline(int argc, char **argv)
34{
35 int opt;
36
37 options = calloc(1,sizeof(struct options));
38 if (options == NULL) {
39 fprintf(stderr, "Allocation error\n");
40 return NULL;
41 }
42
43 while ((opt = getopt(argc, argv, "vngDW::I:")) != -1) {
44 switch (opt) {
45 case 'I':
46 options->idlpath = strdup(optarg);
47 break;
48
49 case 'v':
50 options->verbose = true;
51 break;
52
53 case 'n':
54 options->dryrun = true;
55 break;
56
57 case 'D':
58 options->debug = true;
59 break;
60
61 case 'g':
62 options->dbglog = true;
63 break;
64
65 case 'W':
66 if ((optarg == NULL) ||
67 (strcmp(optarg, "all") == 0)) {
68 /* all warnings */
70 } else if (strcmp(optarg, "unimplemented") == 0) {
72 } else if (strcmp(optarg, "duplicated") == 0) {
74 } else if (strcmp(optarg, "generated") == 0) {
76 } else {
77 fprintf(stderr,
78 "Unknown warning option \"%s\" valid options are: all, unimplemented,\n"
79 " duplicated, generated\n",
80 optarg);
82 return NULL;
83
84 }
85 break;
86
87 default: /* '?' */
88 fprintf(stderr,
89 "Usage: %s [-v] [-g] [-D] [-W] [-I idlpath] inputfile outputdir\n",
90 argv[0]);
92 return NULL;
93 }
94 }
95
96 if (optind > (argc - 2)) {
97 fprintf(stderr,
98 "Error: expected input filename and output directory\n");
100 return NULL;
101 }
102
103 options->infilename = strdup(argv[optind]);
104
105 options->outdirname = strdup(argv[optind + 1]);
106
107 return options;
108
109}
110
111static int webidl_file_cb(struct genbind_node *node, void *ctx)
112{
113 struct webidl_node **webidl_ast = ctx;
114 char *filename;
115
117
118 if (options->verbose) {
119 printf("Opening IDL file \"%s\"\n", filename);
120 }
121
122 return webidl_parsefile(filename, webidl_ast);
123}
124
125static int genbind_load_idl(struct genbind_node *genbind,
126 struct webidl_node **webidl_out)
127{
128 int res;
129 struct genbind_node *binding_node;
130
131 binding_node = genbind_node_find_type(genbind, NULL,
133
134 /* walk AST and load any web IDL files required */
136 genbind_node_getnode(binding_node),
138 webidl_file_cb,
139 webidl_out);
140 if (res != 0) {
141 fprintf(stderr, "Error: failed reading Web IDL\n");
142 return -1;
143 }
144
145 /* implements are implemented as mixins so intercalate them */
146 res = webidl_intercalate_implements(*webidl_out);
147 if (res != 0) {
148 fprintf(stderr, "Error: Failed to intercalate implements\n");
149 return -1;
150 }
151
152 return 0;
153}
154
158static enum bindingtype_e genbind_get_type(struct genbind_node *node)
159{
160 struct genbind_node *binding_node;
161 const char *binding_type;
162
163 binding_node = genbind_node_find_type(node,
164 NULL,
166 if (binding_node == NULL) {
167 /* binding entry is missing which is invalid */
168 return BINDINGTYPE_UNKNOWN;
169 }
170
171 binding_type = genbind_node_gettext(
173 genbind_node_getnode(binding_node),
174 NULL,
176 if (binding_type == NULL) {
177 fprintf(stderr, "Error: missing binding type\n");
178 return BINDINGTYPE_UNKNOWN;
179 }
180
181 if (strcmp(binding_type, "jsapi_libdom") == 0) {
183 }
184
185 if (strcmp(binding_type, "duk_libdom") == 0) {
187 }
188
189 fprintf(stderr, "Error: unsupported binding type \"%s\"\n", binding_type);
190
191 return BINDINGTYPE_UNKNOWN;
192}
193
194int main(int argc, char **argv)
195{
196 int res;
197 struct genbind_node *genbind_root = NULL;
198 struct webidl_node *webidl_root = NULL;
199 struct ir *ir = NULL;
200 enum bindingtype_e bindingtype;
201
202 options = process_cmdline(argc, argv);
203 if (options == NULL) {
204 return 1; /* bad commandline */
205 }
206
207 /* parse binding */
208 res = genbind_parsefile(options->infilename, &genbind_root);
209 if (res != 0) {
210 fprintf(stderr, "Error: parse failed with code %d\n", res);
211 return res;
212 }
213
214 /* dump the binding AST */
215 genbind_dump_ast(genbind_root);
216
217 /* get type of binding */
218 bindingtype = genbind_get_type(genbind_root);
219 if (bindingtype == BINDINGTYPE_UNKNOWN) {
220 return 3;
221 }
222
223 /* load the IDL files specified in the binding */
224 res = genbind_load_idl(genbind_root, &webidl_root);
225 if (res != 0) {
226 return 4;
227 }
228
229 /* debug dump of web idl AST */
230 webidl_dump_ast(webidl_root);
231
232 /* generate intermediate representation */
233 res = ir_new(genbind_root, webidl_root, &ir);
234 if (res != 0) {
235 return 5;
236 }
237
238 /* dump the intermediate representation */
239 ir_dump(ir);
240 ir_dumpdot(ir);
241
242 /* generate binding */
243 switch (bindingtype) {
245 res = duk_libdom_output(ir);
246 break;
247
248 default:
249 fprintf(stderr, "Unable to generate binding of this type\n");
250 res = 7;
251 }
252
253 return res;
254}
int duk_libdom_output(struct ir *ir)
Definition duk-libdom.c:579
DOMString filename
Definition html.idl:1561
int ir_new(struct genbind_node *genbind, struct webidl_node *webidl, struct ir **map_out)
Definition ir.c:963
int ir_dump(struct ir *ir)
Definition ir.c:1131
int ir_dumpdot(struct ir *index)
Definition ir.c:1175
int genbind_parsefile(char *infilename, struct genbind_node **ast)
struct genbind_node * genbind_node_find_type(struct genbind_node *node, struct genbind_node *prev, enum genbind_node_type type)
int genbind_dump_ast(struct genbind_node *node)
struct genbind_node * genbind_node_getnode(struct genbind_node *node)
char * genbind_node_gettext(struct genbind_node *node)
int genbind_node_foreach_type(struct genbind_node *node, enum genbind_node_type type, genbind_callback_t *cb, void *ctx)
@ GENBIND_NODE_TYPE_BINDING
@ GENBIND_NODE_TYPE_WEBIDL
@ GENBIND_NODE_TYPE_NAME
void free(void *)
bindingtype_e
Definition nsgenbind.c:27
@ BINDINGTYPE_DUK_LIBDOM
Definition nsgenbind.c:30
@ BINDINGTYPE_JSAPI_LIBDOM
Definition nsgenbind.c:29
@ BINDINGTYPE_UNKNOWN
Definition nsgenbind.c:28
struct options * options
Definition nsgenbind.c:25
int main(int argc, char **argv)
Definition nsgenbind.c:194
@ WARNING_UNIMPLEMENTED
Definition options.h:29
@ WARNING_GENERATED
Definition options.h:32
@ WARNING_DUPLICATED
Definition options.h:30
#define WARNING_ALL
Definition options.h:35
struct genbind_node * node
Definition ir.h:172
char * outdirname
Definition options.h:15
bool debug
Definition options.h:19
char * idlpath
Definition options.h:16
bool verbose
Definition options.h:18
bool dryrun
Definition options.h:21
unsigned int warnings
Definition options.h:23
char * infilename
Definition options.h:14
bool dbglog
Definition options.h:20
struct webidl_node * node
Definition webidl-ast.c:35
int webidl_intercalate_implements(struct webidl_node *webidl_ast)
Definition webidl-ast.c:769
int webidl_dump_ast(struct webidl_node *node)
Definition webidl-ast.c:571
int webidl_parsefile(char *filename, struct webidl_node **webidl_ast)
Definition webidl-ast.c:619