1 /* parse_util.c - the opkg package management system
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
6 Copyright (C) 2002 Compaq Computer Corporation
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.
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.
21 #include "opkg_utils.h"
22 #include "libbb/libbb.h"
24 #include "parse_util.h"
25 #include "pkg_parse.h"
28 is_field(const char *type
, const char *line
)
30 if (!strncmp(line
, type
, strlen(type
)))
36 parse_simple(const char *type
, const char *line
)
38 char *field
= trim_xstrdup(line
+ strlen(type
) + 1);
39 if (strlen(field
) == 0) {
47 * Parse a comma separated string into an array.
50 parse_list(const char *raw
, unsigned int *count
, const char sep
, int skip_field
)
52 char **depends
= NULL
;
53 const char *start
, *end
;
56 /* skip past the "Field:" marker */
58 while (*raw
&& *raw
!= ':')
63 if (line_is_blank(raw
)) {
69 depends
= xrealloc(depends
, sizeof(char *) * (line_count
+ 1));
75 while (*raw
!= sep
&& *raw
)
79 while (end
> start
&& isspace(*end
))
85 depends
[line_count
] = xstrndup(start
, end
-start
);
97 parse_from_stream_nomalloc(parse_line_t parse_line
, void *item
, FILE *fp
, uint mask
,
98 char **buf0
, size_t buf0len
)
112 if (fgets(buf
, (int)buflen
, fp
) == NULL
) {
114 opkg_perror(ERROR
, "fgets");
116 } else if (strlen(*buf0
) == buf0len
-1) {
117 opkg_msg(ERROR
, "Missing new line character"
118 " at end of file!\n");
119 parse_line(item
, *buf0
, mask
);
124 nl
= strchr(buf
, '\n');
126 if (strlen(buf
) < buflen
-1) {
128 * Line could be exactly buflen-1 long and
129 * missing a newline, but we won't know until
130 * fgets fails to read more data.
132 opkg_msg(ERROR
, "Missing new line character"
133 " at end of file!\n");
134 parse_line(item
, *buf0
, mask
);
137 if (buf0len
>= EXCESSIVE_LINE_LEN
) {
138 opkg_msg(ERROR
, "Excessively long line at "
139 "%d. Corrupt file?\n",
146 * Realloc and point buf past the data already read,
147 * at the NULL terminator inserted by fgets.
148 * |<--------------- buf0len ----------------->|
149 * | |<------- buflen ---->|
150 * |---------------------|---------------------|
155 *buf0
= xrealloc(*buf0
, buf0len
);
156 buf
= *buf0
+ buflen
-2;
165 if (parse_line(item
, *buf0
, mask
))