2 * FOR.C - for internal batch command.
7 * 16-Jul-1998 (Hans B Pufal)
10 * 16-Jul-1998 (John P Price)
11 * Seperated commands into individual files.
13 * 19-Jul-1998 (Hans B Pufal)
14 * Implementation of FOR.
16 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
17 * Added config.h include.
19 * 20-Jan-1999 (Eric Kohl)
20 * Unicode and redirection safe!
22 * 01-Sep-1999 (Eric Kohl)
25 * 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
26 * Implemented preservation of echo flag. Some other for related
27 * code in other files fixed, too.
29 * 28-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
30 * Remove all hardcode string to En.rc
38 * Perform FOR command.
40 * First check syntax is correct : FOR %v IN ( <list> ) DO <command>
41 * v must be alphabetic, <command> must not be empty.
43 * If all is correct build a new bcontext structure which preserves
44 * the necessary information so that readbatchline can expand
45 * each the command prototype for each list element.
47 * You might look on a FOR as being a called batch file with one line
51 INT
cmd_for (LPTSTR cmd
, LPTSTR param
)
53 LPBATCH_CONTEXT lpNew
;
56 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
59 DebugPrintf (_T("cmd_for (\'%s\', \'%s\'\n"), cmd
, param
);
62 if (!_tcsncmp (param
, _T("/?"), 2))
64 ConOutResPaging(TRUE
,STRING_FOR_HELP1
);
68 /* Check that first element is % then an alpha char followed by space */
69 if ((*param
!= _T('%')) || !_istalpha (*(param
+ 1)) || !_istspace (*(param
+ 2)))
71 LoadString( CMD_ModuleHandle
, STRING_FOR_ERROR
, szMsg
, RC_STRING_MAX_SIZE
);
77 var
= *param
++; /* Save FOR var name */
79 while (_istspace (*param
))
82 /* Check next element is 'IN' */
83 if ((_tcsnicmp (param
, _T("in"), 2) != 0) || !_istspace (*(param
+ 2)))
85 LoadString(CMD_ModuleHandle
, STRING_FOR_ERROR1
, szMsg
, RC_STRING_MAX_SIZE
);
91 while (_istspace (*param
))
94 /* Folowed by a '(', find also matching ')' */
95 if ((*param
!= _T('(')) || (NULL
== (pp
= _tcsrchr (param
, _T(')')))))
97 LoadString(CMD_ModuleHandle
, STRING_FOR_ERROR2
, szMsg
, RC_STRING_MAX_SIZE
);
103 param
++; /* param now points at null terminated list */
105 while (_istspace (*pp
))
108 /* Check DO follows */
109 if ((_tcsnicmp (pp
, _T("do"), 2) != 0) || !_istspace (*(pp
+ 2)))
111 LoadString(CMD_ModuleHandle
, STRING_FOR_ERROR3
, szMsg
, RC_STRING_MAX_SIZE
);
117 while (_istspace (*pp
))
120 /* Check that command tail is not empty */
123 LoadString(CMD_ModuleHandle
, STRING_FOR_ERROR4
, szMsg
, RC_STRING_MAX_SIZE
);
128 /* OK all is correct, build a bcontext.... */
129 lpNew
= (LPBATCH_CONTEXT
)malloc (sizeof (BATCH_CONTEXT
));
134 bc
->hBatchFile
= INVALID_HANDLE_VALUE
;
136 bc
->params
= BatchParams (_T(""), param
); /* Split out list */
139 bc
->forproto
= _tcsdup (pp
);
141 bc
->bEcho
= bc
->prev
->bEcho
;
144 bc
->In
[0] = _T('\0');
145 bc
->Out
[0] = _T('\0');
146 bc
->Err
[0] = _T('\0');