Rizin
unix-like reverse engineering framework and cli tools
cmd_shell.c File Reference
#include <rz_core.h>

Go to the source code of this file.

Functions

RZ_IPI RzCmdStatus rz_cmd_shell_env_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_exit_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_ls_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_rm_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_sleep_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_uniq_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_uname_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_echo_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_cp_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_cd_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_cat_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_mv_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_mkdir_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_pwd_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_sort_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_clear_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_which_handler (RzCore *core, int argc, const char **argv)
 
RZ_IPI RzCmdStatus rz_cmd_shell_fortune_handler (RzCore *core, int argc, const char **argv)
 

Function Documentation

◆ rz_cmd_shell_cat_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_cat_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 149 of file cmd_shell.c.

149  {
150  const char *path = argv[1];
151  if (*path == '$') {
152  const char *oldText = rz_cmd_alias_get(core->rcmd, path, 1);
153  if (oldText) {
154  rz_cons_printf("%s\n", oldText + 1);
155  } else {
156  RZ_LOG_ERROR("Invalid alias\n");
157  }
158  return oldText ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR;
159  }
160  char *res = rz_syscmd_cat(path);
161  if (res) {
162  rz_cons_print(res);
163  free(res);
164  }
165  return RZ_CMD_STATUS_OK;
166 }
RZ_API char * rz_cmd_alias_get(RzCmd *cmd, const char *k, int remote)
Definition: cmd_api.c:501
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
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
@ 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_syscmd_cat(RZ_NONNULL const char *file)
Definition: syscmd.c:442
RzCmd * rcmd
Definition: rz_core.h:319

