[2/2] cmd: Add escape carat char at end of line feature and test cases
John Chow
jkchow at ucla.edu
Thu Mar 11 01:05:11 CST 2010
---
programs/cmd/tests/test_builtins.cmd | 5 +++-
programs/cmd/tests/test_builtins.cmd.exp | 4 ++-
programs/cmd/wcmdmain.c | 36 ++++++++++++++++++++++-------
3 files changed, 34 insertions(+), 11 deletions(-)
diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd
index 44eecaa..d5d9b6b 100644
--- a/programs/cmd/tests/test_builtins.cmd
+++ b/programs/cmd/tests/test_builtins.cmd
@@ -46,4 +46,7 @@ if /I not foo==FOO echo if /I seems to be broken
echo ---------- Testing caret escape char ------------
echo the caret char should escape special char such as pipe in echo
-echo This should ouput "| more" after: ^| more
+echo This should output a single carat: ^^
+echo This should output "| more" after: ^| more
+echo The following command should not be called: ^
+echo Success!
diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp
index f5318aa..6a1a010 100644
--- a/programs/cmd/tests/test_builtins.cmd.exp
+++ b/programs/cmd/tests/test_builtins.cmd.exp
@@ -26,4 +26,6 @@ if /i seems to work
if /I seems to work
---------- Testing caret escape char ------------
the caret char should escape special char such as pipe in echo
-This should ouput "| more" after: | more
+This should output a single carat: ^
+This should output "| more" after: | more
+The following command should not be called: echo Success!
diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c
index 070ed50..2128aff 100644
--- a/programs/cmd/wcmdmain.c
+++ b/programs/cmd/wcmdmain.c
@@ -1747,6 +1747,7 @@ WCHAR *WCMD_ReadAndParseLine(WCHAR *optionalcmd, CMD_LIST **output, HANDLE readF
BOOL lastWasIn = FALSE;
BOOL lastWasElse = FALSE;
BOOL lastWasRedirect = TRUE;
+ BOOL lastWasCarat = FALSE;
/* Allocate working space for a command read from keyboard, file etc */
if (!extraSpace)
@@ -1794,6 +1795,12 @@ WCHAR *WCMD_ReadAndParseLine(WCHAR *optionalcmd, CMD_LIST **output, HANDLE readF
while (*curPos != 0x00) {
WCHAR thisChar;
+
+ /* Prevent overflow caused by the carat escape char*/
+ if (*curLen >= MAXSTRING) {
+ WINE_ERR("Overflow detected in command\n");
+ return NULL;
+ }
/* Debugging AID:
WINE_TRACE("Looking at '%c' (len:%d, lws:%d, ows:%d)\n", *curPos, *curLen,
@@ -1876,6 +1883,7 @@ WCHAR *WCMD_ReadAndParseLine(WCHAR *optionalcmd, CMD_LIST **output, HANDLE readF
else thisChar = 'X'; /* Character with no special processing */
lastWasWhiteSpace = FALSE; /* Will be reset below */
+ lastWasCarat = FALSE;
switch (thisChar) {
@@ -2018,8 +2026,14 @@ WCHAR *WCMD_ReadAndParseLine(WCHAR *optionalcmd, CMD_LIST **output, HANDLE readF
break;
case '^': if (!inQuotes) {
- curPos++;
- curCopyTo[(*curLen)++] = *curPos;
+ WINE_TRACE("Found '^' conditions: curLen(%d),\n", *curLen);
+ if (*(curPos+1) != 0x00) {
+ curPos++;
+ curCopyTo[(*curLen)++] = *curPos;
+ }
+ else {
+ lastWasCarat = TRUE;
+ }
} else {
curCopyTo[(*curLen)++] = *curPos;
}
@@ -2096,8 +2110,9 @@ WCHAR *WCMD_ReadAndParseLine(WCHAR *optionalcmd, CMD_LIST **output, HANDLE readF
lastWasIn = lastWasDo = FALSE;
}
- /* If we have reached the end, add this command into the list */
- if (*curPos == 0x00 && *curLen > 0) {
+ /* If we have reached the end, add this command into the list.
+ Do not add command to list if escape char ^ was last */
+ if (*curPos == 0x00 && !lastWasCarat && *curLen > 0) {
/* Add an entry to the command list */
WCMD_addCommand(curString, &curStringLen,
@@ -2107,18 +2122,21 @@ WCHAR *WCMD_ReadAndParseLine(WCHAR *optionalcmd, CMD_LIST **output, HANDLE readF
&lastEntry, output);
}
- /* If we have reached the end of the string, see if bracketing outstanding */
- if (*curPos == 0x00 && curDepth > 0 && readFrom != INVALID_HANDLE_VALUE) {
+ /* If we have reached the end of the string, see if bracketing
+ or carat outstanding */
+ if (*curPos == 0x00 && (curDepth > 0 || lastWasCarat)
+ && readFrom != INVALID_HANDLE_VALUE) {
inRem = FALSE;
prevDelim = CMD_NONE;
inQuotes = 0;
memset(extraSpace, 0x00, (MAXSTRING+1) * sizeof(WCHAR));
- /* Read more, skipping any blank lines */
- while (*extraSpace == 0x00) {
+ /* Read more, skipping any blank lines unless carat escapes newline*/
+ do {
if (!context) WCMD_output_asis( WCMD_LoadMessage(WCMD_MOREPROMPT));
if (WCMD_fgets(extraSpace, MAXSTRING, readFrom) == NULL) break;
- }
+ } while (!lastWasCarat && *extraSpace == 0x00);
+
curPos = extraSpace;
if (context) handleExpansion(extraSpace, FALSE, NULL, NULL);
/* Continue to echo commands IF echo is on and in batch program */
--
1.6.3.3
--------------050702020803060201030004--
More information about the wine-patches
mailing list