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