add command for loading files from the local device

This commit is contained in:
Lisa Milne 2023-12-15 19:53:50 +10:00
parent c52058dacb
commit 279fc3c8aa
4 changed files with 114 additions and 8 deletions

View file

@ -45,9 +45,12 @@ var bios = {
script.onload = callback;
head.appendChild(script);
},
localfile:function(name,callback) {
bios.io.write(3,{file:name,fn:callback});
},
file:function(name,callback) {
if (window.location.protocol == 'file:') {
bios.io.write(3,{file:name,fn:callback});
bios.core.load.localfile(name,callback);
return;
}

View file

@ -1978,6 +1978,98 @@ Options:
return main(args);
});
clite.commands.load('load',function(args,env,io) {
var stdio = io.include('stdio');
var clite = io.include('clite');
var short = null;
var file = null;
function help() {
stdio.printf(`
load - load a text file from the local device
Usage: load [OPTION] <FILE>
Options:
-? Print this help information
`);
}
function fcb(data) {
if (data == null) {
stdio.write(io.stderr,'no file received\n');
io.exit(1);
return;
}
if (typeof data != string) {
stdio.write(io.stderr,'not a normal file\n');
io.exit(1);
return;
}
var fd = stdio.open(file,stdio.flags.O_WRONLY|stdio.flags.O_CREAT|stdio.flags.O_EXCL|stdio.flags.O_SYNC);
if (!fd) {
stdio.write(io.stderr,'could not create file: %s\n',file);
io.exit(1);
return;
}
if (!stdio.write(fd,data)) {
stdio.close(fd);
stdio.write(io.stderr,'could not write file: %s\n',file);
io.exit(1);
return;
}
stdio.close(fd);
io.exit(0);
}
function main(args) {
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:
stdio.fprintf(io.stderr,'unknown argument: -%c\n',args[i][j]);
}
}
}else{
short = args[i];
file = clite.resolvePath(short);
}
}
if (!file) {
stdio.write(io.stderr,'no file specified\n');
return 1;
}
var fd = stdio.open('/dev/local',stdio.flags.O_RDWR|stdio.flags.O_SYNC);
if (!fd) {
stdio.write(io.stderr,'could not open local device for reading\n');
return 1;
}
if (!stdio.read(fd,fcb)) {
stdio.close(fd);
stdio.write(io.stderr,'could not read from local device\n');
return 1;
}
stdio.close(fd);
return null;
}
return main(args);
});
if (window.location.protocol == 'file:')
clite.commands.load('exp',function(args,env,io) {
var stdio = io.include('stdio');

View file

@ -113,7 +113,10 @@ var clite = {
return false;
}
n.data.content = {
read:null,
read:function(cb) {
clite.state.bios.core.load.localfile('',cb);
return true;
},
write:function(obj) {
if (typeof obj === 'string') {
var n = 'data.txt';
@ -1215,10 +1218,18 @@ clite.io = {
return false;
}else if (fd.node.data.isdev) {
if (typeof fd.node.data.content !== 'string') {
try{
return fd.node.data.content.read();
} catch(err) {
return null;
if (typeof cb === 'function' && fd.node.data.content.read.length == 1) {
try{
return fd.node.data.content.read(cb);
} catch(err) {
return null;
}
}else{
try{
return fd.node.data.content.read();
} catch(err) {
return null;
}
}
}
}

View file

@ -100,13 +100,13 @@ clite.libs.load('libtime','time',function(io,env) {
out += (y/100).toFixed(0);
break;
case 'd': //Day of the month as a decimal number [01,31].
out += (tm.tm_mday+1).toFixed(0).padStart(2,'0');
out += (tm.tm_mday).toFixed(0).padStart(2,'0');
break;
case 'D': //Date in the format mm/dd/yy.
out += getDTStr('%M/%D/%y',tm);
break;
case 'e': //Day of the month as a decimal number [1,31] in a two-digit field with leading <space> character fill.
out += (tm.tm_mday+1).toFixed(0).padStart(2,' ');
out += (tm.tm_mday).toFixed(0).padStart(2,' ');
break;
case 'H': //Hour (24-hour clock) as a decimal number [00,23].
out += (tm.tm_hour).toFixed(0).padStart(2,'0');