From cf91d6c709f2736742dc1217b17dc864ea1fa8b5 Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Thu, 30 Apr 2020 11:51:41 -0700 Subject: [PATCH] 1. Fixed bug with os.tmpdir() 2. Updated ScriptMode, so current durrectory is not changed to script path 3. Fixed process.cwd() on windows to support UTF8 --- meshcore/agentcore.c | 12 ----- microscript/ILibDuktape_ScriptContainer.c | 23 +++++---- modules/MSH_Installer.js | 58 ++++++++++++++++++++++- 3 files changed, 72 insertions(+), 21 deletions(-) diff --git a/meshcore/agentcore.c b/meshcore/agentcore.c index 0cf0c13..c18989d 100644 --- a/meshcore/agentcore.c +++ b/meshcore/agentcore.c @@ -4584,18 +4584,6 @@ void MeshAgent_ScriptMode(MeshAgentHostContainer *agentHost, int argc, char **ar strncpy_s(jsPath, ILibMemory_GetExtraMemorySize(jsPath), ILibScratchPad2, ILibMemory_GetExtraMemorySize(jsPath)); scriptArgs[0] = jsPath; -#ifdef WIN32 - i = ILibString_LastIndexOf(ILibScratchPad2, pathLen, "\\", 1); -#else - i = ILibString_LastIndexOf(ILibScratchPad2, pathLen, "/", 1); -#endif - ILibScratchPad2[i] = 0; -#ifdef WIN32 - SetCurrentDirectory(ILibScratchPad2); -#else - ignore_result(chdir(ILibScratchPad2)); -#endif - // Parse arguments. Handle the ones we can, others will be passed to the JavaScript engine. for (i = 2; i < argc; ++i) { diff --git a/microscript/ILibDuktape_ScriptContainer.c b/microscript/ILibDuktape_ScriptContainer.c index 1ba8bd0..8d364d4 100644 --- a/microscript/ILibDuktape_ScriptContainer.c +++ b/microscript/ILibDuktape_ScriptContainer.c @@ -863,16 +863,23 @@ duk_ret_t ILibDuktape_ScriptContainer_Process_Kill(duk_context *ctx) duk_ret_t ILibDuktape_Process_cwd(duk_context *ctx) { #ifdef WIN32 - GetCurrentDirectoryA((DWORD)sizeof(ILibScratchPad), ILibScratchPad); - duk_push_string(ctx, ILibScratchPad); - return(1); -#elif defined(_POSIX) + GetCurrentDirectoryW((DWORD)sizeof(ILibScratchPad)/2, (LPWSTR)ILibScratchPad); + ILibDuktape_String_PushWideString(ctx, ILibScratchPad, 0); +#else ignore_result((uintptr_t)getcwd(ILibScratchPad, sizeof(ILibScratchPad))); duk_push_string(ctx, ILibScratchPad); - return(1); -#else - return(ILibDuktape_Error(ctx, "Error")); #endif + + duk_get_prop_string(ctx, -1, "concat"); // [string][concat] + duk_swap_top(ctx, -2); // [concat][this] +#ifdef WIN32 + duk_push_string(ctx, "\\"); +#else + duk_push_string(ctx, "/"); +#endif + + duk_call_method(ctx, 1); + return(1); } #ifdef _POSIX @@ -1822,7 +1829,7 @@ duk_ret_t ILibDuktape_tmpdir(duk_context *ctx) #ifdef WIN32 WCHAR tmp[1024]; if (GetTempPathW(sizeof(tmp) / 2, (LPWSTR)tmp) == 0) { return(ILibDuktape_Error(ctx, "Error getting temp folder")); } - ILibDuktape_String_PushWideString(ctx, (char*)tmp, -1); + ILibDuktape_String_PushWideString(ctx, (char*)tmp, 0); #elif defined (_POSIX) #if defined(__APPLE__) duk_eval_string(ctx, "process.env['TMPDIR']"); diff --git a/modules/MSH_Installer.js b/modules/MSH_Installer.js index c6b0bc5..1d441ae 100644 --- a/modules/MSH_Installer.js +++ b/modules/MSH_Installer.js @@ -149,4 +149,60 @@ function addMsh(options) } } -module.exports = addMsh; +try +{ + module.exports = addMsh; +} +catch(e) +{ + // We were run from the command line + + var outputFile = null; + var inputFile = null; + var msh = null; + + for (var i = 1; i < process.argv.length; i += 2) + { + switch (process.argv[i]) + { + case '-o': + outputFile = process.argv[i + 1]; + break; + case '-i': + inputFile = process.argv[i + 1]; + break; + default: + console.log('unrecognized parameter: ' + process.argv[i]); + break; + } + } + + + if (process.argv.length != 5 || outputFile == null || inputFile == null) + { + + console.log('usage: ' + process.execPath.split(process.platform == 'win32' ? '\\' : '/').pop() + ' MSH_Installer.js -o outputFile -i mshFile'); + process.exit(); + } + + try + { + msh = require('fs').readFileSync(inputFile); + } + catch(e) + { + console.log('Unable to read ' + inputFile, e); + process.exit(); + } + + var options = + { + destinationStream: require('fs').createWriteStream(outputFile, { flags: 'wb' }), + sourceFileName: process.execPath, + msh: msh + }; + + console.log('Creating MSH integrated binary...'); + options.destinationStream.on('close', function () { console.log('DONE'); process.exit(); }); + addMsh(options); +}