Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change macro syntax to be more lispy #23618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3814,6 +3814,7 @@ static char *parse_tmp_evals(RCore *core, const char *str) {
}

static bool is_macro_command(const char *ptr) {
return false;
if (!strchr (ptr, ')')) {
return false;
}
Expand Down
61 changes: 42 additions & 19 deletions libr/core/cmd_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,13 @@ R_API bool r_cmd_macro_add(RCmdMacro *mac, const char *oname) {
return false;
}

pbody = strchr (name, ';');
pbody = strchr (name, '(');
if (!pbody) {
R_LOG_ERROR ("Invalid macro body in '%s'", name);
free (name);
return false;
}
*pbody = '\0';
pbody++;
*pbody++ = '\0';

if (*name && name[1] && name[strlen (name) - 1] == ')') {
R_LOG_ERROR ("missing macro body?");
Expand All @@ -602,7 +601,7 @@ R_API bool r_cmd_macro_add(RCmdMacro *mac, const char *oname) {
ptr = strchr (name, ' ');
if (ptr) {
*ptr = '\0';
args = ptr +1;
args = ptr + 1;
}
macro_update = false;
r_list_foreach (mac->macros, iter, m) {
Expand All @@ -627,8 +626,12 @@ R_API bool r_cmd_macro_add(RCmdMacro *mac, const char *oname) {
macro->name = strdup (name);
}

macro->codelen = (pbody[0])? strlen (pbody)+2 : 4096;
macro->codelen = (pbody[0])? strlen (pbody) + 2 : 4096;
macro->code = (char *)malloc (macro->codelen);
if (!macro->code) {
free (macro->name);
return false;
}
*macro->code = '\0';
macro->nargs = 0;
if (!args) {
Expand All @@ -638,18 +641,38 @@ R_API bool r_cmd_macro_add(RCmdMacro *mac, const char *oname) {
ptr = strchr (macro->name, ' ');
if (ptr) {
*ptr = '\0';
macro->nargs = r_str_word_set0 (ptr+1);
macro->nargs = r_str_word_set0 (ptr + 1);
}

bool openMode = true;
for (lidx = 0; pbody[lidx]; lidx++) {
if (pbody[lidx] == ';') {
pbody[lidx] = '\n';
} else if (pbody[lidx] == ')' && pbody[lidx - 1] == '\n') {
pbody[lidx] = '\0';
switch (pbody[lidx]) {
case '(':
pbody[lidx] = ' ';
if (openMode) {
R_LOG_ERROR ("double (");
} else {
openMode = true;
}
break;
case ')':
if (openMode) {
openMode = false;
pbody[lidx] = '\n';
} else {
pbody[lidx] = ' ';
R_LOG_ERROR ("double )");
}
break;
default:
if (!openMode) {
R_LOG_WARN ("IGNORED %c\n", pbody[lidx]);
}
break;
}
}
strncpy (macro->code, pbody, macro->codelen);
macro->code[macro->codelen-1] = 0;
macro->code[macro->codelen - 1] = 0;
if (macro_update == false) {
r_list_append (mac->macros, macro);
}
Expand Down Expand Up @@ -687,15 +710,15 @@ static void macro_meta(RCmdMacro *mac) {
int j;
RListIter *iter;
r_list_foreach (mac->macros, iter, m) {
mac->cb_printf ("\"(%s %s; ", m->name, m->args);
mac->cb_printf ("'(%s %s (", m->name, m->args);
for (j = 0; m->code[j]; j++) {
if (m->code[j] == '\n') {
mac->cb_printf (";");
mac->cb_printf (")(");
} else {
mac->cb_printf ("%c", m->code[j]);
}
}
mac->cb_printf (")\"\n");
mac->cb_printf ("))\n");
}
}
// TODO: use mac->cb_printf which is r_cons_printf at the end
Expand Down Expand Up @@ -730,15 +753,15 @@ R_API void r_cmd_macro_list(RCmdMacro *mac, int mode) {
return;
}
r_list_foreach (mac->macros, iter, m) {
mac->cb_printf ("%d (%s %s; ", idx, m->name, m->args);
mac->cb_printf ("%d (%s %s (", idx, m->name, m->args);
for (j = 0; m->code[j]; j++) {
if (m->code[j] == '\n') {
mac->cb_printf ("; ");
mac->cb_printf (")(");
} else {
mac->cb_printf ("%c", m->code[j]);
}
}
mac->cb_printf (")\n");
mac->cb_printf ("))\n");
idx++;
}
}
Expand Down Expand Up @@ -802,9 +825,9 @@ R_API int r_cmd_macro_cmd_args(RCmdMacro *mac, const char *ptr, const char *args
for (pcmd = cmd; *pcmd && (*pcmd == ' ' || *pcmd == '\t'); pcmd++) {
;
}
//eprintf ("-pre %d\n", (int)mac->num->value);
// eprintf ("-pre %d\n", (int)mac->num->value);
int xx = (*pcmd == ')')? 0: mac->cmd (mac->user, pcmd);
//eprintf ("-pos %p %d\n", mac->num, (int)mac->num->value);
// eprintf ("-pos %p %d\n", mac->num, (int)mac->num->value);
return xx;
}

Expand Down
7 changes: 6 additions & 1 deletion libr/core/cmd_macro.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ static int cmd_macro(void *data, const char *_input) {
int i, mustcall = 0;
buf = strdup (input);

if (r_str_endswith (buf, ")()")) {
mustcall = buf + strlen (buf) - 2;
}
#if 0
for (i = 0; buf[i]; i++) {
switch (buf[i]) {
case '(':
Expand All @@ -95,10 +99,11 @@ static int cmd_macro(void *data, const char *_input) {
break;
}
}
#endif
buf[strlen (buf) - 1] = 0;
char *comma = strchr (buf, ' '); // haveargs
if (!comma) {
comma = strchr (buf, ';');
comma = strchr (buf, '(');
}
if (comma) {
r_cmd_macro_add (&core->rcmd->macro, buf);
Expand Down
Loading