Rizin
unix-like reverse engineering framework and cli tools
cmd_tasks.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2020 ret2libc <sirmy15@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_cmd.h>
5 #include <rz_core.h>
6 #include <cmd_descs.h>
7 
8 static int task_enqueue(RzCore *core, const char *cmd, bool transient) {
10  if (!task) {
11  return -1;
12  }
13  task->transient = transient;
14  rz_core_task_enqueue(&core->tasks, task);
15  return 0;
16 }
17 
18 static int task_output(RzCore *core, int tid) {
19  if (!tid) {
20  return -1;
21  }
22  RzCoreTask *task = rz_core_task_get_incref(&core->tasks, tid);
23  if (task) {
24  const char *res = rz_core_cmd_task_get_result(task);
25  if (res) {
26  rz_cons_println(res);
27  }
28  rz_core_task_decref(task);
29  } else {
30  eprintf("Cannot find task\n");
31  return -1;
32  }
33  return 0;
34 }
35 
36 static int task_break(RzCore *core, int tid) {
37  if (!tid) {
38  return -1;
39  }
40  if (!rz_core_task_is_cmd(core, tid)) {
41  return -1;
42  }
43  rz_core_task_break(&core->tasks, tid);
44  return 0;
45 }
46 
47 RZ_IPI RzCmdStatus rz_tasks_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode) {
48  if (argc == 1) {
49  rz_core_task_list(core, mode == RZ_OUTPUT_MODE_STANDARD ? '\0' : 'j');
50  return RZ_CMD_STATUS_OK;
51  } else if (argc == 2) {
52  return rz_cmd_int2status(task_enqueue(core, argv[1], false));
53  }
54  return RZ_CMD_STATUS_ERROR;
55 }
56 
57 RZ_IPI RzCmdStatus rz_tasks_transient_handler(RzCore *core, int argc, const char **argv) {
58  return rz_cmd_int2status(task_enqueue(core, argv[1], true));
59 }
60 
61 RZ_IPI RzCmdStatus rz_tasks_output_handler(RzCore *core, int argc, const char **argv) {
62  int tid = rz_num_math(core->num, argv[1]);
63  return rz_cmd_int2status(task_output(core, tid));
64 }
65 
66 RZ_IPI RzCmdStatus rz_tasks_break_handler(RzCore *core, int argc, const char **argv) {
67  int tid = rz_num_math(core->num, argv[1]);
68  return rz_cmd_int2status(task_break(core, tid));
69 }
70 
71 RZ_IPI RzCmdStatus rz_tasks_delete_handler(RzCore *core, int argc, const char **argv) {
72  int tid = rz_num_math(core->num, argv[1]);
73  if (!rz_core_task_is_cmd(core, tid)) {
74  return RZ_CMD_STATUS_ERROR;
75  }
77 }
78 
79 RZ_IPI RzCmdStatus rz_tasks_delete_all_handler(RzCore *core, int argc, const char **argv) {
81  return RZ_CMD_STATUS_OK;
82 }
83 
84 RZ_IPI RzCmdStatus rz_tasks_wait_handler(RzCore *core, int argc, const char **argv) {
85  int tid = 0;
86  if (argc == 2) {
87  tid = rz_num_math(core->num, argv[1]);
88  }
89  if (!rz_core_task_is_cmd(core, tid)) {
90  return RZ_CMD_STATUS_ERROR;
91  }
92  rz_core_task_join(&core->tasks, core->tasks.current_task, tid ? tid : -1);
93  return RZ_CMD_STATUS_OK;
94 }
#define RZ_IPI
Definition: analysis_wasm.c:11
static int task_break(RzCore *core, int tid)
Definition: cmd_tasks.c:36
RZ_IPI RzCmdStatus rz_tasks_delete_all_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_tasks.c:79
RZ_IPI RzCmdStatus rz_tasks_output_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_tasks.c:61
RZ_IPI RzCmdStatus rz_tasks_handler(RzCore *core, int argc, const char **argv, RzOutputMode mode)
Definition: cmd_tasks.c:47
RZ_IPI RzCmdStatus rz_tasks_break_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_tasks.c:66
RZ_IPI RzCmdStatus rz_tasks_delete_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_tasks.c:71
static int task_output(RzCore *core, int tid)
Definition: cmd_tasks.c:18
static int task_enqueue(RzCore *core, const char *cmd, bool transient)
Definition: cmd_tasks.c:8
RZ_IPI RzCmdStatus rz_tasks_transient_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_tasks.c:57
RZ_IPI RzCmdStatus rz_tasks_wait_handler(RzCore *core, int argc, const char **argv)
Definition: cmd_tasks.c:84
RZ_API void rz_cons_println(const char *str)
Definition: cons.c:233
#define NULL
Definition: cris-opc.c:27
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
const char int mode
Definition: ioapi.h:137
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define eprintf(x, y...)
Definition: rlcc.c:7
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
struct rz_core_task_t RzCoreTask
Definition: rz_core.h:254
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
RzOutputMode
Enum to describe the way data are printed.
Definition: rz_types.h:38
@ RZ_OUTPUT_MODE_STANDARD
Definition: rz_types.h:39
RzNum * num
Definition: rz_core.h:316
RzCoreTaskScheduler tasks
Definition: rz_core.h:362
struct rz_core_task_t * current_task
Definition: rz_core.h:276
RZ_API bool rz_core_task_is_cmd(RzCore *core, int id)
Definition: task.c:834
RZ_API void rz_core_task_join(RzCoreTaskScheduler *scheduler, RzCoreTask *current, int id)
Definition: task.c:96
RZ_API void rz_core_task_decref(RzCoreTask *task)
Definition: task.c:200
RZ_API int rz_core_task_del(RzCoreTaskScheduler *scheduler, int id)
Definition: task.c:508
RZ_API void rz_core_task_del_all_done(RzCore *core)
Definition: task.c:844
RZ_API void rz_core_task_enqueue(RzCoreTaskScheduler *scheduler, RzCoreTask *task)
Definition: task.c:374
RZ_API RzCoreTask * rz_core_task_get_incref(RzCoreTaskScheduler *scheduler, int id)
Definition: task.c:461
RZ_API RzCoreTask * rz_core_cmd_task_new(RzCore *core, const char *cmd, RzCoreCmdTaskFinished finished_cb, void *finished_cb_user)
Definition: task.c:629
RZ_API void rz_core_task_list(RzCore *core, int mode)
Definition: task.c:811
RZ_API const char * rz_core_cmd_task_get_result(RzCoreTask *task)
Definition: task.c:646
RZ_API void rz_core_task_break(RzCoreTaskScheduler *scheduler, int id)
Definition: task.c:483