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
64 while (_istspace (*line
))
67 /*first thing first. check to see if this is "rem" and hope out*/
68 if(!_tcsncmp (line
, _T("rem "), 4))
77 /* find and remove all the redirections first */
80 if ((*sp
== _T('"')) || (*sp
== _T('\'')))
82 /* No redirects inside quotes */
87 while (*sp
&& *sp
!= qc
);
91 else if ((*sp
== _T('<')) || (*sp
== _T('>')) ||
92 (*sp
== _T('2')) || (*sp
== _T('&')))
94 /* MS-DOS ignores multiple redirection symbols and uses the last */
95 /* redirection, so we'll emulate that and not check */
99 /* input redirection */
100 *lpnFlags
|= INPUT_REDIRECTION
;
102 while( _istspace (*sp
) );
105 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
109 else if (*sp
== _T('>'))
111 /* output redirection */
112 *lpnFlags
|= OUTPUT_REDIRECTION
;
115 /* append request ? */
118 *lpnFlags
|= OUTPUT_APPEND
;
122 while (_istspace (*sp
))
126 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
130 else if (*sp
== _T('2'))
132 /* error redirection */
137 *lpnFlags
|= ERROR_REDIRECTION
;
140 /* append request ? */
143 *lpnFlags
|= ERROR_APPEND
;
149 /* no redirection!! copy the '2' character! */
155 while (_istspace (*sp
))
159 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
163 else if (*sp
== _T('&'))
165 /* output AND error redirection */
170 *lpnFlags
|= (ERROR_REDIRECTION
| OUTPUT_REDIRECTION
);
173 /* append request ? */
176 *lpnFlags
|= (ERROR_APPEND
| OUTPUT_APPEND
);
182 /* no redirection!! copy the '&' character! */
188 while (_istspace (*sp
))
192 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
193 *ofn
++ = *efn
++ = *sp
++;
194 *ofn
= *efn
= _T('\0');
203 /* now go for the pipes */
207 if ((*sp
== _T('"')) || (*sp
== _T('\'')))
213 while (*sp
&& *sp
!= qc
);
217 else if (*sp
== _T('|'))
229 #endif /* FEATURE_REDIRECTION */