summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--meson.build2
-rw-r--r--src/compgen.c113
-rw-r--r--src/compgen.h1
-rw-r--r--src/history.c32
-rw-r--r--src/main.c2
-rw-r--r--src/main_compgen.c6
-rw-r--r--src/mkdirp.c38
-rw-r--r--src/mkdirp.h8
-rw-r--r--src/string_vec.c34
-rw-r--r--src/string_vec.h4
10 files changed, 202 insertions, 38 deletions
diff --git a/meson.build b/meson.build
index ebdcead..85d6176 100644
--- a/meson.build
+++ b/meson.build
@@ -55,6 +55,7 @@ common_sources = files(
'src/entry.c',
'src/history.c',
'src/log.c',
+ 'src/mkdirp.c',
'src/shm.c',
'src/string_vec.c',
'src/surface.c',
@@ -68,6 +69,7 @@ compgen_sources = files(
'src/main_compgen.c',
'src/compgen.c',
'src/log.c',
+ 'src/mkdirp.c',
'src/string_vec.c',
'src/xmalloc.c'
)
diff --git a/src/compgen.c b/src/compgen.c
index b0ea44d..df04386 100644
--- a/src/compgen.c
+++ b/src/compgen.c
@@ -1,14 +1,127 @@
#include <dirent.h>
+#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include "compgen.h"
#include "history.h"
#include "log.h"
+#include "mkdirp.h"
#include "string_vec.h"
#include "xmalloc.h"
+static const char *default_state_dir = ".cache";
+static const char *cache_basename = "tofi-compgen";
+
+[[nodiscard]]
+static char *get_cache_path() {
+ char *cache_name = NULL;
+ const char *state_path = getenv("XDG_CACHE_HOME");
+ if (state_path == NULL) {
+ const char *home = getenv("HOME");
+ if (home == NULL) {
+ log_error("Couldn't retrieve HOME from environment.\n");
+ return NULL;
+ }
+ size_t len = strlen(home) + 1
+ + strlen(default_state_dir) + 1
+ + strlen(cache_basename) + 1;
+ cache_name = xmalloc(len);
+ snprintf(
+ cache_name,
+ len,
+ "%s/%s/%s",
+ home,
+ default_state_dir,
+ cache_basename);
+ } else {
+ size_t len = strlen(state_path) + 1
+ + strlen(cache_basename) + 1;
+ cache_name = xmalloc(len);
+ snprintf(
+ cache_name,
+ len,
+ "%s/%s",
+ state_path,
+ cache_basename);
+ }
+ return cache_name;
+}
+
+struct string_vec compgen_cached()
+{
+ log_debug("Retrieving PATH.\n");
+ const char *env_path = getenv("PATH");
+ if (env_path == NULL) {
+ log_error("Couldn't retrieve PATH from environment.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ log_debug("Retrieving cache location.\n");
+ char *cache_path = get_cache_path();
+
+ struct stat sb;
+ if (cache_path == NULL) {
+ return compgen();
+ }
+
+ /* If the cache doesn't exist, create it and return */
+ errno = 0;
+ if (stat(cache_path, &sb) == -1) {
+ if (errno == ENOENT) {
+ struct string_vec commands = compgen();
+ if (!mkdirp(cache_path)) {
+ free(cache_path);
+ return commands;
+ }
+ FILE *cache = fopen(cache_path, "wb");
+ string_vec_save(&commands, cache);
+ fclose(cache);
+ free(cache_path);
+ return commands;
+ }
+ free(cache_path);
+ return compgen();
+ }
+
+ /* The cache exists, so check if it's still in date */
+ char *path = xstrdup(env_path);
+ char *saveptr = NULL;
+ char *path_entry = strtok_r(path, ":", &saveptr);
+ bool out_of_date = false;
+ while (path_entry != NULL) {
+ struct stat path_sb;
+ if (stat(path_entry, &path_sb) == 0) {
+ if (path_sb.st_mtim.tv_sec > sb.st_mtim.tv_sec) {
+ out_of_date = true;
+ break;
+ }
+ }
+ path_entry = strtok_r(NULL, ":", &saveptr);
+ }
+ free(path);
+
+ struct string_vec commands;
+ if (out_of_date) {
+ log_debug("Cache out of date, updating.\n");
+ log_indent();
+ commands = compgen();
+ log_unindent();
+ FILE *cache = fopen(cache_path, "wb");
+ string_vec_save(&commands, cache);
+ fclose(cache);
+ } else {
+ log_debug("Cache up to date, loading.\n");
+ FILE *cache = fopen(cache_path, "rb");
+ commands = string_vec_load(cache);
+ fclose(cache);
+ }
+ free(cache_path);
+ return commands;
+}
+
struct string_vec compgen()
{
log_debug("Retrieving PATH.\n");
diff --git a/src/compgen.h b/src/compgen.h
index c79f548..a80c62b 100644
--- a/src/compgen.h
+++ b/src/compgen.h
@@ -5,6 +5,7 @@
#include "string_vec.h"
struct string_vec compgen(void);
+struct string_vec compgen_cached(void);
void compgen_history_sort(struct string_vec *programs, struct history *history);
#endif /* COMPGEN_H */
diff --git a/src/history.c b/src/history.c
index 8cb9eaf..89a45d3 100644
--- a/src/history.c
+++ b/src/history.c
@@ -8,6 +8,7 @@
#include <sys/stat.h>
#include "history.h"
#include "log.h"
+#include "mkdirp.h"
#include "xmalloc.h"
static const char *default_state_dir = ".local/state";
@@ -15,7 +16,6 @@ static const char *histfile_basename = "tofi-history";
[[nodiscard]]
static struct history history_create(void);
-static bool mkdirp(const char *path);
static char *get_histfile_path() {
char *histfile_name = NULL;
@@ -49,7 +49,6 @@ static char *get_histfile_path() {
histfile_basename);
}
return histfile_name;
-
}
struct history history_load()
@@ -193,32 +192,3 @@ void history_remove(struct history *restrict vec, const char *restrict str)
}
}
}
-
-bool mkdirp(const char *path)
-{
- struct stat statbuf;
- if (stat(path, &statbuf) == 0) {
- /* If the history file exists, we don't need to do anything. */
- return true;
- }
-
- /*
- * Walk down the path, creating directories as we go.
- * This works by repeatedly finding the next / in path, then calling
- * mkdir() on the string up to that point.
- */
- char *tmp = xstrdup(path);
- char *cursor = tmp;
- while ((cursor = strchr(cursor + 1, '/')) != NULL) {
- *cursor = '\0';
- log_debug("Creating directory %s\n", tmp);
- if (mkdir(tmp, 0700) != 0 && errno != EEXIST) {
- log_error("Error creating history file path: %s.\n", strerror(errno));
- free(tmp);
- return false;
- }
- *cursor = '/';
- }
- free(tmp);
- return true;
-}
diff --git a/src/main.c b/src/main.c
index 806ad1c..ee961f8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -584,7 +584,7 @@ int main(int argc, char *argv[])
log_debug("Generating command list.\n");
log_indent();
tofi.window.entry.history = history_load();
- tofi.window.entry.commands = compgen();
+ tofi.window.entry.commands = compgen_cached();
compgen_history_sort(&tofi.window.entry.commands, &tofi.window.entry.history);
tofi.window.entry.results = string_vec_copy(&tofi.window.entry.commands);
log_unindent();
diff --git a/src/main_compgen.c b/src/main_compgen.c
index a62bc11..743d2cc 100644
--- a/src/main_compgen.c
+++ b/src/main_compgen.c
@@ -4,9 +4,7 @@
int main()
{
- struct string_vec commands = compgen();
- for (size_t i = 0; i < commands.count; i++) {
- printf("%s\n", commands.buf[i]);
- }
+ struct string_vec commands = compgen_cached();
+ string_vec_save(&commands, stdout);
string_vec_destroy(&commands);
}
diff --git a/src/mkdirp.c b/src/mkdirp.c
new file mode 100644
index 0000000..14bfb2c
--- /dev/null
+++ b/src/mkdirp.c
@@ -0,0 +1,38 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include "log.h"
+#include "mkdirp.h"
+#include "xmalloc.h"
+
+bool mkdirp(const char *path)
+{
+ struct stat statbuf;
+ if (stat(path, &statbuf) == 0) {
+ /* If the file exists, we don't need to do anything. */
+ return true;
+ }
+
+ /*
+ * Walk down the path, creating directories as we go.
+ * This works by repeatedly finding the next / in path, then calling
+ * mkdir() on the string up to that point.
+ */
+ char *tmp = xstrdup(path);
+ char *cursor = tmp;
+ while ((cursor = strchr(cursor + 1, '/')) != NULL) {
+ *cursor = '\0';
+ log_debug("Creating directory %s\n", tmp);
+ if (mkdir(tmp, 0700) != 0 && errno != EEXIST) {
+ log_error(
+ "Error creating file path: %s.\n",
+ strerror(errno));
+ free(tmp);
+ return false;
+ }
+ *cursor = '/';
+ }
+ free(tmp);
+ return true;
+}
diff --git a/src/mkdirp.h b/src/mkdirp.h
new file mode 100644
index 0000000..52cba9c
--- /dev/null
+++ b/src/mkdirp.h
@@ -0,0 +1,8 @@
+#ifndef MKDIRP_H
+#define MKDIRP_H
+
+#include <stdbool.h>
+
+bool mkdirp(const char *path);
+
+#endif /* MKDIRP_H */
diff --git a/src/string_vec.c b/src/string_vec.c
index 0bcd7f2..e55dea4 100644
--- a/src/string_vec.c
+++ b/src/string_vec.c
@@ -1,7 +1,8 @@
-#define _GNU_SOURCE /* Required for strcasecmp */
#include <stdint.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/mman.h>
#include "string_vec.h"
#include "xmalloc.h"
@@ -99,9 +100,38 @@ struct string_vec string_vec_filter(
{
struct string_vec filt = string_vec_create();
for (size_t i = 0; i < vec->count; i++) {
- if (strcasestr(vec->buf[i], substr) != NULL) {
+ if (strstr(vec->buf[i], substr) != NULL) {
string_vec_add(&filt, vec->buf[i]);
}
}
return filt;
}
+
+struct string_vec string_vec_load(FILE *file)
+{
+ struct string_vec vec = string_vec_create();
+ if (file == NULL) {
+ return vec;
+ }
+
+ ssize_t bytes_read;
+ char *line = NULL;
+ size_t len;
+ while ((bytes_read = getline(&line, &len, file)) != -1) {
+ if (line[bytes_read - 1] == '\n') {
+ line[bytes_read - 1] = '\0';
+ }
+ string_vec_add(&vec, line);
+ }
+ free(line);
+
+ return vec;
+}
+
+void string_vec_save(struct string_vec *restrict vec, FILE *restrict file)
+{
+ for (size_t i = 0; i < vec->count; i++) {
+ fputs(vec->buf[i], file);
+ fputc('\n', file);
+ }
+}
diff --git a/src/string_vec.h b/src/string_vec.h
index a5f00d5..0537873 100644
--- a/src/string_vec.h
+++ b/src/string_vec.h
@@ -2,6 +2,7 @@
#define STRING_VEC_H
#include <stddef.h>
+#include <stdio.h>
struct string_vec {
size_t count;
@@ -29,4 +30,7 @@ struct string_vec string_vec_filter(
const struct string_vec *restrict vec,
const char *restrict substr);
+struct string_vec string_vec_load(FILE *file);
+void string_vec_save(struct string_vec *restrict vec, FILE *restrict file);
+
#endif /* STRING_VEC_H */