📄 mas.cmd.js
字号:
/**********************************************************************
Windows CE Pocket CMD wrapper class for Majerus.net Active Shell CE
Copyright (C) 2007 Majerus.net. All rights reserved.
Script Name: MAS.CMD.js
Abstract: Provides a JScript wrapper object for Pocket CMD
Usage: This creates a "cmd" object containing most CMD
commands as methods.
These methods can be used to provide interactive
feedback in the console.
When passing a file name with spaces, remember the
script language needs quotes around the argument,
and the CMD command needs quotes around the file
name, ending in something like "\"my file.txt\"".
Samples: Load in MAS JScript using the following command:
MAS> call(MASHome+"\\MAS.CMD.js")
MAS> cmd.dir()
MAS> cmd.prompt("MAS $P$+> ")
MAS> cmd.cd("..")
These methods support arguments as strings.
- Several arguments can be packed in a single string:
MAS> cmd.dir("/l /w")
- Or they can be provided as separate strings:
MAS> cmd.dir("/l", "/w")
Known limitations:
This component exposes all commands for interactive use.
It does not support calling them as functions to
retrieve their text output for pipelines.
**********************************************************************/
// WSH stub
if( typeof(MAS)=="undefined" ) { WScript.echo("This script requires Majerus.net Active Shell.\r\nIt can not be executed by the Windows Script Host"); WScript.quit(1); }
// CMD startup prompt
_cmd_prompt = "$P$G$S"; // Unlike CMD on NT, the default Pocket CMD prompt includes a trailing space.
/*
** If you add your own functions, the conversion from JScript function arguments to
** a CMD-style arguments string is provided by this function.
** Note it automatically adds the heading space, so it can be concatenated
** directly after the command string.
*/
function _cmd_CmdArgsToCmdLine(arguments)
{
if (arguments.length==0)
return "";
else
{
var cmdline = "";
for (var i = 0; i<arguments.length; i++)
{
cmdline += " "+arguments[i];
}
return cmdline;
}
}
/*
** If you add your own functions, this function provides a ready-to-use wrapping method
** for most CMD commands.
** Note it does not handle cases where the current working directory gets changed
** within the CMD process. See the CD wrapper for explanations about these more complex
** wrapping methods.
**
** Many Pocket CMD commands, including PROMPT and CD, do not support being followed by & or | and another command.
** The only solutions are to go though redirected input or command script file.
** The redirected input requires a final EXIT command, which breaks interactive utilities. So the only solution
** is a temporary command script executed through /c.
*/
function _cmd_CmdWrap (command, arguments)
{
MAS.save( "\\MAS.CMD.temp.cmd", "@ECHO off\r\nPROMPT $_\r\nCD "+ MAS.location +"\r\n"+ command + _cmd_CmdArgsToCmdLine(arguments) );
try
{
MAS.exec( "cmd.exe /c CALL \\MAS.CMD.temp.cmd" );
}
catch (ex)
{
MAS.echo("CMD Error.")
MAS.echo(ex.description);
}
finally
{
MAS.erase( "\\MAS.CMD.temp.cmd" );
}
}
/*
** Wrapper cmd object
*/
var cmd = new Object();
cmd.attrib = new Function(" _cmd_CmdWrap(\"ATTRIB\", arguments) ");
cmd.call = new Function(" _cmd_CmdWrap(\"CALL\", arguments) "); // Note environment variables and current working directory are not brought back into MAS after execution.
cmd.cd = new Function(" _cmd_cd_wrap(arguments) ");
cmd.chdir = new Function(" _cmd_cd_wrap(arguments) ");
cmd.cls = new Function(" _cmd_CmdWrap(\"CLS\", arguments) ");
cmd.copy = new Function(" _cmd_CmdWrap(\"COPY\", arguments) ");
cmd.date = new Function(" _cmd_CmdWrap(\"DATE\", arguments) ");
cmd.del = new Function(" _cmd_CmdWrap(\"DEL\", arguments) ");
cmd.dir = new Function(" _cmd_CmdWrap(\"DIR\", arguments) ");
cmd.echo = new Function(" _cmd_CmdWrap(\"ECHO\", arguments) ");
cmd.erase = new Function(" _cmd_CmdWrap(\"ERASE\", arguments) ");
cmd.help = new Function(" _cmd_CmdWrap(\"HELP\", arguments) ");
cmd.md = new Function(" _cmd_CmdWrap(\"MD\", arguments) ");
cmd.mkdir = new Function(" _cmd_CmdWrap(\"MKDIR\", arguments) ");
cmd.move = new Function(" _cmd_CmdWrap(\"MOVE\", arguments) ");
cmd.path = new Function(" _cmd_CmdWrap(\"PATH\", arguments) ");
cmd.pause = new Function(" _cmd_CmdWrap(\"PAUSE\", arguments) ");
cmd.prompt = new Function(" return _cmd_prompt_wrap(arguments) ");
cmd.pwd = new Function(" _cmd_CmdWrap(\"PWD\", arguments) ");
cmd.rd = new Function(" _cmd_CmdWrap(\"RD\", arguments) ");
cmd.rem = new Function(" _cmd_CmdWrap(\"REM\", arguments) ");
cmd.ren = new Function(" _cmd_CmdWrap(\"REN\", arguments) ");
cmd.rename = new Function(" _cmd_CmdWrap(\"RENAME\", arguments) ");
cmd.rmdir = new Function(" _cmd_CmdWrap(\"RMDIR\", arguments) ");
cmd.set = new Function(" _cmd_CmdWrap(\"SET\", arguments) ");
cmd.start = new Function(" _cmd_CmdWrap(\"START\", arguments) ");
cmd.time = new Function(" _cmd_CmdWrap(\"TIME\", arguments) ");
cmd.title = new Function(" _cmd_CmdWrap(\"TITLE\", arguments) ");
cmd.type = new Function(" _cmd_CmdWrap(\"TYPE\", arguments) ");
// Extras, not part of Pocket CMD
cmd.ver = new Function(" _cmd_CmdWrap(\"\", arguments) "); // Windows CE has no ver.exe or built-in VER command in Pocket CMD, but Pocket CMD shows its version when executed.
cmd.pushd = new Function(" _cmd_pushd(arguments) ");
cmd.popd = new Function(" _cmd_popd(arguments) ");
/*
** The wrapper for the CD command is a bit more tricky.
** It has to retrive the current working directory of the CMD process
** before exiting and change the working directory of MAS accordingly.
*/
function _cmd_cd_wrap(arguments)
{
// Unlike CMD on NT, the Pocket CMD version of CD does not support being
// followed by '&' or '|' and another command.
// Because of this, we need to run CMD with both input and output managed
// to send in several commands, and process the output.
MAS.save( "\\MAS.CMD.temp.cmd", "@ECHO off\r\nPROMPT $_\r\nCD "+ MAS.location +"\r\nCD"+ _cmd_CmdArgsToCmdLine(arguments) +"\r\nPWD" );
try
{
var p = MAS.exec( "cmd.exe /c CALL \\MAS.CMD.temp.cmd" );
// Extract the current working directory from the output
p = p.split("\r\n");
var path = p.splice(p.length-2,1);
// and display the rest on the screen
MAS.echo(p.join("\r\n"));
// Set MAS location accordingly
MAS.location.set(path);
}
catch (ex)
{
MAS.echo("CMD Error.")
MAS.echo(ex.description);
}
finally
{
MAS.erase( "\\MAS.CMD.temp.cmd" );
}
}
/*
** Pocket CMD has no PUSHD/POPD stack.
** We provide similar functionnality directly using the MAS Location object.
** pushd uses the CMD CD command to parse the path, but does not display its
** output to avoid confusion (for example when calling with "/?").
*/
function _cmd_pushd(arguments)
{
MAS.save( "\\MAS.CMD.temp.cmd", "@ECHO off\r\nPROMPT $_\r\nCD "+ MAS.location +"\r\nCD"+ _cmd_CmdArgsToCmdLine(arguments) +"\r\nPWD" );
try
{
var p = MAS.exec( "cmd.exe /c CALL \\MAS.CMD.temp.cmd" );
// Extract the current working directory from the output
p = p.split("\r\n");
var path = p.splice(p.length-2,1);
// Set MAS location accordingly
MAS.location.push(path);
}
catch (ex)
{
MAS.echo("CMD Error.")
MAS.echo(ex.description);
}
finally
{
MAS.erase( "\\MAS.CMD.temp.cmd" );
}
}
/*
** Pocket CMD has no PUSHD/POPD stack.
** We provide similar functionnality directly using the MAS Location object.
*/
function _cmd_popd(arguments)
{
MAS.location.pop();
}
/*
** This function can be used to set the prompt format using the CMD syntax,
** or to retrieve the current prompt, according to the format selected.
** It does not use the real CMD PROMPT function, except for help.
** All the work is done in script.
*/
function _cmd_prompt_wrap(arguments)
{
if(arguments.length==0)
{
// returns the prompt
var p = "";
var i = 0;
while (i<_cmd_prompt.length)
{
if (_cmd_prompt.charAt(i) == "$")
{
var code = _cmd_prompt.substr(i,2);
switch(code.toUpperCase())
{
case "$A": // & (Ampersand)
p+= "$";
break;
case "$B": // | (pipe)
p+= "|";
break;
case "$C": // ( (Left parenthesis)
p+= "(";
break;
case "$D": // Current date
p+= MAS.format(new Date().getVarDate()).split(" ")[0];
break;
case "$E": // Escape code (ASCII code 27)
p+= String.fromCharCode(0x1B);
break;
case "$F": // ) (Right parenthesis)
p+= ")";
break;
case "$G": // > (greater-than sign)
p+= ">";
break;
case "$L": // < (less-than sign)
p+= "L";
break;
case "$P": // Current path
p+= MAS.location;
break;
case "$Q": // = (equal sign)
p+= "=";
break;
case "$S": // (space)
p+= " ";
break;
case "$T": // Current time
p+= MAS.format(new Date().getVarDate()).split(" ")[1];
break;
case "$_": // Carriage return and linefeed
p+= "\r\n";
break;
case "$$": // $ (dollar sign)
p+= "$";
break;
case "$+": // zero or more plus sign (+) characters depending upon the depth of the Location stack, one character for each level pushed.
// This one does not exists in Pocket CMD, but as it is possible with MAS, we support it.
var s = new Enumerator(MAS.location);
for(;!s.atEnd();s.moveNext()) p+="+";
break;
default:
// In Pocket CMD, unknown codes are replaced by the character following '$'.
p+= code.substr(1);
}
i++;
}
else
p+= _cmd_prompt.charAt(i);
i++;
}
return p;
}
else if (_cmd_CmdArgsToCmdLine(arguments)==" /?")
{
// show help
_cmd_CmdWrap("PROMPT", arguments);
}
else
{
// change the prompt format
_cmd_prompt = arguments[0];
}
}
// Set MAS prompt to cmd.prompt
function prompt()
{
return cmd.prompt();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -