Remove unneeded NULL cast.
[wine/wine-kai.git] / programs / wcmd / batch.c
blob36b59ca40b4831bfa05a81c5af489ec947e874b5
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 int echo_mode;
26 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
27 extern BATCH_CONTEXT *context;
28 extern DWORD errorlevel;
30 #define MAXSTRING 1024
32 /****************************************************************************
33 * WCMD_batch
35 * Open and execute a batch file.
36 * On entry *command includes the complete command line beginning with the name
37 * of the batch file (if a CALL command was entered the CALL has been removed).
38 * *file is the name of the file, which might not exist and may not have the
39 * .BAT suffix on. Called is 1 for a CALL, 0 otherwise.
41 * We need to handle recursion correctly, since one batch program might call another.
42 * So parameters for this batch file are held in a BATCH_CONTEXT structure.
45 void WCMD_batch (char *file, char *command, int called) {
47 #define WCMD_BATCH_EXT_SIZE 5
49 HANDLE h = INVALID_HANDLE_VALUE;
50 char string[MAXSTRING];
51 char extension_batch[][WCMD_BATCH_EXT_SIZE] = {".bat",".cmd"};
52 char extension_exe[WCMD_BATCH_EXT_SIZE] = ".exe";
53 unsigned int i;
54 BATCH_CONTEXT *prev_context;
56 for(i=0; (i<(sizeof(extension_batch)/WCMD_BATCH_EXT_SIZE)) &&
57 (h == INVALID_HANDLE_VALUE); i++) {
58 strcpy (string, file);
59 CharLower (string);
60 if (strstr (string, extension_batch[i]) == NULL) strcat (string, extension_batch[i]);
61 h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ,
62 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
64 if (h == INVALID_HANDLE_VALUE) {
65 strcpy (string, file);
66 CharLower (string);
67 if (strstr (string, extension_exe) == NULL) strcat (string, extension_exe);
68 h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ,
69 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
70 if (h != INVALID_HANDLE_VALUE) {
71 WCMD_run_program (command);
72 } else {
73 SetLastError (ERROR_FILE_NOT_FOUND);
74 WCMD_print_error ();
76 return;
80 * Create a context structure for this batch file.
83 prev_context = context;
84 context = (BATCH_CONTEXT *)LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT));
85 context -> h = h;
86 context -> command = command;
87 context -> shift_count = 0;
88 context -> prev_context = prev_context;
91 * Work through the file line by line. Specific batch commands are processed here,
92 * the rest are handled by the main command processor.
95 while (WCMD_fgets (string, sizeof(string), h)) {
96 if (strlen(string) == MAXSTRING -1)
97 WCMD_output("Line in Batch processing possible truncated. Using:\n%s\n",string);
98 if (string[0] != ':') { /* Skip over labels */
99 WCMD_batch_command (string);
102 CloseHandle (h);
105 * If invoked by a CALL, we return to the context of our caller. Otherwise return
106 * to the caller's caller.
109 LocalFree ((HANDLE)context);
110 if ((prev_context != NULL) && (!called)) {
111 CloseHandle (prev_context -> h);
112 context = prev_context -> prev_context;
113 LocalFree ((HANDLE)prev_context);
115 else {
116 context = prev_context;
120 /****************************************************************************
121 * WCMD_batch_command
123 * Execute one line from a batch file, expanding parameters.
126 void WCMD_batch_command (char *line) {
128 DWORD status;
129 char cmd1[MAXSTRING],cmd2[MAXSTRING];
130 char *p, *s, *t;
131 int i;
133 /* Get working version of command line */
134 strcpy(cmd1, line);
136 /* Expand environment variables in a batch file %{0-9} first */
137 /* Then env vars, and if any left (ie use of undefined vars,*/
138 /* replace with spaces */
139 /* FIXME: Winnt would replace %1%fred%1 with first parm, then */
140 /* contents of fred, then the digit 1. Would need to remove */
141 /* ExpandEnvStrings to achieve this */
143 /* Replace use of %0...%9 */
144 p = cmd1;
145 while ((p = strchr(p, '%'))) {
146 i = *(p+1) - '0';
147 if ((i >= 0) && (i <= 9)) {
148 s = strdup (p+2);
149 t = WCMD_parameter (context -> command, i + context -> shift_count, NULL);
150 strcpy (p, t);
151 strcat (p, s);
152 free (s);
153 } else {
154 p++;
158 /* Now replace environment variables */
159 status = ExpandEnvironmentStrings(cmd1, cmd2, sizeof(cmd2));
160 if (!status) {
161 WCMD_print_error ();
162 return;
165 /* In a batch program, unknown variables are replace by nothing */
166 /* so remove any remaining %var% */
167 p = cmd2;
168 while ((p = strchr(p, '%'))) {
169 s = strchr(p+1, '%');
170 if (!s) {
171 *p=0x00;
172 } else {
173 t = strdup(s+1);
174 strcpy(p, t);
175 free(t);
179 /* Show prompt before batch line IF echo is on */
180 if (echo_mode && (line[0] != '@')) {
181 WCMD_show_prompt();
182 WCMD_output ("%s\n", cmd2);
185 WCMD_process_command (cmd2);
188 /*******************************************************************
189 * WCMD_parameter - extract a parameter from a command line.
191 * Returns the 'n'th space-delimited parameter on the command line (zero-based).
192 * Parameter is in static storage overwritten on the next call.
193 * Parameters in quotes (and brackets) are handled.
194 * Also returns a pointer to the location of the parameter in the command line.
197 char *WCMD_parameter (char *s, int n, char **where) {
199 int i = 0;
200 static char param[MAX_PATH];
201 char *p;
203 p = param;
204 while (TRUE) {
205 switch (*s) {
206 case ' ':
207 s++;
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 '(':
225 if (where != NULL) *where = s;
226 s++;
227 while ((*s != '\0') && (*s != ')')) {
228 *p++ = *s++;
230 if (i == n) {
231 *p = '\0';
232 return param;
234 if (*s == ')') s++;
235 param[0] = '\0';
236 i++;
237 p = param;
238 break;
239 case '\0':
240 return param;
241 default:
242 if (where != NULL) *where = s;
243 while ((*s != '\0') && (*s != ' ')) {
244 *p++ = *s++;
246 if (i == n) {
247 *p = '\0';
248 return param;
250 param[0] = '\0';
251 i++;
252 p = param;
257 /****************************************************************************
258 * WCMD_fgets
260 * Get one line from a batch file. We can't use the native f* functions because
261 * of the filename syntax differences between DOS and Unix. Also need to lose
262 * the LF (or CRLF) from the line.
265 char *WCMD_fgets (char *s, int n, HANDLE h) {
267 DWORD bytes;
268 BOOL status;
269 char *p;
271 p = s;
272 do {
273 status = ReadFile (h, s, 1, &bytes, NULL);
274 if ((status == 0) || ((bytes == 0) && (s == p))) return NULL;
275 if (*s == '\n') bytes = 0;
276 else if (*s != '\r') {
277 s++;
278 n--;
280 *s = '\0';
281 } while ((bytes == 1) && (n > 1));
282 return p;