Skip to content

Commit c13bd51

Browse files
author
Your Name
committed
Added support for macro parameters
1 parent f5ab6d9 commit c13bd51

4 files changed

Lines changed: 43 additions & 8 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frida-cshell",
3-
"version": "1.6.2",
3+
"version": "1.6.3",
44
"description": "Frida's CShell",
55
"scripts": {
66
"prepare": "npm run build && npm run version && npm run package && npm run copy",

src/cmdlets/misc/macro.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Var } from '../../vars/var.js';
99
import { Vars } from '../../vars/vars.js';
1010

1111
export class MacroCmdLet extends CmdLet implements InputInterceptLine {
12+
private static readonly PARAM_PREFIX: string = '$';
1213
name = 'm';
1314
category = 'misc';
1415
help = 'manage macros';
@@ -110,7 +111,7 @@ m name ${CmdLet.DELETE_CHAR} - delete a macro
110111
return Var.ZERO;
111112
}
112113

113-
public static runSync(macro: Macro): Var {
114+
public static runSync(macro: Macro, tokens: Token[]): Var {
114115
let ret = Var.ZERO;
115116
for (const [idx, command] of macro.commands.entries()) {
116117
if (command.length === 0) continue;
@@ -121,8 +122,9 @@ m name ${CmdLet.DELETE_CHAR} - delete a macro
121122
if (command.trim().length === 0) continue;
122123

123124
const parser = new Parser(command.toString());
124-
const tokens = parser.tokenize();
125-
ret = Command.runSync(tokens);
125+
const commandTokens = parser.tokenize();
126+
const substituted = MacroCmdLet.substituteTokens(commandTokens, tokens);
127+
ret = Command.runSync(substituted);
126128
Vars.setRet(ret);
127129

128130
/*
@@ -136,4 +138,37 @@ m name ${CmdLet.DELETE_CHAR} - delete a macro
136138
}
137139
return ret;
138140
}
141+
142+
private static substituteTokens(
143+
input: Token[],
144+
substitutes: Token[],
145+
): Token[] {
146+
const tokens: Token[] = [];
147+
for (const token of input) {
148+
const literal = token.getLiteral();
149+
150+
/* If it's not a parameter, don't substitute it */
151+
if (!literal.startsWith(MacroCmdLet.PARAM_PREFIX)) {
152+
tokens.push(token);
153+
continue;
154+
}
155+
156+
const indexString = literal.slice(1);
157+
const index = parseInt(indexString);
158+
/* Our token may be a register (which also starts with a '$' */
159+
if (isNaN(index)) {
160+
tokens.push(token);
161+
continue;
162+
}
163+
164+
if (index >= substitutes.length) {
165+
throw new Error(
166+
`macro parameter index ${index} out of range (max ${substitutes.length - 1})`,
167+
);
168+
}
169+
170+
tokens.push(substitutes[index] as Token);
171+
}
172+
return tokens;
173+
}
139174
}

src/commands/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class Command {
1717

1818
const macro = this.getMacro(tokens);
1919
if (macro !== null) {
20-
return MacroCmdLet.runSync(macro);
20+
return MacroCmdLet.runSync(macro, tokens.slice(1));
2121
}
2222

2323
return this.runFunction(tokens);
@@ -31,7 +31,7 @@ export class Command {
3131

3232
const macro = this.getMacro(tokens);
3333
if (macro !== null) {
34-
return MacroCmdLet.runSync(macro);
34+
return MacroCmdLet.runSync(macro, tokens.slice(1));
3535
}
3636

3737
return this.runFunction(tokens);

0 commit comments

Comments
 (0)