47ff3bb8ea9f57261adb16cf5e5bfde839f7bf17
[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 parseConffiles(pkg_t * pkg, const char * raw)
86 {
87     char file_name[1048], md5sum[1048];  /* please tell me there aren't any longer that 1k */
88
89     if(!strncmp(raw, "Conffiles:", 10))
90         raw += strlen("Conffiles:");
91
92     while(*raw && (sscanf(raw, "%s%s", file_name, md5sum) == 2)){
93         conffile_list_append(&pkg->conffiles, file_name, md5sum);
94         /*      fprintf(stderr, "%s %s ", file_name, md5sum);*/
95         while (*raw && isspace(*raw)) {
96             raw++;
97         }
98         raw += strlen(file_name);
99         while (*raw && isspace(*raw)) {
100             raw++;
101         }
102         raw += strlen(md5sum);
103     }
104 }    
105
106 int parseVersion(pkg_t *pkg, const char *raw)
107 {
108   char *colon, *eepochcolon;
109   char *hyphen;
110   unsigned long epoch;
111
112   if (!*raw) {
113       fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__);
114       return EINVAL;
115   }
116
117   if (strncmp(raw, "Version:", 8) == 0) {
118       raw += 8;
119   }
120   while (*raw && isspace(*raw)) {
121       raw++;
122   }
123   
124   colon= strchr(raw,':');
125   if (colon) {
126     epoch= strtoul(raw,&eepochcolon,10);
127     if (colon != eepochcolon) {
128         fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__);
129         return EINVAL;
130     }
131     if (!*++colon) {
132         fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__);
133         return EINVAL;
134     }
135     raw= colon;
136     pkg->epoch= epoch;
137   } else {
138     pkg->epoch= 0;
139   }
140
141   pkg->revision = "";
142
143   if (!pkg->version)
144   {
145     pkg->version= xcalloc(1, strlen(raw)+1);
146     strcpy(pkg->version, raw);
147   }
148
149   hyphen= strrchr(pkg->version,'-');
150
151   if (hyphen) {
152     *hyphen++= 0;
153       pkg->revision = hyphen;
154   }
155
156   return 0;
157 }
158
159 static int
160 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
161 {
162         /* these flags are a bit hackish... */
163         static int reading_conffiles = 0, reading_description = 0;
164
165         switch (*line) {
166         case 'A':
167             if((mask & PFM_ARCHITECTURE ) && isGenericFieldType("Architecture:", line))
168                 pkg->architecture = parseGenericFieldType("Architecture", line);
169             else if((mask & PFM_AUTO_INSTALLED) && isGenericFieldType("Auto-Installed:", line)) {
170                 char *auto_installed_value;
171                 auto_installed_value = parseGenericFieldType("Auto-Installed:", line);
172                 if (strcmp(auto_installed_value, "yes") == 0) {
173                     pkg->auto_installed = 1;
174                 }
175                 free(auto_installed_value);
176             }
177             break;
178
179         case 'C':
180             if((mask & PFM_CONFFILES) && isGenericFieldType("Conffiles", line)){
181                 parseConffiles(pkg, line);
182                 reading_conffiles = 1;
183                 reading_description = 0;
184                 goto dont_reset_flags;
185             }
186             else if((mask & PFM_CONFLICTS) && isGenericFieldType("Conflicts", line))
187                 pkg->conflicts_str = parseDependsString(line, &pkg->conflicts_count);
188             break;
189
190         case 'D':
191             if((mask & PFM_DESCRIPTION) && isGenericFieldType("Description", line)) {
192                 pkg->description = parseGenericFieldType("Description", line);
193                 reading_conffiles = 0;
194                 reading_description = 1;
195                 goto dont_reset_flags;
196             }
197             else if((mask & PFM_DEPENDS) && isGenericFieldType("Depends", line))
198                 pkg->depends_str = parseDependsString(line, &pkg->depends_count);
199             break;
200
201         case 'E':
202             if((mask & PFM_ESSENTIAL) && isGenericFieldType("Essential:", line)) {
203                 char *essential_value;
204                 essential_value = parseGenericFieldType("Essential", line);
205                 if (strcmp(essential_value, "yes") == 0) {
206                     pkg->essential = 1;
207                 }
208                 free(essential_value);
209             }
210             break;
211
212         case 'F':
213             if((mask & PFM_FILENAME) && isGenericFieldType("Filename:", line))
214                 pkg->filename = parseGenericFieldType("Filename", line);
215             break;
216
217         case 'I':
218             if((mask && PFM_INSTALLED_SIZE) && isGenericFieldType("Installed-Size:", line))
219                 pkg->installed_size = parseGenericFieldType("Installed-Size", line);
220             else if((mask && PFM_INSTALLED_TIME) && isGenericFieldType("Installed-Time:", line)) {
221                 char *time_str = parseGenericFieldType("Installed-Time", line);
222                 pkg->installed_time = strtoul(time_str, NULL, 0);
223                 free (time_str);
224             }       
225             break;
226
227         case 'M':
228             if(mask && PFM_MD5SUM) {
229                 if (isGenericFieldType("MD5sum:", line))
230                         pkg->md5sum = parseGenericFieldType("MD5sum", line);
231                         /* The old opkg wrote out status files with the wrong
232                          * case for MD5sum, let's parse it either way */
233                 else if(isGenericFieldType("MD5Sum:", line))
234                         pkg->md5sum = parseGenericFieldType("MD5Sum", line);
235             } else if((mask & PFM_MAINTAINER) && isGenericFieldType("Maintainer", line))
236                 pkg->maintainer = parseGenericFieldType("Maintainer", line);
237             break;
238
239         case 'P':
240             if((mask & PFM_PACKAGE) && isGenericFieldType("Package:", line)) 
241                 pkg->name = parseGenericFieldType("Package", line);
242             else if((mask & PFM_PRIORITY) && isGenericFieldType("Priority:", line))
243                 pkg->priority = parseGenericFieldType("Priority", line);
244             else if((mask & PFM_PROVIDES) && isGenericFieldType("Provides", line)){
245                 pkg->provides_str = parseDependsString(line, &pkg->provides_count);
246             } 
247             else if((mask & PFM_PRE_DEPENDS) && isGenericFieldType("Pre-Depends", line))
248                 pkg->pre_depends_str = parseDependsString(line, &pkg->pre_depends_count);
249             break;
250
251         case 'R':
252             if((mask & PFM_RECOMMENDS) && isGenericFieldType("Recommends", line))
253                 pkg->recommends_str = parseDependsString(line, &pkg->recommends_count);
254             else if((mask & PFM_REPLACES) && isGenericFieldType("Replaces", line))
255                 pkg->replaces_str = parseDependsString(line, &pkg->replaces_count);
256             
257             break;
258
259         case 'S':
260             if((mask & PFM_SECTION) && isGenericFieldType("Section:", line))
261                 pkg->section = parseGenericFieldType("Section", line);
262 #ifdef HAVE_SHA256
263             else if((mask & PFM_SHA256SUM) && isGenericFieldType("SHA256sum:", line))
264                 pkg->sha256sum = parseGenericFieldType("SHA256sum", line);
265 #endif
266             else if((mask & PFM_SIZE) && isGenericFieldType("Size:", line))
267                 pkg->size = parseGenericFieldType("Size", line);
268             else if((mask & PFM_SOURCE) && isGenericFieldType("Source:", line))
269                 pkg->source = parseGenericFieldType("Source", line);
270             else if((mask & PFM_STATUS) && isGenericFieldType("Status", line))
271                 parseStatus(pkg, line);
272             else if((mask & PFM_SUGGESTS) && isGenericFieldType("Suggests", line))
273                 pkg->suggests_str = parseDependsString(line, &pkg->suggests_count);
274             break;
275
276         case 'T':
277             if((mask & PFM_TAGS) && isGenericFieldType("Tags:", line))
278                 pkg->tags = parseGenericFieldType("Tags", line);
279             break;
280
281         case 'V':
282             if((mask & PFM_VERSION) && isGenericFieldType("Version", line))
283                 parseVersion(pkg, line);
284             break;
285
286         case ' ':
287             if((mask & PFM_DESCRIPTION) && reading_description) {
288                 /* we already know it's not blank, so the rest of description */      
289                 pkg->description = xrealloc(pkg->description,
290                                            strlen(pkg->description)
291                                            + 1 + strlen(line) + 1);
292                 strcat(pkg->description, "\n");
293                 strcat(pkg->description, (line));
294                 goto dont_reset_flags;
295             }
296             else if((mask && PFM_CONFFILES) && reading_conffiles) {
297                 parseConffiles(pkg, line);
298                 goto dont_reset_flags;
299             }
300             break;
301
302         default:
303             /* For package lists, signifies end of package. */
304             if(line_is_blank(line)) {
305                 return 1;
306             }
307         }
308
309         reading_description = 0;
310         reading_conffiles = 0;
311
312 dont_reset_flags:
313
314         return 0;
315 }
316
317 int
318 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
319                                                 char **buf0, size_t buf0len)
320 {
321         int ret, lineno;
322         char *buf, *nl;
323         size_t buflen;
324
325         lineno = 1;
326         ret = 0;
327
328         buflen = buf0len;
329         buf = *buf0;
330         buf[0] = '\0';
331
332         while (1) {
333                 if (fgets(buf, buflen, fp) == NULL) {
334                         if (ferror(fp)) {
335                                 fprintf(stderr, "%s: fgets: %s\n",
336                                         __FUNCTION__, strerror(errno));
337                                 ret = -1;
338                         } else if (strlen(*buf0) == buflen-1) {
339                                 fprintf(stderr, "%s: missing new line character"
340                                                 " at end of file!\n",
341                                         __FUNCTION__);
342                                 pkg_parse_line(pkg, *buf0, mask);
343                         }
344                         break;
345                 }
346
347                 nl = strchr(buf, '\n');
348                 if (nl == NULL) {
349                         if (strlen(buf) < buflen-1) {
350                                 /*
351                                  * Line could be exactly buflen-1 long and
352                                  * missing a newline, but we won't know until
353                                  * fgets fails to read more data.
354                                  */
355                                 fprintf(stderr, "%s: missing new line character"
356                                                 " at end of file!\n",
357                                         __FUNCTION__);
358                                 pkg_parse_line(pkg, *buf0, mask);
359                                 break;
360                         }
361                         if (buf0len >= EXCESSIVE_LINE_LEN) {
362                                 fprintf(stderr, "%s: excessively long line at "
363                                         "%d. Corrupt file?\n",
364                                         __FUNCTION__, lineno);
365                                 ret = -1;
366                                 break;
367                         }
368
369                         /*
370                          * Realloc and move buf past the data already read.
371                          * |<--------------- buf0len ----------------->|
372                          * |                     |<------- buflen ---->|
373                          * |---------------------|---------------------|
374                          * buf0                   buf
375                          */
376                         buflen = buf0len;
377                         buf0len *= 2;
378                         *buf0 = xrealloc(*buf0, buf0len);
379                         buf = *buf0 + buflen -1;
380
381                         continue;
382                 }
383
384                 *nl = '\0';
385
386                 lineno++;
387
388                 if (pkg_parse_line(pkg, *buf0, mask))
389                         break;
390
391                 if (buf != *buf0)
392                         fprintf(stderr, "%s: %s\n", __FUNCTION__, pkg->name);
393
394                 buf = *buf0;
395                 buflen = buf0len;
396                 buf[0] = '\0';
397         };
398
399         if (pkg->name == NULL) {
400                 /* probably just a blank line */
401                 ret = EINVAL;
402         }
403
404         return ret;
405 }
406
407 int
408 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
409 {
410         int ret;
411         char *buf;
412         const size_t len = 4096;
413
414         buf = xmalloc(len);
415         ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
416         free(buf);
417
418         return ret;
419 }