From 7ceb503feeb87821402c916f7dc6c736144f2994 Mon Sep 17 00:00:00 2001 From: Lisa Milne Date: Sun, 17 Dec 2023 14:57:15 +1000 Subject: [PATCH] add the start of shell history saving --- clite/shell.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/clite/shell.js b/clite/shell.js index 21ee312..63de40f 100644 --- a/clite/shell.js +++ b/clite/shell.js @@ -89,11 +89,10 @@ clite.commands.load('sh',function(args,env,io) { return 1; }, exit:function(args,io) { - has_exited = true; var ev = 0; if (args.length > 1) ev = parseInt(args[1]); - io.exit(ev); + doShellExit(ev); return 0; } }; @@ -102,6 +101,27 @@ clite.commands.load('sh',function(args,env,io) { data:[], current:'', index:0, + load:function() { + let path = clite.resolvePath('~/.shhistory'); + let fd = stdio.open(path,stdio.flags.O_RDONLY|stdio.flags.O_SYNC); + if (!fd) + return; + var e; + while ((e = stdio.readLine(fd)) != null) { + history.add(e); + } + stdio.close(fd); + }, + save:function() { + let path = clite.resolvePath('~/.shhistory'); + let fd = stdio.open(path,stdio.flags.O_WRONLY|stdio.flags.O_TRUNC|stdio.flags.O_CREAT|stdio.flags.O_SYNC); + if (!fd) + return; + history.data.forEach(function(l) { + stdio.write(fd,l+'\n'); + }); + stdio.close(fd); + }, add:function(txt) { if (txt.length < 1) return; @@ -816,6 +836,13 @@ Options: `); } + function doShellExit(v) { + history.save(); + stdio.write(io.stdout,'\n'); + has_exited = true; + io.exit(v); + } + function resolvePATH(txt) { // special case of test command if (txt == '[') @@ -953,6 +980,10 @@ Options: function inputProcess(str) { switch (str) { + case String.fromCharCode(4): + case 4: + doShellExit(0); + break; case '\t': // tab case 9: var ct = tabfill(c); @@ -1075,6 +1106,7 @@ Options: parser.queue(env.HOME+'/.shrc'); parser.run(function() { history.clear(); + history.load(); inputRead(); }); }