Rizin
unix-like reverse engineering framework and cli tools
cmd_history.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 RizinOrg <info@rizin.re>
2 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_util/rz_path.h>
6 #include <rz_core.h>
7 #include <rz_cmd.h>
8 #include <rz_cons.h>
9 
11  if (argc == 1) {
13  return RZ_CMD_STATUS_OK;
14  }
15 
16  int index = atoi(argv[1]);
17  if (index < 1) {
18  RZ_LOG_ERROR("index must be a positive number.\n");
19  return RZ_CMD_STATUS_ERROR;
20  }
21 
22  const char *cmd = rz_line_hist_get(index);
23  if (!cmd) {
24  RZ_LOG_ERROR("cannot find command with index %d.\n", index);
25  return RZ_CMD_STATUS_ERROR;
26  }
27 
28  int ret = rz_core_cmd0(core, cmd);
29  return !ret ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR;
30 }
31 
32 RZ_IPI RzCmdStatus rz_history_clear_handler(RzCore *core, int argc, const char **argv) {
34  return RZ_CMD_STATUS_OK;
35 }
36 
37 RZ_IPI RzCmdStatus rz_history_save_handler(RzCore *core, int argc, const char **argv) {
38  char *history = rz_path_home_history();
39  rz_line_hist_save(history);
40  free(history);
41  return RZ_CMD_STATUS_OK;
42 }
#define RZ_IPI
Definition: analysis_wasm.c:11
RZ_API int rz_core_cmd0(RzCore *core, const char *cmd)
Definition: cmd.c:5428
RZ_IPI RzCmdStatus rz_history_list_or_exec_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_history.c:10
RZ_IPI RzCmdStatus rz_history_save_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_history.c:37
RZ_IPI RzCmdStatus rz_history_clear_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_history.c:32
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
RZ_API int rz_line_hist_list(void)
Definition: dietline.c:440
RZ_API void rz_line_hist_free(void)
Definition: dietline.c:455
RZ_API const char * rz_line_hist_get(int n)
Definition: dietline.c:424
RZ_API int rz_line_hist_save(RZ_NONNULL const char *path)
Save the history of commands executed until now to file path.
Definition: dietline.c:497
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
enum rz_cmd_status_t RzCmdStatus
@ RZ_CMD_STATUS_OK
command handler exited in the right way
Definition: rz_cmd.h:24
@ RZ_CMD_STATUS_ERROR
command handler had issues while running (e.g. allocation error, etc.)
Definition: rz_cmd.h:26
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API RZ_OWN char * rz_path_home_history(void)
Return the path for the command history file.
Definition: path.c:212