Implement loading fortunes from home dir

This commit is contained in:
condret 2022-06-16 08:24:17 +02:00
parent 89ce21d68a
commit 8390a2f72f
4 changed files with 48 additions and 27 deletions

View File

@ -3699,15 +3699,12 @@ R_API int r_core_config_init(RCore *core) {
free (whoami);
SETCB ("cfg.fortunes", "true", &cb_cfg_fortunes, "if enabled show tips at start");
RList *fortune_types = r_core_fortune_types ();
RStrBuf *tmp_sb = r_strbuf_new ("");
RListIter *iter;
char *fortune_type;
r_list_foreach (fortune_types, iter, fortune_type) {
r_strbuf_appendf (tmp_sb, ",%s", fortune_type);
}
char *fts = r_str_list_join(fortune_types, ",");
r_list_free (fortune_types);
SETCB ("cfg.fortunes.type", &(r_strbuf_get (tmp_sb)[1]), &cb_cfg_fortunes_type, "type of fortunes to show");
r_strbuf_free (tmp_sb);
char *fortune_desc = r_str_newf ("type of fortunes to show(%s)", fts);
SETCB ("cfg.fortunes.type", fts, &cb_cfg_fortunes_type, fortune_desc);
free (fts);
free (fortune_desc);
SETBPREF ("cfg.fortunes.clippy", "false", "use ?E instead of ?e");
SETBPREF ("cfg.fortunes.tts", "false", "speak out the fortune");
SETPREF ("cfg.prefixdump", "dump", "filename prefix for automated dumps");

View File

@ -4,11 +4,38 @@
#include <r_util.h>
static char *getFortuneFile(RCore *core, const char *type) {
char *home = r_sys_getenv (R_SYS_HOME);
if (!home) {
return NULL;
}
char *path = r_str_newf (R_JOIN_3_PATHS ("%s", R2_HOME_FORTUNES, "fortunes.%s"),
home, type);
free (home);
if (path && r_file_exists (path)) {
return path;
}
free (path);
return r_str_newf (R_JOIN_3_PATHS ("%s", R2_FORTUNES, "fortunes.%s"),
r_sys_prefix (NULL), type);
}
R_API RList *r_core_fortune_types(void) {
static bool _push_types(RList *type_list, char *fortune_dir) {
RList *files = r_sys_dir (fortune_dir);
if (!files) {
return false;
}
RListIter *iter;
char *file;
r_list_foreach (files, iter, file) {
if (r_str_startswith (file, "fortunes.") && file[9]) {
r_list_push (type_list, r_str_new (file + 9));
}
}
r_list_free (files);
return true;
}
R_IPI RList *r_core_fortune_types(void) { // R_API 5.8
RList *types = r_list_newf (free);
if (!types) {
return NULL;
@ -16,32 +43,28 @@ R_API RList *r_core_fortune_types(void) {
char *fortune_dir = r_str_newf (R_JOIN_2_PATHS ("%s", R2_FORTUNES), r_sys_prefix (NULL));
if (!fortune_dir) {
r_list_free (types);
return NULL;
}
RList *files = r_sys_dir (fortune_dir);
free (fortune_dir);
if (!files) {
if (!_push_types (types, fortune_dir)) {
free (fortune_dir);
r_list_free (types);
return NULL;
}
RListIter *iter;
char *file;
r_list_foreach (files, iter, file) {
if (r_str_startswith (file, "fortunes.") && file[9]) {
r_list_append (types, r_str_new (&file[9]));
}
free (fortune_dir);
fortune_dir = r_str_home (R2_HOME_FORTUNES);
if (fortune_dir) {
_push_types (types, fortune_dir);
free (fortune_dir);
}
r_list_free (files);
return types;
}
R_API void r_core_fortune_list_types(void) {
RList *types = r_core_fortune_types ();
while (!r_list_empty (types)) {
char *type = r_list_pop (types);
r_cons_printf ("%s\n", type);
free (type);
}
char *fts = r_str_list_join (types, "\n");
r_list_free (types);
r_cons_println (fts);
free (fts);
}
R_API void r_core_fortune_list(RCore *core) {
@ -61,7 +84,7 @@ R_API void r_core_fortune_list(RCore *core) {
free (file);
continue;
}
r_cons_printf ("%s\n", str);
r_cons_println (str);
free (str);
free (file);
}
@ -81,7 +104,7 @@ static char *getrandomline(RCore *core) {
r_list_foreach (ftypes, iter, fortunes) {
if (strstr (types, fortunes)) {
int lines = 0;
char *file = getFortuneFile(core, fortunes);
char *file = getFortuneFile (core, fortunes);
templine = r_file_slurp_random_line_count (file, &lines);
if (templine && *templine) {
free (line);

View File

@ -564,7 +564,7 @@ R_API int r_core_set_file_by_name(RBin * bin, const char *name);
R_API void r_core_debug_rr(RCore *core, RReg *reg, int mode);
/* fortune */
R_API RList *r_core_fortune_types(void);
R_IPI RList *r_core_fortune_types(void);
R_API void r_core_fortune_list_types(void);
R_API void r_core_fortune_list(RCore *core);
R_API void r_core_fortune_print_random(RCore *core);

View File

@ -79,6 +79,7 @@
#define R2_HOME_CACHEDIR R_JOIN_2_PATHS (".cache", "radare2")
#define R2_HOME_THEMES R_JOIN_2_PATHS (R2_HOME_DATADIR, "cons")
#define R2_HOME_FORTUNES R_JOIN_2_PATHS (R2_HOME_DATADIR, "fortunes")
#define R2_HOME_PLUGINS R_JOIN_2_PATHS (R2_HOME_DATADIR, "plugins")
#define R2_HOME_ZIGNS R_JOIN_2_PATHS (R2_HOME_DATADIR, "zigns")
#define R2_HOME_PDB R_JOIN_2_PATHS (R2_HOME_DATADIR, "pdb")