reactos/base/shell/cmd/goto.c
Hermès Bélusca-Maïto 6eb1cae348
[CMD] Fixes for Batch error execution control flow.
CORE-13713 CORE-13736

- In case execution of all batch contexts is stopped (by selecting "All"
  at the Ctrl-C/Ctrl-Break prompt), notify as well the CheckCtrlBreak()
  signal handler once there are no more batch contexts (this in effect
  resets the internal 'bLeaveAll' static flag in CheckCtrlBreak).
  This is an adaptation of the fix present in FreeCOM 1.5, first
  described in https://gcfl.net/FreeDOS/command.com/bugs074g.html .

- Introduce a ParseErrorEx() helper that sets the 'bParseError' flag and
  displays a customized syntax-error message, only for the first syntax
  error encountered. Implement ParseError() around the *Ex function.

- In batch mode, echo the original pre-parsed batch file line if a parse
  error has been encountered.

- When running a compound command - including IF, FOR, command blocks -,
  and that control flow is modified by any CALL/GOTO/EXIT command,
  detect this while running the compound command so as to stop it and go
  back to the main batch execution loop, that will then set up the actual
  new command to run.

- In GOTO, do not process any more parts of a compound command only when
  we have found a valid label.
2020-08-19 20:35:58 +02:00

122 lines
2.8 KiB
C

/*
* GOTO.C - goto internal batch command.
*
* History:
*
* 16 Jul 1998 (Hans B Pufal)
* started.
*
* 16 Jul 1998 (John P Price)
* Separated commands into individual files.
*
* 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
* added config.h include
*
* 28 Jul 1998 (Hans B Pufal) [HBP_003]
* Terminate label on first space character, use only first 8 chars of
* label string
*
* 24-Jan-1999 (Eric Kohl)
* Unicode and redirection safe!
*
* 27-Jan-1999 (Eric Kohl)
* Added help text ("/?").
*
* 28-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
* Remove all hardcoded strings in En.rc
*/
#include "precomp.h"
/*
* Perform GOTO command.
*
* Only valid when a batch context is active.
*/
INT cmd_goto(LPTSTR param)
{
LPTSTR tmp, tmp2;
TRACE("cmd_goto(\'%s\')\n", debugstr_aw(param));
if (!_tcsncmp(param, _T("/?"), 2))
{
ConOutResPaging(TRUE, STRING_GOTO_HELP1);
return 0;
}
/* If not in batch, fail */
if (bc == NULL)
return 1;
/* Fail if no label has been provided */
if (*param == _T('\0'))
{
ConErrResPrintf(STRING_GOTO_ERROR1);
ExitBatch();
return 1;
}
/* Terminate label at first space char */
tmp = param + 1;
while (!_istcntrl(*tmp) && !_istspace(*tmp) && (*tmp != _T(':')))
++tmp;
*tmp = _T('\0');
/* jump to end of the file */
if ( _tcsicmp( param, _T(":eof"))==0)
{
/* Position at the end of the batch file */
bc->mempos = bc->memsize;
/* Do not process any more parts of a compound command */
bc->current = NULL;
return 0;
}
/* jump to begin of the file */
bc->mempos=0;
while (BatchGetString(textline, ARRAYSIZE(textline)))
{
INT pos;
INT_PTR size;
/* Strip out any trailing spaces or control chars */
tmp = textline + _tcslen(textline) - 1;
while (tmp > textline && (_istcntrl(*tmp) || _istspace(*tmp) || (*tmp == _T(':'))))
--tmp;
*(tmp + 1) = _T('\0');
/* Then leading spaces... */
tmp = textline;
while (_istspace(*tmp))
++tmp;
/* All space after leading space terminate the string */
size = _tcslen(tmp) - 1;
pos = 0;
while (tmp + pos < tmp + size)
{
if (_istspace(tmp[pos]))
tmp[pos]=_T('\0');
++pos;
}
tmp2 = param;
/* Use whole label name */
if ((*tmp == _T(':')) && ((_tcsicmp(++tmp, param) == 0) || (_tcsicmp(tmp, ++tmp2) == 0)))
{
/* Do not process any more parts of a compound command */
bc->current = NULL;
return 0;
}
}
ConErrResPrintf(STRING_GOTO_ERROR2, param);
ExitBatch();
return 1;
}
/* EOF */