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];
108 static inline const char *
109 find_attr(const char *txt, const char **atts)
111 for (; *atts; atts += 2)
112 if (!strcmp(*atts, txt))
119 startElement(void *userData, const char *name, const char **atts)
121 struct parsedata *pd = userData;
122 Pool *pool = pd->pool;
123 Solvable *s = pd->solvable;
124 struct stateswitch *sw;
127 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
129 if (pd->depth != pd->statedepth)
136 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
139 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
143 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
144 if (!strcmp(sw->ename, name))
147 if (sw->from != pd->state)
150 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
155 pd->docontent = sw->docontent;
156 pd->statedepth = pd->depth;
162 case STATE_APPLICATION:
163 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
164 pd->handle = s - pool->solvables;
170 if (find_attr("xml:lang", atts))
173 case STATE_DESCRIPTION:
175 if (find_attr("xml:lang", atts))
177 pd->description = solv_free(pd->description);
182 if (find_attr("xml:lang", atts))
188 if (find_attr("xml:lang", atts))
194 if (find_attr("xml:lang", atts))
202 /* replace whitespace with one space/newline */
203 /* also strip starting/ending whitespace */
205 wsstrip(struct parsedata *pd)
209 for (i = j = 0; pd->content[i]; i++)
211 if (pd->content[i] == ' ' || pd->content[i] == '\t' || pd->content[i] == '\n')
213 ws |= pd->content[i] == '\n' ? 2 : 1;
217 pd->content[j++] = (ws & 2) ? '\n' : ' ';
219 pd->content[j++] = pd->content[i];
225 /* indent all lines */
227 indent(struct parsedata *pd, int il)
230 for (l = 0; pd->content[l]; )
232 if (pd->content[l] == '\n')
237 if (pd->lcontent + il + 1 > pd->acontent)
239 pd->acontent = pd->lcontent + il + 256;
240 pd->content = realloc(pd->content, pd->acontent);
242 memmove(pd->content + l + il, pd->content + l, pd->lcontent - l + 1);
243 for (i = 0; i < il; i++)
244 pd->content[l + i] = ' ';
246 while (pd->content[l] && pd->content[l] != '\n')
252 add_missing_tags_from_desktop_file(struct parsedata *pd, Solvable *s, const char *desktop_file)
254 Pool *pool = pd->pool;
256 const char *filepath;
261 filepath = pool_tmpjoin(pool, "/usr/share/applications/", desktop_file, 0);
262 if (pd->flags & REPO_USE_ROOTDIR)
263 filepath = pool_prepend_rootdir_tmp(pool, filepath);
264 if (!(fp = fopen(filepath, "r")))
266 while (fgets(buf, sizeof(buf), fp) > 0)
268 int c, l = strlen(buf);
271 if (buf[l - 1] != '\n')
273 /* ignore overlong lines */
274 while ((c = getc(fp)) != EOF)
282 while (l && (buf[l - 1] == ' ' || buf[l - 1] == '\t'))
285 while (*p == ' ' || *p == '\t')
287 if (!*p || *p == '#')
291 if (!strcmp(p, "[Desktop Entry]"))
302 for (p3 = p2 - 1; *p3 == ' ' || *p3 == '\t'; p3--)
305 while (*p2 == ' ' || *p2 == '\t')
309 if (!s->name && !strcmp(p, "Name"))
310 s->name = pool_str2id(pool, pool_tmpjoin(pool, "application:", p2, 0), 1);
311 else if (!pd->havesummary && !strcmp(p, "Comment"))
314 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, p2);
318 if (s->name && pd->havesummary)
319 break; /* our work is done */
325 endElement(void *userData, const char *name)
327 struct parsedata *pd = userData;
328 Pool *pool = pd->pool;
329 Solvable *s = pd->solvable;
333 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
335 if (pd->depth != pd->statedepth)
339 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
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);
390 s->name = pool_str2id(pd->pool, pool_tmpjoin(pool, "application:", pd->content, 0), 1);
393 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_LICENSE, pd->content);
399 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, pd->content);
402 repodata_set_str(pd->data, pd->handle, SOLVABLE_URL, pd->content);
405 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_GROUP, pd->content);
407 case STATE_DESCRIPTION:
408 if (pd->description && !pd->skip_tag_d)
410 /* strip trailing newlines */
411 int l = strlen(pd->description);
412 while (l && pd->description[l - 1] == '\n')
413 pd->description[--l] = 0;
414 repodata_set_str(pd->data, pd->handle, SOLVABLE_DESCRIPTION, pd->description);
421 pd->description = solv_dupappend(pd->description, pd->content, "\n\n");
424 if (pd->skip_tag || pd->skip_tag_li)
428 pd->content[2] = '-';
429 pd->description = solv_dupappend(pd->description, pd->content, "\n");
432 if (pd->skip_tag || pd->skip_tag_li)
436 if (++pd->licnt >= 10)
437 pd->content[0] = '0' + (pd->licnt / 10) % 10;
438 pd->content[1] = '0' + pd->licnt % 10;
439 pd->content[2] = '.';
440 pd->description = solv_dupappend(pd->description, pd->content, "\n");
446 pd->description = solv_dupappend(pd->description, "\n", 0);
452 pd->state = pd->sbtab[pd->state];
456 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
462 characterData(void *userData, const XML_Char *s, int len)
464 struct parsedata *pd = userData;
469 l = pd->lcontent + len + 1;
470 if (l > pd->acontent)
472 pd->acontent = l + 256;
473 pd->content = realloc(pd->content, pd->acontent);
475 c = pd->content + pd->lcontent;
482 #define BUFF_SIZE 8192
485 repo_add_appdata(Repo *repo, FILE *fp, int flags)
487 Pool *pool = repo->pool;
489 struct stateswitch *sw;
495 data = repo_add_repodata(repo, flags);
496 memset(&pd, 0, sizeof(pd));
498 pd.pool = repo->pool;
502 pd.content = malloc(256);
505 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
507 if (!pd.swtab[sw->from])
508 pd.swtab[sw->from] = sw;
509 pd.sbtab[sw->to] = sw->from;
512 XML_Parser parser = XML_ParserCreate(NULL);
513 XML_SetUserData(parser, &pd);
514 XML_SetElementHandler(parser, startElement, endElement);
515 XML_SetCharacterDataHandler(parser, characterData);
519 l = fread(buf, 1, sizeof(buf), fp);
520 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
522 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));
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);