2 * REDIR.C - redirection handling.
7 * 12/15/95 (Tim Norman)
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.
14 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
15 * Added config.h include
17 * 22-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
19 * Added new error redirection "2>" and "2>>".
21 * 26-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
22 * Added new error AND output redirection "&>" and "&>>".
24 * 24-Jun-2005 (Brandon Turner <turnerb7@msu.edu>)
25 * simple check to fix > and | bug with 'rem'
30 #ifdef FEATURE_REDIRECTION
34 IsRedirection (TCHAR c
)
36 return (c
== _T('<')) || (c
== _T('>')) || (c
== _T('|'));
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
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
50 * Return number of command strings found.
54 INT
GetRedirection (LPTSTR s
, LPTSTR ifn
, LPTSTR ofn
, LPTSTR efn
, LPINT lpnFlags
)
60 #ifdef INCLUDE_CMD_REM
65 while (_istspace (*line
))
68 /*first thing first. check to see if this is "rem" and hope out*/
69 if(!_tcsncmp (line
, _T("rem "), 4))
78 /* find and remove all the redirections first */
87 if ((*sp
== _T('"')) || (*sp
== _T('\'')))
89 /* No redirects inside quotes */
94 while (*sp
&& *sp
!= qc
);
98 else if ((*sp
== _T('<')) || (*sp
== _T('>')) ||
99 (*sp
== _T('1')) || (*sp
== _T('2')) || (*sp
== _T('&')))
101 /* MS-DOS ignores multiple redirection symbols and uses the last */
102 /* redirection, so we'll emulate that and not check */
106 /* input redirection */
107 *lpnFlags
|= INPUT_REDIRECTION
;
109 while( _istspace (*sp
) );
112 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
116 else if (*sp
== _T('>'))
118 /* output redirection */
119 *lpnFlags
|= OUTPUT_REDIRECTION
;
122 /* append request ? */
125 *lpnFlags
|= OUTPUT_APPEND
;
129 while (_istspace (*sp
))
133 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
138 else if (*sp
== _T('1'))
140 /* output redirection */
145 /* output redirection */
146 *lpnFlags
|= OUTPUT_REDIRECTION
;
149 /* append request ? */
152 *lpnFlags
|= OUTPUT_APPEND
;
158 /* no redirection!! copy the '1' character! */
164 while (_istspace (*sp
))
168 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
173 else if (*sp
== _T('2'))
175 /* error redirection */
180 *lpnFlags
|= ERROR_REDIRECTION
;
183 /* append request ? */
186 *lpnFlags
|= ERROR_APPEND
;
192 /* no redirection!! copy the '2' character! */
198 while (_istspace (*sp
))
202 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
206 else if (*sp
== _T('&'))
208 /* output AND error redirection */
213 *lpnFlags
|= (ERROR_REDIRECTION
| OUTPUT_REDIRECTION
);
216 /* append request ? */
219 *lpnFlags
|= (ERROR_APPEND
| OUTPUT_APPEND
);
225 /* no redirection!! copy the '&' character! */
231 while (_istspace (*sp
))
235 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
236 *ofn
++ = *efn
++ = *sp
++;
237 *ofn
= *efn
= _T('\0');
247 /* now go for the pipes */
257 else if ((*sp
== _T('"')) || (*sp
== _T('\'')))
263 while (*sp
&& *sp
!= qc
);
267 else if (*sp
== _T('|'))
279 #endif /* FEATURE_REDIRECTION */