|
krh@80
|
1 |
#include <stdio.h>
|
|
krh@80
|
2 |
#include <string.h>
|
|
krh@80
|
3 |
#include <stdarg.h>
|
|
krh@80
|
4 |
#include <unistd.h>
|
|
krh@80
|
5 |
#include <fcntl.h>
|
|
krh@80
|
6 |
#include <errno.h>
|
|
krh@80
|
7 |
#include <expat.h>
|
|
krh@80
|
8 |
|
|
krh@80
|
9 |
#include "razor.h"
|
|
krh@80
|
10 |
|
|
krh@80
|
11 |
#define XML_BUFFER_SIZE 4096
|
|
krh@80
|
12 |
|
|
krh@80
|
13 |
static void
|
|
krh@80
|
14 |
parse_xml_file(const char *filename,
|
|
krh@80
|
15 |
XML_StartElementHandler start,
|
|
krh@80
|
16 |
XML_EndElementHandler end,
|
|
krh@80
|
17 |
void *data)
|
|
krh@80
|
18 |
{
|
|
krh@80
|
19 |
XML_Parser parser;
|
|
krh@80
|
20 |
char *buffer;
|
|
krh@80
|
21 |
int fd, len, err;
|
|
krh@80
|
22 |
|
|
krh@80
|
23 |
parser = XML_ParserCreate(NULL);
|
|
krh@80
|
24 |
XML_SetElementHandler(parser, start, end);
|
|
krh@80
|
25 |
XML_SetUserData(parser, data);
|
|
krh@80
|
26 |
|
|
krh@80
|
27 |
buffer = XML_GetBuffer(parser, XML_BUFFER_SIZE);
|
|
krh@80
|
28 |
|
|
krh@80
|
29 |
fd = open(filename, O_RDONLY);
|
|
krh@80
|
30 |
if (fd < 0) {
|
|
krh@80
|
31 |
fprintf(stderr, "open: %m\n");
|
|
krh@80
|
32 |
exit(-1);
|
|
krh@80
|
33 |
}
|
|
krh@80
|
34 |
|
|
krh@80
|
35 |
while (len = read(fd, buffer, XML_BUFFER_SIZE), len > 0) {
|
|
krh@80
|
36 |
err = XML_ParseBuffer(parser, len, len == 0);
|
|
krh@80
|
37 |
if (err == XML_STATUS_ERROR) {
|
|
krh@80
|
38 |
fprintf(stderr, "parse error at line %lu:\n%s\n",
|
|
krh@80
|
39 |
XML_GetCurrentLineNumber(parser),
|
|
krh@80
|
40 |
XML_ErrorString(XML_GetErrorCode(parser)));
|
|
krh@80
|
41 |
exit(-1);
|
|
krh@80
|
42 |
}
|
|
krh@80
|
43 |
}
|
|
krh@80
|
44 |
|
|
krh@80
|
45 |
if (fd < 0) {
|
|
krh@80
|
46 |
fprintf(stderr, "read: %m\n");
|
|
krh@80
|
47 |
exit(-1);
|
|
krh@80
|
48 |
}
|
|
krh@80
|
49 |
|
|
krh@80
|
50 |
close(fd);
|
|
krh@80
|
51 |
}
|
|
krh@80
|
52 |
|
|
krh@80
|
53 |
struct test_set {
|
|
krh@80
|
54 |
char *name;
|
|
krh@80
|
55 |
struct razor_set *set;
|
|
krh@80
|
56 |
struct test_set *next;
|
|
krh@80
|
57 |
};
|
|
krh@80
|
58 |
|
|
krh@80
|
59 |
struct test_context {
|
|
krh@80
|
60 |
struct razor_importer *importer;
|
|
krh@80
|
61 |
struct test_set *sets;
|
|
krh@80
|
62 |
};
|
|
krh@80
|
63 |
|
|
krh@80
|
64 |
static void
|
|
krh@80
|
65 |
get_atts(const char **atts, ...)
|
|
krh@80
|
66 |
{
|
|
krh@80
|
67 |
va_list ap;
|
|
krh@80
|
68 |
const char *name, **ptr;
|
|
krh@80
|
69 |
int i;
|
|
krh@80
|
70 |
|
|
krh@80
|
71 |
va_start(ap, atts);
|
|
krh@80
|
72 |
while (name = va_arg(ap, const char *), name != NULL) {
|
|
krh@80
|
73 |
ptr = va_arg(ap, const char **);
|
|
krh@80
|
74 |
for (i = 0; atts[i]; i += 2) {
|
|
krh@80
|
75 |
if (strcmp(atts[i], name) == 0)
|
|
krh@80
|
76 |
*ptr = atts[i + 1];
|
|
krh@80
|
77 |
}
|
|
krh@80
|
78 |
}
|
|
krh@80
|
79 |
va_end(ap);
|
|
krh@80
|
80 |
}
|
|
krh@80
|
81 |
|
|
krh@80
|
82 |
static void
|
|
krh@80
|
83 |
parse_property(struct test_context *ctx, const char **atts,
|
|
krh@80
|
84 |
enum razor_property_type type)
|
|
krh@80
|
85 |
{
|
|
krh@80
|
86 |
const char *name = NULL, *version = NULL;
|
|
krh@80
|
87 |
int i;
|
|
krh@80
|
88 |
|
|
krh@80
|
89 |
for (i = 0; atts[i]; i += 2) {
|
|
krh@80
|
90 |
if (strcmp(atts[i], "name") == 0)
|
|
krh@80
|
91 |
name = atts[i + 1];
|
|
krh@80
|
92 |
else if (strcmp(atts[i], "eq") == 0)
|
|
krh@80
|
93 |
version = atts[i + 1];
|
|
krh@80
|
94 |
}
|
|
krh@80
|
95 |
|
|
krh@80
|
96 |
if (name == NULL) {
|
|
krh@80
|
97 |
fprintf(stderr, "no name specified for property\n");
|
|
krh@80
|
98 |
exit(-1);
|
|
krh@80
|
99 |
}
|
|
krh@80
|
100 |
|
|
krh@80
|
101 |
razor_importer_add_property(ctx->importer, name, version, type);
|
|
krh@80
|
102 |
}
|
|
krh@80
|
103 |
|
|
krh@80
|
104 |
static void
|
|
krh@80
|
105 |
start_test_sets_element(void *data, const char *element, const char **atts)
|
|
krh@80
|
106 |
{
|
|
krh@80
|
107 |
struct test_context *ctx = data;
|
|
krh@80
|
108 |
struct test_set *set;
|
|
krh@80
|
109 |
const char *name, *version;
|
|
krh@80
|
110 |
|
|
krh@80
|
111 |
if (strcmp(element, "set") == 0) {
|
|
krh@80
|
112 |
get_atts(atts, "name", &name, NULL);
|
|
krh@80
|
113 |
ctx->importer = razor_importer_new();
|
|
krh@80
|
114 |
set = malloc(sizeof *set);
|
|
krh@80
|
115 |
set->name = strdup(name);
|
|
krh@80
|
116 |
set->next = ctx->sets;
|
|
krh@80
|
117 |
ctx->sets = set;
|
|
krh@80
|
118 |
} else if (strcmp(element, "package") == 0) {
|
|
krh@80
|
119 |
get_atts(atts, "name", &name, "version", &version, NULL);
|
|
krh@80
|
120 |
razor_importer_begin_package(ctx->importer, name, version);
|
|
krh@80
|
121 |
} else if (strcmp(element, "requires") == 0) {
|
|
krh@80
|
122 |
parse_property(ctx, atts, RAZOR_PROPERTY_REQUIRES);
|
|
krh@80
|
123 |
} else if (strcmp(element, "provides") == 0) {
|
|
krh@80
|
124 |
parse_property(ctx, atts, RAZOR_PROPERTY_PROVIDES);
|
|
krh@80
|
125 |
} else if (strcmp(element, "obsoletes") == 0) {
|
|
krh@80
|
126 |
parse_property(ctx, atts, RAZOR_PROPERTY_OBSOLETES);
|
|
krh@80
|
127 |
} else if (strcmp(element, "conflicts") == 0) {
|
|
krh@80
|
128 |
parse_property(ctx, atts, RAZOR_PROPERTY_CONFLICTS);
|
|
krh@80
|
129 |
} else if (strcmp(element, "file") == 0) {
|
|
krh@80
|
130 |
get_atts(atts, "name", &name, NULL);
|
|
krh@80
|
131 |
razor_importer_add_file(ctx->importer, name);
|
|
krh@80
|
132 |
} else if (strcmp(element, "dir") == 0) {
|
|
krh@80
|
133 |
get_atts(atts, "name", &name, NULL);
|
|
krh@80
|
134 |
razor_importer_add_file(ctx->importer, name);
|
|
krh@80
|
135 |
}
|
|
krh@80
|
136 |
}
|
|
krh@80
|
137 |
|
|
krh@80
|
138 |
static void
|
|
krh@80
|
139 |
end_test_sets_element (void *data, const char *name)
|
|
krh@80
|
140 |
{
|
|
krh@80
|
141 |
struct test_context *ctx = data;
|
|
krh@80
|
142 |
|
|
krh@80
|
143 |
if (strcmp(name, "set") == 0) {
|
|
krh@80
|
144 |
ctx->sets->set = razor_importer_finish(ctx->importer);
|
|
krh@80
|
145 |
} else if (strcmp(name, "package") == 0) {
|
|
krh@80
|
146 |
razor_importer_finish_package(ctx->importer);
|
|
krh@80
|
147 |
}
|
|
krh@80
|
148 |
}
|
|
krh@80
|
149 |
|
|
krh@80
|
150 |
int main(int argc, char *argv[])
|
|
krh@80
|
151 |
{
|
|
krh@80
|
152 |
struct test_context ctx;
|
|
krh@80
|
153 |
struct test_set *set;
|
|
krh@80
|
154 |
|
|
krh@80
|
155 |
if (argc != 3) {
|
|
krh@80
|
156 |
fprintf(stderr, "usage: %s SETS-FILE TESTS-FILE\n", argv[0]);
|
|
krh@80
|
157 |
exit(-1);
|
|
krh@80
|
158 |
}
|
|
krh@80
|
159 |
|
|
krh@80
|
160 |
memset(&ctx, 0, sizeof ctx);
|
|
krh@80
|
161 |
parse_xml_file(argv[1],
|
|
krh@80
|
162 |
start_test_sets_element,
|
|
krh@80
|
163 |
end_test_sets_element,
|
|
krh@80
|
164 |
&ctx);
|
|
krh@80
|
165 |
|
|
krh@80
|
166 |
for (set = ctx.sets; set != NULL; set = set->next)
|
|
krh@80
|
167 |
printf("set %s\n", set->name);
|
|
krh@80
|
168 |
|
|
krh@80
|
169 |
return 0;
|
|
krh@80
|
170 |
}
|