diff options
Diffstat (limited to 'swaynag/config.c')
-rw-r--r-- | swaynag/config.c | 76 |
1 files changed, 33 insertions, 43 deletions
diff --git a/swaynag/config.c b/swaynag/config.c index cd34dcc2..85aa380a 100644 --- a/swaynag/config.c +++ b/swaynag/config.c | |||
@@ -1,11 +1,10 @@ | |||
1 | #define _XOPEN_SOURCE 700 | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #define _POSIX_C_SOURCE 200112L | ||
3 | #include <getopt.h> | 2 | #include <getopt.h> |
3 | #include <stdio.h> | ||
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
5 | #include <wordexp.h> | 5 | #include <wordexp.h> |
6 | #include "log.h" | 6 | #include "log.h" |
7 | #include "list.h" | 7 | #include "list.h" |
8 | #include "readline.h" | ||
9 | #include "swaynag/swaynag.h" | 8 | #include "swaynag/swaynag.h" |
10 | #include "swaynag/types.h" | 9 | #include "swaynag/types.h" |
11 | #include "util.h" | 10 | #include "util.h" |
@@ -13,21 +12,19 @@ | |||
13 | 12 | ||
14 | static char *read_from_stdin(void) { | 13 | static char *read_from_stdin(void) { |
15 | char *buffer = NULL; | 14 | char *buffer = NULL; |
16 | while (!feof(stdin)) { | 15 | size_t buffer_len = 0; |
17 | char *line = read_line(stdin); | 16 | char *line = NULL; |
18 | if (!line) { | 17 | size_t line_size = 0; |
19 | continue; | 18 | ssize_t nread; |
20 | } | 19 | while ((nread = getline(&line, &line_size, stdin)) != -1) { |
21 | 20 | buffer = realloc(buffer, buffer_len + nread); | |
22 | size_t curlen = buffer ? strlen(buffer) : 0; | 21 | snprintf(&buffer[buffer_len], nread + 1, "%s", line); |
23 | buffer = realloc(buffer, curlen + strlen(line) + 2); | 22 | buffer_len += nread; |
24 | snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line); | ||
25 | |||
26 | free(line); | ||
27 | } | 23 | } |
24 | free(line); | ||
28 | 25 | ||
29 | while (buffer && buffer[strlen(buffer) - 1] == '\n') { | 26 | while (buffer && buffer[buffer_len - 1] == '\n') { |
30 | buffer[strlen(buffer) - 1] = '\0'; | 27 | buffer[--buffer_len] = '\0'; |
31 | } | 28 | } |
32 | 29 | ||
33 | return buffer; | 30 | return buffer; |
@@ -53,6 +50,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, | |||
53 | 50 | ||
54 | static struct option opts[] = { | 51 | static struct option opts[] = { |
55 | {"button", required_argument, NULL, 'b'}, | 52 | {"button", required_argument, NULL, 'b'}, |
53 | {"button-no-terminal", required_argument, NULL, 'B'}, | ||
56 | {"config", required_argument, NULL, 'c'}, | 54 | {"config", required_argument, NULL, 'c'}, |
57 | {"debug", no_argument, NULL, 'd'}, | 55 | {"debug", no_argument, NULL, 'd'}, |
58 | {"edge", required_argument, NULL, 'e'}, | 56 | {"edge", required_argument, NULL, 'e'}, |
@@ -87,7 +85,10 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, | |||
87 | "Usage: swaynag [options...]\n" | 85 | "Usage: swaynag [options...]\n" |
88 | "\n" | 86 | "\n" |
89 | " -b, --button <text> <action> Create a button with text that " | 87 | " -b, --button <text> <action> Create a button with text that " |
90 | "executes action when pressed. Multiple buttons can be defined.\n" | 88 | "executes action in a terminal when pressed. Multiple buttons can " |
89 | "be defined.\n" | ||
90 | " -B, --button-no-terminal <text> <action> Like --button, but does" | ||
91 | "not run the action in a terminal.\n" | ||
91 | " -c, --config <path> Path to config file.\n" | 92 | " -c, --config <path> Path to config file.\n" |
92 | " -d, --debug Enable debugging.\n" | 93 | " -d, --debug Enable debugging.\n" |
93 | " -e, --edge top|bottom Set the edge to use.\n" | 94 | " -e, --edge top|bottom Set the edge to use.\n" |
@@ -118,12 +119,13 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, | |||
118 | 119 | ||
119 | optind = 1; | 120 | optind = 1; |
120 | while (1) { | 121 | while (1) { |
121 | int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL); | 122 | int c = getopt_long(argc, argv, "b:B:c:de:f:hlL:m:o:s:t:v", opts, NULL); |
122 | if (c == -1) { | 123 | if (c == -1) { |
123 | break; | 124 | break; |
124 | } | 125 | } |
125 | switch (c) { | 126 | switch (c) { |
126 | case 'b': // Button | 127 | case 'b': // Button |
128 | case 'B': // Button (No Terminal) | ||
127 | if (swaynag) { | 129 | if (swaynag) { |
128 | if (optind >= argc) { | 130 | if (optind >= argc) { |
129 | fprintf(stderr, "Missing action for button %s\n", optarg); | 131 | fprintf(stderr, "Missing action for button %s\n", optarg); |
@@ -134,6 +136,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, | |||
134 | button->text = strdup(optarg); | 136 | button->text = strdup(optarg); |
135 | button->type = SWAYNAG_ACTION_COMMAND; | 137 | button->type = SWAYNAG_ACTION_COMMAND; |
136 | button->action = strdup(argv[optind]); | 138 | button->action = strdup(argv[optind]); |
139 | button->terminal = c == 'b'; | ||
137 | list_add(swaynag->buttons, button); | 140 | list_add(swaynag->buttons, button); |
138 | } | 141 | } |
139 | optind++; | 142 | optind++; |
@@ -343,32 +346,24 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { | |||
343 | type->name = strdup("<config>"); | 346 | type->name = strdup("<config>"); |
344 | list_add(types, type); | 347 | list_add(types, type); |
345 | 348 | ||
346 | char *line; | 349 | char *line = NULL; |
350 | size_t line_size = 0; | ||
351 | ssize_t nread; | ||
347 | int line_number = 0; | 352 | int line_number = 0; |
348 | while (!feof(config)) { | 353 | int result = 0; |
349 | line = read_line(config); | 354 | while ((nread = getline(&line, &line_size, config)) != -1) { |
350 | if (!line) { | ||
351 | continue; | ||
352 | } | ||
353 | |||
354 | line_number++; | 355 | line_number++; |
355 | if (line[0] == '#') { | 356 | if (!*line || line[0] == '\n' || line[0] == '#') { |
356 | free(line); | ||
357 | continue; | ||
358 | } | ||
359 | if (strlen(line) == 0) { | ||
360 | free(line); | ||
361 | continue; | 357 | continue; |
362 | } | 358 | } |
363 | 359 | ||
364 | if (line[0] == '[') { | 360 | if (line[0] == '[') { |
365 | char *close = strchr(line, ']'); | 361 | char *close = strchr(line, ']'); |
366 | if (!close) { | 362 | if (!close) { |
367 | free(line); | ||
368 | fclose(config); | ||
369 | fprintf(stderr, "Closing bracket not found on line %d\n", | 363 | fprintf(stderr, "Closing bracket not found on line %d\n", |
370 | line_number); | 364 | line_number); |
371 | return 1; | 365 | result = 1; |
366 | break; | ||
372 | } | 367 | } |
373 | char *name = calloc(1, close - line); | 368 | char *name = calloc(1, close - line); |
374 | strncat(name, line + 1, close - line - 1); | 369 | strncat(name, line + 1, close - line - 1); |
@@ -380,22 +375,17 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { | |||
380 | } | 375 | } |
381 | free(name); | 376 | free(name); |
382 | } else { | 377 | } else { |
383 | char flag[strlen(line) + 3]; | 378 | char flag[nread + 3]; |
384 | sprintf(flag, "--%s", line); | 379 | sprintf(flag, "--%s", line); |
385 | char *argv[] = {"swaynag", flag}; | 380 | char *argv[] = {"swaynag", flag}; |
386 | int result; | ||
387 | result = swaynag_parse_options(2, argv, swaynag, types, type, | 381 | result = swaynag_parse_options(2, argv, swaynag, types, type, |
388 | NULL, NULL); | 382 | NULL, NULL); |
389 | if (result != 0) { | 383 | if (result != 0) { |
390 | free(line); | 384 | break; |
391 | fclose(config); | ||
392 | return result; | ||
393 | } | 385 | } |
394 | } | 386 | } |
395 | |||
396 | free(line); | ||
397 | } | 387 | } |
388 | free(line); | ||
398 | fclose(config); | 389 | fclose(config); |
399 | return 0; | 390 | return result; |
400 | } | 391 | } |
401 | |||