Merge pull request #82 from gnh1201/pipe-ipc-shell

Enhance the shell interface with PIPE IPC
This commit is contained in:
Namhyeon Go 2023-09-20 16:48:11 +09:00 committed by GitHub
commit 44a9a32775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 60 deletions

View File

@ -69,6 +69,20 @@ function createADO() {
return CreateObject("ADODB.Stream"); return CreateObject("ADODB.Stream");
} }
function AdoConvertToStream(ado, position) {
position = (position !== "undefined" ? position : 0);
var newAdo = createADO();
newAdo.Type = adTypeBinary;
newAdo.Mode = adModeReadWrite;
newAdo.Open();
ado.Position = position;
ado.CopyTo(newAdo);
ado.Flush();
ado.Close();
return newAdo;
}
function PipeIPC() { function PipeIPC() {
this.path = "data\\.pipe_:pipename"; this.path = "data\\.pipe_:pipename";
this.delimiter = "\r\n"; this.delimiter = "\r\n";
@ -147,13 +161,15 @@ function PipeIPC() {
this.tmpfile = this.savefile + ".tmp"; this.tmpfile = this.savefile + ".tmp";
// read a text from save file // read a text from save file
var isExistsSaveFile = createFSO().FileExists(this.savefile); var isExistsTmpFile = createFSO().FileExists(this.tmpfile);
if (isExistsSaveFile) { if (!isExistsTmpFile) {
var handler = createFSO().OpenTextFile(this.tmpfile, ForWriting, true, TristateTrue); var handler = createFSO().OpenTextFile(this.tmpfile, ForWriting, true, TristateTrue);
handler.Write(this.readTextFromFile(filename)); handler.Write(this.readTextFromFile(this.savefile));
handler.Close(); handler.Close();
} }
// with iomode (default: ForAppending)
iomode = (iomode !== "undefined" ? ForAppending : iomode);
this.openRecorder(iomode); this.openRecorder(iomode);
}; };
@ -181,7 +197,10 @@ function PipeIPC() {
this.writer.Write(message + this.delimiter); this.writer.Write(message + this.delimiter);
}; };
this.write = function(message) { this.write = function(message, iomode) {
// with iomode (default: ForAppending)
iomode = (iomode !== "undefined" ? ForAppending : iomode);
if (this.savefile != null) { if (this.savefile != null) {
this.record(message); this.record(message);
} }
@ -190,7 +209,7 @@ function PipeIPC() {
while (!isWritten) { while (!isWritten) {
try { try {
this.flush(); this.flush();
this.openWriter(ForAppending); this.openWriter(iomode);
this._write(message); this._write(message);
this.closeWriter(); this.closeWriter();
isWritten = true; isWritten = true;
@ -232,19 +251,6 @@ function PipeIPC() {
var charset = this.charset; var charset = this.charset;
var isCommited = false; var isCommited = false;
// define functions
var StripAdoBOM = function(adoObj) {
var newAdoObj = createADO();
newAdoObj.Type = adTypeBinary;
newAdoObj.Mode = adModeReadWrite;
newAdoObj.Open();
adoObj.Position = 3;
adoObj.CopyTo(newAdoObj);
adoObj.Flush();
adoObj.Close();
return newAdoObj;
};
while (!isCommited) { while (!isCommited) {
try { try {
// Open a temporary file // Open a temporary file
@ -263,13 +269,13 @@ function PipeIPC() {
str = text; str = text;
} }
// Convert UTF-16 BOM to UTF-8 // Convert UTF-16 BOM to a character set
var ado = createADO(); var ado = createADO();
ado.Type = adTypeText; ado.Type = adTypeText;
ado.Charset = charset; ado.Charset = charset;
ado.Open(); ado.Open();
ado.WriteText(str); ado.WriteText(str);
ado = StripAdoBOM(ado); ado = AdoConvertToStream(ado, 3);
ado.SaveToFile(dst, adSaveCreateOverWrite); ado.SaveToFile(dst, adSaveCreateOverWrite);
ado.Close(); ado.Close();
@ -345,23 +351,23 @@ function PipeIPC() {
}; };
this.readTextFromFile = function(filename) { this.readTextFromFile = function(filename, charset) {
var text = ''; var text = '';
var isLoaded = false;
var isFileExists = createFSO().FileExists(filename); var isFileExists = createFSO().FileExists(filename);
if (isFileExists) { if (isFileExists) {
//console.info("File", filename, "exists"); var isRead = false;
while (!isLoaded) { while (!isRead) {
try { try {
var ado = createADO(); var ado = createADO();
ado.CharSet = this.charset; ado.CharSet = charset;
ado.Open(); ado.Open();
ado.LoadFromFile(filename); ado.LoadFromFile(filename);
text += ado.ReadText(); text += ado.ReadText();
ado.Close(); ado.Close();
isLoaded = true; isRead = true;
} catch (e) { } catch (e) {
isLoaded = false; isRead = false;
} }
} }
} else { } else {
@ -372,8 +378,8 @@ function PipeIPC() {
}; };
this.loadFromFile = function(filename) { this.loadFromFile = function(filename, charset) {
this.write(this.readTextFromFile(filename)); this.write(this.readTextFromFile(filename, charset), ForWriting);
}; };
} }
@ -401,7 +407,7 @@ exports.adSaveCreateNotExist = adSaveCreateNotExist;
exports.adSaveCreateOverWrite = adSaveCreateOverWrite; exports.adSaveCreateOverWrite = adSaveCreateOverWrite;
exports.adModeReadWrite = adModeReadWrite; exports.adModeReadWrite = adModeReadWrite;
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.8"; exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.10";
exports.AUTHOR = "abuse@catswords.net"; exports.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = require; exports.require = require;

View File

@ -1,10 +1,8 @@
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// Shell API // Shell API with PIPE-IPC
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file"); var FILE = require("lib/file");
var RAND = require("lib/rand"); var PipeIPC = require("lib/pipe-ipc");
var SEED = RAND.getSeed();
var ShellObject = function() { var ShellObject = function() {
this.interface = null; this.interface = null;
@ -14,7 +12,7 @@ var ShellObject = function() {
this.isElevated = false; this.isElevated = false;
this.isFork = false; this.isFork = false;
this.isVisibleWindow = false; this.isVisibleWindow = false;
this.charset = "utf-8"; this.charset = PipeIPC.CdoUS_ASCII;
this.stdout = null; this.stdout = null;
this.stderr = null; this.stderr = null;
@ -28,6 +26,12 @@ var ShellObject = function() {
} }
return this; return this;
}; };
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
this.setWorkingDirectory = function(dirname) { this.setWorkingDirectory = function(dirname) {
if (typeof(dirname) === "string") { if (typeof(dirname) === "string") {
@ -72,34 +76,40 @@ var ShellObject = function() {
}; };
this.exec = function(cmd, stdOutPath, stdErrPath) { this.exec = function(cmd, stdOutPath, stdErrPath) {
var stdout, stderr; this.stdout = PipeIPC.connect("volatile");
var stdOutPath = (typeof(stdOutPath) === "undefined" ? "tmp\\stdout_" + SEED + ".txt" : stdOutPath); this.stderr = PipeIPC.connect("volatile");
var stdErrPath = (typeof(stdErrPath) === "undefined" ? "tmp\\stderr_" + SEED + ".txt" : stdErrPath);
var c = "%comspec% /c (" + this.build(cmd) + ") 1> " + stdOutPath; this.stdout.flush();
this.stderr.flush();
if (typeof stdOutPath === "string")
this.stdout.startRecorder(stdOutPath, PipeIPC.ForWriting);
if (typeof stdErrPath === "string")
this.stderr.startRecorder(stdErrPath, PipeIPC.ForWriting);
var c = "%comspec% /c (" + this.build(cmd) + ") 1> " + this.stdout.path;
//c += " 2>&1"; //c += " 2>&1";
c += " 2> " + stdErrPath; c += " 2> " + this.stderr.path;
this.interface.Run(c, 0, true); this.interface.Run(c, 0, true);
console.log("ShellObject.exec() ->", c); console.log("ShellObject.exec() ->", c);
sleep(1); sleep(1);
if (FILE.fileExists(stdOutPath)) { this.stdout.loadFromFile(this.stdout.path, this.charset);
stdout = FILE.readFile(stdOutPath, this.charset); this.stderr.loadFromFile(this.stderr.path, this.charset);
FILE.deleteFile(stdOutPath);
}
if (FILE.fileExists(stdErrPath)) { var stdout = (function(s) {
stderr = FILE.readFile(stdErrPath, this.charset); return s.length > 3 ? s.substring(3) : '';
FILE.deleteFile(stdErrPath); })(this.stdout.read());
} var stderr = (function(s) {
return s.length > 3 ? s.substring(3) : '';
})(this.stderr.read());
this.stdout = stdout;
this.stderr = stderr;
console.log(c);
//console.log("[stdout] " + stdout); //console.log("[stdout] " + stdout);
//console.log("[stderr] " + stderr); //console.log("[stderr] " + stderr);
this.stdout.destroy();
this.stderr.destroy();
return stdout; return stdout;
}; };
@ -150,11 +160,6 @@ var ShellObject = function() {
this.interface = null; this.interface = null;
}; };
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
this.create(); this.create();
}; };
@ -166,8 +171,8 @@ exports.build = function(cmd) {
return (new ShellObject()).build(cmd); return (new ShellObject()).build(cmd);
}; };
exports.exec = function(cmd, stdOutPath) { exports.exec = function(cmd, stdOutPath, stdErrPath) {
return (new ShellObject()).exec(cmd, stdOutPath); return (new ShellObject()).setCharset(PipeIPC.CdoEUC_KR).exec(cmd, stdOutPath, stdErrPath);
}; };
exports.run = function(cmd, fork) { exports.run = function(cmd, fork) {
@ -201,6 +206,7 @@ exports.getPathOfMyDocuments = function() {
return (new ShellObject()).getPathOfMyDocuments(); return (new ShellObject()).getPathOfMyDocuments();
}; };
exports.VERSIONINFO = "Shell interface (shell.js) version 0.3.1"; exports.VERSIONINFO = "Shell interface (shell.js) version 0.3.2";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;