testcase: support "haiku" disttype
[platform/upstream/libsolv.git] / ext / repo_appdata.c
1 /*
2  * repo_appdatadb.c
3  *
4  * Parses AppSteam Data files.
5  * See http://people.freedesktop.org/~hughsient/appdata/
6  *
7  *
8  * Copyright (c) 2013, Novell Inc.
9  *
10  * This program is licensed under the BSD license, read LICENSE.BSD
11  * for further information
12  */
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #include <dirent.h>
25 #include <expat.h>
26 #include <errno.h>
27
28 #include "pool.h"
29 #include "repo.h"
30 #include "util.h"
31 #include "repo_appdata.h"
32
33
34 enum state {
35   STATE_START,
36   STATE_APPLICATION,
37   STATE_ID,
38   STATE_LICENCE,
39   STATE_NAME,
40   STATE_SUMMARY,
41   STATE_DESCRIPTION,
42   STATE_P,
43   STATE_UL,
44   STATE_UL_LI,
45   STATE_OL,
46   STATE_OL_LI,
47   STATE_URL,
48   STATE_GROUP,
49   NUMSTATES
50 };
51
52 struct stateswitch {
53   enum state from;
54   char *ename;
55   enum state to;
56   int docontent;
57 };
58
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 },
75   { NUMSTATES }
76 };
77
78 struct parsedata {
79   int depth;
80   enum state state;
81   int statedepth;
82   char *content;
83   int lcontent;
84   int acontent;
85   int docontent;
86   Pool *pool;
87   Repo *repo;
88   Repodata *data;
89
90   struct stateswitch *swtab[NUMSTATES];
91   enum state sbtab[NUMSTATES];
92
93   Solvable *solvable;
94   Id handle;
95
96   char *description;
97   int licnt;
98 };
99
100
101 static inline const char *
102 find_attr(const char *txt, const char **atts)
103 {
104   for (; *atts; atts += 2)
105     if (!strcmp(*atts, txt))
106       return atts[1];
107   return 0;
108 }
109
110
111 static void XMLCALL
112 startElement(void *userData, const char *name, const char **atts)
113 {
114   struct parsedata *pd = userData;
115   Pool *pool = pd->pool;
116   Solvable *s = pd->solvable;
117   struct stateswitch *sw;
118
119 #if 0
120   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
121 #endif
122   if (pd->depth != pd->statedepth)
123     {
124       pd->depth++;
125       return;
126     }
127
128   pd->depth++;
129   if (!pd->swtab[pd->state])    /* no statetable -> no substates */
130     {
131 #if 0
132       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
133 #endif
134       return;
135     }
136   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
137     if (!strcmp(sw->ename, name))
138       break;
139
140   if (sw->from != pd->state)
141     {
142 #if 0
143       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
144 #endif
145       return;
146     }
147   pd->state = sw->to;
148   pd->docontent = sw->docontent;
149   pd->statedepth = pd->depth;
150   pd->lcontent = 0;
151   *pd->content = 0;
152
153   switch(pd->state)
154     {
155     case STATE_APPLICATION:
156       s = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
157       pd->handle = s - pool->solvables;
158       break;
159     case STATE_DESCRIPTION:
160       pd->description = solv_free(pd->description);
161       break;
162     case STATE_OL:
163       pd->licnt = 0;
164       break;
165     default:
166       break;
167     }
168 }
169
170 /* replace whitespace with one space/newline */
171 /* also strip starting/ending whitespace */
172 static void
173 wsstrip(struct parsedata *pd)
174 {
175   int i, j;
176   int ws = 0;
177   for (i = j = 0; pd->content[i]; i++)
178     {
179       if (pd->content[i] == ' ' || pd->content[i] == '\t' || pd->content[i] == '\n')
180         {
181           ws |= pd->content[i] == '\n' ? 2 : 1;
182           continue;
183         }
184       if (ws && j)
185         pd->content[j++] = (ws & 2) ? '\n' : ' ';
186       ws = 0;
187       pd->content[j++] = pd->content[i];
188     }
189   pd->content[j] = 0;
190   pd->lcontent = j;
191 }
192
193 /* indent all lines */
194 static void
195 indent(struct parsedata *pd, int il)
196 {
197   int i, l;
198   for (l = 0; pd->content[l]; )
199     {
200       if (pd->content[l] == '\n')
201         {
202           l++;
203           continue;
204         }
205       if (pd->lcontent + il + 1 > pd->acontent)
206         {
207           pd->acontent = pd->lcontent + il + 256;
208           pd->content = realloc(pd->content, pd->acontent);
209         }
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] = ' ';
213       pd->lcontent += il;
214       while (pd->content[l] && pd->content[l] != '\n')
215         l++;
216     }
217 }
218
219 static void XMLCALL
220 endElement(void *userData, const char *name)
221 {
222   struct parsedata *pd = userData;
223   Pool *pool = pd->pool;
224   Solvable *s = pd->solvable;
225   Id id;
226
227 #if 0
228   fprintf(stderr, "end: [%d]%s\n", pd->state, name);
229 #endif
230   if (pd->depth != pd->statedepth)
231     {
232       pd->depth--;
233 #if 0
234       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
235 #endif
236       return;
237     }
238
239   pd->depth--;
240   pd->statedepth--;
241
242   switch (pd->state)
243     {
244     case STATE_APPLICATION:
245       if (!s->arch)
246         s->arch = ARCH_NOARCH;
247       if (!s->evr)
248         s->evr = ID_EMPTY;
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);
251       pd->solvable = 0;
252       break;
253     case STATE_ID:
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);
260       break;
261     case STATE_NAME:
262       s->name = pool_str2id(pd->pool, pool_tmpjoin(pool, "application:", pd->content, 0), 1);
263       break;
264     case STATE_LICENCE:
265       repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_LICENSE, pd->content);
266       break;
267     case STATE_SUMMARY:
268       repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, pd->content);
269       break;
270     case STATE_URL:
271       repodata_set_str(pd->data, pd->handle, SOLVABLE_URL, pd->content);
272       break;
273     case STATE_GROUP:
274       repodata_add_poolstr_array(pd->data, pd->handle, SOLVABLE_GROUP, pd->content);
275       break;
276     case STATE_DESCRIPTION:
277       if (pd->description)
278         {
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);
284         }
285       break;
286     case STATE_P:
287       wsstrip(pd);
288       pd->description = solv_dupappend(pd->description, pd->content, "\n\n");
289       break;
290     case STATE_UL_LI:
291       wsstrip(pd);
292       indent(pd, 4);
293       pd->content[2] = '-';
294       pd->description = solv_dupappend(pd->description, pd->content, "\n");
295       break;
296     case STATE_OL_LI:
297       wsstrip(pd);
298       indent(pd, 4);
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");
304       break;
305     case STATE_UL:
306     case STATE_OL:
307       pd->description = solv_dupappend(pd->description, "\n", 0);
308       break;
309     default:
310       break;
311     }
312
313   pd->state = pd->sbtab[pd->state];
314   pd->docontent = 0;
315
316 #if 0
317   fprintf(stderr, "end: [%s] -> %d\n", name, pd->state);
318 #endif
319 }
320
321
322 static void XMLCALL
323 characterData(void *userData, const XML_Char *s, int len)
324 {
325   struct parsedata *pd = userData;
326   int l;
327   char *c;
328   if (!pd->docontent)
329     return;
330   l = pd->lcontent + len + 1;
331   if (l > pd->acontent)
332     {
333       pd->acontent = l + 256;
334       pd->content = realloc(pd->content, pd->acontent);
335     }
336   c = pd->content + pd->lcontent;
337   pd->lcontent += len;
338   while (len-- > 0)
339     *c++ = *s++;
340   *c = 0;
341 }
342
343 #define BUFF_SIZE 8192
344
345 int
346 repo_add_appdata(Repo *repo, FILE *fp, int flags)
347 {
348   Pool *pool = repo->pool;
349   struct parsedata pd;
350   struct stateswitch *sw;
351   Repodata *data;
352   char buf[BUFF_SIZE];
353   int i, l;
354   int ret = 0;
355
356   data = repo_add_repodata(repo, flags);
357   memset(&pd, 0, sizeof(pd));
358   pd.repo = repo;
359   pd.pool = repo->pool;
360   pd.data = data;
361
362   pd.content = malloc(256);
363   pd.acontent = 256;
364
365   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
366     {
367       if (!pd.swtab[sw->from])
368         pd.swtab[sw->from] = sw;
369       pd.sbtab[sw->to] = sw->from;
370     }
371
372   XML_Parser parser = XML_ParserCreate(NULL);
373   XML_SetUserData(parser, &pd);
374   XML_SetElementHandler(parser, startElement, endElement);
375   XML_SetCharacterDataHandler(parser, characterData);
376
377   for (;;)
378     {
379       l = fread(buf, 1, sizeof(buf), fp);
380       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
381         {
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));
383           ret = -1;
384           break;
385         }
386       if (l == 0)
387         break;
388     }
389   XML_ParserFree(parser);
390
391   if (!(flags & REPO_NO_INTERNALIZE))
392     repodata_internalize(data);
393
394   free(pd.content);
395   return ret;
396 }
397
398 /* add all files ending in .appdata.xml */
399 int
400 repo_add_appdata_dir(Repo *repo, const char *appdatadir, int flags)
401 {
402   DIR *dir;
403   char *dirpath;
404   Repodata *data;
405
406   data = repo_add_repodata(repo, flags);
407   if (flags & REPO_USE_ROOTDIR)
408     dirpath = pool_prepend_rootdir(repo->pool, appdatadir);
409   else
410     dirpath = solv_strdup(appdatadir);
411   if ((dir = opendir(dirpath)) != 0)
412     {
413       struct dirent *entry;
414       while ((entry = readdir(dir)))
415         {
416           const char *n;
417           FILE *fp;
418           int len = strlen(entry->d_name);
419           if (len <= 12 || strcmp(entry->d_name + len - 12, ".appdata.xml") != 0)
420             continue;
421           if (entry->d_name[0] == '.')
422             continue;
423           n = pool_tmpjoin(repo->pool, dirpath, "/", entry->d_name);
424           fp = fopen(n, "r");
425           if (!fp)
426             {
427               pool_error(repo->pool, 0, "%s: %s", n, strerror(errno));
428               continue;
429             }
430           repo_add_appdata(repo, fp, flags | REPO_NO_INTERNALIZE | REPO_REUSE_REPODATA);
431           fclose(fp);
432         }
433     }
434   if (!(flags & REPO_NO_INTERNALIZE))
435     repodata_internalize(data);
436   return 0;
437 }