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, "application", STATE_APPLICATION, 0 },
66 { STATE_APPLICATION, "id", STATE_ID, 1 },
67 { STATE_APPLICATION, "pkgname", STATE_PKGNAME, 1 },
68 { STATE_APPLICATION, "product_license", STATE_LICENCE, 1 },
69 { STATE_APPLICATION, "name", STATE_NAME, 1 },
70 { STATE_APPLICATION, "summary", STATE_SUMMARY, 1 },
71 { STATE_APPLICATION, "description", STATE_DESCRIPTION, 0 },
72 { STATE_APPLICATION, "url", STATE_URL, 1 },
73 { STATE_APPLICATION, "project_group", STATE_GROUP, 1 },
74 { STATE_APPLICATION, "keywords", STATE_KEYWORDS, 0 },
75 { STATE_DESCRIPTION, "p", STATE_P, 1 },
76 { STATE_DESCRIPTION, "ul", STATE_UL, 0 },
77 { STATE_DESCRIPTION, "ol", STATE_OL, 0 },
78 { STATE_UL, "li", STATE_UL_LI, 1 },
79 { STATE_OL, "li", STATE_OL_LI, 1 },
80 { STATE_KEYWORDS, "keyword", STATE_KEYWORD, 1 },
96 struct stateswitch *swtab[NUMSTATES];
97 enum state sbtab[NUMSTATES];
111 static inline const char *
112 find_attr(const char *txt, const char **atts)
114 for (; *atts; atts += 2)
115 if (!strcmp(*atts, txt))
122 startElement(void *userData, const char *name, const char **atts)
124 struct parsedata *pd = userData;
125 Pool *pool = pd->pool;
126 Solvable *s = pd->solvable;
127 struct stateswitch *sw;
130 fprintf(stderr, "start: [%d]%s\n", pd->state, name);
132 if (pd->depth != pd->statedepth)
139 if (!pd->swtab[pd->state]) /* no statetable -> no substates */
142 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
146 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
147 if (!strcmp(sw->ename, name))
150 if (sw->from != pd->state)
153 fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
158 pd->docontent = sw->docontent;
159 pd->statedepth = pd->depth;
163 if (!pd->skip_depth && find_attr("xml:lang", atts))
164 pd->skip_depth = pd->depth;
173 case STATE_APPLICATION:
174 s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
175 pd->handle = s - pool->solvables;
178 case STATE_DESCRIPTION:
179 pd->description = solv_free(pd->description);
190 /* replace whitespace with one space/newline */
191 /* also strip starting/ending whitespace */
193 wsstrip(struct parsedata *pd)
197 for (i = j = 0; pd->content[i]; i++)
199 if (pd->content[i] == ' ' || pd->content[i] == '\t' || pd->content[i] == '\n')
201 ws |= pd->content[i] == '\n' ? 2 : 1;
205 pd->content[j++] = (ws & 2) ? '\n' : ' ';
207 pd->content[j++] = pd->content[i];
213 /* indent all lines */
215 indent(struct parsedata *pd, int il)
218 for (l = 0; pd->content[l]; )
220 if (pd->content[l] == '\n')
225 if (pd->lcontent + il + 1 > pd->acontent)
227 pd->acontent = pd->lcontent + il + 256;
228 pd->content = realloc(pd->content, pd->acontent);
230 memmove(pd->content + l + il, pd->content + l, pd->lcontent - l + 1);
231 for (i = 0; i < il; i++)
232 pd->content[l + i] = ' ';
234 while (pd->content[l] && pd->content[l] != '\n')
240 add_missing_tags_from_desktop_file(struct parsedata *pd, Solvable *s, const char *desktop_file)
242 Pool *pool = pd->pool;
244 const char *filepath;
249 filepath = pool_tmpjoin(pool, "/usr/share/applications/", desktop_file, 0);
250 if (pd->flags & REPO_USE_ROOTDIR)
251 filepath = pool_prepend_rootdir_tmp(pool, filepath);
252 if (!(fp = fopen(filepath, "r")))
254 while (fgets(buf, sizeof(buf), fp) > 0)
256 int c, l = strlen(buf);
259 if (buf[l - 1] != '\n')
261 /* ignore overlong lines */
262 while ((c = getc(fp)) != EOF)
270 while (l && (buf[l - 1] == ' ' || buf[l - 1] == '\t'))
273 while (*p == ' ' || *p == '\t')
275 if (!*p || *p == '#')
279 if (!strcmp(p, "[Desktop Entry]"))
290 for (p3 = p2 - 1; *p3 == ' ' || *p3 == '\t'; p3--)
293 while (*p2 == ' ' || *p2 == '\t')
297 if (!s->name && !strcmp(p, "Name"))
298 s->name = pool_str2id(pool, pool_tmpjoin(pool, "application:", p2, 0), 1);
299 else if (!pd->havesummary && !strcmp(p, "Comment"))
302 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, p2);
306 if (s->name && pd->havesummary)
307 break; /* our work is done */
313 endElement(void *userData, const char *name)
315 struct parsedata *pd = userData;
316 Pool *pool = pd->pool;
317 Solvable *s = pd->solvable;
321 fprintf(stderr, "end: [%d]%s\n", pd->state, name);
323 if (pd->depth != pd->statedepth)
327 fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
335 if (pd->skip_depth && pd->depth + 1 >= pd->skip_depth)
337 if (pd->depth + 1 == pd->skip_depth)
339 pd->state = pd->sbtab[pd->state];
347 case STATE_APPLICATION:
349 s->arch = ARCH_NOARCH;
352 if ((!s->name || !pd->havesummary) && (pd->flags & APPDATA_CHECK_DESKTOP_FILE) != 0 && pd->desktop_file)
353 add_missing_tags_from_desktop_file(pd, s, pd->desktop_file);
354 if (!s->name && pd->desktop_file)
356 char *name = pool_tmpjoin(pool, "application:", pd->desktop_file, 0);
357 int l = strlen(name);
358 if (l > 8 && !strcmp(".desktop", name + l - 8))
360 s->name = pool_strn2id(pool, name, l, 1);
362 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
363 s->provides = repo_addid_dep(pd->repo, s->provides, pool_rel2id(pd->pool, s->name, s->evr, REL_EQ, 1), 0);
365 pd->desktop_file = solv_free(pd->desktop_file);
368 pd->desktop_file = solv_strdup(pd->content);
369 /* guess the appdata.xml file name from the id element */
370 if (pd->lcontent > 8 && !strcmp(".desktop", pd->content + pd->lcontent - 8))
371 pd->content[pd->lcontent - 8] = 0;
372 else if (pd->lcontent > 4 && !strcmp(".ttf", pd->content + pd->lcontent - 4))
373 pd->content[pd->lcontent - 4] = 0;
374 else if (pd->lcontent > 4 && !strcmp(".otf", pd->content + pd->lcontent - 4))
375 pd->content[pd->lcontent - 4] = 0;
376 else if (pd->lcontent > 4 && !strcmp(".xml", pd->content + pd->lcontent - 4))
377 pd->content[pd->lcontent - 4] = 0;
378 else if (pd->lcontent > 3 && !strcmp(".db", pd->content + pd->lcontent - 3))
379 pd->content[pd->lcontent - 3] = 0;
380 id = pool_str2id(pd->pool, pool_tmpjoin(pool, "appdata(", pd->content, ".appdata.xml)"), 1);
381 s->requires = repo_addid_dep(pd->repo, s->requires, id, 0);
382 id = pool_str2id(pd->pool, pool_tmpjoin(pool, "application-appdata(", pd->content, ".appdata.xml)"), 1);
383 s->provides = repo_addid_dep(pd->repo, s->provides, id, 0);
386 s->name = pool_str2id(pd->pool, pool_tmpjoin(pool, "application:", pd->content, 0), 1);
389 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_LICENSE, pd->content);
393 repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, pd->content);
396 repodata_set_str(pd->data, pd->handle, SOLVABLE_URL, pd->content);
399 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_GROUP, pd->content);
401 case STATE_DESCRIPTION:
404 /* strip trailing newlines */
405 int l = strlen(pd->description);
406 while (l && pd->description[l - 1] == '\n')
407 pd->description[--l] = 0;
408 repodata_set_str(pd->data, pd->handle, SOLVABLE_DESCRIPTION, pd->description);
413 pd->description = solv_dupappend(pd->description, pd->content, "\n\n");
418 pd->content[2] = '-';
419 pd->description = solv_dupappend(pd->description, pd->content, "\n");
424 if (++pd->licnt >= 10)
425 pd->content[0] = '0' + (pd->licnt / 10) % 10;
426 pd->content[1] = '0' + pd->licnt % 10;
427 pd->content[2] = '.';
428 pd->description = solv_dupappend(pd->description, pd->content, "\n");
432 pd->description = solv_dupappend(pd->description, "\n", 0);
435 id = pool_str2id(pd->pool, pd->content, 1);
436 s->requires = repo_addid_dep(pd->repo, s->requires, id, 0);
438 repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_KEYWORDS, pd->content);
444 pd->state = pd->sbtab[pd->state];
448 fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
454 characterData(void *userData, const XML_Char *s, int len)
456 struct parsedata *pd = userData;
461 l = pd->lcontent + len + 1;
462 if (l > pd->acontent)
464 pd->acontent = l + 256;
465 pd->content = realloc(pd->content, pd->acontent);
467 c = pd->content + pd->lcontent;
474 #define BUFF_SIZE 8192
477 repo_add_appdata(Repo *repo, FILE *fp, int flags)
479 Pool *pool = repo->pool;
481 struct stateswitch *sw;
487 data = repo_add_repodata(repo, flags);
488 memset(&pd, 0, sizeof(pd));
490 pd.pool = repo->pool;
494 pd.content = malloc(256);
497 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
499 if (!pd.swtab[sw->from])
500 pd.swtab[sw->from] = sw;
501 pd.sbtab[sw->to] = sw->from;
504 XML_Parser parser = XML_ParserCreate(NULL);
505 XML_SetUserData(parser, &pd);
506 XML_SetElementHandler(parser, startElement, endElement);
507 XML_SetCharacterDataHandler(parser, characterData);
511 l = fread(buf, 1, sizeof(buf), fp);
512 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
514 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));
517 repo_free_solvable(repo, pd.solvable - pd.pool->solvables, 1);
526 XML_ParserFree(parser);
528 if (!(flags & REPO_NO_INTERNALIZE))
529 repodata_internalize(data);
531 solv_free(pd.content);
532 solv_free(pd.desktop_file);
533 solv_free(pd.description);
537 /* add all files ending in .appdata.xml */
539 repo_add_appdata_dir(Repo *repo, const char *appdatadir, int flags)
545 data = repo_add_repodata(repo, flags);
546 if (flags & REPO_USE_ROOTDIR)
547 dirpath = pool_prepend_rootdir(repo->pool, appdatadir);
549 dirpath = solv_strdup(appdatadir);
550 if ((dir = opendir(dirpath)) != 0)
552 struct dirent *entry;
553 while ((entry = readdir(dir)))
557 int len = strlen(entry->d_name);
558 if (len <= 12 || strcmp(entry->d_name + len - 12, ".appdata.xml") != 0)
560 if (entry->d_name[0] == '.')
562 n = pool_tmpjoin(repo->pool, dirpath, "/", entry->d_name);
566 pool_error(repo->pool, 0, "%s: %s", n, strerror(errno));
569 repo_add_appdata(repo, fp, flags | REPO_NO_INTERNALIZE | REPO_REUSE_REPODATA | APPDATA_CHECK_DESKTOP_FILE);
575 if (!(flags & REPO_NO_INTERNALIZE))
576 repodata_internalize(data);