messy work on extended parser.

This commit is contained in:
Christopher Jeffrey 2013-02-24 15:07:54 -06:00
parent 38d3a6445a
commit 8b479fb169
2 changed files with 222 additions and 4 deletions

View File

@ -2,6 +2,7 @@ var Tput = require('../').Tput;
var tput = Tput({ var tput = Tput({
term: process.argv[2] || 'xterm', term: process.argv[2] || 'xterm',
extended: true,
debug: true debug: true
}); });

View File

@ -37,6 +37,7 @@ function Tput(options) {
this.info = {}; this.info = {};
this.debug = options.debug; this.debug = options.debug;
this.padding = options.padding; this.padding = options.padding;
this.extended = options.extended;
this.readTerminfo(); this.readTerminfo();
this.compile(); this.compile();
@ -170,12 +171,15 @@ Tput.prototype.parseTerminfo = function(data) {
// Extended Header // Extended Header
if (this.extended) { if (this.extended) {
i += h.strTableSize + 1; // offset? i += h.strTableSize - 0;
l = data.length; l = data.length;
console.log(i, l);
if (i < l) { if (i < l) {
info.extended = this.parseExtended(data.slice(i)); info.extended = this.parseExtended(data.slice(i));
//['bools', 'numbers', 'strings'].forEach(function(key) {
Object.keys(info.extended).forEach(function(key) { Object.keys(info.extended).forEach(function(key) {
info[key].extended = info.extended[key]; info[key].extended = info.extended[key];
// merge(info[key], info.extended[key]);
}); });
delete info.extended; delete info.extended;
} }
@ -209,6 +213,9 @@ Tput.prototype.parseExtended = function(data) {
i = h.headerSize; i = h.headerSize;
console.log(h);
//process.exit(0);
// Booleans Section // Booleans Section
// One byte for each flag // One byte for each flag
// Same order as <term.h> // Same order as <term.h>
@ -239,6 +246,15 @@ Tput.prototype.parseExtended = function(data) {
} }
} }
// looks like: ffff 0000
// 0000920: 1b6c 001b 6d00 0200 0100 3900 7500 a802 .l..m.....9.u...
// 0000930: 0100 ffff ffff 0000 0700 0e00 1500 1c00 ................
//console.log(data.slice(i, i + 10));
//process.exit(0);
if (data[i + 1] === 0377 && data[i] === 0377) {
i += 2;
}
// Strings Section // Strings Section
info.strings = {}; info.strings = {};
l = i + h.strCount * 2; l = i + h.strCount * 2;
@ -246,22 +262,38 @@ Tput.prototype.parseExtended = function(data) {
for (; i < l; i += 2) { for (; i < l; i += 2) {
v = Tput.strings[o++] || 'OFFSET: ' + (o - 1); v = Tput.strings[o++] || 'OFFSET: ' + (o - 1);
if (data[i + 1] === 0377 && data[i] === 0377) { if (data[i + 1] === 0377 && data[i] === 0377) {
//break;
info.strings[v] = -1; info.strings[v] = -1;
} else { } else {
info.strings[v] = (data[i + 1] << 8) | data[i]; info.strings[v] = (data[i + 1] << 8) | data[i];
} }
} }
// 0000990: 3b01 4201 4901 5001 5701 5e01 6501 ffff ;.B.I.P.W.^.e...
// 00009a0: ffff ffff ffff 0000 0300 0600 0900 0c00 ................
// i = 130, l = 130
//console.log(data.slice(i, i + 10));
//process.exit(0);
//i--; // without this it's on `03`
//i = l - 1;
//assert.equal(data[i], 0);
i = l;
// String Table // String Table
Object.keys(info.strings).forEach(function(key) { Object.keys(info.strings).forEach(function(key, k, keys) {
if (info.strings[key] === -1) { if (info.strings[key] === -1) {
delete info.strings[key]; //delete info.strings[key];
return; //return;
} }
var s = i + info.strings[key] var s = i + info.strings[key]
, j = s; , j = s;
//if (keys.length > k + 1) {
// var next = info.strings[keys[k + 1]];
// j = i + next - 2;
//} else {
while (data[s - 1] !== 0) s--;
while (data[j]) j++; while (data[j]) j++;
if (s >= data.length || j > data.length) { if (s >= data.length || j > data.length) {
@ -270,11 +302,194 @@ Tput.prototype.parseExtended = function(data) {
} }
info.strings[key] = data.toString('ascii', s, j); info.strings[key] = data.toString('ascii', s, j);
info.strings[key] = (s - i) + '-' + (j - i) + '-' + info.strings[key];
}); });
// Symbol Table
i = h.lastStrTableOffset - 68;
l = data.length;
//console.log(data.slice(i - 10, i + 10));
//console.log(JSON.stringify(data.slice(i - 10, i + 10).toString('ascii')));
//process.exit(0);
var sym = []
, j = 0;
for (; i < l; i++) {
j = i;
while (data[j]) j++;
sym.push(data.toString('ascii', i, j));
i = j;
}
console.log(sym);
console.log(sym.length);
console.log(Object.keys(info.strings).length);
//process.exit(0);
return info; return info;
}; };
Tput.prototype.parseExtended = function(data) {
var info = {}
, l = data.length
, i = 0
, v
, o;
var h = info.header = {
dataSize: data.length,
headerSize: 10,
boolCount: (data[i + 1] << 8) | data[i + 0],
numCount: (data[i + 3] << 8) | data[i + 2],
strCount: (data[i + 5] << 8) | data[i + 4],
strTableSize: (data[i + 7] << 8) | data[i + 6],
lastStrTableOffset: (data[i + 9] << 8) | data[i + 8]
};
h.total = h.headerSize
+ h.boolCount
+ h.numCount * 2
+ h.strCount * 2
+ h.strTableSize;
i = h.headerSize;
console.log(h);
//process.exit(0);
// Booleans Section
// One byte for each flag
// Same order as <term.h>
info.bools = [];
l = i + h.boolCount;
for (; i < l; i++) {
info.bools.push(!!data[i]);
}
// Null byte in between to make sure numbers begin on an even byte.
if (i % 2) {
assert.equal(data[i], 0);
i++;
}
// Numbers Section
info.numbers = [];
l = i + h.numCount * 2;
for (; i < l; i += 2) {
if (data[i + 1] === 0377 && data[i] === 0377) {
info.numbers.push(-1);
} else {
info.numbers.push((data[i + 1] << 8) | data[i]);
}
}
// looks like: ffff 0000
// 0000920: 1b6c 001b 6d00 0200 0100 3900 7500 a802 .l..m.....9.u...
// 0000930: 0100 ffff ffff 0000 0700 0e00 1500 1c00 ................
//console.log(data.slice(i, i + 10));
//process.exit(0);
if (data[i + 1] === 0377 && data[i] === 0377) {
i += 2;
}
// Strings Section
info.strings = [];
l = i + h.strCount * 2;
for (; i < l; i += 2) {
if (data[i + 1] === 0377 && data[i] === 0377) {
//break;
info.strings.push(-1);
} else {
info.strings.push((data[i + 1] << 8) | data[i]);
}
}
// 0000990: 3b01 4201 4901 5001 5701 5e01 6501 ffff ;.B.I.P.W.^.e...
// 00009a0: ffff ffff ffff 0000 0300 0600 0900 0c00 ................
// i = 130, l = 130
//console.log(data.slice(i, i + 10));
//process.exit(0);
//i--; // without this it's on `03`
//i = l - 1;
//assert.equal(data[i], 0);
i = l;
// String Table
info.strings.forEach(function(offset, k) {
if (offset === -1) {
//info.strings[k] = '';
//return;
}
var s = i + offset
, j = s;
// XXX - WHY?
while (data[s - 1] !== 0) s--;
while (data[j]) j++;
if (s >= data.length || j > data.length) {
//info.strings[k] = '';
return;
}
info.strings[k] = data.toString('ascii', s, j);
//info.strings[k] = (s - i) + '-' + (j - i) + '-' + info.strings[k];
});
// Symbol Table
// XXX - WHY?
i = h.lastStrTableOffset - 68;
l = data.length;
console.log(data.slice(i - 10, i + 10));
console.log(JSON.stringify(data.slice(i - 10, i + 10).toString('ascii')));
//process.exit(0);
var sym = []
, j = 0;
for (; i < l; i++) {
j = i;
while (data[j]) j++;
sym.push(data.toString('ascii', i, j));
i = j;
}
console.log(sym);
console.log(sym.length);
console.log(info.strings.length);
//process.exit(0);
var _bools = {}
, _numbers = {}
, _strings = {};
info.bools.forEach(function(bool) {
_bools[sym.shift()] = bool;
});
info.bools = _bools;
info.numbers.forEach(function(number) {
_numbers[sym.shift()] = number;
});
info.numbers = _numbers;
info.strings.forEach(function(string) {
_strings[sym.shift()] = string;
});
info.strings = _strings;
console.log(info);
process.exit(0);
return info;
};
/** /**
* Compiler - terminfo cap->javascript * Compiler - terminfo cap->javascript
*/ */
@ -287,6 +502,8 @@ Tput.prototype.compile = function(key) {
console.log(this.info); console.log(this.info);
} }
return;
this.methods = {}; this.methods = {};
this.info.all = {}; this.info.all = {};