aboutsummaryrefslogtreecommitdiffstats
path: root/swaynag/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaynag/config.c')
-rw-r--r--swaynag/config.c79
1 files changed, 50 insertions, 29 deletions
diff --git a/swaynag/config.c b/swaynag/config.c
index 6db7cce5..efd71ce7 100644
--- a/swaynag/config.c
+++ b/swaynag/config.c
@@ -1,4 +1,3 @@
1#define _POSIX_C_SOURCE 200809L
2#include <getopt.h> 1#include <getopt.h>
3#include <stdio.h> 2#include <stdio.h>
4#include <stdlib.h> 3#include <stdlib.h>
@@ -11,24 +10,40 @@
11#include "util.h" 10#include "util.h"
12#include "wlr-layer-shell-unstable-v1-client-protocol.h" 11#include "wlr-layer-shell-unstable-v1-client-protocol.h"
13 12
14static char *read_from_stdin(void) { 13static char *read_and_trim_stdin(void) {
15 char *buffer = NULL; 14 char *buffer = NULL, *line = NULL;
16 size_t buffer_len = 0; 15 size_t buffer_len = 0, line_size = 0;
17 char *line = NULL; 16 while (1) {
18 size_t line_size = 0; 17 ssize_t nread = getline(&line, &line_size, stdin);
19 ssize_t nread; 18 if (nread == -1) {
20 while ((nread = getline(&line, &line_size, stdin)) != -1) { 19 if (feof(stdin)) {
20 break;
21 } else {
22 perror("getline");
23 goto freeline;
24 }
25 }
21 buffer = realloc(buffer, buffer_len + nread + 1); 26 buffer = realloc(buffer, buffer_len + nread + 1);
22 snprintf(&buffer[buffer_len], nread + 1, "%s", line); 27 if (!buffer) {
28 perror("realloc");
29 goto freebuf;
30 }
31 memcpy(&buffer[buffer_len], line, nread + 1);
23 buffer_len += nread; 32 buffer_len += nread;
24 } 33 }
25 free(line); 34 free(line);
26 35
27 while (buffer && buffer[buffer_len - 1] == '\n') { 36 while (buffer_len && buffer[buffer_len - 1] == '\n') {
28 buffer[--buffer_len] = '\0'; 37 buffer[--buffer_len] = '\0';
29 } 38 }
30 39
31 return buffer; 40 return buffer;
41
42freeline:
43 free(line);
44freebuf:
45 free(buffer);
46 return NULL;
32} 47}
33 48
34int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, 49int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
@@ -150,8 +165,11 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
150 fprintf(stderr, "Missing action for button %s\n", optarg); 165 fprintf(stderr, "Missing action for button %s\n", optarg);
151 return EXIT_FAILURE; 166 return EXIT_FAILURE;
152 } 167 }
153 struct swaynag_button *button; 168 struct swaynag_button *button = calloc(1, sizeof(struct swaynag_button));
154 button = calloc(sizeof(struct swaynag_button), 1); 169 if (!button) {
170 perror("calloc");
171 return EXIT_FAILURE;
172 }
155 button->text = strdup(optarg); 173 button->text = strdup(optarg);
156 button->type = SWAYNAG_ACTION_COMMAND; 174 button->type = SWAYNAG_ACTION_COMMAND;
157 button->action = strdup(argv[optind]); 175 button->action = strdup(argv[optind]);
@@ -207,22 +225,25 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
207 break; 225 break;
208 case 'f': // Font 226 case 'f': // Font
209 if (type) { 227 if (type) {
210 free(type->font); 228 pango_font_description_free(type->font_description);
211 type->font = strdup(optarg); 229 type->font_description = pango_font_description_from_string(optarg);
212 } 230 }
213 break; 231 break;
214 case 'l': // Detailed Message 232 case 'l': // Detailed Message
215 if (swaynag) { 233 if (swaynag) {
216 free(swaynag->details.message); 234 free(swaynag->details.message);
217 swaynag->details.message = read_from_stdin(); 235 swaynag->details.message = read_and_trim_stdin();
236 if (!swaynag->details.message) {
237 return EXIT_FAILURE;
238 }
218 swaynag->details.button_up.text = strdup("▲"); 239 swaynag->details.button_up.text = strdup("▲");
219 swaynag->details.button_down.text = strdup("▼"); 240 swaynag->details.button_down.text = strdup("▼");
220 } 241 }
221 break; 242 break;
222 case 'L': // Detailed Button Text 243 case 'L': // Detailed Button Text
223 if (swaynag) { 244 if (swaynag) {
224 free(swaynag->details.button_details->text); 245 free(swaynag->details.details_text);
225 swaynag->details.button_details->text = strdup(optarg); 246 swaynag->details.details_text = strdup(optarg);
226 } 247 }
227 break; 248 break;
228 case 'm': // Message 249 case 'm': // Message
@@ -239,8 +260,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
239 break; 260 break;
240 case 's': // Dismiss Button Text 261 case 's': // Dismiss Button Text
241 if (swaynag) { 262 if (swaynag) {
242 struct swaynag_button *button_close; 263 struct swaynag_button *button_close = swaynag->buttons->items[0];
243 button_close = swaynag->buttons->items[0];
244 free(button_close->text); 264 free(button_close->text);
245 button_close->text = strdup(optarg); 265 button_close->text = strdup(optarg);
246 } 266 }
@@ -399,23 +419,24 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
399 419
400 if (line[0] == '[') { 420 if (line[0] == '[') {
401 char *close = strchr(line, ']'); 421 char *close = strchr(line, ']');
402 if (!close) { 422 if (!close || close != &line[nread - 2] || nread <= 3) {
403 fprintf(stderr, "Closing bracket not found on line %d\n", 423 fprintf(stderr, "Line %d is malformed\n", line_number);
404 line_number);
405 result = 1; 424 result = 1;
406 break; 425 break;
407 } 426 }
408 char *name = calloc(1, close - line); 427 *close = '\0';
409 strncat(name, line + 1, close - line - 1); 428 type = swaynag_type_get(types, &line[1]);
410 type = swaynag_type_get(types, name);
411 if (!type) { 429 if (!type) {
412 type = swaynag_type_new(name); 430 type = swaynag_type_new(&line[1]);
413 list_add(types, type); 431 list_add(types, type);
414 } 432 }
415 free(name);
416 } else { 433 } else {
417 char *flag = malloc(sizeof(char) * (nread + 3)); 434 char *flag = malloc(nread + 3);
418 sprintf(flag, "--%s", line); 435 if (!flag) {
436 perror("calloc");
437 return EXIT_FAILURE;
438 }
439 snprintf(flag, nread + 3, "--%s", line);
419 char *argv[] = {"swaynag", flag}; 440 char *argv[] = {"swaynag", flag};
420 result = swaynag_parse_options(2, argv, swaynag, types, type, 441 result = swaynag_parse_options(2, argv, swaynag, types, type,
421 NULL, NULL); 442 NULL, NULL);