summaryrefslogtreecommitdiff
path: root/src/selection.c
blob: d44c4829f928f037da8c72401e4f44b73215ee68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
/*
 * Copyright (c) 2012, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

/*
 * selection.c
 *
 */

#define _GNU_SOURCE
#include <string.h>
#include <fnmatch.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "selection.h"
#include "solver.h"
#include "evr.h"


static int
str2archid(Pool *pool, const char *arch)
{
  Id id;
  if (!*arch)
    return 0;
  id = pool_str2id(pool, arch, 0);
  if (!id || id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
    return id;
  if (pool->id2arch && pool_arch2score(pool, id) == 0)
    return 0;
  return id;
}

/* remove empty jobs from the selection */
static void
selection_prune(Pool *pool, Queue *selection)
{
  int i, j;
  Id p, pp;
  for (i = j = 0; i < selection->count; i += 2)
    {
      Id select = selection->elements[i] & SOLVER_SELECTMASK;
      p = 0;
      if (select == SOLVER_SOLVABLE_ALL)
	p = 1;
      else if (select == SOLVER_SOLVABLE_REPO)
	{
	  Solvable *s;
	  Repo *repo = pool_id2repo(pool, selection->elements[i + 1]);
	  if (repo)
	    {
	      FOR_REPO_SOLVABLES(repo, p, s)
	        break;
	    }
	}
      else
	{
	  FOR_JOB_SELECT(p, pp, select, selection->elements[i + 1])
	    break;
	}
      if (!p)
	continue;
      selection->elements[j] = selection->elements[i];
      selection->elements[j + 1] = selection->elements[i + 1];
      j += 2;
    }
  queue_truncate(selection, j);
}


static int
selection_solvables_sortcmp(const void *ap, const void *bp, void *dp)
{
  return *(const Id *)ap - *(const Id *)bp;
}

void
selection_solvables(Pool *pool, Queue *selection, Queue *pkgs)
{
  int i, j;
  Id p, pp, lastid;
  queue_empty(pkgs);
  for (i = 0; i < selection->count; i += 2)
    {
      Id select = selection->elements[i] & SOLVER_SELECTMASK;
      if (select == SOLVER_SOLVABLE_ALL)
	{
	  FOR_POOL_SOLVABLES(p)
	    queue_push(pkgs, p);
	}
      if (select == SOLVER_SOLVABLE_REPO)
	{
	  Solvable *s;
	  Repo *repo = pool_id2repo(pool, selection->elements[i + 1]);
	  if (repo)
	    {
	      FOR_REPO_SOLVABLES(repo, p, s)
	        queue_push(pkgs, p);
	    }
	}
      else
	{
	  FOR_JOB_SELECT(p, pp, select, selection->elements[i + 1])
	    queue_push(pkgs, p);
	}
    }
  if (pkgs->count < 2)
    return;
  /* sort and unify */
  solv_sort(pkgs->elements, pkgs->count, sizeof(Id), selection_solvables_sortcmp, NULL);
  lastid = pkgs->elements[0];
  for (i = j = 1; i < pkgs->count; i++)
    if (pkgs->elements[i] != lastid)
      pkgs->elements[j++] = lastid = pkgs->elements[i];
  queue_truncate(pkgs, j);
}

static void
selection_flatten(Pool *pool, Queue *selection)
{
  Queue q;
  int i;
  if (selection->count <= 2)
    return;
  for (i = 0; i < selection->count; i += 2)
    if ((selection->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
      {
	selection->elements[0] = selection->elements[i];
	selection->elements[1] = selection->elements[i + 1];
	queue_truncate(selection, 2);
	return;
      }
  queue_init(&q);
  selection_solvables(pool, selection, &q);
  if (!q.count)
    {
      queue_empty(selection);
      return;
    }
  queue_truncate(selection, 2);
  if (q.count > 1)
    {
      selection->elements[0] = SOLVER_SOLVABLE_ONE_OF;
      selection->elements[1] = pool_queuetowhatprovides(pool, &q);
    }
  else
    {
      selection->elements[0] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
      selection->elements[1] = q.elements[0];
    }
}

/* only supports simple rels plus REL_ARCH */
static int
match_nevr_rel(Pool *pool, Solvable *s, Id rflags, Id revr)
{
  if (rflags == REL_ARCH)
    {
      if (s->arch != revr) 
	{
	  if (revr != ARCH_SRC || s->arch != ARCH_NOSRC)
	    return 0;
	}
      return 1;
    }
  if (rflags > 7)
    return 0;
  return pool_intersect_evrs(pool, REL_EQ, s->evr, rflags, revr);
}

/* only supports simple rels plus REL_ARCH */
static void
selection_filter_rel_noprune(Pool *pool, Queue *selection, Id relflags, Id relevr)
{
  int i;

  if (!selection->count)
    return;

  for (i = 0; i < selection->count; i += 2)
    {
      Id select = selection->elements[i] & SOLVER_SELECTMASK;
      Id id = selection->elements[i + 1];
      if (select == SOLVER_SOLVABLE || select == SOLVER_SOLVABLE_ONE_OF)
	{
	  /* done by selection_addextra, currently implies SELECTION_NAME */
	  Queue q;
	  Id p, pp;
	  int miss = 0;

	  queue_init(&q);
	  FOR_JOB_SELECT(p, pp, select, id)
	    {
	      Solvable *s = pool->solvables + p;
	      if (match_nevr_rel(pool, s, relflags, relevr))
	        queue_push(&q, p);
	      else
		miss = 1;
	    }
	  if (miss)
	    {
	      if (q.count == 1)
		{
		  selection->elements[i] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
		  selection->elements[i + 1] = q.elements[0];
		}
	      else
		{
		  selection->elements[i] = SOLVER_SOLVABLE_ONE_OF;
		  selection->elements[i + 1] = pool_queuetowhatprovides(pool, &q);
		}
	    }
	  queue_free(&q);
	}
      else if (select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES)
	{
	  /* don't stack src reldeps */
	  if (relflags == REL_ARCH && (relevr == ARCH_SRC || relevr == ARCH_NOSRC) && ISRELDEP(id))
	    {
	      Reldep *rd = GETRELDEP(pool, id);
	      if (rd->flags == REL_ARCH && rd->evr == ARCH_SRC)
		id = rd->name;
	    }
	  selection->elements[i + 1] = pool_rel2id(pool, id, relevr, relflags, 1);
	}
      else
	continue;	/* actually cannot happen */

      /* now add the setflags we gained */
      if (relflags == REL_ARCH)
	selection->elements[i] |= SOLVER_SETARCH;
      if (relflags == REL_EQ && select != SOLVER_SOLVABLE_PROVIDES)
	{
	  if (pool->disttype == DISTTYPE_DEB)
	    selection->elements[i] |= SOLVER_SETEVR;	/* debian can't match version only like rpm */
	  else
	    {
	      const char *rel =  strrchr(pool_id2str(pool, relevr), '-');
	      selection->elements[i] |= rel ? SOLVER_SETEVR : SOLVER_SETEV;
	    }
	}
    }
}

/* only supports simple rels plus REL_ARCH */
/* prunes empty jobs */
static void
selection_filter_rel(Pool *pool, Queue *selection, Id relflags, Id relevr)
{
  selection_filter_rel_noprune(pool, selection, relflags, relevr);
  selection_prune(pool, selection);
}

/* limit a selection to to repository */
/* prunes empty jobs */
static void
selection_filter_repo(Pool *pool, Queue *selection, Repo *repo)
{
  Queue q;
  int i, j;

  if (!selection->count)
    return;
  if (!repo)
    {
      queue_empty(selection);
      return;
    }
  queue_init(&q);
  for (i = j = 0; i < selection->count; i += 2)
    {
      Id select = selection->elements[i] & SOLVER_SELECTMASK;
      Id id = selection->elements[i + 1];
      if (select == SOLVER_SOLVABLE_ALL)
	{
	  select = SOLVER_SOLVABLE_REPO;
	  id = repo->repoid;
	}
      else if (select == SOLVER_SOLVABLE_REPO)
	{
	  if (id != repo->repoid)
	    select = 0;
	}
      else
	{
	  int bad = 0;
	  Id p, pp;
	  queue_empty(&q);
	  FOR_JOB_SELECT(p, pp, select, id)
	    {
	      if (pool->solvables[p].repo != repo)
		bad = 1;
	      else
		queue_push(&q, p);
	    }
	  if (bad || !q.count)
	    {
	      if (!q.count)
		select = 0;		/* prune empty jobs */
	      else if (q.count == 1)
		{
		  select = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
		  id = q.elements[0];
		}
	      else
		{
		  select = SOLVER_SOLVABLE_ONE_OF;
	          id = pool_queuetowhatprovides(pool, &q);
		}
	    }
	}
      if (!select)
	continue;	/* job is now empty */
      if (select == SOLVER_SOLVABLE_REPO)
	{
	  Id p;
	  Solvable *s;
	  FOR_REPO_SOLVABLES(repo, p, s)
	    break;
	  if (!p)
	    continue;	/* repo is empty */
	}
      selection->elements[j++] = select | (selection->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SETREPO;
      selection->elements[j++] = id;
    }
  queue_truncate(selection, j);
  queue_free(&q);
}


static int
matchprovides(Pool *pool, Solvable *s, Id dep)
{
  Id id, *idp;
  idp = s->repo->idarraydata + s->provides;
  while ((id = *idp++) != 0)
    if (pool_match_dep(pool, id, dep))
      return 1;
  return 0;
}

/* change a SOLVER_SOLVABLE_NAME/PROVIDES selection to something that also includes
 * extra packages.
 * extra packages are: src, badarch, disabled
 */
static void
selection_addextra(Pool *pool, Queue *selection, int flags)
{
  Queue q;
  Id p, pp, dep;
  int i, isextra, haveextra, doprovides;

  if ((flags & SELECTION_INSTALLED_ONLY) != 0)
    flags &= ~SELECTION_WITH_SOURCE;

  if (!(flags & (SELECTION_WITH_SOURCE | SELECTION_WITH_DISABLED | SELECTION_WITH_BADARCH)))
    return;	/* nothing to add */

  queue_init(&q);
  for (i = 0; i < selection->count; i += 2)
    {
      if (selection->elements[i] == SOLVER_SOLVABLE_NAME)
	doprovides = 0;
      else if (selection->elements[i] == SOLVER_SOLVABLE_PROVIDES)
	doprovides = 1;
      else
	continue;
      dep = selection->elements[i + 1];
      haveextra = 0;
      queue_empty(&q);
      if (doprovides)
	{
	  /* first put all non-extra packages on the queue */
	  FOR_PROVIDES(p, pp, dep)
	    {
	      if ((flags & SELECTION_INSTALLED_ONLY) != 0 && pool->solvables[p].repo != pool->installed)
		continue;
	      queue_push(&q, p);
	    }
	}
      FOR_POOL_SOLVABLES(p)
	{
	  Solvable *s = pool->solvables + p;
	  if (!doprovides && !pool_match_nevr(pool, s, dep))
	    continue;
	  if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
	    continue;
	  isextra = 0;
	  if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
	    {
	      if (!(flags & SELECTION_WITH_SOURCE) && !(flags & SELECTION_SOURCE_ONLY))
		continue;
	      if (!(flags & SELECTION_SOURCE_ONLY))
		isextra = 1;
	      if (pool_disabled_solvable(pool, s))
		{
		  if (!(flags & SELECTION_WITH_DISABLED))
		    continue;
		  isextra = 1;
		}
	    }
	  else
	    {
	      if ((flags & SELECTION_SOURCE_ONLY) != 0)
		continue;
	      if (s->repo != pool->installed)
		{
		  if (pool_disabled_solvable(pool, s))
		    {
		      if (!(flags & SELECTION_WITH_DISABLED))
			continue;
		      isextra = 1;
		    }
		  if (pool_badarch_solvable(pool, s))
		    {
		      if (!(flags & SELECTION_WITH_BADARCH))
			continue;
		      isextra = 1;
		    }
		}
	    }
	  if (doprovides)
	    {
	      if (!isextra)
		continue;	/* already done above in FOR_PROVIDES */
	      if (!s->provides || !matchprovides(pool, s, dep))
		continue;
	    }
	  haveextra |= isextra;
	  queue_push(&q, p);
	}
      if (!haveextra || !q.count)
	continue;
      if (q.count == 1)
	{
	  selection->elements[i] = (selection->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
	  selection->elements[i + 1] = q.elements[0];
	}
      else
	{
	  if (doprovides)
	    solv_sort(q.elements, q.count, sizeof(Id), selection_solvables_sortcmp, NULL);
	  selection->elements[i] = (selection->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF;
	  selection->elements[i + 1] = pool_queuetowhatprovides(pool, &q);
	}
    }
  queue_free(&q);
}

static inline const char *
skipkind(const char *n)
{
  const char *s;
  for (s = n; *s >= 'a' && *s <= 'z'; s++)
    ;
  if (*s == ':' && s != n)
     return s + 1;
  return n;
}

static inline void
queue_pushunique2(Queue *q, Id id1, Id id2)
{
  int i;
  for (i = 0; i < q->count; i += 2)
    if (q->elements[i] == id1 && q->elements[i + 1] == id2)
      return;
  queue_push2(q, id1, id2);
}


/*****  provides matching  *****/

static int
selection_addextra_provides(Pool *pool, Queue *selection, const char *name, int flags)
{
  Id p, id, *idp;
  int match = 0;
  int doglob, nocase, globflags;

  if ((flags & SELECTION_INSTALLED_ONLY) != 0)
    return 0;	/* neither disabled nor badarch nor src */

  nocase = flags & SELECTION_NOCASE;
  doglob = (flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0;
  globflags = doglob && nocase ? FNM_CASEFOLD : 0;

  FOR_POOL_SOLVABLES(p)
    {
      const char *n;
      Solvable *s = pool->solvables + p;
      if (!s->provides)
	continue;
      if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)	/* no provides */
	continue;
      if (s->repo == pool->installed)
	continue;
      if (pool_disabled_solvable(pool, s))
	{
	  if (!(flags & SELECTION_WITH_DISABLED))
	    continue;
	  if (!(flags & SELECTION_WITH_BADARCH) && pool_badarch_solvable(pool, s))
	    continue;
	}
      else if (pool_badarch_solvable(pool, s))
	{
	  if (!(flags & SELECTION_WITH_BADARCH))
	    continue;
	}
      else
	continue;
      /* here is an extra solvable we need to consider */
      idp = s->repo->idarraydata + s->provides;
      while ((id = *idp++) != 0)
	{
	  while (ISRELDEP(id))
	    {
	      Reldep *rd = GETRELDEP(pool, id);
	      id = rd->name;
	    }
	  if (pool->whatprovides[id] > 1)
	    continue;	/* we already did that one in the normal code path */
	  n = pool_id2str(pool, id);
	  if ((doglob ? fnmatch(name, n, globflags) : nocase ? strcasecmp(name, n) : strcmp(name, n)) == 0)
	    {
	      queue_pushunique2(selection, SOLVER_SOLVABLE_PROVIDES, id);
	      match = 1;
	    }
	}
    }
  return match;
}

/* this is the fast path of selection_provides: the id for the name
 * is known and thus we can quickly check the existance of a
 * package with that provides */
static int
selection_provides_id(Pool *pool, Queue *selection, Id id, int flags)
{
  Id p, pp;

  FOR_PROVIDES(p, pp, id)
    {
      Solvable *s = pool->solvables + p;
      if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
	continue;
      break;
    }
  if (p)
    {
      queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
      return SELECTION_PROVIDES;
    }

  if ((flags & (SELECTION_WITH_BADARCH | SELECTION_WITH_DISABLED)) != 0)
    {
      queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
      selection_addextra(pool, selection, flags);
      if (selection->elements[0] == SOLVER_SOLVABLE_PROVIDES)
	queue_empty(selection);
      else
	{
          selection->elements[0] = SOLVER_SOLVABLE_PROVIDES;
          selection->elements[1] = id;
	}
      return selection->count ? SELECTION_PROVIDES : 0;
    }

  return 0;
}

/* add missing provides matchers to the selection */
/* match the provides of a package */
/* note that we only return raw SOLVER_SOLVABLE_PROVIDES jobs
 * so that the selection can be modified later. */
static int
selection_provides(Pool *pool, Queue *selection, const char *name, int flags)
{
  Id id, p, pp;
  int match;
  int doglob;
  int nocase;
  int globflags;
  const char *n;

  if ((flags & SELECTION_SOURCE_ONLY) != 0)
    return 0;	/* sources do not have provides */

  nocase = flags & SELECTION_NOCASE;
  if (!nocase)
    {
      /* try the fast path first */
      id = pool_str2id(pool, name, 0);
      if (id)
	{
	  /* the id is known, do the fast id matching */
	  int ret = selection_provides_id(pool, selection, id, flags);
	  if (ret)
	    return ret;
	}
    }

  doglob = (flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0;
  if (!nocase && !doglob)
    {
      /* all done above in selection_provides_id */
      return 0;
    }

  /* looks like a glob or nocase match. really hard work. */
  match = 0;
  globflags = doglob && nocase ? FNM_CASEFOLD : 0;
  for (id = 1; id < pool->ss.nstrings; id++)
    {
      /* do we habe packages providing this id? */
      if (!pool->whatprovides[id] || pool->whatprovides[id] == 1)
	continue;
      n = pool_id2str(pool, id);
      if ((doglob ? fnmatch(name, n, globflags) : nocase ? strcasecmp(name, n) : strcmp(name, n)) == 0)
	{
	  if ((flags & SELECTION_INSTALLED_ONLY) != 0)
	    {
	      FOR_PROVIDES(p, pp, id)
		if (pool->solvables[p].repo == pool->installed)
		  break;
	      if (!p)
		continue;
	    }
	  queue_push2(selection, SOLVER_SOLVABLE_PROVIDES, id);
	  match = 1;
	}
    }

  if (flags & (SELECTION_WITH_BADARCH | SELECTION_WITH_DISABLED))
    match |= selection_addextra_provides(pool, selection, name, flags);

  return match ? SELECTION_PROVIDES : 0;
}

/*****  name matching  *****/

/* this is the fast path of selection_name: the id for the name
 * is known and thus we can quickly check the existance of a
 * package with that name */
static int
selection_name_id(Pool *pool, Queue *selection, Id id, int flags)
{
  Id p, pp, matchid;

  matchid = id;
  if ((flags & SELECTION_SOURCE_ONLY) != 0)
    {
      /* sources cannot be installed */
      if ((flags & SELECTION_INSTALLED_ONLY) != 0)
	return 0;
      /* add ARCH_SRC to match only sources */
      matchid = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
      flags &= ~SELECTION_WITH_SOURCE;
    }

  FOR_PROVIDES(p, pp, matchid)
    {
      Solvable *s = pool->solvables + p;
      if (s->name != id)
	continue;
      if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
	continue;
      /* one match is all we need */
      queue_push2(selection, SOLVER_SOLVABLE_NAME, matchid);
      /* add the requested extra packages */
      if ((flags & (SELECTION_WITH_SOURCE | SELECTION_WITH_BADARCH | SELECTION_WITH_DISABLED)) != 0)
	selection_addextra(pool, selection, flags);
      return SELECTION_NAME;
    }

  if ((flags & (SELECTION_WITH_BADARCH | SELECTION_WITH_DISABLED)) != 0)
    {
      queue_push2(selection, SOLVER_SOLVABLE_NAME, matchid);
      selection_addextra(pool, selection, flags);
      if (selection->elements[0] == SOLVER_SOLVABLE_NAME)
	queue_empty(selection);
      return selection->count ? SELECTION_NAME : 0;
    }

  if ((flags & SELECTION_WITH_SOURCE) != 0 && (flags & SELECTION_INSTALLED_ONLY) == 0)
    {
      /* WITH_SOURCE case, but we had no match. try SOURCE_ONLY instead */
      matchid = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
      FOR_PROVIDES(p, pp, matchid)
	{
	  Solvable *s = pool->solvables + p;
	  if (s->name != id)
	    continue;
	  queue_push2(selection, SOLVER_SOLVABLE_NAME, matchid);
	  return SELECTION_NAME;
	}
    }
  return 0;
}

/* match the name of a package */
/* note that for SELECTION_INSTALLED_ONLY the result is not trimmed */
static int
selection_name(Pool *pool, Queue *selection, const char *name, int flags)
{
  Id id, p;
  int match;
  int doglob, nocase;
  int globflags;
  const char *n;

  if ((flags & SELECTION_SOURCE_ONLY) != 0)
    flags &= ~SELECTION_WITH_SOURCE;

  nocase = flags & SELECTION_NOCASE;
  if (!nocase && !(flags & SELECTION_SKIP_KIND))
    {
      /* try the fast path first */
      id = pool_str2id(pool, name, 0);
      if (id)
	{
	  int ret = selection_name_id(pool, selection, id, flags);
	  if (ret)
	    return ret;
	}
    }

  doglob = (flags & SELECTION_GLOB) != 0 && strpbrk(name, "[*?") != 0;
  if (!nocase && !(flags & SELECTION_SKIP_KIND) && !doglob)
    return 0;	/* all done above in selection_name_id */

  /* do a name match over all packages. hard work. */
  match = 0;
  globflags = doglob && nocase ? FNM_CASEFOLD : 0;
  FOR_POOL_SOLVABLES(p)
    {
      Solvable *s = pool->solvables + p;
      if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
	continue;
      if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
	{
	  if (!(flags & SELECTION_SOURCE_ONLY) && !(flags & SELECTION_WITH_SOURCE))
	    continue;
	  if (!(flags & SELECTION_WITH_DISABLED) && pool_disabled_solvable(pool, s))
	    continue;
	}
      else if (s->repo != pool->installed)
	{
	  if (!(flags & SELECTION_WITH_DISABLED) && pool_disabled_solvable(pool, s))
	    continue;
	  if (!(flags & SELECTION_WITH_BADARCH) && pool_badarch_solvable(pool, s))
	    continue;
	}
      id = s->name;
      n = pool_id2str(pool, id);
      if (flags & SELECTION_SKIP_KIND)
	n = skipkind(n);
      if ((doglob ? fnmatch(name, n, globflags) : nocase ? strcasecmp(name, n) : strcmp(name, n)) == 0)
	{
	  if ((flags & SELECTION_SOURCE_ONLY) != 0)
	    {
	      if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
		continue;
	      id = pool_rel2id(pool, id, ARCH_SRC, REL_ARCH, 1);
	    }
	  queue_pushunique2(selection, SOLVER_SOLVABLE_NAME, id);
	  match = 1;
	}
    }
  if (match)
    {
      /* if there was a match widen the selector to include all extra packages */
      if ((flags & (SELECTION_WITH_SOURCE | SELECTION_WITH_BADARCH | SELECTION_WITH_DISABLED)) != 0)
	selection_addextra(pool, selection, flags);
      return SELECTION_NAME;
    }
  return 0;
}


/*****  SELECTION_DOTARCH and SELECTION_REL handling *****/

/* like selection_name, but check for a .arch suffix if the match did
   not work and SELECTION_DOTARCH is used */
static int
selection_name_arch(Pool *pool, Queue *selection, const char *name, int flags, int doprovides, int noprune)
{
  int ret;
  const char *r;
  Id archid;

  if (doprovides)
    ret = selection_provides(pool, selection, name, flags);
  else
    ret = selection_name(pool, selection, name, flags);
  if (ret)
    return ret;
  if (!(flags & SELECTION_DOTARCH))
    return 0;
  /* check if there is an .arch suffix */
  if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
    {
      char *rname = solv_strdup(name);
      rname[r - name] = 0;
      if (archid == ARCH_SRC || archid == ARCH_NOSRC)
	flags |= SELECTION_SOURCE_ONLY;
      if (doprovides)
	ret = selection_provides(pool, selection, rname, flags);
      else
	ret = selection_name(pool, selection, rname, flags);
      if (ret)
	{
	  selection_filter_rel_noprune(pool, selection, REL_ARCH, archid);
	  if (!noprune)
	    selection_prune(pool, selection);
	}
      solv_free(rname);
      return ret && selection->count ? ret | SELECTION_DOTARCH : 0;
    }
  return 0;
}

static char *
splitrel(char *rname, char *r, int *rflagsp)
{
  int nend = r - rname;
  int rflags = 0;
  if (nend && *r == '=' && r[-1] == '!')
    {
      nend--;
      r++;
      rflags = REL_LT|REL_GT;
    }
  for (; *r; r++)
    {
      if (*r == '<')
	rflags |= REL_LT;
      else if (*r == '=')
	rflags |= REL_EQ;
      else if (*r == '>')
	rflags |= REL_GT;
      else
	break;
    }
  while (*r && (*r == ' ' || *r == '\t'))
    r++;
  while (nend && (rname[nend - 1] == ' ' || rname[nend - 1] == '\t'))
    nend--;
  if (nend <= 0 || !*r || !rflags)
    return 0;
  *rflagsp = rflags;
  rname[nend] = 0;
  return r;
}

/* match name/provides, support DOTARCH and REL modifiers
 */
static int
selection_name_arch_rel(Pool *pool, Queue *selection, const char *name, int flags, int doprovides)
{
  int ret, rflags = 0, noprune;
  char *r = 0, *rname = 0;

  /* try to split off an relation part */
  if ((flags & SELECTION_REL) != 0)
    {
      if ((r = strpbrk(name, "<=>")) != 0)
	{
	  rname = solv_strdup(name);
	  r = rname + (r - name);
	  if ((r = splitrel(rname, r, &rflags)) == 0)
	    rname = solv_free(rname);
	}
    }

  /* check if we need to call selection_addextra */
  noprune = doprovides && (flags & (SELECTION_WITH_DISABLED | SELECTION_WITH_BADARCH));

  if (!r)
    {
      /* could not split off relation */
      ret = selection_name_arch(pool, selection, name, flags, doprovides, noprune);
      if (ret && noprune)
	{
	  selection_addextra(pool, selection, flags);
	  selection_prune(pool, selection);
	}
      return ret && selection->count ? ret : 0;
    }

  /* we could split of a relation. prune name and then filter rel */
  ret = selection_name_arch(pool, selection, rname, flags, doprovides, noprune);
  if (ret)
    {
      selection_filter_rel_noprune(pool, selection, rflags, pool_str2id(pool, r, 1));
      if (noprune)
        selection_addextra(pool, selection, flags);
      selection_prune(pool, selection);
    }
  solv_free(rname);
  return ret && selection->count ? ret | SELECTION_REL : 0;
}

/*****  filelist matching  *****/

static int
selection_filelist_sortcmp(const void *ap, const void *bp, void *dp)
{
  Pool *pool = dp;
  const Id *a = ap, *b = bp;
  if (a[0] != b[0])
    return strcmp(pool_id2str(pool, a[0]), pool_id2str(pool, b[0]));
  return a[1] - b[1];
}

static int
selection_filelist(Pool *pool, Queue *selection, const char *name, int flags)
{
  Dataiterator di;
  Queue q;
  Id id;
  int type;
  int i, j, lastid;

  /* all files in the file list start with a '/' */
  if (*name != '/')
    {
      if (!(flags & SELECTION_GLOB))
	return 0;
      if (*name != '*' && *name != '[' && *name != '?')
	return 0;
    }
  type = !(flags & SELECTION_GLOB) || strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
  if ((flags & SELECTION_NOCASE) != 0)
    type |= SEARCH_NOCASE;
  queue_init(&q);
  dataiterator_init(&di, pool, flags & SELECTION_INSTALLED_ONLY ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
  while (dataiterator_step(&di))
    {
      Solvable *s = pool->solvables + di.solvid;
      if (!s->repo)
	continue;
      if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
	{
	  if (!(flags & SELECTION_SOURCE_ONLY) && !(flags & SELECTION_WITH_SOURCE))
	    continue;
	  if (!(flags & SELECTION_WITH_DISABLED) && pool_disabled_solvable(pool, s))
	    continue;
	}
      else
	{
	  if ((flags & SELECTION_SOURCE_ONLY) != 0)
	    continue;
	  if (s->repo != pool->installed)
	    {
	      if (!(flags & SELECTION_WITH_DISABLED) && pool_disabled_solvable(pool, s))
		continue;
	      if (!(flags & SELECTION_WITH_BADARCH) && pool_badarch_solvable(pool, s))
		continue;
	    }
	}
      if ((flags & SELECTION_FLAT) != 0)
	{
	  /* don't bother with the complex stuff */
          queue_push2(selection, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, di.solvid);
	  dataiterator_skip_solvable(&di);
	  continue;
        }
      id = pool_str2id(pool, di.kv.str, 1);
      queue_push2(&q, id, di.solvid);
    }
  dataiterator_free(&di);
  if ((flags & SELECTION_FLAT) != 0)
    {
      queue_free(&q);
      return selection->count ? SELECTION_FILELIST : 0;
    }
  if (!q.count)
    {
      queue_free(&q);
      return 0;
    }
  solv_sort(q.elements, q.count / 2, 2 * sizeof(Id), selection_filelist_sortcmp, pool);
  lastid = 0;
  queue_push2(&q, 0, 0);
  for (i = j = 0; i < q.count; i += 2)
    {
      if (q.elements[i] != lastid)
	{
	  if (j == 1)
	    queue_pushunique2(selection, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
	  else if (j > 1)
	    {
	      int k;
	      Id *idp;
	      /* check if we already have it */
	      for (k = 0; k < selection->count; k += 2)
		{
		  if (selection->elements[k] != SOLVER_SOLVABLE_ONE_OF)
		    continue;
		  idp = pool->whatprovidesdata + selection->elements[k + 1];
		  if (!memcmp(idp, q.elements, j * sizeof(Id)) && !idp[j])
		    break;
		}
	      if (k == selection->count)
	        queue_push2(selection, SOLVER_SOLVABLE_ONE_OF, pool_ids2whatprovides(pool, q.elements, j));
	    }
          lastid = q.elements[i];
          j = 0;
	}
      if (!j || q.elements[j - 1] != q.elements[i])
        q.elements[j++] = q.elements[i + 1];
    }
  queue_free(&q);
  return SELECTION_FILELIST;
}


/*****  canon name matching  *****/

#if defined(MULTI_SEMANTICS)
# define EVRCMP_DEPCMP (pool->disttype == DISTTYPE_DEB ? EVRCMP_COMPARE : EVRCMP_MATCH_RELEASE)
#elif defined(DEBIAN)
# define EVRCMP_DEPCMP EVRCMP_COMPARE
#else
# define EVRCMP_DEPCMP EVRCMP_MATCH_RELEASE
#endif

/* magic epoch promotion code, works only for SELECTION_NAME selections */
static void
selection_filter_evr(Pool *pool, Queue *selection, const char *evr)
{
  int i, j;
  Queue q;
  Id qbuf[10];
  const char *sp;

  /* do we already have an epoch? */
  for (sp = evr; *sp >= '0' && *sp <= '9'; sp++)
    ;
  if (*sp == ':' && sp != evr)
    {
      /* yes, just add the rel filter */
      selection_filter_rel(pool, selection, REL_EQ, pool_str2id(pool, evr, 1));
      return;
    }

  queue_init(&q);
  queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
  for (i = j = 0; i < selection->count; i += 2)
    {
      Id select = selection->elements[i] & SOLVER_SELECTMASK;
      Id id = selection->elements[i + 1];
      Id p, pp;
      const char *lastepoch = 0;
      int lastepochlen = 0;

      queue_empty(&q);
      FOR_JOB_SELECT(p, pp, select, id)
	{
	  Solvable *s = pool->solvables + p;
	  const char *sevr = pool_id2str(pool, s->evr);
	  for (sp = sevr; *sp >= '0' && *sp <= '9'; sp++)
	    ;
	  if (*sp != ':')
	    sp = sevr;
	  /* compare vr part */
	  if (strcmp(evr, sp != sevr ? sp + 1 : sevr) != 0)
	    {
	      int r = pool_evrcmp_str(pool, sp != sevr ? sp + 1 : sevr, evr, EVRCMP_DEPCMP);
	      if (r == -1 || r == 1)
		continue;	/* solvable does not match vr */
	    }
	  queue_push(&q, p);
	  if (sp > sevr)
	    {
	      while (sevr < sp && *sevr == '0')	/* normalize epoch */
		sevr++;
	    }
	  if (!lastepoch)
	    {
	      lastepoch = sevr;
	      lastepochlen = sp - sevr;
	    }
	  else if (lastepochlen != sp - sevr || strncmp(lastepoch, sevr, lastepochlen) != 0)
	    lastepochlen = -1;	/* multiple different epochs */
	}
      if (!lastepoch || lastepochlen == 0)
	id = pool_str2id(pool, evr, 1);		/* no match at all or zero epoch */
      else if (lastepochlen >= 0)
	{
	  /* found exactly one epoch, simply prepend */
	  char *evrx = solv_malloc(strlen(evr) + lastepochlen + 2);
	  strncpy(evrx, lastepoch, lastepochlen + 1);
	  strcpy(evrx + lastepochlen + 1, evr);
	  id = pool_str2id(pool, evrx, 1);
	  solv_free(evrx);
	}
      else
	{
	  /* multiple epochs in multiple solvables, convert to list of solvables */
	  selection->elements[j] = (selection->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF;
	  selection->elements[j + 1] = pool_queuetowhatprovides(pool, &q);
	  j += 2;
	  continue;
	}
      queue_empty(&q);
      queue_push2(&q, selection->elements[i], selection->elements[i + 1]);
      selection_filter_rel(pool, &q, REL_EQ, id);
      if (!q.count)
        continue;		/* oops, no match */
      selection->elements[j] = q.elements[0];
      selection->elements[j + 1] = q.elements[1];
      j += 2;
    }
  queue_truncate(selection, j);
  queue_free(&q);
}

/* match the "canonical" name of the package */
static int
selection_canon(Pool *pool, Queue *selection, const char *name, int flags)
{
  char *rname, *r, *r2;
  Id archid = 0;
  int ret;

  /*
   * nameglob-version
   * nameglob-version.arch
   * nameglob-version-release
   * nameglob-version-release.arch
   */
  flags |= SELECTION_NAME;
  flags &= ~SELECTION_PROVIDES;

  if (pool->disttype == DISTTYPE_DEB)
    {
      if ((r = strchr(name, '_')) == 0)
	return 0;
      rname = solv_strdup(name);	/* so we can modify it */
      r = rname + (r - name);
      *r++ = 0;
      if ((ret = selection_name(pool, selection, rname, flags)) == 0)
	{
	  solv_free(rname);
	  return 0;
	}
      /* is there a vaild arch? */
      if ((r2 = strrchr(r, '_')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
	{
	  *r2 = 0;	/* split off */
          selection_filter_rel(pool, selection, REL_ARCH, archid);
	}
      selection_filter_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
      solv_free(rname);
      return selection->count ? ret | SELECTION_CANON : 0;
    }

  if (pool->disttype == DISTTYPE_HAIKU)
    {
      if ((r = strchr(name, '-')) == 0)
	return 0;
      rname = solv_strdup(name);	/* so we can modify it */
      r = rname + (r - name);
      *r++ = 0;
      if ((ret = selection_name(pool, selection, rname, flags)) == 0)
	{
	  solv_free(rname);
	  return 0;
	}
      /* is there a vaild arch? */
      if ((r2 = strrchr(r, '-')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
	{
	  *r2 = 0;	/* split off */
          selection_filter_rel(pool, selection, REL_ARCH, archid);
	}
      selection_filter_rel(pool, selection, REL_EQ, pool_str2id(pool, r, 1));
      solv_free(rname);
      return selection->count ? ret | SELECTION_CANON : 0;
    }

  if ((r = strrchr(name, '-')) == 0)
    return 0;
  rname = solv_strdup(name);	/* so we can modify it */
  r = rname + (r - name);
  *r = 0;

  /* split off potential arch part from version */
  if ((r2 = strrchr(r + 1, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
    *r2 = 0;	/* found valid arch, split it off */
  if (archid == ARCH_SRC || archid == ARCH_NOSRC)
    flags |= SELECTION_SOURCE_ONLY;

  /* try with just the version */
  if ((ret = selection_name(pool, selection, rname, flags)) == 0)
    {
      /* no luck, try with version-release */
      if ((r2 = strrchr(rname, '-')) == 0)
	{
	  solv_free(rname);
	  return 0;
	}
      *r = '-';
      *r2 = 0;
      r = r2;
      if ((ret = selection_name(pool, selection, rname, flags)) == 0)
	{
	  solv_free(rname);
	  return 0;
	}
    }
  if (archid)
    selection_filter_rel(pool, selection, REL_ARCH, archid);
  selection_filter_evr(pool, selection, r + 1);	/* magic epoch promotion */
  solv_free(rname);
  return selection->count ? ret | SELECTION_CANON : 0;
}

/* return the needed withbits to match the provided selection */
static int
selection_extrabits(Pool *pool, Queue *selection, int flags)
{
  int i, needflags, isextra;
  int allflags;
  Id p;
  Solvable *s;
  Queue qlimit;

  allflags = flags & (SELECTION_WITH_SOURCE | SELECTION_WITH_DISABLED | SELECTION_WITH_BADARCH);
  if (!selection->count)
    return allflags;
  if (selection->count == 2 && selection->elements[0] == SOLVER_SOLVABLE_ALL)
    return allflags;	/* don't bother */
  queue_init(&qlimit);
  selection_solvables(pool, selection, &qlimit);
  needflags = 0;
  for (i = 0; i < qlimit.count; i++)
    {
      p = qlimit.elements[i];
      s = pool->solvables + p;
      if ((flags & SELECTION_INSTALLED_ONLY) != 0 && s->repo != pool->installed)
	continue;
      isextra = 0;
      if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
	{
	  if (!(flags & SELECTION_WITH_SOURCE))
	    continue;
          isextra |= SELECTION_WITH_SOURCE;
	  if (pool_disabled_solvable(pool, s))
	    {
	      if (!(flags & SELECTION_WITH_DISABLED))
		continue;
	      isextra |= SELECTION_WITH_DISABLED;
	    }
	}
      else
	{
	  if ((flags & SELECTION_SOURCE_ONLY) != 0)
	    continue;
	  if (s->repo != pool->installed)
	    {
	      if (pool_disabled_solvable(pool, s))
		{
		  if (!(flags & SELECTION_WITH_DISABLED))
		    continue;
		  isextra |= SELECTION_WITH_DISABLED;
		}
	      if (pool_badarch_solvable(pool, s))
		{
		  if (!(flags & SELECTION_WITH_BADARCH))
		    continue;
		  isextra |= SELECTION_WITH_BADARCH;
		}
	    }
	}
      if (isextra)
	{
	  needflags |= isextra;
	  if (needflags == allflags)
	    break;
	}
    }
  queue_free(&qlimit);
  return needflags;
}

int
selection_make(Pool *pool, Queue *selection, const char *name, int flags)
{
  int ret = 0;
  if ((flags & SELECTION_MODEBITS) != 0)
    {
      Queue q;

      queue_init(&q);
      if ((flags & SELECTION_MODEBITS) == SELECTION_SUBTRACT || (flags & SELECTION_MODEBITS) == SELECTION_FILTER)
	{
	  if (!selection->count)
	    {
	      queue_free(&q);
	      return 0;
	    }
	  if ((flags & (SELECTION_WITH_DISABLED | SELECTION_WITH_BADARCH | SELECTION_WITH_SOURCE)) != 0)
	    {
	      /* try to drop expensive extra bits */
	      flags = (flags & ~(SELECTION_WITH_DISABLED | SELECTION_WITH_BADARCH | SELECTION_WITH_SOURCE)) | selection_extrabits(pool, selection, flags);
	    }
	}
      ret = selection_make(pool, &q, name, flags & ~SELECTION_MODEBITS);
      if ((flags & SELECTION_MODEBITS) == SELECTION_ADD)
	selection_add(pool, selection, &q);
      else if ((flags & SELECTION_MODEBITS) == SELECTION_SUBTRACT)
	selection_subtract(pool, selection, &q);
      else if (ret || !(flags & SELECTION_FILTER_KEEP_IFEMPTY))
	{
	  if ((flags & SELECTION_FILTER_SWAPPED) != 0)
	    {
	      selection_filter(pool, &q, selection);
	      queue_free(selection);
	      queue_init_clone(selection, &q);
	    }
	  else
	    selection_filter(pool, selection, &q);
	}
      queue_free(&q);
      return ret;
    }
  queue_empty(selection);
  if ((flags & SELECTION_INSTALLED_ONLY) != 0 && !pool->installed)
    return 0;

  /* here come our four selection modes */
  if ((flags & SELECTION_FILELIST) != 0)
    ret = selection_filelist(pool, selection, name, flags);
  if (!ret && (flags & SELECTION_NAME) != 0)
    ret = selection_name_arch_rel(pool, selection, name, flags, 0);
  if (!ret && (flags & SELECTION_PROVIDES) != 0)
    ret = selection_name_arch_rel(pool, selection, name, flags, 1);
  if (!ret && (flags & SELECTION_CANON) != 0)
    ret = selection_canon(pool, selection, name, flags);

  /* now do result filtering */
  if (ret && (flags & SELECTION_INSTALLED_ONLY) != 0)
    selection_filter_repo(pool, selection, pool->installed);

  /* flatten if requested */
  if (ret && (flags & SELECTION_FLAT) != 0)
    selection_flatten(pool, selection);
  return selection->count ? ret : 0;
}

struct limiter {
  int start;	/* either 2 or repofilter->start */
  int end;	/* either nsolvables or repofilter->end */
  Id *mapper;
  Repo *repofilter;
};

/* add matching src packages to simple SOLVABLE_NAME selections */
static void
setup_limiter(Pool *pool, int flags, struct limiter *limiter)
{
  limiter->start = 2;
  limiter->end = pool->nsolvables;
  limiter->mapper = 0;
  limiter->repofilter = 0;
  if ((flags & SELECTION_INSTALLED_ONLY) != 0)
    {
      Repo *repo = pool->installed;
      limiter->repofilter = repo;
      limiter->start = repo ? repo->start : 0;
      limiter->end = repo ? repo->end : 0;
    }
}

static int
matchdep_str(const char *pattern, const char *string, int flags)
{
  if (!pattern || !string)
    return 0;
  if (flags & SELECTION_GLOB)
    {
      int globflags = (flags & SELECTION_NOCASE) != 0 ? FNM_CASEFOLD : 0;
      return fnmatch(pattern, string, globflags) == 0 ? 1 : 0;
    }
  if (flags & SELECTION_NOCASE)
    return strcasecmp(pattern, string) == 0 ? 1 : 0;
  return strcmp(pattern, string) == 0 ? 1 : 0;
}

/* like pool_match_dep but uses matchdep_str to match the name for glob and nocase matching */
static int
matchdep(Pool *pool, Id id, char *rname, int rflags, Id revr, int flags)
{
  if (ISRELDEP(id))
    {
      Reldep *rd = GETRELDEP(pool, id);
      if (rd->flags > 7)
	{
	  if (rd->flags == REL_AND || rd->flags == REL_OR || rd->flags == REL_WITH || rd->flags == REL_WITHOUT || rd->flags == REL_COND || rd->flags == REL_UNLESS)
	    {
	      if (matchdep(pool, rd->name, rname, rflags, revr, flags))
		return 1;
	      if ((rd->flags == REL_COND || rd->flags == REL_UNLESS) && ISRELDEP(rd->evr))
		{
		  rd = GETRELDEP(pool, rd->evr);
		  if (rd->flags != REL_ELSE)
		    return 0;
		}
	      if (rd->flags != REL_COND && rd->flags != REL_UNLESS && rd->flags != REL_WITHOUT && matchdep(pool, rd->evr, rname, rflags, revr, flags))
		return 1;
	      return 0;
	    }
	  if (rd->flags == REL_ARCH)
	    return matchdep(pool, rd->name, rname, rflags, revr, flags);
	}
      if (!matchdep(pool, rd->name, rname, rflags, revr, flags))
	return 0;
      if (rflags && !pool_intersect_evrs(pool, rd->flags, rd->evr, rflags, revr))
	return 0;
      return 1;
    }
  return matchdep_str(rname, pool_id2str(pool, id), flags);
}

static int
selection_make_matchdeps_common_limited(Pool *pool, Queue *selection, const char *name, Id dep, int flags, int keyname, int marker, struct limiter *limiter)
{
  int li, i, j;
  int ret = 0;
  char *rname = 0, *r = 0;
  int rflags = 0;
  Id revr = 0;
  Id p;
  Queue q;

  queue_empty(selection);
  if (!limiter->end)
    return 0;
  if (!name && !dep)
    return 0;
  if (name && dep)
    return 0;

  if ((flags & SELECTION_MATCH_DEPSTR) != 0)
    flags &= ~SELECTION_REL;

  if (name)
    {
      rname = solv_strdup(name);
      if ((flags & SELECTION_REL) != 0)
	{
	  if ((r = strpbrk(rname, "<=>")) != 0)
	    {
	      if ((r = splitrel(rname, r, &rflags)) == 0)
		{
		  solv_free(rname);
		  return 0;
		}
	    }
	  revr = pool_str2id(pool, r, 1);
	  ret |= SELECTION_REL;
	}
      if ((flags & SELECTION_GLOB) != 0 && !strpbrk(rname, "[*?") != 0)
	flags &= ~SELECTION_GLOB;

      if ((flags & SELECTION_GLOB) == 0 && (flags & SELECTION_NOCASE) == 0 && (flags & SELECTION_MATCH_DEPSTR) == 0)
	{
	  /* we can use the faster selection_make_matchdepid */
	  dep = pool_str2id(pool, rname, 1);
	  if (rflags)
	    dep = pool_rel2id(pool, dep, revr, rflags, 1);
	  rname = solv_free(rname);
	  name = 0;
	}
    }
  if (dep)
    {
      if (keyname == SOLVABLE_NAME && (flags & SELECTION_MATCH_DEPSTR) != 0)
	{
	  Reldep *rd;
	  if (!ISRELDEP(dep))
	    return 0;
	  rd = GETRELDEP(pool, dep);
	  if (!rd->name || rd->flags != REL_EQ)
	    return 0;
	  dep = rd->name;
	  rflags = rd->flags;
	  revr = rd->evr;
	}
    }

  queue_init(&q);
  for (li = limiter->start; li < limiter->end; li++)
    {
      Solvable *s;
      p = limiter->mapper ? limiter->mapper[li] : li;
      s = pool->solvables + p;
      if (!s->repo || (limiter->repofilter && s->repo != limiter->repofilter))
	continue;
      if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
	{
	  if (!(flags & SELECTION_SOURCE_ONLY) && !(flags & SELECTION_WITH_SOURCE))
	    continue;
	  if (!(flags & SELECTION_WITH_DISABLED) && pool_disabled_solvable(pool, s))
	    continue;
	}
      else
        {
	  if ((flags & SELECTION_SOURCE_ONLY) != 0)
	    continue;
	  if (s->repo != pool->installed)
	    {
	      if (!(flags & SELECTION_WITH_DISABLED) && pool_disabled_solvable(pool, s))
		continue;
	      if (!(flags & SELECTION_WITH_BADARCH) && pool_badarch_solvable(pool, s))
		continue;
	    }
	}
      if (keyname == SOLVABLE_NAME)			/* nevr match hack */
	{
	  if (dep)
	    {
	      if ((flags & SELECTION_MATCH_DEPSTR) != 0)
		{
		  if (s->name != dep || s->evr != revr)
		    continue;
		}
	      else
		{
		  if (!pool_match_nevr(pool, s, dep))
		    continue;
		}
	    }
	  else
	    {
	      if ((flags & SELECTION_MATCH_DEPSTR) != 0)	/* mis-use */
		{
		  char *tmp = pool_tmpjoin(pool, pool_id2str(pool, s->name), " = ", pool_id2str(pool, s->evr));
		  if (!matchdep_str(rname, tmp, flags))
		    continue;
		}
	      else
		{
		  if (!matchdep(pool, s->name, rname, rflags, revr, flags))
		    continue;
		  if (rflags && !pool_intersect_evrs(pool, rflags, revr, REL_EQ, s->evr))
		    continue;
		}
	    }
	  queue_push(selection, p);
	  continue;
	}
      queue_empty(&q);
      repo_lookup_deparray(s->repo, p, keyname, &q, marker);
      if (!q.count)
	continue;
      if (dep)
	{
	  if ((flags & SELECTION_MATCH_DEPSTR) != 0)    /* mis-use */
	    {
	      for (i = 0; i < q.count; i++)
	        if (q.elements[i] == dep)
		  break;
	    }
	  else
	    {
	      for (i = 0; i < q.count; i++)
	        if (pool_match_dep(pool, q.elements[i], dep))
		  break;
	    }
	}
      else
	{
	  if ((flags & SELECTION_MATCH_DEPSTR) != 0)
	    {
	      for (i = 0; i < q.count; i++)
	        if (matchdep_str(rname, pool_dep2str(pool, q.elements[i]), flags))
		  break;
	    }
	  else
	    {
	      for (i = 0; i < q.count; i++)
	        if (matchdep(pool, q.elements[i], rname, rflags, revr, flags))
		  break;
	    }
	}
      if (i < q.count)
	queue_push(selection, p);
    }
  queue_free(&q);
  solv_free(rname);
  if (!selection->count)
    return 0;

  /* convert package list to selection */
  j = selection->count;
  queue_insertn(selection, 0, selection->count, 0);
  for (i = 0; i < selection->count; )
    {
      selection->elements[i++] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET;
      selection->elements[i++] = selection->elements[j++];
    }

  if ((flags & SELECTION_FLAT) != 0)
    selection_flatten(pool, selection);
  return ret | (keyname == SOLVABLE_NAME ? SELECTION_NAME : SELECTION_PROVIDES);
}

static int
selection_make_matchdeps_common(Pool *pool, Queue *selection, const char *name, Id dep, int flags, int keyname, int marker)
{
  struct limiter limiter;

  setup_limiter(pool, flags, &limiter);
  if ((flags & SELECTION_MODEBITS) != SELECTION_REPLACE)
    {
      int ret;
      Queue q, qlimit;
      queue_init(&q);
      queue_init(&qlimit);
      /* deal with special filter cases */
      if ((flags & SELECTION_MODEBITS) == SELECTION_FILTER && selection->count == 2 && limiter.end)
	{
	  if ((selection->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL)
	    flags = (flags & ~SELECTION_MODEBITS) | SELECTION_REPLACE;
	  else if ((selection->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_REPO)
	    {
	      Repo *repo = pool_id2repo(pool, selection->elements[1]);
	      if (limiter.repofilter && repo != limiter.repofilter)
		repo = 0;
	      limiter.repofilter = repo;
	      limiter.start = repo ? repo->start : 0;
	      limiter.end = repo ? repo->end : 0;
	      flags = (flags & ~SELECTION_MODEBITS) | SELECTION_REPLACE;
	    }
	}
      if (limiter.end && ((flags & SELECTION_MODEBITS) == SELECTION_SUBTRACT || (flags & SELECTION_MODEBITS) == SELECTION_FILTER))
	{
	  selection_solvables(pool, selection, &qlimit);
	  limiter.start = 0;
	  limiter.end = qlimit.count;
	  limiter.mapper = qlimit.elements;
	}
      ret = selection_make_matchdeps_common_limited(pool, &q, name, dep, flags & ~SELECTION_MODEBITS, keyname, marker, &limiter);
      queue_free(&qlimit);
      if ((flags & SELECTION_MODEBITS) == SELECTION_ADD)
	selection_add(pool, selection, &q);
      else if ((flags & SELECTION_MODEBITS) == SELECTION_SUBTRACT)
	selection_subtract(pool, selection, &q);
      else if ((flags & SELECTION_MODEBITS) == SELECTION_FILTER)
	{
          if (ret || !(flags & SELECTION_FILTER_KEEP_IFEMPTY))
	    {
	      if ((flags & SELECTION_FILTER_SWAPPED) != 0)
		{
		  selection_filter(pool, &q, selection);
		  queue_free(selection);
		  queue_init_clone(selection, &q);
		}
	      else
		selection_filter(pool, selection, &q);
	    }
	}
      else if (ret || !(flags & SELECTION_FILTER_KEEP_IFEMPTY))
	{
	  /* special filter case from above */
	  int i;
	  Id f = selection->elements[0] & ~(SOLVER_SELECTMASK|SOLVER_NOAUTOSET);	/* job, jobflags, setflags */
	  queue_free(selection);
	  queue_init_clone(selection, &q);
	  for (i = 0; i < selection->count; i += 2)
	    selection->elements[i] = (selection->elements[i] & (SOLVER_SELECTMASK | SOLVER_SETMASK)) | f;
	}
      queue_free(&q);
      return ret;
    }
  return selection_make_matchdeps_common_limited(pool, selection, name, dep, flags, keyname, marker, &limiter);
}

/*
 *  select against the dependencies in keyname
 *  like SELECTION_PROVIDES, but with the deps in keyname instead of provides.
 *  supported match modifiers:
 *    SELECTION_REL
 *    SELECTION_GLOB
 *    SELECTION_NOCASE
 */
int
selection_make_matchdeps(Pool *pool, Queue *selection, const char *name, int flags, int keyname, int marker)
{
  return selection_make_matchdeps_common(pool, selection, name, 0, flags, keyname, marker);
}

/*
 *  select against the dependency id in keyname
 */
int
selection_make_matchdepid(Pool *pool, Queue *selection, Id dep, int flags, int keyname, int marker)
{
  return selection_make_matchdeps_common(pool, selection, 0, dep, flags, keyname, marker);
}

static inline int
pool_is_kind(Pool *pool, Id name, Id kind)
{
  const char *n;
  if (!kind)
    return 1;
  n = pool_id2str(pool, name);
  if (kind != 1)
    {
      const char *kn = pool_id2str(pool, kind);
      int knl = strlen(kn);
      return !strncmp(n, kn, knl) && n[knl] == ':' ? 1 : 0;
    }
  else
    {
      if (*n == ':')
        return 1;
      while(*n >= 'a' && *n <= 'z')
        n++;
      return *n == ':' ? 0 : 1;
    }
}

static void
selection_filter_map(Pool *pool, Queue *sel, Map *m, int setflags)
{
  int i, j, miss;
  Queue q;
  Id p, pp;

  queue_init(&q);
  for (i = j = 0; i < sel->count; i += 2)
    {
      Id select = sel->elements[i] & SOLVER_SELECTMASK;
      queue_empty(&q);
      miss = 0;
      if (select == SOLVER_SOLVABLE_ALL)
	{
	  FOR_POOL_SOLVABLES(p)
	    {
	      if (map_tst(m, p))
	        queue_push(&q, p);
	      else
	        miss = 1;
	    }
	}
      else if (select == SOLVER_SOLVABLE_REPO)
	{
	  Solvable *s;
	  Repo *repo = pool_id2repo(pool, sel->elements[i + 1]);
	  if (repo)
	    {
	      FOR_REPO_SOLVABLES(repo, p, s)
		{
		  if (map_tst(m, p))
		    queue_push(&q, p);
		  else
		    miss = 1;
		}
	    }
	}
      else
	{
	  FOR_JOB_SELECT(p, pp, select, sel->elements[i + 1])
	    {
	      if (map_tst(m, p))
	        queue_pushunique(&q, p);
	      else
	        miss = 1;
	    }
	}
      if (!q.count)
	continue;
      if (!miss)
	{
	  sel->elements[j] = sel->elements[i] | setflags;
	  sel->elements[j + 1] = sel->elements[i + 1];
	}
      else if (q.count > 1)
	{
	  sel->elements[j] = (sel->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE_ONE_OF | setflags;
	  sel->elements[j + 1] = pool_queuetowhatprovides(pool, &q);
	}
      else
	{
	  sel->elements[j] = (sel->elements[i] & ~SOLVER_SELECTMASK) | SOLVER_SOLVABLE | SOLVER_NOAUTOSET | setflags;
	  sel->elements[j + 1] = q.elements[0];
	}
      j += 2;
    }
  queue_truncate(sel, j);
  queue_free(&q);
}

static void
selection_filter_int(Pool *pool, Queue *sel1, Queue *sel2, int invert)
{
  int i, j;
  Id p, pp, q1filled = 0;
  Queue q1;
  Map m2;
  Id setflags = 0;

  if (!sel1->count || !sel2->count)
    {
      if (invert && !sel2->count)
	return;
      queue_empty(sel1);
      return;
    }
  if (sel1->count == 2 && (sel1->elements[0] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_ALL && !invert)
    {
      /* XXX: not 100% correct, but very useful */
      p = sel1->elements[0] & ~(SOLVER_SELECTMASK | SOLVER_SETMASK);	/* job & jobflags */
      queue_free(sel1);
      queue_init_clone(sel1, sel2);
      for (i = 0; i < sel1->count; i += 2)
        sel1->elements[i] = (sel1->elements[i] & (SOLVER_SELECTMASK | SOLVER_SETMASK)) | p ;
      return;
    }

  /* convert sel2 into a map */
  queue_init(&q1);
  map_init(&m2, pool->nsolvables);
  for (i = 0; i < sel2->count; i += 2)
    {
      Id select = sel2->elements[i] & SOLVER_SELECTMASK;
      if (select == SOLVER_SOLVABLE_ALL)
	{
	  queue_free(&q1);
	  map_free(&m2);
	  if (invert)
	    queue_empty(sel1);
	  return;
	}
      if (select == SOLVER_SOLVABLE_REPO)
	{
	  Solvable *s;
	  Repo *repo = pool_id2repo(pool, sel2->elements[i + 1]);
	  if (repo)
	    {
	      FOR_REPO_SOLVABLES(repo, p, s)
	        map_set(&m2, p);
	    }
	}
      else
	{
	  if ((select == SOLVER_SOLVABLE_NAME || select == SOLVER_SOLVABLE_PROVIDES) && ISRELDEP(sel2->elements[i + 1]))
	    {
	      Reldep *rd = GETRELDEP(pool, sel2->elements[i + 1]);
	      if (rd->flags == REL_ARCH && rd->name == 0)
		{
		  /* special arch filter */
		  if (!q1filled++)
		    selection_solvables(pool, sel1, &q1);
		  for (j = 0; j < q1.count; j++)
		    {
		      Id p = q1.elements[j];
		      Solvable *s = pool->solvables + p;
		      if (s->arch == rd->evr || (rd->evr == ARCH_SRC && s->arch == ARCH_NOSRC))
		        map_set(&m2, p);
		    }
		  continue;
		}
	      else if (rd->flags == REL_KIND && rd->name == 0)
		{
		  /* special kind filter */
		  if (!q1filled++)
		    selection_solvables(pool, sel1, &q1);
		  for (j = 0; j < q1.count; j++)
		    {
		      Id p = q1.elements[j];
		      Solvable *s = pool->solvables + p;
		      if (pool_is_kind(pool, s->name, rd->evr))
		        map_set(&m2, p);
		    }
		  continue;
		}
	    }
	  FOR_JOB_SELECT(p, pp, select, sel2->elements[i + 1])
	    map_set(&m2, p);
	}
    }
  queue_free(&q1);

  /* now filter sel1 with the map */
  if (invert)
    map_invertall(&m2);
  if (sel2->count == 2)
    setflags = sel2->elements[0] & SOLVER_SETMASK & ~SOLVER_NOAUTOSET;
  selection_filter_map(pool, sel1, &m2, setflags);
  map_free(&m2);
}

void
selection_filter(Pool *pool, Queue *sel1, Queue *sel2)
{
  selection_filter_int(pool, sel1, sel2, 0);
}

void
selection_add(Pool *pool, Queue *sel1, Queue *sel2)
{
  if (sel2->count)
    queue_insertn(sel1, sel1->count, sel2->count, sel2->elements);
}

void
selection_subtract(Pool *pool, Queue *sel1, Queue *sel2)
{
  selection_filter_int(pool, sel1, sel2, 1);
}

const char *
pool_selection2str(Pool *pool, Queue *selection, Id flagmask)
{
  char *s;
  const char *s2;
  int i;
  s = pool_tmpjoin(pool, 0, 0, 0);
  for (i = 0; i < selection->count; i += 2)
    {
      Id how = selection->elements[i];
      if (*s)
	s = pool_tmpappend(pool, s, " + ", 0);
      s2 = solver_select2str(pool, how & SOLVER_SELECTMASK, selection->elements[i + 1]);
      s = pool_tmpappend(pool, s, s2, 0);
      pool_freetmpspace(pool, s2);
      how &= flagmask & SOLVER_SETMASK;
      if (how)
	{
	  int o = strlen(s);
	  s = pool_tmpappend(pool, s, " ", 0);
	  if (how & SOLVER_SETEV)
	    s = pool_tmpappend(pool, s, ",setev", 0);
	  if (how & SOLVER_SETEVR)
	    s = pool_tmpappend(pool, s, ",setevr", 0);
	  if (how & SOLVER_SETARCH)
	    s = pool_tmpappend(pool, s, ",setarch", 0);
	  if (how & SOLVER_SETVENDOR)
	    s = pool_tmpappend(pool, s, ",setvendor", 0);
	  if (how & SOLVER_SETREPO)
	    s = pool_tmpappend(pool, s, ",setrepo", 0);
	  if (how & SOLVER_NOAUTOSET)
	    s = pool_tmpappend(pool, s, ",noautoset", 0);
	  if (s[o + 1] != ',')
	    s = pool_tmpappend(pool, s, ",?", 0);
	  s[o + 1] = '[';
	  s = pool_tmpappend(pool, s, "]", 0);
	}
    }
  return s;
}