4f7fcf26f5409fe93afa6e35a9a6a21692c6b95c
[oweals/opkg-lede.git] / libopkg / pkg_parse.c
1 /* pkg_parse.c - the opkg package management system
2
3    Steven M. Ayer
4    
5    Copyright (C) 2002 Compaq Computer Corporation
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include <errno.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 isGenericFieldType(char * type, const char * line)
28 {
29     if(!strncmp(line, type, strlen(type)))
30         return 1;
31     return 0;
32 }
33
34 static char * parseGenericFieldType(char * type, const char * raw)
35 {
36     const char * field_value = raw + (strlen(type) + 1);
37     return trim_alloc(field_value);
38 }
39
40 static void parseStatus(pkg_t *pkg, const char * raw)
41 {
42     char sw_str[64], sf_str[64], ss_str[64];
43
44     sscanf(raw, "Status: %s %s %s", sw_str, sf_str, ss_str);
45     pkg->state_want = pkg_state_want_from_str(sw_str);
46     pkg->state_flag = pkg_state_flag_from_str(sf_str);
47     pkg->state_status = pkg_state_status_from_str(ss_str);
48 }
49
50 static char ** parseDependsString(const char * raw, int * depends_count)
51 {
52     char ** depends = NULL;
53     int line_count = 0;
54     char buff[2048], * dest;
55
56     while(raw && *raw && !isspace(*raw)) {
57         raw++;
58     }
59
60     if(line_is_blank(raw)){
61         *depends_count = line_count;
62         return NULL;
63     }
64     while(raw && *raw){
65         depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
66         
67         while(isspace(*raw)) raw++;
68
69         dest = buff;
70         while((*raw != ',') && *raw)
71             *dest++ = *raw++;
72
73         *dest = '\0';
74         depends[line_count] = trim_alloc(buff);
75         if(depends[line_count] ==NULL)
76            return NULL;
77         line_count++;
78         if(*raw == ',')
79             raw++;
80     }
81     *depends_count = line_count;
82     return depends;
83 }
84
85 static void
86 parseConffiles(pkg_t *pkg, const char *cstr)
87 {
88         char file_name[1024], md5sum[35];
89
90         if (sscanf(cstr, "%1023s %34s", file_name, md5sum) != 2) {
91                 fprintf(stderr, "%s: failed to parse Conffiles line for %s\n",
92                                 __FUNCTION__, pkg->name);
93                 return;
94         }
95
96         conffile_list_append(&pkg->conffiles, file_name, md5sum);
97 }
98
99 int
100 parseVersion(pkg_t *pkg, const char *vstr)
101 {
102         char *colon;
103
104         if (strncmp(vstr, "Version:", 8) == 0)
105                 vstr += 8;
106
107         while (*vstr && isspace(*vstr))
108                 vstr++;
109
110         colon = strchr(vstr, ':');
111         if (colon) {
112                 errno = 0;
113                 pkg->epoch = strtoul(vstr, NULL, 10);
114                 if (errno) {
115                         fprintf(stderr, "%s: %s: invalid epoch: %s\n",
116                                 __FUNCTION__, pkg->name, strerror(errno));
117                 }
118                 vstr = ++colon;
119         } else {
120                 pkg->epoch= 0;
121         }
122
123         pkg->version= xstrdup(vstr);
124         pkg->revision = strrchr(pkg->version,'-');
125
126         if (pkg->revision)
127                 *pkg->revision++ = '\0';
128
129         return 0;
130 }
131
132 static int
133 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
134 {
135         /* these flags are a bit hackish... */
136         static int reading_conffiles = 0, reading_description = 0;
137
138         switch (*line) {
139         case 'A':
140             if((mask & PFM_ARCHITECTURE ) && isGenericFieldType("Architecture:", line))
141                 pkg->architecture = parseGenericFieldType("Architecture", line);
142             else if((mask & PFM_AUTO_INSTALLED) && isGenericFieldType("Auto-Installed:", line)) {
143                 char *auto_installed_value;
144                 auto_installed_value = parseGenericFieldType("Auto-Installed:", line);
145                 if (strcmp(auto_installed_value, "yes") == 0) {
146                     pkg->auto_installed = 1;
147                 }
148                 free(auto_installed_value);
149             }
150             break;
151
152         case 'C':
153             if((mask & PFM_CONFFILES) && isGenericFieldType("Conffiles", line)){
154                 reading_conffiles = 1;
155                 reading_description = 0;
156                 goto dont_reset_flags;
157             }
158             else if((mask & PFM_CONFLICTS) && isGenericFieldType("Conflicts", line))
159                 pkg->conflicts_str = parseDependsString(line, &pkg->conflicts_count);
160             break;
161
162         case 'D':
163             if((mask & PFM_DESCRIPTION) && isGenericFieldType("Description", line)) {
164                 pkg->description = parseGenericFieldType("Description", line);
165                 reading_conffiles = 0;
166                 reading_description = 1;
167                 goto dont_reset_flags;
168             }
169             else if((mask & PFM_DEPENDS) && isGenericFieldType("Depends", line))
170                 pkg->depends_str = parseDependsString(line, &pkg->depends_count);
171             break;
172
173         case 'E':
174             if((mask & PFM_ESSENTIAL) && isGenericFieldType("Essential:", line)) {
175                 char *essential_value;
176                 essential_value = parseGenericFieldType("Essential", line);
177                 if (strcmp(essential_value, "yes") == 0) {
178                     pkg->essential = 1;
179                 }
180                 free(essential_value);
181             }
182             break;
183
184         case 'F':
185             if((mask & PFM_FILENAME) && isGenericFieldType("Filename:", line))
186                 pkg->filename = parseGenericFieldType("Filename", line);
187             break;
188
189         case 'I':
190             if((mask && PFM_INSTALLED_SIZE) && isGenericFieldType("Installed-Size:", line))
191                 pkg->installed_size = parseGenericFieldType("Installed-Size", line);
192             else if((mask && PFM_INSTALLED_TIME) && isGenericFieldType("Installed-Time:", line)) {
193                 char *time_str = parseGenericFieldType("Installed-Time", line);
194                 pkg->installed_time = strtoul(time_str, NULL, 0);
195                 free (time_str);
196             }       
197             break;
198
199         case 'M':
200             if(mask && PFM_MD5SUM) {
201                 if (isGenericFieldType("MD5sum:", line))
202                         pkg->md5sum = parseGenericFieldType("MD5sum", line);
203                         /* The old opkg wrote out status files with the wrong
204                          * case for MD5sum, let's parse it either way */
205                 else if(isGenericFieldType("MD5Sum:", line))
206                         pkg->md5sum = parseGenericFieldType("MD5Sum", line);
207             } else if((mask & PFM_MAINTAINER) && isGenericFieldType("Maintainer", line))
208                 pkg->maintainer = parseGenericFieldType("Maintainer", line);
209             break;
210
211         case 'P':
212             if((mask & PFM_PACKAGE) && isGenericFieldType("Package:", line)) 
213                 pkg->name = parseGenericFieldType("Package", line);
214             else if((mask & PFM_PRIORITY) && isGenericFieldType("Priority:", line))
215                 pkg->priority = parseGenericFieldType("Priority", line);
216             else if((mask & PFM_PROVIDES) && isGenericFieldType("Provides", line)){
217                 pkg->provides_str = parseDependsString(line, &pkg->provides_count);
218             } 
219             else if((mask & PFM_PRE_DEPENDS) && isGenericFieldType("Pre-Depends", line))
220                 pkg->pre_depends_str = parseDependsString(line, &pkg->pre_depends_count);
221             break;
222
223         case 'R':
224             if((mask & PFM_RECOMMENDS) && isGenericFieldType("Recommends", line))
225                 pkg->recommends_str = parseDependsString(line, &pkg->recommends_count);
226             else if((mask & PFM_REPLACES) && isGenericFieldType("Replaces", line))
227                 pkg->replaces_str = parseDependsString(line, &pkg->replaces_count);
228             
229             break;
230
231         case 'S':
232             if((mask & PFM_SECTION) && isGenericFieldType("Section:", line))
233                 pkg->section = parseGenericFieldType("Section", line);
234 #ifdef HAVE_SHA256
235             else if((mask & PFM_SHA256SUM) && isGenericFieldType("SHA256sum:", line))
236                 pkg->sha256sum = parseGenericFieldType("SHA256sum", line);
237 #endif
238             else if((mask & PFM_SIZE) && isGenericFieldType("Size:", line))
239                 pkg->size = parseGenericFieldType("Size", line);
240             else if((mask & PFM_SOURCE) && isGenericFieldType("Source:", line))
241                 pkg->source = parseGenericFieldType("Source", line);
242             else if((mask & PFM_STATUS) && isGenericFieldType("Status", line))
243                 parseStatus(pkg, line);
244             else if((mask & PFM_SUGGESTS) && isGenericFieldType("Suggests", line))
245                 pkg->suggests_str = parseDependsString(line, &pkg->suggests_count);
246             break;
247
248         case 'T':
249             if((mask & PFM_TAGS) && isGenericFieldType("Tags:", line))
250                 pkg->tags = parseGenericFieldType("Tags", line);
251             break;
252
253         case 'V':
254             if((mask & PFM_VERSION) && isGenericFieldType("Version", line))
255                 parseVersion(pkg, line);
256             break;
257
258         case ' ':
259             if((mask & PFM_DESCRIPTION) && reading_description) {
260                 /* we already know it's not blank, so the rest of description */      
261                 pkg->description = xrealloc(pkg->description,
262                                            strlen(pkg->description)
263                                            + 1 + strlen(line) + 1);
264                 strcat(pkg->description, "\n");
265                 strcat(pkg->description, (line));
266                 goto dont_reset_flags;
267             }
268             else if((mask && PFM_CONFFILES) && reading_conffiles) {
269                 parseConffiles(pkg, line);
270                 goto dont_reset_flags;
271             }
272             break;
273
274         default:
275             /* For package lists, signifies end of package. */
276             if(line_is_blank(line)) {
277                 return 1;
278             }
279         }
280
281         reading_description = 0;
282         reading_conffiles = 0;
283
284 dont_reset_flags:
285
286         return 0;
287 }
288
289 int
290 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
291                                                 char **buf0, size_t buf0len)
292 {
293         int ret, lineno;
294         char *buf, *nl;
295         size_t buflen;
296
297         lineno = 1;
298         ret = 0;
299
300         buflen = buf0len;
301         buf = *buf0;
302         buf[0] = '\0';
303
304         while (1) {
305                 if (fgets(buf, buflen, fp) == NULL) {
306                         if (ferror(fp)) {
307                                 fprintf(stderr, "%s: fgets: %s\n",
308                                         __FUNCTION__, strerror(errno));
309                                 ret = -1;
310                         } else if (strlen(*buf0) == buflen-1) {
311                                 fprintf(stderr, "%s: missing new line character"
312                                                 " at end of file!\n",
313                                         __FUNCTION__);
314                                 pkg_parse_line(pkg, *buf0, mask);
315                         }
316                         break;
317                 }
318
319                 nl = strchr(buf, '\n');
320                 if (nl == NULL) {
321                         if (strlen(buf) < buflen-1) {
322                                 /*
323                                  * Line could be exactly buflen-1 long and
324                                  * missing a newline, but we won't know until
325                                  * fgets fails to read more data.
326                                  */
327                                 fprintf(stderr, "%s: missing new line character"
328                                                 " at end of file!\n",
329                                         __FUNCTION__);
330                                 pkg_parse_line(pkg, *buf0, mask);
331                                 break;
332                         }
333                         if (buf0len >= EXCESSIVE_LINE_LEN) {
334                                 fprintf(stderr, "%s: excessively long line at "
335                                         "%d. Corrupt file?\n",
336                                         __FUNCTION__, lineno);
337                                 ret = -1;
338                                 break;
339                         }
340
341                         /*
342                          * Realloc and move buf past the data already read.
343                          * |<--------------- buf0len ----------------->|
344                          * |                     |<------- buflen ---->|
345                          * |---------------------|---------------------|
346                          * buf0                   buf
347                          */
348                         buflen = buf0len;
349                         buf0len *= 2;
350                         *buf0 = xrealloc(*buf0, buf0len);
351                         buf = *buf0 + buflen -1;
352
353                         continue;
354                 }
355
356                 *nl = '\0';
357
358                 lineno++;
359
360                 if (pkg_parse_line(pkg, *buf0, mask))
361                         break;
362
363                 buf = *buf0;
364                 buflen = buf0len;
365                 buf[0] = '\0';
366         };
367
368         if (pkg->name == NULL) {
369                 /* probably just a blank line */
370                 ret = EINVAL;
371         }
372
373         return ret;
374 }
375
376 int
377 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
378 {
379         int ret;
380         char *buf;
381         const size_t len = 4096;
382
383         buf = xmalloc(len);
384         ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
385         free(buf);
386
387         return ret;
388 }