Skip to content

Commit

Permalink
Pass environment to builtins. Store command return value / data in en…
Browse files Browse the repository at this point in the history
…vironment.
  • Loading branch information
unconed committed May 30, 2011
1 parent 82f73d0 commit 6463794
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 22 deletions.
Binary file added HTML/Images/files.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion HTML/termkit.js
Expand Up @@ -14,7 +14,7 @@ $(document).ready(function () {
view.newCommand();
});
};

});

})(jQuery);
Expand Down
4 changes: 3 additions & 1 deletion Node-API.md
Expand Up @@ -5,7 +5,7 @@
* Make a .js file that exports a `main` function:

```
exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {
exit(true); // Success
exit(false); // Error
Expand All @@ -28,6 +28,8 @@ There is no plug-in system yet, so you'll have to:

`exit` is a function to call when ending the process. This call can be nested inside asynchronous closures. Pass `true` for success, `false` for error, -1 for success with warnings. You can pass out additional meta-data in the second argument, though this is currently not used for anything.

`environment` is a read-only object with name-value pairs containing the execution environment.

## Pipes

The 5 pipes are:
Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/cat.js
Expand Up @@ -4,7 +4,7 @@ var fs = require('fs'),
meta = require('shell/meta'),
expandPath = require('misc').expandPath;

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {
var out = new view.bridge(pipes.viewOut);
var chunkSize = 16384;

Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/cd.js
@@ -1,7 +1,7 @@
var view = require('view/view'),
expandPath = require('misc').expandPath;

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {

var out = new view.bridge(pipes.viewOut);

Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/clear.js
@@ -1,6 +1,6 @@
var view = require('view/view');

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {

pipes.viewOut('view.clear');

Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/echo.js
@@ -1,6 +1,6 @@
var meta = require('shell/meta');

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {

// Prepare text.
tokens.shift();
Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/get.js
Expand Up @@ -4,7 +4,7 @@ var view = require('view/view'),
http = require('http'),
url = require('url');

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {
var out = new view.bridge(pipes.viewOut);
var chunkSize = 16384;

Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/grep.js
Expand Up @@ -2,7 +2,7 @@ var view = require('view/view'),
reader = require('shell/reader'),
parseArgs = require('misc').parseArgs;

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {
var out = new view.bridge(pipes.viewOut);

var args = parseArgs(tokens),
Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/ls.js
Expand Up @@ -8,7 +8,7 @@ var fs = require('fs'),
expandPath = require('misc').expandPath,
parseArgs = require('misc').parseArgs;

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {

var out = new view.bridge(pipes.viewOut);

Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/null.js
@@ -1,6 +1,6 @@
var view = require('view/view');

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {
var out = new view.bridge(pipes.viewOut);
out.print('Unknown command "' + tokens[0] + '"');
return exit(false);
Expand Down
2 changes: 1 addition & 1 deletion Node/shell/builtin/pwd.js
@@ -1,6 +1,6 @@
var view = require('view/view');

exports.main = function (tokens, pipes, exit) {
exports.main = function (tokens, pipes, exit, environment) {
var out = new view.bridge(pipes.viewOut);

var cwd = process.cwd();
Expand Down
24 changes: 13 additions & 11 deletions Node/shell/command.js
Expand Up @@ -73,7 +73,8 @@ exports.commandList = function (processor, tokens, exit, rel) {
});

// Create command units.
var that = this;
var that = this,
environment = processor.environment();
this.units = tokens.map(function (command, i) {
// Track exit invocation.
var exit = track(function (success, object) {
Expand All @@ -82,7 +83,7 @@ exports.commandList = function (processor, tokens, exit, rel) {
returns = [ success, object ];
}
});
return exports.commandFactory(command, views[i].emitter, views[i].invoke, exit);
return exports.commandFactory(command, views[i].emitter, views[i].invoke, exit, environment);
});

// Spawn and link together.
Expand Down Expand Up @@ -110,22 +111,22 @@ exports.commandList.prototype = {
/**
* Command unit factory.
*/
exports.commandFactory = function (command, emitter, invoke, exit) {
exports.commandFactory = function (command, emitter, invoke, exit, environment) {
var unit;
try {
// Try built-in.
unit = new exports.commandUnit.builtinCommand(command, emitter, invoke, exit);
unit = new exports.commandUnit.builtinCommand(command, emitter, invoke, exit, environment);
unit.spawn();
}
catch (e) {
try {
// Try native command.
unit = new exports.commandUnit.unixCommand(command, emitter, invoke, exit);
unit = new exports.commandUnit.unixCommand(command, emitter, invoke, exit, environment);
unit.spawn();
}
catch (e) {
// Execute null.js fallback.
unit = new exports.commandUnit.builtinCommand(command, emitter, invoke, exit);
unit = new exports.commandUnit.builtinCommand(command, emitter, invoke, exit, environment);
unit.override = 'null';
unit.spawn();
}
Expand All @@ -137,11 +138,12 @@ exports.commandFactory = function (command, emitter, invoke, exit) {
/**
* A single command in a pipeline.
*/
exports.commandUnit = function (command, emitter, invoke, exit) {
exports.commandUnit = function (command, emitter, invoke, exit, environment) {
this.command = command;
this.emitter = emitter;
this.invoke = invoke;
this.exit = exit;
this.environment = environment;
};

exports.commandUnit.prototype = {
Expand Down Expand Up @@ -170,7 +172,7 @@ exports.commandUnit.prototype = {
/**
* Built-in command.
*/
exports.commandUnit.builtinCommand = function (command, emitter, invoke, exit) {
exports.commandUnit.builtinCommand = function (command, emitter, invoke, exit, environment) {
exports.commandUnit.apply(this, arguments);
}

Expand Down Expand Up @@ -256,14 +258,14 @@ exports.commandUnit.builtinCommand.prototype.go = function () {
};

async(function () {
that.handler.main.call(that, that.command, pipes, exit);
that.handler.main.call(that, that.command, pipes, exit, this.environment);
});
};

/**
* UNIX command.
*/
exports.commandUnit.unixCommand = function (command, emitter, invoke, exit) {
exports.commandUnit.unixCommand = function (command, emitter, invoke, exit, environment) {
exports.commandUnit.apply(this, arguments);
}

Expand All @@ -277,7 +279,7 @@ exports.commandUnit.unixCommand.prototype.spawn = function () {
this.process = spawn(prefix, command);

this.process.on('exit', function (code) {
that.exit(!code);
that.exit(!code, { code: code });
});
};

Expand Down
7 changes: 7 additions & 0 deletions Node/shell/processor.js
Expand Up @@ -59,6 +59,7 @@ exports.processor.prototype = {
meta.answer = message.query;
meta.args = object;

that.status(success, object);
that.send(meta);
returned = true;
}
Expand Down Expand Up @@ -103,6 +104,11 @@ exports.processor.prototype = {
};
this.send(message);
},

// Store command status in environment.
status: function (success, object) {
this.status = { success: success, data: object };
},

// Return the environment.
environment: function () {
Expand All @@ -115,6 +121,7 @@ exports.processor.prototype = {
path: process.env.PATH.split(':'),
manPath: process.env.MANPATH,
defaultShell: process.env.SHELL,
command: this.status,
};
},
};
Expand Down
3 changes: 3 additions & 0 deletions todo.txt
@@ -1,5 +1,7 @@
Tasks:
[X] return process meta-data in environment
[ ] backgrounding / new command trigger
[ ] command aliases
[ ] command decoration
[ ] interactive execution
[ ] inline man-pages tips
Expand Down Expand Up @@ -109,6 +111,7 @@ Prototype:
[X] smart binary output
[ ] handle encodings
[ ] backgrounding / new command trigger
[ ] command aliases
[X] preferences
[ ] sudo support (askpass env?)

Expand Down

0 comments on commit 6463794

Please sign in to comment.