e921c03193cee70eff402555e0fd81d852e8f7ec
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>)
41 INT
cmd_if (LPTSTR cmd
, LPTSTR param
)
43 INT x_flag
= 0; /* when set cause 'then' clause to be executed */
47 DebugPrintf ("cmd_if: (\'%s\', \'%s\'\n", cmd
, param
);
50 if (!_tcsncmp (param
, _T("/?"), 2))
52 ConOutPuts (_T("Performs conditional processing in batch programs.\n"
54 " IF [NOT] ERRORLEVEL number command\n"
55 " IF [NOT] string1==string2 command\n"
56 " IF [NOT] EXIST filename command\n"
58 "NOT Specifies that CMD should carry out the command only if\n"
59 " the condition is false\n"
60 "ERRORLEVEL number Specifies a true condition if the last program run returned\n"
61 " an exit code equal or greater than the number specified.\n"
62 "command Specifies the command to carry out if the condition is met.\n"
63 "string1==string2 Specifies a true condition if the specified text strings\n"
65 "EXIST filename Specifies a true condition if the specified filename exists."));
69 /* First check if param string begins with word 'not' */
70 if (!_tcsnicmp (param
, _T("not"), 3) && _istspace (*(param
+ 3)))
72 x_flag
= X_EXEC
; /* Remember 'NOT' */
73 param
+= 3; /* Step over 'NOT' */
74 while (_istspace (*param
)) /* And subsequent spaces */
78 /* Check for 'exist' form */
79 if (!_tcsnicmp (param
, _T("exist"), 5) && _istspace (*(param
+ 5)))
82 while (_istspace (*param
))
86 while (*pp
&& !_istspace (*pp
))
95 hFind
= FindFirstFile (param
, &f
);
96 x_flag
^= (hFind
!= INVALID_HANDLE_VALUE
) ? 0 : X_EXEC
;
97 if (hFind
!= INVALID_HANDLE_VALUE
)
104 /* Check for 'errorlevel' form */
105 else if (!_tcsnicmp (param
, _T("errorlevel"), 10) && _istspace (*(param
+ 10)))
110 while (_istspace (*pp
))
113 while (_istdigit (*pp
))
114 n
= n
* 10 + (*pp
++ - _T('0'));
116 x_flag
^= (nErrorLevel
< n
) ? 0 : X_EXEC
;
118 x_flag
|= X_EMPTY
; /* Syntax error if comd empty */
121 /* Check that '==' is present, syntax error if not */
122 else if (NULL
== (pp
= _tcsstr (param
, _T("=="))))
130 /* Change first '='to space to terminate comparison loop */
132 *pp
= _T(' '); /* Need a space to terminate comparison loop */
133 pp
+= 2; /* over '==' */
134 while (_istspace (*pp
)) /* Skip subsequent spaces */
137 _tcscat (pp
, _T(" ")); /* Add one space to ensure comparison ends */
139 while (*param
== *pp
) /* Comparison loop */
141 if (_istspace (*param
)) /* Terminates on space */
147 if (x_flag
^= (*param
!= *pp
) ? 0 : X_EXEC
)
149 while (*pp
&& !_istspace (*pp
)) /* Find first space, */
156 if (x_flag
& X_EMPTY
)
158 while (_istspace (*pp
)) /* Then skip spaces */
161 if (*pp
== _T('\0')) /* If nothing left then syntax err */
169 ParseCommandLine (pp
);