References argv, free(), path, rz_core_t::rcmd, rz_cmd_alias_get(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, rz_cons_printf(), RZ_LOG_ERROR, and rz_syscmd_cat().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_cd_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_cd_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 112 of file cmd_shell.c.

112  {
113  static char *olddir = NULL;
114  bool ret = true;
115  const char *dir = "~";
116  if (argc > 1) {
117  dir = argv[1];
118  }
119  if (!strcmp(dir, "-")) {
120  if (olddir) {
121  char *newdir = olddir;
122  olddir = rz_sys_getdir();
123  if (!rz_sys_chdir(newdir)) {
124  RZ_LOG_ERROR("Cannot chdir to %s\n", newdir);
125  free(olddir);
126  olddir = newdir;
127  } else {
128  free(newdir);
129  ret = true;
130  }
131  } else {
132  RZ_LOG_ERROR("No old directory found\n");
133  }
134  } else {
135  char *cwd = rz_sys_getdir();
136  if (!rz_sys_chdir(dir)) {
137  RZ_LOG_ERROR("Cannot chdir to %s\n", dir);
138  free(cwd);
139  } else {
140  free(olddir);
141  olddir = cwd;
142  ret = true;
143  }
144  }
145  return ret ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR;
146 }
#define NULL
Definition: cris-opc.c:27
RZ_API bool rz_sys_chdir(RZ_NONNULL const char *s)
Change current directory to s, taking care of home expansion ~.
Definition: sys.c:532
RZ_API char * rz_sys_getdir(void)
Get current working directory.
Definition: sys.c:521

References argv, test-lz4-speed::cwd, free(), NULL, RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, RZ_LOG_ERROR, rz_sys_chdir(), and rz_sys_getdir().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_clear_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_clear_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 214 of file cmd_shell.c.

214  {
215  rz_cons_clear00();
216  return RZ_CMD_STATUS_OK;
217 }
RZ_API void rz_cons_clear00(void)
Definition: cons.c:778

References RZ_CMD_STATUS_OK, and rz_cons_clear00().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_cp_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_cp_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 103 of file cmd_shell.c.

103  {
104  bool rc = rz_file_copy(argv[1], argv[2]);
105  if (!rc) {
106  RZ_LOG_ERROR("Failed to copy %s to %s\n", argv[1], argv[2]);
107  }
109 }
RZ_API bool rz_file_copy(const char *src, const char *dst)
Definition: file.c:1179

References argv, RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, rz_file_copy(), and RZ_LOG_ERROR.

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_echo_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_echo_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 92 of file cmd_shell.c.

92  {
93  if (argc >= 2) {
94  char *output = rz_str_array_join(argv + 1, argc - 1, " ");
96  free(output);
97  return RZ_CMD_STATUS_OK;
98  }
99  return RZ_CMD_STATUS_ERROR;
100 }
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
RZ_API char * rz_str_array_join(const char **a, size_t n, const char *sep)
Definition: str.c:3861
diff_output_t output
Definition: zipcmp.c:237

References argv, free(), output, RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, rz_cons_println(), and rz_str_array_join().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_env_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_env_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 7 of file cmd_shell.c.

7  {
8  char *p, **e;
9  switch (argc) {
10  case 1:
12  while (!RZ_STR_ISEMPTY(e)) {
14  e++;
15  }
16  return RZ_CMD_STATUS_OK;
17  case 2:
18  p = rz_sys_getenv(argv[1]);
19  if (!p) {
20  return RZ_CMD_STATUS_ERROR;
21  }
23  free(p);
24  return RZ_CMD_STATUS_OK;
25  case 3:
26  rz_sys_setenv(argv[1], argv[2]);
27  return RZ_CMD_STATUS_OK;
28  default:
30  }
31 }
#define e(frag)
void * p
Definition: libc.cpp:67
@ RZ_CMD_STATUS_WRONG_ARGS
command handler could not handle the arguments passed to it
Definition: rz_cmd.h:25
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
RZ_API int rz_sys_setenv(const char *key, const char *value)
Set an environment variable in the calling process.
Definition: sys.c:405
RZ_API char ** rz_sys_get_environ(void)
Definition: sys.c:1115

References argv, e, free(), p, RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, RZ_CMD_STATUS_WRONG_ARGS, rz_cons_println(), RZ_STR_ISEMPTY, rz_sys_get_environ(), rz_sys_getenv(), and rz_sys_setenv().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_exit_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_exit_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 34 of file cmd_shell.c.

34  {
35  core->num->value = 0LL;
36  return RZ_CMD_STATUS_EXIT;
37 }
@ RZ_CMD_STATUS_EXIT
command handler asks to exit the prompt loop
Definition: rz_cmd.h:29
RzNum * num
Definition: rz_core.h:316
ut64 value
Definition: rz_num.h:63

References rz_core_t::num, RZ_CMD_STATUS_EXIT, and rz_num_t::value.

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_fortune_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_fortune_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 232 of file cmd_shell.c.

232  {
234  return RZ_CMD_STATUS_OK;
235 }
RZ_API void rz_core_fortune_print_random(RzCore *core)
Definition: fortune.c:60

References RZ_CMD_STATUS_OK, and rz_core_fortune_print_random().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_ls_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_ls_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 40 of file cmd_shell.c.

40  {
41  char *arg = rz_str_array_join(argv + 1, argc - 1, " ");
42  char *res = rz_syscmd_ls(arg);
43  free(arg);
44  if (!res) {
45  return RZ_CMD_STATUS_ERROR;
46  }
47  rz_cons_print(res);
48  free(res);
49  return RZ_CMD_STATUS_OK;
50 }
RZ_API RZ_OWN char * rz_syscmd_ls(RZ_NONNULL const char *input)
Definition: syscmd.c:121

References argv, free(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, rz_str_array_join(), and rz_syscmd_ls().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_mkdir_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_mkdir_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 177 of file cmd_shell.c.

177  {
179  for (int i = 1; i < argc; i++) {
180  rz_strbuf_appendf(buf, " %s", argv[i]);
181  }
182  char *input = rz_strbuf_drain(buf);
183  char *res = rz_syscmd_mkdir(input);
184  free(input);
185  if (res) {
186  rz_cons_print(res);
187  free(res);
188  }
189  return RZ_CMD_STATUS_OK;
190 }
lzma_index ** i
Definition: index.h:629
voidpf void * buf
Definition: ioapi.h:138
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API RZ_OWN char * rz_syscmd_mkdir(RZ_NONNULL const char *dir)
Definition: syscmd.c:468
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)

References argv, free(), i, input(), NULL, RZ_CMD_STATUS_OK, rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), and rz_syscmd_mkdir().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_mv_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_mv_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 169 of file cmd_shell.c.

169  {
170  char *input = rz_str_newf("mv %s %s", argv[1], argv[2]);
171  int ec = rz_sys_system(input);
172  free(input);
173  return ec == 0 ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR;
174 }
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API int rz_sys_system(const char *command)
Definition: sys.c:1658

