cmd: Trim whitespace in echo on/off.

This commit is contained in:
Frédéric Delanoy 2011-11-11 19:27:43 +01:00 committed by Alexandre Julliard
parent 3acc2068a0
commit e8d8df3c54
3 changed files with 49 additions and 13 deletions

View File

@ -854,6 +854,35 @@ BOOL WCMD_delete (WCHAR *command) {
return foundAny;
}
/*
* WCMD_strtrim
*
* Returns a trimmed version of s with all leading and trailing whitespace removed
* Pre: s non NULL
*
*/
static WCHAR *WCMD_strtrim(const WCHAR *s)
{
DWORD len = strlenW(s);
const WCHAR *start = s;
WCHAR* result;
if (!(result = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR))))
return NULL;
while (isspaceW(*start)) start++;
if (*start) {
const WCHAR *end = s + len - 1;
while (end > start && isspaceW(*end)) end--;
memcpy(result, start, (end - start + 2) * sizeof(WCHAR));
result[end - start + 1] = '\0';
} else {
result[0] = '\0';
}
return result;
}
/****************************************************************************
* WCMD_echo
*
@ -861,33 +890,36 @@ BOOL WCMD_delete (WCHAR *command) {
* in DOS (try typing "ECHO ON AGAIN" for an example).
*/
void WCMD_echo (const WCHAR *command) {
void WCMD_echo (const WCHAR *command)
{
int count;
const WCHAR *origcommand = command;
WCHAR *trimmed;
if ( command[0]==' ' || command[0]=='\t' || command[0]=='.'
|| command[0]==':' || command[0]==';')
command++;
count = strlenW(command);
trimmed = WCMD_strtrim(command);
if (!trimmed) return;
count = strlenW(trimmed);
if (count == 0 && origcommand[0]!='.' && origcommand[0]!=':'
&& origcommand[0]!=';') {
if (echo_mode) WCMD_output (WCMD_LoadMessage(WCMD_ECHOPROMPT), onW);
else WCMD_output (WCMD_LoadMessage(WCMD_ECHOPROMPT), offW);
return;
}
if (lstrcmpiW(command, onW) == 0) {
echo_mode = TRUE;
return;
}
if (lstrcmpiW(command, offW) == 0) {
echo_mode = FALSE;
return;
}
WCMD_output_asis (command);
WCMD_output (newline);
if (lstrcmpiW(trimmed, onW) == 0)
echo_mode = TRUE;
else if (lstrcmpiW(trimmed, offW) == 0)
echo_mode = FALSE;
else {
WCMD_output_asis (command);
WCMD_output (newline);
}
HeapFree(GetProcessHeap(), 0, trimmed);
}
/**************************************************************************

View File

@ -26,8 +26,10 @@ echo@tab@word@tab@@space@
echo @tab@word
echo @tab@word
echo@tab@@tab@word
echo @tab@ on @space@
@echo off
echo off@tab@@space@
echo ------------ Testing 'echo' [OFF] --------------
echo word
echo 'singlequotedword'

View File

@ -74,6 +74,8 @@ word
@pwd@>echo@tab@@tab@word@space@
@tab@word
@pwd@>echo @tab@ on @space@@space@
------------ Testing 'echo' [OFF] --------------
word
'singlequotedword'