summaryrefslogtreecommitdiff
path: root/src/compgen.c
diff options
context:
space:
mode:
authorPhil Jones <philj56@gmail.com>2021-11-15 19:37:48 +0000
committerPhil Jones <philj56@gmail.com>2021-11-15 19:37:48 +0000
commit49c7405b6a88e56bb69e12189adb719927343e07 (patch)
tree72c7691e746563cc1396c5b555659c813572c12e /src/compgen.c
parent108df42e561b7e81ba09a8c278a562129e651bb6 (diff)
Multiple smaller changes.
- Remove the background image and libpng dependency - Add a prompt - Add xmalloc with out-of-memory handling - Add beginnings of a rofi-like run cache
Diffstat (limited to 'src/compgen.c')
-rw-r--r--src/compgen.c57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/compgen.c b/src/compgen.c
index e04e0a5..8190a21 100644
--- a/src/compgen.c
+++ b/src/compgen.c
@@ -1,16 +1,71 @@
#include <dirent.h>
+#include <stdbool.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "log.h"
#include "string_vec.h"
+#include "xmalloc.h"
+
+static const char *default_state_dir = ".local/state";
+static const char *histfile_basename = "tofi-history";
+
+static char *get_histfile_path() {
+ char *histfile_name;
+ const char *state_path = getenv("XDG_STATE_HOME");
+ if (state_path == NULL) {
+ const char *home = getenv("HOME");
+ if (home == NULL) {
+ log_error("Couldn't retrieve HOME from environment.\n");
+ exit(EXIT_FAILURE);
+ }
+ size_t len = strlen(home) + 1
+ + strlen(default_state_dir) + 1
+ + strlen(histfile_basename) + 1;
+ histfile_name = xmalloc(len);
+ snprintf(
+ histfile_name,
+ len,
+ "%s/%s/%s",
+ home,
+ default_state_dir,
+ histfile_basename);
+ } else {
+ size_t len = strlen(state_path) + 1
+ + strlen(histfile_basename) + 1;
+ histfile_name = xmalloc(len);
+ snprintf(
+ histfile_name,
+ len,
+ "%s/%s",
+ state_path,
+ histfile_basename);
+ }
+ return histfile_name;
+
+}
+
+static void load_history()
+{
+ char *name = get_histfile_path();
+ FILE *histfile = fopen(name, "rb");
+
+ free(name);
+
+ if (histfile == NULL) {
+ return;
+ }
+
+}
struct string_vec compgen()
{
+ load_history();
log_debug("Retrieving PATH.\n");
const char *env_path = getenv("PATH");
if (env_path == NULL) {
- log_error("Couldn't retrieve PATH from environment.");
+ log_error("Couldn't retrieve PATH from environment.\n");
exit(EXIT_FAILURE);
}
struct string_vec programs = string_vec_create();