4 * Parses RedHat comps format
6 * Copyright (c) 2012, Novell Inc.
8 * This program is licensed under the BSD license, read LICENSE.BSD
9 * for further information
12 #include <sys/types.h>
29 #include "tools_util.h"
30 #include "repo_comps.h"
35 * what's the difference between group/category?
36 * handle "default" and "langonly".
38 * maybe handle REL_COND in solver recommends handling?
72 /* must be sorted by first column */
73 static struct stateswitch stateswitches[] = {
74 { STATE_START, "comps", STATE_COMPS, 0 },
75 { STATE_COMPS, "group", STATE_GROUP, 0 },
76 { STATE_COMPS, "category", STATE_CATEGORY, 0 },
77 { STATE_GROUP, "id", STATE_ID, 1 },
78 { STATE_GROUP, "name", STATE_NAME, 1 },
79 { STATE_GROUP, "description", STATE_DESCRIPTION, 1 },
80 { STATE_GROUP, "uservisible", STATE_USERVISIBLE, 1 },
81 { STATE_GROUP, "display_order", STATE_DISPLAY_ORDER, 1 },
82 { STATE_GROUP, "default", STATE_DEFAULT, 1 },
83 { STATE_GROUP, "langonly", STATE_LANGONLY, 1 },
84 { STATE_GROUP, "lang_only", STATE_LANG_ONLY, 1 },
85 { STATE_GROUP, "packagelist", STATE_PACKAGELIST, 0 },
86 { STATE_PACKAGELIST, "packagereq", STATE_PACKAGEREQ, 1 },
87 { STATE_CATEGORY, "id", STATE_CID, 1 },
88 { STATE_CATEGORY, "name", STATE_CNAME, 1 },
89 { STATE_CATEGORY, "description", STATE_CDESCRIPTION, 1 },
90 { STATE_CATEGORY , "grouplist", STATE_GROUPLIST, 0 },
91 { STATE_CATEGORY , "display_order", STATE_CDISPLAY_ORDER, 1 },
92 { STATE_GROUPLIST, "groupid", STATE_GROUPID, 1 },
110 struct stateswitch *swtab[NUMSTATES];
111 enum state sbtab[NUMSTATES];
124 * find value for xml attribute
125 * I: txt, name of attribute
126 * I: atts, list of key/value attributes
128 * O: pointer to value of matching key, or NULL
132 static inline const char *
133 find_attr(const char *txt, const char **atts, int dup)
135 for (; *atts; atts += 2)
137 if (!strcmp(*atts, txt))
138 return dup ? solv_strdup(atts[1]) : atts[1];
145 * XML callback: startElement
149 startElement(void *userData, const char *name, const char **atts)
151 struct parsedata *pd = userData;
152 Pool *pool = pd->pool;
153 Solvable *s = pd->solvable;
154 struct stateswitch *sw;
157 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
159 if (pd->depth != pd->statedepth)
166 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
169 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
173 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
174 if (!strcmp(sw->ename, name))
177 if (sw->from != pd->state)
180 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
185 pd->docontent = sw->docontent;
186 pd->statedepth = pd->depth;
194 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
195 pd->handle = s - pool->solvables;
200 case STATE_DESCRIPTION:
201 case STATE_CDESCRIPTION:
202 pd->tmplang = find_attr("xml:lang", atts, 1);
205 case STATE_PACKAGEREQ:
207 const char *type = find_attr("type", atts, 0);
209 pd->reqtype = SOLVABLE_RECOMMENDS;
210 if (type && !strcmp(type, "conditional"))
212 const char *requires = find_attr("requires", atts, 0);
213 if (requires && *requires)
214 pd->condreq = pool_str2id(pool, requires, 1);
216 else if (type && !strcmp(type, "mandatory"))
217 pd->reqtype = SOLVABLE_REQUIRES;
218 else if (type && !strcmp(type, "optional"))
219 pd->reqtype = SOLVABLE_SUGGESTS;
230 endElement(void *userData, const char *name)
232 struct parsedata *pd = userData;
233 Solvable *s = pd->solvable;
237 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
239 if (pd->depth != pd->statedepth)
243 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
256 s->arch = ARCH_NOARCH;
259 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
260 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
266 s->name = pool_str2id(pd->pool, join2("pattern", ":", pd->content), 1);
271 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
272 pd->tmplang = solv_free((void *)pd->tmplang);
275 case STATE_DESCRIPTION:
276 case STATE_CDESCRIPTION:
277 repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_DESCRIPTION, pd->tmplang, 1), pd->content);
278 pd->tmplang = solv_free((void *)pd->tmplang);
281 case STATE_PACKAGEREQ:
282 id = pool_str2id(pd->pool, pd->content, 1);
284 id = pool_rel2id(pd->pool, id, pd->condreq, REL_COND, 1);
285 repo_add_idarray(pd->repo, pd->handle, pd->reqtype, id);
289 id = pool_str2id(pd->pool, join2("pattern", ":", pd->content), 1);
290 s->requires = repo_addid_dep(pd->repo, s->requires, id, 0);
293 case STATE_USERVISIBLE:
294 repodata_set_void(pd->data, pd->handle, SOLVABLE_ISVISIBLE);
297 case STATE_DISPLAY_ORDER:
298 case STATE_CDISPLAY_ORDER:
299 repodata_set_str(pd->data, pd->handle, SOLVABLE_ORDER, pd->content);
306 case STATE_LANG_ONLY:
313 pd->state = pd->sbtab[pd->state];
317 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
323 characterData(void *userData, const XML_Char *s, int len)
325 struct parsedata *pd = userData;
330 l = pd->lcontent + len + 1;
331 if (l > pd->acontent)
333 pd->content = solv_realloc(pd->content, l + 256);
334 pd->acontent = l + 256;
336 c = pd->content + pd->lcontent;
343 #define BUFF_SIZE 8192
347 repo_add_comps(Repo *repo, FILE *fp, int flags)
353 struct stateswitch *sw;
356 data = repo_add_repodata(repo, flags);
358 memset(&pd, 0, sizeof(pd));
360 pd.pool = repo->pool;
363 pd.content = solv_malloc(256);
366 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
368 if (!pd.swtab[sw->from])
369 pd.swtab[sw->from] = sw;
370 pd.sbtab[sw->to] = sw->from;
373 parser = XML_ParserCreate(NULL);
374 XML_SetUserData(parser, &pd);
375 XML_SetElementHandler(parser, startElement, endElement);
376 XML_SetCharacterDataHandler(parser, characterData);
379 l = fread(buf, 1, sizeof(buf), fp);
380 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
382 pool_debug(pd.pool, SOLV_ERROR, "%s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
388 XML_ParserFree(parser);
390 solv_free((void *)pd.tmplang);
391 solv_free(pd.content);
394 if (!(flags & REPO_NO_INTERNALIZE))
395 repodata_internalize(data);