Skip to content

Commit c476c80

Browse files
committed
lib/external: add inih
1 parent 3ca652b commit c476c80

3 files changed

Lines changed: 531 additions & 2 deletions

File tree

lib/Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ endif
3131
LIBFETCH_INCS = fetch/common.h
3232
LIBFETCH_GEN = fetch/ftperr.h fetch/httperr.h
3333

34+
INIH_OBJS = external/inih/ini.o
35+
INIH_CPPFLAGS = -DINI_HANDLER_LINENO=1 -DINI_CALL_HANDLER_ON_NEW_SECTION=1
36+
INIH_CFLAGS= -Wno-error=cast-qual
37+
ifdef HAVE_VISIBILITY
38+
INIH_CFLAGS+= -fvisibility=hidden
39+
endif
40+
3441
# External code used by libxbps
3542
EXTOBJS = external/dewey.o external/fexec.o external/mkpath.o
3643

@@ -76,11 +83,16 @@ $(LIBPROP_OBJS): %.o: %.c
7683
${SILENT}$(CC) $(CPPFLAGS) $(LIBPROP_CPPFLAGS) \
7784
$(CFLAGS) $(LIBPROP_CFLAGS) -c $< -o $@
7885

86+
$(INIH_OBJS): %.o: %.c
87+
@printf " [CC]\t\t$@\n"
88+
${SILENT}$(CC) $(CPPFLAGS) $(INIH_CPPFLAGS) \
89+
$(CFLAGS) $(INIH_CFLAGS) -c $< -o $@
90+
7991
$(OBJS): %.o: %.c
8092
@printf " [CC]\t\t$@\n"
8193
${SILENT}$(CC) $(CPPFLAGS) $(CFLAGS) $(SHAREDLIB_CFLAGS) -c $< -o $@
8294

83-
libxbps.so: $(LIBFETCH_OBJS) $(LIBPROP_OBJS) $(OBJS)
95+
libxbps.so: $(LIBFETCH_OBJS) $(LIBPROP_OBJS) $(INIH_OBJS) $(OBJS)
8496
@printf " [CCLD]\t\t$@\n"
8597
${SILENT}$(CC) $^ $(CFLAGS) $(LDFLAGS) -o $(LIBXBPS_SHLIB)
8698
@-ln -sf $(LIBXBPS_SHLIB) libxbps.so.$(LIBXBPS_MAJOR)
@@ -106,5 +118,5 @@ uninstall:
106118

107119
.PHONY: clean
108120
clean:
109-
-rm -f libxbps* $(OBJS) $(LIBFETCH_OBJS) $(LIBPROP_OBJS)
121+
-rm -f libxbps* $(OBJS) $(LIBFETCH_OBJS) $(LIBPROP_OBJS) $(INIH_OBJS)
110122
-rm -f $(LIBFETCH_GEN)

