Fix signed/unsigned comparison warnings.
[wine] / programs / wcmd / batch.c
1 /*
2  * WCMD - Wine-compatible command line interface - batch interface.
3  *
4  * Copyright (C) 1999 D A Pickles
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "wcmd.h"
22
23 void WCMD_batch_command (char *line);
24
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;
29
30 #define MAXSTRING 1024
31
32 /****************************************************************************
33  * WCMD_batch
34  *
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.
40  *
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.
43  */
44
45 void WCMD_batch (char *file, char *command, int called) {
46
47 #define WCMD_BATCH_EXT_SIZE 5
48
49 HANDLE h = INVALID_HANDLE_VALUE;
50 char string[MAXSTRING];
51 char extension[][WCMD_BATCH_EXT_SIZE] = {".bat",".cmd"};
52 unsigned int  i;
53 BATCH_CONTEXT *prev_context;
54
55   for(i=0; (i<(sizeof(extension)/WCMD_BATCH_EXT_SIZE)) && 
56            (h == INVALID_HANDLE_VALUE); i++) {
57   strcpy (string, file);
58   CharLower (string);
59     if (strstr (string, extension[i]) == NULL) strcat (string, extension[i]);
60   h = CreateFile (string, GENERIC_READ, FILE_SHARE_READ,
61                   NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
62   }
63   if (h == INVALID_HANDLE_VALUE) {
64     SetLastError (ERROR_FILE_NOT_FOUND);
65     WCMD_print_error ();
66     return;
67   }
68
69 /*
70  *      Create a context structure for this batch file.
71  */
72
73   prev_context = context;
74   context = (BATCH_CONTEXT *)LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT));
75   context -> h = h;
76   context -> command = command;
77   context -> shift_count = 0;
78   context -> prev_context = prev_context;
79
80 /*
81  *      Work through the file line by line. Specific batch commands are processed here,
82  *      the rest are handled by the main command processor.
83  */
84
85   while (WCMD_fgets (string, sizeof(string), h)) {
86     if (strlen(string) == MAXSTRING -1)
87       WCMD_output("Line in Batch processing possible truncated. Using:\n%s\n",string);
88     if (string[0] != ':') {                      /* Skip over labels */
89       WCMD_batch_command (string);
90     }
91   }
92   CloseHandle (h);
93
94 /*
95  *      If invoked by a CALL, we return to the context of our caller. Otherwise return
96  *      to the caller's caller.
97  */
98
99   LocalFree ((HANDLE)context);
100   if ((prev_context != NULL) && (!called)) {
101     CloseHandle (prev_context -> h);
102     context = prev_context -> prev_context;
103     LocalFree ((HANDLE)prev_context);
104   }
105   else {
106     context = prev_context;
107   }
108 }
109
110 /****************************************************************************
111  * WCMD_batch_command
112  *
113  * Execute one line from a batch file, expanding parameters.
114  */
115
116 void WCMD_batch_command (char *line) {
117
118 DWORD status;
119 char cmd1[MAXSTRING],cmd2[MAXSTRING];
120 char *p, *s, *t;
121 int i;
122
123   /* Get working version of command line */
124   strcpy(cmd1, line);
125
126   /* Expand environment variables in a batch file %{0-9} first  */
127   /*   Then env vars, and if any left (ie use of undefined vars,*/
128   /*   replace with spaces                                      */
129   /* FIXME: Winnt would replace %1%fred%1 with first parm, then */
130   /*   contents of fred, then the digit 1. Would need to remove */
131   /*   ExpandEnvStrings to achieve this                         */
132
133   /* Replace use of %0...%9 */
134   p = cmd1;
135   while ((p = strchr(p, '%'))) {
136     i = *(p+1) - '0';
137     if ((i >= 0) && (i <= 9)) {
138       s = strdup (p+2);
139       t = WCMD_parameter (context -> command, i + context -> shift_count, NULL);
140       strcpy (p, t);
141       strcat (p, s);
142       free (s);
143     } else {
144       p++;
145     }
146   }
147
148   /* Now replace environment variables */
149   status = ExpandEnvironmentStrings(cmd1, cmd2, sizeof(cmd2));
150   if (!status) {
151     WCMD_print_error ();
152     return;
153   }
154
155   /* In a batch program, unknown variables are replace by nothing */
156   /* so remove any remaining %var%                                */
157   p = cmd2;
158   while ((p = strchr(p, '%'))) {
159     s = strchr(p+1, '%');
160     if (!s) {
161       *p=0x00;
162     } else {
163       t = strdup(s+1);
164       strcpy(p, t);
165       free(t);
166     }
167   }
168
169   /* Show prompt before batch line IF echo is on */
170   if (echo_mode && (line[0] != '@')) {
171     WCMD_show_prompt();
172     WCMD_output ("%s\n", cmd2);
173   }
174
175   WCMD_process_command (cmd2);
176 }
177
178 /*******************************************************************
179  * WCMD_parameter - extract a parameter from a command line.
180  *
181  *      Returns the 'n'th space-delimited parameter on the command line (zero-based).
182  *      Parameter is in static storage overwritten on the next call.
183  *      Parameters in quotes (and brackets) are handled.
184  *      Also returns a pointer to the location of the parameter in the command line.
185  */
186
187 char *WCMD_parameter (char *s, int n, char **where) {
188
189 int i = 0;
190 static char param[MAX_PATH];
191 char *p;
192
193   p = param;
194   while (TRUE) {
195     switch (*s) {
196       case ' ':
197         s++;
198         break;
199       case '"':
200         if (where != NULL) *where = s;
201         s++;
202         while ((*s != '\0') && (*s != '"')) {
203           *p++ = *s++;
204         }
205         if (i == n) {
206           *p = '\0';
207           return param;
208         }
209         if (*s == '"') s++;
210           param[0] = '\0';
211           i++;
212         p = param;
213         break;
214       case '(':
215         if (where != NULL) *where = s;
216         s++;
217         while ((*s != '\0') && (*s != ')')) {
218           *p++ = *s++;
219         }
220         if (i == n) {
221           *p = '\0';
222           return param;
223         }
224         if (*s == ')') s++;
225           param[0] = '\0';
226           i++;
227         p = param;
228         break;
229       case '\0':
230         return param;
231       default:
232         if (where != NULL) *where = s;
233         while ((*s != '\0') && (*s != ' ')) {
234           *p++ = *s++;
235         }
236         if (i == n) {
237           *p = '\0';
238           return param;
239         }
240           param[0] = '\0';
241           i++;
242         p = param;
243     }
244   }
245 }
246
247 /****************************************************************************
248  * WCMD_fgets
249  *
250  * Get one line from a batch file. We can't use the native f* functions because
251  * of the filename syntax differences between DOS and Unix. Also need to lose
252  * the LF (or CRLF) from the line.
253  */
254
255 char *WCMD_fgets (char *s, int n, HANDLE h) {
256
257 DWORD bytes;
258 BOOL status;
259 char *p;
260
261   p = s;
262   do {
263     status = ReadFile (h, s, 1, &bytes, NULL);
264     if ((status == 0) || ((bytes == 0) && (s == p))) return NULL;
265     if (*s == '\n') bytes = 0;
266     else if (*s != '\r') {
267       s++;
268       n--;
269     }
270     *s = '\0';
271   } while ((bytes == 1) && (n > 1));
272   return p;
273 }