From 8b479fb16944d2b91e5e1e8623b2506a303be947 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 24 Feb 2013 15:07:54 -0600 Subject: [PATCH] messy work on extended parser. --- example/tput.js | 1 + lib/tput.js | 225 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 222 insertions(+), 4 deletions(-) diff --git a/example/tput.js b/example/tput.js index 35240a4..119bb46 100644 --- a/example/tput.js +++ b/example/tput.js @@ -2,6 +2,7 @@ var Tput = require('../').Tput; var tput = Tput({ term: process.argv[2] || 'xterm', + extended: true, debug: true }); diff --git a/lib/tput.js b/lib/tput.js index 8dea4b5..4db6d47 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -37,6 +37,7 @@ function Tput(options) { this.info = {}; this.debug = options.debug; this.padding = options.padding; + this.extended = options.extended; this.readTerminfo(); this.compile(); @@ -170,12 +171,15 @@ Tput.prototype.parseTerminfo = function(data) { // Extended Header if (this.extended) { - i += h.strTableSize + 1; // offset? + i += h.strTableSize - 0; l = data.length; + console.log(i, l); if (i < l) { info.extended = this.parseExtended(data.slice(i)); + //['bools', 'numbers', 'strings'].forEach(function(key) { Object.keys(info.extended).forEach(function(key) { info[key].extended = info.extended[key]; + // merge(info[key], info.extended[key]); }); delete info.extended; } @@ -209,6 +213,9 @@ Tput.prototype.parseExtended = function(data) { i = h.headerSize; + console.log(h); + //process.exit(0); + // Booleans Section // One byte for each flag // Same order as @@ -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 info.strings = {}; l = i + h.strCount * 2; @@ -246,22 +262,38 @@ Tput.prototype.parseExtended = function(data) { for (; i < l; i += 2) { v = Tput.strings[o++] || 'OFFSET: ' + (o - 1); if (data[i + 1] === 0377 && data[i] === 0377) { + //break; info.strings[v] = -1; } else { 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 - Object.keys(info.strings).forEach(function(key) { + Object.keys(info.strings).forEach(function(key, k, keys) { if (info.strings[key] === -1) { - delete info.strings[key]; - return; + //delete info.strings[key]; + //return; } var s = i + info.strings[key] , 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++; 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] = (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; }; +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 + 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 */ @@ -287,6 +502,8 @@ Tput.prototype.compile = function(key) { console.log(this.info); } + return; + this.methods = {}; this.info.all = {};