// // popen.ms - 擬似パイプを作成する // $Id: popen.msl,v 1.2 2002/06/07 03:53:39 monnai Exp monnai $ // //【使い方】var pipe = File.popen(cmd [, folder [, mode]]); // cmd - 実行したいコマンド // folder - cmd を何処で実行するかのフォルダパス // mode - "r", "rb", "w", "wb" のいずれか、省略すると"rb" // //【例】 // var pipe = File.popen("moca calendar.ms", "D:\\sample"); // if (pipe) // { // while (!pipe.eof()) // { // var line = pipe.readln(); // write(line); // } // pipe.close(); // } // File.popen = function(cmd, folder, mode) { if (!cmd) return null; var file = File.createTemporary(); if (!file) return null; file.cmd = cmd; file.folder = folder; file._close = file.close; // 擬似パイプ用にテンポラリファイルの後片付けもするclose()メソッドをオーバーライドする。 file.close = function() { if (!this._close()) return false; // コマンドへの出力ストリームならば、ここでコマンドを実行する。 var ok = this.reading || runCommand(this.cmd, this.folder, this, null) >= 0; // テンポラリファイルの後始末をして終了。 this.remove(); return ok; }; file.abort = function() { // 中止したときもテンポラリファイルの後始末をしましょう。 this._close(); this.remove(); }; if (mode && mode.indexOf("w") >= 0) { // 出力ストリーム -> コマンドのSTDIN file.reading = false; if (file.open(mode)) return file; } else { // コマンドのSTDOUT -> 入力ストリーム file.reading = true; if (runCommand(cmd, folder, null, file) >= 0 && file.open(mode || "rb")) return file; } // NOTE: File.remove() はファイルが開いていたら閉じてから削除する。 file.remove(); return null; };