7c9d7f0946b94d0ba209c38b0b54625fe4a85269
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)
39 * Perform FOR command.
41 * First check syntax is correct : FOR %v IN ( <list> ) DO <command>
42 * v must be alphabetic, <command> must not be empty.
44 * If all is correct build a new bcontext structure which preserves
45 * the necessary information so that readbatchline can expand
46 * each the command prototype for each list element.
48 * You might look on a FOR as being a called batch file with one line
52 INT
cmd_for (LPTSTR cmd
, LPTSTR param
)
54 LPBATCH_CONTEXT lpNew
;
59 DebugPrintf ("cmd_for (\'%s\', \'%s\'\n", cmd
, param
);
62 if (!_tcsncmp (param
, _T("/?"), 2))
64 ConOutPuts (_T("Runs a specified command for each file in a set of files\n"
66 "FOR %variable IN (set) DO command [parameters]\n"
68 " %variable Specifies a replaceable parameter.\n"
69 " (set) Specifies a set of one or more files. Wildcards may be used.\n"
70 " command Specifies the command to carry out for each file.\n"
71 " parameters Specifies parameters or switches for the specified command.\n"
73 "To user the FOR comamnd in a batch program, specify %%variable instead of\n"
78 /* Check that first element is % then an alpha char followed by space */
79 if ((*param
!= _T('%')) || !_istalpha (*(param
+ 1)) || !_istspace (*(param
+ 2)))
81 error_syntax (_T("bad variable specification."));
86 var
= *param
++; /* Save FOR var name */
88 while (_istspace (*param
))
91 /* Check next element is 'IN' */
92 if ((_tcsnicmp (param
, _T("in"), 2) != 0) || !_istspace (*(param
+ 2)))
94 error_syntax (_T("'in' missing in for statement."));
99 while (_istspace (*param
))
102 /* Folowed by a '(', find also matching ')' */
103 if ((*param
!= _T('(')) || (NULL
== (pp
= _tcsrchr (param
, _T(')')))))
105 error_syntax (_T("no brackets found."));
110 param
++; /* param now points at null terminated list */
112 while (_istspace (*pp
))
115 /* Check DO follows */
116 if ((_tcsnicmp (pp
, _T("do"), 2) != 0) || !_istspace (*(pp
+ 2)))
118 error_syntax (_T("'do' missing."));
123 while (_istspace (*pp
))
126 /* Check that command tail is not empty */
129 error_syntax (_T("no command after 'do'."));
133 /* OK all is correct, build a bcontext.... */
134 lpNew
= (LPBATCH_CONTEXT
)malloc (sizeof (BATCH_CONTEXT
));
139 bc
->hBatchFile
= INVALID_HANDLE_VALUE
;
141 bc
->params
= BatchParams (_T(""), param
); /* Split out list */
144 bc
->forproto
= _tcsdup (pp
);