4 * Parses AppSteam Data files.
5 * See http://people.freedesktop.org/~hughsient/appdata/
8 * Copyright (c) 2013, Novell Inc.
10 * This program is licensed under the BSD license, read LICENSE.BSD
11 * for further information
14 #include <sys/types.h>
31 #include "repo_appdata.h"
59 /* !! must be sorted by first column !! */
60 static struct stateswitch stateswitches[] = {
61 { STATE_START, "applications", STATE_START, 0 },
62 { STATE_START, "application", STATE_APPLICATION, 0 },
63 { STATE_APPLICATION, "id", STATE_ID, 1 },
64 { STATE_APPLICATION, "licence", STATE_LICENCE, 1 },
65 { STATE_APPLICATION, "name", STATE_NAME, 1 },
66 { STATE_APPLICATION, "summary", STATE_SUMMARY, 1 },
67 { STATE_APPLICATION, "description", STATE_DESCRIPTION, 0 },
68 { STATE_APPLICATION, "url", STATE_URL, 1 },
69 { STATE_APPLICATION, "project_group", STATE_GROUP, 1 },
70 { STATE_DESCRIPTION, "p", STATE_P, 1 },
71 { STATE_DESCRIPTION, "ul", STATE_UL, 0 },
72 { STATE_DESCRIPTION, "ol", STATE_OL, 0 },
73 { STATE_UL, "li", STATE_UL_LI, 1 },
74 { STATE_OL, "li", STATE_OL_LI, 1 },
90 struct stateswitch *swtab[NUMSTATES];
91 enum state sbtab[NUMSTATES];
101 static inline const char *
102 find_attr(const char *txt, const char **atts)
104 for (; *atts; atts += 2)
105 if (!strcmp(*atts, txt))
112 startElement(void *userData, const char *name, const char **atts)
114 struct parsedata *pd = userData;
115 Pool *pool = pd->pool;
116 Solvable *s = pd->solvable;
117 struct stateswitch *sw;
120 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
122 if (pd->depth != pd->statedepth)
129 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
132 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
136 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
137 if (!strcmp(sw->ename, name))
140 if (sw->from != pd->state)
143 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
148 pd->docontent = sw->docontent;
149 pd->statedepth = pd->depth;
155 case STATE_APPLICATION:
156 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
157 pd->handle = s - pool->solvables;
159 case STATE_DESCRIPTION:
160 pd->description = solv_free(pd->description);
170 /* replace whitespace with one space/newline */
171 /* also strip starting/ending whitespace */
173 wsstrip(struct parsedata *pd)
177 for (i = j = 0; pd->content[i]; i++)
179 if (pd->content[i] == ' ' || pd->content[i] == '\t' || pd->content[i] == '\n')
181 ws |= pd->content[i] == '\n' ? 2 : 1;
185 pd->content[j++] = (ws & 2) ? '\n' : ' ';
187 pd->content[j++] = pd->content[i];
193 /* indent all lines */
195 indent(struct parsedata *pd, int il)
198 for (l = 0; pd->content[l]; )
200 if (pd->content[l] == '\n')
205 if (pd->lcontent + il + 1 > pd->acontent)
207 pd->acontent = pd->lcontent + il + 256;
208 pd->content = realloc(pd->content, pd->acontent);
210 memmove(pd->content + l + il, pd->content + l, pd->lcontent - l + 1);
211 for (i = 0; i < il; i++)
212 pd->content[l + i] = ' ';
214 while (pd->content[l] && pd->content[l] != '\n')
220 endElement(void *userData, const char *name)
222 struct parsedata *pd = userData;
223 Pool *pool = pd->pool;
224 Solvable *s = pd->solvable;
228 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
230 if (pd->depth != pd->statedepth)
234 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
244 case STATE_APPLICATION:
246 s->arch = ARCH_NOARCH;
249 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
250 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
254 if (pd->lcontent > 8 && !strcmp(".desktop", pd->content + pd->lcontent - 8))
255 pd->content[pd->lcontent - 8] = 0;
256 id = pool_str2id(pd->pool, pool_tmpjoin(pool, "appdata(", pd->content, ")"), 1);
257 s->requires = repo_addid_dep(pd->repo, s->requires, id, 0);
258 id = pool_str2id(pd->pool, pool_tmpjoin(pool, "application-appdata(", pd->content, ")"), 1);
259 s->provides = repo_addid_dep(pd->repo, s->provides, id, 0);
262 s->name = pool_str2id(pd->pool, pool_tmpjoin(pool, "application:", pd->content, 0), 1);
265 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_LICENSE, pd->content);
268 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, pd->content);
271 repodata_set_str(pd->data, pd->handle, SOLVABLE_URL, pd->content);
274 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_GROUP, pd->content);
276 case STATE_DESCRIPTION:
279 /* strip trailing newlines */
280 int l = strlen(pd->description);
281 while (l && pd->description[l - 1] == '\n')
282 pd->description[--l] = 0;
283 repodata_set_str(pd->data, pd->handle, SOLVABLE_DESCRIPTION, pd->description);
288 pd->description = solv_dupappend(pd->description, pd->content, "\n\n");
293 pd->content[2] = '-';
294 pd->description = solv_dupappend(pd->description, pd->content, "\n");
299 if (++pd->licnt >= 10)
300 pd->content[0] = '0' + (pd->licnt / 10) % 10;
301 pd->content[1] = '0' + pd->licnt % 10;
302 pd->content[2] = '.';
303 pd->description = solv_dupappend(pd->description, pd->content, "\n");
307 pd->description = solv_dupappend(pd->description, "\n", 0);
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->acontent = l + 256;
334 pd->content = realloc(pd->content, pd->acontent);
336 c = pd->content + pd->lcontent;
343 #define BUFF_SIZE 8192
346 repo_add_appdata(Repo *repo, FILE *fp, int flags)
348 Pool *pool = repo->pool;
350 struct stateswitch *sw;
356 data = repo_add_repodata(repo, flags);
357 memset(&pd, 0, sizeof(pd));
359 pd.pool = repo->pool;
362 pd.content = malloc(256);
365 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
367 if (!pd.swtab[sw->from])
368 pd.swtab[sw->from] = sw;
369 pd.sbtab[sw->to] = sw->from;
372 XML_Parser parser = XML_ParserCreate(NULL);
373 XML_SetUserData(parser, &pd);
374 XML_SetElementHandler(parser, startElement, endElement);
375 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_error(pool, -1, "repo_appdata: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
389 XML_ParserFree(parser);
391 if (!(flags & REPO_NO_INTERNALIZE))
392 repodata_internalize(data);
398 /* add all files ending in .appdata.xml */
400 repo_add_appdata_dir(Repo *repo, const char *appdatadir, int flags)
406 data = repo_add_repodata(repo, flags);
407 if (flags & REPO_USE_ROOTDIR)
408 dirpath = pool_prepend_rootdir(repo->pool, appdatadir);
410 dirpath = solv_strdup(appdatadir);
411 if ((dir = opendir(dirpath)) != 0)
413 struct dirent *entry;
414 while ((entry = readdir(dir)))
418 int len = strlen(entry->d_name);
419 if (len <= 12 || strcmp(entry->d_name + len - 12, ".appdata.xml") != 0)
421 if (entry->d_name[0] == '.')
423 n = pool_tmpjoin(repo->pool, dirpath, "/", entry->d_name);
427 pool_error(repo->pool, 0, "%s: %s", n, strerror(errno));
430 repo_add_appdata(repo, fp, flags | REPO_NO_INTERNALIZE | REPO_REUSE_REPODATA);
434 if (!(flags & REPO_NO_INTERNALIZE))
435 repodata_internalize(data);