// (c) 2004-2006 Rob Rohan, Richard Applebaum, and Barney Boisvert

if(typeof OUTPUT_VERSION=="undefined")
{alert("Fatal Error: Console is missing required libraries");throw new Error("console.js missing required libraries");}
var CONSOLE_VERSION="0.4";var PROMPT_A="[neuro@"+(window.location.host||"widget")+"]:~# ";var PROMPT_B="[neuro@"+(window.location.host||"widget")+"]:~$ ";var current_command;var PROMPT=PROMPT_A;var TERMINAL_TYPE="javascript";var _command_buffer="";function __con__help()
{neuro_SystemOut(NEWLINE);neuro_SystemOut("----------------------------------------------------------------"+NEWLINE);neuro_SystemOut("| Neuromancer Shell v: "+CONSOLE_VERSION+" "+NEWLINE);neuro_SystemOut("|"+NEWLINE);neuro_SystemOut("| Commands:"+NEWLINE);neuro_SystemOut("|---------------------------------------------------------------"+NEWLINE);neuro_SystemOut("| reflect(object)   - show object innards"+NEWLINE);neuro_SystemOut("| echo(object)      - show on stdout"+NEWLINE);neuro_SystemOut("| clear             - clears the console"+NEWLINE);neuro_SystemOut("| help              - this cruft"+NEWLINE);neuro_SystemOut("| all normal js commands (in javascript mode)"+NEWLINE);neuro_SystemOut("----------------------------------------------------------------"+NEWLINE);neuro_Runner("");}
echo=neuro_SystemOut;reflect=neuro_Reflect;function env()
{reflect(this);}
function neuro_Runner(str)
{try
{if(TERMINAL_TYPE=="javascript")
{var ret=eval(str);if(typeof ret!="undefined")
{neuro_SystemOut(NEWLINE+ret);}
neuro_SystemOut(NEWLINE+PROMPT);}
else if(TERMINAL_TYPE=="system")
{if(window.widget)
{if(str.length>0)
{neuro_SystemOut(window.NEWLINE);if(current_command!=null)
{current_command.cancel();}
current_command=widget.system("PATH=/bin:/usr/local/bin:/usr/bin:/sbin;"+str,handler_EndSystemCall);current_command.onreadoutput=handler_OutputHandler;}
else
{neuro_SystemOut(NEWLINE+PROMPT);}}
else
{TERMINAL_TYPE="javascript";PROMPT=PROMPT_A;throw new Error("Not in a widget! System mode is not available");}}}
catch(e)
{neuro_SystemErr(NEWLINE+neuro_expandError(e));neuro_SystemOut(NEWLINE+PROMPT);}}
function handler_EndSystemCall()
{var textele=document.getElementById("neurostdout");neuro_SystemOut(window.NEWLINE+window.PROMPT);textele.scrollTop=textele.scrollHeight;current_command.close();}
function handler_OutputHandler(currentStringOnStdout)
{var textele=document.getElementById("neurostdout");if(currentStringOnStdout.length)
{var withNewLine=currentStringOnStdout.replace(/(\n)/g,NEWLINE);neuro_SystemOut(withNewLine);textele.scrollTop=textele.scrollHeight;}}
function stopCurrentSystemCommand()
{if(current_command!=null)
{neuro_SystemOut(window.NEWLINE+PROMPT);current_command.cancel();current_command.close();}}
function Console(){;}
Console.prototype.keyListener=function _console_keyListener(evt)
{if(evt!=null)
{var textele=document.getElementById("neurostdout");var code=neuro_getKeyCodeFromEvent(evt);var key=neuro_getKeyFromEvent(evt);if(_command_buffer==null)_command_buffer="";switch(code)
{case 13:var cmd=_command_buffer;_command_buffer="";if(cmd=="clear")
{textele.innerHTML="";neuro_Runner("");}
else if(cmd=="help")
{__con__help();}
else if(cmd=="reboot")
{reboot();}
else if(cmd=="javascript")
{PROMPT=PROMPT_A;TERMINAL_TYPE="javascript";neuro_Runner("");}
else if(cmd=="system")
{PROMPT=PROMPT_B;TERMINAL_TYPE="system";neuro_Runner("");}
else
{if(cmd.length>0)
neuro_Runner(cmd);else
neuro_Runner("");}
textele.scrollTop=textele.scrollHeight;break;case 8:if(_command_buffer.length>0)
{_command_buffer=_command_buffer.substring(0,_command_buffer.length-1);textele.innerHTML=textele.innerHTML.toString().substring(0,textele.innerHTML.toString().length-1);}
break;case 0:break;case 27:break;default:if(key!=null&&typeof key!="undefined")
{if((evt.ctrlKey&&code==99)||code==3)
{log.debug("ctrl+c");stopCurrentSystemCommand();break;}
_command_buffer+=key;if(code==32)
{textele.innerHTML+="&nbsp;";}
else if(code==60)
{textele.innerHTML+="&lt;";}
else if(code==38)
{textele.innerHTML+="&amp;";}
else
{textele.innerHTML+=key;}}
break;}
evt.cancelBubble=true;evt.returnValue=false;return false;}};Console.prototype.keyUpListener=function _console_keyUpListener(evt)
{var code=neuro_getKeyCodeFromEvent(evt);var textele=document.getElementById("neurostdout");switch(code)
{case 13:break;case 8:if(SysBrowser.explore)
{if(_command_buffer.length>0)
_command_buffer=_command_buffer.substring(0,_command_buffer.length-1);}
break;case 38:evt.cancelBubble=true;evt.returnValue=false;return false;break;}};Console.prototype.keyDownListener=function _console_keyDownListener(evt)
{var code=neuro_getKeyCodeFromEvent(evt);var textele=document.getElementById("neurostdout");switch(code)
{case 38:case 40:case 8:case 37:case 39:if(SysBrowser.explore)
{evt.cancelBubble=true;evt.returnValue=false;}
return false;break;}};