References argv, free(), input(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, rz_str_newf(), and rz_sys_system().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_pwd_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_pwd_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 193 of file cmd_shell.c.

193  {
194  char *cwd = rz_sys_getdir();
195  if (cwd) {
197  free(cwd);
198  }
199  return RZ_CMD_STATUS_OK;
200 }

References test-lz4-speed::cwd, free(), RZ_CMD_STATUS_OK, rz_cons_println(), and rz_sys_getdir().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_rm_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_rm_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 53 of file cmd_shell.c.

53  {
55 }
RZ_API bool rz_file_rm(const char *file)
Definition: file.c:865

References argv, RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, and rz_file_rm().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_sleep_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_sleep_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 58 of file cmd_shell.c.

58  {
59  void *bed = rz_cons_sleep_begin();
60  rz_sys_sleep(atoi(argv[1] + 1));
61  rz_cons_sleep_end(bed);
62  return RZ_CMD_STATUS_OK;
63 }
RZ_API void * rz_cons_sleep_begin(void)
Definition: cons.c:443
RZ_API void rz_cons_sleep_end(void *user)
Definition: cons.c:450
RZ_API int rz_sys_sleep(int secs)
Sleep for secs seconds.
Definition: sys.c:300

References argv, RZ_CMD_STATUS_OK, rz_cons_sleep_begin(), rz_cons_sleep_end(), and rz_sys_sleep().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_sort_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_sort_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 203 of file cmd_shell.c.

203  {
204  char *res = rz_syscmd_sort(argv[1]);
205  if (res) {
206  rz_cons_print(res);
207  free(res);
208  }
209  return RZ_CMD_STATUS_OK;
210 }
RZ_API RZ_OWN char * rz_syscmd_sort(RZ_NONNULL const char *file)
Definition: syscmd.c:254

References argv, free(), RZ_CMD_STATUS_OK, and rz_syscmd_sort().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_uname_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_uname_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 77 of file cmd_shell.c.

77  {
78  RSysInfo *si = rz_sys_info();
79  if (!si) {
80  return RZ_CMD_STATUS_ERROR;
81  }
82  rz_cons_printf("%s", si->sysname);
83  if (argc > 1 && strcmp(argv[1], "-r") == 0) {
84  rz_cons_printf(" %s", si->release);
85  }
88  return RZ_CMD_STATUS_OK;
89 }
si
RZ_API void rz_cons_newline(void)
Definition: cons.c:1274
RZ_API void rz_sys_info_free(RSysInfo *si)
Definition: sys.c:1251
RZ_API RSysInfo * rz_sys_info(void)
Definition: sys.c:1175

References argv, RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, rz_cons_newline(), rz_cons_printf(), rz_sys_info(), rz_sys_info_free(), and si.

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_uniq_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_uniq_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 66 of file cmd_shell.c.

66  {
67  char *res = rz_syscmd_uniq(argv[1]);
68  if (!res) {
69  return RZ_CMD_STATUS_ERROR;
70  }
71  rz_cons_print(res);
72  free(res);
73  return RZ_CMD_STATUS_OK;
74 }
RZ_API RZ_OWN char * rz_syscmd_uniq(RZ_NONNULL const char *file)
Definition: syscmd.c:334

References argv, free(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, and rz_syscmd_uniq().

Referenced by rzshell_cmddescs_init().

◆ rz_cmd_shell_which_handler()

RZ_IPI RzCmdStatus rz_cmd_shell_which_handler ( RzCore core,
int  argc,
const char **  argv 
)

Definition at line 220 of file cmd_shell.c.

220  {
221  char *solved = rz_file_path(argv[1]);
222  if (!solved) {
223  RZ_LOG_ERROR("Could not get the full path of '%s'\n", argv[1]);
224  return RZ_CMD_STATUS_ERROR;
225  }
226  rz_cons_println(solved);
227  free(solved);
228  return RZ_CMD_STATUS_OK;
229 }
RZ_API char * rz_file_path(const char *bin)
Definition: file.c:354

References argv, free(), RZ_CMD_STATUS_ERROR, RZ_CMD_STATUS_OK, rz_cons_println(), rz_file_path(), and RZ_LOG_ERROR.

Referenced by rzshell_cmddescs_init().