Initial stab at untangling the #include maze. Probably needs a second pass.
[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 <stdio.h>
20 #include <ctype.h>
21
22 #include "pkg.h"
23 #include "opkg_utils.h"
24 #include "pkg_parse.h"
25 #include "libbb/libbb.h"
26
27 static int
28 is_field(const char *type, const char *line)
29 {
30         if (!strncmp(line, type, strlen(type)))
31                 return 1;
32         return 0;
33 }
34
35 static char *
36 parse_simple(const char *type, const char *line)
37 {
38         return trim_xstrdup(line + strlen(type) + 1);
39 }
40
41 /*
42  * Parse a comma separated string into an array.
43  */
44 static char **
45 parse_comma_separated(const char *raw, unsigned int *count)
46 {
47         char **depends = NULL;
48         const char *start, *end;
49         int line_count = 0;
50
51         /* skip past the "Field:" marker */
52         while (*raw && *raw != ':')
53                 raw++;
54         raw++;
55
56         if (line_is_blank(raw)) {
57                 *count = line_count;
58                 return NULL;
59         }
60
61         while (*raw) {
62                 depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
63         
64                 while (isspace(*raw))
65                         raw++;
66
67                 start = raw;
68                 while (*raw != ',' && *raw)
69                         raw++;
70                 end = raw;
71
72                 while (end > start && isspace(*end))
73                         end--;
74
75                 depends[line_count] = xstrndup(start, end-start);
76
77                 line_count++;
78                 if (*raw == ',')
79                     raw++;
80         }
81
82         *count = line_count;
83         return depends;
84 }
85
86 static void
87 parse_status(pkg_t *pkg, const char *sstr)
88 {
89         char sw_str[64], sf_str[64], ss_str[64];
90
91         if (sscanf(sstr, "Status: %63s %63s %63s",
92                                 sw_str, sf_str, ss_str) != 3) {
93                 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
94                                 pkg->name);
95                 return;
96         }
97
98         pkg->state_want = pkg_state_want_from_str(sw_str);
99         pkg->state_flag = pkg_state_flag_from_str(sf_str);
100         pkg->state_status = pkg_state_status_from_str(ss_str);
101 }
102
103 static void
104 parse_conffiles(pkg_t *pkg, const char *cstr)
105 {
106         char file_name[1024], md5sum[35];
107
108         if (sscanf(cstr, "%1023s %34s", file_name, md5sum) != 2) {
109                 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
110                                 pkg->name);
111                 return;
112         }
113
114         conffile_list_append(&pkg->conffiles, file_name, md5sum);
115 }
116
117 int
118 parse_version(pkg_t *pkg, const char *vstr)
119 {
120         char *colon;
121
122         if (strncmp(vstr, "Version:", 8) == 0)
123                 vstr += 8;
124
125         while (*vstr && isspace(*vstr))
126                 vstr++;
127
128         colon = strchr(vstr, ':');
129         if (colon) {
130                 errno = 0;
131                 pkg->epoch = strtoul(vstr, NULL, 10);
132                 if (errno) {
133                         opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
134                 }
135                 vstr = ++colon;
136         } else {
137                 pkg->epoch= 0;
138         }
139
140         pkg->version= xstrdup(vstr);
141         pkg->revision = strrchr(pkg->version,'-');
142
143         if (pkg->revision)
144                 *pkg->revision++ = '\0';
145
146         return 0;
147 }
148
149 static int
150 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
151 {
152         /* these flags are a bit hackish... */
153         static int reading_conffiles = 0, reading_description = 0;
154         int ret = 0;
155
156         /* Exclude globally masked fields. */
157         mask |= conf->pfm;
158
159         /* Flip the semantics of the mask. */
160         mask ^= PFM_ALL;
161
162         switch (*line) {
163         case 'A':
164                 if ((mask & PFM_ARCHITECTURE ) && is_field("Architecture", line))
165                         pkg->architecture = parse_simple("Architecture", line);
166                 else if ((mask & PFM_AUTO_INSTALLED) && is_field("Auto-Installed", line)) {
167                         char *tmp = parse_simple("Auto-Installed", line);
168                         if (strcmp(tmp, "yes") == 0)
169                             pkg->auto_installed = 1;
170                         free(tmp);
171                 }
172                 break;
173
174         case 'C':
175                 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
176                         reading_conffiles = 1;
177                         reading_description = 0;
178                         goto dont_reset_flags;
179                 }
180                 else if ((mask & PFM_CONFLICTS) && is_field("Conflicts", line))
181                         pkg->conflicts_str = parse_comma_separated(line, &pkg->conflicts_count);
182                 break;
183
184         case 'D':
185                 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
186                         pkg->description = parse_simple("Description", line);
187                         reading_conffiles = 0;
188                         reading_description = 1;
189                         goto dont_reset_flags;
190                 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
191                         pkg->depends_str = parse_comma_separated(line, &pkg->depends_count);
192                 break;
193
194         case 'E':
195                 if((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
196                         char *tmp = parse_simple("Essential", line);
197                         if (strcmp(tmp, "yes") == 0)
198                                 pkg->essential = 1;
199                         free(tmp);
200                 }
201                 break;
202
203         case 'F':
204                 if((mask & PFM_FILENAME) && is_field("Filename", line))
205                         pkg->filename = parse_simple("Filename", line);
206                 break;
207
208         case 'I':
209                 if ((mask && PFM_INSTALLED_SIZE) && is_field("Installed-Size", line)) {
210                         char *tmp = parse_simple("Installed-Size", line);
211                         pkg->installed_size = strtoul(tmp, NULL, 0);
212                         free (tmp);
213                 } else if ((mask && PFM_INSTALLED_TIME) && is_field("Installed-Time", line)) {
214                         char *tmp = parse_simple("Installed-Time", line);
215                         pkg->installed_time = strtoul(tmp, NULL, 0);
216                         free (tmp);
217                 }           
218                 break;
219
220         case 'M':
221                 if (mask && PFM_MD5SUM) {
222                         if (is_field("MD5sum:", line))
223                                 pkg->md5sum = parse_simple("MD5sum", line);
224                         /* The old opkg wrote out status files with the wrong
225                         * case for MD5sum, let's parse it either way */
226                         else if (is_field("MD5Sum:", line))
227                                 pkg->md5sum = parse_simple("MD5Sum", line);
228                 } else if((mask & PFM_MAINTAINER) && is_field("Maintainer", line))
229                         pkg->maintainer = parse_simple("Maintainer", line);
230                 break;
231
232         case 'P':
233                 if ((mask & PFM_PACKAGE) && is_field("Package", line)) 
234                         pkg->name = parse_simple("Package", line);
235                 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
236                         pkg->priority = parse_simple("Priority", line);
237                 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
238                         pkg->provides_str = parse_comma_separated(line, &pkg->provides_count);
239                 else if ((mask & PFM_PRE_DEPENDS) && is_field("Pre-Depends", line))
240                         pkg->pre_depends_str = parse_comma_separated(line, &pkg->pre_depends_count);
241                 break;
242
243         case 'R':
244                 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
245                         pkg->recommends_str = parse_comma_separated(line, &pkg->recommends_count);
246                 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
247                         pkg->replaces_str = parse_comma_separated(line, &pkg->replaces_count);
248
249                 break;
250
251         case 'S':
252                 if ((mask & PFM_SECTION) && is_field("Section", line))
253                         pkg->section = parse_simple("Section", line);
254 #ifdef HAVE_SHA256
255                 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
256                         pkg->sha256sum = parse_simple("SHA256sum", line);
257 #endif
258                 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
259                         char *tmp = parse_simple("Size", line);
260                         pkg->size = strtoul(tmp, NULL, 0);
261                         free (tmp);
262                 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
263                         pkg->source = parse_simple("Source", line);
264                 else if ((mask & PFM_STATUS) && is_field("Status", line))
265                         parse_status(pkg, line);
266                 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
267                         pkg->suggests_str = parse_comma_separated(line, &pkg->suggests_count);
268                 break;
269
270         case 'T':
271                 if ((mask & PFM_TAGS) && is_field("Tags", line))
272                         pkg->tags = parse_simple("Tags", line);
273                 break;
274
275         case 'V':
276                 if ((mask & PFM_VERSION) && is_field("Version", line))
277                         parse_version(pkg, line);
278                 break;
279
280         case ' ':
281                 if ((mask & PFM_DESCRIPTION) && reading_description) {
282                         pkg->description = xrealloc(pkg->description,
283                                                 strlen(pkg->description)
284                                                 + 1 + strlen(line) + 1);
285                         strcat(pkg->description, "\n");
286                         strcat(pkg->description, (line));
287                         goto dont_reset_flags;
288                 } else if ((mask && PFM_CONFFILES) && reading_conffiles) {
289                         parse_conffiles(pkg, line);
290                         goto dont_reset_flags;
291                 }
292
293                 /* FALLTHROUGH */
294         default:
295                 /* For package lists, signifies end of package. */
296                 if(line_is_blank(line)) {
297                         ret = 1;
298                         break;
299                 }
300         }
301
302         reading_description = 0;
303         reading_conffiles = 0;
304
305 dont_reset_flags:
306
307         return ret;
308 }
309
310 int
311 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
312                                                 char **buf0, size_t buf0len)
313 {
314         int ret, lineno;
315         char *buf, *nl;
316         size_t buflen;
317
318         lineno = 1;
319         ret = 0;
320
321         buflen = buf0len;
322         buf = *buf0;
323         buf[0] = '\0';
324
325         while (1) {
326                 if (fgets(buf, (int)buflen, fp) == NULL) {
327                         if (ferror(fp)) {
328                                 opkg_perror(ERROR, "fgets");
329                                 ret = -1;
330                         } else if (strlen(*buf0) == buf0len-1) {
331                                 opkg_msg(ERROR, "Missing new line character"
332                                                 " at end of file!\n");
333                                 pkg_parse_line(pkg, *buf0, mask);
334                         }
335                         break;
336                 }
337
338                 nl = strchr(buf, '\n');
339                 if (nl == NULL) {
340                         if (strlen(buf) < buflen-1) {
341                                 /*
342                                  * Line could be exactly buflen-1 long and
343                                  * missing a newline, but we won't know until
344                                  * fgets fails to read more data.
345                                  */
346                                 opkg_msg(ERROR, "Missing new line character"
347                                                 " at end of file!\n");
348                                 pkg_parse_line(pkg, *buf0, mask);
349                                 break;
350                         }
351                         if (buf0len >= EXCESSIVE_LINE_LEN) {
352                                 opkg_msg(ERROR, "Excessively long line at "
353                                         "%d. Corrupt file?\n",
354                                         lineno);
355                                 ret = -1;
356                                 break;
357                         }
358
359                         /*
360                          * Realloc and point buf past the data already read,
361                          * at the NULL terminator inserted by fgets.
362                          * |<--------------- buf0len ----------------->|
363                          * |                     |<------- buflen ---->|
364                          * |---------------------|---------------------|
365                          * buf0                   buf
366                          */
367                         buflen = buf0len +1;
368                         buf0len *= 2;
369                         *buf0 = xrealloc(*buf0, buf0len);
370                         buf = *buf0 + buflen -2;
371
372                         continue;
373                 }
374
375                 *nl = '\0';
376
377                 lineno++;
378
379                 if (pkg_parse_line(pkg, *buf0, mask))
380                         break;
381
382                 buf = *buf0;
383                 buflen = buf0len;
384                 buf[0] = '\0';
385         };
386
387         if (pkg->name == NULL) {
388                 /* probably just a blank line */
389                 ret = 1;
390         }
391
392         return ret;
393 }
394
395 int
396 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
397 {
398         int ret;
399         char *buf;
400         const size_t len = 4096;
401
402         buf = xmalloc(len);
403         ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
404         free(buf);
405
406         return ret;
407 }