From 7071a63dfa1112cc1c6dde0216ebc8d7ddd9ba2b Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Tue, 19 Dec 2023 14:36:09 +1000 Subject: [PATCH] convert format of programs to always have main(argc,argv) --- clite/commands.js | 1207 ++++++++++++++++++++++++--------------------- clite/shell.js | 32 +- clite/user.js | 88 ++-- clite/vi.js | 88 ++-- 4 files changed, 744 insertions(+), 671 deletions(-) diff --git a/clite/commands.js b/clite/commands.js index a106bea..b9661be 100644 --- a/clite/commands.js +++ b/clite/commands.js @@ -3,7 +3,9 @@ clite.commands.data = function() { clite.commands.load('help',function(args,env,io) { var stdio = io.include('stdio'); - stdio.printf(` + + function main(argc,argv) { + stdio.printf(` Welcome to CLIte (pronounced like 'site' with an added L). Various common Unix commands are available in the CLIte shell: @@ -22,19 +24,24 @@ As a basic guide for guests: Use ls to see what content is available, eg: ls web Use less to view the content, eg: less web/about `); - return 0; + return 0; + } + + return main(args.length,args); }); clite.commands.load('ls',function(args,env,io) { + var stdlib = io.include('stdlib'); + var stdio = io.include('stdio'); + var time = io.include('time'); + var clite = io.include('clite'); + var dirs = []; var long = false; var all = false; var access = false; var one = false; - var stdlib = io.include('stdlib'); - var stdio = io.include('stdio'); - var time = io.include('time'); - var clite = io.include('clite'); + function help() { stdio.printf(` ls - list directory contents @@ -94,85 +101,91 @@ Options: } } - for (var i=1; i 1) - stdio.printf('%s:\n',dir); - var st = stdio.lstat(dir); - if (!st) { - stdio.fprintf(io.stderr,'cannot read file: %s\n',dir); + dirs.forEach(function(dir,index) { + if (dirs.length > 1) + stdio.printf('%s:\n',dir); + var st = stdio.lstat(dir); + if (!st) { + stdio.fprintf(io.stderr,'cannot read file: %s\n',dir); + stdio.close(fd); + return; + } + if (st.type != stdio.types.FT_DIR) { + writeNodeData(st,dir); + if (!one && !long) + stdio.write(io.stdout,'\n'); + return; + } + var fd = stdio.open(dir,stdio.flags.O_RDONLY|stdio.flags.O_SYNC); + if (!fd) { + stdio.fprintf(io.stderr,'cannot open directory: %s\n',dir); + return; + } + var e; + while ((e = stdio.read(fd)) != null) { + if (e[0] == '.' && !all) + continue; + var est = stdio.lstat(dir+'/'+e,stdio.flags.AT_SYMLINK_NOFOLLOW); + if (!est) { + stdio.fprintf(io.stderr,'cannot read content: %s\n',dir); + break; + } + writeNodeData(est,dir+'/'+e); + } stdio.close(fd); - return; - } - if (st.type != stdio.types.FT_DIR) { - writeNodeData(st,dir); if (!one && !long) stdio.write(io.stdout,'\n'); - return; - } - var fd = stdio.open(dir,stdio.flags.O_RDONLY|stdio.flags.O_SYNC); - if (!fd) { - stdio.fprintf(io.stderr,'cannot open directory: %s\n',dir); - return; - } - var e; - while ((e = stdio.read(fd)) != null) { - if (e[0] == '.' && !all) - continue; - var est = stdio.lstat(dir+'/'+e,stdio.flags.AT_SYMLINK_NOFOLLOW); - if (!est) { - stdio.fprintf(io.stderr,'cannot read content: %s\n',dir); - break; - } - writeNodeData(est,dir+'/'+e); - } - stdio.close(fd); - if (!one && !long) - stdio.write(io.stdout,'\n'); - }); + }); - return 0; + return 0; + } + + return main(args.length,args); }); clite.commands.load('cat',function(args,env,io) { + var stdlib = io.include('stdlib'); + var stdio = io.include('stdio'); + var clite = io.include('clite'); + var files = []; var download = false; var remote = true; var pending = 0; - var stdlib = io.include('stdlib'); - var stdio = io.include('stdio'); - var clite = io.include('clite'); function help() { stdio.printf(` @@ -186,40 +199,6 @@ Options: `); } - for (var i=1; i 0) + if (argv[1].length > 0) return 0; return 1; } - if (args.length > 5) + if (argc > 5) return 1; - var uargs = Array.from(args); + var uargs = Array.from(argv); uargs.shift(); - if (args[0] == '[' && uargs[uargs.length-1] == ']') + if (argv[0] == '[' && uargs[uargs.length-1] == ']') uargs.pop(); if (uargs[0] == '!') { @@ -2065,7 +2127,7 @@ Options: } - return main(args); + return main(args.length,args); }); clite.commands.load('load',function(args,env,io) { @@ -2116,21 +2178,21 @@ Options: io.exit(0); } - function main(args) { - for (var i=1; i