2 * IF.C - if internal batch command.
7 * 16 Jul 1998 (Hans B Pufal)
10 * 16 Jul 1998 (John P Price)
11 * Seperated commands into individual files.
13 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
14 * added config.h include
16 * 07-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
17 * Added help text ("if /?") and cleaned up.
19 * 21-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
20 * Unicode and redirection ready!
22 * 01-Sep-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
26 * IF DEFINED variable command
43 INT
cmd_if (LPTSTR cmd
, LPTSTR param
)
45 INT x_flag
= 0; /* when set cause 'then' clause to be executed */
49 DebugPrintf ("cmd_if: (\'%s\', \'%s\'\n", cmd
, param
);
52 if (!_tcsncmp (param
, _T("/?"), 2))
54 ConOutPuts (_T("Performs conditional processing in batch programs.\n"
56 " IF [NOT] ERRORLEVEL number command\n"
57 " IF [NOT] string1==string2 command\n"
58 " IF [NOT] EXIST filename command\n"
59 " IF [NOT] DEFINED variable command\n"
61 "NOT Specifies that CMD should carry out the command only if\n"
62 " the condition is false\n"
63 "ERRORLEVEL number Specifies a true condition if the last program run returned\n"
64 " an exit code equal or greater than the number specified.\n"
65 "command Specifies the command to carry out if the condition is met.\n"
66 "string1==string2 Specifies a true condition if the specified text strings\n"
68 "EXIST filename Specifies a true condition if the specified filename exists.\n"
69 "DEFINED variable Specifies a true condition if the specified variable is\n"
74 /* First check if param string begins with word 'not' */
75 if (!_tcsnicmp (param
, _T("not"), 3) && _istspace (*(param
+ 3)))
77 x_flag
= X_EXEC
; /* Remember 'NOT' */
78 param
+= 3; /* Step over 'NOT' */
79 while (_istspace (*param
)) /* And subsequent spaces */
83 /* Check for 'exist' form */
84 if (!_tcsnicmp (param
, _T("exist"), 5) && _istspace (*(param
+ 5)))
87 while (_istspace (*param
))
91 while (*pp
&& !_istspace (*pp
))
100 hFind
= FindFirstFile (param
, &f
);
101 x_flag
^= (hFind
== INVALID_HANDLE_VALUE
) ? 0 : X_EXEC
;
102 if (hFind
!= INVALID_HANDLE_VALUE
)
111 /* Check for 'defined' form */
112 else if (!_tcsnicmp (param
, _T("defined"), 7) && _istspace (*(param
+ 7)))
118 /* IF [NOT] DEFINED var COMMAND */
120 while (_istspace (*param
))
122 /* IF [NOT] DEFINED var COMMAND */
125 while (*pp
&& !_istspace (*pp
))
127 /* IF [NOT] DEFINED var COMMAND */
132 ValueSize
= GetEnvironmentVariable(param
, Value
, sizeof Value
);
133 x_flag
^= (0 == ValueSize
)
142 /* Check for 'errorlevel' form */
143 else if (!_tcsnicmp (param
, _T("errorlevel"), 10) && _istspace (*(param
+ 10)))
148 while (_istspace (*pp
))
151 while (_istdigit (*pp
))
152 n
= n
* 10 + (*pp
++ - _T('0'));
154 x_flag
^= (nErrorLevel
< n
) ? 0 : X_EXEC
;
156 x_flag
|= X_EMPTY
; /* Syntax error if comd empty */
159 /* Check that '==' is present, syntax error if not */
160 else if (NULL
== (pp
= _tcsstr (param
, _T("=="))))
168 /* Change first '='to space to terminate comparison loop */
170 *pp
= _T(' '); /* Need a space to terminate comparison loop */
171 pp
+= 2; /* over '==' */
172 while (_istspace (*pp
)) /* Skip subsequent spaces */
175 _tcscat (pp
, _T(" ")); /* Add one space to ensure comparison ends */
177 while (*param
== *pp
) /* Comparison loop */
179 if (_istspace (*param
)) /* Terminates on space */
185 if (x_flag
^= (*param
!= *pp
) ? 0 : X_EXEC
)
187 while (*pp
&& !_istspace (*pp
)) /* Find first space, */
194 if (x_flag
& X_EMPTY
)
196 while (_istspace (*pp
)) /* Then skip spaces */
199 if (*pp
== _T('\0')) /* If nothing left then syntax err */
208 ParseCommandLine (pp
);