summaryrefslogtreecommitdiffstats
path: root/src/libopkg/parse_util.c
blob: 54850a86432f5696e45271a198a83d3fceba0c7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* parse_util.c - the opkg package management system

   Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>

   Steven M. Ayer
   Copyright (C) 2002 Compaq Computer Corporation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include <ctype.h>

#include "opkg_utils.h"
#include "libbb/libbb.h"

#include "parse_util.h"

int
is_field(const char *type, const char *line)
{
	if (!strncmp(line, type, strlen(type)))
		return 1;
	return 0;
}

char *
parse_simple(const char *type, const char *line)
{
	return trim_xstrdup(line + strlen(type) + 1);
}

/*
 * Parse a comma separated string into an array.
 */
char **
parse_list(const char *raw, unsigned int *count, const char sep, int skip_field)
{
	char **depends = NULL;
	const char *start, *end;
	int line_count = 0;

	/* skip past the "Field:" marker */
	if (!skip_field) {
	while (*raw && *raw != ':')
		raw++;
	raw++;
	}

	if (line_is_blank(raw)) {
		*count = line_count;
		return NULL;
	}

	while (*raw) {
		depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
	
		while (isspace(*raw))
			raw++;

		start = raw;
		while (*raw != sep && *raw)
			raw++;
		end = raw;

		while (end > start && isspace(*end))
			end--;

		if (sep == ' ')
			end++;

		depends[line_count] = xstrndup(start, end-start);

        	line_count++;
		if (*raw == sep)
		    raw++;
	}

	*count = line_count;
	return depends;
}

int
parse_from_stream_nomalloc(parse_line_t parse_line, void *ptr, FILE *fp, uint mask,
						char **buf0, size_t buf0len)
{
	int ret, lineno;
	char *buf, *nl;
	size_t buflen;

	lineno = 1;
	ret = 0;

	buflen = buf0len;
	buf = *buf0;
	buf[0] = '\0';

	while (1) {
		if (fgets(buf, (int)buflen, fp) == NULL) {
			if (ferror(fp)) {
				opkg_perror(ERROR, "fgets");
				ret = -1;
			} else if (strlen(*buf0) == buf0len-1) {
				opkg_msg(ERROR, "Missing new line character"
						" at end of file!\n");
				parse_line(ptr, *buf0, mask);
			}
			break;
		}

		nl = strchr(buf, '\n');
		if (nl == NULL) {
			if (strlen(buf) < buflen-1) {
				/*
				 * Line could be exactly buflen-1 long and
				 * missing a newline, but we won't know until
				 * fgets fails to read more data.
				 */
				opkg_msg(ERROR, "Missing new line character"
						" at end of file!\n");
				parse_line(ptr, *buf0, mask);
				break;
			}
			if (buf0len >= EXCESSIVE_LINE_LEN) {
				opkg_msg(ERROR, "Excessively long line at "
					"%d. Corrupt file?\n",
					lineno);
				ret = -1;
				break;
			}

			/*
			 * Realloc and point buf past the data already read,
			 * at the NULL terminator inserted by fgets.
			 * |<--------------- buf0len ----------------->|
			 * |                     |<------- buflen ---->|
			 * |---------------------|---------------------|
			 * buf0                   buf
			 */
			buflen = buf0len +1;
			buf0len *= 2;
			*buf0 = xrealloc(*buf0, buf0len);
			buf = *buf0 + buflen -2;

			continue;
		}

		*nl = '\0';

		lineno++;

		if (parse_line(ptr, *buf0, mask))
			break;

		buf = *buf0;
		buflen = buf0len;
		buf[0] = '\0';
	}

	return ret;
}