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"
62 /* !! must be sorted by first column !! */
63 static struct stateswitch stateswitches[] = {
64 { STATE_START, "applications", STATE_START, 0 },
65 { STATE_START, "components", STATE_START, 0 },
66 { STATE_START, "application", STATE_APPLICATION, 0 },
67 { STATE_START, "component", STATE_APPLICATION, 0 },
68 { STATE_APPLICATION, "id", STATE_ID, 1 },
69 { STATE_APPLICATION, "pkgname", STATE_PKGNAME, 1 },
70 { STATE_APPLICATION, "product_license", STATE_LICENCE, 1 },
71 { STATE_APPLICATION, "name", STATE_NAME, 1 },
72 { STATE_APPLICATION, "summary", STATE_SUMMARY, 1 },
73 { STATE_APPLICATION, "description", STATE_DESCRIPTION, 0 },
74 { STATE_APPLICATION, "url", STATE_URL, 1 },
75 { STATE_APPLICATION, "project_group", STATE_GROUP, 1 },
76 { STATE_APPLICATION, "keywords", STATE_KEYWORDS, 0 },
77 { STATE_DESCRIPTION, "p", STATE_P, 1 },
78 { STATE_DESCRIPTION, "ul", STATE_UL, 0 },
79 { STATE_DESCRIPTION, "ol", STATE_OL, 0 },
80 { STATE_UL, "li", STATE_UL_LI, 1 },
81 { STATE_OL, "li", STATE_OL_LI, 1 },
82 { STATE_KEYWORDS, "keyword", STATE_KEYWORD, 1 },
98 struct stateswitch *swtab[NUMSTATES];
99 enum state sbtab[NUMSTATES];
113 static inline const char *
114 find_attr(const char *txt, const char **atts)
116 for (; *atts; atts += 2)
117 if (!strcmp(*atts, txt))
124 startElement(void *userData, const char *name, const char **atts)
126 struct parsedata *pd = userData;
127 Pool *pool = pd->pool;
128 Solvable *s = pd->solvable;
129 struct stateswitch *sw;
132 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
134 if (pd->depth != pd->statedepth)
141 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
144 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
148 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
149 if (!strcmp(sw->ename, name))
152 if (sw->from != pd->state)
155 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
160 pd->docontent = sw->docontent;
161 pd->statedepth = pd->depth;
165 if (!pd->skip_depth && find_attr("xml:lang", atts))
166 pd->skip_depth = pd->depth;
175 case STATE_APPLICATION:
176 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
177 pd->handle = s - pool->solvables;
180 case STATE_DESCRIPTION:
181 pd->description = solv_free(pd->description);
192 /* replace whitespace with one space/newline */
193 /* also strip starting/ending whitespace */
195 wsstrip(struct parsedata *pd)
199 for (i = j = 0; pd->content[i]; i++)
201 if (pd->content[i] == ' ' || pd->content[i] == '\t' || pd->content[i] == '\n')
203 ws |= pd->content[i] == '\n' ? 2 : 1;
207 pd->content[j++] = (ws & 2) ? '\n' : ' ';
209 pd->content[j++] = pd->content[i];
215 /* indent all lines */
217 indent(struct parsedata *pd, int il)
220 for (l = 0; pd->content[l]; )
222 if (pd->content[l] == '\n')
227 if (pd->lcontent + il + 1 > pd->acontent)
229 pd->acontent = pd->lcontent + il + 256;
230 pd->content = realloc(pd->content, pd->acontent);
232 memmove(pd->content + l + il, pd->content + l, pd->lcontent - l + 1);
233 for (i = 0; i < il; i++)
234 pd->content[l + i] = ' ';
236 while (pd->content[l] && pd->content[l] != '\n')
242 add_missing_tags_from_desktop_file(struct parsedata *pd, Solvable *s, const char *desktop_file)
244 Pool *pool = pd->pool;
246 const char *filepath;
251 filepath = pool_tmpjoin(pool, "/usr/share/applications/", desktop_file, 0);
252 if (pd->flags & REPO_USE_ROOTDIR)
253 filepath = pool_prepend_rootdir_tmp(pool, filepath);
254 if (!(fp = fopen(filepath, "r")))
256 while (fgets(buf, sizeof(buf), fp) > 0)
258 int c, l = strlen(buf);
261 if (buf[l - 1] != '\n')
263 /* ignore overlong lines */
264 while ((c = getc(fp)) != EOF)
272 while (l && (buf[l - 1] == ' ' || buf[l - 1] == '\t'))
275 while (*p == ' ' || *p == '\t')
277 if (!*p || *p == '#')
281 if (!strcmp(p, "[Desktop Entry]"))
292 for (p3 = p2 - 1; *p3 == ' ' || *p3 == '\t'; p3--)
295 while (*p2 == ' ' || *p2 == '\t')
299 if (!s->name && !strcmp(p, "Name"))
300 s->name = pool_str2id(pool, pool_tmpjoin(pool, "application:", p2, 0), 1);
301 else if (!pd->havesummary && !strcmp(p, "Comment"))
304 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, p2);
308 if (s->name && pd->havesummary)
309 break; /* our work is done */
315 endElement(void *userData, const char *name)
317 struct parsedata *pd = userData;
318 Pool *pool = pd->pool;
319 Solvable *s = pd->solvable;
323 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
325 if (pd->depth != pd->statedepth)
329 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
337 if (pd->skip_depth && pd->depth + 1 >= pd->skip_depth)
339 if (pd->depth + 1 == pd->skip_depth)
341 pd->state = pd->sbtab[pd->state];
349 case STATE_APPLICATION:
351 s->arch = ARCH_NOARCH;
354 if ((!s->name || !pd->havesummary) && (pd->flags & APPDATA_CHECK_DESKTOP_FILE) != 0 && pd->desktop_file)
355 add_missing_tags_from_desktop_file(pd, s, pd->desktop_file);
356 if (!s->name && pd->desktop_file)
358 char *name = pool_tmpjoin(pool, "application:", pd->desktop_file, 0);
359 int l = strlen(name);
360 if (l > 8 && !strcmp(".desktop", name + l - 8))
362 s->name = pool_strn2id(pool, name, l, 1);
364 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
365 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
367 pd->desktop_file = solv_free(pd->desktop_file);
370 pd->desktop_file = solv_strdup(pd->content);
371 /* guess the appdata.xml file name from the id element */
372 if (pd->lcontent > 8 && !strcmp(".desktop", pd->content + pd->lcontent - 8))
373 pd->content[pd->lcontent - 8] = 0;
374 else if (pd->lcontent > 4 && !strcmp(".ttf", pd->content + pd->lcontent - 4))
375 pd->content[pd->lcontent - 4] = 0;
376 else if (pd->lcontent > 4 && !strcmp(".otf", pd->content + pd->lcontent - 4))
377 pd->content[pd->lcontent - 4] = 0;
378 else if (pd->lcontent > 4 && !strcmp(".xml", pd->content + pd->lcontent - 4))
379 pd->content[pd->lcontent - 4] = 0;
380 else if (pd->lcontent > 3 && !strcmp(".db", pd->content + pd->lcontent - 3))
381 pd->content[pd->lcontent - 3] = 0;
382 id = pool_str2id(pd->pool, pool_tmpjoin(pool, "appdata(", pd->content, ".appdata.xml)"), 1);
383 s->requires = repo_addid_dep(pd->repo, s->requires, id, 0);
384 id = pool_str2id(pd->pool, pool_tmpjoin(pool, "application-appdata(", pd->content, ".appdata.xml)"), 1);
385 s->provides = repo_addid_dep(pd->repo, s->provides, id, 0);
388 s->name = pool_str2id(pd->pool, pool_tmpjoin(pool, "application:", pd->content, 0), 1);
391 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_LICENSE, pd->content);
395 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, pd->content);
398 repodata_set_str(pd->data, pd->handle, SOLVABLE_URL, pd->content);
401 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_GROUP, pd->content);
403 case STATE_DESCRIPTION:
406 /* strip trailing newlines */
407 int l = strlen(pd->description);
408 while (l && pd->description[l - 1] == '\n')
409 pd->description[--l] = 0;
410 repodata_set_str(pd->data, pd->handle, SOLVABLE_DESCRIPTION, pd->description);
415 pd->description = solv_dupappend(pd->description, pd->content, "\n\n");
420 pd->content[2] = '-';
421 pd->description = solv_dupappend(pd->description, pd->content, "\n");
426 if (++pd->licnt >= 10)
427 pd->content[0] = '0' + (pd->licnt / 10) % 10;
428 pd->content[1] = '0' + pd->licnt % 10;
429 pd->content[2] = '.';
430 pd->description = solv_dupappend(pd->description, pd->content, "\n");
434 pd->description = solv_dupappend(pd->description, "\n", 0);
437 id = pool_str2id(pd->pool, pd->content, 1);
438 s->requires = repo_addid_dep(pd->repo, s->requires, id, 0);
441 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_KEYWORDS, pd->content);
447 pd->state = pd->sbtab[pd->state];
451 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
457 characterData(void *userData, const XML_Char *s, int len)
459 struct parsedata *pd = userData;
464 l = pd->lcontent + len + 1;
465 if (l > pd->acontent)
467 pd->acontent = l + 256;
468 pd->content = realloc(pd->content, pd->acontent);
470 c = pd->content + pd->lcontent;
477 #define BUFF_SIZE 8192
480 repo_add_appdata(Repo *repo, FILE *fp, int flags)
482 Pool *pool = repo->pool;
484 struct stateswitch *sw;
490 data = repo_add_repodata(repo, flags);
491 memset(&pd, 0, sizeof(pd));
493 pd.pool = repo->pool;
497 pd.content = malloc(256);
500 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
502 if (!pd.swtab[sw->from])
503 pd.swtab[sw->from] = sw;
504 pd.sbtab[sw->to] = sw->from;
507 XML_Parser parser = XML_ParserCreate(NULL);
508 XML_SetUserData(parser, &pd);
509 XML_SetElementHandler(parser, startElement, endElement);
510 XML_SetCharacterDataHandler(parser, characterData);
514 l = fread(buf, 1, sizeof(buf), fp);
515 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
517 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));
520 repo_free_solvable(repo, pd.solvable - pd.pool->solvables, 1);
529 XML_ParserFree(parser);
531 if (!(flags & REPO_NO_INTERNALIZE))
532 repodata_internalize(data);
534 solv_free(pd.content);
535 solv_free(pd.desktop_file);
536 solv_free(pd.description);
540 /* add all files ending in .appdata.xml */
542 repo_add_appdata_dir(Repo *repo, const char *appdatadir, int flags)
548 data = repo_add_repodata(repo, flags);
549 if (flags & REPO_USE_ROOTDIR)
550 dirpath = pool_prepend_rootdir(repo->pool, appdatadir);
552 dirpath = solv_strdup(appdatadir);
553 if ((dir = opendir(dirpath)) != 0)
555 struct dirent *entry;
556 while ((entry = readdir(dir)))
560 int len = strlen(entry->d_name);
561 if (len <= 12 || strcmp(entry->d_name + len - 12, ".appdata.xml") != 0)
563 if (entry->d_name[0] == '.')
565 n = pool_tmpjoin(repo->pool, dirpath, "/", entry->d_name);
569 pool_error(repo->pool, 0, "%s: %s", n, strerror(errno));
572 repo_add_appdata(repo, fp, flags | REPO_NO_INTERNALIZE | REPO_REUSE_REPODATA | APPDATA_CHECK_DESKTOP_FILE);
578 if (!(flags & REPO_NO_INTERNALIZE))
579 repodata_internalize(data);