Fix r2.js to be used from duktape

This commit is contained in:
pancake 2014-10-23 04:45:01 +02:00
parent e43f0622fe
commit 8ec5e4ae94
2 changed files with 210 additions and 157 deletions

View File

@ -127,12 +127,14 @@ static char *getstr(const char *src) {
*pat++ = 0;
int i, rep = atoi (src+1);
int len = strlen (pat);
if (rep>0) {
char *buf = malloc (rep);
for(i=0;i<rep;i++) {
buf[i] = pat[i%len];
}
return buf;
}
}
// slurp file
return r_file_slurp (src+1, NULL);
}
@ -551,8 +553,8 @@ R_API int r_run_start(RRunProfile *p) {
eprintf ("rarun2: %s: file not found\n", p->_program);
return 1;
}
// close all non-tty fds
{ int i; for (i=3; i<9999; i++) close (i); }
// XXX HACK close all non-tty fds
{ int i; for (i=3; i<10; i++) close (i); }
// TODO: use posix_spawn
exit (execv (p->_program, (char* const*)p->_args));
}

View File

@ -8,7 +8,7 @@ var next_curoff = 0;
var next_lastoff = 0;
var prev_curoff = 0;
var prev_lastoff = 0;
var r2cmd = false;
var hascmd = false;
// async helper
function asyncLoop(iterations, func, callback) {
@ -45,9 +45,11 @@ function asyncLoop(iterations, func, callback) {
if (typeof (module) !== 'undefined') {
module.exports = function(r) {
if (typeof (r) == 'function')
r2cmd = r;
else r2cmd = r.cmd;
if (typeof (r) == 'function') {
hascmd = r;
} else {
hascmd = r.cmd;
}
return r2;
}
}
@ -55,7 +57,11 @@ if (typeof (module) !== 'undefined') {
r2.plugin = function() {
console.error ("r2.plugin is not available in this environment");
}
try { if (r2plugin) r2.plugin = r2plugin } catch(e) { }
try {
if (r2plugin) {
r2.plugin = r2plugin
}
} catch ( e ) {}
r2.root = ""; // prefix path
@ -63,9 +69,11 @@ r2.root = ""; // prefix path
function dump(obj) {
var x = "";
for (var a in obj) x += a + "\n";
if (typeof ('alert') != 'undefined')
if (typeof ('alert') != 'undefined') {
alert (x);
else console.log (x);
} else {
console.log (x);
}
}
r2.analOp = function(addr, cb) {
@ -87,17 +95,26 @@ function objtostr(obj) {
}
function Ajax(method, uri, body, fn) {
if (typeof (XMLHttpRequest) == "undefined")
return false;
var x = new XMLHttpRequest ();
if (!x)
return false;
x.open (method, uri, false);
x.setRequestHeader ('Accept', 'text/plain');
x.setRequestHeader ('Accept', 'text/html');
x.setRequestHeader ("Content-Type", "application/x-ww-form-urlencoded; charset=UTF-8");
x.onreadystatechange = function(y) {
if (x.status == 200) {
if (fn) fn (x.responseText);
} else console.error ("ajax "+x.status)
if (fn) {
fn (x.responseText);
}
} else {
console.error ("ajax " + x.status)
}
}
x.send (body);
return true;
}
r2.assemble = function(offset, opcode, fn) {
@ -189,7 +206,9 @@ r2.cmds = function (cmds, cb) {
cmd = cmds[0];
cmds = cmds.splice (1);
r2.cmd (cmd, lala);
if (cb) cb ();
if (cb) {
cb ();
}
return;
}
r2.cmd (cmd, lala);
@ -197,11 +216,23 @@ r2.cmds = function (cmds, cb) {
function _internal_cmd(c, cb) {
if (r2cmd) {
hascmd = r2cmd;
}
if (hascmd) {
// TODO: use setTimeout for async?
return r2cmd (c, cb);
if (typeof (r2plugin) != "undefined") {
// duktape
cb (r2cmd(c));
} else {
Ajax ('GET', r2.root+"/cmd/"+encodeURI (c), '',
function (x) { if (cb) cb (x); });
// node
return hascmd (c, cb);
}
} else {
Ajax ('GET', r2.root + "/cmd/" + encodeURI (c), '', function(x) {
if (cb) {
cb (x);
}
});
}
}
@ -238,15 +269,19 @@ cb (null);
r2.alive = function(cb) {
r2.cmd ("b", function(o) {
var ret = false;
if (o && o.length () > 0)
if (o && o.length () > 0) {
ret = true;
if (cb) cb (o);
}
if (cb) {
cb (o);
}
});
}
r2.getTextLogger = function(obj) {
if (typeof (obj) != "object")
if (typeof (obj) != "object") {
obj = {};
}
obj.last = 0;
obj.events = {};
obj.interval = null;
@ -255,7 +290,9 @@ r2.getTextLogger = function (obj) {
});
obj.load = function(cb) {
r2.cmd ("Tj " + (obj.last + 1), function(ret) {
if (cb) cb (JSON.parse (ret));
if (cb) {
cb (JSON.parse (ret));
}
});
}
obj.clear = function(cb) {
@ -274,16 +311,20 @@ r2.getTextLogger = function (obj) {
"id": message[0],
"text": message[1]
});
if (message[0] > obj.last)
if (message[0] > obj.last) {
obj.last = message[0];
}
if (cb) cb ();
}
if (cb) {
cb ();
}
});
}
obj.autorefresh = function(n) {
if (!n) {
if (obj.interval)
if (obj.interval) {
obj.interval.stop ();
}
return;
}
function to() {
@ -303,10 +344,14 @@ r2.getTextLogger = function (obj) {
}
r2.filter_asm = function(x, display) {
var curoff = backward? prev_curoff: next_curoff;;
var lastoff = backward? prev_lastoff: next_lastoff;;
var curoff = backward ? prev_curoff : next_curoff;
;
var lastoff = backward ? prev_lastoff : next_lastoff;
;
var lines = x.split (/\n/g);
r2.cmd ("s", function (x) { curoff = x; });
r2.cmd ("s", function(x) {
curoff = x;
});
for (var i = lines.length - 1; i > 0; i--) {
var a = lines[i].match (/0x([a-fA-F0-9]+)/);
if (a && a.length > 0) {
@ -322,8 +367,7 @@ r2.filter_asm = function (x, display) {
z += row[0] + " " + row[3] + "\n";
}
x = z;
} else
if (display[0] == 'f') {
} else if (display[0] == 'f') {
//hasmore (false);
if (display[1] == 's') {
var z = "";
@ -338,8 +382,7 @@ r2.filter_asm = function (x, display) {
x = z;
} else {
}
} else
if (display[0] == "i") {
} else if (display[0] == "i") {
//hasmore (false);
if (display[1]) {
var z = "";
@ -349,9 +392,15 @@ r2.filter_asm = function (x, display) {
var addr = "";
for (var j = 0; j < elems.length; j++) {
var kv = elems[j].split (/=/);
if (kv[0] == "addr") addr = kv[1];
if (kv[0] == "name") name = kv[1];
if (kv[0] == "string") name = kv[1];
if (kv[0] == "addr") {
addr = kv[1];
}
if (kv[0] == "name") {
name = kv[1];
}
if (kv[0] == "string") {
name = kv[1];
}
}
z += addr + " " + name + "\n";
}
@ -387,8 +436,10 @@ r2.filter_asm = function (x, display) {
} else {
next_curoff = curoff;
next_lastoff = lastoff;
if (!prev_curoff)
if (!prev_curoff) {
prev_curoff = next_curoff;
}
}
return x;
}