server: Added access rights mapping to process and thread objects.
[wine/multimedia.git] / programs / wcmd / batch.c
blob89d50aa4db17783c7aea6d1487074f8de0c59dcb
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_asis( "Line in Batch processing possible truncated. Using:\n");
98 WCMD_output_asis( string);
99 WCMD_output_asis( "\n");
100 if (string[0] != ':') { /* Skip over labels */
101 WCMD_batch_command (string);
104 CloseHandle (h);
107 * If invoked by a CALL, we return to the context of our caller. Otherwise return
108 * to the caller's caller.
111 LocalFree ((HANDLE)context);
112 if ((prev_context != NULL) && (!called)) {
113 CloseHandle (prev_context -> h);
114 context = prev_context -> prev_context;
115 LocalFree ((HANDLE)prev_context);
117 else {
118 context = prev_context;
122 /****************************************************************************
123 * WCMD_batch_command
125 * Execute one line from a batch file, expanding parameters.
128 void WCMD_batch_command (char *line) {
130 DWORD status;
131 char cmd1[MAXSTRING],cmd2[MAXSTRING];
132 char *p, *s, *t;
133 int i;
135 /* Get working version of command line */
136 strcpy(cmd1, line);
138 /* Expand environment variables in a batch file %{0-9} first */
139 /* Then env vars, and if any left (ie use of undefined vars,*/
140 /* replace with spaces */
141 /* FIXME: Winnt would replace %1%fred%1 with first parm, then */
142 /* contents of fred, then the digit 1. Would need to remove */
143 /* ExpandEnvStrings to achieve this */
145 /* Replace use of %0...%9 */
146 p = cmd1;
147 while ((p = strchr(p, '%'))) {
148 i = *(p+1) - '0';
149 if ((i >= 0) && (i <= 9)) {
150 s = strdup (p+2);
151 t = WCMD_parameter (context -> command, i + context -> shift_count, NULL);
152 strcpy (p, t);
153 strcat (p, s);
154 free (s);
155 } else {
156 p++;
160 /* Now replace environment variables */
161 status = ExpandEnvironmentStrings(cmd1, cmd2, sizeof(cmd2));
162 if (!status) {
163 WCMD_print_error ();
164 return;
167 /* In a batch program, unknown variables are replace by nothing */
168 /* so remove any remaining %var% */
169 p = cmd2;
170 while ((p = strchr(p, '%'))) {
171 s = strchr(p+1, '%');
172 if (!s) {
173 *p=0x00;
174 } else {
175 t = strdup(s+1);
176 strcpy(p, t);
177 free(t);
181 /* Show prompt before batch line IF echo is on */
182 if (echo_mode && (line[0] != '@')) {
183 WCMD_show_prompt();
184 WCMD_output_asis ( cmd2);
185 WCMD_output_asis ( "\n");
188 WCMD_process_command (cmd2);
191 /*******************************************************************
192 * WCMD_parameter - extract a parameter from a command line.
194 * Returns the 'n'th space-delimited parameter on the command line (zero-based).
195 * Parameter is in static storage overwritten on the next call.
196 * Parameters in quotes (and brackets) are handled.
197 * Also returns a pointer to the location of the parameter in the command line.
200 char *WCMD_parameter (char *s, int n, char **where) {
202 int i = 0;
203 static char param[MAX_PATH];
204 char *p;
206 p = param;
207 while (TRUE) {
208 switch (*s) {
209 case ' ':
210 s++;
211 break;
212 case '"':
213 if (where != NULL) *where = s;
214 s++;
215 while ((*s != '\0') && (*s != '"')) {
216 *p++ = *s++;
218 if (i == n) {
219 *p = '\0';
220 return param;
222 if (*s == '"') s++;
223 param[0] = '\0';
224 i++;
225 p = param;
226 break;
227 case '(':
228 if (where != NULL) *where = s;
229 s++;
230 while ((*s != '\0') && (*s != ')')) {
231 *p++ = *s++;
233 if (i == n) {
234 *p = '\0';
235 return param;
237 if (*s == ')') s++;
238 param[0] = '\0';
239 i++;
240 p = param;
241 break;
242 case '\0':
243 return param;
244 default:
245 if (where != NULL) *where = s;
246 while ((*s != '\0') && (*s != ' ')) {
247 *p++ = *s++;
249 if (i == n) {
250 *p = '\0';
251 return param;
253 param[0] = '\0';
254 i++;
255 p = param;
260 /****************************************************************************
261 * WCMD_fgets
263 * Get one line from a batch file. We can't use the native f* functions because
264 * of the filename syntax differences between DOS and Unix. Also need to lose
265 * the LF (or CRLF) from the line.
268 char *WCMD_fgets (char *s, int n, HANDLE h) {
270 DWORD bytes;
271 BOOL status;
272 char *p;
274 p = s;
275 do {
276 status = ReadFile (h, s, 1, &bytes, NULL);
277 if ((status == 0) || ((bytes == 0) && (s == p))) return NULL;
278 if (*s == '\n') bytes = 0;
279 else if (*s != '\r') {
280 s++;
281 n--;
283 *s = '\0';
284 } while ((bytes == 1) && (n > 1));
285 return p;