mirror of
https://github.com/reactos/reactos.git
synced 2025-08-03 13:45:50 +00:00
cmd_set:
- When given a name with no value, show all variables that start with that name. - Implement /P switch - Set nErrorLevel on failure - Make syntax more compatible with Windows (allow any control character to act as space; implement quoting) svn path=/trunk/; revision=35649
This commit is contained in:
parent
2820db81b3
commit
cbcbf539b8
1 changed files with 86 additions and 40 deletions
|
@ -48,12 +48,33 @@ seta_eval ( LPCTSTR expr );
|
||||||
static LPCTSTR
|
static LPCTSTR
|
||||||
skip_ws ( LPCTSTR p )
|
skip_ws ( LPCTSTR p )
|
||||||
{
|
{
|
||||||
return p + _tcsspn ( p, _T(" \t") );
|
while (*p && *p <= _T(' '))
|
||||||
|
p++;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Used to check for and handle:
|
||||||
|
* SET "var=value", SET /P "var=prompt", and SET /P var="prompt" */
|
||||||
|
static LPTSTR
|
||||||
|
GetQuotedString(TCHAR *p)
|
||||||
|
{
|
||||||
|
TCHAR *end;
|
||||||
|
if (*p == _T('"'))
|
||||||
|
{
|
||||||
|
p = (LPTSTR)skip_ws(p + 1);
|
||||||
|
/* If a matching quote is found, truncate the string */
|
||||||
|
end = _tcsrchr(p, _T('"'));
|
||||||
|
if (end)
|
||||||
|
*end = _T('\0');
|
||||||
|
}
|
||||||
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
INT cmd_set (LPTSTR param)
|
INT cmd_set (LPTSTR param)
|
||||||
{
|
{
|
||||||
LPTSTR p;
|
LPTSTR p;
|
||||||
|
LPTSTR lpEnv;
|
||||||
|
LPTSTR lpOutput;
|
||||||
|
|
||||||
if ( !_tcsncmp (param, _T("/?"), 2) )
|
if ( !_tcsncmp (param, _T("/?"), 2) )
|
||||||
{
|
{
|
||||||
|
@ -61,26 +82,20 @@ INT cmd_set (LPTSTR param)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
param = (LPTSTR)skip_ws(param);
|
||||||
|
|
||||||
/* if no parameters, show the environment */
|
/* if no parameters, show the environment */
|
||||||
if (param[0] == _T('\0'))
|
if (param[0] == _T('\0'))
|
||||||
{
|
{
|
||||||
LPTSTR lpEnv;
|
|
||||||
LPTSTR lpOutput;
|
|
||||||
INT len;
|
|
||||||
|
|
||||||
lpEnv = (LPTSTR)GetEnvironmentStrings ();
|
lpEnv = (LPTSTR)GetEnvironmentStrings ();
|
||||||
if (lpEnv)
|
if (lpEnv)
|
||||||
{
|
{
|
||||||
lpOutput = lpEnv;
|
lpOutput = lpEnv;
|
||||||
while (*lpOutput)
|
while (*lpOutput)
|
||||||
{
|
{
|
||||||
len = _tcslen(lpOutput);
|
if (*lpOutput != _T('='))
|
||||||
if (len)
|
ConOutPuts(lpOutput);
|
||||||
{
|
lpOutput += _tcslen(lpOutput) + 1;
|
||||||
if (*lpOutput != _T('='))
|
|
||||||
ConOutPuts (lpOutput);
|
|
||||||
lpOutput += (len + 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
FreeEnvironmentStrings (lpEnv);
|
FreeEnvironmentStrings (lpEnv);
|
||||||
}
|
}
|
||||||
|
@ -97,16 +112,35 @@ INT cmd_set (LPTSTR param)
|
||||||
/*might seem random but this is what windows xp does */
|
/*might seem random but this is what windows xp does */
|
||||||
nErrorLevel = 9165;
|
nErrorLevel = 9165;
|
||||||
}
|
}
|
||||||
/* TODO FIXME - what are we supposed to return? */
|
return !Success;
|
||||||
return Success;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !_tcsnicmp (param, _T("/"), 1) )
|
if (!_tcsnicmp(param, _T("/P"), 2))
|
||||||
{
|
{
|
||||||
ConErrResPrintf (STRING_SYNTAX_COMMAND_INCORRECT, param);
|
TCHAR value[1023];
|
||||||
|
param = GetQuotedString((LPTSTR)skip_ws(param + 2));
|
||||||
|
p = _tcschr(param, _T('='));
|
||||||
|
if (!p)
|
||||||
|
{
|
||||||
|
ConErrResPuts(STRING_SYNTAX_COMMAND_INCORRECT);
|
||||||
|
nErrorLevel = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*p++ = _T('\0');
|
||||||
|
ConOutPrintf(_T("%s"), GetQuotedString(p));
|
||||||
|
ConInString(value, 1023);
|
||||||
|
|
||||||
|
if (!*value || !SetEnvironmentVariable(param, value))
|
||||||
|
{
|
||||||
|
nErrorLevel = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
param = GetQuotedString(param);
|
||||||
|
|
||||||
p = _tcschr (param, _T('='));
|
p = _tcschr (param, _T('='));
|
||||||
if (p)
|
if (p)
|
||||||
{
|
{
|
||||||
|
@ -114,41 +148,53 @@ INT cmd_set (LPTSTR param)
|
||||||
if (p == param)
|
if (p == param)
|
||||||
{
|
{
|
||||||
/* handle set =val case */
|
/* handle set =val case */
|
||||||
ConErrResPrintf (STRING_SYNTAX_COMMAND_INCORRECT, param);
|
ConErrResPuts(STRING_SYNTAX_COMMAND_INCORRECT);
|
||||||
return 0;
|
nErrorLevel = 1;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*p = _T('\0');
|
*p++ = _T('\0');
|
||||||
p++;
|
if (!SetEnvironmentVariable(param, p))
|
||||||
if (*p == _T('\0'))
|
|
||||||
{
|
{
|
||||||
p = NULL;
|
nErrorLevel = 1;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
SetEnvironmentVariable (param, p);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* display environment variable */
|
/* display all environment variable with the given prefix */
|
||||||
LPTSTR pszBuffer;
|
BOOL bFound = FALSE;
|
||||||
DWORD dwBuffer;
|
|
||||||
|
|
||||||
pszBuffer = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
|
while (_istspace(*param) || *param == _T(',') || *param == _T(';'))
|
||||||
dwBuffer = GetEnvironmentVariable (param, pszBuffer, ENV_BUFFER_SIZE);
|
param++;
|
||||||
if (dwBuffer == 0)
|
|
||||||
|
p = _tcsrchr(param, _T(' '));
|
||||||
|
if (!p)
|
||||||
|
p = param + _tcslen(param);
|
||||||
|
*p = _T('\0');
|
||||||
|
|
||||||
|
lpEnv = GetEnvironmentStrings();
|
||||||
|
if (lpEnv)
|
||||||
|
{
|
||||||
|
lpOutput = lpEnv;
|
||||||
|
while (*lpOutput)
|
||||||
|
{
|
||||||
|
if (!_tcsnicmp(lpOutput, param, p - param))
|
||||||
|
{
|
||||||
|
ConOutPuts(lpOutput);
|
||||||
|
bFound = TRUE;
|
||||||
|
}
|
||||||
|
lpOutput += _tcslen(lpOutput) + 1;
|
||||||
|
}
|
||||||
|
FreeEnvironmentStrings(lpEnv);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bFound)
|
||||||
{
|
{
|
||||||
ConErrResPrintf (STRING_PATH_ERROR, param);
|
ConErrResPrintf (STRING_PATH_ERROR, param);
|
||||||
return 0;
|
nErrorLevel = 1;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
else if (dwBuffer > ENV_BUFFER_SIZE)
|
|
||||||
{
|
|
||||||
pszBuffer = (LPTSTR)cmd_realloc (pszBuffer, dwBuffer * sizeof (TCHAR));
|
|
||||||
GetEnvironmentVariable (param, pszBuffer, dwBuffer);
|
|
||||||
}
|
|
||||||
ConOutPrintf (_T("%s\n"), pszBuffer);
|
|
||||||
|
|
||||||
cmd_free (pszBuffer);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue