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 */
81 if ((*sp
== _T('"')) || (*sp
== _T('\'')))
83 /* No redirects inside quotes */
88 while (*sp
&& *sp
!= qc
);
92 else if ((*sp
== _T('<')) || (*sp
== _T('>')) ||
93 (*sp
== _T('1')) || (*sp
== _T('2')) || (*sp
== _T('&')))
95 /* MS-DOS ignores multiple redirection symbols and uses the last */
96 /* redirection, so we'll emulate that and not check */
100 /* input redirection */
101 *lpnFlags
|= INPUT_REDIRECTION
;
103 while( _istspace (*sp
) );
106 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
110 else if (*sp
== _T('>'))
112 /* output redirection */
113 *lpnFlags
|= OUTPUT_REDIRECTION
;
116 /* append request ? */
119 *lpnFlags
|= OUTPUT_APPEND
;
123 while (_istspace (*sp
))
127 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
132 else if (*sp
== _T('1'))
134 /* output redirection */
139 /* output redirection */
140 *lpnFlags
|= OUTPUT_REDIRECTION
;
143 /* append request ? */
146 *lpnFlags
|= OUTPUT_APPEND
;
152 /* no redirection!! copy the '1' character! */
158 while (_istspace (*sp
))
162 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
167 else if (*sp
== _T('2'))
169 /* error redirection */
174 *lpnFlags
|= ERROR_REDIRECTION
;
177 /* append request ? */
180 *lpnFlags
|= ERROR_APPEND
;
186 /* no redirection!! copy the '2' character! */
192 while (_istspace (*sp
))
196 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
200 else if (*sp
== _T('&'))
202 /* output AND error redirection */
207 *lpnFlags
|= (ERROR_REDIRECTION
| OUTPUT_REDIRECTION
);
210 /* append request ? */
213 *lpnFlags
|= (ERROR_APPEND
| OUTPUT_APPEND
);
219 /* no redirection!! copy the '&' character! */
225 while (_istspace (*sp
))
229 while (*sp
&& !IsRedirection (*sp
) && !_istspace (*sp
))
230 *ofn
++ = *efn
++ = *sp
++;
231 *ofn
= *efn
= _T('\0');
241 /* now go for the pipes */
245 if ((*sp
== _T('"')) || (*sp
== _T('\'')))
251 while (*sp
&& *sp
!= qc
);
255 else if (*sp
== _T('|'))
267 #endif /* FEATURE_REDIRECTION */