/
confparse.c
163 lines (139 loc) · 3.7 KB
/
confparse.c
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
/** @file
Light-weight (i.e. dumb) config-file parser.
- a valid config line is a keyword followed by an argument to the end of line
- whitespace around the keyword is ignored
- comments start with a hash sign, no inline comments, empty lines are ok.
- whitespace is space and tab
Copyright (C) 2018 Christian W. Zuckschwerdt <zany@triq.net>
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 of the License, or
(at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "confparse.h"
#include "fatal.h"
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#ifdef _MSC_VER
#define F_OK 0
#define R_OK (1<<2)
#endif
#endif
#ifndef _MSC_VER
#include <unistd.h>
#endif
static off_t fsize(const char *path)
{
struct stat st;
if (stat(path, &st) == 0)
return st.st_size;
return -1;
}
int hasconf(char const *path)
{
return !access(path, R_OK);
}
char *readconf(char const *path)
{
FILE *fp;
char *conf;
off_t file_size = fsize(path);
if (file_size < 0) {
fprintf(stderr, "Failed to stat \"%s\"\n", path);
return NULL;
}
fp = fopen(path, "rb");
if (fp == NULL) {
fprintf(stderr, "Failed to open \"%s\"\n", path);
return NULL;
}
conf = malloc(file_size + 1);
if (!conf) {
WARN_MALLOC("readconf()");
fprintf(stderr, "Failed to allocate memory for \"%s\"\n", path);
fclose(fp);
return NULL;
}
off_t n_read = fread(conf, sizeof(char), file_size, fp);
fclose(fp);
if (n_read != file_size) {
fprintf(stderr, "Failed to read \"%s\"\n", path);
free(conf);
return NULL;
}
conf[file_size] = '\0';
return conf;
}
int getconf(char **conf, struct conf_keywords const keywords[], char **arg)
{
// abort if no conf or EOF
if (!conf || !*conf || !**conf)
return -1;
char *p = *conf;
// skip whitespace and comments
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n' || *p == '#')
if (*p++ == '#') {
while (*p && *p != '\r' && *p != '\n') p++;
}
// abort if EOF
if (!*p)
return -1;
// parse keyword
char *kw = p;
while (*p && *p != ' ' && *p != '\t' && *p != '\r' && *p != '\n')
p++;
if (*p)
*p++ = '\0';
// parse arg
while (*p == ' ' || *p == '\t')
p++;
char *ka = p;
if (*p == '{') { // quoted
ka = ++p;
while (*p) { // skip to end-quote
while (*p && *p != '}')
p++;
char *e = p; // possible end-quote
if (*p)
p++;
// skip ws
while (*p == ' ' || *p == '\t')
p++;
// check if proper end-quote
if (!*p || *p == '\r' || *p == '\n' || *p == '#') {
*e = '\0';
break;
}
}
} else { // not quoted
// find end of arg/eol
while (*p && *p != '\r' && *p != '\n' && *p != '#')
p++;
// skip eol comments
if (*p == '#') {
*p++ = '\0';
while (*p && *p != '\r' && *p != '\n')
p++;
}
if (*p)
*p++ = '\0';
}
// set OUT vars
if (arg)
*arg = ka;
*conf = p;
// decode keyword
for (; keywords->keyword; keywords++) {
if (!strcmp(keywords->keyword, kw)) {
return keywords->key;
}
}
fprintf(stderr, "Unknown keyword \"%s\"\n", kw);
return '?';
}