-
Notifications
You must be signed in to change notification settings - Fork 38
/
parsleyc_main.c
108 lines (92 loc) · 2.57 KB
/
parsleyc_main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <argp.h>
#include <string.h>
#include <json/printbuf.h>
#include "parsley.h"
#include "util.h"
struct list_elem {
int has_next;
struct list_elem *next;
char *string;
};
struct arguments
{
struct list_elem *include_files;
char *parsley;
char *output_file;
};
const char *argp_program_version = "parsleyc 0.1";
const char *argp_program_bug_address = "<[email protected]>";
static char args_doc[] = "DEX_FILE";
static char doc[] = "Parsleyc is a parselet to XSLT compiler";
static struct argp_option options[] = {
{"debug", 'd', 0, 0, "Turn on Bison parser debugging" },
{"output", 'o', "FILE", 0, "Output to FILE instead of standard output" },
{"include", 'i', "FILE", 0, "Include the contents of FILE in the produced XSLT" },
{ 0 }
};
static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
struct arguments *arguments = state->input;
struct list_elem *base = arguments->include_files;
struct list_elem *e;
switch (key)
{
case 'i':
e = (struct list_elem *) calloc(1, sizeof(e));
e->string = arg;
while(base->has_next) base = base->next;
base->next = e;
base->has_next = 1;
break;
case 'd':
// parsley_set_debug_mode(1);
break;
case 'o':
arguments->output_file = arg;
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 1) argp_usage (state);
arguments->parsley = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 1) argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc };
int main (int argc, char **argv) {
struct arguments arguments;
struct list_elem elem;
struct list_elem *elemptr = &elem;
elem.has_next = 0;
arguments.include_files = elemptr;
arguments.output_file = "-";
arguments.parsley = "-";
argp_parse (&argp, argc, argv, 0, 0, &arguments);
struct printbuf* parsley = printbuf_new();
struct printbuf* incl = printbuf_new();
sprintbuf(parsley, "");
sprintbuf(incl, "");
FILE* in = parsley_fopen(arguments.parsley, "r");
printbuf_file_read(in, parsley);
while(elemptr->has_next) {
elemptr = elemptr->next;
FILE* f = parsley_fopen(elemptr->string, "r");
printbuf_file_read(f, incl);
fclose(f);
}
parsleyPtr compiled = parsley_compile(parsley->buf, incl->buf);
if(compiled->error != NULL) {
fprintf(stderr, "%s\n", compiled->error);
exit(1);
}
FILE* fo = parsley_fopen(arguments.output_file, "w");
xmlDocFormatDump(fo, compiled->stylesheet->doc, 1);
fclose(fo);
return 0;
}