CLIte/clite/commands.js

245 lines
4.8 KiB
JavaScript

clite.commands.data = function() {
// insert commands below this line
clite.commands.load('help',function(args,env,io) {
if (args.length > 1) {
// TODO: implement command help lookup
io.write('help with arguments is a work in progress');
}
io.write(`
Welcome to CLIte (pronounced like 'site' with an added L).
Various unix-like commands are available for navigating the CLIte shell:
ls - list directory contents
cat - concatenate files and print to std output or local file
touch - create a new file
less - simple text file viewer
Running any command with the argument -? will give you help for that program.
Or you can run help <command> to get the same info.
`);
return 0;
});
clite.commands.load('ls',function(args,env,io) {
var dirs = [];
var long = false;
function help() {
io.write(`
ls - list directory contents
Usage: ls [OPTION] [FILE]
Options:
-? Print this help information
-l Display data in long format
`);
}
function writeNodeData(node) {
if (long) {
if (node.data.islink) {
io.write(node.perms+'\t'+node.uid+'\t'+node.gid+'\t'+node.name+' -> '+node.data.content);
}else{
io.write(node.perms+'\t'+node.uid+'\t'+node.gid+'\t'+node.name);
}
}else{
io.write(node.name);
}
}
for (var i=1; i<args.length; i++) {
if (args[i][0] == '-') {
for (var j=1; j<args[i].length; j++) {
switch (args[i][j]) {
case '?':
help();
return 0;
break;
case 'l':
long = true;
break;
default:
io.error('unknown argument: -'+args[i][j]);
}
}
}else{
dirs.push(clite.lib.resolvePath(args[i],false));
}
}
if (dirs.length < 1)
dirs.push(env.PWD);
dirs.forEach(function(dir,index) {
if (dirs.length > 1)
io.write(dir+':');
var fd = clite.io.open(dir);
if (!fd) {
io.error('cannot open directory: '+dir);
return;
}
if (!fd.node.data.isdir) {
writeNodeData(fd.node);
clite.io.close(fd);
return;
}
var e;
while ((e = clite.io.read(fd)) != null) {
writeNodeData(e);
}
clite.io.close(fd);
});
return 0;
});
clite.commands.load('cat',function(args,env,io) {
var files = [];
var download = false;
var pending = 0;
function help() {
io.write(`
cat - concatenate files and print to std output or local file
Usage: cat [OPTION] <FILE> [FILE]
Options:
-? Print this help information
-d Download the file data, instead of printing it
`);
}
for (var i=1; i<args.length; i++) {
if (args[i][0] == '-') {
for (var j=1; j<args[i].length; j++) {
switch (args[i][j]) {
case '?':
help();
return 0;
break;
case 'd':
download = true;
break;
default:
io.error('unknown argument: -'+args[i][j]);
}
}
}else{
files.push(clite.lib.resolvePath(args[i],false));
}
}
if (files.length < 1)
return 1;
pending = files.length;
function fcb(fd) {
if (fd.node.data.isdir || fd.node.data.islink || fd.node.data.isdev) {
io.error('not a normal file: '+file);
}else{
var d = clite.io.readAll(fd);
if (d != null) {
if (download) {
var link = document.createElement("a");
link.target = '_blank';
link.download = fd.node.name;
var blob = new Blob([d], {type: "text/plain"});
link.href = URL.createObjectURL(blob);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}else{
io.write(d);
}
}
}
pending--;
clite.io.close(fd);
if (pending <1)
io.exit(0);
}
files.forEach(function(file,index) {
var fd = clite.io.open(file,fcb);
if (!fd) {
io.error('cannot open file: '+file);
pending--;
return;
}
if (fd.node.data.isdir || fd.node.data.islink || fd.node.data.isdev) {
io.error('not a normal file: '+file);
pending--;
return;
}
});
if (pending <1)
return 0;
return null;
});
clite.commands.load('touch',function(args,env,io) {
var file = null;
function help() {
io.write(`
touch - create a new empty file
Usage: touch [OPTION] <FILE>
Options:
-? Print this help information
`);
}
for (var i=1; i<args.length; i++) {
if (args[i][0] == '-') {
for (var j=1; j<args[i].length; j++) {
switch (args[i][j]) {
case '?':
help();
return 0;
break;
default:
io.error('unknown argument: -'+args[i][j]);
}
}
}else if (file == null) {
if (args[i][0] == '/') {
file = args[i];
}else{
file = clite.lib.resolvePath(args[i],false);
}
}else{
io.error('unknown argument: '+args[i]);
}
}
if (file == null) {
io.error('no file specified');
return 1;
}
if (!clite.io.creat(file)) {
io.error('unable to create file: '+file);
return 1;
}
return 0;
});
clite.commands.load('less',function(args,env,io) {
return 0;
});
clite.commands.load('view',function(args,env,io) {
return 0;
});
clite.commands.load('edit',function(args,env,io) {
return 0;
});
// insert commands above this line
}