Skip to content

Commit 697964e

Browse files
committed
config: Parse whitespace separated values correctly
The man page says we accept "whitespace", so tabs seem reasonable.
1 parent f9ed68b commit 697964e

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

src/config.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <X11/Xatom.h>
22
#include <X11/Xlib.h>
3+
#include <ctype.h>
34
#include <errno.h>
45
#include <stdlib.h>
56
#include <string.h>
@@ -158,6 +159,14 @@ static int _nonnull_ convert_launcher(const char *str, void *output) {
158159
#define DEFAULT_SELECTION_STATE(name) \
159160
(struct selection) { name, 0, NULL }
160161

162+
static char *skip_whitespace(char *str) {
163+
while (*str != '\0' && isspace((unsigned char)*str)) {
164+
str++;
165+
}
166+
167+
return str;
168+
}
169+
161170
static int convert_selections(const char *str, void *output) {
162171
struct selection *sels = malloc(3 * sizeof(struct selection));
163172
expect(sels);
@@ -167,12 +176,19 @@ static int convert_selections(const char *str, void *output) {
167176

168177
_drop_(free) char *inner_str = strdup(str);
169178
expect(inner_str);
170-
const char *token = strtok(inner_str, " ");
171-
size_t i;
179+
char *next = skip_whitespace(inner_str);
180+
while (*next != '\0') {
181+
const char *token = next;
182+
while (*next != '\0' && !isspace((unsigned char)*next)) {
183+
next++;
184+
}
185+
if (*next != '\0') {
186+
*next++ = '\0';
187+
next = skip_whitespace(next);
188+
}
172189

173-
while (token) {
174190
bool found = false;
175-
for (i = 0; i < CM_SEL_MAX; i++) {
191+
for (size_t i = 0; i < CM_SEL_MAX; i++) {
176192
if (streq(token, sels[i].name)) {
177193
sels[i].active = true;
178194
found = true;
@@ -182,7 +198,6 @@ static int convert_selections(const char *str, void *output) {
182198
if (!found) {
183199
return -EINVAL;
184200
}
185-
token = strtok(NULL, " ");
186201
}
187202

188203
*(struct selection **)output = sels;
@@ -269,14 +284,25 @@ static int config_parse_file(FILE *file, struct config_entry entries[],
269284

270285
char line[256];
271286
while (fgets(line, sizeof(line), file)) {
272-
const char *key = strtok(line, " ");
273-
char *value = strtok(NULL, "\n");
274-
if (!key || !value) {
287+
char *key = skip_whitespace(line);
288+
if (*key == '\0' || *key == '#') {
275289
continue;
276290
}
277-
while (*value == ' ' || *value == '\t') {
291+
292+
char *value = key;
293+
while (*value != '\0' && !isspace((unsigned char)*value)) {
278294
value++;
279295
}
296+
if (*value == '\0') {
297+
continue;
298+
}
299+
*value++ = '\0';
300+
value = skip_whitespace(value);
301+
302+
char *newline = strpbrk(value, "\r\n");
303+
if (newline) {
304+
*newline = '\0';
305+
}
280306

281307
for (size_t i = 0; i < entries_len; ++i) {
282308
if (!entries[i].is_set && streq(entries[i].config_key, key)) {

0 commit comments

Comments
 (0)