diff options
author | Will McKinnon <[email protected]> | 2022-08-15 00:54:07 -0400 |
---|---|---|
committer | Will McKinnon <[email protected]> | 2022-08-15 00:54:07 -0400 |
commit | 7b530bf4487f36273938ffd7236dad8a7629790a (patch) | |
tree | 0c5cdceb7fbda31530d2d32388db8e27f67748a0 /swaynag | |
parent | cf1ed777ae56f677bcc0f832c52c04dc65be18ce (diff) |
Revert "merge sway master"
This reverts commit 7460d9f565092836f81b917a040caff57142d91a.
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, 47 insertions, 59 deletions
diff --git a/swaynag/config.c b/swaynag/config.c index b212a0c3..6db7cce5 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -19,10 +19,6 @@ 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; } @@ -156,10 +152,6 @@ 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]); @@ -223,17 +215,14 @@ 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 @@ -417,10 +406,6 @@ 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) { @@ -429,12 +414,8 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { } free(name); } else { - char *flag = malloc(nread + 3); - if (!flag) { - perror("calloc"); - return EXIT_FAILURE; - } - snprintf(flag, nread + 3, "--%s", line); + char *flag = malloc(sizeof(char) * (nread + 3)); + sprintf(flag, "--%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 56e4950b..88007818 100644 --- a/swaynag/main.c +++ b/swaynag/main.c @@ -20,20 +20,33 @@ void sway_terminate(int code) { } int main(int argc, char **argv) { - int status = EXIT_SUCCESS; + int exit_code = 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; - status = swaynag_parse_options(argc, argv, NULL, NULL, NULL, + int launch_status = swaynag_parse_options(argc, argv, NULL, NULL, NULL, &config_path, &debug); - if (status != 0) { + if (launch_status != 0) { + exit_code = launch_status; goto cleanup; } sway_log_init(debug ? SWAY_DEBUG : SWAY_ERROR, NULL); @@ -43,29 +56,29 @@ int main(int argc, char **argv) { } if (config_path) { sway_log(SWAY_DEBUG, "Loading config file: %s", config_path); - status = swaynag_load_config(config_path, &swaynag, types); - if (status != 0) { + int config_status = swaynag_load_config(config_path, &swaynag, types); + free(config_path); + if (config_status != 0) { + exit_code = config_status; 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); - status = swaynag_parse_options(argc, argv, &swaynag, types, + int result = swaynag_parse_options(argc, argv, &swaynag, types, type_args, NULL, NULL); - if (status != 0) { + if (result != 0) { + exit_code = result; goto cleanup; } } if (!swaynag.message) { sway_log(SWAY_ERROR, "No message passed. Please provide --message/-m"); - status = EXIT_FAILURE; + exit_code = EXIT_FAILURE; goto cleanup; } @@ -85,13 +98,11 @@ 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); + list_add(swaynag.buttons, swaynag.details.button_details); + } else { + free(swaynag.details.button_details->text); + free(swaynag.details.button_details); } sway_log(SWAY_DEBUG, "Output: %s", swaynag.type->output); @@ -109,11 +120,12 @@ int main(int argc, char **argv) { swaynag_setup(&swaynag); swaynag_run(&swaynag); - return status; + return exit_code; cleanup: swaynag_types_free(types); - free(swaynag.details.button_details.text); + free(swaynag.details.button_details->text); + free(swaynag.details.button_details); swaynag_destroy(&swaynag); - return status; + return exit_code; } diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c index 5620155d..9b57d578 100644 --- a/swaynag/swaynag.c +++ b/swaynag/swaynag.c @@ -28,13 +28,8 @@ 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); - 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); + char *cmd = malloc(sizeof(char) * (strlen(terminal) + strlen(" -e ") + strlen(fname) + 1)); + sprintf(cmd, "%s -e %s", terminal, fname); execlp("sh", "sh", "-c", cmd, NULL); sway_log_errno(SWAY_ERROR, "Failed to run command, execlp() returned."); free(cmd); @@ -63,7 +58,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 && *terminal) { + if (button->terminal && terminal && strlen(terminal)) { sway_log(SWAY_DEBUG, "Found $TERMINAL: %s", terminal); if (!terminal_execute(terminal, button->action)) { swaynag_destroy(swaynag); @@ -143,7 +138,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 && *env_cursor_size) { + if (env_cursor_size && strlen(env_cursor_size) > 0) { errno = 0; char *end; unsigned size = strtoul(env_cursor_size, &end, 10); @@ -344,7 +339,6 @@ 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; } @@ -362,10 +356,6 @@ 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; @@ -521,8 +511,13 @@ void swaynag_destroy(struct swaynag *swaynag) { swaynag_seat_destroy(seat); } - destroy_buffer(&swaynag->buffers[0]); - destroy_buffer(&swaynag->buffers[1]); + if (&swaynag->buffers[0]) { + destroy_buffer(&swaynag->buffers[0]); + } + + if (&swaynag->buffers[1]) { + destroy_buffer(&swaynag->buffers[1]); + } if (swaynag->outputs.prev || swaynag->outputs.next) { struct swaynag_output *output, *temp; |