home *** CD-ROM | disk | FTP | other *** search
- class com.neodelight.flanix.Console
- {
- var width;
- var height;
- var offsetX;
- var offsetY;
- var inputHeight;
- var outputHeight;
- var outputMaxLines;
- var cols;
- var mc;
- var output;
- var input;
- var history;
- var format;
- var cursorMc;
- var codepage;
- var active;
- var parameters;
- var flags;
- static var COLOR_LINE = 65280;
- static var COLOR_BG = 2236962;
- static var COLOR_BG_ALPHA = 100;
- static var COLOR_TEXT = 65280;
- var padding = 5;
- var spacing = 5;
- var promptVar = ">";
- var historyLen = 100;
- var historyPtr = -1;
- var cursorPos = 0;
- var doBufStdOut = false;
- var bufStdOut = "";
- var ram = {CWD:"/",PATH:"/bin:."};
- function Console(width, height)
- {
- if(_global.console)
- {
- trace("#ERROR: Console(): only one instance of Console allowed!");
- }
- else
- {
- com.neodelight.flanix.Kernel.init();
- _global.console = this;
- this.width = !width ? 550 : width;
- this.height = !height ? 400 : height;
- this.offsetX = 0;
- this.offsetY = 0;
- this.inputHeight = 18;
- this.outputHeight = this.height - this.padding * 2 - this.spacing - this.inputHeight;
- this.outputMaxLines = 50;
- this.cols = Math.floor((this.width - this.padding * 2) / 8) - 1;
- this.mc = _root.createEmptyMovieClip(com.neodelight.std.Unique.getKey(),_root.getNextHighestDepth());
- this.mc.createTextField("output",this.mc.getNextHighestDepth(),this.padding,this.padding,this.width - this.padding * 2,this.outputHeight);
- this.output = this.mc.output;
- this.mc.createTextField("input",this.mc.getNextHighestDepth(),this.padding,this.padding + this.outputHeight + this.spacing,this.width - this.padding * 2,20);
- this.input = this.mc.input;
- this.input.text = this.promptVar;
- this.input.addListener(this);
- Key.addListener(this);
- this.history = new Array();
- this.format = new TextFormat();
- this.format.color = com.neodelight.flanix.Console.COLOR_TEXT;
- this.format.font = "Courier";
- this.format.size = 10;
- this.input.setNewTextFormat(this.format);
- this.input.setTextFormat(this.format);
- this.output.setTextFormat(this.format);
- this.output.setNewTextFormat(this.format);
- this.output.wordWrap = true;
- this.cursorMc = this.mc.createEmptyMovieClip(com.neodelight.std.Unique.getKey(),this.mc.getNextHighestDepth());
- this.cursorMc.lineStyle(0,com.neodelight.flanix.Console.COLOR_TEXT);
- this.cursorMc.moveTo(0,0);
- this.cursorMc.lineTo(8,0);
- setInterval(function(cursorMc)
- {
- cursorMc._visible = !cursorMc._visible;
- }
- ,200,this.cursorMc);
- this.cursorMc._y = this.height - this.padding - 2 + this.offsetY;
- this.updateCursor();
- this.codepage = new Array();
- this.codepage[221] = "┬┤";
- this.codepage[304] = "=";
- this.codepage[306] = "\"";
- this.codepage[307] = "§";
- this.codepage[310] = "&";
- this.codepage[311] = "/";
- this.codepage[312] = "(";
- this.codepage[313] = ")";
- this.codepage[443] = "*";
- this.codepage[444] = ";";
- this.codepage[446] = ":";
- this.codepage[447] = "\'";
- this.codepage[475] = "?";
- this.codepage[477] = "`";
- this.codepage[482] = ">";
- this.codepage[1584] = "}";
- this.codepage[1591] = "{";
- this.codepage[1592] = "[";
- this.codepage[1593] = "]";
- this.codepage[1617] = "@";
- this.codepage[1723] = "~";
- this.codepage[1755] = "\\";
- this.codepage[1762] = "|";
- this.redraw();
- this.stdout("Neodelight Flash Console " + _global.$FLANIX_VERSION);
- this.stdout("OK.");
- this.active = false;
- this.mc._visible = false;
- }
- }
- function show()
- {
- this.active = true;
- this.mc._visible = true;
- this.mc.swapDepths(_root.getNextHighestDepth());
- }
- function hide()
- {
- this.active = false;
- this.mc._visible = false;
- }
- function echo(msg)
- {
- if(msg == undefined)
- {
- msg = "";
- }
- if(this.output.maxscroll > this.outputMaxLines)
- {
- var _loc2_ = this.output.text.split(String.fromCharCode(13));
- _loc2_.splice(0,_loc2_.length - this.outputMaxLines);
- this.output.text = _loc2_.join("\n");
- }
- else if(this.output.text != "")
- {
- }
- this.output.text += "\n";
- this.output.text += msg;
- this.output.scroll = this.output.maxscroll;
- }
- function stdout(msg)
- {
- if(this.doBufStdOut)
- {
- this.bufStdOut += msg;
- }
- else
- {
- this.echo(msg);
- }
- }
- function flushStdOut()
- {
- this.echo(this.bufStdOut);
- this.bufStdOut = "";
- }
- function stderr(msg)
- {
- this.echo(msg);
- }
- function setExitCode(code)
- {
- this.ram["?"] = code;
- }
- function cls()
- {
- this.output.text = "";
- }
- static function addFunction(id, f)
- {
- _global.bin[id.toLowerCase()] = f;
- }
- function getObjectAtCwd()
- {
- if(this.ram.CWD == undefined)
- {
- this.ram.CWD = "/";
- }
- return this.getObjectAtPath(this.ram.CWD);
- }
- function getObjectAtPath(fullPath)
- {
- if(fullPath.substr(0,1) != "/")
- {
- fullPath = this.ram.CWD + "/" + fullPath;
- }
- var _loc4_ = this.simplifyPath(fullPath).split("/");
- var _loc5_ = _global;
- _global._level0 = _level0;
- _global._level1 = _level1;
- _global._level2 = _level2;
- _global._level3 = _level3;
- var _loc3_ = 0;
- while(_loc3_ < _loc4_.length)
- {
- if(_loc4_[_loc3_] != "")
- {
- _loc5_ = _loc5_[_loc4_[_loc3_]];
- }
- _loc3_ = _loc3_ + 1;
- }
- return _loc5_;
- }
- function getBasePath(fullPath)
- {
- if(fullPath.charAt(fullPath.length - 1) == "/")
- {
- return fullPath;
- }
- if(fullPath.charAt(0) != "/")
- {
- fullPath = this.ram.CWD + "/" + fullPath;
- }
- var _loc3_ = this.simplifyPath(fullPath).split("/");
- _loc3_.pop();
- return _loc3_.join("/") + "/";
- }
- function getFileName(fullPath)
- {
- return String(fullPath.split("/").pop());
- }
- function simplifyPath(fullPath)
- {
- var _loc2_ = fullPath.split("/");
- var _loc1_ = 0;
- while(_loc1_ < _loc2_.length)
- {
- if(_loc2_[_loc1_] == "..")
- {
- _loc2_.splice(_loc1_ - 1,2);
- _loc1_ = Math.max(-1,_loc1_ - 2);
- }
- else if(_loc2_[_loc1_] == "." || !_loc2_[_loc1_] || _loc2_[_loc1_] == "")
- {
- _loc2_.splice(_loc1_,1);
- _loc1_ = Math.max(-1,_loc1_ - 1);
- }
- _loc1_ = _loc1_ + 1;
- }
- fullPath = "/" + _loc2_.join("/");
- return fullPath;
- }
- function onKeyDown()
- {
- switch(Key.getCode())
- {
- case 8:
- if(!this.active)
- {
- break;
- }
- if(this.cursorPos - 1 >= this.promptVar.length)
- {
- this.input.text = this.input.text.substring(0,this.cursorPos - 1) + this.input.text.substring(this.cursorPos,this.input.text.length);
- this.updateCursor(-1);
- }
- break;
- case 9:
- if(!this.active)
- {
- break;
- }
- var _loc6_ = this.input.text.substring(this.promptVar.length,this.cursorPos);
- var _loc10_ = this.input.text.lastIndexOf(" ",this.cursorPos);
- if(_loc10_ != -1)
- {
- _loc6_ = this.input.text.substring(_loc10_ + 1,this.input.text.length + 1);
- }
- var _loc13_ = (this.ram.CWD + "/" + _loc6_).split("/");
- var _loc7_ = String(_loc13_.pop());
- var _loc14_ = _loc13_.join("/");
- var _loc8_ = this.getObjectAtPath(_loc14_);
- var _loc3_ = new Array();
- for(var _loc4_ in _global.bin)
- {
- if(_loc4_.substr(0,_loc6_.length) == _loc6_ && !_loc3_[_loc4_])
- {
- _loc3_.push(_loc4_);
- _loc3_[_loc4_] = true;
- }
- }
- for(_loc4_ in _loc8_)
- {
- if(_loc4_.substr(0,_loc7_.length) == _loc7_ && !_loc3_[_loc4_])
- {
- _loc3_.push(_loc4_);
- _loc3_[_loc4_] = true;
- }
- }
- if(_loc3_.length == 1)
- {
- this.insertAtCursor(_loc3_[0].substring(_loc7_.length,_loc3_[0].length));
- }
- else if(_loc3_.length > 1)
- {
- this.stdout();
- var _loc5_ = _loc3_[0].length;
- _loc4_ = 0;
- while(_loc4_ < _loc3_.length)
- {
- this.stdout(_loc3_[_loc4_]);
- while(_loc3_[_loc4_].substr(0,_loc5_) != _loc3_[0].substr(0,_loc5_))
- {
- _loc5_ = _loc5_ - 1;
- }
- _loc4_ = _loc4_ + 1;
- }
- if(_loc5_)
- {
- this.insertAtCursor(_loc3_[0].substring(_loc7_.length,Math.min(_loc5_,_loc3_[0].length)));
- }
- }
- break;
- case 13:
- if(this.active)
- {
- var _loc9_ = this.input.text.substring(this.promptVar.length,this.input.text.length);
- this.history.unshift(_loc9_);
- if(this.history.length > this.historyLen)
- {
- this.history.pop();
- }
- this.historyPtr = -1;
- this.stdout(this.promptVar + _loc9_);
- this.stdout(this.executeCommand(_loc9_));
- this.input.text = this.promptVar;
- this.updatePrompt();
- this.updateCursor();
- }
- if(this.active)
- {
- Selection.setFocus(this.mc.input);
- Selection.setSelection(0,0);
- }
- break;
- case 16:
- case 17:
- case 18:
- case 20:
- break;
- case 35:
- this.cursorPos = this.input.text.length;
- this.updateCursor();
- break;
- case 36:
- this.cursorPos = 0;
- this.updateCursor();
- break;
- case 37:
- if(!this.active)
- {
- break;
- }
- this.updateCursor(-1);
- break;
- case 38:
- if(!this.active)
- {
- break;
- }
- this.historyPtr = Math.min(this.history.length - 1,this.historyPtr + 1);
- this.input.text = this.promptVar + this.history[this.historyPtr];
- this.cursorPos = this.input.text.length;
- this.updateCursor();
- break;
- case 39:
- if(!this.active)
- {
- break;
- }
- this.updateCursor(1);
- break;
- case 40:
- if(!this.active)
- {
- break;
- }
- this.historyPtr = Math.max(-1,this.historyPtr - 1);
- if(this.historyPtr == -1)
- {
- this.input.text = this.promptVar;
- }
- else
- {
- this.input.text = this.promptVar + this.history[this.historyPtr];
- }
- this.cursorPos = this.input.text.length;
- this.updateCursor();
- break;
- case 46:
- if(!this.active)
- {
- break;
- }
- this.input.text = this.input.text.substring(0,this.cursorPos) + this.input.text.substring(this.cursorPos + 1,this.input.text.length);
- break;
- case 220:
- if(this.active)
- {
- this.hide();
- }
- else
- {
- this.show();
- }
- break;
- default:
- if(!this.active)
- {
- break;
- }
- var _loc11_ = Key.getCode() + (!(Key.isDown(16) || Key.isToggled(20)) ? 0 : 256) + (!Key.isDown(17) ? 0 : 512) + (!Key.isDown(18) ? 0 : 1024);
- var _loc15_ = Key.getAscii();
- var _loc12_ = undefined;
- if(this.codepage[_loc11_])
- {
- _loc12_ = this.codepage[_loc11_];
- }
- else
- {
- _loc12_ = String.fromCharCode(_loc15_);
- }
- this.insertAtCursor(_loc12_);
- Selection.setFocus(this.mc.input);
- Selection.setSelection(2,2);
- break;
- }
- }
- function redraw()
- {
- this.mc.lineStyle(0,com.neodelight.flanix.Console.COLOR_LINE);
- this.mc.moveTo(this.offsetX,this.offsetY);
- this.mc.beginFill(com.neodelight.flanix.Console.COLOR_BG,com.neodelight.flanix.Console.COLOR_BG_ALPHA);
- this.mc.lineTo(this.width + this.offsetX,this.offsetY);
- this.mc.lineTo(this.width + this.offsetX,this.height + this.offsetY);
- this.mc.lineTo(this.offsetX,this.height + this.offsetY);
- this.mc.lineTo(this.offsetX,this.offsetY);
- this.mc.endFill();
- this.output._x = this.offsetX;
- this.output._y = this.offsetY;
- this.input._x = this.padding + this.offsetX;
- this.input._y = this.padding + this.offsetY + this.outputHeight + this.spacing;
- this.updatePrompt();
- this.updateCursor();
- }
- function updateCursor(offset)
- {
- if(offset)
- {
- this.cursorPos += offset;
- }
- this.cursorPos = Math.max(this.promptVar.length,Math.min(this.input.text.length,this.cursorPos));
- this.cursorMc._x = this.offsetX + this.padding + 2 + this.cursorPos * 8;
- this.cursorMc._visible = true;
- }
- function updatePrompt()
- {
- this.promptVar = this.ram.CWD + ">";
- this.input.text = this.promptVar;
- }
- function insertAtCursor(str)
- {
- this.input.text = this.input.text.substring(0,this.cursorPos) + str + this.input.text.substring(this.cursorPos,this.input.text.length);
- this.updateCursor(str.length);
- }
- function executeCommand(cmd, stdin)
- {
- var _loc24_ = "";
- cmd = com.neodelight.std.XString.trim(cmd);
- while(cmd.charAt(cmd.length - 1) == ";")
- {
- cmd = cmd.substr(0,cmd.length - 1);
- }
- var _loc4_ = "cmd";
- var _loc2_ = "";
- var _loc12_ = cmd.length - 1;
- for(; _loc12_ >= 0; _loc12_ = _loc12_ - 1)
- {
- if(cmd.charAt(_loc12_ - 1) == "\\")
- {
- continue;
- }
- var _loc3_ = cmd.charAt(_loc12_);
- switch(_loc4_)
- {
- case "cmd":
- switch(_loc3_)
- {
- case "\"":
- _loc4_ = "quotDouble";
- break;
- case "\'":
- _loc4_ = "quotSingle";
- break;
- case ";":
- case "\n":
- var _loc19_ = cmd.substr(0,_loc12_);
- var _loc14_ = String(this.executeCommand(_loc19_));
- if(_loc14_ != "")
- {
- _loc24_ += _loc14_ + "\n";
- }
- cmd = cmd.substr(_loc12_ + 1);
- _loc12_ = -1;
- }
- break;
- case "quotSingle":
- if(_loc3_ == "\'")
- {
- _loc4_ = "cmd";
- }
- break;
- case "quotDouble":
- if(_loc3_ == "\"")
- {
- _loc4_ = "cmd";
- }
- break;
- default:
- trace("#ERROR: unknown mode: " + _loc4_ + ".. flanix is buggy");
- break;
- }
- }
- if(this.ram.CWD == undefined)
- {
- this.ram.CWD = "/";
- }
- var _loc18_ = undefined;
- var _loc21_ = undefined;
- var _loc11_ = new Array();
- _loc2_ = "";
- _loc4_ = "cmd";
- cmd += " ";
- _loc12_ = 0;
- for(; _loc12_ < cmd.length; _loc12_ = _loc12_ + 1)
- {
- _loc3_ = cmd.charAt(_loc12_);
- switch(_loc4_)
- {
- case "cmd":
- if(_loc3_ == " ")
- {
- _loc2_ = com.neodelight.std.XString.stripSlashes(_loc2_);
- _loc4_ = "param";
- if(_loc2_ != "")
- {
- _loc11_.push(_loc2_);
- }
- _loc2_ = "";
- }
- else
- {
- _loc2_ += _loc3_;
- }
- break;
- case "param":
- if(_loc2_ == "")
- {
- if(_loc3_ == " ")
- {
- continue;
- }
- if(_loc3_ == "`")
- {
- _loc4_ = "paramExpression";
- _loc2_ += _loc3_;
- continue;
- }
- if(_loc3_ == "\"")
- {
- _loc4_ = "paramQuoteDouble";
- continue;
- }
- if(_loc3_ == "\'")
- {
- _loc4_ = "paramQuoteSingle";
- continue;
- }
- if(_loc3_ == "|")
- {
- _loc18_ = cmd.substr(_loc12_ + 1);
- _loc21_ = "|";
- _loc12_ = 1.7976931348623157e+308;
- continue;
- }
- if(_loc3_ == ">")
- {
- _loc4_ = "paramPipeOutFile";
- continue;
- }
- }
- if(_loc3_ == " ")
- {
- _loc4_ = "param";
- if(Number(_loc2_) == _loc2_)
- {
- _loc2_ = Number(_loc2_);
- }
- else
- {
- _loc2_ = com.neodelight.std.XString.stripSlashes(_loc2_);
- if(_loc2_.indexOf("$") != -1)
- {
- if(_loc2_.length <= _loc2_.indexOf("$") + 1)
- {
- this.stderr("bash: unknown variable: $");
- this.setExitCode(2);
- return undefined;
- }
- var _loc8_ = this.ram[_loc2_.substr(_loc2_.indexOf("$") + 1)];
- if(_loc8_ == undefined)
- {
- _loc8_ = "";
- }
- _loc2_ = _loc2_.substring(0,_loc2_.indexOf("$")) + String(_loc8_);
- }
- }
- _loc11_.push(_loc2_);
- _loc2_ = "";
- }
- else
- {
- _loc2_ += _loc3_;
- }
- break;
- case "paramExpression":
- if(_loc3_ == "`" && cmd.charAt(_loc12_ - 1) != "\\")
- {
- _loc2_ = this.executeCommand(_loc2_);
- _loc4_ = "param";
- _loc11_.push(_loc2_);
- _loc2_ = "";
- }
- else
- {
- _loc2_ += _loc3_;
- }
- break;
- case "paramQuoteSingle":
- if(_loc3_ == "\'" && cmd.charAt(_loc12_ - 1) != "\\")
- {
- _loc2_ = com.neodelight.std.XString.stripSlashes(_loc2_);
- _loc4_ = "param";
- _loc11_.push(_loc2_);
- _loc2_ = "";
- }
- else
- {
- _loc2_ += _loc3_;
- }
- break;
- case "paramQuoteDouble":
- if(_loc3_ == "\"" && cmd.charAt(_loc12_ - 1) != "\\")
- {
- _loc2_ = com.neodelight.std.XString.stripSlashes(_loc2_);
- while(_loc2_.indexOf("$") != -1)
- {
- var _loc6_ = _loc2_.indexOf("$");
- var _loc10_ = Math.min(_loc2_.length,_loc2_.indexOf(" ",_loc6_) <= -1 ? 1.7976931348623157e+308 : _loc2_.indexOf(" ",_loc6_));
- var _loc13_ = _loc2_.substring(_loc6_ + 1,_loc10_);
- _loc8_ = this.ram[_loc13_];
- if(_loc8_ == undefined)
- {
- _loc8_ = "";
- }
- _loc2_ = _loc2_.slice(0,_loc6_) + String(_loc8_) + _loc2_.slice(_loc10_,_loc2_.length);
- }
- _loc4_ = "param";
- _loc11_.push(_loc2_);
- _loc2_ = "";
- }
- else
- {
- _loc2_ += _loc3_;
- }
- break;
- case "paramPipeOutFile":
- if(!(_loc3_ == " " && _loc2_ == ""))
- {
- if(_loc3_ == " ")
- {
- _loc18_ = _loc2_;
- _loc21_ = ">";
- _loc2_ = "";
- _loc4_ = "param";
- }
- else
- {
- _loc2_ += _loc3_;
- }
- }
- break;
- default:
- trace("unknown mode: " + _loc4_);
- break;
- }
- }
- var _loc22_ = "";
- if(_loc11_.length)
- {
- var _loc7_ = new Array();
- _loc12_ = 1;
- while(_loc12_ < _loc11_.length)
- {
- _loc7_.push(_loc11_[_loc12_]);
- _loc12_ = _loc12_ + 1;
- }
- var _loc15_ = {};
- for(_loc12_ in _loc7_)
- {
- if(_loc7_[_loc12_].substr(0,2) == "--")
- {
- _loc15_[_loc7_[_loc12_].substr(2,_loc7_[_loc12_].length - 2)] = true;
- }
- else if(String(_loc7_[_loc12_]).substr(0,1) == "-")
- {
- var _loc9_ = 1;
- while(_loc9_ < String(_loc7_[_loc12_]).length)
- {
- _loc15_[String(_loc7_[_loc12_]).charAt(_loc9_)] = true;
- _loc9_ = _loc9_ + 1;
- }
- }
- }
- if(stdin != undefined)
- {
- _loc7_.push(stdin);
- }
- var _loc20_ = String(_loc11_[0]);
- this.setExitCode(undefined);
- var _loc17_ = undefined;
- var _loc23_ = undefined;
- if(_loc20_.indexOf("/") != -1)
- {
- if(_loc20_.charAt(0) == "/")
- {
- _loc17_ = this.getObjectAtPath(_loc20_);
- _loc23_ = this.getBasePath(_loc20_);
- }
- else
- {
- _loc17_ = this.getObjectAtPath(this.ram.CWD + "/" + _loc20_);
- _loc23_ = this.getBasePath(this.ram.CWD + "/" + _loc20_);
- }
- }
- else
- {
- var _loc16_ = this.ram.PATH.indexOf("/bin") != -1 ? this.ram.PATH.split(":") : ("/bin:" + this.ram.PATH).split(":");
- _loc12_ = 0;
- while(_loc12_ < _loc16_.length)
- {
- _loc17_ = this.getObjectAtPath(_loc16_[_loc12_] + "/" + _loc20_);
- if(_loc17_ != undefined)
- {
- _loc23_ = _loc16_[_loc12_];
- break;
- }
- _loc12_ = _loc12_ + 1;
- }
- }
- if(_loc17_ != undefined)
- {
- switch(typeof _loc17_)
- {
- case "function":
- var _loc27_ = Function(_loc17_);
- this.parameters = _loc7_;
- this.flags = _loc15_;
- _loc22_ = _loc27_.apply(this.getObjectAtPath(_loc23_),_loc7_);
- break;
- case "string":
- var _loc25_ = _loc17_.split("\n",2);
- if(_loc25_[0] == "#!/bin/sh")
- {
- _loc22_ = this.executeCommand(_loc25_[1]);
- }
- else
- {
- this.stderr("bash: to execute a string as a script, type: sh <filename>");
- }
- break;
- default:
- this.stderr("bash: not a command: " + _loc20_ + " (" + typeof _loc17_ + ")");
- }
- }
- else
- {
- this.stderr("bash: command not found or undefined: " + _loc20_);
- this.setExitCode(3);
- }
- this.ram.CWD = this.simplifyPath(this.ram.CWD);
- if(_loc18_)
- {
- switch(_loc21_)
- {
- case "|":
- _loc22_ = this.executeCommand(_loc18_,_loc22_);
- break;
- case ">":
- if(_loc18_.indexOf("/") != -1)
- {
- var _loc26_ = this.getObjectAtPath(this.getBasePath(_loc18_));
- _loc26_[this.getFileName(_loc18_)] = _loc22_;
- }
- else
- {
- _loc26_ = this.getObjectAtCwd();
- _loc26_[_loc18_] = _loc22_;
- }
- break;
- case "<":
- }
- _loc22_ = "";
- }
- }
- else
- {
- _loc22_ = "";
- }
- return _loc24_ + _loc22_;
- }
- }
-