WCMD_batch: increase possible line length and warn if still too small.
[wine/multimedia.git] / programs / wcmd / batch.c
blob4fd4b5977fff0ade1787ca5a7bd15f047def292a
1 /*
2 * WCMD - Wine-compatible command line interface - batch interface.
4 * Copyright (C) 1999 D A Pickles
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wcmd.h"
23 void WCMD_batch_command (char *line);
25 extern char nyi[];
26 extern char newline[];
27 extern char version_string[];
28 extern int echo_mode;
29 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
30 extern BATCH_CONTEXT *context;
31 extern DWORD errorlevel;
33 #define MAXSTRING 1024
35 /****************************************************************************
36 * WCMD_batch
38 * Open and execute a batch file.
39 * On entry *command includes the complete command line beginning with the name
40 * of the batch file (if a CALL command was entered the CALL has been removed).
41 * *file is the name of the file, which might not exist and may not have the
42 * .BAT suffix on. Called is 1 for a CALL, 0 otherwise.
44 * We need to handle recursion correctly, since one batch program might call another.
45 * So parameters for this batch file are held in a BATCH_CONTEXT structure.
48 void WCMD_batch (char *file, char *command, int called) {
50 HANDLE h;
51 char string[MAXSTRING];
52 BATCH_CONTEXT *prev_context;
54 strcpy (string, file);
55 CharLower (string);
56 if (strstr (string, ".bat") == NULL) strcat (string, ".bat");
57 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
58 if (h == INVALID_HANDLE_VALUE) {
59 SetLastError (ERROR_FILE_NOT_FOUND);
60 WCMD_print_error ();
61 return;
65 * Create a context structure for this batch file.
68 prev_context = context;
69 context = (BATCH_CONTEXT *)LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT));
70 context -> h = h;
71 context -> command = command;
72 context -> shift_count = 0;
73 context -> prev_context = prev_context;
76 * Work through the file line by line. Specific batch commands are processed here,
77 * the rest are handled by the main command processor.
80 while (WCMD_fgets (string, sizeof(string), h)) {
81 if (strlen(string) == MAXSTRING -1)
82 WCMD_output("Line in Batch processing possible truncated. Using:\n%s\n",string);
83 if (string[0] != ':') { /* Skip over labels */
84 WCMD_batch_command (string);
87 CloseHandle (h);
90 * If invoked by a CALL, we return to the context of our caller. Otherwise return
91 * to the caller's caller.
94 LocalFree ((HANDLE)context);
95 if ((prev_context != NULL) && (!called)) {
96 CloseHandle (prev_context -> h);
97 context = prev_context -> prev_context;
98 LocalFree ((HANDLE)prev_context);
100 else {
101 context = prev_context;
105 /****************************************************************************
106 * WCMD_batch_command
108 * Execute one line from a batch file, expanding parameters.
111 void WCMD_batch_command (char *line) {
113 DWORD status;
114 char cmd1[MAXSTRING],cmd2[MAXSTRING];
115 char *p, *s, *t;
116 int i;
118 /* Get working version of command line */
119 strcpy(cmd1, line);
121 /* Expand environment variables in a batch file %{0-9} first */
122 /* Then env vars, and if any left (ie use of undefined vars,*/
123 /* replace with spaces */
124 /* FIXME: Winnt would replace %1%fred%1 with first parm, then */
125 /* contents of fred, then the digit 1. Would need to remove */
126 /* ExpandEnvStrings to achieve this */
128 /* Replace use of %0...%9 */
129 p = cmd1;
130 while ((p = strchr(p, '%'))) {
131 i = *(p+1) - '0';
132 if ((i >= 0) && (i <= 9)) {
133 s = strdup (p+2);
134 t = WCMD_parameter (context -> command, i + context -> shift_count, NULL);
135 strcpy (p, t);
136 strcat (p, s);
137 free (s);
138 } else {
139 p++;
143 /* Now replace environment variables */
144 status = ExpandEnvironmentStrings(cmd1, cmd2, sizeof(cmd2));
145 if (!status) {
146 WCMD_print_error ();
147 return;
150 /* In a batch program, unknown variables are replace by nothing */
151 /* so remove any remaining %var% */
152 p = cmd2;
153 while ((p = strchr(p, '%'))) {
154 s = strchr(p+1, '%');
155 if (!s) {
156 *p=0x00;
157 } else {
158 t = strdup(s+1);
159 strcpy(p, t);
160 free(t);
164 /* Show prompt before batch line IF echo is on */
165 if (echo_mode && (line[0] != '@')) {
166 WCMD_show_prompt();
167 WCMD_output ("%s\n", cmd2);
170 WCMD_process_command (cmd2);
173 /*******************************************************************
174 * WCMD_parameter - extract a parameter from a command line.
176 * Returns the 'n'th space-delimited parameter on the command line (zero-based).
177 * Parameter is in static storage overwritten on the next call.
178 * Parameters in quotes (and brackets) are handled.
179 * Also returns a pointer to the location of the parameter in the command line.
182 char *WCMD_parameter (char *s, int n, char **where) {
184 int i = 0;
185 static char param[MAX_PATH];
186 char *p;
188 p = param;
189 while (TRUE) {
190 switch (*s) {
191 case ' ':
192 s++;
193 break;
194 case '"':
195 if (where != NULL) *where = s;
196 s++;
197 while ((*s != '\0') && (*s != '"')) {
198 *p++ = *s++;
200 if (i == n) {
201 *p = '\0';
202 return param;
204 if (*s == '"') s++;
205 param[0] = '\0';
206 i++;
207 p = param;
208 break;
209 case '(':
210 if (where != NULL) *where = s;
211 s++;
212 while ((*s != '\0') && (*s != ')')) {
213 *p++ = *s++;
215 if (i == n) {
216 *p = '\0';
217 return param;
219 if (*s == ')') s++;
220 param[0] = '\0';
221 i++;
222 p = param;
223 break;
224 case '\0':
225 return param;
226 default:
227 if (where != NULL) *where = s;
228 while ((*s != '\0') && (*s != ' ')) {
229 *p++ = *s++;
231 if (i == n) {
232 *p = '\0';
233 return param;
235 param[0] = '\0';
236 i++;
237 p = param;
242 /****************************************************************************
243 * WCMD_fgets
245 * Get one line from a batch file. We can't use the native f* functions because
246 * of the filename syntax differences between DOS and Unix. Also need to lose
247 * the LF (or CRLF) from the line.
250 char *WCMD_fgets (char *s, int n, HANDLE h) {
252 DWORD bytes;
253 BOOL status;
254 char *p;
256 p = s;
257 do {
258 status = ReadFile (h, s, 1, &bytes, NULL);
259 if ((status == 0) || ((bytes == 0) && (s == p))) return NULL;
260 if (*s == '\n') bytes = 0;
261 else if (*s != '\r') {
262 s++;
263 n--;
265 *s = '\0';
266 } while ((bytes == 1) && (n > 1));
267 return p;