diff --git a/lib/tput.js b/lib/tput.js index 43b7a77..6080ee7 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -294,14 +294,14 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { while (val) { // '\e' -> ^[ - if (cap = /\\e/gi.exec(val)) { + if (cap = /^\\e/gi.exec(val)) { val = val.substring(cap[0].length); code += '\x1b'; continue; } // '^A' -> ^A - if (cap = /\^(.)/gi.exec(val)) { // case-insensitive? + if (cap = /^\^(.)/gi.exec(val)) { // case-insensitive? val = val.substring(cap[0].length); ch = cap[1]; switch (ch) { @@ -411,7 +411,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // '\n' -> \n // '\r' -> \r // '\0' -> \200 (special case) - if (cap = /\\([nlrtbfs\^\\,:0])/g.exec(val)) { + if (cap = /^\\([nlrtbfs\^\\,:0])/g.exec(val)) { val = val.substring(cap[0].length); ch = cap[1]; switch (ch) { @@ -443,7 +443,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { } // 3 octal digits -> character - if (cap = /\\(\d\d\d)/g.exec(val)) { + if (cap = /^\\(\d\d\d)/g.exec(val)) { val = val.substring(cap[0].length); ch = cap[1]; code += String.fromCharCode(parseInt(ch, 8)); @@ -451,7 +451,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { } // $<5> -> padding - if (cap = /\$<(\d+)>(\*|\/)/g.exec(val)) { + if (cap = /^\$<(\d+)>(\*|\/)/g.exec(val)) { val = val.substring(cap[0].length); ch = cap[1]; code += Array(+ch + 1).join(' '); // "padding" characters? @@ -460,7 +460,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // man terminfo, around page 1034 // %% outputs `%' - if (cap = /%%/g.exec(val)) { + if (cap = /^%%/g.exec(val)) { val = val.substring(cap[0].length); code += '%'; continue; @@ -470,21 +470,29 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // as in printf, flags are [-+#] and space. Use a `:' to allow the // next character to be a `-' flag, avoiding interpreting "%-" as an // operator. - if (cap = /%(?:(:)?([\-+# ]+)?)(?:(\d+)(\.\d+)?)?([doxXs])?/g.exec(val)) { + if (cap = /^%(?:(:)?([\-+# ]+)?)(?:(\d+)(\.\d+)?)?([doxXs])?/g.exec(val)) { val = val.substring(cap[0].length); code += 'TODO'; continue; } // %c print pop() like %c in printf - if (cap = /%c/g.exec(val)) { + if (cap = /^%c/g.exec(val)) { + val = val.substring(cap[0].length); + code += 'stack.pop()'; // TODO: FORMAT + continue; + } + + // %d print pop() like %d in printf + // NOT SURE ABOUT %d being print! + if (cap = /^%d/g.exec(val)) { val = val.substring(cap[0].length); code += 'stack.pop()'; // TODO: FORMAT continue; } // %s print pop() like %s in printf - if (cap = /%s/g.exec(val)) { + if (cap = /^%s/g.exec(val)) { val = val.substring(cap[0].length); code += 'stack.pop()'; // TODO: FORMAT continue; @@ -492,7 +500,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // %p[1-9] // push i'th parameter - if (cap = /%p([1-9])/g.exec(val)) { + if (cap = /^%p([1-9])/g.exec(val)) { val = val.substring(cap[0].length); code += 'params[i]'; continue; @@ -500,28 +508,28 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // %P[a-z] // set dynamic variable [a-z] to pop() - if (cap = /%P([a-z])/g.exec(val)) { + if (cap = /^%P([a-z])/g.exec(val)) { val = val.substring(cap[0].length); v = cap[1]; - code += 'dyn[' + v + '] = stack.pop()'; + code += 'dyn.' + v + ' = stack.pop()'; continue; } // %g[a-z] // get dynamic variable [a-z] and push it - if (cap = /%g([a-z])/g.exec(val)) { + if (cap = /^%g([a-z])/g.exec(val)) { val = val.substring(cap[0].length); v = cap[1]; - code += '(stack.push(dyn[' + v + ']), dyn[' + v + '])'; + code += '(stack.push(dyn.' + v + '), dyn.' + v + ')'; continue; } // %P[A-Z] // set static variable [a-z] to pop() - if (cap = /%P([A-Z])/g.exec(val)) { + if (cap = /^%P([A-Z])/g.exec(val)) { val = val.substring(cap[0].length); v = cap[1]; - code += 'stat[' + v + '] = stack.pop()'; + code += 'stat.' + v + ' = stack.pop()'; continue; } @@ -534,15 +542,15 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // documented in other implementations. Relying on it will adversely // impact portability to other implementations. - if (cap = /%g([A-Z])/g.exec(val)) { + if (cap = /^%g([A-Z])/g.exec(val)) { val = val.substring(cap[0].length); v = cap[1]; - code += 'stack.push(stat[' + v + '])'; + code += 'stack.push(stat.' + v + ')'; continue; } // %'c' char constant c - if (cap = /%'(\w)'/g.exec(val)) { + if (cap = /^%'(\w)'/g.exec(val)) { val = val.substring(cap[0].length); ch = cap[1]; code += '"' + ch + '"'; @@ -551,7 +559,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // %{nn} // integer constant nn - if (cap = /%\{(\d+)\}/g.exec(val)) { + if (cap = /^%\{(\d+)\}/g.exec(val)) { val = val.substring(cap[0].length); ch = cap[1]; code += '(' + ch + ')'; @@ -559,7 +567,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { } // %l push strlen(pop) - if (cap = /%l/g.exec(val)) { + if (cap = /^%l/g.exec(val)) { val = val.substring(cap[0].length); code += 'stack.push(stack.pop().length)'; continue; @@ -571,7 +579,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // bit operations (AND, OR and exclusive-OR): push(pop() op pop()) // %= %> %< // logical operations: push(pop() op pop()) - if (cap = /%([+\-*\/m&|\^=><])/g.exec(val)) { + if (cap = /^%([+\-*\/m&|\^=><])/g.exec(val)) { val = val.substring(cap[0].length); op = cap[1]; if (op === '=') op = '==='; @@ -582,7 +590,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // %A, %O // logical AND and OR operations (for conditionals) - if (cap = /%([AO])/g.exec(val)) { + if (cap = /^%([AO])/g.exec(val)) { val = val.substring(cap[0].length); op = cap[1]; code += op === ' A ' ? ' && ' : ' || '; @@ -591,7 +599,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // %! %~ // unary operations (logical and bit complement): push(op pop()) - if (cap = /%([!~])/g.exec(val)) { + if (cap = /^%([!~])/g.exec(val)) { val = val.substring(cap[0].length); op = cap[1]; code += 'stack.push(' + op + 'stack.pop())'; @@ -599,7 +607,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { } // %i add 1 to first two parameters (for ANSI terminals) - if (cap = /%i/g.exec(val)) { + if (cap = /^%i/g.exec(val)) { val = val.substring(cap[0].length); code += '(params[0]++, params[1]++)'; continue; @@ -620,27 +628,27 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { // if-then-else's. Some strings, e.g., sgr can be very complicated when // written on one line. The -f option splits the string into lines with // the parts indented. - if (cap = /%\?/g.exec(val)) { + if (cap = /^%\?/g.exec(val)) { val = val.substring(cap[0].length); - code += ';if ('; + code += '"); if ('; continue; } - if (cap = /%t/g.exec(val)) { + if (cap = /^%t/g.exec(val)) { val = val.substring(cap[0].length); - code += ') {'; + code += ') { out.push("'; continue; } - if (cap = /%e/g.exec(val)) { + if (cap = /^%e/g.exec(val)) { val = val.substring(cap[0].length); - code += '} else {'; + code += '"); } else { out.push("'; continue; } - if (cap = /%;/g.exec(val)) { + if (cap = /^%;/g.exec(val)) { val = val.substring(cap[0].length); - code += '}'; + code += '"); } out.push("'; continue; } @@ -675,6 +683,9 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) { code += val[0]; val = val.substring(1); } + + code += '"); return out.join("");'; + break; }