mirror of
https://codeberg.org/TicklishHoneyBee/CLIte.git
synced 2026-03-11 09:04:37 +00:00
136 lines
2.5 KiB
JavaScript
136 lines
2.5 KiB
JavaScript
clite.commands.data = function() {
|
|
// insert commands below this line
|
|
|
|
clite.commands.load('help',function(args,env,io) {
|
|
io.write('yeah, I should probably implement this');
|
|
return 0;
|
|
});
|
|
|
|
clite.commands.load('ls',function(args,env,io) {
|
|
var dirs = [];
|
|
var long = false;
|
|
function help() {
|
|
io.write(`
|
|
Usage: ls [OPTION] [FILE]
|
|
|
|
Options:
|
|
-? Print this help information
|
|
-l Display data in long format
|
|
`);
|
|
}
|
|
|
|
function writeNodeData(node) {
|
|
if (long) {
|
|
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]);
|
|
}
|
|
}
|
|
}else if (args[i][0] == '/') {
|
|
dirs.push(args[i]);
|
|
}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 pending = 0;
|
|
|
|
for (var i=1; i<args.length; i++) {
|
|
if (args[i][0] == '-') {
|
|
io.error('unknown argument: '+args[i]);
|
|
}else if (args[i][0] == '/') {
|
|
files.push(args[i]);
|
|
}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) {
|
|
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('download',function(args,env,io) {
|
|
return 0;
|
|
});
|
|
|
|
// insert commands above this line
|
|
}
|