radare2/libr/core/fortune.c
pancake c40ddd3552 Several improvements in the Visual mode (help, tabs, ...) ##visual
- Remove the V@ command - not intuitive and a bit alien
- rotating print modes with tab was segfault because of UB
- Implement recursive interactive help
- Support ranged cursor selections (for pxd, pxb, pxq, ...)
- 'i' in pxb visual runs Vd1 to edit the bits under the cursor
- V' is no longer mixed with Vt
- Properly restore the print sub-modes in tabs
- Smart tab key hints (beter located and shorter)
2019-01-02 02:09:31 +01:00

80 lines
1.9 KiB
C

/* radare2 - LGPL - Copyright 2009-2019 - pancake */
#include <r_core.h>
static const char *fortunes[] = {
"tips", "fun", "nsfw", "creepy"
};
static char *getFortuneFile(RCore *core, const char *type) {
return r_str_newf (R_JOIN_3_PATHS ("%s", R2_FORTUNES, "fortunes.%s"),
r_sys_prefix (NULL), type);
}
R_API void r_core_fortune_list_types(void) {
int i;
for (i = 0; i < R_ARRAY_SIZE (fortunes); i++) {
r_cons_printf ("%s\n", fortunes[i]);
}
}
R_API void r_core_fortune_list(RCore *core) {
// TODO: use file.fortunes // can be dangerous in sandbox mode
const char *types = (char *)r_config_get (core->config, "cfg.fortunes.type");
int i, j;
for (i = 0; i < R_ARRAY_SIZE (fortunes); i++) {
if (strstr (types, fortunes[i])) {
char *file = getFortuneFile(core, fortunes[i]);
char *str = r_file_slurp (file, NULL);
for (j = 0; str[j]; j++) {
if (str[j] == '\n') {
if (i < j) {
str[j] = '\0';
r_cons_printf ("%s\n", str + i);
}
i = j + 1;
}
}
free (str);
free (file);
}
}
}
static char *getrandomline(RCore *core) {
int i, lines = 0;
const char *types = (char *)r_config_get (core->config, "cfg.fortunes.type");
char *line = NULL, *templine;
for (i = 0; i < R_ARRAY_SIZE (fortunes); i++) {
if (strstr (types, fortunes[i])) {
char *file = getFortuneFile(core, fortunes[i]);
templine = r_file_slurp_random_line_count (file, &lines);
if (templine && *templine) {
free (line);
line = templine;
}
free (file);
}
}
return line;
}
R_API void r_core_fortune_print_random(RCore *core) {
// TODO: use file.fortunes // can be dangerous in sandbox mode
char *line = getrandomline (core);
if (!line) {
line = getrandomline (core);
}
if (line) {
if (r_config_get_i (core->config, "cfg.fortunes.clippy")) {
r_core_clippy (line);
} else {
r_cons_printf (" -- %s\n", line);
}
if (r_config_get_i (core->config, "cfg.fortunes.tts")) {
r_sys_tts (line, true);
}
free (line);
}
}