pkg: move active_list structure out of pkg_t
[oweals/opkg-lede.git] / libopkg / pkg_parse.c
1 /* pkg_parse.c - the opkg package management system
2
3    Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5    Steven M. Ayer
6    Copyright (C) 2002 Compaq Computer Corporation
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 */
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <unistd.h>
24
25 #include "pkg.h"
26 #include "opkg_utils.h"
27 #include "pkg_parse.h"
28 #include "pkg_depends.h"
29 #include "libbb/libbb.h"
30
31 #include "parse_util.h"
32
33 static void parse_status(pkg_t * pkg, const char *sstr)
34 {
35         char sw_str[64], sf_str[64], ss_str[64];
36
37         if (sscanf(sstr, "Status: %63s %63s %63s", sw_str, sf_str, ss_str) != 3) {
38                 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
39                          pkg->name);
40                 return;
41         }
42
43         pkg->state_want = pkg_state_want_from_str(sw_str);
44         pkg->state_flag = pkg_state_flag_from_str(sf_str);
45         pkg->state_status = pkg_state_status_from_str(ss_str);
46 }
47
48 static void parse_conffiles(pkg_t * pkg, const char *cstr)
49 {
50         conffile_list_t *cl;
51         char file_name[1024], md5sum[85];
52
53         if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
54                 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
55                          pkg->name);
56                 return;
57         }
58
59         cl = pkg_get_ptr(pkg, PKG_CONFFILES);
60
61         if (cl)
62                 conffile_list_append(cl, file_name, md5sum);
63 }
64
65 int parse_version(pkg_t * pkg, const char *vstr)
66 {
67         char *colon, *rev;
68
69         if (strncmp(vstr, "Version:", 8) == 0)
70                 vstr += 8;
71
72         while (*vstr && isspace(*vstr))
73                 vstr++;
74
75         colon = strchr(vstr, ':');
76         if (colon) {
77                 errno = 0;
78                 pkg_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10));
79                 if (errno) {
80                         opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
81                 }
82                 vstr = ++colon;
83         }
84
85         rev = strrchr(vstr, '-');
86
87         if (rev) {
88                 *rev++ = '\0';
89                 pkg_set_string(pkg, PKG_REVISION, rev);
90         }
91
92         pkg_set_string(pkg, PKG_VERSION, vstr);
93
94         return 0;
95 }
96
97 static int get_arch_priority(const char *arch)
98 {
99         nv_pair_list_elt_t *l;
100
101         list_for_each_entry(l, &conf->arch_list.head, node) {
102                 nv_pair_t *nv = (nv_pair_t *) l->data;
103                 if (strcmp(nv->name, arch) == 0)
104                         return strtol(nv->value, NULL, 0);
105         }
106         return 0;
107 }
108
109 int pkg_parse_line(void *ptr, const char *line, uint mask)
110 {
111         pkg_t *pkg = (pkg_t *) ptr;
112         abstract_pkg_t *ab_pkg = NULL;
113
114         /* these flags are a bit hackish... */
115         static int reading_conffiles = 0, reading_description = 0;
116         static char *description = NULL;
117         int ret = 0;
118
119         /* Exclude globally masked fields. */
120         mask |= conf->pfm;
121
122         /* Flip the semantics of the mask. */
123         mask ^= PFM_ALL;
124
125         switch (*line) {
126         case 'A':
127                 if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line)) {
128                         pkg_set_int(pkg, PKG_ARCH_PRIORITY, get_arch_priority(
129                                 pkg_set_string(pkg, PKG_ARCHITECTURE, line + strlen("Architecture") + 1)));
130
131                 } else if ((mask & PFM_AUTO_INSTALLED)
132                            && is_field("Auto-Installed", line)) {
133                         char *tmp = parse_simple("Auto-Installed", line);
134                         if (strcmp(tmp, "yes") == 0)
135                                 pkg->auto_installed = 1;
136                         free(tmp);
137                 }
138                 break;
139
140         case 'C':
141                 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
142                         reading_conffiles = 1;
143                         reading_description = 0;
144                         goto dont_reset_flags;
145                 } else if ((mask & PFM_CONFLICTS)
146                            && is_field("Conflicts", line))
147                         parse_deplist(pkg, CONFLICTS, line + strlen("Conflicts") + 1);
148                 break;
149
150         case 'D':
151                 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
152                         description = parse_simple("Description", line);
153                         reading_conffiles = 0;
154                         reading_description = 1;
155                         goto dont_reset_flags;
156                 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
157                         parse_deplist(pkg, DEPEND, line + strlen("Depends") + 1);
158                 break;
159
160         case 'E':
161                 if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
162                         char *tmp = parse_simple("Essential", line);
163                         if (strcmp(tmp, "yes") == 0)
164                                 pkg->essential = 1;
165                         free(tmp);
166                 }
167                 break;
168
169         case 'F':
170                 if ((mask & PFM_FILENAME) && is_field("Filename", line))
171                         pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1);
172                 break;
173
174         case 'I':
175                 if ((mask & PFM_INSTALLED_SIZE)
176                     && is_field("Installed-Size", line)) {
177                         pkg_set_int(pkg, PKG_INSTALLED_SIZE, strtoul(line + strlen("Installed-Size") + 1, NULL, 0));
178                 } else if ((mask & PFM_INSTALLED_TIME)
179                            && is_field("Installed-Time", line)) {
180                         pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0));
181                 }
182                 break;
183
184         case 'M':
185                 if ((mask & PFM_MD5SUM) && is_field("MD5sum:", line))
186                         pkg_set_string(pkg, PKG_MD5SUM, line + strlen("MD5sum") + 1);
187                 /* The old opkg wrote out status files with the wrong
188                  * case for MD5sum, let's parse it either way */
189                 else if ((mask & PFM_MD5SUM) && is_field("MD5Sum:", line))
190                         pkg_set_string(pkg, PKG_MD5SUM, line + strlen("MD5Sum") + 1);
191                 else if ((mask & PFM_MAINTAINER)
192                          && is_field("Maintainer", line))
193                         pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1);
194                 break;
195
196         case 'P':
197                 if ((mask & PFM_PACKAGE) && is_field("Package", line))
198                         pkg->name = parse_simple("Package", line);
199                 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
200                         pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
201                 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
202                         parse_providelist(pkg, line + strlen("Provides") + 1);
203                 else if ((mask & PFM_PRE_DEPENDS)
204                          && is_field("Pre-Depends", line))
205                         parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1);
206                 break;
207
208         case 'R':
209                 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
210                         parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1);
211                 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
212                         parse_replacelist(pkg, line + strlen("Replaces") + 1);
213                 break;
214
215         case 'S':
216                 if ((mask & PFM_SECTION) && is_field("Section", line))
217                         pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
218 #ifdef HAVE_SHA256
219                 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
220                         pkg_set_string(pkg, PKG_SHA256SUM, line + strlen("SHA256sum") + 1);
221 #endif
222                 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
223                         pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0));
224                 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
225                         pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
226                 else if ((mask & PFM_STATUS) && is_field("Status", line))
227                         parse_status(pkg, line);
228                 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
229                         parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1);
230                 break;
231
232         case 'T':
233                 if ((mask & PFM_TAGS) && is_field("Tags", line))
234                         pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
235                 break;
236
237         case 'V':
238                 if ((mask & PFM_VERSION) && is_field("Version", line))
239                         parse_version(pkg, line);
240                 break;
241
242         case ' ':
243                 if ((mask & PFM_DESCRIPTION) && reading_description) {
244                         if (isatty(1)) {
245                                 description = xrealloc(description,
246                                                             strlen(description)
247                                                             + 1 + strlen(line) +
248                                                             1);
249                                 strcat(description, "\n");
250                         } else {
251                                 description = xrealloc(description,
252                                                             strlen(description)
253                                                             + 1 + strlen(line));
254                         }
255                         strcat(description, (line));
256                         goto dont_reset_flags;
257                 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
258                         parse_conffiles(pkg, line);
259                         goto dont_reset_flags;
260                 }
261
262                 /* FALLTHROUGH */
263         default:
264                 /* For package lists, signifies end of package. */
265                 if (line_is_blank(line)) {
266                         ret = 1;
267                         break;
268                 }
269         }
270
271         if (reading_description && description) {
272                 pkg_set_string(pkg, PKG_DESCRIPTION, description);
273                 free(description);
274                 reading_description = 0;
275                 description = NULL;
276         }
277
278         reading_conffiles = 0;
279
280 dont_reset_flags:
281
282         return ret;
283 }
284
285 int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask)
286 {
287         int ret;
288         char *buf;
289         const size_t len = 4096;
290
291         buf = xmalloc(len);
292         ret =
293             parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf,
294                                        len);
295         free(buf);
296
297         if (pkg->name == NULL) {
298                 /* probably just a blank line */
299                 ret = 1;
300         }
301
302         return ret;
303 }