diff --git a/lib/IOInterface.js b/lib/IOInterface.js index 7d04abc..d0a21fd 100644 --- a/lib/IOInterface.js +++ b/lib/IOInterface.js @@ -103,7 +103,7 @@ IOInterface.prototype.readln = function(callback) { this.read(function(char, value, cancel) { if (char === "\n") { cancel(); - var result = value.substring(0, value.length - 2); + var result = value.substring(0, value.length - 1); callback(result); } }); diff --git a/lib/parser/commands/ShellCommand.js b/lib/parser/commands/ShellCommand.js new file mode 100644 index 0000000..826c94c --- /dev/null +++ b/lib/parser/commands/ShellCommand.js @@ -0,0 +1,64 @@ +/* ************************************************************************** + Additional Command to Execute Shell Command from BASIC + Joe Nicholson Rufilla Ltd. + ************************************************************************** +*/ + +var statements = require('../statements'); +var SyntaxError = require('../SyntaxError'); +var rl = require('../../IOInterface').getDefault(); + +var exec = require('child_process').exec; + +/** + * Shells execution + * + * @param {String} args The arguments to the command + * @param {Function} define + * @constructor + */ +function ShellCommand(args, define) { + if (args.length) { + this.message = new statements.ExpressionStatement(args, define); + if (this.message.error) throw this.message.error; + } else this.message = new statements.StringStatement("[<< Shelld, Press RETURN to Continue >>]"); +} + +/** + * Converts the command arguments to a string + * + * @returns {string} + */ +ShellCommand.prototype.toString = function() { + return this.message.toString(); +}; + +/** + * Converts the command to JSON + * + * @returns {Object} + */ +ShellCommand.prototype.toJSON = function() { + return { + message: this.message.toJSON() + }; +}; + +/** + * Executes the command + * + * @param {ExecutionContext} data + * @param {Function} next + */ +ShellCommand.prototype.execute = function(data, next) { + var message = this.message.execute(data); + data.validate(message, 'string'); + + // Executre the shell command + exec(message, function (error, stdout, stderr) { + console.log(stdout); + next(); + }); +}; + +module.exports = ShellCommand; \ No newline at end of file diff --git a/lib/parser/commands/index.js b/lib/parser/commands/index.js index 42abe6b..0ba2087 100644 --- a/lib/parser/commands/index.js +++ b/lib/parser/commands/index.js @@ -21,6 +21,9 @@ exports.restore = require('./RestoreCommand'); exports.data = require('./DataCommand'); exports.read = require('./ReadCommand'); +// Added by Joe +exports.shell = require('./ShellCommand'); + // Graphic commands exports.color = require('./ColorCommand'); exports.tcolor = require('./TcolorCommand');