3e292a3ba1a2dde2792baf68c323fe6aa59ba02e
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.
43 * Perform FOR command.
45 * First check syntax is correct : FOR %v IN ( <list> ) DO <command>
46 * v must be alphabetic, <command> must not be empty.
48 * If all is correct build a new bcontext structure which preserves
49 * the necessary information so that readbatchline can expand
50 * each the command prototype for each list element.
52 * You might look on a FOR as being a called batch file with one line
56 INT
cmd_for (LPTSTR cmd
, LPTSTR param
)
58 LPBATCH_CONTEXT lpNew
;
63 DebugPrintf ("cmd_for (\'%s\', \'%s\'\n", cmd
, param
);
66 if (!_tcsncmp (param
, _T("/?"), 2))
68 ConOutPuts (_T("Runs a specified command for each file in a set of files\n"
70 "FOR %variable IN (set) DO command [parameters]\n"
72 " %variable Specifies a replaceable parameter.\n"
73 " (set) Specifies a set of one or more files. Wildcards may be used.\n"
74 " command Specifies the command to carry out for each file.\n"
75 " parameters Specifies parameters or switches for the specified command.\n"
77 "To user the FOR comamnd in a batch program, specify %%variable instead of\n"
82 /* Check that first element is % then an alpha char followed by space */
83 if ((*param
!= _T('%')) || !_istalpha (*(param
+ 1)) || !_istspace (*(param
+ 2)))
85 error_syntax (_T("bad variable specification."));
90 var
= *param
++; /* Save FOR var name */
92 while (_istspace (*param
))
95 /* Check next element is 'IN' */
96 if ((_tcsnicmp (param
, _T("in"), 2) != 0) || !_istspace (*(param
+ 2)))
98 error_syntax (_T("'in' missing in for statement."));
103 while (_istspace (*param
))
106 /* Folowed by a '(', find also matching ')' */
107 if ((*param
!= _T('(')) || (NULL
== (pp
= _tcsrchr (param
, _T(')')))))
109 error_syntax (_T("no brackets found."));
114 param
++; /* param now points at null terminated list */
116 while (_istspace (*pp
))
119 /* Check DO follows */
120 if ((_tcsnicmp (pp
, _T("do"), 2) != 0) || !_istspace (*(pp
+ 2)))
122 error_syntax (_T("'do' missing."));
127 while (_istspace (*pp
))
130 /* Check that command tail is not empty */
133 error_syntax (_T("no command after 'do'."));
137 /* OK all is correct, build a bcontext.... */
138 lpNew
= (LPBATCH_CONTEXT
)malloc (sizeof (BATCH_CONTEXT
));
143 bc
->hBatchFile
= INVALID_HANDLE_VALUE
;
145 bc
->params
= BatchParams (_T(""), param
); /* Split out list */
148 bc
->forproto
= _tcsdup (pp
);
150 bc
->bEcho
= bc
->prev
->bEcho
;