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