From 6124d0f9a202ba2f39125602a35bb47d3742022b Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sat, 28 Jul 2018 22:56:12 -0400 Subject: swaynag: split config into own file and fix optind --- swaynag/config.c | 292 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 swaynag/config.c (limited to 'swaynag/config.c') diff --git a/swaynag/config.c b/swaynag/config.c new file mode 100644 index 00000000..4e7edf2e --- /dev/null +++ b/swaynag/config.c @@ -0,0 +1,292 @@ +#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 200112L +#include +#include +#include +#include "log.h" +#include "list.h" +#include "readline.h" +#include "swaynag/nagbar.h" +#include "swaynag/types.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +static char *read_from_stdin() { + char *buffer = NULL; + while (!feof(stdin)) { + char *line = read_line(stdin); + if (!line) { + continue; + } + + if (!buffer) { + buffer = strdup(line); + } else { + buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); + strcat(buffer, line); + strcat(buffer, "\n"); + } + + free(line); + } + + if (buffer && buffer[strlen(buffer) - 1] == '\n') { + buffer[strlen(buffer) - 1] = '\0'; + } + + return buffer; +} + +int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, + list_t *types, char **config, bool *debug) { + static struct option opts[] = { + {"button", required_argument, NULL, 'b'}, + {"config", required_argument, NULL, 'c'}, + {"debug", no_argument, NULL, 'd'}, + {"edge", required_argument, NULL, 'e'}, + {"font", required_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {"detailed-message", no_argument, NULL, 'l'}, + {"detailed-button", required_argument, NULL, 'L'}, + {"message", required_argument, NULL, 'm'}, + {"output", required_argument, NULL, 'o'}, + {"dismiss-button", required_argument, NULL, 's'}, + {"type", required_argument, NULL, 't'}, + {"version", no_argument, NULL, 'v'}, + {0, 0, 0, 0} + }; + + const char *usage = + "Usage: swaynag [options...]\n" + "\n" + " -b, --button Create a button with text that " + "executes action when pressed. Multiple buttons can be defined.\n" + " -c, --config Path to config file.\n" + " -d, --debug Enable debugging.\n" + " -e, --edge top|bottom Set the edge to use.\n" + " -f, --font Set the font to use.\n" + " -h, --help Show help message and quit.\n" + " -l, --detailed-message Read a detailed message from stdin.\n" + " -L, --detailed-button Set the text of the detail button.\n" + " -m, --message Set the message text.\n" + " -o, --output Set the output to use.\n" + " -s, --dismiss-button Set the dismiss button text.\n" + " -t, --type Set the message type.\n" + " -v, --version Show the version number and quit.\n"; + + optind = 1; + while (1) { + int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL); + if (c == -1) { + break; + } + switch (c) { + case 'b': // Button + if (nagbar) { + if (optind >= argc) { + fprintf(stderr, "Missing action for button %s\n", optarg); + return EXIT_FAILURE; + } + struct sway_nagbar_button *button; + button = calloc(sizeof(struct sway_nagbar_button), 1); + button->text = strdup(optarg); + button->type = NAGBAR_ACTION_COMMAND; + button->action = strdup(argv[optind]); + list_add(nagbar->buttons, button); + } + optind++; + break; + case 'c': // Config + if (config) { + *config = strdup(optarg); + } + break; + case 'd': // Debug + if (debug) { + *debug = true; + } + break; + case 'e': // Edge + if (nagbar) { + if (strcmp(optarg, "top") == 0) { + nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + } else if (strcmp(optarg, "bottom") == 0) { + nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + } else { + fprintf(stderr, "Invalid edge: %s\n", optarg); + return EXIT_FAILURE; + } + } + break; + case 'f': // Font + if (nagbar) { + free(nagbar->font); + nagbar->font = strdup(optarg); + } + break; + case 'l': // Detailed Message + if (nagbar) { + free(nagbar->details.message); + nagbar->details.message = read_from_stdin(); + nagbar->details.button_up.text = strdup("▲"); + nagbar->details.button_down.text = strdup("▼"); + } + break; + case 'L': // Detailed Button Text + if (nagbar) { + free(nagbar->details.button_details.text); + nagbar->details.button_details.text = strdup(optarg); + } + break; + case 'm': // Message + if (nagbar) { + free(nagbar->message); + nagbar->message = strdup(optarg); + } + break; + case 'o': // Output + if (nagbar) { + free(nagbar->output.name); + nagbar->output.name = strdup(optarg); + } + break; + case 's': // Dismiss Button Text + if (nagbar) { + struct sway_nagbar_button *button_close; + button_close = nagbar->buttons->items[0]; + free(button_close->text); + button_close->text = strdup(optarg); + } + break; + case 't': // Type + if (nagbar) { + nagbar->type = nagbar_type_get(types, optarg); + if (!nagbar->type) { + fprintf(stderr, "Unknown type %s\n", optarg); + return EXIT_FAILURE; + } + } + break; + case 'v': // Version + fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); + return -1; + default: // Help or unknown flag + fprintf(c == 'h' ? stdout : stderr, "%s", usage); + return -1; + } + } + + return 0; +} + +static bool file_exists(const char *path) { + return path && access(path, R_OK) != -1; +} + +char *nagbar_get_config_path(void) { + static const char *config_paths[] = { + "$HOME/.swaynag/config", + "$XDG_CONFIG_HOME/swaynag/config", + SYSCONFDIR "/swaynag/config", + }; + + if (!getenv("XDG_CONFIG_HOME")) { + char *home = getenv("HOME"); + char *config_home = malloc(strlen(home) + strlen("/.config") + 1); + if (!config_home) { + wlr_log(WLR_ERROR, "Unable to allocate $HOME/.config"); + } else { + strcpy(config_home, home); + strcat(config_home, "/.config"); + setenv("XDG_CONFIG_HOME", config_home, 1); + wlr_log(WLR_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); + free(config_home); + } + } + + wordexp_t p; + char *path; + for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { + if (wordexp(config_paths[i], &p, 0) == 0) { + path = strdup(p.we_wordv[0]); + wordfree(&p); + if (file_exists(path)) { + return path; + } + free(path); + } + } + + return NULL; +} + +int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { + FILE *config = fopen(path, "r"); + if (!config) { + fprintf(stderr, "Failed to read config. Running without it.\n"); + return 0; + } + struct sway_nagbar_type *type = NULL; + char *line; + int line_number = 0; + while (!feof(config)) { + line = read_line(config); + if (!line) { + continue; + } + + line_number++; + if (line[0] == '#') { + free(line); + continue; + } + if (strlen(line) == 0) { + free(line); + continue; + } + + if (line[0] == '[') { + char *close = strchr(line, ']'); + if (!close) { + free(line); + fclose(config); + fprintf(stderr, "Closing bracket not found on line %d\n", + line_number); + return 1; + } + char *name = calloc(1, close - line); + strncat(name, line + 1, close - line - 1); + type = nagbar_type_get(types, name); + if (!type) { + type = calloc(1, sizeof(struct sway_nagbar_type)); + type->name = strdup(name); + list_add(types, type); + } + free(name); + } else { + char flag[strlen(line) + 3]; + sprintf(flag, "--%s", line); + char *argv[] = {"swaynag", flag}; + int result; + if (type) { + result = nagbar_parse_type(2, argv, type); + } else { + result = nagbar_parse_options(2, argv, nagbar, types, + NULL, NULL); + } + if (result != 0) { + free(line); + fclose(config); + return result; + } + } + + free(line); + } + fclose(config); + return 0; +} + -- cgit v1.2.3 From a6145914c60351d8e541192c7fe35556f8e02507 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sat, 28 Jul 2018 23:15:12 -0400 Subject: swaynag: refactor {sway_,}nagbar to swaynag --- swaynag/config.c | 80 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'swaynag/config.c') diff --git a/swaynag/config.c b/swaynag/config.c index 4e7edf2e..289fc82a 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -6,7 +6,7 @@ #include "log.h" #include "list.h" #include "readline.h" -#include "swaynag/nagbar.h" +#include "swaynag/swaynag.h" #include "swaynag/types.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" @@ -36,7 +36,7 @@ static char *read_from_stdin() { return buffer; } -int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, +int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, list_t *types, char **config, bool *debug) { static struct option opts[] = { {"button", required_argument, NULL, 'b'}, @@ -81,17 +81,17 @@ int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, } switch (c) { case 'b': // Button - if (nagbar) { + if (swaynag) { if (optind >= argc) { fprintf(stderr, "Missing action for button %s\n", optarg); return EXIT_FAILURE; } - struct sway_nagbar_button *button; - button = calloc(sizeof(struct sway_nagbar_button), 1); + struct swaynag_button *button; + button = calloc(sizeof(struct swaynag_button), 1); button->text = strdup(optarg); - button->type = NAGBAR_ACTION_COMMAND; + button->type = SWAYNAG_ACTION_COMMAND; button->action = strdup(argv[optind]); - list_add(nagbar->buttons, button); + list_add(swaynag->buttons, button); } optind++; break; @@ -106,13 +106,13 @@ int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, } break; case 'e': // Edge - if (nagbar) { + if (swaynag) { if (strcmp(optarg, "top") == 0) { - nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + swaynag->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; } else if (strcmp(optarg, "bottom") == 0) { - nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + swaynag->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; } else { @@ -122,49 +122,49 @@ int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, } break; case 'f': // Font - if (nagbar) { - free(nagbar->font); - nagbar->font = strdup(optarg); + if (swaynag) { + free(swaynag->font); + swaynag->font = strdup(optarg); } break; case 'l': // Detailed Message - if (nagbar) { - free(nagbar->details.message); - nagbar->details.message = read_from_stdin(); - nagbar->details.button_up.text = strdup("▲"); - nagbar->details.button_down.text = strdup("▼"); + if (swaynag) { + free(swaynag->details.message); + swaynag->details.message = read_from_stdin(); + swaynag->details.button_up.text = strdup("▲"); + swaynag->details.button_down.text = strdup("▼"); } break; case 'L': // Detailed Button Text - if (nagbar) { - free(nagbar->details.button_details.text); - nagbar->details.button_details.text = strdup(optarg); + if (swaynag) { + free(swaynag->details.button_details.text); + swaynag->details.button_details.text = strdup(optarg); } break; case 'm': // Message - if (nagbar) { - free(nagbar->message); - nagbar->message = strdup(optarg); + if (swaynag) { + free(swaynag->message); + swaynag->message = strdup(optarg); } break; case 'o': // Output - if (nagbar) { - free(nagbar->output.name); - nagbar->output.name = strdup(optarg); + if (swaynag) { + free(swaynag->output.name); + swaynag->output.name = strdup(optarg); } break; case 's': // Dismiss Button Text - if (nagbar) { - struct sway_nagbar_button *button_close; - button_close = nagbar->buttons->items[0]; + if (swaynag) { + struct swaynag_button *button_close; + button_close = swaynag->buttons->items[0]; free(button_close->text); button_close->text = strdup(optarg); } break; case 't': // Type - if (nagbar) { - nagbar->type = nagbar_type_get(types, optarg); - if (!nagbar->type) { + if (swaynag) { + swaynag->type = swaynag_type_get(types, optarg); + if (!swaynag->type) { fprintf(stderr, "Unknown type %s\n", optarg); return EXIT_FAILURE; } @@ -186,7 +186,7 @@ static bool file_exists(const char *path) { return path && access(path, R_OK) != -1; } -char *nagbar_get_config_path(void) { +char *swaynag_get_config_path(void) { static const char *config_paths[] = { "$HOME/.swaynag/config", "$XDG_CONFIG_HOME/swaynag/config", @@ -223,13 +223,13 @@ char *nagbar_get_config_path(void) { return NULL; } -int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { +int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { FILE *config = fopen(path, "r"); if (!config) { fprintf(stderr, "Failed to read config. Running without it.\n"); return 0; } - struct sway_nagbar_type *type = NULL; + struct swaynag_type *type = NULL; char *line; int line_number = 0; while (!feof(config)) { @@ -259,9 +259,9 @@ int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { } char *name = calloc(1, close - line); strncat(name, line + 1, close - line - 1); - type = nagbar_type_get(types, name); + type = swaynag_type_get(types, name); if (!type) { - type = calloc(1, sizeof(struct sway_nagbar_type)); + type = calloc(1, sizeof(struct swaynag_type)); type->name = strdup(name); list_add(types, type); } @@ -272,9 +272,9 @@ int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { char *argv[] = {"swaynag", flag}; int result; if (type) { - result = nagbar_parse_type(2, argv, type); + result = swaynag_parse_type(2, argv, type); } else { - result = nagbar_parse_options(2, argv, nagbar, types, + result = swaynag_parse_options(2, argv, swaynag, types, NULL, NULL); } if (result != 0) { -- cgit v1.2.3 From e01acb6097b583fcf2f6d0e0afe1bd878dd9b683 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sun, 29 Jul 2018 22:42:03 -0400 Subject: swaynag: allow more config options --- swaynag/config.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 131 insertions(+), 18 deletions(-) (limited to 'swaynag/config.c') diff --git a/swaynag/config.c b/swaynag/config.c index 289fc82a..80c5ad88 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -8,6 +8,7 @@ #include "readline.h" #include "swaynag/swaynag.h" #include "swaynag/types.h" +#include "util.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" static char *read_from_stdin() { @@ -37,7 +38,23 @@ static char *read_from_stdin() { } int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, - list_t *types, char **config, bool *debug) { + list_t *types, struct swaynag_type *type, char **config, bool *debug) { + enum type_options { + TO_COLOR_BACKGROUND = 256, + TO_COLOR_BORDER, + TO_COLOR_BORDER_BOTTOM, + TO_COLOR_BUTTON, + TO_COLOR_TEXT, + TO_THICK_BAR_BORDER, + TO_PADDING_MESSAGE, + TO_THICK_DET_BORDER, + TO_THICK_BTN_BORDER, + TO_GAP_BTN, + TO_GAP_BTN_DISMISS, + TO_MARGIN_BTN_RIGHT, + TO_PADDING_BTN, + }; + static struct option opts[] = { {"button", required_argument, NULL, 'b'}, {"config", required_argument, NULL, 'c'}, @@ -52,6 +69,21 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, {"dismiss-button", required_argument, NULL, 's'}, {"type", required_argument, NULL, 't'}, {"version", no_argument, NULL, 'v'}, + + {"background", required_argument, NULL, TO_COLOR_BACKGROUND}, + {"border", required_argument, NULL, TO_COLOR_BORDER}, + {"border-bottom", required_argument, NULL, TO_COLOR_BORDER_BOTTOM}, + {"button-background", required_argument, NULL, TO_COLOR_BUTTON}, + {"text", required_argument, NULL, TO_COLOR_TEXT}, + {"border-bottom-size", required_argument, NULL, TO_THICK_BAR_BORDER}, + {"message-padding", required_argument, NULL, TO_PADDING_MESSAGE}, + {"details-border-size", required_argument, NULL, TO_THICK_DET_BORDER}, + {"button-border-size", required_argument, NULL, TO_THICK_BTN_BORDER}, + {"button-gap", required_argument, NULL, TO_GAP_BTN}, + {"button-dismiss-gap", required_argument, NULL, TO_GAP_BTN_DISMISS}, + {"button-margin-right", required_argument, NULL, TO_MARGIN_BTN_RIGHT}, + {"button-padding", required_argument, NULL, TO_PADDING_BTN}, + {0, 0, 0, 0} }; @@ -71,7 +103,22 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, " -o, --output Set the output to use.\n" " -s, --dismiss-button Set the dismiss button text.\n" " -t, --type Set the message type.\n" - " -v, --version Show the version number and quit.\n"; + " -v, --version Show the version number and quit.\n" + "\n" + "The following appearance options can also be given:\n" + " --background RRGGBB[AA] Background color.\n" + " --border RRGGBB[AA] Border color.\n" + " --border-bottom RRGGBB[AA] Bottom border color.\n" + " --button-background RRGGBB[AA] Button background color.\n" + " --text RRGGBB[AA] Text color.\n" + " --border-bottom-size size Thickness of the bar border.\n" + " --message-padding padding Padding for the message.\n" + " --details-border-size size Thickness for the details border.\n" + " --button-border-size size Thickness for the button border.\n" + " --button-gap gap Size of the gap between buttons\n" + " --button-dismiss-gap gap Size of the gap for dismiss button.\n" + " --button-margin-right margin Margin from dismiss button to edge.\n" + " --button-padding padding Padding for the button text.\n"; optind = 1; while (1) { @@ -106,13 +153,13 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, } break; case 'e': // Edge - if (swaynag) { + if (type) { if (strcmp(optarg, "top") == 0) { - swaynag->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + type->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; } else if (strcmp(optarg, "bottom") == 0) { - swaynag->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + type->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; } else { @@ -122,9 +169,9 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, } break; case 'f': // Font - if (swaynag) { - free(swaynag->font); - swaynag->font = strdup(optarg); + if (type) { + free(type->font); + type->font = strdup(optarg); } break; case 'l': // Detailed Message @@ -148,9 +195,9 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, } break; case 'o': // Output - if (swaynag) { - free(swaynag->output.name); - swaynag->output.name = strdup(optarg); + if (type) { + free(type->output); + type->output = strdup(optarg); } break; case 's': // Dismiss Button Text @@ -173,6 +220,71 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, case 'v': // Version fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); return -1; + case TO_COLOR_BACKGROUND: // Background color + if (type) { + type->background = parse_color(optarg); + } + break; + case TO_COLOR_BORDER: // Border color + if (type) { + type->border = parse_color(optarg); + } + break; + case TO_COLOR_BORDER_BOTTOM: // Bottom border color + if (type) { + type->border_bottom = parse_color(optarg); + } + break; + case TO_COLOR_BUTTON: // Button background color + if (type) { + type->button_background = parse_color(optarg); + } + break; + case TO_COLOR_TEXT: // Text color + if (type) { + type->text = parse_color(optarg); + } + break; + case TO_THICK_BAR_BORDER: // Bottom border thickness + if (type) { + type->bar_border_thickness = strtol(optarg, NULL, 0); + } + break; + case TO_PADDING_MESSAGE: // Message padding + if (type) { + type->message_padding = strtol(optarg, NULL, 0); + } + break; + case TO_THICK_DET_BORDER: // Details border thickness + if (type) { + type->details_border_thickness = strtol(optarg, NULL, 0); + } + break; + case TO_THICK_BTN_BORDER: // Button border thickness + if (type) { + type->button_border_thickness = strtol(optarg, NULL, 0); + } + break; + case TO_GAP_BTN: // Gap between buttons + if (type) { + type->button_gap = strtol(optarg, NULL, 0); + } + break; + case TO_GAP_BTN_DISMISS: // Gap between dismiss button + if (type) { + type->button_gap_close = strtol(optarg, NULL, 0); + } + break; + case TO_MARGIN_BTN_RIGHT: // Margin on the right side of button area + if (type) { + type->button_margin_right = strtol(optarg, NULL, 0); + } + break; + case TO_PADDING_BTN: // Padding for the button text + if (type) { + type->button_padding = strtol(optarg, NULL, 0); + } + break; default: // Help or unknown flag fprintf(c == 'h' ? stdout : stderr, "%s", usage); return -1; @@ -229,7 +341,12 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { fprintf(stderr, "Failed to read config. Running without it.\n"); return 0; } - struct swaynag_type *type = NULL; + + struct swaynag_type *type; + type = calloc(1, sizeof(struct swaynag_type)); + type->name = strdup(""); + list_add(types, type); + char *line; int line_number = 0; while (!feof(config)) { @@ -271,12 +388,8 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { sprintf(flag, "--%s", line); char *argv[] = {"swaynag", flag}; int result; - if (type) { - result = swaynag_parse_type(2, argv, type); - } else { - result = swaynag_parse_options(2, argv, swaynag, types, - NULL, NULL); - } + result = swaynag_parse_options(2, argv, swaynag, types, type, + NULL, NULL); if (result != 0) { free(line); fclose(config); -- cgit v1.2.3 From 26c5ef18ba295e016074c9d87affe5da44e71cb1 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Wed, 1 Aug 2018 22:55:20 -0400 Subject: swaynag: don't drop \n for first line --- swaynag/config.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'swaynag/config.c') diff --git a/swaynag/config.c b/swaynag/config.c index 80c5ad88..d6c5739d 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -19,18 +19,14 @@ static char *read_from_stdin() { continue; } - if (!buffer) { - buffer = strdup(line); - } else { - buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); - strcat(buffer, line); - strcat(buffer, "\n"); - } + size_t curlen = buffer ? strlen(buffer) : 0; + buffer = realloc(buffer, curlen + strlen(line) + 2); + snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line); free(line); } - if (buffer && buffer[strlen(buffer) - 1] == '\n') { + while (buffer && buffer[strlen(buffer) - 1] == '\n') { buffer[strlen(buffer) - 1] = '\0'; } -- cgit v1.2.3