2019-09-13 03:25:25 -04:00
|
|
|
/*
|
2024-02-07 21:16:11 -05:00
|
|
|
* Copyright (C) 2019-2024 Scoopta
|
2019-09-13 03:25:25 -04:00
|
|
|
* This file is part of Wofi
|
|
|
|
* Wofi is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Wofi is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Wofi. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-03-26 02:05:46 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <utils.h>
|
|
|
|
#include <config.h>
|
|
|
|
#include <wofi_api.h>
|
|
|
|
|
2020-08-08 02:36:09 -04:00
|
|
|
static const char* arg_names[] = {"always_parse_args", "show_all", "print_command"};
|
2019-12-02 01:59:04 -05:00
|
|
|
|
|
|
|
static bool always_parse_args;
|
2019-12-02 02:02:49 -05:00
|
|
|
static bool show_all;
|
2020-08-08 02:36:09 -04:00
|
|
|
static bool print_command;
|
2020-01-20 23:09:40 -05:00
|
|
|
static struct mode* mode;
|
2020-01-23 22:59:43 -05:00
|
|
|
static const char* arg_str = "__args";
|
2019-12-02 01:59:04 -05:00
|
|
|
|
2020-01-16 19:35:51 -05:00
|
|
|
struct node {
|
|
|
|
struct widget* widget;
|
|
|
|
struct wl_list link;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct wl_list widgets;
|
|
|
|
|
2020-01-20 23:09:40 -05:00
|
|
|
void wofi_run_init(struct mode* this, struct map* config) {
|
|
|
|
mode = this;
|
2019-12-02 01:59:04 -05:00
|
|
|
always_parse_args = strcmp(config_get(config, arg_names[0], "false"), "true") == 0;
|
2019-12-02 02:02:49 -05:00
|
|
|
show_all = strcmp(config_get(config, arg_names[1], "true"), "true") == 0;
|
2020-08-08 02:36:09 -04:00
|
|
|
print_command = strcmp(config_get(config, arg_names[2], "false"), "true") == 0;
|
2019-12-02 01:59:04 -05:00
|
|
|
|
2020-01-16 19:35:51 -05:00
|
|
|
wl_list_init(&widgets);
|
|
|
|
|
2019-09-13 03:25:25 -04:00
|
|
|
struct map* cached = map_init();
|
2020-01-20 23:09:40 -05:00
|
|
|
struct wl_list* cache = wofi_read_cache(mode);
|
2019-09-13 03:25:25 -04:00
|
|
|
|
2019-12-02 02:02:49 -05:00
|
|
|
struct map* entries = map_init();
|
|
|
|
|
2019-09-13 03:25:25 -04:00
|
|
|
struct cache_line* node, *tmp;
|
|
|
|
wl_list_for_each_safe(node, tmp, cache, link) {
|
2020-01-24 23:00:56 -05:00
|
|
|
char* text = node->line;
|
2020-01-23 22:59:43 -05:00
|
|
|
if(strncmp(node->line, arg_str, strlen(arg_str)) == 0) {
|
|
|
|
text = strchr(text, ' ') + 1;
|
|
|
|
}
|
|
|
|
|
2020-01-24 23:00:56 -05:00
|
|
|
char* full_path = text;
|
|
|
|
|
|
|
|
char* final_slash = strrchr(text, '/');
|
|
|
|
if(final_slash != NULL) {
|
|
|
|
text = final_slash + 1;
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:36:54 -05:00
|
|
|
struct stat info;
|
|
|
|
stat(node->line, &info);
|
2020-01-24 23:00:56 -05:00
|
|
|
if(((access(node->line, X_OK) == 0 && S_ISREG(info.st_mode)) ||
|
|
|
|
strncmp(node->line, arg_str, strlen(arg_str)) == 0) && !map_contains(cached, full_path)) {
|
2020-01-16 19:35:51 -05:00
|
|
|
struct node* widget = malloc(sizeof(struct node));
|
2020-01-24 23:00:56 -05:00
|
|
|
widget->widget = wofi_create_widget(mode, &text, text, &node->line, 1);
|
2020-01-16 19:35:51 -05:00
|
|
|
wl_list_insert(&widgets, &widget->link);
|
2020-01-24 23:00:56 -05:00
|
|
|
map_put(cached, full_path, "true");
|
2020-01-02 17:36:54 -05:00
|
|
|
map_put(entries, text, "true");
|
|
|
|
} else {
|
2020-01-20 23:09:40 -05:00
|
|
|
wofi_remove_cache(mode, node->line);
|
2020-01-02 17:36:54 -05:00
|
|
|
}
|
2019-09-13 03:25:25 -04:00
|
|
|
free(node->line);
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cache);
|
|
|
|
|
|
|
|
char* path = strdup(getenv("PATH"));
|
2019-11-05 01:41:53 -05:00
|
|
|
|
2020-01-28 20:46:16 -05:00
|
|
|
struct map* paths = map_init();
|
|
|
|
|
2019-11-05 01:41:53 -05:00
|
|
|
char* save_ptr;
|
|
|
|
char* str = strtok_r(path, ":", &save_ptr);
|
|
|
|
do {
|
2020-01-28 20:46:16 -05:00
|
|
|
|
|
|
|
str = realpath(str, NULL);
|
2020-01-28 22:17:42 -05:00
|
|
|
if(str == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-28 20:46:16 -05:00
|
|
|
if(map_contains(paths, str)) {
|
|
|
|
free(str);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
map_put(paths, str, "true");
|
|
|
|
|
2019-11-05 01:41:53 -05:00
|
|
|
DIR* dir = opendir(str);
|
2019-09-13 03:25:25 -04:00
|
|
|
if(dir == NULL) {
|
2019-11-05 01:41:53 -05:00
|
|
|
continue;
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
|
|
|
struct dirent* entry;
|
|
|
|
while((entry = readdir(dir)) != NULL) {
|
|
|
|
if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-05 01:41:53 -05:00
|
|
|
char* full_path = utils_concat(3, str, "/", entry->d_name);
|
2019-09-13 03:25:25 -04:00
|
|
|
struct stat info;
|
|
|
|
stat(full_path, &info);
|
2020-01-24 23:00:56 -05:00
|
|
|
if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) &&
|
|
|
|
!map_contains(cached, full_path) &&
|
|
|
|
(show_all || !map_contains(entries, entry->d_name))) {
|
2019-11-13 04:17:45 -05:00
|
|
|
char* text = strdup(entry->d_name);
|
2019-12-02 02:02:49 -05:00
|
|
|
map_put(entries, text, "true");
|
2020-01-16 19:35:51 -05:00
|
|
|
struct node* widget = malloc(sizeof(struct node));
|
2020-01-20 23:09:40 -05:00
|
|
|
widget->widget = wofi_create_widget(mode, &text, text, &full_path, 1);
|
2020-01-16 19:35:51 -05:00
|
|
|
wl_list_insert(&widgets, &widget->link);
|
2019-11-13 04:17:45 -05:00
|
|
|
free(text);
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
|
|
|
free(full_path);
|
|
|
|
}
|
2020-01-28 20:46:16 -05:00
|
|
|
free(str);
|
2019-09-13 03:25:25 -04:00
|
|
|
closedir(dir);
|
2019-11-05 01:41:53 -05:00
|
|
|
} while((str = strtok_r(NULL, ":", &save_ptr)) != NULL);
|
|
|
|
free(path);
|
2020-01-28 20:46:16 -05:00
|
|
|
map_free(paths);
|
2019-09-13 03:25:25 -04:00
|
|
|
map_free(cached);
|
2019-12-02 02:17:23 -05:00
|
|
|
map_free(entries);
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
|
|
|
|
2020-01-31 16:29:00 -05:00
|
|
|
struct widget* wofi_run_get_widget(void) {
|
2020-01-16 19:35:51 -05:00
|
|
|
struct node* node, *tmp;
|
|
|
|
wl_list_for_each_reverse_safe(node, tmp, &widgets, link) {
|
|
|
|
struct widget* widget = node->widget;
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:26:04 -04:00
|
|
|
static char* parse_args(const char* cmd, size_t* space_count) {
|
|
|
|
size_t cmd_l = strlen(cmd);
|
|
|
|
char* ret = calloc(1, cmd_l + 1);
|
|
|
|
|
|
|
|
bool in_quote = false;
|
|
|
|
size_t ret_count = 0;
|
|
|
|
for(size_t count = 0; count < cmd_l; ++count) {
|
|
|
|
if(cmd[count] == ' ' && !in_quote) {
|
|
|
|
++*space_count;
|
|
|
|
ret[ret_count++] = '\n';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cmd[count] == '"') {
|
|
|
|
in_quote = !in_quote;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cmd[count] == '\\') {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
ret[ret_count++] = cmd[count];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-01-23 22:59:43 -05:00
|
|
|
|
2020-06-23 03:35:40 -04:00
|
|
|
void wofi_run_exec(const char* cmd) {
|
2020-01-23 22:59:43 -05:00
|
|
|
bool arg_run = wofi_mod_control() || always_parse_args;
|
|
|
|
if(strncmp(cmd, arg_str, strlen(arg_str)) == 0) {
|
|
|
|
arg_run = true;
|
|
|
|
cmd = strchr(cmd, ' ') + 1;
|
|
|
|
}
|
|
|
|
|
2019-11-19 01:16:06 -05:00
|
|
|
if(wofi_mod_shift()) {
|
2020-01-20 23:09:40 -05:00
|
|
|
wofi_write_cache(mode, cmd);
|
2019-09-27 02:24:48 -04:00
|
|
|
wofi_term_run(cmd);
|
|
|
|
}
|
2020-08-08 02:36:09 -04:00
|
|
|
if(print_command) {
|
|
|
|
printf("%s\n", cmd);
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(0);
|
2020-08-08 02:36:09 -04:00
|
|
|
}
|
2020-01-23 22:59:43 -05:00
|
|
|
if(arg_run) {
|
2019-11-19 01:13:14 -05:00
|
|
|
size_t space_count = 2;
|
2020-06-30 03:26:04 -04:00
|
|
|
char* tmp = parse_args(cmd, &space_count);
|
|
|
|
|
2019-11-19 01:13:14 -05:00
|
|
|
char** args = malloc(space_count * sizeof(char*));
|
|
|
|
char* save_ptr;
|
|
|
|
char* str = strtok_r(tmp, "\n", &save_ptr);
|
|
|
|
size_t count = 0;
|
|
|
|
do {
|
|
|
|
args[count++] = str;
|
|
|
|
} while((str = strtok_r(NULL, "\n", &save_ptr)) != NULL);
|
|
|
|
args[space_count - 1] = NULL;
|
2020-01-23 22:59:43 -05:00
|
|
|
char* cache = utils_concat(2, "__args ", cmd);
|
|
|
|
wofi_write_cache(mode, cache);
|
|
|
|
free(cache);
|
2019-11-19 01:13:14 -05:00
|
|
|
execvp(tmp, args);
|
|
|
|
} else {
|
2020-01-20 23:09:40 -05:00
|
|
|
wofi_write_cache(mode, cmd);
|
2019-11-19 01:13:14 -05:00
|
|
|
execl(cmd, cmd, NULL);
|
|
|
|
}
|
2020-11-02 21:49:15 -05:00
|
|
|
fprintf(stderr, "%s cannot be executed %s\n", cmd, strerror(errno));
|
2024-02-07 21:16:11 -05:00
|
|
|
wofi_exit(errno);
|
2019-09-13 03:25:25 -04:00
|
|
|
}
|
2019-12-02 01:59:04 -05:00
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
const char** wofi_run_get_arg_names(void) {
|
2019-12-02 01:59:04 -05:00
|
|
|
return arg_names;
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:39:47 -05:00
|
|
|
size_t wofi_run_get_arg_count(void) {
|
2019-12-02 01:59:04 -05:00
|
|
|
return sizeof(arg_names) / sizeof(char*);
|
|
|
|
}
|
2020-01-23 23:26:33 -05:00
|
|
|
|
|
|
|
bool wofi_run_no_entry(void) {
|
|
|
|
return true;
|
|
|
|
}
|