forked from blackav/ejudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-clars.c
240 lines (211 loc) · 6.39 KB
/
convert-clars.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* -*- mode: c -*- */
/* Copyright (C) 2008-2015 Alexander Chernov <[email protected]> */
/*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*/
#include "ejudge/config.h"
#include "ejudge/ej_types.h"
#include "ejudge/ej_limits.h"
#include "ejudge/version.h"
#include "ejudge/ejudge_cfg.h"
#include "ejudge/contests.h"
#include "ejudge/clarlog.h"
#include "ejudge/xml_utils.h"
#include "ejudge/compat.h"
#include "ejudge/ej_uuid.h"
#include "ejudge/xalloc.h"
#include "ejudge/osdeps.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
static const unsigned char *program_name = "";
static const unsigned char *ejudge_xml_path = 0;
static const unsigned char *src_plugin_name = 0;
static const unsigned char *dst_plugin_name = 0;
static int contest_id = 0;
static struct ejudge_cfg *config = 0;
static const struct contest_desc *cnts = 0;
static clarlog_state_t src_clarlog = 0;
static clarlog_state_t dst_clarlog = 0;
static void
die(const char *format, ...)
__attribute__((format(printf, 1, 2), noreturn));
static void
die(const char *format, ...)
{
va_list args;
char buf[1024];
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
fprintf(stderr, "%s: %s\n", program_name,
buf);
exit(1);
}
static void write_help(void) __attribute__((noreturn));
static void
write_help(void)
{
printf("%s: clarification database converter\n"
"Usage: %s [OPTIONS] CNTS-ID SRC-PLUGIN DST-PLUGIN\n"
" OPTIONS:\n"
" --help write this message and exit\n"
" --version report version and exit\n"
" -f CFG specify the ejudge configuration file\n"
/*" COMMAND:\n"*/
/*" status report the new-server status\n"*/,
program_name, program_name);
exit(0);
}
static void write_version(void) __attribute__((noreturn));
static void
write_version(void)
{
printf("%s %s, compiled %s\n", program_name, compile_version, compile_date);
exit(0);
}
/* force linking of certain functions that may be needed by plugins */
void *forced_link_table[] =
{
xml_parse_ip,
xml_parse_date,
xml_parse_int,
xml_parse_ip_mask,
xml_parse_bool,
xml_unparse_text,
xml_unparse_bool,
xml_unparse_ip,
xml_unparse_date,
xml_unparse_ip_mask,
xml_err_get_elem_name,
xml_err_get_attr_name,
xml_err,
xml_err_a,
xml_err_attrs,
xml_err_nested_elems,
xml_err_attr_not_allowed,
xml_err_elem_not_allowed,
xml_err_elem_redefined,
xml_err_top_level,
xml_err_top_level_s,
xml_err_attr_invalid,
xml_err_elem_undefined,
xml_err_elem_undefined_s,
xml_err_attr_undefined,
xml_err_attr_undefined_s,
xml_err_elem_invalid,
xml_err_elem_empty,
xml_leaf_elem,
xml_empty_text,
xml_empty_text_c,
xml_attr_bool,
xml_attr_bool_byte,
xml_attr_int,
xml_attr_ulong,
xml_attr_date,
xml_do_parse_ipv6,
xml_parse_ipv6_2,
xml_parse_ipv6,
xml_unparse_ipv6,
ipv6cmp,
ipv6_match_mask,
xml_msg,
xml_unparse_ipv6_mask,
xml_parse_ipv6_mask,
xml_elem_ipv6_mask,
ipv6_is_empty,
xml_unparse_full_cookie,
xml_parse_full_cookie,
close_memstream,
};
int
main(int argc, char *argv[])
{
int i = 1;
char *eptr = 0;
int total_clars, clar_id;
struct clar_entry_v2 clar;
unsigned char *text = 0;
size_t size = 0;
program_name = os_GetBasename(argv[0]);
if (argc <= 1) die("not enough parameters");
if (!strcmp(argv[1], "--help")) {
write_help();
} else if (!strcmp(argv[1], "--version")) {
write_version();
}
i = 1;
while (i < argc) {
if (!strcmp(argv[i], "-f")) {
if (i + 1 >= argc) die("argument expected for `-f'");
ejudge_xml_path = argv[i + 1];
i += 2;
} else if (!strcmp(argv[i], "--")) {
i++;
break;
} else if (argv[i][0] == '-') {
die("invalid option `%s'", argv[i]);
} else {
break;
}
}
#if defined EJUDGE_XML_PATH
if (!ejudge_xml_path) ejudge_xml_path = EJUDGE_XML_PATH;
#endif /* EJUDGE_XML_PATH */
if (!ejudge_xml_path) die("ejudge.xml path is not specified");
if (!(config = ejudge_cfg_parse(ejudge_xml_path, 1))) return 1;
if (!config->contests_dir) die("<contests_dir> tag is not set!");
if (contests_set_directory(config->contests_dir) < 0)
die("contests directory is invalid");
if (i >= argc) die("contest-id is expected");
if (!argv[i][0]) die("contest-id is not specified");
errno = 0;
contest_id = strtol(argv[i], &eptr, 10);
if (*eptr || errno || contest_id <= 0 || contest_id > EJ_MAX_CONTEST_ID)
die("contest-id is invalid");
i++;
if (i >= argc) die("source plugin name is expected");
src_plugin_name = argv[i];
i++;
if (i >= argc) die("destination plugin name is expected");
dst_plugin_name = argv[i];
i++;
if (i < argc) die("extra parameters");
if (!src_plugin_name || !*src_plugin_name) src_plugin_name = "file";
if (!dst_plugin_name || !*dst_plugin_name) dst_plugin_name = "file";
if (!strcmp(src_plugin_name, dst_plugin_name))
die("plugins are the same");
if (contests_get(contest_id, &cnts) < 0 || !cnts)
die("cannot load contest %d", contest_id);
if (!(src_clarlog = clar_init()))
die("cannot open the source clarlog");
if (!(dst_clarlog = clar_init()))
die("cannot open the destination clarlog");
if (clar_open(src_clarlog, config, cnts, 0, src_plugin_name, 0) < 0)
die("cannot open the source clarlog");
if (clar_open(dst_clarlog, config, cnts, 0, dst_plugin_name, 0) < 0)
die("cannot open the destination clarlog");
total_clars = clar_get_total(src_clarlog);
for (clar_id = 0; clar_id < total_clars; clar_id++) {
if (clar_get_record(src_clarlog, clar_id, &clar) < 0) continue;
if (clar.id < 0) continue;
if (!ej_uuid_is_nonempty(clar.uuid)) {
ej_uuid_generate(&clar.uuid);
}
clar_put_record(dst_clarlog, clar_id, &clar);
if (clar_get_raw_text(src_clarlog, clar_id, &text, &size) < 0) continue;
clar_add_text(dst_clarlog, clar_id, &clar.uuid, text, size);
xfree(text); text = 0; size = 0;
}
return 0;
}