Bug 2988: slovak translation for downloader by kario@szm.sk
[reactos.git] / reactos / base / shell / cmd / if.c
1 /*
2 * IF.C - if 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 * 07-Jan-1999 (Eric Kohl)
17 * Added help text ("if /?") and cleaned up.
18 *
19 * 21-Jan-1999 (Eric Kohl)
20 * Unicode and redirection ready!
21 *
22 * 01-Sep-1999 (Eric Kohl)
23 * Fixed help text.
24 *
25 * 17-Feb-2001 (ea)
26 * IF DEFINED variable command
27 *
28 * 28-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
29 * Remove all hardcode string to En.rc
30 *
31 */
32
33 #include <precomp.h>
34
35
36 #define X_EXEC 1
37 #define X_EMPTY 0x80
38
39 INT cmd_if (LPTSTR cmd, LPTSTR param)
40 {
41 INT x_flag = 0; /* when set cause 'then' clause to be executed */
42 LPTSTR pp;
43
44 #ifdef _DEBUG
45 DebugPrintf (_T("cmd_if: (\'%s\', \'%s\')\n"), cmd, param);
46 #endif
47
48 if (!_tcsncmp (param, _T("/?"), 2))
49 {
50 ConOutResPaging(TRUE,STRING_IF_HELP1);
51 return 0;
52 }
53
54 /* First check if param string begins with 'not' */
55 if (!_tcsnicmp (param, _T("not"), 3) && _istspace (*(param + 3)))
56 {
57 x_flag = X_EXEC; /* Remember 'NOT' */
58 param += 3; /* Step over 'NOT' */
59 while (_istspace (*param)) /* And subsequent spaces */
60 param++;
61 }
62
63 /* Check for 'exist' form */
64 if (!_tcsnicmp (param, _T("exist"), 5) && _istspace (*(param + 5)))
65 {
66 UINT i;
67 BOOL bInside = FALSE;
68
69 param += 5;
70 while (_istspace (*param))
71 param++;
72
73 pp = param;
74
75 /* find the whole path to the file */
76 for(i = 0; i < _tcslen(param); i++)
77 {
78 if(param[i] == _T('\"'))
79 bInside = !bInside;
80 if((param[i] == _T(' ')) && !bInside)
81 {
82 break;
83 }
84 pp++;
85 }
86 *pp++ = _T('\0');
87 i = 0;
88 /* remove quotes */
89 while(i < _tcslen(param))
90 {
91 if(param[i] == _T('\"'))
92 memmove(&param[i],&param[i + 1], _tcslen(&param[i]) * sizeof(TCHAR));
93 else
94 i++;
95 }
96
97 if (*pp)
98 {
99 WIN32_FIND_DATA f;
100 HANDLE hFind;
101
102 hFind = FindFirstFile (param, &f);
103 x_flag ^= (hFind == INVALID_HANDLE_VALUE) ? 0 : X_EXEC;
104 if (hFind != INVALID_HANDLE_VALUE)
105 {
106 FindClose (hFind);
107 }
108 }
109 else
110 return 0;
111 }
112 else if (!_tcsnicmp (param, _T("defined"), 7) && _istspace (*(param + 7)))
113 {
114 /* Check for 'defined' form */
115 TCHAR Value [1];
116 INT ValueSize = 0;
117
118 param += 7;
119 /* IF [NOT] DEFINED var COMMAND */
120 /* ^ */
121 while (_istspace (*param))
122 param++;
123 /* IF [NOT] DEFINED var COMMAND */
124 /* ^ */
125 pp = param;
126 while (*pp && !_istspace (*pp))
127 pp++;
128 /* IF [NOT] DEFINED var COMMAND */
129 /* ^ */
130 if (*pp)
131 {
132 *pp++ = _T('\0');
133 ValueSize = GetEnvironmentVariable(param, Value, sizeof(Value) / sizeof(Value[0]));
134 x_flag ^= (0 == ValueSize)
135 ? 0
136 : X_EXEC;
137 x_flag |= X_EMPTY;
138 }
139 else
140 return 0;
141 }
142 else if (!_tcsnicmp (param, _T("errorlevel"), 10) && _istspace (*(param + 10)))
143 {
144 /* Check for 'errorlevel' form */
145 INT n = 0;
146
147 pp = param + 10;
148 while (_istspace (*pp))
149 pp++;
150
151 while (_istdigit (*pp))
152 n = n * 10 + (*pp++ - _T('0'));
153
154 x_flag ^= (nErrorLevel != n) ? 0 : X_EXEC;
155
156 x_flag |= X_EMPTY; /* Syntax error if comd empty */
157 }
158 else
159 {
160 BOOL bInQuote = FALSE;
161 INT p1len;
162 pp = param;
163 while ( *pp && ( bInQuote || *pp != _T('=') ) )
164 {
165 if ( *pp == _T('\"') )
166 bInQuote = !bInQuote;
167 ++pp;
168 }
169 p1len = pp-param;
170 /* check for "==" */
171 if ( *pp++ != _T('=') || *pp++ != _T('=') )
172 {
173 error_syntax ( NULL );
174 return 1;
175 }
176 while (_istspace (*pp)) /* Skip subsequent spaces */
177 pp++;
178
179 /* are the two sides equal*/
180 if ( !_tcsncmp(param,pp,p1len))
181 x_flag ^= X_EXEC;
182 pp += p1len;
183
184 while (_istspace (*pp)) /* skip spaces */
185 pp++;
186
187 if (*pp == _T('('))
188 {
189 if (bc)
190 {
191 bc->bCmdBlock = TRUE;
192 bc->bExecuteBlock = x_flag & X_EXEC;
193 }
194 }
195
196 if ( x_flag )
197 {
198 x_flag |= X_EMPTY;
199 }
200 }
201
202 if (x_flag & X_EMPTY)
203 {
204 while (_istspace (*pp)) /* Then skip spaces */
205 pp++;
206
207 if (*pp == _T('\0')) /* If nothing left then syntax err */
208 {
209 error_syntax (NULL);
210 return 1;
211 }
212 }
213
214 if (x_flag & X_EXEC)
215 {
216 ParseCommandLine (pp);
217 }
218
219 return 0;
220 }
221
222 /* EOF */