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 return trim_xstrdup(line + strlen(type) + 1);
42 * Parse a comma separated string into an array.
45 parse_list(const char *raw, unsigned int *count, const char sep, int skip_field)
47 char **depends = NULL;
48 const char *start, *end;
51 /* skip past the "Field:" marker */
53 while (*raw && *raw != ':')
58 if (line_is_blank(raw)) {
64 depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
70 while (*raw != sep && *raw)
74 while (end > start && isspace(*end))
80 depends[line_count] = xstrndup(start, end-start);
92 parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask,
93 char **buf0, size_t buf0len)
107 if (fgets(buf, (int)buflen, fp) == NULL) {
109 opkg_perror(ERROR, "fgets");
111 } else if (strlen(*buf0) == buf0len-1) {
112 opkg_msg(ERROR, "Missing new line character"
113 " at end of file!\n");
114 parse_line(item, *buf0, mask);
119 nl = strchr(buf, '\n');
121 if (strlen(buf) < buflen-1) {
123 * Line could be exactly buflen-1 long and
124 * missing a newline, but we won't know until
125 * fgets fails to read more data.
127 opkg_msg(ERROR, "Missing new line character"
128 " at end of file!\n");
129 parse_line(item, *buf0, mask);
132 if (buf0len >= EXCESSIVE_LINE_LEN) {
133 opkg_msg(ERROR, "Excessively long line at "
134 "%d. Corrupt file?\n",
141 * Realloc and point buf past the data already read,
142 * at the NULL terminator inserted by fgets.
143 * |<--------------- buf0len ----------------->|
144 * | |<------- buflen ---->|
145 * |---------------------|---------------------|
150 *buf0 = xrealloc(*buf0, buf0len);
151 buf = *buf0 + buflen -2;
160 if (parse_line(item, *buf0, mask))