lib/external/inih/ini.c

Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
/* inih -- simple .INI file parser
2+
3+
SPDX-License-Identifier: BSD-3-Clause
4+
5+
Copyright (C) 2009-2025, Ben Hoyt
6+
7+
inih is released under the New BSD license (see LICENSE.txt). Go to the project
8+
home page for more info:
9+
10+
https://github.com/benhoyt/inih
11+
12+
*/
13+
14+
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
15+
#define _CRT_SECURE_NO_WARNINGS
16+
#endif
17+
18+
#include <stdio.h>
19+
#include <ctype.h>
20+
#include <string.h>
21+
22+
#include "ini.h"
23+
24+
#if !INI_USE_STACK
25+
#if INI_CUSTOM_ALLOCATOR
26+
#include <stddef.h>
27+
void* ini_malloc(size_t size);
28+
void ini_free(void* ptr);
29+
void* ini_realloc(void* ptr, size_t size);
30+
#else
31+
#include <stdlib.h>
32+
#define ini_malloc malloc
33+
#define ini_free free
34+
#define ini_realloc realloc
35+
#endif
36+
#endif
37+
38+
#define MAX_SECTION 50
39+
#define MAX_NAME 50
40+
41+
/* Used by ini_parse_string() to keep track of string parsing state. */
42+
typedef struct {
43+
const char* ptr;
44+
size_t num_left;
45+
} ini_parse_string_ctx;
46+
47+
/* Strip whitespace chars off end of given string, in place. end must be a
48+
pointer to the NUL terminator at the end of the string. Return s. */
49+
static char* ini_rstrip(char* s, char* end)
50+
{
51+
while (end > s && isspace((unsigned char)(*--end)))
52+
*end = '\0';
53+
return s;
54+
}
55+
56+
/* Return pointer to first non-whitespace char in given string. */
57+
static char* ini_lskip(const char* s)
58+
{
59+
while (*s && isspace((unsigned char)(*s)))
60+
s++;
61+
return (char*)s;
62+
}
63+
64+
/* Return pointer to first char (of chars) or inline comment in given string,
65+
or pointer to NUL at end of string if neither found. Inline comment must
66+
be prefixed by a whitespace character to register as a comment. */
67+
static char* ini_find_chars_or_comment(const char* s, const char* chars)
68+
{
69+
#if INI_ALLOW_INLINE_COMMENTS
70+
int was_space = 0;
71+
while (*s && (!chars || !strchr(chars, *s)) &&
72+
!(was_space && strchr(INI_INLINE_COMMENT_PREFIXES, *s))) {
73+
was_space = isspace((unsigned char)(*s));
74+
s++;
75+
}
76+
#else
77+
while (*s && (!chars || !strchr(chars, *s))) {
78+
s++;
79+
}
80+
#endif
81+
return (char*)s;
82+
}
83+
84+
/* Similar to strncpy, but ensures dest (size bytes) is
85+
NUL-terminated, and doesn't pad with NULs. */
86+
static char* ini_strncpy0(char* dest, const char* src, size_t size)
87+
{
88+
/* Could use strncpy internally, but it causes gcc warnings (see issue #91) */
89+
size_t i;
90+
for (i = 0; i < size - 1 && src[i]; i++)
91+
dest[i] = src[i];
92+
dest[i] = '\0';
93+
return dest;
94+
}
95+
96+
/* See documentation in header file. */
97+
int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
98+
void* user)
99+
{
100+
/* Uses a fair bit of stack (use heap instead if you need to) */
101+
#if INI_USE_STACK
102+
char line[INI_MAX_LINE];
103+
size_t max_line = INI_MAX_LINE;
104+
#else
105+
char* line;
106+
size_t max_line = INI_INITIAL_ALLOC;
107+
#endif
108+
#if INI_ALLOW_REALLOC && !INI_USE_STACK
109+
char* new_line;
110+
#endif
111+
char section[MAX_SECTION] = "";
112+
#if INI_ALLOW_MULTILINE
113+
char prev_name[MAX_NAME] = "";
114+
#endif
115+
116+
size_t offset;
117+
char* start;
118+
char* end;
119+
char* name;
120+
char* value;
121+
int lineno = 0;
122+
int error = 0;
123+
char abyss[16]; /* Used to consume input when a line is too long. */
124+
size_t abyss_len;
125+
126+
#if !INI_USE_STACK
127+
line = (char*)ini_malloc(INI_INITIAL_ALLOC);
128+
if (!line) {
129+
return -2;
130+
}
131+
#endif
132+
133+
#if INI_HANDLER_LINENO
134+
#define HANDLER(u, s, n, v) handler(u, s, n, v, lineno)
135+
#else
136+
#define HANDLER(u, s, n, v) handler(u, s, n, v)
137+
#endif
138+
139+
/* Scan through stream line by line */
140+
while (reader(line, (int)max_line, stream) != NULL) {
141+
offset = strlen(line);
142+
143+
#if INI_ALLOW_REALLOC && !INI_USE_STACK
144+
while (max_line < INI_MAX_LINE &&
145+
offset == max_line - 1 && line[offset - 1] != '\n') {
146+
max_line *= 2;
147+
if (max_line > INI_MAX_LINE)
148+
max_line = INI_MAX_LINE;
149+
new_line = ini_realloc(line, max_line);
150+
if (!new_line) {
151+
ini_free(line);
152+
return -2;
153+
}
154+
line = new_line;
155+
if (reader(line + offset, (int)(max_line - offset), stream) == NULL)
156+
break;
157+
offset += strlen(line + offset);
158+
}
159+
#endif
160+
161+
lineno++;
162+
163+
/* If line exceeded INI_MAX_LINE bytes, discard till end of line. */
164+
if (offset == max_line - 1 && line[offset - 1] != '\n') {
165+
while (reader(abyss, sizeof(abyss), stream) != NULL) {
166+
if (!error)
167+
error = lineno;
168+
abyss_len = strlen(abyss);
169+
if (abyss_len > 0 && abyss[abyss_len - 1] == '\n')
170+
break;
171+
}
172+
}
173+
174+
start = line;
175+
#if INI_ALLOW_BOM
176+
if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
177+
(unsigned char)start[1] == 0xBB &&
178+
(unsigned char)start[2] == 0xBF) {
179+
start += 3;
180+
}
181+
#endif
182+
start = ini_rstrip(ini_lskip(start), line + offset);
183+
184+
if (strchr(INI_START_COMMENT_PREFIXES, *start)) {
185+
/* Start-of-line comment */
186+
}
187+
#if INI_ALLOW_MULTILINE
188+
else if (*prev_name && *start && start > line) {
189+
#if INI_ALLOW_INLINE_COMMENTS
190+
end = ini_find_chars_or_comment(start, NULL);
191+
*end = '\0';
192+
ini_rstrip(start, end);
193+
#endif
194+
/* Non-blank line with leading whitespace, treat as continuation
195+
of previous name's value (as per Python configparser). */
196+
if (!HANDLER(user, section, prev_name, start) && !error)
197+
error = lineno;
198+
}
199+
#endif
200+
else if (*start == '[') {
201+
/* A "[section]" line */
202+
end = ini_find_chars_or_comment(start + 1, "]");
203+
if (*end == ']') {
204+
*end = '\0';
205+
ini_strncpy0(section, start + 1, sizeof(section));
206+
#if INI_ALLOW_MULTILINE
207+
*prev_name = '\0';
208+
#endif
209+
#if INI_CALL_HANDLER_ON_NEW_SECTION
210+
if (!HANDLER(user, section, NULL, NULL) && !error)
211+
error = lineno;
212+
#endif
213+
}
214+
else if (!error) {
215+
/* No ']' found on section line */
216+
error = lineno;
217+
}
218+
}
219+
else if (*start) {
220+
/* Not a comment, must be a name[=:]value pair */
221+
end = ini_find_chars_or_comment(start, "=:");
222+
if (*end == '=' || *end == ':') {
223+
*end = '\0';
224+
name = ini_rstrip(start, end);
225+
value = end + 1;
226+
#if INI_ALLOW_INLINE_COMMENTS
227+
end = ini_find_chars_or_comment(value, NULL);
228+
*end = '\0';
229+
#endif
230+
value = ini_lskip(value);
231+
ini_rstrip(value, end);
232+
233+
#if INI_ALLOW_MULTILINE
234+
ini_strncpy0(prev_name, name, sizeof(prev_name));
235+
#endif
236+
/* Valid name[=:]value pair found, call handler */
237+
if (!HANDLER(user, section, name, value) && !error)
238+
error = lineno;
239+
}
240+
else {
241+
/* No '=' or ':' found on name[=:]value line */
242+
#if INI_ALLOW_NO_VALUE
243+
*end = '\0';
244+
name = ini_rstrip(start, end);
245+
if (!HANDLER(user, section, name, NULL) && !error)
246+
error = lineno;
247+
#else
248+
if (!error)
249+
error = lineno;
250+
#endif
251+
}
252+
}
253+
254+
#if INI_STOP_ON_FIRST_ERROR
255+
if (error)
256+
break;
257+
#endif
258+
}
259+
260+
#if !INI_USE_STACK
261+
ini_free(line);
262+
#endif
263+
264+
return error;
265+
}
266+
267+
/* See documentation in header file. */
268+
int ini_parse_file(FILE* file, ini_handler handler, void* user)
269+
{
270+
return ini_parse_stream((ini_reader)fgets, file, handler, user);
271+
}
272+
273+
/* See documentation in header file. */
274+
int ini_parse(const char* filename, ini_handler handler, void* user)
275+
{
276+
FILE* file;
277+
int error;
278+
279+
file = fopen(filename, "r");
280+
if (!file)
281+
return -1;
282+
error = ini_parse_file(file, handler, user);
283+
fclose(file);
284+
return error;
285+
}
286+
287+
/* An ini_reader function to read the next line from a string buffer. This
288+
is the fgets() equivalent used by ini_parse_string(). */
289+
static char* ini_reader_string(char* str, int num, void* stream) {
290+
ini_parse_string_ctx* ctx = (ini_parse_string_ctx*)stream;
291+
const char* ctx_ptr = ctx->ptr;
292+
size_t ctx_num_left = ctx->num_left;
293+
char* strp = str;
294+
char c;
295+
296+
if (ctx_num_left == 0 || num < 2)
297+
return NULL;
298+
299+
while (num > 1 && ctx_num_left != 0) {
300+
c = *ctx_ptr++;
301+
ctx_num_left--;
302+
*strp++ = c;
303+
if (c == '\n')
304+
break;
305+
num--;
306+
}
307+
308+
*strp = '\0';
309+
ctx->ptr = ctx_ptr;
310+
ctx->num_left = ctx_num_left;
311+
return str;
312+
}
313+
314+
/* See documentation in header file. */
315+
int ini_parse_string(const char* string, ini_handler handler, void* user) {
316+
return ini_parse_string_length(string, strlen(string), handler, user);
317+
}
318+
319+
/* See documentation in header file. */
320+
int ini_parse_string_length(const char* string, size_t length,
321+
ini_handler handler, void* user) {
322+
ini_parse_string_ctx ctx;
323+
324+
ctx.ptr = string;
325+
ctx.num_left = length;
326+
return ini_parse_stream((ini_reader)ini_reader_string, &ctx, handler,
327+
user);
328+
}

0 commit comments

Comments
 (0)