Rizin
unix-like reverse engineering framework and cli tools
cmd_shell.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Dhruv Maroo <dhruvmaru007@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_core.h>
5 
6 // env
7 RZ_IPI RzCmdStatus rz_cmd_shell_env_handler(RzCore *core, int argc, const char **argv) {
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 }
32 
33 // exit
34 RZ_IPI RzCmdStatus rz_cmd_shell_exit_handler(RzCore *core, int argc, const char **argv) {
35  core->num->value = 0LL;
36  return RZ_CMD_STATUS_EXIT;
37 }
38 
39 // ls
40 RZ_IPI RzCmdStatus rz_cmd_shell_ls_handler(RzCore *core, int argc, const char **argv) {
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 }
51 
52 // rm
53 RZ_IPI RzCmdStatus rz_cmd_shell_rm_handler(RzCore *core, int argc, const char **argv) {
55 }
56 
57 // sleep
58 RZ_IPI RzCmdStatus rz_cmd_shell_sleep_handler(RzCore *core, int argc, const char **argv) {
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 }
64 
65 // uniq
66 RZ_IPI RzCmdStatus rz_cmd_shell_uniq_handler(RzCore *core, int argc, const char **argv) {
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 }
75 
76 // uname
77 RZ_IPI RzCmdStatus rz_cmd_shell_uname_handler(RzCore *core, int argc, const char **argv) {
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 }
90 
91 // echo
92 RZ_IPI RzCmdStatus rz_cmd_shell_echo_handler(RzCore *core, int argc, const char **argv) {
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 }
101 
102 // cp
103 RZ_IPI RzCmdStatus rz_cmd_shell_cp_handler(RzCore *core, int argc, const char **argv) {
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 }
110 
111 // cd
112 RZ_IPI RzCmdStatus rz_cmd_shell_cd_handler(RzCore *core, int argc, const char **argv) {
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 }
147 
148 // cat
149 RZ_IPI RzCmdStatus rz_cmd_shell_cat_handler(RzCore *core, int argc, const char **argv) {
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 }
167 
168 // mv
169 RZ_IPI RzCmdStatus rz_cmd_shell_mv_handler(RzCore *core, int argc, const char **argv) {
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 }
175 
176 // mkdir
177 RZ_IPI RzCmdStatus rz_cmd_shell_mkdir_handler(RzCore *core, int argc, const char **argv) {
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 }
191 
192 // pwd
193 RZ_IPI RzCmdStatus rz_cmd_shell_pwd_handler(RzCore *core, int argc, const char **argv) {
194  char *cwd = rz_sys_getdir();
195  if (cwd) {
197  free(cwd);
198  }
199  return RZ_CMD_STATUS_OK;
200 }
201 
202 // sort
203 RZ_IPI RzCmdStatus rz_cmd_shell_sort_handler(RzCore *core, int argc, const char **argv) {
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 }
211 
212 // clear
213 // cls
214 RZ_IPI RzCmdStatus rz_cmd_shell_clear_handler(RzCore *core, int argc, const char **argv) {
215  rz_cons_clear00();
216  return RZ_CMD_STATUS_OK;
217 }
218 
219 // which
220 RZ_IPI RzCmdStatus rz_cmd_shell_which_handler(RzCore *core, int argc, const char **argv) {
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 }
230 
231 // fortune
234  return RZ_CMD_STATUS_OK;
235 }
si
#define e(frag)
#define RZ_IPI
Definition: analysis_wasm.c:11
lzma_index ** i
Definition: index.h:629
RZ_API char * rz_cmd_alias_get(RzCmd *cmd, const char *k, int remote)
Definition: cmd_api.c:501
RZ_IPI RzCmdStatus rz_cmd_shell_mv_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:169
RZ_IPI RzCmdStatus rz_cmd_shell_uniq_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:66
RZ_IPI RzCmdStatus rz_cmd_shell_cat_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:149
RZ_IPI RzCmdStatus rz_cmd_shell_cd_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:112
RZ_IPI RzCmdStatus rz_cmd_shell_rm_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:53
RZ_IPI RzCmdStatus rz_cmd_shell_env_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:7
RZ_IPI RzCmdStatus rz_cmd_shell_exit_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:34
RZ_IPI RzCmdStatus rz_cmd_shell_echo_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:92
RZ_IPI RzCmdStatus rz_cmd_shell_which_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:220
RZ_IPI RzCmdStatus rz_cmd_shell_sleep_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:58
RZ_IPI RzCmdStatus rz_cmd_shell_pwd_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:193
RZ_IPI RzCmdStatus rz_cmd_shell_uname_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:77
RZ_IPI RzCmdStatus rz_cmd_shell_ls_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:40
RZ_IPI RzCmdStatus rz_cmd_shell_cp_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:103
RZ_IPI RzCmdStatus rz_cmd_shell_fortune_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:232
RZ_IPI RzCmdStatus rz_cmd_shell_sort_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:203
RZ_IPI RzCmdStatus rz_cmd_shell_clear_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:214
RZ_IPI RzCmdStatus rz_cmd_shell_mkdir_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_shell.c:177
RZ_API void rz_cons_clear00(void)
Definition: cons.c:778
RZ_API void rz_cons_newline(void)
Definition: cons.c:1274
RZ_API void * rz_cons_sleep_begin(void)
Definition: cons.c:443
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
RZ_API void rz_cons_sleep_end(void *user)
Definition: cons.c:450
#define NULL
Definition: cris-opc.c:27
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 rz_core_fortune_print_random(RzCore *core)
Definition: fortune.c:60
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
void * p
Definition: libc.cpp:67
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_WRONG_ARGS
command handler could not handle the arguments passed to it
Definition: rz_cmd.h:25
@ RZ_CMD_STATUS_ERROR
command handler had issues while running (e.g. allocation error, etc.)
Definition: rz_cmd.h:26
@ RZ_CMD_STATUS_EXIT
command handler asks to exit the prompt loop
Definition: rz_cmd.h:29
RZ_API bool rz_file_copy(const char *src, const char *dst)
Definition: file.c:1179
RZ_API bool rz_file_rm(const char *file)
Definition: file.c:865
RZ_API char * rz_file_path(const char *bin)
Definition: file.c:354
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_str_array_join(const char **a, size_t n, const char *sep)
Definition: str.c:3861
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
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 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 RZ_OWN char * rz_syscmd_cat(RZ_NONNULL const char *file)
Definition: syscmd.c:442
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 RZ_OWN char * rz_syscmd_sort(RZ_NONNULL const char *file)
Definition: syscmd.c:254
RZ_API RZ_OWN char * rz_syscmd_mkdir(RZ_NONNULL const char *dir)
Definition: syscmd.c:468
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 int rz_sys_system(const char *command)
Definition: sys.c:1658
RZ_API void rz_sys_info_free(RSysInfo *si)
Definition: sys.c:1251
RZ_API RSysInfo * rz_sys_info(void)
Definition: sys.c:1175
RZ_API char * rz_sys_getdir(void)
Get current working directory.
Definition: sys.c:521
RZ_API int rz_sys_sleep(int secs)
Sleep for secs seconds.
Definition: sys.c:300
RZ_API RZ_OWN char * rz_syscmd_uniq(RZ_NONNULL const char *file)
Definition: syscmd.c:334
RZ_API char ** rz_sys_get_environ(void)
Definition: sys.c:1115
RZ_API RZ_OWN char * rz_syscmd_ls(RZ_NONNULL const char *input)
Definition: syscmd.c:121
RzCmd * rcmd
Definition: rz_core.h:319
RzNum * num
Definition: rz_core.h:316
ut64 value
Definition: rz_num.h:63
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
diff_output_t output
Definition: zipcmp.c:237