libopkg: do not modify original version string in parse_version()
[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 <stdio.h>
20 #include <ctype.h>
21 #include <unistd.h>
22
23 #include "pkg.h"
24 #include "opkg_utils.h"
25 #include "pkg_parse.h"
26 #include "pkg_depends.h"
27 #include "libbb/libbb.h"
28
29 #include "file_util.h"
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 conffile_list_t *cl;
50 char file_name[1024], md5sum[85];
51
52 if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
53 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
54 pkg->name);
55 return;
56 }
57
58 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
59
60 if (cl)
61 conffile_list_append(cl, file_name, md5sum);
62 }
63
64 int parse_version(pkg_t * pkg, const char *vstr)
65 {
66 char *colon, *rev;
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_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10));
78 if (errno) {
79 opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
80 }
81 vstr = ++colon;
82 }
83
84 rev = strrchr(vstr, '-');
85
86 if (rev) {
87 pkg_set_string(pkg, PKG_REVISION, rev + 1);
88 pkg_set_raw(pkg, PKG_VERSION, vstr, rev - vstr - 1);
89 } else {
90 pkg_set_string(pkg, PKG_VERSION, vstr);
91 }
92
93 return 0;
94 }
95
96 static char *parse_architecture(pkg_t *pkg, const char *str)
97 {
98 const char *s = str;
99 const char *e;
100
101 while (isspace(*s))
102 s++;
103
104 e = s + strlen(s);
105
106 while (e > s && isspace(*e))
107 e--;
108
109 return pkg_set_architecture(pkg, s, e - s);
110 }
111
112 int pkg_parse_line(void *ptr, const char *line, uint mask)
113 {
114 pkg_t *pkg = (pkg_t *) ptr;
115 abstract_pkg_t *ab_pkg = NULL;
116
117 /* these flags are a bit hackish... */
118 static int reading_conffiles = 0, reading_description = 0;
119 static char *description = NULL;
120 char *s;
121 int ret = 0;
122
123 /* Exclude globally masked fields. */
124 mask |= conf->pfm;
125
126 /* Flip the semantics of the mask. */
127 mask ^= PFM_ALL;
128
129 switch (*line) {
130 case 'A':
131 if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line))
132 parse_architecture(pkg, line + strlen("Architecture") + 1);
133 else if ((mask & PFM_AUTO_INSTALLED)
134 && is_field("Auto-Installed", line)) {
135 char *tmp = parse_simple("Auto-Installed", line);
136 if (strcmp(tmp, "yes") == 0)
137 pkg->auto_installed = 1;
138 free(tmp);
139 }
140 break;
141
142 case 'C':
143 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
144 reading_conffiles = 1;
145 reading_description = 0;
146 goto dont_reset_flags;
147 } else if ((mask & PFM_CONFLICTS)
148 && is_field("Conflicts", line))
149 parse_deplist(pkg, CONFLICTS, line + strlen("Conflicts") + 1);
150 break;
151
152 case 'D':
153 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
154 description = parse_simple("Description", line);
155 reading_conffiles = 0;
156 reading_description = 1;
157 goto dont_reset_flags;
158 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
159 parse_deplist(pkg, DEPEND, line + strlen("Depends") + 1);
160 break;
161
162 case 'E':
163 if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
164 char *tmp = parse_simple("Essential", line);
165 if (strcmp(tmp, "yes") == 0)
166 pkg->essential = 1;
167 free(tmp);
168 }
169 break;
170
171 case 'F':
172 if ((mask & PFM_FILENAME) && is_field("Filename", line))
173 pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1);
174 break;
175
176 case 'I':
177 if ((mask & PFM_INSTALLED_SIZE)
178 && is_field("Installed-Size", line)) {
179 pkg_set_int(pkg, PKG_INSTALLED_SIZE, strtoul(line + strlen("Installed-Size") + 1, NULL, 0));
180 } else if ((mask & PFM_INSTALLED_TIME)
181 && is_field("Installed-Time", line)) {
182 pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0));
183 }
184 break;
185
186 case 'M':
187 if ((mask & PFM_MD5SUM) && (is_field("MD5sum:", line) || is_field("MD5Sum:", line)))
188 pkg_set_md5(pkg, 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 ab_pkg = abstract_pkg_fetch_by_name(pkg->name);
198
199 if (ab_pkg && (ab_pkg->state_flag & SF_NEED_DETAIL)) {
200 if (!(pkg->state_flag & SF_NEED_DETAIL)) {
201 opkg_msg(DEPEND, "propagating abpkg flag to pkg %s\n", pkg->name);
202 pkg->state_flag |= SF_NEED_DETAIL;
203 }
204 }
205 }
206 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
207 pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
208 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
209 parse_providelist(pkg, line + strlen("Provides") + 1);
210 else if ((mask & PFM_PRE_DEPENDS)
211 && is_field("Pre-Depends", line))
212 parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1);
213 break;
214
215 case 'R':
216 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
217 parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1);
218 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
219 parse_replacelist(pkg, line + strlen("Replaces") + 1);
220 break;
221
222 case 'S':
223 if ((mask & PFM_SECTION) && is_field("Section", line))
224 pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
225 #ifdef HAVE_SHA256
226 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
227 pkg_set_sha256(pkg, line + strlen("SHA256sum") + 1);
228 #endif
229 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
230 pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0));
231 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
232 pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
233 else if ((mask & PFM_STATUS) && is_field("Status", line))
234 parse_status(pkg, line);
235 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
236 parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1);
237 break;
238
239 case 'T':
240 if ((mask & PFM_TAGS) && is_field("Tags", line))
241 pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
242 break;
243
244 case 'V':
245 if ((mask & PFM_VERSION) && is_field("Version", line))
246 parse_version(pkg, line);
247 break;
248
249 case ' ':
250 if ((mask & PFM_DESCRIPTION) && reading_description) {
251 if (isatty(1)) {
252 description = xrealloc(description,
253 strlen(description)
254 + 1 + strlen(line) +
255 1);
256 strcat(description, "\n");
257 } else {
258 description = xrealloc(description,
259 strlen(description)
260 + 1 + strlen(line));
261 }
262 strcat(description, (line));
263 goto dont_reset_flags;
264 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
265 parse_conffiles(pkg, line);
266 goto dont_reset_flags;
267 }
268
269 /* FALLTHROUGH */
270 default:
271 /* For package lists, signifies end of package. */
272 if (line_is_blank(line)) {
273 ret = 1;
274 break;
275 }
276 }
277
278 if (reading_description && description) {
279 pkg_set_string(pkg, PKG_DESCRIPTION, description);
280 free(description);
281 reading_description = 0;
282 description = NULL;
283 }
284
285 reading_conffiles = 0;
286
287 dont_reset_flags:
288
289 return ret;
290 }
291
292 int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask)
293 {
294 int ret;
295 char *buf;
296 const size_t len = 4096;
297
298 buf = xmalloc(len);
299 ret =
300 parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf,
301 len);
302 free(buf);
303
304 if (pkg->name == NULL) {
305 /* probably just a blank line */
306 ret = 1;
307 }
308
309 return ret;
310 }