1 /* -*- mode: C; c-file-style: "gnu"; fill-column: 78 -*- */
5 * Parse 'helix' XML representation
10 #include <sys/types.h>
18 #include "source_helix.h"
22 /* XML parser states */
45 STATE_RECOMMENDSENTRY,
47 STATE_SUPPLEMENTSENTRY,
69 #define PACK_BLOCK 255
78 static struct stateswitch stateswitches[] = {
79 { STATE_START, "channel", STATE_CHANNEL, 0 },
80 { STATE_CHANNEL, "subchannel", STATE_SUBCHANNEL, 0 },
81 { STATE_SUBCHANNEL, "package", STATE_PACKAGE, 0 },
82 { STATE_SUBCHANNEL, "selection", STATE_PACKAGE, 0 },
83 { STATE_SUBCHANNEL, "pattern", STATE_PACKAGE, 0 },
84 { STATE_SUBCHANNEL, "atom", STATE_PACKAGE, 0 },
85 { STATE_SUBCHANNEL, "patch", STATE_PACKAGE, 0 },
86 { STATE_SUBCHANNEL, "product", STATE_PACKAGE, 0 },
87 { STATE_PACKAGE, "name", STATE_NAME, 1 },
88 { STATE_PACKAGE, "epoch", STATE_PEPOCH, 1 },
89 { STATE_PACKAGE, "version", STATE_PVERSION, 1 },
90 { STATE_PACKAGE, "release", STATE_PRELEASE, 1 },
91 { STATE_PACKAGE, "arch", STATE_PARCH, 1 },
92 { STATE_PACKAGE, "history", STATE_HISTORY, 0 },
93 { STATE_PACKAGE, "provides", STATE_PROVIDES, 0 },
94 { STATE_PACKAGE, "requires", STATE_REQUIRES, 0 },
95 { STATE_PACKAGE, "obsoletes", STATE_OBSOLETES , 0 },
96 { STATE_PACKAGE, "conflicts", STATE_CONFLICTS , 0 },
97 { STATE_PACKAGE, "recommends" , STATE_RECOMMENDS , 0 },
98 { STATE_PACKAGE, "supplements", STATE_SUPPLEMENTS, 0 },
99 { STATE_PACKAGE, "suggests", STATE_SUGGESTS, 0 },
100 { STATE_PACKAGE, "enhances", STATE_ENHANCES, 0 },
101 { STATE_PACKAGE, "freshens", STATE_FRESHENS, 0 },
103 { STATE_HISTORY, "update", STATE_UPDATE, 0 },
104 { STATE_UPDATE, "epoch", STATE_EPOCH, 1 },
105 { STATE_UPDATE, "version", STATE_VERSION, 1 },
106 { STATE_UPDATE, "release", STATE_RELEASE, 1 },
107 { STATE_UPDATE, "arch", STATE_ARCH, 1 },
109 { STATE_PROVIDES, "dep", STATE_PROVIDESENTRY, 0 },
110 { STATE_REQUIRES, "dep", STATE_REQUIRESENTRY, 0 },
111 { STATE_OBSOLETES, "dep", STATE_OBSOLETESENTRY, 0 },
112 { STATE_CONFLICTS, "dep", STATE_CONFLICTSENTRY, 0 },
113 { STATE_RECOMMENDS, "dep", STATE_RECOMMENDSENTRY, 0 },
114 { STATE_SUPPLEMENTS, "dep", STATE_SUPPLEMENTSENTRY, 0 },
115 { STATE_SUGGESTS, "dep", STATE_SUGGESTSENTRY, 0 },
116 { STATE_ENHANCES, "dep", STATE_ENHANCESENTRY, 0 },
117 { STATE_FRESHENS, "dep", STATE_FRESHENSENTRY, 0 },
122 // Deps are stored as offsets into source->idarraydata
123 typedef struct _deps {
139 typedef struct _parsedata {
142 enum state state; // current state
144 char *content; // buffer for content of node
145 int lcontent; // actual length of current content
146 int acontent; // actual buffer size
147 int docontent; // handle content
150 int pack; // number of solvables
152 Pool *pool; // current pool
153 Source *source; // current source
154 Solvable *start; // collected solvables
157 Deps *deps; // dependencies array, indexed by pack#
160 int epoch; // epoch (as offset into evrspace)
161 int version; // version (as offset into evrspace)
162 int release; // release (as offset into evrspace)
163 char *evrspace; // buffer for evr
164 int aevrspace; // actual buffer space
165 int levrspace; // actual evr length
168 struct stateswitch *swtab[NUMSTATES];
169 enum state sbtab[NUMSTATES];
173 /*------------------------------------------------------------------*/
176 // create Id from epoch:version-release
179 evr2id(Pool *pool, Parsedata *pd, const char *e, const char *v, const char *r)
184 // treat explitcit 0 as NULL
185 if (e && !strcmp(e, "0"))
191 // scan version for ":"
192 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++) // skip leading digits
194 // if version contains ":", set epoch to "0"
195 if (v2 > v && *v2 == ':')
199 // compute length of Id string
202 l += strlen(e) + 1; // e:
206 l += strlen(r) + 1; // -r
208 // extend content if not sufficient
209 if (l > pd->acontent)
211 pd->content = (char *)realloc(pd->content, l + 256);
212 pd->acontent = l + 256;
215 // copy e-v-r to content
235 // if nothing inserted, return Id 0
239 fprintf(stderr, "evr: %s\n", pd->content);
242 return str2id(pool, pd->content, 1);
246 // create e:v-r from attributes
247 // atts is array of name,value pairs, NULL at end
248 // even index into atts is name
249 // odd index is value
252 evr_atts2id(Pool *pool, Parsedata *pd, const char **atts)
254 const char *e, *v, *r;
256 for (; *atts; atts += 2)
258 if (!strcmp(*atts, "epoch"))
260 else if (!strcmp(*atts, "version"))
262 else if (!strcmp(*atts, "release"))
265 return evr2id(pool, pd, e, v, r);
268 /*------------------------------------------------------------------*/
269 /* rel operator handling */
276 static struct flagtab flagtab[] = {
279 { ">=", REL_GT|REL_EQ },
281 { "!=", REL_GT|REL_LT },
282 { "<=", REL_LT|REL_EQ },
283 { "(any)", REL_LT|REL_EQ|REL_GT },
287 { "ge", REL_GT|REL_EQ },
289 { "ne", REL_GT|REL_LT },
290 { "le", REL_LT|REL_EQ },
291 { "gte", REL_GT|REL_EQ },
292 { "lte", REL_LT|REL_EQ },
295 { "GE", REL_GT|REL_EQ },
297 { "NE", REL_GT|REL_LT },
298 { "LE", REL_LT|REL_EQ }
302 * process new dependency from parser
303 * olddeps = already collected deps, this defines the 'kind' of dep
304 * atts = array of name,value attributes of dep
305 * isreq == 1 if its a requires
309 adddep(Pool *pool, Parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
312 const char *n, *f, *k;
317 /* loop over name,value pairs */
318 for (a = atts; *a; a += 2)
320 if (!strcmp(*a, "name"))
322 if (!strcmp(*a, "kind"))
324 else if (!strcmp(*a, "op"))
326 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
329 if (!n) /* quit if no name found */
333 if (k && !strcmp(k, "package"))
334 k = NULL; /* package is default */
336 if (k) /* if kind!=package, intern <kind>:<name> */
338 int l = strlen(k) + 1 + strlen(n) + 1;
339 if (l > pd->acontent) /* extend buffer if needed */
341 pd->content = (char *)realloc(pd->content, l + 256);
342 pd->acontent = l + 256;
344 sprintf(pd->content, "%s:%s", k, n);
345 name = str2id(pool, pd->content, 1);
348 name = str2id(pool, n, 1); /* package: just intern <name> */
351 if (f) /* operator ? */
354 Id evr = evr_atts2id(pool, pd, atts);
355 /* parser operator to flags */
357 for (flags = 0; flags < sizeof(flagtab)/sizeof(*flagtab); flags++)
358 if (!strcmp(f, flagtab[flags].from))
360 flags = flagtab[flags].to;
366 id = rel2id(pool, name, evr, flags, 1);
369 id = name; /* no operator */
371 /* add new dependency to source */
372 return source_addid_dep(pd->source, olddeps, id, isreq);
376 /*----------------------------------------------------------------*/
385 startElement(void *userData, const char *name, const char **atts)
387 Parsedata *pd = (Parsedata *)userData;
388 struct stateswitch *sw;
389 Pool *pool = pd->pool;
391 if (pd->depth != pd->statedepth)
397 /* ignore deps element */
398 if (pd->state == STATE_PACKAGE && !strcmp(name, "deps"))
403 /* find node name in stateswitch */
404 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
406 if (!strcmp(sw->ename, name))
410 /* check if we're at the right level */
411 if (sw->from != pd->state)
414 fprintf(stderr, "into unknown: %s\n", name);
422 pd->docontent = sw->docontent;
423 pd->statedepth = pd->depth;
425 // start with empty content
426 // (will collect data until end element
434 if (pd->kind) /* if kind is set (non package) */
436 strcpy(pd->content, pd->kind);
437 pd->lcontent = strlen(pd->content);
438 pd->content[pd->lcontent++] = ':'; /* prefix name with '<kind>:' */
439 pd->content[pd->lcontent] = 0;
443 case STATE_SUBCHANNEL:
448 case STATE_PACKAGE: /* solvable name */
450 if ((pd->pack & PACK_BLOCK) == 0) /* alloc new block ? */
452 pool->solvables = (Solvable *)realloc(pool->solvables, (pool->nsolvables + pd->pack + PACK_BLOCK + 1) * sizeof(Solvable));
453 pd->start = pool->solvables + pd->source->start;
454 memset(pd->start + pd->pack, 0, (PACK_BLOCK + 1) * sizeof(Solvable));
456 pd->deps = (Deps *)malloc((pd->pack + PACK_BLOCK + 1) * sizeof(Deps));
458 pd->deps = (Deps *)realloc(pd->deps, (pd->pack + PACK_BLOCK + 1) * sizeof(Deps));
459 memset(pd->deps + pd->pack, 0, (PACK_BLOCK + 1) * sizeof(Deps));
462 if (!strcmp(name, "selection"))
463 pd->kind = "selection";
464 else if (!strcmp(name, "pattern"))
465 pd->kind = "pattern";
466 else if (!strcmp(name, "atom"))
468 else if (!strcmp(name, "product"))
469 pd->kind = "product";
470 else if (!strcmp(name, "patch"))
473 pd->kind = NULL; /* default is package */
479 fprintf(stderr, "package #%d\n", pd->pack);
490 case STATE_PROVIDES: /* start of provides */
491 pd->deps[pd->pack].provides = 0;
493 case STATE_PROVIDESENTRY: /* entry within provides */
494 pd->deps[pd->pack].provides = adddep(pool, pd, pd->deps[pd->pack].provides, atts, 0);
497 pd->deps[pd->pack].requires = 0;
499 case STATE_REQUIRESENTRY:
500 pd->deps[pd->pack].requires = adddep(pool, pd, pd->deps[pd->pack].requires, atts, 1);
502 case STATE_OBSOLETES:
503 pd->deps[pd->pack].obsoletes = 0;
505 case STATE_OBSOLETESENTRY:
506 pd->deps[pd->pack].obsoletes = adddep(pool, pd, pd->deps[pd->pack].obsoletes, atts, 0);
508 case STATE_CONFLICTS:
509 pd->deps[pd->pack].conflicts = 0;
511 case STATE_CONFLICTSENTRY:
512 pd->deps[pd->pack].conflicts = adddep(pool, pd, pd->deps[pd->pack].conflicts, atts, 0);
514 case STATE_RECOMMENDS:
515 pd->deps[pd->pack].recommends = 0;
517 case STATE_RECOMMENDSENTRY:
518 pd->deps[pd->pack].recommends = adddep(pool, pd, pd->deps[pd->pack].recommends, atts, 0);
520 case STATE_SUPPLEMENTS:
521 pd->deps[pd->pack].supplements= 0;
523 case STATE_SUPPLEMENTSENTRY:
524 pd->deps[pd->pack].supplements = adddep(pool, pd, pd->deps[pd->pack].supplements, atts, 0);
527 pd->deps[pd->pack].suggests = 0;
529 case STATE_SUGGESTSENTRY:
530 pd->deps[pd->pack].suggests = adddep(pool, pd, pd->deps[pd->pack].suggests, atts, 0);
533 pd->deps[pd->pack].enhances = 0;
535 case STATE_ENHANCESENTRY:
536 pd->deps[pd->pack].enhances = adddep(pool, pd, pd->deps[pd->pack].enhances, atts, 0);
539 pd->deps[pd->pack].freshens = 0;
541 case STATE_FRESHENSENTRY:
542 pd->deps[pd->pack].freshens = adddep(pool, pd, pd->deps[pd->pack].freshens, atts, 0);
549 static const char* findKernelFlavor(Parsedata *pd)
551 Pool *pool = pd->pool;
555 for (pidp = pd->source->idarraydata + pd->deps[pd->pack].provides; pidp && (pid = *pidp++) != 0; )
560 continue; /* wrong provides name */
561 prd = GETRELDEP(pool, pid);
562 const char *depname = id2str(pool, prd->name);
563 if (!strncmp(depname, "kernel-", strlen("kernel-")))
565 return depname + strlen("kernel-");
569 //fprintf(stderr, "pack %d\n", pd->pack);
570 //fprintf(stderr, "source %d\n", pd->deps[pd->pack].requires);
572 if (! pd->deps[pd->pack].requires )
575 for (pidp = pd->source->idarraydata + pd->deps[pd->pack].requires ; pidp && (pid = *pidp++) != 0; )
577 const char *depname = 0;
581 depname = id2str(pool, pid);
585 Reldep *prd = GETRELDEP(pool, pid);
586 depname = id2str(pool, prd->name);
588 if (!strncmp(depname, "kernel-", strlen("kernel-")))
590 return depname + strlen("kernel-");
601 * create Solvable from collected data
605 endElement(void *userData, const char *name)
607 Parsedata *pd = (Parsedata *)userData;
608 Pool *pool = pd->pool;
609 Solvable *s = pd->start ? pd->start + pd->pack : NULL;
612 if (pd->depth != pd->statedepth)
615 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
619 /* ignore deps element */
620 if (pd->state == STATE_PACKAGE && !strcmp(name, "deps"))
628 case STATE_PACKAGE: /* package complete */
630 if (!s->arch) /* default to "noarch" */
631 s->arch = ARCH_NOARCH;
633 if (!s->evr && pd->version) /* set solvable evr */
634 s->evr = evr2id(pool, pd,
635 pd->epoch ? pd->evrspace + pd->epoch : 0,
636 pd->version ? pd->evrspace + pd->version : 0,
637 pd->release ? pd->evrspace + pd->release : 0);
638 /* ensure self-provides */
639 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
640 pd->deps[pd->pack].provides = source_addid_dep(pd->source, pd->deps[pd->pack].provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
641 pd->deps[pd->pack].supplements = source_fix_legacy(pd->source, pd->deps[pd->pack].provides, pd->deps[pd->pack].supplements);
643 const char *flavor = findKernelFlavor(pd);
646 char *cflavor = strdup(flavor);
651 /* this is either a kernel package or a kmp */
652 fprintf(stderr, "flavor %s\n", flavor);
654 for (pidp = pd->source->idarraydata + pd->deps[pd->pack].provides; pidp && (pid = *pidp++) != 0; )
656 const char *depname = 0;
661 prd = GETRELDEP(pool, pid);
662 depname = id2str(pool, prd->name);
666 depname = id2str(pool, pid);
669 if (!strncmp(depname, "kernel(", strlen("kernel(")) && !strchr(depname, ':'))
672 strcpy(newdep, "kernel(");
673 strncat(newdep, cflavor, sizeof(newdep));
674 strncat(newdep, ":", sizeof(newdep));
675 strncat(newdep, depname + strlen("kernel("), 100);
676 // fprintf(stderr, "dep %s %s\n", depname, newdep);
677 Id newid = str2id(pool, newdep, 1);
679 newid = rel2id(pool, newid, prd->evr, prd->flags, 1);
680 npr = source_addid_dep(pd->source, npr, newid, 0);
683 pd->deps[pd->pack].provides = npr;
686 fprintf(stderr, "dd %d\n", pd->deps[pd->pack].requires);
687 for (pidp = pd->source->idarraydata + pd->deps[pd->pack].requires; pidp && (pid = *pidp++) != 0; )
689 const char *depname = 0;
694 prd = GETRELDEP(pool, pid);
695 depname = id2str(pool, prd->name);
699 depname = id2str(pool, pid);
702 if (!strncmp(depname, "kernel(", strlen("kernel(")) && !strchr(depname, ':'))
705 strcpy(newdep, "kernel(");
706 strncat(newdep, cflavor, sizeof(newdep));
707 strncat(newdep, ":", sizeof(newdep));
708 strncat(newdep, depname + strlen("kernel("), 100);
709 fprintf(stderr, "dep %s %s\n", depname, newdep);
710 source_reserve_ids(pd->source, 0, 1);
711 Id newid = str2id(pool, newdep, 1);
713 newid = rel2id(pool, newid, prd->evr, prd->flags, 1);
714 npr = source_addid_dep(pd->source, npr, newid, 0);
718 npr = source_addid_dep(pd->source, npr, pid, 0);
720 pd->deps[pd->pack].requires = npr;
725 pd->pack++; /* inc pack count */
729 s->name = str2id(pool, pd->content, 1);
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);
740 /* use highest evr */
741 if (!s->evr || evrcmp(pool, s->evr, evr) <= 0)
750 /* ensure buffer space */
751 if (pd->lcontent + 1 + pd->levrspace > pd->aevrspace)
753 pd->evrspace = (char *)realloc(pd->evrspace, pd->lcontent + 1 + pd->levrspace + 256);
754 pd->aevrspace = pd->lcontent + 1 + pd->levrspace + 256;
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;
762 pd->release = pd->levrspace;
763 pd->levrspace += pd->lcontent + 1;
767 s->arch = str2id(pool, pd->content, 1);
772 pd->state = pd->sbtab[pd->state];
774 // printf("back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
785 characterData(void *userData, const XML_Char *s, int len)
787 Parsedata *pd = (Parsedata *)userData;
791 // check if current nodes content is interesting
795 // adapt content buffer
796 l = pd->lcontent + len + 1;
797 if (l > pd->acontent)
799 pd->content = (char *)realloc(pd->content, l + 256);
800 pd->acontent = l + 256;
802 // append new content to buffer
803 c = pd->content + pd->lcontent;
810 /*-------------------------------------------------------------------*/
812 #define BUFF_SIZE 8192
815 * read 'helix' type xml from fp
816 * add packages to pool/source
821 pool_addsource_helix(Pool *pool, FILE *fp)
829 struct stateswitch *sw;
831 // create empty source
832 source = pool_addsource_empty(pool);
835 memset(&pd, 0, sizeof(pd));
836 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
838 if (!pd.swtab[sw->from])
839 pd.swtab[sw->from] = sw;
840 pd.sbtab[sw->to] = sw->from;
846 pd.content = (char *)malloc(256); /* must hold all solvable kinds! */
850 pd.evrspace = (char *)malloc(256);
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);
861 // read/parse XML file
864 l = fread(buf, 1, sizeof(buf), fp);
865 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
867 fprintf(stderr, "%s at line %u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser));
873 XML_ParserFree(parser);
875 // adapt package count
876 pool->nsolvables += pd.pack;
877 source->nsolvables = pd.pack;
879 // now set dependency pointers for each solvable
881 solvable = pool->solvables + source->start;
882 for (i = 0; i < pd.pack; i++, solvable++)
884 if (deps[i].provides)
885 solvable->provides = source->idarraydata + deps[i].provides;
886 if (deps[i].requires)
887 solvable->requires = source->idarraydata + deps[i].requires;
888 if (deps[i].conflicts)
889 solvable->conflicts = source->idarraydata + deps[i].conflicts;
890 if (deps[i].obsoletes)
891 solvable->obsoletes = source->idarraydata + deps[i].obsoletes;
892 if (deps[i].recommends)
893 solvable->recommends = source->idarraydata + deps[i].recommends;
894 if (deps[i].supplements)
895 solvable->supplements = source->idarraydata + deps[i].supplements;
896 if (deps[i].suggests)
897 solvable->suggests = source->idarraydata + deps[i].suggests;
898 if (deps[i].enhances)
899 solvable->enhances = source->idarraydata + deps[i].enhances;
900 if (deps[i].freshens)
901 solvable->freshens = source->idarraydata + deps[i].freshens;