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");
}
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() {
this.path = "data\\.pipe_:pipename";
this.delimiter = "\r\n";
@ -147,13 +161,15 @@ function PipeIPC() {
this.tmpfile = this.savefile + ".tmp";
// read a text from save file
var isExistsSaveFile = createFSO().FileExists(this.savefile);
if (isExistsSaveFile) {
var isExistsTmpFile = createFSO().FileExists(this.tmpfile);
if (!isExistsTmpFile) {
var handler = createFSO().OpenTextFile(this.tmpfile, ForWriting, true, TristateTrue);
handler.Write(this.readTextFromFile(filename));
handler.Write(this.readTextFromFile(this.savefile));
handler.Close();
}
// with iomode (default: ForAppending)
iomode = (iomode !== "undefined" ? ForAppending : iomode);
this.openRecorder(iomode);
};
@ -181,7 +197,10 @@ function PipeIPC() {
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) {
this.record(message);
}
@ -190,7 +209,7 @@ function PipeIPC() {
while (!isWritten) {
try {
this.flush();
this.openWriter(ForAppending);
this.openWriter(iomode);
this._write(message);
this.closeWriter();
isWritten = true;
@ -232,19 +251,6 @@ function PipeIPC() {
var charset = this.charset;
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) {
try {
// Open a temporary file
@ -263,13 +269,13 @@ function PipeIPC() {
str = text;
}
// Convert UTF-16 BOM to UTF-8
// Convert UTF-16 BOM to a character set
var ado = createADO();
ado.Type = adTypeText;
ado.Charset = charset;
ado.Open();
ado.WriteText(str);
ado = StripAdoBOM(ado);
ado = AdoConvertToStream(ado, 3);
ado.SaveToFile(dst, adSaveCreateOverWrite);
ado.Close();
@ -345,23 +351,23 @@ function PipeIPC() {
};
this.readTextFromFile = function(filename) {
this.readTextFromFile = function(filename, charset) {
var text = '';
var isLoaded = false;
var isFileExists = createFSO().FileExists(filename);
if (isFileExists) {
//console.info("File", filename, "exists");
while (!isLoaded) {
var isRead = false;
while (!isRead) {
try {
var ado = createADO();
ado.CharSet = this.charset;
ado.CharSet = charset;
ado.Open();
ado.LoadFromFile(filename);
text += ado.ReadText();
ado.Close();
isLoaded = true;
isRead = true;
} catch (e) {
isLoaded = false;
isRead = false;
}
}
} else {
@ -372,8 +378,8 @@ function PipeIPC() {
};
this.loadFromFile = function(filename) {
this.write(this.readTextFromFile(filename));
this.loadFromFile = function(filename, charset) {
this.write(this.readTextFromFile(filename, charset), ForWriting);
};
}
@ -401,7 +407,7 @@ exports.adSaveCreateNotExist = adSaveCreateNotExist;
exports.adSaveCreateOverWrite = adSaveCreateOverWrite;
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.global = global;
exports.require = require;

View File

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