Skip to content

Commit f40155e

Browse files
authored
Some minor code simplifications
1 parent 94b350f commit f40155e

File tree

2 files changed

+11
-39
lines changed

2 files changed

+11
-39
lines changed

src/r2api.inc.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* r2mcp - MIT - Copyright 2025 - pancake, dnakov */
1+
/* r2mcp - MIT - Copyright 2025-2026 - pancake, dnakov */
22

33
static void r2state_settings(RCore *core) {
44
r_config_set_i (core->config, "scr.color", 0);
@@ -46,23 +46,19 @@ static void r2mcp_log_reset(ServerState *ss) {
4646

4747
static char *r2mcp_log_drain(ServerState *ss) {
4848
char *s = r_strbuf_drain (ss->sb);
49-
if (R_STR_ISNOTEMPTY (s)) {
50-
ss->sb = NULL;
51-
return s;
49+
if (R_STR_ISEMPTY (s)) {
50+
R_FREE (s);
5251
}
53-
free (s);
5452
ss->sb = NULL;
55-
return NULL;
53+
return s;
5654
}
5755

5856
static inline void r2mcp_log(ServerState *ss, const char *x) {
5957
R_LOG_INFO ("[R2MCP] %s", x);
60-
#if R2MCP_DEBUG
6158
if (ss && ss->logfile && *ss->logfile) {
6259
r_file_dump (ss->logfile, (const ut8 *) (x), -1, true);
6360
r_file_dump (ss->logfile, (const ut8 *)"\n", -1, true);
6461
}
65-
#endif
6662
}
6763

6864
static char *r2_cmd_filter(const char *cmd, bool *changed) {
@@ -138,7 +134,7 @@ static bool path_contains_parent_ref(const char *p) {
138134
}
139135

140136
static bool path_is_within_sandbox(const char *p, const char *sb) {
141-
if (!sb || !*sb) {
137+
if (R_STR_ISEMPTY (sb)) {
142138
return true;
143139
}
144140
size_t plen = strlen (p);
@@ -208,7 +204,7 @@ R_IPI bool r2_open_file(ServerState *ss, const char *filepath) {
208204
ss->rstate.current_file = NULL;
209205
}
210206

211-
bool is_frida = strstr (filepath, "frida://") != NULL;
207+
const bool is_frida = strstr (filepath, "frida://") != NULL;
212208
if (is_frida) {
213209
ss->frida_mode = true;
214210
} else {
@@ -222,20 +218,18 @@ R_IPI bool r2_open_file(ServerState *ss, const char *filepath) {
222218
R_LOG_INFO ("Running r2 command: %s", cmd);
223219
char *result = r_core_cmd_str (core, cmd);
224220
free (cmd);
225-
bool success = (result && strlen (result) > 0);
221+
bool success = R_STR_ISNOTEMPTY (result);
226222
free (result);
227223

228224
if (!success) {
229225
R_LOG_INFO ("Trying alternative method to open file");
230226
RIODesc *fd = r_core_file_open (core, filepath, R_PERM_R, 0);
231-
if (fd) {
232-
r_core_bin_load (core, filepath, 0);
233-
R_LOG_INFO ("File opened using r_core_file_open");
234-
success = true;
235-
} else {
227+
if (!fd) {
236228
R_LOG_ERROR ("Failed to open file: %s", filepath);
237229
return false;
238230
}
231+
r_core_bin_load (core, filepath, 0);
232+
R_LOG_INFO ("File opened using r_core_file_open");
239233
}
240234
free (ss->rstate.current_file);
241235
ss->rstate.current_file = strdup (filepath);

src/r2mcp.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
/* r2mcp - MIT - Copyright 2025-2026 - pancake, dnakov */
22

3+
#include <signal.h>
34
#include <r_core.h>
4-
#include <r_util/r_json.h>
5-
#include <r_util/pj.h>
6-
#include <r_util.h>
7-
#include <r_util/r_print.h>
8-
95
#include "config.h"
106
#include "jsonrpc.h"
11-
12-
#if defined(R2__UNIX__)
13-
#include <unistd.h>
14-
#include <fcntl.h>
15-
#include <sys/wait.h>
16-
#include <signal.h>
17-
#include <errno.h>
18-
#include <string.h>
19-
#elif defined(R2__WINDOWS__)
20-
#include <windows.h>
21-
#include <io.h>
22-
#include <fcntl.h>
23-
#include <signal.h>
24-
#else
25-
#error please define R2__WINDOWS__ or R2__UNIX__ for platform detection
26-
#endif
277
#include "r2mcp.h"
288
#include "tools.h"
299
#include "prompts.h"
3010

31-
#define R2MCP_DEBUG 1
32-
3311
#ifndef R2MCP_VERSION
3412
#warning R2MCP_VERSION is not defined
3513
#define R2MCP_VERSION "1.5.6"

0 commit comments

Comments
 (0)