Imported Upstream version 0.6.27
[platform/upstream/libsolv.git] / ext / testcase.c
1 /*
2  * Copyright (c) 2012, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include "pool.h"
16 #include "poolarch.h"
17 #include "poolvendor.h"
18 #include "repo.h"
19 #include "repo_solv.h"
20 #include "solver.h"
21 #include "solverdebug.h"
22 #include "chksum.h"
23 #include "testcase.h"
24 #include "selection.h"
25 #include "solv_xfopen.h"
26
27 #define DISABLE_JOIN2
28 #include "tools_util.h"
29
30 static struct job2str {
31   Id job;
32   const char *str;
33 } job2str[] = {
34   { SOLVER_NOOP,           "noop" },
35   { SOLVER_INSTALL,        "install" },
36   { SOLVER_ERASE,          "erase" },
37   { SOLVER_UPDATE,         "update" },
38   { SOLVER_WEAKENDEPS,     "weakendeps" },
39   { SOLVER_MULTIVERSION,   "multiversion" },
40   { SOLVER_MULTIVERSION,   "noobsoletes" },     /* old name */
41   { SOLVER_LOCK,           "lock" },
42   { SOLVER_DISTUPGRADE,    "distupgrade" },
43   { SOLVER_VERIFY,         "verify" },
44   { SOLVER_DROP_ORPHANED,  "droporphaned" },
45   { SOLVER_USERINSTALLED,  "userinstalled" },
46   { SOLVER_ALLOWUNINSTALL, "allowuninstall" },
47   { SOLVER_FAVOR,          "favor" },
48   { SOLVER_DISFAVOR,       "disfavor" },
49   { 0, 0 }
50 };
51
52 static struct jobflags2str {
53   Id flag;
54   const char *str;
55 } jobflags2str[] = {
56   { SOLVER_WEAK,      "weak" },
57   { SOLVER_ESSENTIAL, "essential" },
58   { SOLVER_CLEANDEPS, "cleandeps" },
59   { SOLVER_ORUPDATE,  "orupdate" },
60   { SOLVER_FORCEBEST, "forcebest" },
61   { SOLVER_TARGETED,  "targeted" },
62   { SOLVER_NOTBYUSER, "notbyuser" },
63   { SOLVER_SETEV,     "setev" },
64   { SOLVER_SETEVR,    "setevr" },
65   { SOLVER_SETARCH,   "setarch" },
66   { SOLVER_SETVENDOR, "setvendor" },
67   { SOLVER_SETREPO,   "setrepo" },
68   { SOLVER_NOAUTOSET, "noautoset" },
69   { 0, 0 }
70 };
71
72 static struct resultflags2str {
73   Id flag;
74   const char *str;
75 } resultflags2str[] = {
76   { TESTCASE_RESULT_TRANSACTION,        "transaction" },
77   { TESTCASE_RESULT_PROBLEMS,           "problems" },
78   { TESTCASE_RESULT_ORPHANED,           "orphaned" },
79   { TESTCASE_RESULT_RECOMMENDED,        "recommended" },
80   { TESTCASE_RESULT_UNNEEDED,           "unneeded" },
81   { TESTCASE_RESULT_ALTERNATIVES,       "alternatives" },
82   { TESTCASE_RESULT_RULES,              "rules" },
83   { TESTCASE_RESULT_GENID,              "genid" },
84   { TESTCASE_RESULT_REASON,             "reason" },
85   { 0, 0 }
86 };
87
88 static struct solverflags2str {
89   Id flag;
90   const char *str;
91   int def;
92 } solverflags2str[] = {
93   { SOLVER_FLAG_ALLOW_DOWNGRADE,            "allowdowngrade", 0 },
94   { SOLVER_FLAG_ALLOW_NAMECHANGE,           "allownamechange", 1 },
95   { SOLVER_FLAG_ALLOW_ARCHCHANGE,           "allowarchchange", 0 },
96   { SOLVER_FLAG_ALLOW_VENDORCHANGE,         "allowvendorchange", 0 },
97   { SOLVER_FLAG_ALLOW_UNINSTALL,            "allowuninstall", 0 },
98   { SOLVER_FLAG_NO_UPDATEPROVIDE,           "noupdateprovide", 0 },
99   { SOLVER_FLAG_SPLITPROVIDES,              "splitprovides", 0 },
100   { SOLVER_FLAG_IGNORE_RECOMMENDED,         "ignorerecommended", 0 },
101   { SOLVER_FLAG_ADD_ALREADY_RECOMMENDED,    "addalreadyrecommended", 0 },
102   { SOLVER_FLAG_NO_INFARCHCHECK,            "noinfarchcheck", 0 },
103   { SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES,    "keepexplicitobsoletes", 0 },
104   { SOLVER_FLAG_BEST_OBEY_POLICY,           "bestobeypolicy", 0 },
105   { SOLVER_FLAG_NO_AUTOTARGET,              "noautotarget", 0 },
106   { SOLVER_FLAG_DUP_ALLOW_DOWNGRADE,        "dupallowdowngrade", 1 },
107   { SOLVER_FLAG_DUP_ALLOW_ARCHCHANGE,       "dupallowarchchange", 1 },
108   { SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE,     "dupallowvendorchange", 1 },
109   { SOLVER_FLAG_DUP_ALLOW_NAMECHANGE,       "dupallownamechange", 1 },
110   { SOLVER_FLAG_KEEP_ORPHANS,               "keeporphans", 0 },
111   { SOLVER_FLAG_BREAK_ORPHANS,              "breakorphans", 0 },
112   { SOLVER_FLAG_FOCUS_INSTALLED,            "focusinstalled", 0 },
113   { SOLVER_FLAG_YUM_OBSOLETES,              "yumobsoletes", 0 },
114   { SOLVER_FLAG_NEED_UPDATEPROVIDE,         "needupdateprovide", 0 },
115   { SOLVER_FLAG_URPM_REORDER,               "urpmreorder", 0 },
116   { SOLVER_FLAG_FOCUS_BEST,                 "focusbest", 0 },
117   { SOLVER_FLAG_STRONG_RECOMMENDS,          "strongrecommends", 0 },
118   { SOLVER_FLAG_INSTALL_ALSO_UPDATES,       "installalsoupdates", 0 },
119   { 0, 0, 0 }
120 };
121
122 static struct poolflags2str {
123   Id flag;
124   const char *str;
125   int def;
126 } poolflags2str[] = {
127   { POOL_FLAG_PROMOTEEPOCH,                 "promoteepoch", 0 },
128   { POOL_FLAG_FORBIDSELFCONFLICTS,          "forbidselfconflicts", 0 },
129   { POOL_FLAG_OBSOLETEUSESPROVIDES,         "obsoleteusesprovides", 0 },
130   { POOL_FLAG_IMPLICITOBSOLETEUSESPROVIDES, "implicitobsoleteusesprovides", 0 },
131   { POOL_FLAG_OBSOLETEUSESCOLORS,           "obsoleteusescolors", 0 },
132   { POOL_FLAG_IMPLICITOBSOLETEUSESCOLORS,   "implicitobsoleteusescolors", 0 },
133   { POOL_FLAG_NOINSTALLEDOBSOLETES,         "noinstalledobsoletes", 0 },
134   { POOL_FLAG_HAVEDISTEPOCH,                "havedistepoch", 0 },
135   { POOL_FLAG_NOOBSOLETESMULTIVERSION,      "noobsoletesmultiversion", 0 },
136   { POOL_FLAG_ADDFILEPROVIDESFILTERED,      "addfileprovidesfiltered", 0 },
137   { POOL_FLAG_NOWHATPROVIDESAUX,            "nowhatprovidesaux", 0 },
138   { 0, 0, 0 }
139 };
140
141 static struct disttype2str {
142   Id type;
143   const char *str;
144 } disttype2str[] = {
145   { DISTTYPE_RPM,  "rpm" },
146   { DISTTYPE_DEB,  "deb" },
147   { DISTTYPE_ARCH, "arch" },
148   { DISTTYPE_HAIKU, "haiku" },
149   { 0, 0 }
150 };
151
152 static struct selflags2str {
153   Id flag;
154   const char *str;
155 } selflags2str[] = {
156   { SELECTION_NAME, "name" },
157   { SELECTION_PROVIDES, "provides" },
158   { SELECTION_FILELIST, "filelist" },
159   { SELECTION_CANON, "canon" },
160   { SELECTION_DOTARCH, "dotarch" },
161   { SELECTION_REL, "rel" },
162   { SELECTION_INSTALLED_ONLY, "installedonly" },
163   { SELECTION_GLOB, "glob" },
164   { SELECTION_FLAT, "flat" },
165   { SELECTION_NOCASE, "nocase" },
166   { SELECTION_SOURCE_ONLY, "sourceonly" },
167   { SELECTION_WITH_SOURCE, "withsource" },
168   { 0, 0 }
169 };
170
171 static const char *features[] = {
172 #ifdef ENABLE_LINKED_PKGS
173   "linked_packages",
174 #endif
175 #ifdef ENABLE_COMPLEX_DEPS
176   "complex_deps",
177 #endif
178   0
179 };
180
181 typedef struct strqueue {
182   char **str;
183   int nstr;
184 } Strqueue;
185
186 #define STRQUEUE_BLOCK 63
187
188 static void
189 strqueue_init(Strqueue *q)
190 {
191   q->str = 0;
192   q->nstr = 0;
193 }
194
195 static void
196 strqueue_free(Strqueue *q)
197 {
198   int i;
199   for (i = 0; i < q->nstr; i++)
200     solv_free(q->str[i]);
201   q->str = solv_free(q->str);
202   q->nstr = 0;
203 }
204
205 static void
206 strqueue_push(Strqueue *q, const char *s)
207 {
208   q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
209   q->str[q->nstr++] = solv_strdup(s);
210 }
211
212 static void
213 strqueue_pushjoin(Strqueue *q, const char *s1, const char *s2, const char *s3)
214 {
215   q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
216   q->str[q->nstr++] = solv_dupjoin(s1, s2, s3);
217 }
218
219 static int
220 strqueue_sort_cmp(const void *ap, const void *bp, void *dp)
221 {
222   const char *a = *(const char **)ap;
223   const char *b = *(const char **)bp;
224   return strcmp(a ? a : "", b ? b : "");
225 }
226
227 static void
228 strqueue_sort(Strqueue *q)
229 {
230   if (q->nstr > 1)
231     solv_sort(q->str, q->nstr, sizeof(*q->str), strqueue_sort_cmp, 0);
232 }
233
234 static void
235 strqueue_sort_u(Strqueue *q)
236 {
237   int i, j;
238   strqueue_sort(q);
239   for (i = j = 0; i < q->nstr; i++)
240     if (!j || strqueue_sort_cmp(q->str + i, q->str + j - 1, 0) != 0)
241       q->str[j++] = q->str[i];
242   q->nstr = j;
243 }
244
245 static char *
246 strqueue_join(Strqueue *q)
247 {
248   int i, l = 0;
249   char *r, *rp;
250   for (i = 0; i < q->nstr; i++)
251     if (q->str[i])
252       l += strlen(q->str[i]) + 1;
253   l++;  /* trailing \0 */
254   r = solv_malloc(l);
255   rp = r;
256   for (i = 0; i < q->nstr; i++)
257     if (q->str[i])
258       {
259         strcpy(rp, q->str[i]);
260         rp += strlen(rp);
261         *rp++ = '\n';
262       }
263   *rp = 0;
264   return r;
265 }
266
267 static void
268 strqueue_split(Strqueue *q, const char *s)
269 {
270   const char *p;
271   if (!s)
272     return;
273   while ((p = strchr(s, '\n')) != 0)
274     {
275       q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
276       q->str[q->nstr] = solv_malloc(p - s + 1);
277       if (p > s)
278         memcpy(q->str[q->nstr], s, p - s);
279       q->str[q->nstr][p - s] = 0;
280       q->nstr++;
281       s = p + 1;
282     }
283   if (*s)
284     strqueue_push(q, s);
285 }
286
287 static void
288 strqueue_diff(Strqueue *sq1, Strqueue *sq2, Strqueue *osq)
289 {
290   int i = 0, j = 0;
291   while (i < sq1->nstr && j < sq2->nstr)
292     {
293       int r = strqueue_sort_cmp(sq1->str + i, sq2->str + j, 0);
294       if (!r)
295         i++, j++;
296       else if (r < 0)
297         strqueue_pushjoin(osq, "-", sq1->str[i++], 0);
298       else
299         strqueue_pushjoin(osq, "+", sq2->str[j++], 0);
300     }
301   while (i < sq1->nstr)
302     strqueue_pushjoin(osq, "-", sq1->str[i++], 0);
303   while (j < sq2->nstr)
304     strqueue_pushjoin(osq, "+", sq2->str[j++], 0);
305 }
306
307
308 static const char *
309 testcase_id2str(Pool *pool, Id id, int isname)
310 {
311   const char *s = pool_id2str(pool, id);
312   const char *ss;
313   char *s2, *s2p;
314   int bad = 0, paren = 0, parenbad = 0;
315
316   if (id == 0)
317     return "<NULL>";
318   if (id == 1)
319     return "\\00";
320   if (strchr("[(<=>!", *s))
321     bad++;
322   if (!strncmp(s, "namespace:", 10))
323     bad++;
324   for (ss = s + bad; *ss; ss++)
325     {
326       if (*ss == ' ' || *ss == '\\' || *(unsigned char *)ss < 32 || *ss == '(' || *ss == ')')
327         bad++;
328       if (*ss == '(')
329         paren = paren == 0 ? 1 : -1;
330       else if (*ss == ')')
331         {
332           paren = paren == 1 ? 0 : -1;
333           if (!paren)
334             parenbad += 2;
335         }
336     }
337   if (isname && ss - s > 4 && !strcmp(ss - 4, ":any"))
338     bad++;
339   if (!paren && !(bad - parenbad))
340     return s;
341
342   /* we need escaping! */
343   s2 = s2p = pool_alloctmpspace(pool, strlen(s) + bad * 2 + 1);
344   if (!strncmp(s, "namespace:", 10))
345     {
346       strcpy(s2p, "namespace\\3a");
347       s2p += 12;
348       s += 10;
349     }
350   ss = s;
351   for (; *ss; ss++)
352     {
353       *s2p++ = *ss;
354       if ((ss == s && strchr("[(<=>!", *s)) || *ss == ' ' || *ss == '\\' || *(unsigned char *)ss < 32 || *ss == '(' || *ss == ')')
355         {
356           s2p[-1] = '\\';
357           solv_bin2hex((unsigned char *)ss, 1, s2p);
358           s2p += 2;
359         }
360     }
361   *s2p = 0;
362   if (isname && s2p - s2 > 4 && !strcmp(s2p - 4, ":any"))
363     strcpy(s2p - 4, "\\3aany");
364   return s2;
365 }
366
367 struct oplist {
368   Id flags;
369   const char *opname;
370 } oplist[] = {
371   { REL_EQ, "=" },
372   { REL_GT | REL_LT | REL_EQ, "<=>" },
373   { REL_LT | REL_EQ, "<=" },
374   { REL_GT | REL_EQ, ">=" },
375   { REL_GT, ">" },
376   { REL_GT | REL_LT, "<>" },
377   { REL_AND,   "&" },
378   { REL_OR ,   "|" },
379   { REL_WITH , "+" },
380   { REL_NAMESPACE , "<NAMESPACE>" },
381   { REL_ARCH,       "." },
382   { REL_MULTIARCH,  "<MULTIARCH>" },
383   { REL_FILECONFLICT,  "<FILECONFLICT>" },
384   { REL_COND,  "<IF>" },
385   { REL_COMPAT,  "compat >=" },
386   { REL_KIND,  "<KIND>" },
387   { REL_ELSE, "<ELSE>" },
388   { REL_ERROR, "<ERROR>" },
389   { REL_LT, "<" },
390   { 0, 0 }
391 };
392
393 static char *
394 testcase_dep2str_complex(Pool *pool, char *s, Id id, int addparens)
395 {
396   Reldep *rd;
397   const char *s2;
398   int needparens;
399   struct oplist *op;
400
401   if (!ISRELDEP(id))
402     {
403       s2 = testcase_id2str(pool, id, 1);
404       s = pool_tmpappend(pool, s, s2, 0);
405       pool_freetmpspace(pool, s2);
406       return s;
407     }
408   rd = GETRELDEP(pool, id);
409
410   /* check for special shortcuts */
411   if (rd->flags == REL_NAMESPACE && !ISRELDEP(rd->name) && !strncmp(pool_id2str(pool, rd->name), "namespace:", 10))
412     {
413       s = pool_tmpappend(pool, s, pool_id2str(pool, rd->name), "(");
414       s = testcase_dep2str_complex(pool, s, rd->evr, 0);
415       return pool_tmpappend(pool, s, ")", 0);
416     }
417   if (rd->flags == REL_MULTIARCH && !ISRELDEP(rd->name) && rd->evr == ARCH_ANY)
418     {
419       /* append special :any suffix */
420       s2 = testcase_id2str(pool, rd->name, 1);
421       s = pool_tmpappend(pool, s, s2, ":any");
422       pool_freetmpspace(pool, s2);
423       return s;
424     }
425
426   needparens = 0;
427   if (ISRELDEP(rd->name))
428     {
429       Reldep *rd2 = GETRELDEP(pool, rd->name);
430       needparens = 1;
431       if (rd->flags > 7 && rd->flags != REL_COMPAT && rd2->flags && rd2->flags <= 7)
432         needparens = 0;
433     }
434
435   if (addparens)
436     s = pool_tmpappend(pool, s, "(", 0);
437   s = testcase_dep2str_complex(pool, s, rd->name, needparens);
438
439   for (op = oplist; op->flags; op++)
440     if (rd->flags == op->flags)
441       break;
442   if (op->flags)
443     {
444       s = pool_tmpappend(pool, s, " ", op->opname);
445       s = pool_tmpappend(pool, s, " ", 0);
446     }
447   else
448     {
449       char buf[64];
450       sprintf(buf, " <%u> ", rd->flags);
451       s = pool_tmpappend(pool, s, buf, 0);
452     }
453
454   needparens = 0;
455   if (ISRELDEP(rd->evr))
456     {
457       Reldep *rd2 = GETRELDEP(pool, rd->evr);
458       needparens = 1;
459       if (rd->flags > 7 && rd2->flags && rd2->flags <= 7)
460         needparens = 0;
461       if (rd->flags == REL_AND && rd2->flags == REL_AND)
462         needparens = 0; /* chain */
463       if (rd->flags == REL_OR && rd2->flags == REL_OR)
464         needparens = 0; /* chain */
465       if (rd->flags > 0 && rd->flags < 8 && rd2->flags == REL_COMPAT)
466         needparens = 0; /* chain */
467     }
468   if (!ISRELDEP(rd->evr))
469     {
470       s2 = testcase_id2str(pool, rd->evr, 0);
471       s = pool_tmpappend(pool, s, s2, 0);
472       pool_freetmpspace(pool, s2);
473     }
474   else
475     s = (char *)testcase_dep2str_complex(pool, s, rd->evr, needparens);
476   if (addparens)
477     s = pool_tmpappend(pool, s, ")", 0);
478   return s;
479 }
480
481 const char *
482 testcase_dep2str(Pool *pool, Id id)
483 {
484   char *s;
485   if (!ISRELDEP(id))
486     return testcase_id2str(pool, id, 1);
487   s = pool_alloctmpspace(pool, 1);
488   *s = 0;
489   return testcase_dep2str_complex(pool, s, id, 0);
490 }
491
492
493 /* Convert a simple string. Also handle the :any suffix */
494 static Id
495 testcase_str2dep_simple(Pool *pool, const char **sp, int isname)
496 {
497   int haveesc = 0;
498   int paren = 0;
499   int isany = 0;
500   Id id;
501   const char *s;
502   for (s = *sp; *s; s++)
503     {
504       if (*s == '\\')
505         haveesc++;
506       if (*s == ' ' || *(unsigned char *)s < 32)
507         break;
508       if (*s == '(')
509         paren++;
510       if (*s == ')' && paren-- <= 0)
511         break;
512     }
513   if (isname && s - *sp > 4 && !strncmp(s - 4, ":any", 4))
514     {
515       isany = 1;
516       s -= 4;
517     }
518   if (!haveesc)
519     {
520       if (s - *sp == 6 && !strncmp(*sp, "<NULL>", 6))
521         id = 0;
522       else
523         id = pool_strn2id(pool, *sp, s - *sp, 1);
524     }
525   else if (s - *sp == 3 && !strncmp(*sp, "\\00", 3))
526     id = 1;
527   else
528     {
529       char buf[128], *bp, *bp2;
530       const char *sp2;
531       bp = s - *sp >= 128 ? solv_malloc(s - *sp + 1) : buf;
532       for (bp2 = bp, sp2 = *sp; sp2 < s;)
533         {
534           *bp2++ = *sp2++;
535           if (bp2[-1] == '\\')
536             solv_hex2bin(&sp2, (unsigned char *)bp2 - 1, 1);
537         }
538       *bp2 = 0;
539       id = pool_str2id(pool, bp, 1);
540       if (bp != buf)
541         solv_free(bp);
542     }
543   if (isany)
544     {
545       id = pool_rel2id(pool, id, ARCH_ANY, REL_MULTIARCH, 1);
546       s += 4;
547     }
548   *sp = s;
549   return id;
550 }
551
552
553 static Id
554 testcase_str2dep_complex(Pool *pool, const char **sp, int relop)
555 {
556   const char *s = *sp;
557   Id flags, id, id2, namespaceid = 0;
558   struct oplist *op;
559
560   while (*s == ' ' || *s == '\t')
561     s++;
562   if (!strncmp(s, "namespace:", 10))
563     {
564       /* special namespace hack */
565       const char *s2;
566       for (s2 = s + 10; *s2 && *s2 != '('; s2++)
567         ;
568       if (*s2 == '(')
569         {
570           namespaceid = pool_strn2id(pool, s, s2 - s, 1);
571           s = s2;
572         }
573     }
574   if (*s == '(')
575     {
576       s++;
577       id = testcase_str2dep_complex(pool, &s, 0);
578       if (!s || *s != ')')
579         {
580           *sp = 0;
581           return 0;
582         }
583       s++;
584     }
585   else
586     id = testcase_str2dep_simple(pool, &s, relop ? 0 : 1);
587   if (namespaceid)
588     id = pool_rel2id(pool, namespaceid, id, REL_NAMESPACE, 1);
589     
590   for (;;)
591     {
592       while (*s == ' ' || *s == '\t')
593         s++;
594       if (!*s || *s == ')' || (relop && strncmp(s, "compat >= ", 10) != 0))
595         {
596           *sp = s;
597           return id;
598         }
599
600       /* we have an op! Find the end */
601       flags = -1;
602       if (s[0] == '<' && (s[1] >= '0' && s[1] <= '9'))
603         {
604           const char *s2;
605           for (s2 = s + 1; *s2 >= '0' && *s2 <= '9'; s2++)
606             ;
607           if (*s2 == '>')
608             {
609               flags = strtoul(s + 1, 0, 10);
610               s = s2 + 1;
611             }
612         }
613       if (flags == -1)
614         {
615           for (op = oplist; op->flags; op++)
616             if (!strncmp(s, op->opname, strlen(op->opname)))
617               break;
618           if (!op->flags)
619             {
620               *sp = 0;
621               return 0;
622             }
623           flags = op->flags;
624           s += strlen(op->opname);
625         }
626       id2 = testcase_str2dep_complex(pool, &s, flags > 0 && flags < 8);
627       if (!s)
628         {
629           *sp = 0;
630           return 0;
631         }
632       id = pool_rel2id(pool, id, id2, flags, 1);
633     }
634 }
635
636 Id
637 testcase_str2dep(Pool *pool, const char *s)
638 {
639   Id id = testcase_str2dep_complex(pool, &s, 0);
640   return s && !*s ? id : 0;
641 }
642
643 /**********************************************************/
644
645 const char *
646 testcase_repoid2str(Pool *pool, Id repoid)
647 {
648   Repo *repo = pool_id2repo(pool, repoid);
649   if (repo->name)
650     {
651       char *r = pool_tmpjoin(pool, repo->name, 0, 0);
652       char *rp;
653       for (rp = r; *rp; rp++)
654         if (*rp == ' ' || *rp == '\t')
655           *rp = '_';
656       return r;
657     }
658   else
659     {
660       char buf[20];
661       sprintf(buf, "#%d", repoid);
662       return pool_tmpjoin(pool, buf, 0, 0);
663     }
664 }
665
666 const char *
667 testcase_solvid2str(Pool *pool, Id p)
668 {
669   Solvable *s = pool->solvables + p;
670   const char *n, *e, *a;
671   char *str, buf[20];
672
673   if (p == SYSTEMSOLVABLE)
674     return "@SYSTEM";
675   n = pool_id2str(pool, s->name);
676   e = pool_id2str(pool, s->evr);
677   a = pool_id2str(pool, s->arch);
678   str = pool_alloctmpspace(pool, strlen(n) + strlen(e) + strlen(a) + 3);
679   sprintf(str, "%s-%s.%s", n, e, a);
680   if (!s->repo)
681     return pool_tmpappend(pool, str, "@", 0);
682   if (s->repo->name)
683     {
684       int l = strlen(str);
685       char *str2 = pool_tmpappend(pool, str, "@", s->repo->name);
686       for (; str2[l]; l++)
687         if (str2[l] == ' ' || str2[l] == '\t')
688           str2[l] = '_';
689       return str2;
690     }
691   sprintf(buf, "@#%d", s->repo->repoid);
692   return pool_tmpappend(pool, str, buf, 0);
693 }
694
695 Repo *
696 testcase_str2repo(Pool *pool, const char *str)
697 {
698   int repoid;
699   Repo *repo = 0;
700   if (str[0] == '#' && (str[1] >= '0' && str[1] <= '9'))
701     {
702       int j;
703       repoid = 0;
704       for (j = 1; str[j] >= '0' && str[j] <= '9'; j++)
705         repoid = repoid * 10 + (str[j] - '0');
706       if (!str[j] && repoid > 0 && repoid < pool->nrepos)
707         repo = pool_id2repo(pool, repoid);
708     }
709   if (!repo)
710     {
711       FOR_REPOS(repoid, repo)
712         {
713           int i, l;
714           if (!repo->name)
715             continue;
716           l = strlen(repo->name);
717           for (i = 0; i < l; i++)
718             {
719               int c = repo->name[i];
720               if (c == ' ' || c == '\t')
721                 c = '_';
722               if (c != str[i])
723                 break;
724             }
725           if (i == l && !str[l])
726             break;
727         }
728       if (repoid >= pool->nrepos)
729         repo = 0;
730     }
731   return repo;
732 }
733
734 Id
735 testcase_str2solvid(Pool *pool, const char *str)
736 {
737   int i, l = strlen(str);
738   int repostart;
739   Repo *repo;
740   Id arch;
741
742   if (!l)
743     return 0;
744   if (*str == '@' && !strcmp(str, "@SYSTEM"))
745     return SYSTEMSOLVABLE;
746   repo = 0;
747   for (i = l - 1; i >= 0; i--)
748     if (str[i] == '@' && (repo = testcase_str2repo(pool, str + i + 1)) != 0)
749       break;
750   if (i < 0)
751     i = l;
752   repostart = i;
753   /* now find the arch (if present) */
754   arch = 0;
755   for (i = repostart - 1; i > 0; i--)
756     if (str[i] == '.')
757       {
758         arch = pool_strn2id(pool, str + i + 1, repostart - (i + 1), 0);
759         if (arch)
760           repostart = i;
761         break;
762       }
763   /* now find the name */
764   for (i = repostart - 1; i > 0; i--)
765     {
766       if (str[i] == '-')
767         {
768           Id nid, evrid, p, pp;
769           nid = pool_strn2id(pool, str, i, 0);
770           if (!nid)
771             continue;
772           evrid = pool_strn2id(pool, str + i + 1, repostart - (i + 1), 0);
773           if (!evrid)
774             continue;
775           /* first check whatprovides */
776           FOR_PROVIDES(p, pp, nid)
777             {
778               Solvable *s = pool->solvables + p;
779               if (s->name != nid || s->evr != evrid)
780                 continue;
781               if (repo && s->repo != repo)
782                 continue;
783               if (arch && s->arch != arch)
784                 continue;
785               return p;
786             }
787           /* maybe it's not installable and thus not in whatprovides. do a slow search */
788           if (repo)
789             {
790               Solvable *s;
791               FOR_REPO_SOLVABLES(repo, p, s)
792                 {
793                   if (s->name != nid || s->evr != evrid)
794                     continue;
795                   if (arch && s->arch != arch)
796                     continue;
797                   return p;
798                 }
799             }
800           else
801             {
802               FOR_POOL_SOLVABLES(p)
803                 {
804                   Solvable *s = pool->solvables + p;
805                   if (s->name != nid || s->evr != evrid)
806                     continue;
807                   if (arch && s->arch != arch)
808                     continue;
809                   return p;
810                 }
811             }
812         }
813     }
814   return 0;
815 }
816
817 const char *
818 testcase_job2str(Pool *pool, Id how, Id what)
819 {
820   char *ret;
821   const char *jobstr;
822   const char *selstr;
823   const char *pkgstr;
824   int i, o;
825   Id select = how & SOLVER_SELECTMASK;
826
827   for (i = 0; job2str[i].str; i++)
828     if ((how & SOLVER_JOBMASK) == job2str[i].job)
829       break;
830   jobstr = job2str[i].str ? job2str[i].str : "unknown";
831   if (select == SOLVER_SOLVABLE)
832     {
833       selstr = " pkg ";
834       pkgstr = testcase_solvid2str(pool, what);
835     }
836   else if (select == SOLVER_SOLVABLE_NAME)
837     {
838       selstr = " name ";
839       pkgstr = testcase_dep2str(pool, what);
840     }
841   else if (select == SOLVER_SOLVABLE_PROVIDES)
842     {
843       selstr = " provides ";
844       pkgstr = testcase_dep2str(pool, what);
845     }
846   else if (select == SOLVER_SOLVABLE_ONE_OF)
847     {
848       Id p;
849       selstr = " oneof ";
850       pkgstr = 0;
851       while ((p = pool->whatprovidesdata[what++]) != 0)
852         {
853           const char *s = testcase_solvid2str(pool, p);
854           if (pkgstr)
855             {
856               pkgstr = pool_tmpappend(pool, pkgstr, " ", s);
857               pool_freetmpspace(pool, s);
858             }
859           else
860             pkgstr = s;
861         }
862       if (!pkgstr)
863         pkgstr = "nothing";
864     }
865   else if (select == SOLVER_SOLVABLE_REPO)
866     {
867       Repo *repo = pool_id2repo(pool, what);
868       selstr = " repo ";
869       if (!repo->name)
870         {
871           char buf[20];
872           sprintf(buf, "#%d", repo->repoid);
873           pkgstr = pool_tmpjoin(pool, buf, 0, 0);
874         }
875       else
876         pkgstr = pool_tmpjoin(pool, repo->name, 0, 0);
877     }
878   else if (select == SOLVER_SOLVABLE_ALL)
879     {
880       selstr = " all ";
881       pkgstr = "packages";
882     }
883   else
884     {
885       selstr = " unknown ";
886       pkgstr = "unknown";
887     }
888   ret = pool_tmpjoin(pool, jobstr, selstr, pkgstr);
889   o = strlen(ret);
890   ret = pool_tmpappend(pool, ret, " ", 0);
891   for (i = 0; jobflags2str[i].str; i++)
892     if ((how & jobflags2str[i].flag) != 0)
893       ret = pool_tmpappend(pool, ret, ",", jobflags2str[i].str);
894   if (!ret[o + 1])
895     ret[o] = 0;
896   else
897     {
898       ret[o + 1] = '[';
899       ret = pool_tmpappend(pool, ret, "]", 0);
900     }
901   return ret;
902 }
903
904 static int
905 str2selflags(Pool *pool, char *s)       /* modifies the string! */
906 {
907   int i, selflags = 0;
908   while (s)
909     {
910       char *se = strchr(s, ',');
911       if (se)
912         *se++ = 0;
913       for (i = 0; selflags2str[i].str; i++)
914         if (!strcmp(s, selflags2str[i].str))
915           {
916             selflags |= selflags2str[i].flag;
917             break;
918           }
919       if (!selflags2str[i].str)
920         pool_debug(pool, SOLV_ERROR, "str2job: unknown selection flag '%s'\n", s);
921       s = se;
922     }
923   return selflags;
924 }
925
926 static int
927 str2jobflags(Pool *pool, char *s)       /* modifies the string */
928 {
929   int i, jobflags = 0;
930   while (s)
931     {
932       char *se = strchr(s, ',');
933       if (se)
934         *se++ = 0;
935       for (i = 0; jobflags2str[i].str; i++)
936         if (!strcmp(s, jobflags2str[i].str))
937           {
938             jobflags |= jobflags2str[i].flag;
939             break;
940           }
941       if (!jobflags2str[i].str)
942         pool_debug(pool, SOLV_ERROR, "str2job: unknown job flag '%s'\n", s);
943       s = se;
944     }
945   return jobflags;
946 }
947
948 Id
949 testcase_str2job(Pool *pool, const char *str, Id *whatp)
950 {
951   int i;
952   Id job;
953   Id what;
954   char *s;
955   char **pieces = 0;
956   int npieces = 0;
957
958   *whatp = 0;
959   /* so we can patch it */
960   s = pool_tmpjoin(pool, str, 0, 0);
961   /* split it in pieces */
962   for (;;)
963     {
964       while (*s == ' ' || *s == '\t')
965         s++;
966       if (!*s)
967         break;
968       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
969       pieces[npieces++] = s;
970       while (*s && *s != ' ' && *s != '\t')
971         s++;
972       if (*s)
973         *s++ = 0;
974     }
975   if (npieces < 3)
976     {
977       pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
978       solv_free(pieces);
979       return -1;
980     }
981
982   for (i = 0; job2str[i].str; i++)
983     if (!strcmp(pieces[0], job2str[i].str))
984       break;
985   if (!job2str[i].str)
986     {
987       pool_debug(pool, SOLV_ERROR, "str2job: unknown job '%s'\n", str);
988       solv_free(pieces);
989       return -1;
990     }
991   job = job2str[i].job;
992   what = 0;
993   if (npieces > 3)
994     {
995       char *flags = pieces[npieces - 1];
996       if (*flags == '[' && flags[strlen(flags) - 1] == ']')
997         {
998           npieces--;
999           flags++;
1000           flags[strlen(flags) - 1] = 0;
1001           job |= str2jobflags(pool, flags);
1002         }
1003     }
1004   if (!strcmp(pieces[1], "pkg"))
1005     {
1006       if (npieces != 3)
1007         {
1008           pool_debug(pool, SOLV_ERROR, "str2job: bad pkg selector in '%s'\n", str);
1009           solv_free(pieces);
1010           return -1;
1011         }
1012       job |= SOLVER_SOLVABLE;
1013       what = testcase_str2solvid(pool, pieces[2]);
1014       if (!what)
1015         {
1016           pool_debug(pool, SOLV_ERROR, "str2job: unknown package '%s'\n", pieces[2]);
1017           solv_free(pieces);
1018           return -1;
1019         }
1020     }
1021   else if (!strcmp(pieces[1], "name") || !strcmp(pieces[1], "provides"))
1022     {
1023       /* join em again for dep2str... */
1024       char *sp;
1025       for (sp = pieces[2]; sp < pieces[npieces - 1]; sp++)
1026         if (*sp == 0)
1027           *sp = ' ';
1028       what = 0;
1029       if (pieces[1][0] == 'p' && strncmp(pieces[2], "namespace:", 10) == 0)
1030         {
1031           char *spe = strchr(pieces[2], '(');
1032           int l = strlen(pieces[2]);
1033           if (spe && pieces[2][l - 1] == ')')
1034             {
1035               /* special namespace provides */
1036               if (strcmp(spe, "(<NULL>)") != 0)
1037                 {
1038                   pieces[2][l - 1] = 0;
1039                   what = testcase_str2dep(pool, spe + 1);
1040                   pieces[2][l - 1] = ')';
1041                 }
1042               what = pool_rel2id(pool, pool_strn2id(pool, pieces[2], spe - pieces[2], 1), what, REL_NAMESPACE, 1);
1043             }
1044         }
1045       if (!what)
1046         what = testcase_str2dep(pool, pieces[2]);
1047       if (pieces[1][0] == 'n')
1048         job |= SOLVER_SOLVABLE_NAME;
1049       else
1050         job |= SOLVER_SOLVABLE_PROVIDES;
1051     }
1052   else if (!strcmp(pieces[1], "oneof"))
1053     {
1054       Queue q;
1055       job |= SOLVER_SOLVABLE_ONE_OF;
1056       queue_init(&q);
1057       if (npieces > 3 && strcmp(pieces[2], "nothing") != 0)
1058         {
1059           for (i = 2; i < npieces; i++)
1060             {
1061               Id p = testcase_str2solvid(pool, pieces[i]);
1062               if (!p)
1063                 {
1064                   pool_debug(pool, SOLV_ERROR, "str2job: unknown package '%s'\n", pieces[i]);
1065                   queue_free(&q);
1066                   solv_free(pieces);
1067                   return -1;
1068                 }
1069               queue_push(&q, p);
1070             }
1071         }
1072       what = pool_queuetowhatprovides(pool, &q);
1073       queue_free(&q);
1074     }
1075   else if (!strcmp(pieces[1], "repo"))
1076     {
1077       Repo *repo;
1078       if (npieces != 3)
1079         {
1080           pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
1081           solv_free(pieces);
1082           return -1;
1083         }
1084       repo = testcase_str2repo(pool, pieces[2]);
1085       if (!repo)
1086         {
1087           pool_debug(pool, SOLV_ERROR, "str2job: unknown repo '%s'\n", pieces[2]);
1088           solv_free(pieces);
1089           return -1;
1090         }
1091       job |= SOLVER_SOLVABLE_REPO;
1092       what = repo->repoid;
1093     }
1094   else if (!strcmp(pieces[1], "all"))
1095     {
1096       if (npieces != 3 && strcmp(pieces[2], "packages") != 0)
1097         {
1098           pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
1099           solv_free(pieces);
1100           return -1;
1101         }
1102       job |= SOLVER_SOLVABLE_ALL;
1103       what = 0;
1104     }
1105   else
1106     {
1107       pool_debug(pool, SOLV_ERROR, "str2job: unknown selection in '%s'\n", str);
1108       solv_free(pieces);
1109       return -1;
1110     }
1111   *whatp = what;
1112   solv_free(pieces);
1113   return job;
1114 }
1115
1116 static int
1117 addselectionjob(Pool *pool, char **pieces, int npieces, Queue *jobqueue)
1118 {
1119   Id job;
1120   int i, r;
1121   int selflags;
1122   Queue sel;
1123
1124   for (i = 0; job2str[i].str; i++)
1125     if (!strcmp(pieces[0], job2str[i].str))
1126       break;
1127   if (!job2str[i].str)
1128     {
1129       pool_debug(pool, SOLV_ERROR, "selstr2job: unknown job '%s'\n", pieces[0]);
1130       return -1;
1131     }
1132   job = job2str[i].job;
1133   if (npieces > 3)
1134     {
1135       char *flags = pieces[npieces - 1];
1136       if (*flags == '[' && flags[strlen(flags) - 1] == ']')
1137         {
1138           npieces--;
1139           flags++;
1140           flags[strlen(flags) - 1] = 0;
1141           job |= str2jobflags(pool, flags);
1142         }
1143     }
1144   if (npieces < 4)
1145     {
1146       pool_debug(pool, SOLV_ERROR, "selstr2job: no selection flags\n");
1147       return -1;
1148     }
1149   selflags = str2selflags(pool, pieces[3]);
1150   queue_init(&sel);
1151   r = selection_make(pool, &sel, pieces[2], selflags);
1152   for (i = 0; i < sel.count; i += 2)
1153     queue_push2(jobqueue, job | sel.elements[i], sel.elements[i + 1]);
1154   queue_free(&sel);
1155   return r;
1156 }
1157
1158 static void
1159 writedeps(Repo *repo, FILE *fp, const char *tag, Id key, Solvable *s, Offset off)
1160 {
1161   Pool *pool = repo->pool;
1162   Id id, *dp;
1163   int tagwritten = 0;
1164   const char *idstr;
1165
1166   if (!off)
1167     return;
1168   dp = repo->idarraydata + off;
1169   while ((id = *dp++) != 0)
1170     {
1171       if (key == SOLVABLE_REQUIRES && id == SOLVABLE_PREREQMARKER)
1172         {
1173           if (tagwritten)
1174             fprintf(fp, "-%s\n", tag);
1175           tagwritten = 0;
1176           tag = "Prq:";
1177           continue;
1178         }
1179       if (key == SOLVABLE_PROVIDES && id == SOLVABLE_FILEMARKER)
1180         continue;
1181       idstr = testcase_dep2str(pool, id);
1182       if (!tagwritten)
1183         {
1184           fprintf(fp, "+%s\n", tag);
1185           tagwritten = 1;
1186         }
1187       fprintf(fp, "%s\n", idstr);
1188     }
1189   if (tagwritten)
1190     fprintf(fp, "-%s\n", tag);
1191 }
1192
1193 static void
1194 writefilelist(Repo *repo, FILE *fp, const char *tag, Solvable *s)
1195 {
1196   Pool *pool = repo->pool;
1197   int tagwritten = 0;
1198   Dataiterator di;
1199
1200   dataiterator_init(&di, pool, repo, s - pool->solvables, SOLVABLE_FILELIST, 0, 0);
1201   while (dataiterator_step(&di))
1202     {
1203       const char *s = repodata_dir2str(di.data, di.kv.id, di.kv.str);
1204       if (!tagwritten)
1205         {
1206           fprintf(fp, "+%s\n", tag);
1207           tagwritten = 1;
1208         }
1209       fprintf(fp, "%s\n", s);
1210     }
1211   if (tagwritten)
1212     fprintf(fp, "-%s\n", tag);
1213   dataiterator_free(&di);
1214 }
1215
1216 int
1217 testcase_write_testtags(Repo *repo, FILE *fp)
1218 {
1219   Pool *pool = repo->pool;
1220   Solvable *s;
1221   Id p;
1222   const char *name;
1223   const char *evr;
1224   const char *arch;
1225   const char *release;
1226   const char *tmp;
1227   unsigned int ti;
1228   Queue q;
1229
1230   fprintf(fp, "=Ver: 3.0\n");
1231   queue_init(&q);
1232   FOR_REPO_SOLVABLES(repo, p, s)
1233     {
1234       name = pool_id2str(pool, s->name);
1235       evr = pool_id2str(pool, s->evr);
1236       arch = pool_id2str(pool, s->arch);
1237       release = strrchr(evr, '-');
1238       if (!release)
1239         release = evr + strlen(evr);
1240       fprintf(fp, "=Pkg: %s %.*s %s %s\n", name, (int)(release - evr), evr, *release && release[1] ? release + 1 : "-", arch);
1241       tmp = solvable_lookup_str(s, SOLVABLE_SUMMARY);
1242       if (tmp)
1243         fprintf(fp, "=Sum: %s\n", tmp);
1244       writedeps(repo, fp, "Req:", SOLVABLE_REQUIRES, s, s->requires);
1245       writedeps(repo, fp, "Prv:", SOLVABLE_PROVIDES, s, s->provides);
1246       writedeps(repo, fp, "Obs:", SOLVABLE_OBSOLETES, s, s->obsoletes);
1247       writedeps(repo, fp, "Con:", SOLVABLE_CONFLICTS, s, s->conflicts);
1248       writedeps(repo, fp, "Rec:", SOLVABLE_RECOMMENDS, s, s->recommends);
1249       writedeps(repo, fp, "Sup:", SOLVABLE_SUPPLEMENTS, s, s->supplements);
1250       writedeps(repo, fp, "Sug:", SOLVABLE_SUGGESTS, s, s->suggests);
1251       writedeps(repo, fp, "Enh:", SOLVABLE_ENHANCES, s, s->enhances);
1252       if (solvable_lookup_idarray(s, SOLVABLE_PREREQ_IGNOREINST, &q))
1253         {
1254           int i;
1255           fprintf(fp, "+Ipr:\n");
1256           for (i = 0; i < q.count; i++)
1257             fprintf(fp, "%s\n", testcase_dep2str(pool, q.elements[i]));
1258           fprintf(fp, "-Ipr:\n");
1259         }
1260       if (s->vendor)
1261         fprintf(fp, "=Vnd: %s\n", pool_id2str(pool, s->vendor));
1262       ti = solvable_lookup_num(s, SOLVABLE_BUILDTIME, 0);
1263       if (ti)
1264         fprintf(fp, "=Tim: %u\n", ti);
1265       writefilelist(repo, fp, "Fls:", s);
1266     }
1267   queue_free(&q);
1268   return 0;
1269 }
1270
1271 static inline Offset
1272 adddep(Repo *repo, Offset olddeps, char *str, Id marker)
1273 {
1274   Id id = *str == '/' ? pool_str2id(repo->pool, str, 1) : testcase_str2dep(repo->pool, str);
1275   return repo_addid_dep(repo, olddeps, id, marker);
1276 }
1277
1278 static void
1279 finish_v2_solvable(Pool *pool, Repodata *data, Solvable *s, char *filelist, int nfilelist)
1280 {
1281   if (nfilelist)
1282     {
1283       int l;
1284       Id did;
1285       for (l = 0; l < nfilelist; l += strlen(filelist + l) + 1)
1286         {
1287           char *p = strrchr(filelist + l, '/');
1288           if (!p)
1289             continue;
1290           *p++ = 0;
1291           did = repodata_str2dir(data, filelist + l, 1);
1292           p[-1] = '/';
1293           if (!did)
1294             did = repodata_str2dir(data, "/", 1);
1295           repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, did, p);
1296         }
1297     }
1298   repo_rewrite_suse_deps(s, 0);
1299 }
1300
1301 /* stripped down version of susetags parser used for testcases */
1302 int
1303 testcase_add_testtags(Repo *repo, FILE *fp, int flags)
1304 {
1305   Pool *pool = repo->pool;
1306   char *line, *linep;
1307   int aline;
1308   int tag;
1309   Repodata *data;
1310   Solvable *s;
1311   char *sp[5];
1312   unsigned int t;
1313   int intag;
1314   char *filelist = 0;
1315   int afilelist = 0;
1316   int nfilelist = 0;
1317   int tagsversion = 0;
1318   int addselfprovides = 1;      /* for compat reasons */
1319
1320   data = repo_add_repodata(repo, flags);
1321   s = 0;
1322   intag = 0;
1323
1324   aline = 1024;
1325   line = solv_malloc(aline);
1326   linep = line;
1327   for (;;)
1328     {
1329       if (linep - line + 16 > aline)
1330         {
1331           aline = linep - line;
1332           line = solv_realloc(line, aline + 512);
1333           linep = line + aline;
1334           aline += 512;
1335         }
1336       if (!fgets(linep, aline - (linep - line), fp))
1337         break;
1338       linep += strlen(linep);
1339       if (linep == line || linep[-1] != '\n')
1340         continue;
1341       linep[-1] = 0;
1342       linep = line + intag;
1343       if (intag)
1344         {
1345           if (line[intag] == '-' && !strncmp(line + 1, line + intag + 1, intag - 2))
1346             {
1347               intag = 0;
1348               linep = line;
1349               continue;
1350             }
1351         }
1352       else if (line[0] == '+' && line[1] && line[1] != ':')
1353         {
1354           char *tagend = strchr(line, ':');
1355           if (!tagend)
1356             continue;
1357           line[0] = '=';
1358           tagend[1] = ' ';
1359           intag = tagend + 2 - line;
1360           linep = line + intag;
1361           continue;
1362         }
1363       if (*line != '=' || !line[1] || !line[2] || !line[3] || line[4] != ':')
1364         continue;
1365       tag = line[1] << 16 | line[2] << 8 | line[3];
1366       switch(tag)
1367         {
1368         case 'V' << 16 | 'e' << 8 | 'r':
1369           tagsversion = atoi(line + 6);
1370           addselfprovides = tagsversion < 3 || strstr(line + 6, "addselfprovides") != 0;
1371           break;
1372         case 'P' << 16 | 'k' << 8 | 'g':
1373           if (s)
1374             {
1375               if (tagsversion == 2)
1376                 finish_v2_solvable(pool, data, s, filelist, nfilelist);
1377               if (addselfprovides && s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
1378                 s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
1379             }
1380           nfilelist = 0;
1381           if (split(line + 5, sp, 5) != 4)
1382             break;
1383           s = pool_id2solvable(pool, repo_add_solvable(repo));
1384           s->name = pool_str2id(pool, sp[0], 1);
1385           /* join back version and release */
1386           if (sp[2] && !(sp[2][0] == '-' && !sp[2][1]))
1387             sp[2][-1] = '-';
1388           s->evr = makeevr(pool, sp[1]);
1389           s->arch = pool_str2id(pool, sp[3], 1);
1390           break;
1391         case 'S' << 16 | 'u' << 8 | 'm':
1392           repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line + 6);
1393           break;
1394         case 'V' << 16 | 'n' << 8 | 'd':
1395           s->vendor = pool_str2id(pool, line + 6, 1);
1396           break;
1397         case 'T' << 16 | 'i' << 8 | 'm':
1398           t = atoi(line + 6);
1399           if (t)
1400             repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, t);
1401           break;
1402         case 'R' << 16 | 'e' << 8 | 'q':
1403           s->requires = adddep(repo, s->requires, line + 6, -SOLVABLE_PREREQMARKER);
1404           break;
1405         case 'P' << 16 | 'r' << 8 | 'q':
1406           s->requires = adddep(repo, s->requires, line + 6, SOLVABLE_PREREQMARKER);
1407           break;
1408         case 'P' << 16 | 'r' << 8 | 'v':
1409           /* version 2 had the file list at the end of the provides */
1410           if (tagsversion == 2)
1411             {
1412               if (line[6] == '/')
1413                 {
1414                   int l = strlen(line + 6) + 1;
1415                   if (nfilelist + l > afilelist)
1416                     {
1417                       afilelist = nfilelist + l + 512;
1418                       filelist = solv_realloc(filelist, afilelist);
1419                     }
1420                   memcpy(filelist + nfilelist, line + 6, l);
1421                   nfilelist += l;
1422                   break;
1423                 }
1424               if (nfilelist)
1425                 {
1426                   int l;
1427                   for (l = 0; l < nfilelist; l += strlen(filelist + l) + 1)
1428                     s->provides = repo_addid_dep(repo, s->provides, pool_str2id(pool, filelist + l, 1), 0);
1429                   nfilelist = 0;
1430                 }
1431             }
1432           s->provides = adddep(repo, s->provides, line + 6, 0);
1433           break;
1434         case 'F' << 16 | 'l' << 8 | 's':
1435           {
1436             char *p = strrchr(line + 6, '/');
1437             Id did;
1438             if (!p)
1439               break;
1440             *p++ = 0;
1441             did = repodata_str2dir(data, line + 6, 1);
1442             if (!did)
1443               did = repodata_str2dir(data, "/", 1);
1444             repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, did, p);
1445             break;
1446           }
1447         case 'O' << 16 | 'b' << 8 | 's':
1448           s->obsoletes = adddep(repo, s->obsoletes, line + 6, 0);
1449           break;
1450         case 'C' << 16 | 'o' << 8 | 'n':
1451           s->conflicts = adddep(repo, s->conflicts, line + 6, 0);
1452           break;
1453         case 'R' << 16 | 'e' << 8 | 'c':
1454           s->recommends = adddep(repo, s->recommends, line + 6, 0);
1455           break;
1456         case 'S' << 16 | 'u' << 8 | 'p':
1457           s->supplements = adddep(repo, s->supplements, line + 6, 0);
1458           break;
1459         case 'S' << 16 | 'u' << 8 | 'g':
1460           s->suggests = adddep(repo, s->suggests, line + 6, 0);
1461           break;
1462         case 'E' << 16 | 'n' << 8 | 'h':
1463           s->enhances = adddep(repo, s->enhances, line + 6, 0);
1464           break;
1465         case 'I' << 16 | 'p' << 8 | 'r':
1466           {
1467             Id id = line[6] == '/' ? pool_str2id(pool, line + 6, 1) : testcase_str2dep(pool, line + 6);
1468             repodata_add_idarray(data, s - pool->solvables, SOLVABLE_PREREQ_IGNOREINST, id);
1469             break;
1470           }
1471         default:
1472           break;
1473         }
1474     }
1475   if (s)
1476     {
1477       if (tagsversion == 2)
1478         finish_v2_solvable(pool, data, s, filelist, nfilelist);
1479       if (addselfprovides && s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
1480         s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
1481     }
1482   solv_free(line);
1483   solv_free(filelist);
1484   repodata_free_dircache(data);
1485   if (!(flags & REPO_NO_INTERNALIZE))
1486     repodata_internalize(data);
1487   return 0;
1488 }
1489
1490 const char *
1491 testcase_getpoolflags(Pool *pool)
1492 {
1493   const char *str = 0;
1494   int i, v;
1495   for (i = 0; poolflags2str[i].str; i++)
1496     {
1497       v = pool_get_flag(pool, poolflags2str[i].flag);
1498       if (v == poolflags2str[i].def)
1499         continue;
1500       str = pool_tmpappend(pool, str, v ? " " : " !", poolflags2str[i].str);
1501     }
1502   return str ? str + 1 : "";
1503 }
1504
1505 int
1506 testcase_setpoolflags(Pool *pool, const char *str)
1507 {
1508   const char *p = str, *s;
1509   int i, v;
1510   for (;;)
1511     {
1512       while (*p == ' ' || *p == '\t' || *p == ',')
1513         p++;
1514       v = 1;
1515       if (*p == '!')
1516         {
1517           p++;
1518           v = 0;
1519         }
1520       if (!*p)
1521         break;
1522       s = p;
1523       while (*p && *p != ' ' && *p != '\t' && *p != ',')
1524         p++;
1525       for (i = 0; poolflags2str[i].str; i++)
1526         if (!strncmp(poolflags2str[i].str, s, p - s) && poolflags2str[i].str[p - s] == 0)
1527           break;
1528       if (!poolflags2str[i].str)
1529         {
1530           pool_debug(pool, SOLV_ERROR, "setpoolflags: unknown flag '%.*s'\n", (int)(p - s), s);
1531           return 0;
1532         }
1533       pool_set_flag(pool, poolflags2str[i].flag, v);
1534     }
1535   return 1;
1536 }
1537
1538 void
1539 testcase_resetpoolflags(Pool *pool)
1540 {
1541   int i;
1542   for (i = 0; poolflags2str[i].str; i++)
1543     pool_set_flag(pool, poolflags2str[i].flag, poolflags2str[i].def);
1544 }
1545
1546 const char *
1547 testcase_getsolverflags(Solver *solv)
1548 {
1549   Pool *pool = solv->pool;
1550   const char *str = 0;
1551   int i, v;
1552   for (i = 0; solverflags2str[i].str; i++)
1553     {
1554       v = solver_get_flag(solv, solverflags2str[i].flag);
1555       if (v == solverflags2str[i].def)
1556         continue;
1557       str = pool_tmpappend(pool, str, v ? " " : " !", solverflags2str[i].str);
1558     }
1559   return str ? str + 1 : "";
1560 }
1561
1562 int
1563 testcase_setsolverflags(Solver *solv, const char *str)
1564 {
1565   const char *p = str, *s;
1566   int i, v;
1567   for (;;)
1568     {
1569       while (*p == ' ' || *p == '\t' || *p == ',')
1570         p++;
1571       v = 1;
1572       if (*p == '!')
1573         {
1574           p++;
1575           v = 0;
1576         }
1577       if (!*p)
1578         break;
1579       s = p;
1580       while (*p && *p != ' ' && *p != '\t' && *p != ',')
1581         p++;
1582       for (i = 0; solverflags2str[i].str; i++)
1583         if (!strncmp(solverflags2str[i].str, s, p - s) && solverflags2str[i].str[p - s] == 0)
1584           break;
1585       if (!solverflags2str[i].str)
1586         {
1587           pool_debug(solv->pool, SOLV_ERROR, "setsolverflags: unknown flag '%.*s'\n", (int)(p - s), s);
1588           return 0;
1589         }
1590       if (solver_set_flag(solv, solverflags2str[i].flag, v) == -1)
1591         {
1592           pool_debug(solv->pool, SOLV_ERROR, "setsolverflags: unsupported flag '%s'\n", solverflags2str[i].str);
1593           return 0;
1594         }
1595     }
1596   return 1;
1597 }
1598
1599 void
1600 testcase_resetsolverflags(Solver *solv)
1601 {
1602   int i;
1603   for (i = 0; solverflags2str[i].str; i++)
1604     solver_set_flag(solv, solverflags2str[i].flag, solverflags2str[i].def);
1605 }
1606
1607 static const char *
1608 testcase_ruleid(Solver *solv, Id rid)
1609 {
1610   Strqueue sq;
1611   Queue q;
1612   int i;
1613   Chksum *chk;
1614   const unsigned char *md5;
1615   int md5l;
1616   const char *s;
1617
1618   queue_init(&q);
1619   strqueue_init(&sq);
1620   solver_ruleliterals(solv, rid, &q);
1621   for (i = 0; i < q.count; i++)
1622     {
1623       Id p = q.elements[i];
1624       s = testcase_solvid2str(solv->pool, p > 0 ? p : -p);
1625       if (p < 0)
1626         s = pool_tmpjoin(solv->pool, "!", s, 0);
1627       strqueue_push(&sq, s);
1628     }
1629   queue_free(&q);
1630   strqueue_sort_u(&sq);
1631   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1632   for (i = 0; i < sq.nstr; i++)
1633     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1634   md5 = solv_chksum_get(chk, &md5l);
1635   s = pool_bin2hex(solv->pool, md5, md5l);
1636   chk = solv_chksum_free(chk, 0);
1637   strqueue_free(&sq);
1638   return s;
1639 }
1640
1641 static const char *
1642 testcase_problemid(Solver *solv, Id problem)
1643 {
1644   Strqueue sq;
1645   Queue q;
1646   Chksum *chk;
1647   const unsigned char *md5;
1648   int i, md5l;
1649   const char *s;
1650
1651   /* we build a hash of all rules that define the problem */
1652   queue_init(&q);
1653   strqueue_init(&sq);
1654   solver_findallproblemrules(solv, problem, &q);
1655   for (i = 0; i < q.count; i++)
1656     strqueue_push(&sq, testcase_ruleid(solv, q.elements[i]));
1657   queue_free(&q);
1658   strqueue_sort_u(&sq);
1659   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1660   for (i = 0; i < sq.nstr; i++)
1661     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1662   md5 = solv_chksum_get(chk, &md5l);
1663   s = pool_bin2hex(solv->pool, md5, 4);
1664   chk = solv_chksum_free(chk, 0);
1665   strqueue_free(&sq);
1666   return s;
1667 }
1668
1669 static const char *
1670 testcase_solutionid(Solver *solv, Id problem, Id solution)
1671 {
1672   Id intid;
1673   Chksum *chk;
1674   const unsigned char *md5;
1675   int md5l;
1676   const char *s;
1677
1678   intid = solver_solutionelement_internalid(solv, problem, solution);
1679   /* internal stuff! handle with care! */
1680   if (intid < 0)
1681     {
1682       /* it's a job */
1683       s = testcase_job2str(solv->pool, solv->job.elements[-intid - 1], solv->job.elements[-intid]);
1684     }
1685   else
1686     {
1687       /* it's a rule */
1688       s = testcase_ruleid(solv, intid);
1689     }
1690   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1691   solv_chksum_add(chk, s, strlen(s) + 1);
1692   md5 = solv_chksum_get(chk, &md5l);
1693   s = pool_bin2hex(solv->pool, md5, 4);
1694   chk = solv_chksum_free(chk, 0);
1695   return s;
1696 }
1697
1698 static const char *
1699 testcase_alternativeid(Solver *solv, int type, Id id, Id from)
1700 {
1701   const char *s;
1702   Pool *pool = solv->pool;
1703   Chksum *chk;
1704   const unsigned char *md5;
1705   int md5l;
1706   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1707   if (type == SOLVER_ALTERNATIVE_TYPE_RECOMMENDS)
1708     {
1709       s = testcase_solvid2str(pool, from);
1710       solv_chksum_add(chk, s, strlen(s) + 1);
1711       s = testcase_dep2str(pool, id);
1712       solv_chksum_add(chk, s, strlen(s) + 1);
1713     }
1714   else if (type == SOLVER_ALTERNATIVE_TYPE_RULE)
1715     {
1716       s = testcase_ruleid(solv, id);
1717       solv_chksum_add(chk, s, strlen(s) + 1);
1718     }
1719   md5 = solv_chksum_get(chk, &md5l);
1720   s = pool_bin2hex(pool, md5, 4);
1721   chk = solv_chksum_free(chk, 0);
1722   return s;
1723 }
1724
1725 static struct class2str {
1726   Id class;
1727   const char *str;
1728 } class2str[] = {
1729   { SOLVER_TRANSACTION_ERASE,          "erase" },
1730   { SOLVER_TRANSACTION_INSTALL,        "install" },
1731   { SOLVER_TRANSACTION_REINSTALLED,    "reinstall" },
1732   { SOLVER_TRANSACTION_DOWNGRADED,     "downgrade" },
1733   { SOLVER_TRANSACTION_CHANGED,        "change" },
1734   { SOLVER_TRANSACTION_UPGRADED,       "upgrade" },
1735   { SOLVER_TRANSACTION_OBSOLETED,      "obsolete" },
1736   { SOLVER_TRANSACTION_MULTIINSTALL,   "multiinstall" },
1737   { SOLVER_TRANSACTION_MULTIREINSTALL, "multireinstall" },
1738   { 0, 0 }
1739 };
1740
1741 static struct reason2str {
1742   Id reason;
1743   const char *str;
1744 } reason2str[] = {
1745   { SOLVER_REASON_UNRELATED,            "unrelated" },
1746   { SOLVER_REASON_UNIT_RULE,            "unit" },
1747   { SOLVER_REASON_KEEP_INSTALLED,       "keep" },
1748   { SOLVER_REASON_RESOLVE_JOB,          "job" },
1749   { SOLVER_REASON_UPDATE_INSTALLED,     "update" },
1750   { SOLVER_REASON_CLEANDEPS_ERASE,      "cleandeps" },
1751   { SOLVER_REASON_RESOLVE,              "resolve" },
1752   { SOLVER_REASON_WEAKDEP,              "weakdep" },
1753   { SOLVER_REASON_RESOLVE_ORPHAN,       "orphan" },
1754
1755   { SOLVER_REASON_RECOMMENDED,          "recommended" },
1756   { SOLVER_REASON_SUPPLEMENTED,         "supplemented" },
1757   { 0, 0 }
1758 };
1759
1760 static const char *
1761 testcase_reason2str(Id reason)
1762 {
1763   int i;
1764   for (i = 0; reason2str[i].str; i++)
1765     if (reason == reason2str[i].reason)
1766       return reason2str[i].str;
1767   return "?";
1768 }
1769
1770 static struct rclass2str {
1771   Id rclass;
1772   const char *str;
1773 } rclass2str[] = {
1774   { SOLVER_RULE_PKG, "pkg" },
1775   { SOLVER_RULE_UPDATE, "update" },
1776   { SOLVER_RULE_FEATURE, "feature" },
1777   { SOLVER_RULE_JOB, "job" },
1778   { SOLVER_RULE_DISTUPGRADE, "distupgrade" },
1779   { SOLVER_RULE_INFARCH, "infarch" },
1780   { SOLVER_RULE_CHOICE, "choice" },
1781   { SOLVER_RULE_LEARNT, "learnt" },
1782   { SOLVER_RULE_BEST, "best" },
1783   { SOLVER_RULE_YUMOBS, "yumobs" },
1784   { 0, 0 }
1785 };
1786
1787 static const char *
1788 testcase_rclass2str(Id rclass)
1789 {
1790   int i;
1791   for (i = 0; rclass2str[i].str; i++)
1792     if (rclass == rclass2str[i].rclass)
1793       return rclass2str[i].str;
1794   return "unknown";
1795 }
1796
1797 static int
1798 dump_genid(Pool *pool, Strqueue *sq, Id id, int cnt)
1799 {
1800   struct oplist *op;
1801   char cntbuf[20];
1802   const char *s;
1803
1804   if (ISRELDEP(id))
1805     {
1806       Reldep *rd = GETRELDEP(pool, id);
1807       for (op = oplist; op->flags; op++)
1808         if (rd->flags == op->flags)
1809           break;
1810       cnt = dump_genid(pool, sq, rd->name, cnt);
1811       cnt = dump_genid(pool, sq, rd->evr, cnt);
1812       sprintf(cntbuf, "genid %2d: genid ", cnt++);
1813       s = pool_tmpjoin(pool, cntbuf, "op ", op->flags ? op->opname : "unknown");
1814     }
1815   else
1816     {
1817       sprintf(cntbuf, "genid %2d: genid ", cnt++);
1818       s = pool_tmpjoin(pool, cntbuf, id ? "lit " : "null", id ? pool_id2str(pool, id) : 0);
1819     }
1820   strqueue_push(sq, s);
1821   return cnt;
1822 }
1823
1824 char *
1825 testcase_solverresult(Solver *solv, int resultflags)
1826 {
1827   Pool *pool = solv->pool;
1828   int i, j;
1829   Id p, op;
1830   const char *s;
1831   char *result;
1832   Strqueue sq;
1833
1834   strqueue_init(&sq);
1835   if ((resultflags & TESTCASE_RESULT_TRANSACTION) != 0)
1836     {
1837       Transaction *trans = solver_create_transaction(solv);
1838       Queue q;
1839
1840       queue_init(&q);
1841       for (i = 0; class2str[i].str; i++)
1842         {
1843           queue_empty(&q);
1844           transaction_classify_pkgs(trans, SOLVER_TRANSACTION_KEEP_PSEUDO, class2str[i].class, 0, 0, &q);
1845           for (j = 0; j < q.count; j++)
1846             {
1847               p = q.elements[j];
1848               op = 0;
1849               if (pool->installed && pool->solvables[p].repo == pool->installed)
1850                 op = transaction_obs_pkg(trans, p);
1851               s = pool_tmpjoin(pool, class2str[i].str, " ", testcase_solvid2str(pool, p));
1852               if (op)
1853                 s = pool_tmpjoin(pool, s, " ", testcase_solvid2str(pool, op));
1854               strqueue_push(&sq, s);
1855             }
1856         }
1857       queue_free(&q);
1858       transaction_free(trans);
1859     }
1860   if ((resultflags & TESTCASE_RESULT_PROBLEMS) != 0)
1861     {
1862       char *probprefix, *solprefix;
1863       int problem, solution, element;
1864       int pcnt, scnt;
1865
1866       pcnt = solver_problem_count(solv);
1867       for (problem = 1; problem <= pcnt; problem++)
1868         {
1869           Id rid, from, to, dep;
1870           SolverRuleinfo rinfo;
1871           rid = solver_findproblemrule(solv, problem);
1872           s = testcase_problemid(solv, problem);
1873           probprefix = solv_dupjoin("problem ", s, 0);
1874           rinfo = solver_ruleinfo(solv, rid, &from, &to, &dep);
1875           s = pool_tmpjoin(pool, probprefix, " info ", solver_problemruleinfo2str(solv, rinfo, from, to, dep));
1876           strqueue_push(&sq, s);
1877           scnt = solver_solution_count(solv, problem);
1878           for (solution = 1; solution <= scnt; solution++)
1879             {
1880               s = testcase_solutionid(solv, problem, solution);
1881               solprefix = solv_dupjoin(probprefix, " solution ", s);
1882               element = 0;
1883               while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &op)) != 0)
1884                 {
1885                   if (p == SOLVER_SOLUTION_JOB)
1886                     s = pool_tmpjoin(pool, solprefix, " deljob ", testcase_job2str(pool, solv->job.elements[op - 1], solv->job.elements[op]));
1887                   else if (p > 0 && op == 0)
1888                     s = pool_tmpjoin(pool, solprefix, " erase ", testcase_solvid2str(pool, p));
1889                   else if (p > 0 && op > 0)
1890                     {
1891                       s = pool_tmpjoin(pool, solprefix, " replace ", testcase_solvid2str(pool, p));
1892                       s = pool_tmpappend(pool, s, " ", testcase_solvid2str(pool, op));
1893                     }
1894                   else if (p < 0 && op > 0)
1895                     s = pool_tmpjoin(pool, solprefix, " allow ", testcase_solvid2str(pool, op));
1896                   else
1897                     s = pool_tmpjoin(pool, solprefix, " unknown", 0);
1898                   strqueue_push(&sq, s);
1899                 }
1900               solv_free(solprefix);
1901             }
1902           solv_free(probprefix);
1903         }
1904     }
1905
1906   if ((resultflags & TESTCASE_RESULT_ORPHANED) != 0)
1907     {
1908       Queue q;
1909
1910       queue_init(&q);
1911       solver_get_orphaned(solv, &q);
1912       for (i = 0; i < q.count; i++)
1913         {
1914           s = pool_tmpjoin(pool, "orphaned ", testcase_solvid2str(pool, q.elements[i]), 0);
1915           strqueue_push(&sq, s);
1916         }
1917       queue_free(&q);
1918     }
1919
1920   if ((resultflags & TESTCASE_RESULT_RECOMMENDED) != 0)
1921     {
1922       Queue qr, qs;
1923
1924       queue_init(&qr);
1925       queue_init(&qs);
1926       solver_get_recommendations(solv, &qr, &qs, 0);
1927       for (i = 0; i < qr.count; i++)
1928         {
1929           s = pool_tmpjoin(pool, "recommended ", testcase_solvid2str(pool, qr.elements[i]), 0);
1930           strqueue_push(&sq, s);
1931         }
1932       for (i = 0; i < qs.count; i++)
1933         {
1934           s = pool_tmpjoin(pool, "suggested ", testcase_solvid2str(pool, qs.elements[i]), 0);
1935           strqueue_push(&sq, s);
1936         }
1937       queue_free(&qr);
1938       queue_free(&qs);
1939     }
1940
1941   if ((resultflags & TESTCASE_RESULT_UNNEEDED) != 0)
1942     {
1943       Queue q, qf;
1944
1945       queue_init(&q);
1946       queue_init(&qf);
1947       solver_get_unneeded(solv, &q, 0);
1948       solver_get_unneeded(solv, &qf, 1);
1949       for (i = j = 0; i < q.count; i++)
1950         {
1951           /* we rely on qf containing a subset of q in the same order */
1952           if (j < qf.count && q.elements[i] == qf.elements[j])
1953             {
1954               s = pool_tmpjoin(pool, "unneeded_filtered ", testcase_solvid2str(pool, q.elements[i]), 0);
1955               j++;
1956             }
1957           else
1958             s = pool_tmpjoin(pool, "unneeded ", testcase_solvid2str(pool, q.elements[i]), 0);
1959           strqueue_push(&sq, s);
1960         }
1961       queue_free(&q);
1962       queue_free(&qf);
1963     }
1964   if ((resultflags & TESTCASE_RESULT_ALTERNATIVES) != 0)
1965     {
1966       char *altprefix;
1967       Queue q, rq;
1968       int cnt;
1969       Id alternative;
1970       queue_init(&q);
1971       queue_init(&rq);
1972       cnt = solver_alternatives_count(solv);
1973       for (alternative = 1; alternative <= cnt; alternative++)
1974         {
1975           Id id, from, chosen;
1976           char num[20];
1977           int type = solver_get_alternative(solv, alternative, &id, &from, &chosen, &q, 0);
1978           altprefix = solv_dupjoin("alternative ", testcase_alternativeid(solv, type, id, from), " ");
1979           strcpy(num, " 0 ");
1980           if (type == SOLVER_ALTERNATIVE_TYPE_RECOMMENDS)
1981             {
1982               char *s = pool_tmpjoin(pool, altprefix, num, testcase_solvid2str(pool, from));
1983               s = pool_tmpappend(pool, s, " recommends ", testcase_dep2str(pool, id));
1984               strqueue_push(&sq, s);
1985             }
1986           else if (type == SOLVER_ALTERNATIVE_TYPE_RULE)
1987             {
1988               /* map choice rules back to pkg rules */
1989               if (solver_ruleclass(solv, id) == SOLVER_RULE_CHOICE)
1990                 id = solver_rule2pkgrule(solv, id);
1991               solver_allruleinfos(solv, id, &rq);
1992               for (i = 0; i < rq.count; i += 4)
1993                 {
1994                   int rtype = rq.elements[i];
1995                   if ((rtype & SOLVER_RULE_TYPEMASK) == SOLVER_RULE_JOB)
1996                     {
1997                       const char *js = testcase_job2str(pool, rq.elements[i + 2], rq.elements[i + 3]);
1998                       char *s = pool_tmpjoin(pool, altprefix, num, " job ");
1999                       s = pool_tmpappend(pool, s, js, 0);
2000                       strqueue_push(&sq, s);
2001                     }
2002                   else if (rtype == SOLVER_RULE_PKG_REQUIRES)
2003                     {
2004                       char *s = pool_tmpjoin(pool, altprefix, num, testcase_solvid2str(pool, rq.elements[i + 1]));
2005                       s = pool_tmpappend(pool, s, " requires ", testcase_dep2str(pool, rq.elements[i + 3]));
2006                       strqueue_push(&sq, s);
2007                     }
2008                 }
2009             }
2010           for (i = 0; i < q.count; i++)
2011             {
2012               Id p = q.elements[i];
2013               if (i >= 9)
2014                 num[0] = '0' + (i + 1) / 10;
2015               num[1] = '0' + (i + 1) % 10;
2016               if (-p == chosen)
2017                 s = pool_tmpjoin(pool, altprefix, num, "+ ");
2018               else if (p < 0)
2019                 s = pool_tmpjoin(pool, altprefix, num, "- ");
2020               else if (p >= 0)
2021                 s = pool_tmpjoin(pool, altprefix, num, "  ");
2022               s = pool_tmpappend(pool, s,  testcase_solvid2str(pool, p < 0 ? -p : p), 0);
2023               strqueue_push(&sq, s);
2024             }
2025           solv_free(altprefix);
2026         }
2027       queue_free(&q);
2028       queue_free(&rq);
2029     }
2030   if ((resultflags & TESTCASE_RESULT_RULES) != 0)
2031     {
2032       /* dump all rules */
2033       Id rid;
2034       SolverRuleinfo rclass;
2035       Queue q;
2036       int i;
2037
2038       queue_init(&q);
2039       for (rid = 1; (rclass = solver_ruleclass(solv, rid)) != SOLVER_RULE_UNKNOWN; rid++)
2040         {
2041           char *prefix = solv_dupjoin("rule ", testcase_rclass2str(rclass), " ");
2042           prefix = solv_dupappend(prefix, testcase_ruleid(solv, rid), 0);
2043           solver_ruleliterals(solv, rid, &q);
2044           if (rclass == SOLVER_RULE_FEATURE && q.count == 1 && q.elements[0] == -SYSTEMSOLVABLE)
2045             continue;
2046           for (i = 0; i < q.count; i++)
2047             {
2048               Id p = q.elements[i];
2049               const char *s;
2050               if (p < 0)
2051                 s = pool_tmpjoin(pool, prefix, " -", testcase_solvid2str(pool, -p));
2052               else
2053                 s = pool_tmpjoin(pool, prefix, "  ", testcase_solvid2str(pool, p));
2054               strqueue_push(&sq, s);
2055             }
2056           solv_free(prefix);
2057         }
2058       queue_free(&q);
2059     }
2060   if ((resultflags & TESTCASE_RESULT_GENID) != 0)
2061     {
2062       for (i = 0 ; i < solv->job.count; i += 2)
2063         {
2064           Id id, id2;
2065           if (solv->job.elements[i] != (SOLVER_NOOP | SOLVER_SOLVABLE_PROVIDES))
2066             continue;
2067           id = solv->job.elements[i + 1];
2068           s = testcase_dep2str(pool, id);
2069           strqueue_push(&sq, pool_tmpjoin(pool, "genid dep ", s, 0));
2070           if ((id2 = testcase_str2dep(pool, s)) != id)
2071             {
2072               s = pool_tmpjoin(pool, "genid roundtrip error: ", testcase_dep2str(pool, id2), 0);
2073               strqueue_push(&sq, s);
2074             }
2075           dump_genid(pool, &sq, id, 1);
2076         }
2077     }
2078   if ((resultflags & TESTCASE_RESULT_REASON) != 0)
2079     {
2080       Queue whyq;
2081       queue_init(&whyq);
2082       FOR_POOL_SOLVABLES(p)
2083         {
2084           Id info, p2, id;
2085           int reason = solver_describe_decision(solv, p, &info);
2086           if (reason == SOLVER_REASON_UNRELATED)
2087             continue;
2088           if (reason == SOLVER_REASON_WEAKDEP)
2089             {
2090               solver_describe_weakdep_decision(solv, p, &whyq);
2091               if (whyq.count)
2092                 {
2093                   for (i = 0; i < whyq.count; i += 3)
2094                     {
2095                       reason = whyq.elements[i];
2096                       p2 = whyq.elements[i + 1];
2097                       id = whyq.elements[i + 2];
2098                       s = pool_tmpjoin(pool, "reason ", testcase_solvid2str(pool, p), 0);
2099                       s = pool_tmpappend(pool, s, " ", testcase_reason2str(reason));
2100                       s = pool_tmpappend(pool, s, " ", testcase_dep2str(pool, id));
2101                       if (p2)
2102                         s = pool_tmpappend(pool, s, " ", testcase_solvid2str(pool, p2));
2103                       strqueue_push(&sq, s);
2104                     }
2105                   continue;
2106                 }
2107             }
2108           s = pool_tmpjoin(pool, "reason ", testcase_solvid2str(pool, p), 0);
2109           s = pool_tmpappend(pool, s, " ", testcase_reason2str(reason));
2110           if (info)
2111             s = pool_tmpappend(pool, s, " ", testcase_ruleid(solv, info));
2112           strqueue_push(&sq, s);
2113         }
2114       queue_free(&whyq);
2115     }
2116   strqueue_sort(&sq);
2117   result = strqueue_join(&sq);
2118   strqueue_free(&sq);
2119   return result;
2120 }
2121
2122
2123 static int
2124 testcase_write_mangled(Solver *solv, const char *dir, int resultflags, const char *testcasename, const char *resultname)
2125 {
2126   Pool *pool = solv->pool;
2127   Repo *repo;
2128   int i;
2129   Id arch, repoid;
2130   Id lowscore;
2131   FILE *fp;
2132   Strqueue sq;
2133   char *cmd, *out;
2134   const char *s;
2135
2136   if (!testcasename)
2137     testcasename = "testcase.t";
2138   if (!resultname)
2139     resultname = "solver.result";
2140
2141   if (mkdir(dir, 0777) && errno != EEXIST)
2142     {
2143       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not create directory '%s'\n", dir);
2144       return 0;
2145     }
2146   strqueue_init(&sq);
2147   FOR_REPOS(repoid, repo)
2148     {
2149       const char *name = testcase_repoid2str(pool, repoid);
2150       char priobuf[50];
2151       if (repo->subpriority)
2152         sprintf(priobuf, "%d.%d", repo->priority, repo->subpriority);
2153       else
2154         sprintf(priobuf, "%d", repo->priority);
2155       out = pool_tmpjoin(pool, name, ".repo", ".gz");
2156       for (i = 0; out[i]; i++)
2157         if (out[i] == '/')
2158           out[i] = '_';
2159       cmd = pool_tmpjoin(pool, "repo ", name, " ");
2160       cmd = pool_tmpappend(pool, cmd, priobuf, " ");
2161       cmd = pool_tmpappend(pool, cmd, "testtags ", out);
2162       strqueue_push(&sq, cmd);
2163       out = pool_tmpjoin(pool, dir, "/", out);
2164       if (!(fp = solv_xfopen(out, "w")))
2165         {
2166           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
2167           strqueue_free(&sq);
2168           return 0;
2169         }
2170       testcase_write_testtags(repo, fp);
2171       if (fclose(fp))
2172         {
2173           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
2174           strqueue_free(&sq);
2175           return 0;
2176         }
2177     }
2178   /* hmm, this is not optimal... we currently search for the lowest score */
2179   lowscore = 0;
2180   arch = pool->solvables[SYSTEMSOLVABLE].arch;
2181   for (i = 0; i < pool->lastarch; i++)
2182     {
2183       if (pool->id2arch[i] == 1 && !lowscore)
2184         arch = i;
2185       if (pool->id2arch[i] > 0x10000 && (!lowscore || pool->id2arch[i] < lowscore))
2186         {
2187           arch = i;
2188           lowscore = pool->id2arch[i];
2189         }
2190     }
2191   cmd = pool_tmpjoin(pool, "system ", pool->lastarch ? pool_id2str(pool, arch) : "unset", 0);
2192   for (i = 0; disttype2str[i].str != 0; i++)
2193     if (pool->disttype == disttype2str[i].type)
2194       break;
2195   pool_tmpappend(pool, cmd, " ", disttype2str[i].str ? disttype2str[i].str : "unknown");
2196   if (pool->installed)
2197     cmd = pool_tmpappend(pool, cmd, " ", testcase_repoid2str(pool, pool->installed->repoid));
2198   strqueue_push(&sq, cmd);
2199   s = testcase_getpoolflags(solv->pool);
2200   if (*s)
2201     {
2202       cmd = pool_tmpjoin(pool, "poolflags ", s, 0);
2203       strqueue_push(&sq, cmd);
2204     }
2205
2206   if (pool->vendorclasses)
2207     {
2208       cmd = 0;
2209       for (i = 0; pool->vendorclasses[i]; i++)
2210         {
2211           cmd = pool_tmpappend(pool, cmd ? cmd : "vendorclass", " ", pool->vendorclasses[i]);
2212           if (!pool->vendorclasses[i + 1])
2213             {
2214               strqueue_push(&sq, cmd);
2215               cmd = 0;
2216               i++;
2217             }
2218         }
2219     }
2220
2221   /* dump disabled packages (must come before the namespace/job lines) */
2222   if (pool->considered)
2223     {
2224       Id p;
2225       FOR_POOL_SOLVABLES(p)
2226         if (!MAPTST(pool->considered, p))
2227           {
2228             cmd = pool_tmpjoin(pool, "disable pkg ", testcase_solvid2str(pool, p), 0);
2229             strqueue_push(&sq, cmd);
2230           }
2231     }
2232
2233   s = testcase_getsolverflags(solv);
2234   if (*s)
2235     {
2236       cmd = pool_tmpjoin(pool, "solverflags ", s, 0);
2237       strqueue_push(&sq, cmd);
2238     }
2239
2240   /* now dump all the ns callback values we know */
2241   if (pool->nscallback)
2242     {
2243       Id rid;
2244       int d;
2245       for (rid = 1; rid < pool->nrels; rid++)
2246         {
2247           Reldep *rd = pool->rels + rid;
2248           if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
2249             continue;
2250           /* evaluate all namespace ids, skip empty results */
2251           d = pool_whatprovides(pool, MAKERELDEP(rid));
2252           if (!d || !pool->whatprovidesdata[d])
2253             continue;
2254           cmd = pool_tmpjoin(pool, "namespace ", pool_id2str(pool, rd->name), "(");
2255           cmd = pool_tmpappend(pool, cmd, pool_id2str(pool, rd->evr), ")");
2256           for (;  pool->whatprovidesdata[d]; d++)
2257             cmd = pool_tmpappend(pool, cmd, " ", testcase_solvid2str(pool, pool->whatprovidesdata[d]));
2258           strqueue_push(&sq, cmd);
2259         }
2260     }
2261
2262   for (i = 0; i < solv->job.count; i += 2)
2263     {
2264       cmd = (char *)testcase_job2str(pool, solv->job.elements[i], solv->job.elements[i + 1]);
2265       cmd = pool_tmpjoin(pool, "job ", cmd, 0);
2266       strqueue_push(&sq, cmd);
2267     }
2268
2269   if (resultflags)
2270     {
2271       char *result;
2272       cmd = 0;
2273       for (i = 0; resultflags2str[i].str; i++)
2274         if ((resultflags & resultflags2str[i].flag) != 0)
2275           cmd = pool_tmpappend(pool, cmd, cmd ? "," : 0, resultflags2str[i].str);
2276       cmd = pool_tmpjoin(pool, "result ", cmd ? cmd : "?", 0);
2277       cmd = pool_tmpappend(pool, cmd, " ", resultname);
2278       strqueue_push(&sq, cmd);
2279       result = testcase_solverresult(solv, resultflags);
2280       if (!strcmp(resultname, "<inline>"))
2281         {
2282           int i;
2283           Strqueue rsq;
2284           strqueue_init(&rsq);
2285           strqueue_split(&rsq, result);
2286           for (i = 0; i < rsq.nstr; i++)
2287             {
2288               cmd = pool_tmpjoin(pool, "#>", rsq.str[i], 0);
2289               strqueue_push(&sq, cmd);
2290             }
2291           strqueue_free(&rsq);
2292         }
2293       else
2294         {
2295           out = pool_tmpjoin(pool, dir, "/", resultname);
2296           if (!(fp = fopen(out, "w")))
2297             {
2298               pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
2299               solv_free(result);
2300               strqueue_free(&sq);
2301               return 0;
2302             }
2303           if (result && *result && fwrite(result, strlen(result), 1, fp) != 1)
2304             {
2305               pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
2306               solv_free(result);
2307               strqueue_free(&sq);
2308               fclose(fp);
2309               return 0;
2310             }
2311           if (fclose(fp))
2312             {
2313               pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
2314               strqueue_free(&sq);
2315               return 0;
2316             }
2317         }
2318       solv_free(result);
2319     }
2320
2321   cmd = strqueue_join(&sq);
2322   out = pool_tmpjoin(pool, dir, "/", testcasename);
2323   if (!(fp = fopen(out, "w")))
2324     {
2325       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
2326       strqueue_free(&sq);
2327       return 0;
2328     }
2329   if (*cmd && fwrite(cmd, strlen(cmd), 1, fp) != 1)
2330     {
2331       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
2332       strqueue_free(&sq);
2333       fclose(fp);
2334       return 0;
2335     }
2336   if (fclose(fp))
2337     {
2338       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
2339       strqueue_free(&sq);
2340       return 0;
2341     }
2342   solv_free(cmd);
2343   strqueue_free(&sq);
2344   return 1;
2345 }
2346
2347 int
2348 testcase_write(Solver *solv, const char *dir, int resultflags, const char *testcasename, const char *resultname)
2349 {
2350   Pool *pool = solv->pool;
2351   int i, r, repoid;
2352   int mangle = 1;
2353   const char **orignames;
2354
2355   /* mangle repo names so that there are no conflicts */
2356   orignames = solv_calloc(pool->nrepos, sizeof(char *));
2357   for (repoid = 1; repoid < pool->nrepos; repoid++)
2358     {
2359       Repo *repo = pool_id2repo(pool, repoid);
2360       char *buf = solv_malloc((repo->name ? strlen(repo->name) : 0) + 40);
2361       char *mp;
2362       orignames[repoid] = repo->name;
2363       if (!repo->name || !repo->name[0])
2364         sprintf(buf, "#%d", repoid);
2365       else
2366         strcpy(buf, repo->name);
2367       for (i = 0; buf[i]; i++)
2368         if (buf[i] == ' ' || buf[i] == '\t' || buf[i] == '/')
2369           buf[i] = '_';
2370       mp = buf + strlen(buf);
2371       for (;;)
2372         {
2373           for (i = 1; i < repoid; i++)
2374             if (!strcmp(buf, pool_id2repo(pool, i)->name))
2375               break;
2376           if (i == repoid)
2377             break;
2378           sprintf(mp, "_%d", mangle++);
2379         }
2380       repo->name = buf;
2381     }
2382   r = testcase_write_mangled(solv, dir, resultflags, testcasename, resultname);
2383   for (repoid = 1; repoid < pool->nrepos; repoid++)
2384     {
2385       Repo *repo = pool_id2repo(pool, repoid);
2386       solv_free((void *)repo->name);
2387       repo->name = orignames[repoid];
2388     }
2389   solv_free(orignames);
2390   return r;
2391 }
2392
2393 static char *
2394 read_inline_file(FILE *fp, char **bufp, char **bufpp, int *buflp)
2395 {
2396   char *result = solv_malloc(1024);
2397   char *rp = result;
2398   int resultl = 1024;
2399
2400   for (;;)
2401     {
2402       size_t rl;
2403       if (rp - result + 256 >= resultl)
2404         {
2405           resultl = rp - result;
2406           result = solv_realloc(result, resultl + 1024);
2407           rp = result + resultl;
2408           resultl += 1024;
2409         }
2410       if (!fgets(rp, resultl - (rp - result), fp))
2411         *rp = 0;
2412       rl = strlen(rp);
2413       if (rl && (rp == result || rp[-1] == '\n'))
2414         {
2415           if (rl > 1 && rp[0] == '#' && rp[1] == '>')
2416             {
2417               memmove(rp, rp + 2, rl - 2);
2418               rl -= 2;
2419             }
2420           else
2421             {
2422               while (rl + 16 > *buflp)
2423                 {
2424                   *bufp = solv_realloc(*bufp, *buflp + 512);
2425                   *buflp += 512;
2426                 }
2427               memmove(*bufp, rp, rl);
2428               if ((*bufp)[rl - 1] == '\n')
2429                 {
2430                   ungetc('\n', fp);
2431                   rl--;
2432                 }
2433               (*bufp)[rl] = 0;
2434               (*bufpp) = *bufp + rl;
2435               rl = 0;
2436             }
2437         }
2438       if (rl <= 0)
2439         {
2440           *rp = 0;
2441           break;
2442         }
2443       rp += rl;
2444     }
2445   return result;
2446 }
2447
2448 static char *
2449 read_file(FILE *fp)
2450 {
2451   char *result = solv_malloc(1024);
2452   char *rp = result;
2453   int resultl = 1024;
2454
2455   for (;;)
2456     {
2457       size_t rl;
2458       if (rp - result + 256 >= resultl)
2459         {
2460           resultl = rp - result;
2461           result = solv_realloc(result, resultl + 1024);
2462           rp = result + resultl;
2463           resultl += 1024;
2464         }
2465       rl = fread(rp, 1, resultl - (rp - result), fp);
2466       if (rl <= 0)
2467         {
2468           *rp = 0;
2469           break;
2470         }
2471       rp += rl;
2472     }
2473   return result;
2474 }
2475
2476 static int
2477 str2resultflags(Pool *pool, char *s)    /* modifies the string! */
2478 {
2479   int i, resultflags = 0;
2480   while (s)
2481     {
2482       char *se = strchr(s, ',');
2483       if (se)
2484         *se++ = 0;
2485       for (i = 0; resultflags2str[i].str; i++)
2486         if (!strcmp(s, resultflags2str[i].str))
2487           {
2488             resultflags |= resultflags2str[i].flag;
2489             break;
2490           }
2491       if (!resultflags2str[i].str)
2492         pool_debug(pool, SOLV_ERROR, "result: unknown flag '%s'\n", s);
2493       s = se;
2494     }
2495   return resultflags;
2496 }
2497
2498 Solver *
2499 testcase_read(Pool *pool, FILE *fp, const char *testcase, Queue *job, char **resultp, int *resultflagsp)
2500 {
2501   Solver *solv;
2502   char *buf, *bufp;
2503   int bufl;
2504   char *testcasedir, *s;
2505   int l;
2506   char **pieces = 0;
2507   int npieces = 0;
2508   int prepared = 0;
2509   int closefp = !fp;
2510   int poolflagsreset = 0;
2511   int missing_features = 0;
2512   Id *genid = 0;
2513   int ngenid = 0;
2514   Queue autoinstq;
2515
2516   if (!fp && !(fp = fopen(testcase, "r")))
2517     {
2518       pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", testcase);
2519       return 0;
2520     }
2521   testcasedir = solv_strdup(testcase);
2522   if ((s = strrchr(testcasedir, '/')) != 0)
2523     s[1] = 0;
2524   else
2525     *testcasedir = 0;
2526   bufl = 1024;
2527   buf = solv_malloc(bufl);
2528   bufp = buf;
2529   solv = 0;
2530   queue_init(&autoinstq);
2531   for (;;)
2532     {
2533       if (bufp - buf + 16 > bufl)
2534         {
2535           bufl = bufp - buf;
2536           buf = solv_realloc(buf, bufl + 512);
2537           bufp = buf + bufl;
2538           bufl += 512;
2539         }
2540       if (!fgets(bufp, bufl - (bufp - buf), fp))
2541         break;
2542       bufp = buf;
2543       l = strlen(buf);
2544       if (!l || buf[l - 1] != '\n')
2545         {
2546           bufp += l;
2547           continue;
2548         }
2549       buf[--l] = 0;
2550       s = buf;
2551       while (*s && (*s == ' ' || *s == '\t'))
2552         s++;
2553       if (!*s || *s == '#')
2554         continue;
2555       npieces = 0;
2556       /* split it in pieces */
2557       for (;;)
2558         {
2559           while (*s == ' ' || *s == '\t')
2560             s++;
2561           if (!*s)
2562             break;
2563           pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
2564           pieces[npieces++] = s;
2565           while (*s && *s != ' ' && *s != '\t')
2566             s++;
2567           if (*s)
2568             *s++ = 0;
2569         }
2570       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
2571       pieces[npieces] = 0;
2572       if (!strcmp(pieces[0], "repo") && npieces >= 4)
2573         {
2574           Repo *repo = repo_create(pool, pieces[1]);
2575           FILE *rfp;
2576           int prio, subprio;
2577           const char *rdata;
2578
2579           prepared = 0;
2580           if (!poolflagsreset)
2581             {
2582               poolflagsreset = 1;
2583               testcase_resetpoolflags(pool);    /* hmm */
2584             }
2585           if (sscanf(pieces[2], "%d.%d", &prio, &subprio) != 2)
2586             {
2587               subprio = 0;
2588               prio = atoi(pieces[2]);
2589             }
2590           repo->priority = prio;
2591           repo->subpriority = subprio;
2592           if (strcmp(pieces[3], "empty") != 0)
2593             {
2594               const char *repotype = pool_tmpjoin(pool, pieces[3], 0, 0);       /* gets overwritten in <inline> case */
2595               if (!strcmp(pieces[4], "<inline>"))
2596                 {
2597                   char *idata = read_inline_file(fp, &buf, &bufp, &bufl);
2598                   rdata = "<inline>";
2599                   rfp = solv_xfopen_buf(rdata, &idata, 0, "rf");
2600                 }
2601               else
2602                 {
2603                   rdata = pool_tmpjoin(pool, testcasedir, pieces[4], 0);
2604                   rfp = solv_xfopen(rdata, "r");
2605                 }
2606               if (!rfp)
2607                 {
2608                   pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", rdata);
2609                 }
2610               else if (!strcmp(repotype, "testtags"))
2611                 {
2612                   testcase_add_testtags(repo, rfp, 0);
2613                   fclose(rfp);
2614                 }
2615               else if (!strcmp(repotype, "solv"))
2616                 {
2617                   repo_add_solv(repo, rfp, 0);
2618                   fclose(rfp);
2619                 }
2620 #if 0
2621               else if (!strcmp(repotype, "helix"))
2622                 {
2623                   extern int repo_add_helix(Repo *repo, FILE *fp, int flags);
2624                   repo_add_helix(repo, rfp, 0);
2625                   fclose(rfp);
2626                 }
2627 #endif
2628               else
2629                 {
2630                   fclose(rfp);
2631                   pool_debug(pool, SOLV_ERROR, "testcase_read: unknown repo type for repo '%s'\n", repo->name);
2632                 }
2633             }
2634         }
2635       else if (!strcmp(pieces[0], "system") && npieces >= 3)
2636         {
2637           int i;
2638
2639           /* must set the disttype before the arch */
2640           prepared = 0;
2641           if (strcmp(pieces[2], "*") != 0)
2642             {
2643               char *dp = pieces[2];
2644               while (dp && *dp)
2645                 {
2646                   char *dpe = strchr(dp, ',');
2647                   if (dpe)
2648                     *dpe = 0;
2649                   for (i = 0; disttype2str[i].str != 0; i++)
2650                     if (!strcmp(disttype2str[i].str, dp))
2651                       break;
2652                   if (dpe)
2653                     *dpe++ = ',';
2654                   if (disttype2str[i].str)
2655                     {
2656 #ifdef MULTI_SEMANTICS
2657                       if (pool->disttype != disttype2str[i].type)
2658                         pool_setdisttype(pool, disttype2str[i].type);
2659 #endif
2660                       if (pool->disttype == disttype2str[i].type)
2661                         break;
2662                     }
2663                   dp = dpe;
2664                 }
2665               if (!(dp && *dp))
2666                 {
2667                   pool_debug(pool, SOLV_ERROR, "testcase_read: system: could not change disttype to '%s'\n", pieces[2]);
2668                   missing_features = 1;
2669                 }
2670             }
2671           if (strcmp(pieces[1], "unset") == 0)
2672             pool_setarch(pool, 0);
2673           else if (pieces[1][0] == ':')
2674             pool_setarchpolicy(pool, pieces[1] + 1);
2675           else
2676             pool_setarch(pool, pieces[1]);
2677           if (npieces > 3)
2678             {
2679               Repo *repo = testcase_str2repo(pool, pieces[3]);
2680               if (!repo)
2681                 pool_debug(pool, SOLV_ERROR, "testcase_read: system: unknown repo '%s'\n", pieces[3]);
2682               else
2683                 pool_set_installed(pool, repo);
2684             }
2685         }
2686       else if (!strcmp(pieces[0], "job") && npieces > 1)
2687         {
2688           char *sp;
2689           Id how, what;
2690           if (prepared <= 0)
2691             {
2692               pool_addfileprovides(pool);
2693               pool_createwhatprovides(pool);
2694               prepared = 1;
2695             }
2696           if (npieces >= 3 && !strcmp(pieces[2], "selection"))
2697             {
2698               addselectionjob(pool, pieces + 1, npieces - 1, job);
2699               continue;
2700             }
2701           /* rejoin */
2702           for (sp = pieces[1]; sp < pieces[npieces - 1]; sp++)
2703             if (*sp == 0)
2704               *sp = ' ';
2705           how = testcase_str2job(pool, pieces[1], &what);
2706           if (how >= 0 && job)
2707             queue_push2(job, how, what);
2708         }
2709       else if (!strcmp(pieces[0], "vendorclass") && npieces > 1)
2710         {
2711           pool_addvendorclass(pool, (const char **)(pieces + 1));
2712         }
2713       else if (!strcmp(pieces[0], "namespace") && npieces > 1)
2714         {
2715           int i = strlen(pieces[1]);
2716           s = strchr(pieces[1], '(');
2717           if (!s && pieces[1][i - 1] != ')')
2718             {
2719               pool_debug(pool, SOLV_ERROR, "testcase_read: bad namespace '%s'\n", pieces[1]);
2720             }
2721           else
2722             {
2723               Id name, evr, id;
2724               Queue q;
2725               queue_init(&q);
2726               *s = 0;
2727               pieces[1][i - 1] = 0;
2728               name = pool_str2id(pool, pieces[1], 1);
2729               evr = pool_str2id(pool, s + 1, 1);
2730               *s = '(';
2731               pieces[1][i - 1] = ')';
2732               id = pool_rel2id(pool, name, evr, REL_NAMESPACE, 1);
2733               for (i = 2; i < npieces; i++)
2734                 queue_push(&q, testcase_str2solvid(pool, pieces[i]));
2735               /* now do the callback */
2736               if (prepared <= 0)
2737                 {
2738                   pool_addfileprovides(pool);
2739                   pool_createwhatprovides(pool);
2740                   prepared = 1;
2741                 }
2742               pool->whatprovides_rel[GETRELID(id)] = pool_queuetowhatprovides(pool, &q);
2743               queue_free(&q);
2744             }
2745         }
2746       else if (!strcmp(pieces[0], "poolflags"))
2747         {
2748           int i;
2749           if (!poolflagsreset)
2750             {
2751               poolflagsreset = 1;
2752               testcase_resetpoolflags(pool);    /* hmm */
2753             }
2754           for (i = 1; i < npieces; i++)
2755             testcase_setpoolflags(pool, pieces[i]);
2756         }
2757       else if (!strcmp(pieces[0], "solverflags") && npieces > 1)
2758         {
2759           int i;
2760           if (!solv)
2761             {
2762               solv = solver_create(pool);
2763               testcase_resetsolverflags(solv);
2764             }
2765           for (i = 1; i < npieces; i++)
2766             testcase_setsolverflags(solv, pieces[i]);
2767         }
2768       else if (!strcmp(pieces[0], "result") && npieces > 1)
2769         {
2770           char *result = 0;
2771           int resultflags = str2resultflags(pool, pieces[1]);
2772           const char *rdata;
2773           if (npieces > 2)
2774             {
2775               rdata = pool_tmpjoin(pool, testcasedir, pieces[2], 0);
2776               if (!strcmp(pieces[2], "<inline>"))
2777                 result = read_inline_file(fp, &buf, &bufp, &bufl);
2778               else
2779                 {
2780                   FILE *rfp = fopen(rdata, "r");
2781                   if (!rfp)
2782                     pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", rdata);
2783                   else
2784                     {
2785                       result = read_file(rfp);
2786                       fclose(rfp);
2787                     }
2788                 }
2789             }
2790           if (resultp)
2791             *resultp = result;
2792           else
2793             solv_free(result);
2794           if (resultflagsp)
2795             *resultflagsp = resultflags;
2796         }
2797       else if (!strcmp(pieces[0], "nextjob") && npieces == 1)
2798         {
2799           break;
2800         }
2801       else if (!strcmp(pieces[0], "disable") && npieces == 3)
2802         {
2803           Id p;
2804           if (strcmp(pieces[1], "pkg"))
2805             {
2806               pool_debug(pool, SOLV_ERROR, "testcase_read: bad disable type '%s'\n", pieces[1]);
2807               continue;
2808             }
2809           if (!prepared)
2810             pool_createwhatprovides(pool);
2811           prepared = -1;
2812           if (!pool->considered)
2813             {
2814               pool->considered = solv_calloc(1, sizeof(Map));
2815               map_init(pool->considered, pool->nsolvables);
2816               map_setall(pool->considered);
2817             }
2818           p = testcase_str2solvid(pool, pieces[2]);
2819           if (p)
2820             MAPCLR(pool->considered, p);
2821           else
2822             pool_debug(pool, SOLV_ERROR, "disable: unknown package '%s'\n", pieces[2]);
2823         }
2824       else if (!strcmp(pieces[0], "feature"))
2825         {
2826           int i, j;
2827           for (i = 1; i < npieces; i++)
2828             {
2829               for (j = 0; features[j]; j++)
2830                 if (!strcmp(pieces[i], features[j]))
2831                   break;
2832               if (!features[j])
2833                 {
2834                   pool_debug(pool, SOLV_ERROR, "testcase_read: missing feature '%s'\n", pieces[i]);
2835                   missing_features++;
2836                 }
2837             }
2838           if (missing_features)
2839             break;
2840         }
2841       else if (!strcmp(pieces[0], "genid") && npieces > 1)
2842         {
2843           Id id;
2844           /* rejoin */
2845           if (npieces > 2)
2846             {
2847               char *sp;
2848               for (sp = pieces[2]; sp < pieces[npieces - 1]; sp++)
2849                 if (*sp == 0)
2850                   *sp = ' ';
2851             }
2852           genid = solv_extend(genid, ngenid, 1, sizeof(*genid), 7);
2853           if (!strcmp(pieces[1], "op") && npieces > 2)
2854             {
2855               struct oplist *op;
2856               for (op = oplist; op->flags; op++)
2857                 if (!strncmp(pieces[2], op->opname, strlen(op->opname)))
2858                   break;
2859               if (!op->flags)
2860                 {
2861                   pool_debug(pool, SOLV_ERROR, "testcase_read: genid: unknown op '%s'\n", pieces[2]);
2862                   break;
2863                 }
2864               if (ngenid < 2)
2865                 {
2866                   pool_debug(pool, SOLV_ERROR, "testcase_read: genid: out of stack\n");
2867                   break;
2868                 }
2869               ngenid -= 2;
2870               id = pool_rel2id(pool, genid[ngenid] , genid[ngenid + 1], op->flags, 1);
2871             }
2872           else if (!strcmp(pieces[1], "lit"))
2873             id = pool_str2id(pool, npieces > 2 ? pieces[2] : "", 1);
2874           else if (!strcmp(pieces[1], "null"))
2875             id = 0;
2876           else if (!strcmp(pieces[1], "dep"))
2877             id = testcase_str2dep(pool, pieces[2]);
2878           else
2879             {
2880               pool_debug(pool, SOLV_ERROR, "testcase_read: genid: unknown command '%s'\n", pieces[1]);
2881               break;
2882             }
2883           genid[ngenid++] = id;
2884         }
2885       else if (!strcmp(pieces[0], "autoinst") && npieces > 2)
2886         {
2887           if (strcmp(pieces[1], "name"))
2888             {
2889               pool_debug(pool, SOLV_ERROR, "testcase_read: autoinst: illegal mode\n");
2890               break;
2891             }
2892           queue_push(&autoinstq, pool_str2id(pool, pieces[2], 1));
2893         }
2894       else
2895         {
2896           pool_debug(pool, SOLV_ERROR, "testcase_read: cannot parse command '%s'\n", pieces[0]);
2897         }
2898     }
2899   while (job && ngenid > 0)
2900     queue_push2(job, SOLVER_NOOP | SOLVER_SOLVABLE_PROVIDES, genid[--ngenid]);
2901   if (autoinstq.count)
2902     pool_add_userinstalled_jobs(pool, &autoinstq, job, GET_USERINSTALLED_NAMES | GET_USERINSTALLED_INVERTED);
2903   queue_free(&autoinstq);
2904   genid = solv_free(genid);
2905   buf = solv_free(buf);
2906   pieces = solv_free(pieces);
2907   solv_free(testcasedir);
2908   if (!prepared)
2909     {
2910       pool_addfileprovides(pool);
2911       pool_createwhatprovides(pool);
2912     }
2913   if (!solv)
2914     {
2915       solv = solver_create(pool);
2916       testcase_resetsolverflags(solv);
2917     }
2918   if (closefp)
2919     fclose(fp);
2920   if (missing_features)
2921     {
2922       solver_free(solv);
2923       solv = 0;
2924       if (resultflagsp)
2925         *resultflagsp = 77;     /* hack for testsolv */
2926     }
2927   return solv;
2928 }
2929
2930 char *
2931 testcase_resultdiff(const char *result1, const char *result2)
2932 {
2933   Strqueue sq1, sq2, osq;
2934   char *r;
2935   strqueue_init(&sq1);
2936   strqueue_init(&sq2);
2937   strqueue_init(&osq);
2938   strqueue_split(&sq1, result1);
2939   strqueue_split(&sq2, result2);
2940   strqueue_sort(&sq1);
2941   strqueue_sort(&sq2);
2942   strqueue_diff(&sq1, &sq2, &osq);
2943   r = osq.nstr ? strqueue_join(&osq) : 0;
2944   strqueue_free(&sq1);
2945   strqueue_free(&sq2);
2946   strqueue_free(&osq);
2947   return r;
2948 }
2949