aboutsummaryrefslogtreecommitdiff
path: root/tests/tforlan.c
blob: 64f84e1f7d07ec2ef3003b6de0dc2af88b737004 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* This file is part of Eclat.
   Copyright (C) 2012 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#include <errno.h>
#include <sysexits.h>
#include <expat.h>
#include <libeclat.h>
#include "forlan.h"
#include <sys/stat.h>

void
usage()
{
	printf("usage: %s [OPTIONS] FILE [INPUT]\n", program_name);
	printf("OPTIONS are:\n");
	printf("  -D         dump parse tree\n");
	printf("  -d LEVEL   set debug level\n");
	printf("  -f         use file interface\n");
	printf("  -s         sort XML tree\n");
	printf("  -h         produce this help list\n");
}

struct grecs_node *
parse_xml(FILE *fp)
{
	XML_Parser parser;
	eclat_partial_tree_t part;
	size_t size;
	char buffer[256];

	parser = XML_ParserCreate("UTF-8");
	if (!parser)
		die(EX_SOFTWARE, "cannot create XML parser");
	XML_SetElementHandler(parser,
			      eclat_partial_tree_start_handler,
			      eclat_partial_tree_end_handler);
	XML_SetCharacterDataHandler(parser,
				    eclat_partial_tree_data_handler);
	part = eclat_partial_tree_create();
	XML_SetUserData(parser, part);

	while ((size = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
		enum XML_Status status = XML_Parse(parser, buffer, size, 0);
		if (status == XML_STATUS_ERROR) {
			enum XML_Error error = XML_GetErrorCode(parser);
			int line = XML_GetCurrentLineNumber(parser);
			int column = XML_GetCurrentColumnNumber(parser);

			die(EX_SOFTWARE, "XML parse error at %d:%d: %s",
			    line, column, XML_ErrorString(error));
		}
	}
	XML_Parse(parser, "", 0, 1);
	
	return eclat_partial_tree_finish(part);
}

static int
node_ident_cmp(struct grecs_node const *a, struct grecs_node const *b)
{
        return strcmp(a->ident, b->ident);
}

#define OPT_DUMP 0x01
#define OPT_SORT 0x02
#define OPT_FILE 0x04

int
main(int argc, char **argv)
{
	FILE *fp;
	char *buf;
	size_t len;
	struct stat st;
	struct grecs_locus_point pt;
	int rc;
	int options = 0;
	forlan_eval_env_t env;
	
	set_program_name(argv[0]);
	forlan_init();

	while ((rc = getopt(argc, argv, "Dd:fhs")) != EOF)
		switch (rc) {
		case 'D':
			options |= OPT_DUMP;
			break;
			
		case 'd':
			if (parse_debug_level(optarg))
				die(EX_USAGE, "bad debug category or level");
			break;

		case 'f':
			options |= OPT_FILE;
			break;
			
		case 'h':
			usage();
			return 0;

		case 's':
			options |= OPT_SORT;
			break;
			
		default:
			exit(EX_USAGE);
		}
	argc -= optind;
	argv += optind;

	
	if (argc == 0 || argc > 2)
		die(EX_USAGE, "one or two arguments expected");
	if (stat(argv[0], &st))
		die(EX_UNAVAILABLE, "cannot stat input file \"%s\": %s",
		    argv[0], strerror(errno));

	fp = fopen(argv[0], "r");
	if (!fp)
		die(EX_UNAVAILABLE, "cannot open input file \"%s\": %s",
		    argv[0], strerror(errno));

	pt.file = argv[0];
	pt.line = 1;
	pt.col = 0;

	if (options & OPT_FILE) {
		env = forlan_parse_file(fp, &pt);
	} else {
		len = st.st_size;
		buf = grecs_malloc(len);
		if (fread(buf, len, 1, fp) != 1)
			die(EX_UNAVAILABLE, "error reading from \"%s\": %s",
			    argv[0], strerror(errno));
		env = forlan_parse_buffer(buf, len, &pt);
	}
	fclose(fp);

	if (!env)
		return EX_UNAVAILABLE;
	if (options & OPT_DUMP)
		forlan_dump_tree(stdout, env);

	if (argv[1]) {
		struct grecs_node *tree;
		
		fp = fopen(argv[1], "r");
		if (!fp)
			die(EX_UNAVAILABLE,
			    "cannot open input file \"%s\": %s",
			    argv[1], strerror(errno));
		tree = parse_xml(fp);
		fclose(fp);

		if (options & OPT_SORT)
			grecs_tree_sort(tree, node_ident_cmp);
		
		rc = forlan_run(env, tree);
		grecs_tree_free(tree);
	} else
		rc = 0;
	
	forlan_free_environment(env);


	return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.