diff options
author | 2019-01-08 10:05:37 +0100 | |
---|---|---|
committer | 2019-01-08 10:05:37 +0100 | |
commit | 140bc2dd5b81205df58bf06e695788e689fae397 (patch) | |
tree | 6a88913630734736763b12ec0b10da68ef413256 /swaynag | |
parent | Merge pull request #3337 from RedSoxFan/fix-seat-cmd-cursor (diff) | |
parent | fixup! stringop.c: rewrite strip_whitespace (diff) | |
download | sway-140bc2dd5b81205df58bf06e695788e689fae397.tar.gz sway-140bc2dd5b81205df58bf06e695788e689fae397.tar.zst sway-140bc2dd5b81205df58bf06e695788e689fae397.zip |
Merge pull request #3275 from ianyfan/remove-readline
Rewrite strip_whitespace and remove readline.c
Diffstat (limited to 'swaynag')
-rw-r--r-- | swaynag/config.c | 62 |
1 files changed, 24 insertions, 38 deletions
diff --git a/swaynag/config.c b/swaynag/config.c index e724aa0c..85aa380a 100644 --- a/swaynag/config.c +++ b/swaynag/config.c | |||
@@ -1,10 +1,10 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #include <getopt.h> | 2 | #include <getopt.h> |
3 | #include <stdio.h> | ||
3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
4 | #include <wordexp.h> | 5 | #include <wordexp.h> |
5 | #include "log.h" | 6 | #include "log.h" |
6 | #include "list.h" | 7 | #include "list.h" |
7 | #include "readline.h" | ||
8 | #include "swaynag/swaynag.h" | 8 | #include "swaynag/swaynag.h" |
9 | #include "swaynag/types.h" | 9 | #include "swaynag/types.h" |
10 | #include "util.h" | 10 | #include "util.h" |
@@ -12,21 +12,19 @@ | |||
12 | 12 | ||
13 | static char *read_from_stdin(void) { | 13 | static char *read_from_stdin(void) { |
14 | char *buffer = NULL; | 14 | char *buffer = NULL; |
15 | while (!feof(stdin)) { | 15 | size_t buffer_len = 0; |
16 | char *line = read_line(stdin); | 16 | char *line = NULL; |
17 | if (!line) { | 17 | size_t line_size = 0; |
18 | continue; | 18 | ssize_t nread; |
19 | } | 19 | while ((nread = getline(&line, &line_size, stdin)) != -1) { |
20 | 20 | buffer = realloc(buffer, buffer_len + nread); | |
21 | size_t curlen = buffer ? strlen(buffer) : 0; | 21 | snprintf(&buffer[buffer_len], nread + 1, "%s", line); |
22 | buffer = realloc(buffer, curlen + strlen(line) + 2); | 22 | buffer_len += nread; |
23 | snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line); | ||
24 | |||
25 | free(line); | ||
26 | } | 23 | } |
24 | free(line); | ||
27 | 25 | ||
28 | while (buffer && buffer[strlen(buffer) - 1] == '\n') { | 26 | while (buffer && buffer[buffer_len - 1] == '\n') { |
29 | buffer[strlen(buffer) - 1] = '\0'; | 27 | buffer[--buffer_len] = '\0'; |
30 | } | 28 | } |
31 | 29 | ||
32 | return buffer; | 30 | return buffer; |
@@ -348,32 +346,24 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { | |||
348 | type->name = strdup("<config>"); | 346 | type->name = strdup("<config>"); |
349 | list_add(types, type); | 347 | list_add(types, type); |
350 | 348 | ||
351 | char *line; | 349 | char *line = NULL; |
350 | size_t line_size = 0; | ||
351 | ssize_t nread; | ||
352 | int line_number = 0; | 352 | int line_number = 0; |
353 | while (!feof(config)) { | 353 | int result = 0; |
354 | line = read_line(config); | 354 | while ((nread = getline(&line, &line_size, config)) != -1) { |
355 | if (!line) { | ||
356 | continue; | ||
357 | } | ||
358 | |||
359 | line_number++; | 355 | line_number++; |
360 | if (line[0] == '#') { | 356 | if (!*line || line[0] == '\n' || line[0] == '#') { |
361 | free(line); | ||
362 | continue; | ||
363 | } | ||
364 | if (strlen(line) == 0) { | ||
365 | free(line); | ||
366 | continue; | 357 | continue; |
367 | } | 358 | } |
368 | 359 | ||
369 | if (line[0] == '[') { | 360 | if (line[0] == '[') { |
370 | char *close = strchr(line, ']'); | 361 | char *close = strchr(line, ']'); |
371 | if (!close) { | 362 | if (!close) { |
372 | free(line); | ||
373 | fclose(config); | ||
374 | fprintf(stderr, "Closing bracket not found on line %d\n", | 363 | fprintf(stderr, "Closing bracket not found on line %d\n", |
375 | line_number); | 364 | line_number); |
376 | return 1; | 365 | result = 1; |
366 | break; | ||
377 | } | 367 | } |
378 | char *name = calloc(1, close - line); | 368 | char *name = calloc(1, close - line); |
379 | strncat(name, line + 1, close - line - 1); | 369 | strncat(name, line + 1, close - line - 1); |
@@ -385,21 +375,17 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { | |||
385 | } | 375 | } |
386 | free(name); | 376 | free(name); |
387 | } else { | 377 | } else { |
388 | char flag[strlen(line) + 3]; | 378 | char flag[nread + 3]; |
389 | sprintf(flag, "--%s", line); | 379 | sprintf(flag, "--%s", line); |
390 | char *argv[] = {"swaynag", flag}; | 380 | char *argv[] = {"swaynag", flag}; |
391 | int result; | ||
392 | result = swaynag_parse_options(2, argv, swaynag, types, type, | 381 | result = swaynag_parse_options(2, argv, swaynag, types, type, |
393 | NULL, NULL); | 382 | NULL, NULL); |
394 | if (result != 0) { | 383 | if (result != 0) { |
395 | free(line); | 384 | break; |
396 | fclose(config); | ||
397 | return result; | ||
398 | } | 385 | } |
399 | } | 386 | } |
400 | |||
401 | free(line); | ||
402 | } | 387 | } |
388 | free(line); | ||
403 | fclose(config); | 389 | fclose(config); |
404 | return 0; | 390 | return result; |
405 | } | 391 | } |