Various fixes to the way cmd handles errorlevel and the "return values" of commands:
[reactos.git] / reactos / base / shell / cmd / call.c
1 /*
2 * CALL.C - call internal batch command.
3 *
4 *
5 * History:
6 *
7 * 16 Jul 1998 (Hans B Pufal)
8 * started.
9 *
10 * 16 Jul 1998 (John P Price)
11 * Seperated commands into individual files.
12 *
13 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
14 * added config.h include
15 *
16 * 04-Aug-1998 (Hans B Pufal)
17 * added lines to initialize for pointers (HBP004) This fixed the
18 * lock-up that happened sometimes when calling a batch file from
19 * another batch file.
20 *
21 * 07-Jan-1999 (Eric Kohl)
22 * Added help text ("call /?") and cleaned up.
23 *
24 * 20-Jan-1999 (Eric Kohl)
25 * Unicode and redirection safe!
26 *
27 * 02-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
28 * Remove all hardcode string to En.rc
29 */
30
31 #include <precomp.h>
32
33
34 /*
35 * Perform CALL command.
36 */
37
38 INT cmd_call (LPTSTR param)
39 {
40 TCHAR line[CMDLINE_LENGTH + 1];
41 TCHAR *first;
42 BOOL bInQuote = FALSE;
43
44 TRACE ("cmd_call: (\'%s\')\n", debugstr_aw(param));
45 if (!_tcsncmp (param, _T("/?"), 2))
46 {
47 ConOutResPaging(TRUE,STRING_CALL_HELP);
48 return 0;
49 }
50
51 /* Do a second round of %-variable substitutions */
52 if (!SubstituteVars(param, line, _T('%')))
53 return nErrorLevel = 1;
54
55 /* Find start and end of first word */
56 first = line;
57 while (_istspace(*first))
58 first++;
59
60 for (param = first; *param; param++)
61 {
62 if (!bInQuote && (_istspace(*param) || _tcschr(_T(",;="), *param)))
63 break;
64 bInQuote ^= (*param == _T('"'));
65 }
66
67 /* Separate first word from rest of line */
68 memmove(param + 1, param, (_tcslen(param) + 1) * sizeof(TCHAR));
69 *param++ = _T('\0');
70
71 if (*first == _T(':') && (bc))
72 {
73 /* CALL :label - call a subroutine of the current batch file */
74 while (*param == _T(' '))
75 param++;
76 nErrorLevel = Batch(bc->BatchFilePath, first, param, NULL);
77 return nErrorLevel;
78 }
79
80 nErrorLevel = DoCommand(first, param, NULL);
81 return nErrorLevel;
82 }
83
84 /* EOF */