From 43b5f170894fa3cf66cfd59ffeca3d35eba96f4d Mon Sep 17 00:00:00 2001 From: DeepanshuPratik Date: Sat, 18 Mar 2023 11:09:08 +0530 Subject: [PATCH 1/4] Added module include_one which fixes the issue of loading multiple config with same config name instead of overriding it according to priority --- include/sway/commands.h | 1 + sway/commands.c | 1 + sway/commands/include_one.c | 78 +++++++++++++++++++++++++++++++++++++ sway/meson.build | 1 + 4 files changed, 81 insertions(+) create mode 100644 sway/commands/include_one.c diff --git a/include/sway/commands.h b/include/sway/commands.h index 67fae5a607..c723bfe83b 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -142,6 +142,7 @@ sway_cmd cmd_fullscreen; sway_cmd cmd_gaps; sway_cmd cmd_hide_edge_borders; sway_cmd cmd_include; +sway_cmd cmd_include_one; sway_cmd cmd_inhibit_idle; sway_cmd cmd_input; sway_cmd cmd_seat; diff --git a/sway/commands.c b/sway/commands.c index 6f2649a29e..c7d8f8ed4f 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -103,6 +103,7 @@ static const struct cmd_handler handlers[] = { static const struct cmd_handler config_handlers[] = { { "default_orientation", cmd_default_orientation }, { "include", cmd_include }, + { "include_one", cmd_include_one}, { "swaybg_command", cmd_swaybg_command }, { "swaynag_command", cmd_swaynag_command }, { "workspace_layout", cmd_workspace_layout }, diff --git a/sway/commands/include_one.c b/sway/commands/include_one.c new file mode 100644 index 0000000000..fe6ae38b81 --- /dev/null +++ b/sway/commands/include_one.c @@ -0,0 +1,78 @@ +#define _XOPEN_SOURCE 700 +#include "sway/commands.h" +#include "sway/config.h" +#include "list.h" +#include "log.h" +#include "unistd.h" +#include +#include +#include + +typedef struct { + char *name; + char *path; +} config_location_map; + +int compare_paths(const config_location_map* item, char* abspath) { + if (strcmp(item->name, basename(abspath))==0) { + return 0; + } + return -1; +} + +void priority_configs(const char *path, const char *parent_dir, struct sway_config *config, list_t *locations) { + char *wd = getcwd(NULL, 0); + + if (chdir(parent_dir) < 0) { + sway_log(SWAY_ERROR, "failed to change working directory"); + goto cleanup; + } + + wordexp_t p; + if (wordexp(path, &p, 0) == 0) { + char **w = p.we_wordv; + size_t i; + for (i = 0; i < p.we_wordc; ++i) { + int index = list_seq_find(locations, (int (*)(const void *, const void *))compare_paths, w[i]); + char* matched_path = strdup(w[i]); + config_location_map config_loc = {basename(matched_path), matched_path}; + if (index == -1) { + list_add(locations, &config_loc); + continue; + } + locations->items[index] = &config_loc; + } + } + + wordfree(&p); + + if (chdir(wd) < 0) { + sway_log(SWAY_ERROR, "failed to change working directory"); + } +cleanup: + free(wd); +} + +struct cmd_results *cmd_include_one(int argc, char **argv) { + + struct cmd_results *error = NULL; + char *parent_path = strdup(config->current_config_path); + const char *parent_dir = dirname(parent_path); + list_t *locs = create_list(); + + if ((error = checkarg(argc, "include_one", EXPECTED_AT_LEAST, 1))) { + return error; + } + + for(int i=0; iitems; + for(int i=0; ilength; ++i) { + load_include_configs (locs_arr[i]->path, config, &config->swaynag_config_errors); + } + list_free(locs); + return cmd_results_new(CMD_SUCCESS, NULL); +} diff --git a/sway/meson.build b/sway/meson.build index 5573d433e3..fbe160fc2b 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -72,6 +72,7 @@ sway_sources = files( 'commands/max_render_time.c', 'commands/opacity.c', 'commands/include.c', + 'commands/include_one.c', 'commands/input.c', 'commands/layout.c', 'commands/mode.c', From 2801ed559659ad38b18cc6525e56e55e344a9471 Mon Sep 17 00:00:00 2001 From: DeepanshuPratik Date: Sun, 19 Mar 2023 17:50:37 +0530 Subject: [PATCH 2/4] fixed memory management issues --- sway/commands/include_one.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sway/commands/include_one.c b/sway/commands/include_one.c index fe6ae38b81..ba5aa4bbe0 100644 --- a/sway/commands/include_one.c +++ b/sway/commands/include_one.c @@ -35,7 +35,9 @@ void priority_configs(const char *path, const char *parent_dir, struct sway_conf for (i = 0; i < p.we_wordc; ++i) { int index = list_seq_find(locations, (int (*)(const void *, const void *))compare_paths, w[i]); char* matched_path = strdup(w[i]); - config_location_map config_loc = {basename(matched_path), matched_path}; + config_location_map* config_loc = malloc(sizeof(config_location_map)); + config_loc->name = basename(matched_path); + config_loc->path = matched_path; if (index == -1) { list_add(locations, &config_loc); continue; @@ -51,6 +53,7 @@ void priority_configs(const char *path, const char *parent_dir, struct sway_conf } cleanup: free(wd); + list_free_items_and_destroy(locations); } struct cmd_results *cmd_include_one(int argc, char **argv) { @@ -66,7 +69,6 @@ struct cmd_results *cmd_include_one(int argc, char **argv) { for(int i=0; iitems; From 667c818d44c8dfba7fa77dbf4715a84685a54405 Mon Sep 17 00:00:00 2001 From: DeepanshuPratik Date: Mon, 20 Mar 2023 15:57:52 +0530 Subject: [PATCH 3/4] fixed pointer reference issues --- sway/commands/include_one.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sway/commands/include_one.c b/sway/commands/include_one.c index ba5aa4bbe0..2f9898ef61 100644 --- a/sway/commands/include_one.c +++ b/sway/commands/include_one.c @@ -39,10 +39,10 @@ void priority_configs(const char *path, const char *parent_dir, struct sway_conf config_loc->name = basename(matched_path); config_loc->path = matched_path; if (index == -1) { - list_add(locations, &config_loc); + list_add(locations, config_loc); continue; } - locations->items[index] = &config_loc; + locations->items[index] = config_loc; } } @@ -53,7 +53,6 @@ void priority_configs(const char *path, const char *parent_dir, struct sway_conf } cleanup: free(wd); - list_free_items_and_destroy(locations); } struct cmd_results *cmd_include_one(int argc, char **argv) { @@ -75,6 +74,6 @@ struct cmd_results *cmd_include_one(int argc, char **argv) { for(int i=0; ilength; ++i) { load_include_configs (locs_arr[i]->path, config, &config->swaynag_config_errors); } - list_free(locs); + list_free_items_and_destroy(locs); return cmd_results_new(CMD_SUCCESS, NULL); } From cc6bb5d526cd31a4dd0409f30107b93f1fb5714d Mon Sep 17 00:00:00 2001 From: DeepanshuPratik Date: Thu, 30 Mar 2023 15:30:39 +0530 Subject: [PATCH 4/4] Fixed all memory issues in file include_one.c --- sway/commands/include_one.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/sway/commands/include_one.c b/sway/commands/include_one.c index 2f9898ef61..d922f66265 100644 --- a/sway/commands/include_one.c +++ b/sway/commands/include_one.c @@ -13,13 +13,17 @@ typedef struct { char *path; } config_location_map; -int compare_paths(const config_location_map* item, char* abspath) { +int compare_paths( const config_location_map* item, char* abspath) { if (strcmp(item->name, basename(abspath))==0) { return 0; } return -1; } - +void free_config_location_map(config_location_map *config_loc) { + free(config_loc->name); + free(config_loc->path); + free(config_loc); +} void priority_configs(const char *path, const char *parent_dir, struct sway_config *config, list_t *locations) { char *wd = getcwd(NULL, 0); @@ -32,18 +36,20 @@ void priority_configs(const char *path, const char *parent_dir, struct sway_conf if (wordexp(path, &p, 0) == 0) { char **w = p.we_wordv; size_t i; + config_location_map *config_loc = malloc(sizeof (config_location_map)); for (i = 0; i < p.we_wordc; ++i) { int index = list_seq_find(locations, (int (*)(const void *, const void *))compare_paths, w[i]); - char* matched_path = strdup(w[i]); - config_location_map* config_loc = malloc(sizeof(config_location_map)); - config_loc->name = basename(matched_path); + char* matched_path = strdup(w[i]); + config_loc->name = strdup(basename(matched_path)); config_loc->path = matched_path; if (index == -1) { list_add(locations, config_loc); continue; } + free_config_location_map(locations->items[index]); locations->items[index] = config_loc; } + } wordfree(&p); @@ -74,6 +80,10 @@ struct cmd_results *cmd_include_one(int argc, char **argv) { for(int i=0; ilength; ++i) { load_include_configs (locs_arr[i]->path, config, &config->swaynag_config_errors); } - list_free_items_and_destroy(locs); + for (int i = 0; ilength; ++i) { + free_config_location_map(locs_arr[i]); + } + list_free(locs); + free(parent_path); return cmd_results_new(CMD_SUCCESS, NULL); }