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)
17 * Added help text ("if /?") and cleaned up.
19 * 21-Jan-1999 (Eric Kohl)
20 * Unicode and redirection ready!
22 * 01-Sep-1999 (Eric Kohl)
26 * IF DEFINED variable command
28 * 28-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
29 * Remove all hardcode string to En.rc
39 INT
cmd_if (LPTSTR cmd
, LPTSTR param
)
41 INT x_flag
= 0; /* when set cause 'then' clause to be executed */
45 DebugPrintf (_T("cmd_if: (\'%s\', \'%s\')\n"), cmd
, param
);
48 if (!_tcsncmp (param
, _T("/?"), 2))
50 ConOutResPaging(TRUE
,STRING_IF_HELP1
);
54 /* First check if param string begins with 'not' */
55 if (!_tcsnicmp (param
, _T("not"), 3) && _istspace (*(param
+ 3)))
57 x_flag
= X_EXEC
; /* Remember 'NOT' */
58 param
+= 3; /* Step over 'NOT' */
59 while (_istspace (*param
)) /* And subsequent spaces */
63 /* Check for 'exist' form */
64 if (!_tcsnicmp (param
, _T("exist"), 5) && _istspace (*(param
+ 5)))
70 while (_istspace (*param
))
75 /* find the whole path to the file */
76 for(i
= 0; i
< _tcslen(param
); i
++)
78 if(param
[i
] == _T('\"'))
80 if((param
[i
] == _T(' ')) && !bInside
)
89 while(i
< _tcslen(param
))
91 if(param
[i
] == _T('\"'))
92 memmove(¶m
[i
],¶m
[i
+ 1], _tcslen(¶m
[i
]) * sizeof(TCHAR
));
102 hFind
= FindFirstFile (param
, &f
);
103 x_flag
^= (hFind
== INVALID_HANDLE_VALUE
) ? 0 : X_EXEC
;
104 if (hFind
!= INVALID_HANDLE_VALUE
)
112 else if (!_tcsnicmp (param
, _T("defined"), 7) && _istspace (*(param
+ 7)))
114 /* Check for 'defined' form */
119 /* IF [NOT] DEFINED var COMMAND */
121 while (_istspace (*param
))
123 /* IF [NOT] DEFINED var COMMAND */
126 while (*pp
&& !_istspace (*pp
))
128 /* IF [NOT] DEFINED var COMMAND */
133 ValueSize
= GetEnvironmentVariable(param
, Value
, sizeof(Value
) / sizeof(Value
[0]));
134 x_flag
^= (0 == ValueSize
)
142 else if (!_tcsnicmp (param
, _T("errorlevel"), 10) && _istspace (*(param
+ 10)))
144 /* Check for 'errorlevel' form */
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 */
160 BOOL bInQuote
= FALSE
;
163 while ( *pp
&& ( bInQuote
|| *pp
!= _T('=') ) )
165 if ( *pp
== _T('\"') )
166 bInQuote
= !bInQuote
;
171 if ( *pp
++ != _T('=') || *pp
++ != _T('=') )
173 error_syntax ( NULL
);
176 while (_istspace (*pp
)) /* Skip subsequent spaces */
179 /* are the two sides equal*/
180 if ( !_tcsncmp(param
,pp
,p1len
))
184 while (_istspace (*pp
)) /* skip spaces */
191 bc
->bCmdBlock
= TRUE
;
192 bc
->bExecuteBlock
= x_flag
& X_EXEC
;
202 if (x_flag
& X_EMPTY
)
204 while (_istspace (*pp
)) /* Then skip spaces */
207 if (*pp
== _T('\0')) /* If nothing left then syntax err */
216 ParseCommandLine (pp
);