Added configure check for libutil.h.
[wine.git] / programs / wcmd / batch.c
blobcc29853b3830e9436d8174d8d5d12be9d4c2df4c
1 /*
2 * WCMD - Wine-compatible command line interface - batch interface.
4 * (C) 1999 D A Pickles
6 */
9 #include "wcmd.h"
11 void WCMD_batch_command (char *line);
13 extern char nyi[];
14 extern char newline[];
15 extern char version_string[];
16 extern int echo_mode;
17 extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
18 extern BATCH_CONTEXT *context;
22 /****************************************************************************
23 * WCMD_batch
25 * Open and execute a batch file.
26 * On entry *command includes the complete command line beginning with the name
27 * of the batch file (if a CALL command was entered the CALL has been removed).
28 * *file is the name of the file, which might not exist and may not have the
29 * .BAT suffix on. Called is 1 for a CALL, 0 otherwise.
31 * We need to handle recursion correctly, since one batch program might call another.
32 * So parameters for this batch file are held in a BATCH_CONTEXT structure.
35 void WCMD_batch (char *file, char *command, int called) {
37 HANDLE h;
38 char string[MAX_PATH];
39 BATCH_CONTEXT *prev_context;
41 strcpy (string, file);
42 CharLower (string);
43 if (strstr (string, ".bat") == NULL) strcat (string, ".bat");
44 h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
45 if (h == INVALID_HANDLE_VALUE) {
46 WCMD_output ("File %s not found\n", string);
47 return;
51 * Create a context structure for this batch file.
54 prev_context = context;
55 context = (BATCH_CONTEXT *)LocalAlloc (LMEM_FIXED, sizeof (BATCH_CONTEXT));
56 context -> h = h;
57 context -> command = command;
58 context -> shift_count = 0;
59 context -> prev_context = prev_context;
62 * Work through the file line by line. Specific batch commands are processed here,
63 * the rest are handled by the main command processor.
66 while (WCMD_fgets (string, sizeof(string), h)) {
67 if (string[0] != ':') { /* Skip over labels */
68 WCMD_batch_command (string);
71 CloseHandle (h);
74 * If invoked by a CALL, we return to the context of our caller. Otherwise return
75 * to the caller's caller.
78 LocalFree ((HANDLE)context);
79 if ((prev_context != NULL) && (!called)) {
80 CloseHandle (prev_context -> h);
81 context = prev_context -> prev_context;
82 LocalFree ((HANDLE)prev_context);
84 else {
85 context = prev_context;
89 /****************************************************************************
90 * WCMD_batch_command
92 * Execute one line from a batch file, expanding parameters.
95 void WCMD_batch_command (char *line) {
97 DWORD status;
98 char cmd[1024];
99 char *p, *s, *t;
100 int i;
102 if (echo_mode && (line[0] != '@')) WCMD_output ("%s", line);
103 status = ExpandEnvironmentStrings (line, cmd, sizeof(cmd));
104 if (!status) {
105 WCMD_print_error ();
106 return;
109 p = cmd;
110 while ((p = strchr(p, '%'))) {
111 i = *(p+1) - '0';
112 if ((i >= 0) && (i <= 9)) {
113 s = strdup (p+2);
114 t = WCMD_parameter (context -> command, i + context -> shift_count, NULL);
115 strcpy (p, t);
116 strcat (p, s);
117 free (s);
120 WCMD_process_command (cmd);
123 /*******************************************************************
124 * WCMD_parameter - extract a parameter from a command line.
126 * Returns the 'n'th space-delimited parameter on the command line (zero-based).
127 * Parameter is in static storage overwritten on the next call.
128 * Parameters in quotes (and brackets) are handled.
129 * Also returns a pointer to the location of the parameter in the command line.
132 char *WCMD_parameter (char *s, int n, char **where) {
134 int i = 0;
135 static char param[MAX_PATH];
136 char *p;
138 p = param;
139 while (TRUE) {
140 switch (*s) {
141 case ' ':
142 s++;
143 break;
144 case '"':
145 if (where != NULL) *where = s;
146 s++;
147 while ((*s != '\0') && (*s != '"')) {
148 *p++ = *s++;
150 if (i == n) {
151 *p = '\0';
152 return param;
154 if (*s == '"') s++;
155 param[0] = '\0';
156 i++;
157 p = param;
158 break;
159 case '(':
160 if (where != NULL) *where = s;
161 s++;
162 while ((*s != '\0') && (*s != ')')) {
163 *p++ = *s++;
165 if (i == n) {
166 *p = '\0';
167 return param;
169 if (*s == ')') s++;
170 param[0] = '\0';
171 i++;
172 p = param;
173 break;
174 case '\0':
175 return param;
176 default:
177 if (where != NULL) *where = s;
178 while ((*s != '\0') && (*s != ' ')) {
179 *p++ = *s++;
181 if (i == n) {
182 *p = '\0';
183 return param;
185 param[0] = '\0';
186 i++;
187 p = param;
192 /****************************************************************************
193 * WCMD_fgets
195 * Get one line from a batch file. We can't use the native f* functions because
196 * of the filename syntax differences between DOS and Unix. Also need to lose
197 * the LF (or CRLF) from the line.
200 char *WCMD_fgets (char *s, int n, HANDLE h) {
202 DWORD bytes;
203 BOOL status;
204 char *p;
206 p = s;
207 do {
208 status = ReadFile (h, s, 1, &bytes, NULL);
209 if ((status == 0) || ((bytes == 0) && (s == p))) return NULL;
210 if (*s == '\n') bytes = 0;
211 else if (*s != '\r') {
212 s++;
213 n--;
215 *s = '\0';
216 } while ((bytes == 1) && (n > 1));
217 return p;