summaryrefslogtreecommitdiff
path: root/src/genjsbind-ast.c
blob: a2e17df988407065cfab55cd02bb2e4fa395d8b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* binding generator AST implementation for parser
 *
 * This file is part of nsgenjsbind.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
 */

/** @todo this currently stuffs everything in one global tree, not very nice
 */

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "genjsbind-ast.h"
#include "webidl-ast.h"
#include "options.h"

/* parser and lexer interface */
extern int genjsbind_debug;
extern int genjsbind__flex_debug;
extern void genjsbind_restart(FILE*);
extern int genjsbind_parse(void);


#define HDR_COMMENT_SEP     "\n * "
#define HDR_COMMENT_SEP_LEN 4
#define HDR_COMMENT_PREABLE "Generated by nsgenjsapi"HDR_COMMENT_SEP

/* current state */
struct genbind_ast *genbind_ast;

int genbind_header_comment(char *comment)
{
	char *fullstr;
	int fulllen;
	fulllen = strlen(genbind_ast->hdr_comments) + 
		strlen(comment) + HDR_COMMENT_SEP_LEN + 1;
	fullstr = malloc(fulllen);
	snprintf(fullstr, fulllen, "%s"HDR_COMMENT_SEP"%s", genbind_ast->hdr_comments , comment);
	free(genbind_ast->hdr_comments);
	free(comment);
	genbind_ast->hdr_comments  = fullstr;

	return 0;
}

int genbind_preamble(char *ccode)
{
	char *fullstr;
	int fulllen;
	fulllen = strlen(genbind_ast->preamble) + strlen(ccode) + 1;
	fullstr = malloc(fulllen);
	snprintf(fullstr, fulllen, "%s%s", genbind_ast->preamble , ccode);
	free(genbind_ast->preamble);
	free(ccode);
	genbind_ast->preamble = fullstr;

	return 0;
}

int genbind_interface(char *interface)
{
	genbind_ast->ifname = interface;
	return 0;
}

static void init_state(void) 
{
	/* allocate state */
	genbind_ast = calloc(1, sizeof(struct genbind_ast));

	/* initialise root IDL node */
	webidl_root = webidl_new_node(WEBIDL_NODE_TYPE_ROOT);

	/* set default comment header text */
	genbind_ast->hdr_comments = strdup(HDR_COMMENT_PREABLE);

	genbind_ast->preamble = strdup("");
}

int genbind_parsefile(char *infilename)
{
	FILE *infile;

        /* open input file */
	if ((infilename[0] == '-') && 
	    (infilename[1] == 0)) {
		if (options->verbose) {
			printf("Using stdin for input\n");
		}
		infile = stdin;
	} else {
		if (options->verbose) {
			printf("Opening binding file %s\n", options->infilename);
		}
		infile = fopen(infilename, "r");
	}
	
	if (!infile) {
		fprintf(stderr, "Error opening %s: %s\n",
			infilename, 
			strerror(errno));
		return 3;
	}

	/* initialise internal state */
	init_state();

	if (options->debug) {
		genjsbind_debug = 1;
		genjsbind__flex_debug = 1;
	}

	/* set flex to read from file */
	genjsbind_restart(infile);

	/* process binding */
	return genjsbind_parse();

}