Enable ATTR_INTLIST to contain zeros. Use the same encoding as IDARRAY.
[platform/upstream/libsolv.git] / tools / dumpsolv.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 #include "pool.h"
14 #include "repo_solv.h"
15 #include "attr_store.h"
16 #include "attr_store_p.h"
17
18 static void
19 dump_attrs_1 (Attrstore *s, unsigned int entry)
20 {
21   attr_iterator ai;
22   FOR_ATTRS (s, entry, &ai)
23     {
24       fprintf (stdout, "%s:", id2str (s->pool, ai.name));
25       switch (ai.type)
26         {
27         case TYPE_ATTR_INT:
28           fprintf (stdout, "int  %u\n", ai.as_int);
29           break;
30         case TYPE_ATTR_CHUNK:
31           {
32             const char *str = attr_retrieve_blob (s, ai.as_chunk[0], ai.as_chunk[1]);
33             if (str)
34               fprintf (stdout, "blob %s\n", str);
35             else
36               fprintf (stdout, "blob %u+%u\n", ai.as_chunk[0], ai.as_chunk[1]);
37           }
38           break;
39         case TYPE_ATTR_STRING:
40           fprintf (stdout, "str  %s\n", ai.as_string);
41           break;
42         case TYPE_ATTR_INTLIST:
43           {
44             fprintf (stdout, "lint\n ");
45             while (1)
46               {
47                 int val;
48                 get_num (ai.as_numlist, val);
49                 fprintf (stdout, " %d", (val & 63) | ((val >> 1) & ~63));
50                 if (!(val & 64))
51                   break;
52               }
53             fprintf (stdout, "\n");
54             break;
55           }
56         case TYPE_ATTR_LOCALIDS:
57           {
58             fprintf (stdout, "lids");
59             while (1)
60               {
61                 Id val;
62                 get_num (ai.as_numlist, val);
63                 if (!val)
64                   break;
65                 fprintf (stdout, "\n  %s(%d)", localid2str (s, val), val);
66               }
67             fprintf (stdout, "\n");
68             break;
69           }
70         default:
71           fprintf (stdout, "\n");
72           break;
73         }
74     }
75 }
76
77 static void
78 dump_attrs (Repo *repo, unsigned int entry)
79 {
80   unsigned i;
81   for (i = 0; i < repo->nrepodata; i++)
82     {
83       Attrstore *s = repo->repodata[i].s;
84       if (s && entry < s->entries)
85         dump_attrs_1 (s, entry);
86     }
87 }
88
89 static void
90 dump_repodata (Repo *repo)
91 {
92   unsigned i;
93   Repodata *data;
94   if (repo->nrepodata == 0)
95     return;
96   printf("repo refers to %d attribute stores:\n", repo->nrepodata);
97   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
98     {
99       unsigned int j;
100       printf("%s has %d keys", data->location ? data->location : "**EMBED**", data->nkeys);
101       for (j = 0; j < data->nkeys; j++)
102         printf("\n  %s", id2str(repo->pool, data->keys[j].name));
103       printf("\n");
104     }
105   printf("\n");
106 }
107
108 static void
109 printids(Repo *repo, char *kind, Offset ido)
110 {
111   Pool *pool = repo->pool;
112   Id id, *ids;
113   if (!ido)
114     return;
115   printf("%s:\n", kind);
116   ids = repo->idarraydata + ido;
117   while((id = *ids++) != 0)
118     printf("  %s\n", dep2str(pool, id));
119 }
120
121 int main(int argc, char **argv)
122 {
123   Repo *repo;
124   Pool *pool;
125   int i, n;
126   Solvable *s;
127
128   if (argc != 1)
129     {
130       if (freopen(argv[1], "r", stdin) == 0)
131         {
132           perror(argv[1]);
133           exit(1);
134         }
135     }
136   pool = pool_create();
137   repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
138   repo_add_solv(repo, stdin);
139   dump_repodata (repo);
140   printf("repo contains %d solvables\n", repo->nsolvables);
141   for (i = repo->start, n = 1; i < repo->end; i++)
142     {
143       s = pool->solvables + i;
144       if (s->repo != repo)
145         continue;
146       printf("\n");
147       printf("solvable %d:\n", n);
148       if (s->name || s->evr || s->arch)
149         printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
150       if (s->vendor)
151         printf("vendor: %s\n", id2str(pool, s->vendor));
152       printids(repo, "provides", s->provides);
153       printids(repo, "obsoletes", s->obsoletes);
154       printids(repo, "conflicts", s->conflicts);
155       printids(repo, "requires", s->requires);
156       printids(repo, "recommends", s->recommends);
157       printids(repo, "suggests", s->suggests);
158       printids(repo, "supplements", s->supplements);
159       printids(repo, "enhances", s->enhances);
160       printids(repo, "freshens", s->freshens);
161       dump_attrs (repo, n - 1);
162       n++;
163     }
164   pool_free(pool);
165   exit(0);
166 }