- kill stillborn KINDS_SEPARATELY, use getopt() in tools
[platform/upstream/libsolv.git] / tools / rpmmd2solv.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 #define _GNU_SOURCE
9
10 #include <sys/types.h>
11 #include <limits.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <zlib.h>
18
19 #include "pool.h"
20 #include "repo.h"
21 #include "repo_rpmmd.h"
22 #include "common_write.h"
23
24
25 static ssize_t
26 cookie_gzread(void *cookie, char *buf, size_t nbytes)
27 {
28   return gzread((gzFile *)cookie, buf, nbytes);
29 }
30
31 static int
32 cookie_gzclose(void *cookie)
33 {
34   return gzclose((gzFile *)cookie);
35 }
36
37 FILE *
38 myfopen(const char *fn)
39 {
40   cookie_io_functions_t cio;
41   char *suf;
42   gzFile *gzf;
43
44   if (!fn)
45     return 0;
46   suf = strrchr(fn, '.');
47   if (!suf || strcmp(suf, ".gz") != 0)
48     return fopen(fn, "r");
49   gzf = gzopen(fn, "r");
50   if (!gzf)
51     return 0;
52   memset(&cio, 0, sizeof(cio));
53   cio.read = cookie_gzread;
54   cio.close = cookie_gzclose;
55   return  fopencookie(gzf, "r", cio);
56 }
57
58 static void
59 usage(int status)
60 {
61   fprintf(stderr, "\nUsage:\n"
62           "rpmmd2solv [-a][-h][-n <attrname>][-l <locale>]\n"
63           "  reads 'primary' from a 'rpmmd' repository from <stdin> and writes a .solv file to <stdout>\n"
64           "  -h : print help & exit\n"
65           "  -n <name>: save attributes as <name>.attr\n"
66           "  -l <locale>: parse localization data for <locale>\n"
67          );
68    exit(status);
69 }
70
71 int
72 main(int argc, char **argv)
73 {
74   int c, flags = 0;
75   const char *attrname = 0;
76   const char *basefile = 0;
77   const char *dir = 0;
78   const char *locale = 0;
79   
80   Pool *pool = pool_create();
81   Repo *repo = repo_create(pool, "<stdin>");
82
83   while ((c = getopt (argc, argv, "hn:b:d:l:")) >= 0)
84     {
85       switch(c)
86         {
87         case 'h':
88           usage(0);
89           break;
90         case 'n':
91           attrname = optarg;
92           break;
93         case 'b':
94           basefile = optarg;
95           break;
96         case 'd':
97           dir = optarg;
98           break;
99         case 'l':
100           locale = optarg;
101           break;
102         default:
103           usage(1);
104           break;
105         }
106     }
107   if (dir)
108     {
109       FILE *fp;
110       int l;
111       char *fnp;
112       l = strlen(dir) + 128;
113       fnp = sat_malloc(l+1);
114       snprintf(fnp, l, "%s/primary.xml.gz", dir);
115       if (!(fp = myfopen(fnp)))
116         {
117           perror(fnp);
118           exit(1);
119         }
120       repo_add_rpmmd(repo, fp, 0, flags);
121       fclose(fp);
122       snprintf(fnp, l, "%s/diskusagedata.xml.gz", dir);
123       if ((fp = myfopen(fnp)))
124         {
125           repo_add_rpmmd(repo, fp, 0, flags);
126           fclose(fp);
127         }
128       if (locale)
129         {
130           if (snprintf(fnp, l, "%s/translation-%s.xml.gz", dir, locale) >= l)
131             {
132               fprintf(stderr, "-l parameter too long\n");
133               exit(1);
134             }
135           while (!(fp = myfopen(fnp)))
136             {
137               fprintf(stderr, "not opened %s\n", fnp);
138               if (strlen(locale) > 2)
139                 {
140                   if (snprintf(fnp, l, "%s/translation-%.2s.xml.gz", dir, locale) >= l)
141                     {
142                       fprintf(stderr, "-l parameter too long\n");
143                       exit(1);
144                     }
145                   if ((fp = myfopen(fnp)))
146                     break;
147                 }
148               perror(fnp);
149               exit(1);
150             }
151           fprintf(stderr, "opened %s\n", fnp);
152           repo_add_rpmmd(repo, fp, 0, flags);
153           fclose(fp);
154         }
155       sat_free(fnp);
156     }
157   else
158     repo_add_rpmmd(repo, stdin, 0, flags);
159   tool_write(repo, basefile, attrname);
160   pool_free(pool);
161   exit(0);
162 }