From 279fc3c8aa6e0e5cba4fa7cc17ca6458a4ebf0d5 Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Fri, 15 Dec 2023 19:53:50 +1000 Subject: [PATCH] add command for loading files from the local device --- clite/bios.js | 5 ++- clite/commands.js | 92 +++++++++++++++++++++++++++++++++++++++++++++++ clite/core.js | 21 ++++++++--- clite/libtime.js | 4 +-- 4 files changed, 114 insertions(+), 8 deletions(-) diff --git a/clite/bios.js b/clite/bios.js index 58a6fe1..7588ded 100644 --- a/clite/bios.js +++ b/clite/bios.js @@ -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; } diff --git a/clite/commands.js b/clite/commands.js index f23b6b2..afc6e23 100644 --- a/clite/commands.js +++ b/clite/commands.js @@ -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] + +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 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');