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"
27 int is_field(const char *type
, const char *line
)
29 if (!strncmp(line
, type
, strlen(type
)))
34 char *parse_simple(const char *type
, const char *line
)
36 char *field
= trim_xstrdup(line
+ strlen(type
) + 1);
37 if (strlen(field
) == 0) {
45 * Parse a comma separated string into an array.
47 char **parse_list(const char *raw
, unsigned int *count
, const char sep
,
50 char **depends
= NULL
;
51 const char *start
, *end
;
54 /* skip past the "Field:" marker */
56 while (*raw
&& *raw
!= ':')
61 if (line_is_blank(raw
)) {
67 depends
= xrealloc(depends
, sizeof(char *) * (line_count
+ 1));
73 while (*raw
!= sep
&& *raw
)
77 while (end
> start
&& isspace(*end
))
83 depends
[line_count
] = xstrndup(start
, end
- start
);
95 parse_from_stream_nomalloc(parse_line_t parse_line
, void *item
, FILE * fp
,
96 uint mask
, char **buf0
, size_t buf0len
)
110 if (fgets(buf
, (int)buflen
, fp
) == NULL
) {
112 opkg_perror(ERROR
, "fgets");
114 } else if (strlen(*buf0
) == buf0len
- 1) {
115 opkg_msg(ERROR
, "Missing new line character"
116 " at end of file!\n");
117 parse_line(item
, *buf0
, mask
);
122 nl
= strchr(buf
, '\n');
124 if (strlen(buf
) < buflen
- 1) {
126 * Line could be exactly buflen-1 long and
127 * missing a newline, but we won't know until
128 * fgets fails to read more data.
130 opkg_msg(ERROR
, "Missing new line character"
131 " at end of file!\n");
132 parse_line(item
, *buf0
, mask
);
135 if (buf0len
>= EXCESSIVE_LINE_LEN
) {
136 opkg_msg(ERROR
, "Excessively long line at "
137 "%d. Corrupt file?\n", lineno
);
143 * Realloc and point buf past the data already read,
144 * at the NULL terminator inserted by fgets.
145 * |<--------------- buf0len ----------------->|
146 * | |<------- buflen ---->|
147 * |---------------------|---------------------|
150 buflen
= buf0len
+ 1;
152 *buf0
= xrealloc(*buf0
, buf0len
);
153 buf
= *buf0
+ buflen
- 2;
162 if (parse_line(item
, *buf0
, mask
))