Skip to content

Commit 0a1b51f

Browse files
committed
Initial import of the tags.r2.js script (CTAGS support) ##r2js
1 parent fabf170 commit 0a1b51f

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

scripts/tags.r2.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
var tagsRegisterPlugin = (function () {
2+
var tags = {};
3+
function loadTagsFile(tagsFile) {
4+
const lines = r2.cmd("cat " + tagsFile).split(/\n/g);;
5+
for (let line of lines) {
6+
const cols = line.split(/\t/g);
7+
tags[cols[0]] = {
8+
file: cols[1],
9+
expr: cols[2],
10+
}
11+
}
12+
}
13+
function catFunction(name) {
14+
const found = tags[name];
15+
if (found) {
16+
console.log(JSON.stringify(tags[name], null, 2));
17+
const par = found.expr.indexOf("(");
18+
if (par != -1) {
19+
found.expr = found.expr.substr(0, par- 1);
20+
}
21+
const res = r2.cmd("cat " + found.file);
22+
const beg = new RegExp (found.expr.substr(2));
23+
const end = new RegExp (/\n}/);
24+
const fileData = r2.cmd("cat " + found.file);
25+
if (fileData && fileData.length > 0) {
26+
const index = fileData.search(beg);
27+
if (index >= 0) {
28+
const functionData = fileData.substr(index);
29+
const elfin = functionData.search(end);
30+
if (elfin != -1) {
31+
r2.log(functionData.substr(0, elfin + 2));
32+
return true;
33+
}
34+
} else {
35+
console.log("Cannot find pattern: "+ beg);
36+
}
37+
} else {
38+
console.log("Cannot open " + found.file);
39+
}
40+
}
41+
return false;
42+
}
43+
function tagsCommand(cmd) {
44+
const args = cmd.substr(4).trim();
45+
if (args.startsWith("-f")) {
46+
loadTagsFile(args.substr(2).trim());
47+
} else if (args.startsWith("-h")) {
48+
console.log("Usage: tags [args]");
49+
console.log(" tags -f ./tags - load tags in memory");
50+
console.log(" tags main - show function contents from file");
51+
console.log(" tags - show function in current offset");
52+
} else if (args === "") {
53+
let functionName = r2.cmd("isqq.").trim();
54+
if (functionName === "") {
55+
functionName = r2.cmd("fd").trim();
56+
if (functionName) {
57+
const space = functionName.indexOf(" ");
58+
if (space != -1) {
59+
functionName = functionName.substr(0, space);
60+
}
61+
}
62+
}
63+
if (functionName.startsWith("_")) {
64+
if (!catFunction(functionName)) {
65+
catFunction(functionName.substr(1));
66+
}
67+
} else {
68+
catFunction(functionName);
69+
}
70+
} else if (args.startsWith("-d")) {
71+
const dir = args.substr(2).trim();
72+
r2.cmd("cd " + dir);
73+
} else {
74+
catFunction(args);
75+
}
76+
}
77+
function main() {
78+
r2.unload('core', 'tags');
79+
r2.plugin('core', function () {
80+
function coreCall (cmd) {
81+
if (cmd.startsWith('tags')) {
82+
try {
83+
tagsCommand(cmd);
84+
} catch (e) {
85+
console.error(e);
86+
}
87+
return true;
88+
}
89+
return false;
90+
}
91+
return {
92+
name: 'tags',
93+
license: 'MIT',
94+
desc: 'read function from ctags',
95+
call: coreCall
96+
};
97+
});
98+
}
99+
return main;
100+
})();
101+
tagsRegisterPlugin();

0 commit comments

Comments
 (0)