pkg: use a blob buffer in pkg_t to store variable fields
[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 "libbb/libbb.h"
29
30 #include "parse_util.h"
31
32 static void parse_status(pkg_t * pkg, const char *sstr)
33 {
34         char sw_str[64], sf_str[64], ss_str[64];
35
36         if (sscanf(sstr, "Status: %63s %63s %63s", sw_str, sf_str, ss_str) != 3) {
37                 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
38                          pkg->name);
39                 return;
40         }
41
42         pkg->state_want = pkg_state_want_from_str(sw_str);
43         pkg->state_flag = pkg_state_flag_from_str(sf_str);
44         pkg->state_status = pkg_state_status_from_str(ss_str);
45 }
46
47 static void parse_conffiles(pkg_t * pkg, const char *cstr)
48 {
49         char file_name[1024], md5sum[85];
50
51         if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
52                 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
53                          pkg->name);
54                 return;
55         }
56
57         conffile_list_append(&pkg->conffiles, file_name, md5sum);
58 }
59
60 int parse_version(pkg_t * pkg, const char *vstr)
61 {
62         char *colon, *rev;
63
64         if (strncmp(vstr, "Version:", 8) == 0)
65                 vstr += 8;
66
67         while (*vstr && isspace(*vstr))
68                 vstr++;
69
70         colon = strchr(vstr, ':');
71         if (colon) {
72                 errno = 0;
73                 pkg_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10));
74                 if (errno) {
75                         opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
76                 }
77                 vstr = ++colon;
78         }
79
80         rev = strrchr(pkg_set_string(pkg, PKG_VERSION, vstr), '-');
81
82         if (rev) {
83                 *rev++ = '\0';
84                 pkg_set_ptr(pkg, PKG_REVISION, rev);
85         }
86
87         return 0;
88 }
89
90 static int get_arch_priority(const char *arch)
91 {
92         nv_pair_list_elt_t *l;
93
94         list_for_each_entry(l, &conf->arch_list.head, node) {
95                 nv_pair_t *nv = (nv_pair_t *) l->data;
96                 if (strcmp(nv->name, arch) == 0)
97                         return strtol(nv->value, NULL, 0);
98         }
99         return 0;
100 }
101
102 int pkg_parse_line(void *ptr, const char *line, uint mask)
103 {
104         pkg_t *pkg = (pkg_t *) ptr;
105
106         /* these flags are a bit hackish... */
107         static int reading_conffiles = 0, reading_description = 0;
108         static char *description = NULL;
109         int ret = 0;
110
111         /* Exclude globally masked fields. */
112         mask |= conf->pfm;
113
114         /* Flip the semantics of the mask. */
115         mask ^= PFM_ALL;
116
117         switch (*line) {
118         case 'A':
119                 if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line)) {
120                         pkg->arch_priority = get_arch_priority(
121                                 pkg_set_string(pkg, PKG_ARCHITECTURE, line + strlen("Architecture") + 1));
122
123                 } else if ((mask & PFM_AUTO_INSTALLED)
124                            && is_field("Auto-Installed", line)) {
125                         char *tmp = parse_simple("Auto-Installed", line);
126                         if (strcmp(tmp, "yes") == 0)
127                                 pkg->auto_installed = 1;
128                         free(tmp);
129                 }
130                 break;
131
132         case 'C':
133                 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
134                         reading_conffiles = 1;
135                         reading_description = 0;
136                         goto dont_reset_flags;
137                 } else if ((mask & PFM_CONFLICTS)
138                            && is_field("Conflicts", line))
139                         pkg->conflicts_str =
140                             parse_list(line, &pkg->conflicts_count, ',', 0);
141                 break;
142
143         case 'D':
144                 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
145                         description = parse_simple("Description", line);
146                         reading_conffiles = 0;
147                         reading_description = 1;
148                         goto dont_reset_flags;
149                 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
150                         pkg->depends_str =
151                             parse_list(line, &pkg->depends_count, ',', 0);
152                 break;
153
154         case 'E':
155                 if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
156                         char *tmp = parse_simple("Essential", line);
157                         if (strcmp(tmp, "yes") == 0)
158                                 pkg->essential = 1;
159                         free(tmp);
160                 }
161                 break;
162
163         case 'F':
164                 if ((mask & PFM_FILENAME) && is_field("Filename", line))
165                         pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1);
166                 break;
167
168         case 'I':
169                 if ((mask & PFM_INSTALLED_SIZE)
170                     && is_field("Installed-Size", line)) {
171                         char *tmp = parse_simple("Installed-Size", line);
172                         pkg->installed_size = strtoul(tmp, NULL, 0);
173                         free(tmp);
174                 } else if ((mask & PFM_INSTALLED_TIME)
175                            && is_field("Installed-Time", line)) {
176                         char *tmp = parse_simple("Installed-Time", line);
177                         pkg->installed_time = strtoul(tmp, NULL, 0);
178                         free(tmp);
179                 }
180                 break;
181
182         case 'M':
183                 if ((mask & PFM_MD5SUM) && is_field("MD5sum:", line))
184                         pkg_set_string(pkg, PKG_MD5SUM, line + strlen("MD5sum") + 1);
185                 /* The old opkg wrote out status files with the wrong
186                  * case for MD5sum, let's parse it either way */
187                 else if ((mask & PFM_MD5SUM) && is_field("MD5Sum:", line))
188                         pkg_set_string(pkg, PKG_MD5SUM, line + strlen("MD5Sum") + 1);
189                 else if ((mask & PFM_MAINTAINER)
190                          && is_field("Maintainer", line))
191                         pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1);
192                 break;
193
194         case 'P':
195                 if ((mask & PFM_PACKAGE) && is_field("Package", line))
196                         pkg->name = parse_simple("Package", line);
197                 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
198                         pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
199                 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
200                         pkg->provides_str =
201                             parse_list(line, &pkg->provides_count, ',', 0);
202                 else if ((mask & PFM_PRE_DEPENDS)
203                          && is_field("Pre-Depends", line))
204                         pkg->pre_depends_str =
205                             parse_list(line, &pkg->pre_depends_count, ',', 0);
206                 break;
207
208         case 'R':
209                 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
210                         pkg->recommends_str =
211                             parse_list(line, &pkg->recommends_count, ',', 0);
212                 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
213                         pkg->replaces_str =
214                             parse_list(line, &pkg->replaces_count, ',', 0);
215
216                 break;
217
218         case 'S':
219                 if ((mask & PFM_SECTION) && is_field("Section", line))
220                         pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
221 #ifdef HAVE_SHA256
222                 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
223                         pkg_set_string(pkg, PKG_SHA256SUM, line + strlen("SHA256sum") + 1);
224 #endif
225                 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
226                         char *tmp = parse_simple("Size", line);
227                         pkg->size = strtoul(tmp, NULL, 0);
228                         free(tmp);
229                 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
230                         pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
231                 else if ((mask & PFM_STATUS) && is_field("Status", line))
232                         parse_status(pkg, line);
233                 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
234                         pkg->suggests_str =
235                             parse_list(line, &pkg->suggests_count, ',', 0);
236                 break;
237
238         case 'T':
239                 if ((mask & PFM_TAGS) && is_field("Tags", line))
240                         pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
241                 break;
242
243         case 'V':
244                 if ((mask & PFM_VERSION) && is_field("Version", line))
245                         parse_version(pkg, line);
246                 break;
247
248         case ' ':
249                 if ((mask & PFM_DESCRIPTION) && reading_description) {
250                         if (isatty(1)) {
251                                 description = xrealloc(description,
252                                                             strlen(description)
253                                                             + 1 + strlen(line) +
254                                                             1);
255                                 strcat(description, "\n");
256                         } else {
257                                 description = xrealloc(description,
258                                                             strlen(description)
259                                                             + 1 + strlen(line));
260                         }
261                         strcat(description, (line));
262                         goto dont_reset_flags;
263                 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
264                         parse_conffiles(pkg, line);
265                         goto dont_reset_flags;
266                 }
267
268                 /* FALLTHROUGH */
269         default:
270                 /* For package lists, signifies end of package. */
271                 if (line_is_blank(line)) {
272                         ret = 1;
273                         break;
274                 }
275         }
276
277         if (reading_description && description) {
278                 pkg_set_string(pkg, PKG_DESCRIPTION, description);
279                 free(description);
280                 reading_description = 0;
281                 description = NULL;
282         }
283
284         reading_conffiles = 0;
285
286 dont_reset_flags:
287
288         return ret;
289 }
290
291 int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask)
292 {
293         int ret;
294         char *buf;
295         const size_t len = 4096;
296
297         buf = xmalloc(len);
298         ret =
299             parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf,
300                                        len);
301         free(buf);
302
303         if (pkg->name == NULL) {
304                 /* probably just a blank line */
305                 ret = 1;
306         }
307
308         return ret;
309 }