diff options
Diffstat (limited to 'swaynag')
-rw-r--r-- | swaynag/config.c | 27 | ||||
-rw-r--r-- | swaynag/main.c | 52 | ||||
-rw-r--r-- | swaynag/swaynag.c | 27 |
3 files changed, 59 insertions, 47 deletions
diff --git a/swaynag/config.c b/swaynag/config.c index 6db7cce5..b212a0c3 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -19,6 +19,10 @@ static char *read_from_stdin(void) { ssize_t nread; while ((nread = getline(&line, &line_size, stdin)) != -1) { buffer = realloc(buffer, buffer_len + nread + 1); + if (!buffer) { + perror("realloc"); + return NULL; + } snprintf(&buffer[buffer_len], nread + 1, "%s", line); buffer_len += nread; } @@ -152,6 +156,10 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, } struct swaynag_button *button; button = calloc(sizeof(struct swaynag_button), 1); + if (!button) { + perror("calloc"); + return EXIT_FAILURE; + } button->text = strdup(optarg); button->type = SWAYNAG_ACTION_COMMAND; button->action = strdup(argv[optind]); @@ -215,14 +223,17 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, if (swaynag) { free(swaynag->details.message); swaynag->details.message = read_from_stdin(); + if (!swaynag->details.message) { + return EXIT_FAILURE; + } swaynag->details.button_up.text = strdup("▲"); swaynag->details.button_down.text = strdup("▼"); } break; case 'L': // Detailed Button Text if (swaynag) { - free(swaynag->details.button_details->text); - swaynag->details.button_details->text = strdup(optarg); + free(swaynag->details.button_details.text); + swaynag->details.button_details.text = strdup(optarg); } break; case 'm': // Message @@ -406,6 +417,10 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { break; } char *name = calloc(1, close - line); + if (!name) { + perror("calloc"); + return EXIT_FAILURE; + } strncat(name, line + 1, close - line - 1); type = swaynag_type_get(types, name); if (!type) { @@ -414,8 +429,12 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { } free(name); } else { - char *flag = malloc(sizeof(char) * (nread + 3)); - sprintf(flag, "--%s", line); + char *flag = malloc(nread + 3); + if (!flag) { + perror("calloc"); + return EXIT_FAILURE; + } + snprintf(flag, nread + 3, "--%s", line); char *argv[] = {"swaynag", flag}; result = swaynag_parse_options(2, argv, swaynag, types, type, NULL, NULL); diff --git a/swaynag/main.c b/swaynag/main.c index 88007818..56e4950b 100644 --- a/swaynag/main.c +++ b/swaynag/main.c @@ -20,33 +20,20 @@ void sway_terminate(int code) { } int main(int argc, char **argv) { - int exit_code = EXIT_SUCCESS; + int status = EXIT_SUCCESS; list_t *types = create_list(); swaynag_types_add_default(types); - memset(&swaynag, 0, sizeof(swaynag)); swaynag.buttons = create_list(); wl_list_init(&swaynag.outputs); wl_list_init(&swaynag.seats); - struct swaynag_button *button_close = - calloc(sizeof(struct swaynag_button), 1); - button_close->text = strdup("X"); - button_close->type = SWAYNAG_ACTION_DISMISS; - list_add(swaynag.buttons, button_close); - - swaynag.details.button_details = - calloc(sizeof(struct swaynag_button), 1); - swaynag.details.button_details->text = strdup("Toggle details"); - swaynag.details.button_details->type = SWAYNAG_ACTION_EXPAND; - char *config_path = NULL; bool debug = false; - int launch_status = swaynag_parse_options(argc, argv, NULL, NULL, NULL, + status = swaynag_parse_options(argc, argv, NULL, NULL, NULL, &config_path, &debug); - if (launch_status != 0) { - exit_code = launch_status; + if (status != 0) { goto cleanup; } sway_log_init(debug ? SWAY_DEBUG : SWAY_ERROR, NULL); @@ -56,29 +43,29 @@ int main(int argc, char **argv) { } if (config_path) { sway_log(SWAY_DEBUG, "Loading config file: %s", config_path); - int config_status = swaynag_load_config(config_path, &swaynag, types); - free(config_path); - if (config_status != 0) { - exit_code = config_status; + status = swaynag_load_config(config_path, &swaynag, types); + if (status != 0) { goto cleanup; } } + swaynag.details.button_details.text = strdup("Toggle details"); + swaynag.details.button_details.type = SWAYNAG_ACTION_EXPAND; + if (argc > 1) { struct swaynag_type *type_args = swaynag_type_new("<args>"); list_add(types, type_args); - int result = swaynag_parse_options(argc, argv, &swaynag, types, + status = swaynag_parse_options(argc, argv, &swaynag, types, type_args, NULL, NULL); - if (result != 0) { - exit_code = result; + if (status != 0) { goto cleanup; } } if (!swaynag.message) { sway_log(SWAY_ERROR, "No message passed. Please provide --message/-m"); - exit_code = EXIT_FAILURE; + status = EXIT_FAILURE; goto cleanup; } @@ -98,11 +85,13 @@ int main(int argc, char **argv) { swaynag_types_free(types); + struct swaynag_button button_close = { 0 }; + button_close.text = strdup("X"); + button_close.type = SWAYNAG_ACTION_DISMISS; + list_add(swaynag.buttons, &button_close); + if (swaynag.details.message) { - list_add(swaynag.buttons, swaynag.details.button_details); - } else { - free(swaynag.details.button_details->text); - free(swaynag.details.button_details); + list_add(swaynag.buttons, &swaynag.details.button_details); } sway_log(SWAY_DEBUG, "Output: %s", swaynag.type->output); @@ -120,12 +109,11 @@ int main(int argc, char **argv) { swaynag_setup(&swaynag); swaynag_run(&swaynag); - return exit_code; + return status; cleanup: swaynag_types_free(types); - free(swaynag.details.button_details->text); - free(swaynag.details.button_details); + free(swaynag.details.button_details.text); swaynag_destroy(&swaynag); - return exit_code; + return status; } diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c index 9b57d578..5620155d 100644 --- a/swaynag/swaynag.c +++ b/swaynag/swaynag.c @@ -28,8 +28,13 @@ static bool terminal_execute(char *terminal, char *command) { fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); fclose(tmp); chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); - char *cmd = malloc(sizeof(char) * (strlen(terminal) + strlen(" -e ") + strlen(fname) + 1)); - sprintf(cmd, "%s -e %s", terminal, fname); + size_t cmd_size = strlen(terminal) + strlen(" -e ") + strlen(fname) + 1; + char *cmd = malloc(cmd_size); + if (!cmd) { + perror("malloc"); + return false; + } + snprintf(cmd, cmd_size, "%s -e %s", terminal, fname); execlp("sh", "sh", "-c", cmd, NULL); sway_log_errno(SWAY_ERROR, "Failed to run command, execlp() returned."); free(cmd); @@ -58,7 +63,7 @@ static void swaynag_button_execute(struct swaynag *swaynag, } else if (pid == 0) { // Child of the child. Will be reparented to the init process char *terminal = getenv("TERMINAL"); - if (button->terminal && terminal && strlen(terminal)) { + if (button->terminal && terminal && *terminal) { sway_log(SWAY_DEBUG, "Found $TERMINAL: %s", terminal); if (!terminal_execute(terminal, button->action)) { swaynag_destroy(swaynag); @@ -138,7 +143,7 @@ static void update_cursor(struct swaynag_seat *seat) { const char *cursor_theme = getenv("XCURSOR_THEME"); unsigned cursor_size = 24; const char *env_cursor_size = getenv("XCURSOR_SIZE"); - if (env_cursor_size && strlen(env_cursor_size) > 0) { + if (env_cursor_size && *env_cursor_size) { errno = 0; char *end; unsigned size = strtoul(env_cursor_size, &end, 10); @@ -339,6 +344,7 @@ static void handle_global(void *data, struct wl_registry *registry, struct swaynag_seat *seat = calloc(1, sizeof(struct swaynag_seat)); if (!seat) { + perror("calloc"); return; } @@ -356,6 +362,10 @@ static void handle_global(void *data, struct wl_registry *registry, if (!swaynag->output) { struct swaynag_output *output = calloc(1, sizeof(struct swaynag_output)); + if (!output) { + perror("calloc"); + return; + } output->wl_output = wl_registry_bind(registry, name, &wl_output_interface, 4); output->wl_name = name; @@ -511,13 +521,8 @@ void swaynag_destroy(struct swaynag *swaynag) { swaynag_seat_destroy(seat); } - if (&swaynag->buffers[0]) { - destroy_buffer(&swaynag->buffers[0]); - } - - if (&swaynag->buffers[1]) { - destroy_buffer(&swaynag->buffers[1]); - } + destroy_buffer(&swaynag->buffers[0]); + destroy_buffer(&swaynag->buffers[1]); if (swaynag->outputs.prev || swaynag->outputs.next) { struct swaynag_output *output, *temp; |