- cleanup repo handlin API
[platform/upstream/libsolv.git] / tools / repo_helix.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * repo_helix.c
10  * 
11  * Parse 'helix' XML representation
12  * and create 'repo'
13  * 
14  */
15
16 #include <sys/types.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <expat.h>
23
24 #include "repo_helix.h"
25 #include "evr.h"
26
27
28 /* XML parser states */
29
30 enum state {
31   STATE_START,
32   STATE_CHANNEL,
33   STATE_SUBCHANNEL,
34   STATE_PACKAGE,
35   STATE_NAME,
36   STATE_VENDOR,
37   STATE_HISTORY,
38   STATE_UPDATE,
39   STATE_EPOCH,
40   STATE_VERSION,
41   STATE_RELEASE,
42   STATE_ARCH,
43   STATE_PROVIDES,
44   STATE_PROVIDESENTRY,
45   STATE_REQUIRES,
46   STATE_REQUIRESENTRY,
47   STATE_OBSOLETES,
48   STATE_OBSOLETESENTRY,
49   STATE_CONFLICTS,
50   STATE_CONFLICTSENTRY,
51   STATE_RECOMMENDS,
52   STATE_RECOMMENDSENTRY,
53   STATE_SUPPLEMENTS,
54   STATE_SUPPLEMENTSENTRY,
55   STATE_SUGGESTS,
56   STATE_SUGGESTSENTRY,
57   STATE_ENHANCES,
58   STATE_ENHANCESENTRY,
59   STATE_FRESHENS,
60   STATE_FRESHENSENTRY,
61
62   STATE_SELECTTION,
63   STATE_PATTERN,
64   STATE_ATOM,
65   STATE_PATCH,
66   STATE_PRODUCT,
67
68   STATE_PEPOCH,
69   STATE_PVERSION,
70   STATE_PRELEASE,
71   STATE_PARCH,
72
73   NUMSTATES
74 };
75
76 #define PACK_BLOCK 255
77
78 struct stateswitch {
79   enum state from;
80   char *ename;
81   enum state to;
82   int docontent;
83 };
84
85 static struct stateswitch stateswitches[] = {
86   { STATE_START,       "channel",         STATE_CHANNEL, 0 },
87   { STATE_CHANNEL,     "subchannel",      STATE_SUBCHANNEL, 0 },
88   { STATE_SUBCHANNEL,  "package",         STATE_PACKAGE, 0 },
89   { STATE_SUBCHANNEL,  "selection",       STATE_PACKAGE, 0 },
90   { STATE_SUBCHANNEL,  "pattern",         STATE_PACKAGE, 0 },
91   { STATE_SUBCHANNEL,  "atom",            STATE_PACKAGE, 0 },
92   { STATE_SUBCHANNEL,  "patch",           STATE_PACKAGE, 0 },
93   { STATE_SUBCHANNEL,  "product",         STATE_PACKAGE, 0 },
94   { STATE_PACKAGE,     "name",            STATE_NAME, 1 },
95   { STATE_PACKAGE,     "vendor",          STATE_VENDOR, 1 },
96   { STATE_PACKAGE,     "epoch",           STATE_PEPOCH, 1 },
97   { STATE_PACKAGE,     "version",         STATE_PVERSION, 1 },
98   { STATE_PACKAGE,     "release",         STATE_PRELEASE, 1 },
99   { STATE_PACKAGE,     "arch",            STATE_PARCH, 1 },
100   { STATE_PACKAGE,     "history",         STATE_HISTORY, 0 },
101   { STATE_PACKAGE,     "provides",        STATE_PROVIDES, 0 },
102   { STATE_PACKAGE,     "requires",        STATE_REQUIRES, 0 },
103   { STATE_PACKAGE,     "obsoletes",       STATE_OBSOLETES , 0 },
104   { STATE_PACKAGE,     "conflicts",       STATE_CONFLICTS , 0 },
105   { STATE_PACKAGE,     "recommends" ,     STATE_RECOMMENDS , 0 },
106   { STATE_PACKAGE,     "supplements",     STATE_SUPPLEMENTS, 0 },
107   { STATE_PACKAGE,     "suggests",        STATE_SUGGESTS, 0 },
108   { STATE_PACKAGE,     "enhances",        STATE_ENHANCES, 0 },
109   { STATE_PACKAGE,     "freshens",        STATE_FRESHENS, 0 },
110
111   { STATE_HISTORY,     "update",          STATE_UPDATE, 0 },
112   { STATE_UPDATE,      "epoch",           STATE_EPOCH, 1 },
113   { STATE_UPDATE,      "version",         STATE_VERSION, 1 },
114   { STATE_UPDATE,      "release",         STATE_RELEASE, 1 },
115   { STATE_UPDATE,      "arch",            STATE_ARCH, 1 },
116
117   { STATE_PROVIDES,    "dep",             STATE_PROVIDESENTRY, 0 },
118   { STATE_REQUIRES,    "dep",             STATE_REQUIRESENTRY, 0 },
119   { STATE_OBSOLETES,   "dep",             STATE_OBSOLETESENTRY, 0 },
120   { STATE_CONFLICTS,   "dep",             STATE_CONFLICTSENTRY, 0 },
121   { STATE_RECOMMENDS,  "dep",             STATE_RECOMMENDSENTRY, 0 },
122   { STATE_SUPPLEMENTS, "dep",             STATE_SUPPLEMENTSENTRY, 0 },
123   { STATE_SUGGESTS,    "dep",             STATE_SUGGESTSENTRY, 0 },
124   { STATE_ENHANCES,    "dep",             STATE_ENHANCESENTRY, 0 },
125   { STATE_FRESHENS,    "dep",             STATE_FRESHENSENTRY, 0 },
126   { NUMSTATES }
127
128 };
129
130 /*
131  * parser data
132  */
133
134 typedef struct _parsedata {
135   // XML parser data
136   int depth;
137   enum state state;     // current state
138   int statedepth;
139   char *content;        // buffer for content of node
140   int lcontent;         // actual length of current content
141   int acontent;         // actual buffer size
142   int docontent;        // handle content
143
144   // repo data
145   int pack;             // number of solvables
146
147   Pool *pool;           // current pool
148   Repo *repo;   // current repo
149   Solvable *start;      // collected solvables
150
151   // package data
152   int  epoch;           // epoch (as offset into evrspace)
153   int  version;         // version (as offset into evrspace)
154   int  release;         // release (as offset into evrspace)
155   char *evrspace;       // buffer for evr
156   int  aevrspace;       // actual buffer space
157   int  levrspace;       // actual evr length
158   char *kind;
159
160   struct stateswitch *swtab[NUMSTATES];
161   enum state sbtab[NUMSTATES];
162 } Parsedata;
163
164
165 /*------------------------------------------------------------------*/
166 /* E:V-R handling */
167
168 // create Id from epoch:version-release
169
170 static Id
171 evr2id(Pool *pool, Parsedata *pd, const char *e, const char *v, const char *r)
172 {
173   char *c;
174   int l;
175
176   // treat explitcit 0 as NULL
177   if (e && !strcmp(e, "0"))
178     e = NULL;
179
180   if (v && !e)
181     {
182       const char *v2;
183       // scan version for ":"
184       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)      // skip leading digits
185         ;
186       // if version contains ":", set epoch to "0"
187       if (v2 > v && *v2 == ':')
188         e = "0";
189     }
190   
191   // compute length of Id string
192   l = 1;  // for the \0
193   if (e)
194     l += strlen(e) + 1;  // e:
195   if (v)
196     l += strlen(v);      // v
197   if (r)
198     l += strlen(r) + 1;  // -r
199
200   // extend content if not sufficient
201   if (l > pd->acontent)
202     {
203       pd->content = (char *)realloc(pd->content, l + 256);
204       pd->acontent = l + 256;
205     }
206
207   // copy e-v-r to content
208   c = pd->content;
209   if (e)
210     {
211       strcpy(c, e);
212       c += strlen(c);
213       *c++ = ':';
214     }
215   if (v)
216     {
217       strcpy(c, v);
218       c += strlen(c);
219     }
220   if (r)
221     {
222       *c++ = '-';
223       strcpy(c, r);
224       c += strlen(c);
225     }
226   *c = 0;
227   // if nothing inserted, return Id 0
228   if (!*pd->content)
229     return ID_NULL;
230 #if 0
231   fprintf(stderr, "evr: %s\n", pd->content);
232 #endif
233   // intern and create
234   return str2id(pool, pd->content, 1);
235 }
236
237
238 // create e:v-r from attributes
239 // atts is array of name,value pairs, NULL at end
240 //   even index into atts is name
241 //   odd index is value
242 //
243 static Id
244 evr_atts2id(Pool *pool, Parsedata *pd, const char **atts)
245 {
246   const char *e, *v, *r;
247   e = v = r = 0;
248   for (; *atts; atts += 2)
249     {
250       if (!strcmp(*atts, "epoch"))
251         e = atts[1];
252       else if (!strcmp(*atts, "version"))
253         v = atts[1];
254       else if (!strcmp(*atts, "release"))
255         r = atts[1];
256     }
257   return evr2id(pool, pd, e, v, r);
258 }
259
260 /*------------------------------------------------------------------*/
261 /* rel operator handling */
262
263 struct flagtab {
264   char *from;
265   int to;
266 };
267
268 static struct flagtab flagtab[] = {
269   { ">",  REL_GT },
270   { "=",  REL_EQ },
271   { ">=", REL_GT|REL_EQ },
272   { "<",  REL_LT },
273   { "!=", REL_GT|REL_LT },
274   { "<=", REL_LT|REL_EQ },
275   { "(any)", REL_LT|REL_EQ|REL_GT },
276   { "==", REL_EQ },
277   { "gt", REL_GT },
278   { "eq", REL_EQ },
279   { "ge", REL_GT|REL_EQ },
280   { "lt", REL_LT },
281   { "ne", REL_GT|REL_LT },
282   { "le", REL_LT|REL_EQ },
283   { "gte", REL_GT|REL_EQ },
284   { "lte", REL_LT|REL_EQ },
285   { "GT", REL_GT },
286   { "EQ", REL_EQ },
287   { "GE", REL_GT|REL_EQ },
288   { "LT", REL_LT },
289   { "NE", REL_GT|REL_LT },
290   { "LE", REL_LT|REL_EQ }
291 };
292
293 /*
294  * process new dependency from parser
295  *  olddeps = already collected deps, this defines the 'kind' of dep
296  *  atts = array of name,value attributes of dep
297  *  isreq == 1 if its a requires
298  */
299
300 static unsigned int
301 adddep(Pool *pool, Parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
302 {
303   Id id, name;
304   const char *n, *f, *k;
305   const char **a;
306
307   n = f = k = NULL;
308
309   /* loop over name,value pairs */
310   for (a = atts; *a; a += 2)
311     {
312       if (!strcmp(*a, "name"))
313         n = a[1];
314       if (!strcmp(*a, "kind"))
315         k = a[1];
316       else if (!strcmp(*a, "op"))
317         f = a[1];
318       else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
319         isreq = 2;
320     }
321   if (!n)                              /* quit if no name found */
322     return olddeps;
323
324   /* kind, name */
325   if (k && !strcmp(k, "package"))
326     k = NULL;                          /* package is default */
327
328   if (k)                               /* if kind!=package, intern <kind>:<name> */
329     {
330       int l = strlen(k) + 1 + strlen(n) + 1;
331       if (l > pd->acontent)            /* extend buffer if needed */
332         {
333           pd->content = (char *)realloc(pd->content, l + 256);
334           pd->acontent = l + 256;
335         }
336       sprintf(pd->content, "%s:%s", k, n);
337       name = str2id(pool, pd->content, 1);
338     }
339   else {
340       name = str2id(pool, n, 1);       /* package: just intern <name> */
341   }
342
343   if (f)                               /* operator ? */
344     {
345       /* intern e:v-r */
346       Id evr = evr_atts2id(pool, pd, atts);
347       /* parser operator to flags */
348       int flags;
349       for (flags = 0; flags < sizeof(flagtab)/sizeof(*flagtab); flags++)
350         if (!strcmp(f, flagtab[flags].from))
351           {
352             flags = flagtab[flags].to;
353             break;
354           }
355       if (flags > 7)
356         flags = 0;
357       /* intern rel */
358       id = rel2id(pool, name, evr, flags, 1);
359     }
360   else
361     id = name;                         /* no operator */
362
363   /* add new dependency to repo */
364   return repo_addid_dep(pd->repo, olddeps, id, isreq);
365 }
366
367
368 /*----------------------------------------------------------------*/
369
370 /*
371  * XML callback
372  * <name>
373  * 
374  */
375
376 static void XMLCALL
377 startElement(void *userData, const char *name, const char **atts)
378 {
379   Parsedata *pd = (Parsedata *)userData;
380   struct stateswitch *sw;
381   Pool *pool = pd->pool;
382   Solvable *s = pd->start ? pd->start + pd->pack : 0;
383
384   if (pd->depth != pd->statedepth)
385     {
386       pd->depth++;
387       return;
388     }
389
390   /* ignore deps element */
391   if (pd->state == STATE_PACKAGE && !strcmp(name, "deps"))
392     return;
393
394   pd->depth++;
395
396   /* find node name in stateswitch */
397   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
398   {
399     if (!strcmp(sw->ename, name))
400       break;
401   }
402
403   /* check if we're at the right level */
404   if (sw->from != pd->state)
405     {
406 #if 0
407       fprintf(stderr, "into unknown: %s\n", name);
408 #endif
409       return;
410     }
411   
412   // set new state
413   pd->state = sw->to;
414
415   pd->docontent = sw->docontent;
416   pd->statedepth = pd->depth;
417
418   // start with empty content
419   // (will collect data until end element
420   pd->lcontent = 0;
421   *pd->content = 0;
422
423   switch (pd->state)
424     {
425
426     case STATE_NAME:
427       if (pd->kind)                    /* if kind is set (non package) */
428         {
429           strcpy(pd->content, pd->kind);
430           pd->lcontent = strlen(pd->content);
431           pd->content[pd->lcontent++] = ':';   /* prefix name with '<kind>:' */
432           pd->content[pd->lcontent] = 0;
433         }
434       break;
435
436     case STATE_SUBCHANNEL:
437       pd->pack = 0;
438       break;
439
440
441     case STATE_PACKAGE:                /* solvable name */
442
443       if ((pd->pack & PACK_BLOCK) == 0)  /* alloc new block ? */
444         {
445           pool->solvables = (Solvable *)realloc(pool->solvables, (pool->nsolvables + pd->pack + PACK_BLOCK + 1) * sizeof(Solvable));
446           pd->start = pool->solvables + pool->nsolvables;
447           memset(pd->start + pd->pack, 0, (PACK_BLOCK + 1) * sizeof(Solvable));
448         }
449
450       if (!strcmp(name, "selection"))
451         pd->kind = "selection";
452       else if (!strcmp(name, "pattern"))
453         pd->kind = "pattern";
454       else if (!strcmp(name, "atom"))
455         pd->kind = "atom";
456       else if (!strcmp(name, "product"))
457         pd->kind = "product";
458       else if (!strcmp(name, "patch"))
459         pd->kind = "patch";
460       else
461         pd->kind = NULL;               /* default is package */
462       pd->levrspace = 1;
463       pd->epoch = 0;
464       pd->version = 0;
465       pd->release = 0;
466 #if 0
467       fprintf(stderr, "package #%d\n", pd->pack);
468 #endif
469       break;
470
471     case STATE_UPDATE:
472       pd->levrspace = 1;
473       pd->epoch = 0;
474       pd->version = 0;
475       pd->release = 0;
476       break;
477
478     case STATE_PROVIDES:               /* start of provides */
479       s->provides = 0;
480       break;
481     case STATE_PROVIDESENTRY:          /* entry within provides */
482       s->provides = adddep(pool, pd, s->provides, atts, 0);
483       break;
484     case STATE_REQUIRES:
485       s->requires = 0;
486       break;
487     case STATE_REQUIRESENTRY:
488       s->requires = adddep(pool, pd, s->requires, atts, 1);
489       break;
490     case STATE_OBSOLETES:
491       s->obsoletes = 0;
492       break;
493     case STATE_OBSOLETESENTRY:
494       s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
495       break;
496     case STATE_CONFLICTS:
497       s->conflicts = 0;
498       break;
499     case STATE_CONFLICTSENTRY:
500       s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
501       break;
502     case STATE_RECOMMENDS:
503       s->recommends = 0;
504       break;
505     case STATE_RECOMMENDSENTRY:
506       s->recommends = adddep(pool, pd, s->recommends, atts, 0);
507       break;
508     case STATE_SUPPLEMENTS:
509       s->supplements= 0;
510       break;
511     case STATE_SUPPLEMENTSENTRY:
512       s->supplements = adddep(pool, pd, s->supplements, atts, 0);
513       break;
514     case STATE_SUGGESTS:
515       s->suggests = 0;
516       break;
517     case STATE_SUGGESTSENTRY:
518       s->suggests = adddep(pool, pd, s->suggests, atts, 0);
519       break;
520     case STATE_ENHANCES:
521       s->enhances = 0;
522       break;
523     case STATE_ENHANCESENTRY:
524       s->enhances = adddep(pool, pd, s->enhances, atts, 0);
525       break;
526     case STATE_FRESHENS:
527       s->freshens = 0;
528       break;
529     case STATE_FRESHENSENTRY:
530       s->freshens = adddep(pool, pd, s->freshens, atts, 0);
531       break;
532     default:
533       break;
534     }
535 }
536
537 static const char* findKernelFlavor(Parsedata *pd, Solvable *s)
538 {
539   Pool *pool = pd->pool;
540
541   Id pid, *pidp = 0;
542   
543   if (s->provides)
544     {
545       for (pidp = pd->repo->idarraydata + s->provides; pidp && (pid = *pidp++) != 0; )
546         {
547           Reldep *prd;
548           const char *depname;
549           
550           if (!ISRELDEP(pid))
551             continue;               /* wrong provides name */
552           prd = GETRELDEP(pool, pid);
553           depname = id2str(pool, prd->name);
554           if (!strncmp(depname, "kernel-", strlen("kernel-")))
555             {
556               return depname + strlen("kernel-");
557             }
558         }
559     }
560
561   //fprintf(stderr, "pack %d\n", pd->pack);
562   //fprintf(stderr, "repo %d\n", s->requires);
563
564   if (!s->requires)
565     return 0;
566
567   for (pidp = pd->repo->idarraydata + s->requires ; pidp && (pid = *pidp++) != 0; )
568     {
569       const char *depname = 0;
570
571       if (!ISRELDEP(pid))
572         {
573           depname = id2str(pool, pid);
574         } 
575       else 
576         {
577           Reldep *prd = GETRELDEP(pool, pid);
578           depname = id2str(pool, prd->name);
579         }
580       if (!strncmp(depname, "kernel-", strlen("kernel-")))
581         {
582           return depname + strlen("kernel-");
583         }
584     }
585
586   return 0;
587 }
588
589
590 /*
591  * XML callback
592  * </name>
593  * 
594  * create Solvable from collected data
595  */
596
597 static void XMLCALL
598 endElement(void *userData, const char *name)
599 {
600   Parsedata *pd = (Parsedata *)userData;
601   Pool *pool = pd->pool;
602   Solvable *s = pd->start ? pd->start + pd->pack : NULL;
603   Id evr;
604
605   if (pd->depth != pd->statedepth)
606     {
607       pd->depth--;
608       // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
609       return;
610     }
611
612   /* ignore deps element */
613   if (pd->state == STATE_PACKAGE && !strcmp(name, "deps"))
614     return;
615
616   pd->depth--;
617   pd->statedepth--;
618   switch (pd->state)
619     {
620
621     case STATE_PACKAGE:                /* package complete */
622       s->repo = pd->repo;
623
624       if (!s->arch)                    /* default to "noarch" */
625         s->arch = ARCH_NOARCH;
626
627       if (!s->evr && pd->version)      /* set solvable evr */
628         s->evr = evr2id(pool, pd,
629                         pd->epoch   ? pd->evrspace + pd->epoch   : 0,
630                         pd->version ? pd->evrspace + pd->version : 0,
631                         pd->release ? pd->evrspace + pd->release : 0);
632       /* ensure self-provides */
633       if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
634         s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
635       s->supplements = repo_fix_legacy(pd->repo, s->provides, s->supplements);
636
637       const char *flavor = findKernelFlavor(pd, s);
638       if (flavor) 
639         {
640           char *cflavor = strdup(flavor);       /* make pointer safe */
641
642           Id npr;
643           Id pid;
644
645           /* this is either a kernel package or a kmp */
646           if (s->provides)
647             {
648               Offset prov = s->provides;
649               npr = 0;
650               while ((pid = pd->repo->idarraydata[prov++]) != 0)
651                 {
652                   const char *depname = 0;
653                   Reldep *prd = 0;
654
655                   if (ISRELDEP(pid))
656                     {
657                       prd = GETRELDEP(pool, pid);
658                       depname = id2str(pool, prd->name);
659                     }
660                   else
661                     {
662                       depname = id2str(pool, pid);
663                     }
664
665
666                   if (!strncmp(depname, "kernel(", strlen("kernel(")) && !strchr(depname, ':'))
667                     {
668                       char newdep[100];
669                       strcpy(newdep, "kernel(");
670                       strncat(newdep, cflavor, sizeof(newdep));
671                       strncat(newdep, ":", sizeof(newdep));
672                       strncat(newdep, depname + strlen("kernel("), 100);
673                       pid = str2id(pool, newdep, 1);
674                       if (prd)
675                         pid = rel2id(pool, pid, prd->evr, prd->flags, 1);
676                     }
677
678                   npr = repo_addid_dep(pd->repo, npr, pid, 0);
679                 }
680               s->provides = npr;
681             }
682 #if 1
683
684           if (s->requires)
685             {
686               Offset reqs = s->requires;
687               npr = 0;
688               while ((pid = pd->repo->idarraydata[reqs++]) != 0)
689                 {
690                   const char *depname = 0;
691                   Reldep *prd = 0;
692
693                   if (ISRELDEP(pid))
694                     {
695                       prd = GETRELDEP(pool, pid);
696                       depname = id2str(pool, prd->name);
697                     }
698                   else
699                     {
700                       depname = id2str(pool, pid);
701                     }
702
703                   if (!strncmp(depname, "kernel(", strlen("kernel(")) && !strchr(depname, ':'))
704                     {
705                       char newdep[100];
706                       strcpy(newdep, "kernel(");
707                       strncat(newdep, cflavor, sizeof(newdep));
708                       strncat(newdep, ":", sizeof(newdep));
709                       strncat(newdep, depname + strlen("kernel("), 100);
710                       pid = str2id(pool, newdep, 1);
711                       if (prd)
712                         pid = rel2id(pool, pid, prd->evr, prd->flags, 1);
713                     }
714                   npr = repo_addid_dep(pd->repo, npr, pid, 0);
715                 }
716               s->requires = npr;
717             }
718 #endif
719           free(cflavor);
720         }
721
722       pd->pack++;                      /* inc pack count */
723
724       break;
725     case STATE_NAME:
726       s->name = str2id(pool, pd->content, 1);
727       break;
728     case STATE_VENDOR:
729       s->vendor = str2id(pool, pd->content, 1);
730       break;
731     case STATE_UPDATE:                 /* new version, keeping all other metadata */
732       evr = evr2id(pool, pd,
733                    pd->epoch   ? pd->evrspace + pd->epoch   : 0,
734                    pd->version ? pd->evrspace + pd->version : 0,
735                    pd->release ? pd->evrspace + pd->release : 0);
736       pd->levrspace = 1;
737       pd->epoch = 0;
738       pd->version = 0;
739       pd->release = 0;
740       /* use highest evr */
741       if (!s->evr || evrcmp(pool, s->evr, evr) <= 0)
742         s->evr = evr;
743       break;
744     case STATE_EPOCH:
745     case STATE_VERSION:
746     case STATE_RELEASE:
747     case STATE_PEPOCH:
748     case STATE_PVERSION:
749     case STATE_PRELEASE:
750       /* ensure buffer space */
751       if (pd->lcontent + 1 + pd->levrspace > pd->aevrspace)
752         {
753           pd->evrspace = (char *)realloc(pd->evrspace, pd->lcontent + 1 + pd->levrspace + 256);
754           pd->aevrspace = pd->lcontent + 1 + pd->levrspace + 256;
755         }
756       memcpy(pd->evrspace + pd->levrspace, pd->content, pd->lcontent + 1);
757       if (pd->state == STATE_EPOCH || pd->state == STATE_PEPOCH)
758         pd->epoch = pd->levrspace;
759       else if (pd->state == STATE_VERSION || pd->state == STATE_PVERSION)
760         pd->version = pd->levrspace;
761       else
762         pd->release = pd->levrspace;
763       pd->levrspace += pd->lcontent + 1;
764       break;
765     case STATE_ARCH:
766     case STATE_PARCH:
767       s->arch = str2id(pool, pd->content, 1);
768       break;
769     default:
770       break;
771     }
772   pd->state = pd->sbtab[pd->state];
773   pd->docontent = 0;
774   // printf("back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
775 }
776
777
778 /*
779  * XML callback
780  * character data
781  * 
782  */
783
784 static void XMLCALL
785 characterData(void *userData, const XML_Char *s, int len)
786 {
787   Parsedata *pd = (Parsedata *)userData;
788   int l;
789   char *c;
790
791   // check if current nodes content is interesting
792   if (!pd->docontent)
793     return;
794
795   // adapt content buffer
796   l = pd->lcontent + len + 1;
797   if (l > pd->acontent)
798     {
799       pd->content = (char *)realloc(pd->content, l + 256);
800       pd->acontent = l + 256;
801     }
802   // append new content to buffer
803   c = pd->content + pd->lcontent;
804   pd->lcontent += len;
805   while (len-- > 0)
806     *c++ = *s++;
807   *c = 0;
808 }
809
810 /*-------------------------------------------------------------------*/
811
812 #define BUFF_SIZE 8192
813
814 /*
815  * read 'helix' type xml from fp
816  * add packages to pool/repo
817  * 
818  */
819
820 void
821 repo_add_helix(Repo *repo, FILE *fp)
822 {
823   Pool *pool = repo->pool;
824   Parsedata pd;
825   char buf[BUFF_SIZE];
826   int i, l;
827   struct stateswitch *sw;
828
829   if (repo->start && repo->start + repo->nsolvables != pool->nsolvables)
830     abort();
831   if (!repo->start)
832     repo->start = pool->nsolvables;
833
834   /* prepare parsedata */
835   memset(&pd, 0, sizeof(pd));
836   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
837     {
838       if (!pd.swtab[sw->from])
839         pd.swtab[sw->from] = sw;
840       pd.sbtab[sw->to] = sw->from;
841     }
842
843   pd.pool = pool;
844   pd.repo = repo;
845
846   pd.content = (char *)malloc(256);     /* must hold all solvable kinds! */
847   pd.acontent = 256;
848   pd.lcontent = 0;
849
850   pd.evrspace = (char *)malloc(256);
851   pd.aevrspace= 256;
852   pd.levrspace = 1;
853
854   // set up XML parser
855
856   XML_Parser parser = XML_ParserCreate(NULL);
857   XML_SetUserData(parser, &pd);       /* make parserdata available to XML callbacks */
858   XML_SetElementHandler(parser, startElement, endElement);
859   XML_SetCharacterDataHandler(parser, characterData);
860
861   // read/parse XML file
862   for (;;)
863     {
864       l = fread(buf, 1, sizeof(buf), fp);
865       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
866         {
867           fprintf(stderr, "%s at line %u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser));
868           exit(1);
869         }
870       if (l == 0)
871         break;
872     }
873   XML_ParserFree(parser);
874
875   // adapt package count
876   pool->nsolvables += pd.pack;
877   repo->nsolvables += pd.pack;
878
879   free(pd.content);
880   free(pd.evrspace);
881 }