mirror of
https://github.com/sigmasternchen/Wish
synced 2025-03-15 07:28:56 +00:00
new version of cat
This commit is contained in:
parent
cafdc12e33
commit
b59e02031c
1 changed files with 51 additions and 14 deletions
|
@ -4,17 +4,38 @@ CatClass.prototype = new Process();
|
|||
CatClass.prototype.buffer = new Array();
|
||||
CatClass.prototype.stream;
|
||||
CatClass.prototype.index = 0;
|
||||
CatClass.prototype.flags = new Array();
|
||||
CatClass.prototype.main = function(args) {
|
||||
console.log("cat: started");
|
||||
console.log("cat: checking for input stream");
|
||||
if (args.length == 1) {
|
||||
|
||||
var i = 1;
|
||||
for (i; i < args.length; i++) {
|
||||
if (args[i].substring(0, 1) == "-") {
|
||||
switch(args[i]) {
|
||||
case "-u":
|
||||
this.flags['u'] = true;
|
||||
break;
|
||||
default:
|
||||
this.files['stdout'].write("cat: Unknown parameter " + args[i] + "\n");
|
||||
this.exit(2);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
args.splice(0, i);
|
||||
|
||||
if (args.length == 0) {
|
||||
console.log("cat: found stdin");
|
||||
this.file = this.files['stdin'];
|
||||
} else {
|
||||
var env = this.Environment;
|
||||
var name = args[1];
|
||||
var name = args[0];
|
||||
if (name.substring(0, 1) != "/")
|
||||
name = env.array['PWD'] + "/" + name;
|
||||
name = env.global['PWD'] + "/" + name;
|
||||
name = Kernel.Filesystem.shortenPath(name);
|
||||
console.log("cat: found " + name);
|
||||
var file = new File(name);
|
||||
|
@ -28,18 +49,34 @@ CatClass.prototype.main = function(args) {
|
|||
}
|
||||
CatClass.prototype.tick = function() {
|
||||
var stdout = this.files['stdout'];
|
||||
var text = this.file.read(200, this.index);
|
||||
for (var i = 0; i < text.length; i++) {
|
||||
if (KeyCodes.isEnter(text.charCodeAt(i)) || (text.charAt(i) == "\n")) {
|
||||
stdout.write(this.buffer.join(""));
|
||||
this.buffer = new Array();
|
||||
}
|
||||
if (text.charAt(i) == EOF) {
|
||||
stdout.write(this.buffer.join(""));
|
||||
this.buffer = new Array();
|
||||
|
||||
if (this.flags['u']) {
|
||||
var text = this.file.read(1, this.index);
|
||||
stdout.write(text);
|
||||
if (text.charAt(i) == EOF)
|
||||
this.exit(0);
|
||||
} else {
|
||||
var text = this.file.read(200, this.index);
|
||||
|
||||
for (var i = 0; i < text.length; i++) {
|
||||
if (this.buffer.length >= 300) {
|
||||
// because we need a maximal buffer size
|
||||
stdout.write(this.buffer.join(""));
|
||||
this.buffer = new Array();
|
||||
}
|
||||
if (KeyCodes.isEnter(text.charCodeAt(i))) {
|
||||
this.buffer.push(text.charAt(i));
|
||||
stdout.write(this.buffer.join(""));
|
||||
this.buffer = new Array();
|
||||
continue;
|
||||
}
|
||||
if (text.charAt(i) == EOF) {
|
||||
stdout.write(this.buffer.join(""));
|
||||
this.buffer = new Array();
|
||||
this.exit(0);
|
||||
}
|
||||
this.buffer.push(text.charAt(i));
|
||||
}
|
||||
this.buffer.push(text.charAt(i));
|
||||
this.index += 200;
|
||||
}
|
||||
this.index += 100;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue