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