From 49c7405b6a88e56bb69e12189adb719927343e07 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Mon, 15 Nov 2021 19:37:48 +0000 Subject: 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 --- src/compgen.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'src/compgen.c') 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 +#include +#include #include #include #include #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(); -- cgit v1.2.3