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