- Support whitespace around commands better, and support the @
[wine/dcerpc.git] / programs / wcmd / batch.c
blob6bb9c227a5e0b4a7e40bbec4f4d9093d91d8c586
1 /*
2 * WCMD - Wine-compatible command line interface - batch interface.
4 * (C) 1999 D A Pickles
6 */
9 #include "wcmd.h"
11 void WCMD_batch_command (char *line);
13 extern char nyi[];
14 extern char newline[];
15 extern char version_string[];
16 extern int echo_mode;
17 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
18 extern BATCH_CONTEXT *context;
22 /****************************************************************************
23 * WCMD_batch
25 * Open and execute a batch file.
26 * On entry *command includes the complete command line beginning with the name
27 * of the batch file (if a CALL command was entered the CALL has been removed).
28 * *file is the name of the file, which might not exist and may not have the
29 * .BAT suffix on. Called is 1 for a CALL, 0 otherwise.
31 * We need to handle recursion correctly, since one batch program might call another.
32 * So parameters for this batch file are held in a BATCH_CONTEXT structure.
35 void WCMD_batch (char *file, char *command, int called) {
37 HANDLE h;
38 char string[MAX_PATH];
39 BATCH_CONTEXT *prev_context;
41 strcpy (string, file);
42 CharLower (string);
43 if (strstr (string, ".bat") == NULL) strcat (string, ".bat");
44 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
45 if (h == INVALID_HANDLE_VALUE) {
46 WCMD_output ("File %s not found\n", string);
47 return;
51 * Create a context structure for this batch file.
54 prev_context = context;
55 context = (BATCH_CONTEXT *)LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT));
56 context -> h = h;
57 context -> command = command;
58 context -> shift_count = 0;
59 context -> prev_context = prev_context;
62 * Work through the file line by line. Specific batch commands are processed here,
63 * the rest are handled by the main command processor.
66 while (WCMD_fgets (string, sizeof(string), h)) {
67 if (string[0] != ':') { /* Skip over labels */
68 WCMD_batch_command (string);
71 CloseHandle (h);
74 * If invoked by a CALL, we return to the context of our caller. Otherwise return
75 * to the caller's caller.
78 LocalFree ((HANDLE)context);
79 if ((prev_context != NULL) && (!called)) {
80 CloseHandle (prev_context -> h);
81 context = prev_context -> prev_context;
82 LocalFree ((HANDLE)prev_context);
84 else {
85 context = prev_context;
89 /****************************************************************************
90 * WCMD_batch_command
92 * Execute one line from a batch file, expanding parameters.
95 void WCMD_batch_command (char *line) {
97 DWORD status;
98 char cmd1[1024],cmd2[1024];
99 char *p, *s, *t;
100 int i;
102 /* Get working version of command line */
103 strcpy(cmd1, line);
105 /* Expand environment variables in a batch file %{0-9} first */
106 /* Then env vars, and if any left (ie use of undefined vars,*/
107 /* replace with spaces */
108 /* FIXME: Winnt would replace %1%fred%1 with first parm, then */
109 /* contents of fred, then the digit 1. Would need to remove */
110 /* ExpandEnvStrings to achieve this */
112 /* Replace use of %0...%9 */
113 p = cmd1;
114 while ((p = strchr(p, '%'))) {
115 i = *(p+1) - '0';
116 if ((i >= 0) && (i <= 9)) {
117 s = strdup (p+2);
118 t = WCMD_parameter (context -> command, i + context -> shift_count, NULL);
119 strcpy (p, t);
120 strcat (p, s);
121 free (s);
122 } else {
123 p++;
127 /* Now replace environment variables */
128 status = ExpandEnvironmentStrings(cmd1, cmd2, sizeof(cmd2));
129 if (!status) {
130 WCMD_print_error ();
131 return;
134 /* In a batch program, unknown variables are replace by nothing */
135 /* so remove any remaining %var% */
136 p = cmd2;
137 while ((p = strchr(p, '%'))) {
138 s = strchr(p+1, '%');
139 if (!s) {
140 *p=0x00;
141 } else {
142 t = strdup(s+1);
143 strcpy(p, t);
144 free(t);
148 /* Show prompt before batch line IF echo is on */
149 if (echo_mode && (line[0] != '@')) {
150 WCMD_show_prompt();
151 WCMD_output ("%s\n", cmd2);
154 WCMD_process_command (cmd2);
157 /*******************************************************************
158 * WCMD_parameter - extract a parameter from a command line.
160 * Returns the 'n'th space-delimited parameter on the command line (zero-based).
161 * Parameter is in static storage overwritten on the next call.
162 * Parameters in quotes (and brackets) are handled.
163 * Also returns a pointer to the location of the parameter in the command line.
166 char *WCMD_parameter (char *s, int n, char **where) {
168 int i = 0;
169 static char param[MAX_PATH];
170 char *p;
172 p = param;
173 while (TRUE) {
174 switch (*s) {
175 case ' ':
176 s++;
177 break;
178 case '"':
179 if (where != NULL) *where = s;
180 s++;
181 while ((*s != '\0') && (*s != '"')) {
182 *p++ = *s++;
184 if (i == n) {
185 *p = '\0';
186 return param;
188 if (*s == '"') s++;
189 param[0] = '\0';
190 i++;
191 p = param;
192 break;
193 case '(':
194 if (where != NULL) *where = s;
195 s++;
196 while ((*s != '\0') && (*s != ')')) {
197 *p++ = *s++;
199 if (i == n) {
200 *p = '\0';
201 return param;
203 if (*s == ')') s++;
204 param[0] = '\0';
205 i++;
206 p = param;
207 break;
208 case '\0':
209 return param;
210 default:
211 if (where != NULL) *where = s;
212 while ((*s != '\0') && (*s != ' ')) {
213 *p++ = *s++;
215 if (i == n) {
216 *p = '\0';
217 return param;
219 param[0] = '\0';
220 i++;
221 p = param;
226 /****************************************************************************
227 * WCMD_fgets
229 * Get one line from a batch file. We can't use the native f* functions because
230 * of the filename syntax differences between DOS and Unix. Also need to lose
231 * the LF (or CRLF) from the line.
234 char *WCMD_fgets (char *s, int n, HANDLE h) {
236 DWORD bytes;
237 BOOL status;
238 char *p;
240 p = s;
241 do {
242 status = ReadFile (h, s, 1, &bytes, NULL);
243 if ((status == 0) || ((bytes == 0) && (s == p))) return NULL;
244 if (*s == '\n') bytes = 0;
245 else if (*s != '\r') {
246 s++;
247 n--;
249 *s = '\0';
250 } while ((bytes == 1) && (n > 1));
251 return p;