enable theming of the checklist control
[reactos.git] / reactos / subsys / system / cmd / redir.c
1 /*
2 * REDIR.C - redirection handling.
3 *
4 *
5 * History:
6 *
7 * 12/15/95 (Tim Norman)
8 * started.
9 *
10 * 12 Jul 98 (Hans B Pufal)
11 * Rewrote to make more efficient and to conform to new command.c
12 * and batch.c processing.
13 *
14 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
15 * Added config.h include
16 *
17 * 22-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
18 * Unicode safe!
19 * Added new error redirection "2>" and "2>>".
20 *
21 * 26-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
22 * Added new error AND output redirection "&>" and "&>>".
23 *
24 * 24-Jun-2005 (Brandon Turner <turnerb7@msu.edu>)
25 * simple check to fix > and | bug with 'rem'
26 */
27
28 #include <precomp.h>
29
30 #ifdef FEATURE_REDIRECTION
31
32
33 static BOOL
34 IsRedirection (TCHAR c)
35 {
36 return (c == _T('<')) || (c == _T('>')) || (c == _T('|'));
37 }
38
39
40 /*
41 * Gets the redirection info from the command line and copies the
42 * file names into ifn, ofn and efn removing them from the command
43 * line.
44 *
45 * Converts remaining command line into a series of null terminated
46 * strings defined by the pipe char '|'. Each string corresponds
47 * to a single executable command. A double null terminates the
48 * command strings.
49 *
50 * Return number of command strings found.
51 *
52 */
53
54 INT GetRedirection (LPTSTR s, LPTSTR ifn, LPTSTR ofn, LPTSTR efn, LPINT lpnFlags)
55 {
56 INT num = 1;
57 LPTSTR dp = s;
58 LPTSTR sp = s;
59
60 #ifdef INCLUDE_CMD_REM
61
62 TCHAR * line = s;
63
64
65 while (_istspace (*line))
66 line++;
67
68 /*first thing first. check to see if this is "rem" and hope out*/
69 if(!_tcsncmp (line, _T("rem "), 4))
70 {
71 lpnFlags = 0;
72 *ifn=('\0');
73 *ofn=('\0');
74 *efn=_T('\0');
75 return 1;
76 }
77 #endif
78 /* find and remove all the redirections first */
79 while (*sp)
80 {
81 if ((*sp == _T('"')) || (*sp == _T('\'')))
82 {
83 /* No redirects inside quotes */
84 TCHAR qc = *sp;
85
86 do
87 *dp++ = *sp++;
88 while (*sp && *sp != qc);
89
90 *dp++ = *sp++;
91 }
92 else if ((*sp == _T('<')) || (*sp == _T('>')) ||
93 (*sp == _T('1')) || (*sp == _T('2')) || (*sp == _T('&')))
94 {
95 /* MS-DOS ignores multiple redirection symbols and uses the last */
96 /* redirection, so we'll emulate that and not check */
97
98 if (*sp == _T('<'))
99 {
100 /* input redirection */
101 *lpnFlags |= INPUT_REDIRECTION;
102 do sp++;
103 while( _istspace (*sp) );
104
105 /* copy file name */
106 while (*sp && !IsRedirection (*sp) && !_istspace (*sp))
107 *ifn++ = *sp++;
108 *ifn = _T('\0');
109 }
110 else if (*sp == _T('>'))
111 {
112 /* output redirection */
113 *lpnFlags |= OUTPUT_REDIRECTION;
114 sp++;
115
116 /* append request ? */
117 if (*sp == _T('>'))
118 {
119 *lpnFlags |= OUTPUT_APPEND;
120 sp++;
121 }
122
123 while (_istspace (*sp))
124 sp++;
125
126 /* copy file name */
127 while (*sp && !IsRedirection (*sp) && !_istspace (*sp))
128 *ofn++ = *sp++;
129 *ofn = _T('\0');
130 }
131
132 else if (*sp == _T('1'))
133 {
134 /* output redirection */
135 sp++;
136
137 if (*sp == _T('>'))
138 {
139 /* output redirection */
140 *lpnFlags |= OUTPUT_REDIRECTION;
141 sp++;
142
143 /* append request ? */
144 if (*sp == _T('>'))
145 {
146 *lpnFlags |= OUTPUT_APPEND;
147 sp++;
148 }
149 }
150 else
151 {
152 /* no redirection!! copy the '1' character! */
153 sp--;
154 *dp++ = *sp++;
155 continue;
156 }
157
158 while (_istspace (*sp))
159 sp++;
160
161 /* copy file name */
162 while (*sp && !IsRedirection (*sp) && !_istspace (*sp))
163 *ofn++ = *sp++;
164 *ofn = _T('\0');
165 }
166
167 else if (*sp == _T('2'))
168 {
169 /* error redirection */
170 sp++;
171
172 if (*sp == _T('>'))
173 {
174 *lpnFlags |= ERROR_REDIRECTION;
175 sp++;
176
177 /* append request ? */
178 if (*sp == _T('>'))
179 {
180 *lpnFlags |= ERROR_APPEND;
181 sp++;
182 }
183 }
184 else
185 {
186 /* no redirection!! copy the '2' character! */
187 sp--;
188 *dp++ = *sp++;
189 continue;
190 }
191
192 while (_istspace (*sp))
193 sp++;
194
195 /* copy file name */
196 while (*sp && !IsRedirection (*sp) && !_istspace (*sp))
197 *efn++ = *sp++;
198 *efn = _T('\0');
199 }
200 else if (*sp == _T('&'))
201 {
202 /* output AND error redirection */
203 sp++;
204
205 if (*sp == _T('>'))
206 {
207 *lpnFlags |= (ERROR_REDIRECTION | OUTPUT_REDIRECTION);
208 sp++;
209
210 /* append request ? */
211 if (*sp == _T('>'))
212 {
213 *lpnFlags |= (ERROR_APPEND | OUTPUT_APPEND);
214 sp++;
215 }
216 }
217 else
218 {
219 /* no redirection!! copy the '&' character! */
220 sp--;
221 *dp++ = *sp++;
222 continue;
223 }
224
225 while (_istspace (*sp))
226 sp++;
227
228 /* copy file name */
229 while (*sp && !IsRedirection (*sp) && !_istspace (*sp))
230 *ofn++ = *efn++ = *sp++;
231 *ofn = *efn = _T('\0');
232 }
233 }
234 else
235 *dp++ = *sp++;
236 }
237
238 *dp++ = _T('\0');
239 *dp = _T('\0');
240
241 /* now go for the pipes */
242 sp = s;
243 while (*sp)
244 {
245 if ((*sp == _T('"')) || (*sp == _T('\'')))
246 {
247 TCHAR qc = *sp;
248
249 do
250 sp++;
251 while (*sp && *sp != qc);
252
253 sp++;
254 }
255 else if (*sp == _T('|'))
256 {
257 *sp++ = _T('\0');
258 num++;
259 }
260 else
261 sp++;
262 }
263
264 return num;
265 }
266
267 #endif /* FEATURE_REDIRECTION */