Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * WCMD - Wine-compatible command line interface - built-in functions. |
| 3 | * |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999 D A Pickles |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 5 | * |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 | /* |
| 22 | * NOTES: |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 23 | * On entry to each function, global variables quals, param1, param2 contain |
| 24 | * the qualifiers (uppercased and concatenated) and parameters entered, with |
| 25 | * environment-variable and batch parameter substitution already done. |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * FIXME: |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 30 | * - No support for pipes, shell parameters |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 31 | * - Lots of functionality missing from builtins |
| 32 | * - Messages etc need international support |
| 33 | */ |
| 34 | |
| 35 | #include "wcmd.h" |
| 36 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 37 | void WCMD_execute (char *orig_command, char *parameter, char *substitution); |
| 38 | |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 39 | extern HINSTANCE hinst; |
| 40 | extern char *inbuilt[]; |
| 41 | extern char nyi[]; |
| 42 | extern char newline[]; |
| 43 | extern char version_string[]; |
| 44 | extern char anykey[]; |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 45 | extern int echo_mode, verify_mode; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 46 | extern char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH]; |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 47 | extern BATCH_CONTEXT *context; |
Dave Pickles | ebecf50 | 2000-08-01 22:02:18 +0000 | [diff] [blame] | 48 | extern DWORD errorlevel; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 49 | |
| 50 | |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 51 | |
| 52 | /**************************************************************************** |
| 53 | * WCMD_clear_screen |
| 54 | * |
| 55 | * Clear the terminal screen. |
| 56 | */ |
| 57 | |
| 58 | void WCMD_clear_screen () { |
| 59 | |
| 60 | WCMD_output (nyi); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | /**************************************************************************** |
| 65 | * WCMD_change_tty |
| 66 | * |
| 67 | * Change the default i/o device (ie redirect STDin/STDout). |
| 68 | */ |
| 69 | |
| 70 | void WCMD_change_tty () { |
| 71 | |
| 72 | WCMD_output (nyi); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | /**************************************************************************** |
| 77 | * WCMD_copy |
| 78 | * |
| 79 | * Copy a file or wildcarded set. |
| 80 | * FIXME: No wildcard support |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | void WCMD_copy () { |
| 84 | |
| 85 | DWORD count; |
| 86 | WIN32_FIND_DATA fd; |
| 87 | HANDLE hff; |
| 88 | BOOL force, status; |
| 89 | static char *overwrite = "Overwrite file (Y/N)?"; |
Dave Pickles | ebecf50 | 2000-08-01 22:02:18 +0000 | [diff] [blame] | 90 | char string[8], outpath[MAX_PATH], inpath[MAX_PATH], *infile; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 91 | |
| 92 | if ((strchr(param1,'*') != NULL) && (strchr(param1,'%') != NULL)) { |
| 93 | WCMD_output ("Wildcards not yet supported\n"); |
| 94 | return; |
| 95 | } |
| 96 | GetFullPathName (param2, sizeof(outpath), outpath, NULL); |
Dave Pickles | ebecf50 | 2000-08-01 22:02:18 +0000 | [diff] [blame] | 97 | hff = FindFirstFile (outpath, &fd); |
| 98 | if (hff != INVALID_HANDLE_VALUE) { |
| 99 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 100 | GetFullPathName (param1, sizeof(inpath), inpath, &infile); |
| 101 | strcat (outpath, "\\"); |
| 102 | strcat (outpath, infile); |
| 103 | } |
| 104 | FindClose (hff); |
| 105 | } |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 106 | force = (strstr (quals, "/Y") != NULL); |
| 107 | if (!force) { |
| 108 | hff = FindFirstFile (outpath, &fd); |
| 109 | if (hff != INVALID_HANDLE_VALUE) { |
| 110 | FindClose (hff); |
| 111 | WCMD_output (overwrite); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 112 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 113 | if (toupper(string[0]) == 'Y') force = TRUE; |
| 114 | } |
| 115 | else force = TRUE; |
| 116 | } |
| 117 | if (force) { |
| 118 | status = CopyFile (param1, outpath, FALSE); |
| 119 | if (!status) WCMD_print_error (); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /**************************************************************************** |
| 124 | * WCMD_create_dir |
| 125 | * |
| 126 | * Create a directory. |
| 127 | */ |
| 128 | |
| 129 | void WCMD_create_dir () { |
| 130 | |
| 131 | if (!CreateDirectory (param1, NULL)) WCMD_print_error (); |
| 132 | } |
| 133 | |
| 134 | /**************************************************************************** |
| 135 | * WCMD_delete |
| 136 | * |
| 137 | * Delete a file or wildcarded set. |
| 138 | * |
| 139 | */ |
| 140 | |
| 141 | void WCMD_delete (int recurse) { |
| 142 | |
| 143 | WIN32_FIND_DATA fd; |
| 144 | HANDLE hff; |
| 145 | char fpath[MAX_PATH]; |
| 146 | char *p; |
| 147 | |
| 148 | hff = FindFirstFile (param1, &fd); |
| 149 | if (hff == INVALID_HANDLE_VALUE) { |
| 150 | WCMD_output ("File Not Found\n"); |
| 151 | return; |
| 152 | } |
| 153 | if ((strchr(param1,'*') == NULL) && (strchr(param1,'?') == NULL) |
| 154 | && (!recurse) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 155 | strcat (param1, "\\*"); |
| 156 | WCMD_delete (1); |
| 157 | return; |
| 158 | } |
| 159 | if ((strchr(param1,'*') != NULL) || (strchr(param1,'?') != NULL)) { |
| 160 | strcpy (fpath, param1); |
| 161 | do { |
| 162 | p = strrchr (fpath, '\\'); |
| 163 | if (p != NULL) { |
| 164 | *++p = '\0'; |
| 165 | strcat (fpath, fd.cFileName); |
| 166 | } |
| 167 | else strcpy (fpath, fd.cFileName); |
| 168 | if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 169 | if (!DeleteFile (fpath)) WCMD_print_error (); |
| 170 | } |
| 171 | } while (FindNextFile(hff, &fd) != 0); |
| 172 | FindClose (hff); |
| 173 | } |
| 174 | else { |
| 175 | if (!DeleteFile (param1)) WCMD_print_error (); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /**************************************************************************** |
| 180 | * WCMD_echo |
| 181 | * |
| 182 | * Echo input to the screen (or not). We don't try to emulate the bugs |
| 183 | * in DOS (try typing "ECHO ON AGAIN" for an example). |
| 184 | */ |
| 185 | |
| 186 | void WCMD_echo (char *command) { |
| 187 | |
| 188 | static char *eon = "Echo is ON\n", *eoff = "Echo is OFF\n"; |
| 189 | int count; |
| 190 | |
| 191 | count = strlen(command); |
| 192 | if (count == 0) { |
| 193 | if (echo_mode) WCMD_output (eon); |
| 194 | else WCMD_output (eoff); |
| 195 | return; |
| 196 | } |
| 197 | if ((count == 1) && (command[0] == '.')) { |
| 198 | WCMD_output (newline); |
| 199 | return; |
| 200 | } |
| 201 | if (lstrcmpi(command, "ON") == 0) { |
| 202 | echo_mode = 1; |
| 203 | return; |
| 204 | } |
| 205 | if (lstrcmpi(command, "OFF") == 0) { |
| 206 | echo_mode = 0; |
| 207 | return; |
| 208 | } |
Jason Edmeades | a5910f4 | 2000-08-01 02:14:33 +0000 | [diff] [blame] | 209 | WCMD_output_asis (command); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 210 | WCMD_output (newline); |
| 211 | |
| 212 | } |
| 213 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 214 | /************************************************************************** |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 215 | * WCMD_for |
| 216 | * |
| 217 | * Batch file loop processing. |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 218 | * FIXME: We don't exhaustively check syntax. Any command which works in MessDOS |
| 219 | * will probably work here, but the reverse is not necessarily the case... |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 220 | */ |
| 221 | |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 222 | void WCMD_for (char *p) { |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 223 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 224 | WIN32_FIND_DATA fd; |
| 225 | HANDLE hff; |
| 226 | char *cmd, *item; |
| 227 | char set[MAX_PATH], param[MAX_PATH]; |
| 228 | int i; |
| 229 | |
| 230 | if (lstrcmpi (WCMD_parameter (p, 1, NULL), "in") |
| 231 | || lstrcmpi (WCMD_parameter (p, 3, NULL), "do") |
| 232 | || (param1[0] != '%')) { |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 233 | WCMD_output ("Syntax error\n"); |
| 234 | return; |
| 235 | } |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 236 | lstrcpyn (set, WCMD_parameter (p, 2, NULL), sizeof(set)); |
| 237 | WCMD_parameter (p, 4, &cmd); |
| 238 | lstrcpy (param, param1); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 239 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 240 | /* |
| 241 | * If the parameter within the set has a wildcard then search for matching files |
| 242 | * otherwise do a literal substitution. |
| 243 | */ |
| 244 | |
| 245 | i = 0; |
| 246 | while (*(item = WCMD_parameter (set, i, NULL))) { |
| 247 | if (strpbrk (item, "*?")) { |
| 248 | hff = FindFirstFile (item, &fd); |
| 249 | if (hff == INVALID_HANDLE_VALUE) { |
| 250 | return; |
| 251 | } |
| 252 | do { |
| 253 | WCMD_execute (cmd, param, fd.cFileName); |
| 254 | } while (FindNextFile(hff, &fd) != 0); |
| 255 | FindClose (hff); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 256 | } |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 257 | else { |
| 258 | WCMD_execute (cmd, param, item); |
| 259 | } |
| 260 | i++; |
| 261 | } |
| 262 | } |
| 263 | |
Dave Pickles | ebecf50 | 2000-08-01 22:02:18 +0000 | [diff] [blame] | 264 | /***************************************************************************** |
| 265 | * WCMD_Execute |
| 266 | * |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 267 | * Execute a command after substituting variable text for the supplied parameter |
| 268 | */ |
| 269 | |
| 270 | void WCMD_execute (char *orig_cmd, char *param, char *subst) { |
| 271 | |
| 272 | char *new_cmd, *p, *s, *dup; |
| 273 | int size; |
| 274 | |
| 275 | size = lstrlen (orig_cmd); |
| 276 | new_cmd = (char *) LocalAlloc (LMEM_FIXED | LMEM_ZEROINIT, size); |
| 277 | dup = s = strdup (orig_cmd); |
| 278 | |
| 279 | while ((p = strstr (s, param))) { |
| 280 | *p = '\0'; |
| 281 | size += lstrlen (subst); |
| 282 | new_cmd = (char *) LocalReAlloc ((HANDLE)new_cmd, size, 0); |
| 283 | strcat (new_cmd, s); |
| 284 | strcat (new_cmd, subst); |
| 285 | s = p + lstrlen (param); |
| 286 | } |
| 287 | strcat (new_cmd, s); |
| 288 | WCMD_process_command (new_cmd); |
| 289 | free (dup); |
| 290 | LocalFree ((HANDLE)new_cmd); |
| 291 | } |
| 292 | |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 293 | |
| 294 | /************************************************************************** |
| 295 | * WCMD_give_help |
| 296 | * |
| 297 | * Simple on-line help. Help text is stored in the resource file. |
| 298 | */ |
| 299 | |
| 300 | void WCMD_give_help (char *command) { |
| 301 | |
| 302 | int i; |
| 303 | char buffer[2048]; |
| 304 | |
| 305 | command = WCMD_strtrim_leading_spaces(command); |
| 306 | if (lstrlen(command) == 0) { |
Alexandre Julliard | bc4b88f | 2000-04-19 16:47:20 +0000 | [diff] [blame] | 307 | LoadString (0, 1000, buffer, sizeof(buffer)); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 308 | WCMD_output (buffer); |
| 309 | } |
| 310 | else { |
| 311 | for (i=0; i<=WCMD_EXIT; i++) { |
| 312 | if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, |
Dave Pickles | ebecf50 | 2000-08-01 22:02:18 +0000 | [diff] [blame] | 313 | param1, -1, inbuilt[i], -1) == 2) { |
| 314 | LoadString (hinst, i, buffer, sizeof(buffer)); |
| 315 | WCMD_output (buffer); |
| 316 | return; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | WCMD_output ("No help available for %s\n", param1); |
| 320 | } |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | /**************************************************************************** |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 325 | * WCMD_go_to |
| 326 | * |
| 327 | * Batch file jump instruction. Not the most efficient algorithm ;-) |
| 328 | * Prints error message if the specified label cannot be found - the file pointer is |
| 329 | * then at EOF, effectively stopping the batch file. |
| 330 | * FIXME: DOS is supposed to allow labels with spaces - we don't. |
| 331 | */ |
| 332 | |
| 333 | void WCMD_goto () { |
| 334 | |
| 335 | char string[MAX_PATH]; |
| 336 | |
| 337 | if (context != NULL) { |
| 338 | SetFilePointer (context -> h, 0, NULL, FILE_BEGIN); |
| 339 | while (WCMD_fgets (string, sizeof(string), context -> h)) { |
| 340 | if ((string[0] == ':') && (strcmp (&string[1], param1) == 0)) return; |
| 341 | } |
| 342 | WCMD_output ("Target to GOTO not found\n"); |
| 343 | } |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /**************************************************************************** |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 349 | * WCMD_if |
| 350 | * |
| 351 | * Batch file conditional. |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 352 | * FIXME: Much more syntax checking needed! |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 353 | */ |
| 354 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 355 | void WCMD_if (char *p) { |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 356 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 357 | HANDLE h; |
| 358 | int negate = 0, test = 0; |
| 359 | char condition[MAX_PATH], *command, *s; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 360 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 361 | if (!lstrcmpi (param1, "not")) { |
| 362 | negate = 1; |
| 363 | lstrcpy (condition, param2); |
| 364 | } |
| 365 | else { |
| 366 | lstrcpy (condition, param1); |
| 367 | } |
| 368 | if (!lstrcmpi (condition, "errorlevel")) { |
Dave Pickles | ebecf50 | 2000-08-01 22:02:18 +0000 | [diff] [blame] | 369 | if (errorlevel >= atoi(WCMD_parameter (p, 1+negate, NULL))) test = 1; |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 370 | return; |
| 371 | } |
| 372 | else if (!lstrcmpi (condition, "exist")) { |
| 373 | if ((h = CreateFile (WCMD_parameter (p, 1+negate, NULL), GENERIC_READ, 0, NULL, |
François Gouget | 12b3526 | 2001-01-02 20:40:58 +0000 | [diff] [blame] | 374 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) { |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 375 | CloseHandle (h); |
| 376 | test = 1; |
| 377 | } |
| 378 | } |
| 379 | else if ((s = strstr (p, "=="))) { |
| 380 | s += 2; |
| 381 | if (!lstrcmpi (condition, WCMD_parameter (s, 0, NULL))) test = 1; |
| 382 | } |
| 383 | else { |
| 384 | WCMD_output ("Syntax error\n"); |
| 385 | return; |
| 386 | } |
| 387 | if (test != negate) { |
| 388 | WCMD_parameter (p, 2+negate, &s); |
| 389 | command = strdup (s); |
| 390 | WCMD_process_command (command); |
| 391 | free (command); |
| 392 | } |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /**************************************************************************** |
| 396 | * WCMD_move |
| 397 | * |
| 398 | * Move a file, directory tree or wildcarded set of files. |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 399 | * FIXME: Needs input and output files to be fully specified. |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 400 | */ |
| 401 | |
| 402 | void WCMD_move () { |
| 403 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 404 | int status; |
| 405 | |
| 406 | if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) { |
| 407 | WCMD_output ("Wildcards not yet supported\n"); |
| 408 | return; |
| 409 | } |
| 410 | status = MoveFile (param1, param2); |
| 411 | if (!status) WCMD_print_error (); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /**************************************************************************** |
| 415 | * WCMD_pause |
| 416 | * |
| 417 | * Wait for keyboard input. |
| 418 | */ |
| 419 | |
| 420 | void WCMD_pause () { |
| 421 | |
| 422 | DWORD count; |
| 423 | char string[32]; |
| 424 | |
| 425 | WCMD_output (anykey); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 426 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /**************************************************************************** |
| 430 | * WCMD_remove_dir |
| 431 | * |
| 432 | * Delete a directory. |
| 433 | */ |
| 434 | |
| 435 | void WCMD_remove_dir () { |
| 436 | |
| 437 | if (!RemoveDirectory (param1)) WCMD_print_error (); |
| 438 | } |
| 439 | |
| 440 | /**************************************************************************** |
| 441 | * WCMD_rename |
| 442 | * |
| 443 | * Rename a file. |
| 444 | * FIXME: Needs input and output files to be fully specified. |
| 445 | */ |
| 446 | |
| 447 | void WCMD_rename () { |
| 448 | |
| 449 | int status; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 450 | |
| 451 | if ((strchr(param1,'*') != NULL) || (strchr(param1,'%') != NULL)) { |
| 452 | WCMD_output ("Wildcards not yet supported\n"); |
| 453 | return; |
| 454 | } |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 455 | status = MoveFile (param1, param2); |
| 456 | if (!status) WCMD_print_error (); |
| 457 | } |
| 458 | |
| 459 | /***************************************************************************** |
| 460 | * WCMD_setshow_attrib |
| 461 | * |
| 462 | * Display and optionally sets DOS attributes on a file or directory |
| 463 | * |
| 464 | * FIXME: Wine currently uses the Unix stat() function to get file attributes. |
| 465 | * As a result only the Readonly flag is correctly reported, the Archive bit |
| 466 | * is always set and the rest are not implemented. We do the Right Thing anyway. |
| 467 | * |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 468 | * FIXME: No SET functionality. |
| 469 | * |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 470 | */ |
| 471 | |
| 472 | void WCMD_setshow_attrib () { |
| 473 | |
| 474 | DWORD count; |
| 475 | HANDLE hff; |
| 476 | WIN32_FIND_DATA fd; |
| 477 | char flags[9] = {" "}; |
| 478 | |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 479 | if (param1[0] == '-') { |
| 480 | WCMD_output (nyi); |
| 481 | return; |
| 482 | } |
| 483 | |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 484 | if (lstrlen(param1) == 0) { |
| 485 | GetCurrentDirectory (sizeof(param1), param1); |
| 486 | strcat (param1, "\\*"); |
| 487 | } |
| 488 | |
| 489 | hff = FindFirstFile (param1, &fd); |
| 490 | if (hff == INVALID_HANDLE_VALUE) { |
| 491 | WCMD_output ("File Not Found\n"); |
| 492 | } |
| 493 | else { |
| 494 | do { |
| 495 | if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 496 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) { |
| 497 | flags[0] = 'H'; |
| 498 | } |
| 499 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) { |
| 500 | flags[1] = 'S'; |
| 501 | } |
| 502 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) { |
| 503 | flags[2] = 'A'; |
| 504 | } |
| 505 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { |
| 506 | flags[3] = 'R'; |
| 507 | } |
| 508 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) { |
| 509 | flags[4] = 'T'; |
| 510 | } |
| 511 | if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) { |
| 512 | flags[5] = 'C'; |
| 513 | } |
| 514 | WCMD_output ("%s %s\n", flags, fd.cFileName); |
| 515 | for (count=0; count < 8; count++) flags[count] = ' '; |
| 516 | } |
| 517 | } while (FindNextFile(hff, &fd) != 0); |
| 518 | } |
| 519 | FindClose (hff); |
| 520 | } |
| 521 | |
| 522 | /***************************************************************************** |
| 523 | * WCMD_setshow_default |
| 524 | * |
| 525 | * Set/Show the current default directory |
| 526 | */ |
| 527 | |
| 528 | void WCMD_setshow_default () { |
| 529 | |
| 530 | BOOL status; |
| 531 | char string[1024]; |
| 532 | |
| 533 | if (strlen(param1) == 0) { |
| 534 | GetCurrentDirectory (sizeof(string), string); |
| 535 | strcat (string, "\n"); |
| 536 | WCMD_output (string); |
| 537 | } |
| 538 | else { |
| 539 | status = SetCurrentDirectory (param1); |
| 540 | if (!status) { |
| 541 | WCMD_print_error (); |
| 542 | return; |
| 543 | } |
| 544 | } |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | /**************************************************************************** |
| 549 | * WCMD_setshow_date |
| 550 | * |
| 551 | * Set/Show the system date |
| 552 | * FIXME: Can't change date yet |
| 553 | */ |
| 554 | |
| 555 | void WCMD_setshow_date () { |
| 556 | |
| 557 | char curdate[64], buffer[64]; |
| 558 | DWORD count; |
| 559 | |
| 560 | if (lstrlen(param1) == 0) { |
| 561 | if (GetDateFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, |
| 562 | curdate, sizeof(curdate))) { |
| 563 | WCMD_output ("Current Date is %s\nEnter new date: ", curdate); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 564 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 565 | if (count > 2) { |
| 566 | WCMD_output (nyi); |
| 567 | } |
| 568 | } |
| 569 | else WCMD_print_error (); |
| 570 | } |
| 571 | else { |
| 572 | WCMD_output (nyi); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /**************************************************************************** |
| 577 | * WCMD_setshow_env |
| 578 | * |
| 579 | * Set/Show the environment variables |
| 580 | * |
| 581 | * FIXME: need to sort variables into order for display? |
| 582 | */ |
| 583 | |
| 584 | void WCMD_setshow_env (char *s) { |
| 585 | |
| 586 | LPVOID env; |
| 587 | char *p; |
| 588 | int status; |
Jason Edmeades | a5910f4 | 2000-08-01 02:14:33 +0000 | [diff] [blame] | 589 | char buffer[1048]; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 590 | |
| 591 | if (strlen(param1) == 0) { |
| 592 | env = GetEnvironmentStrings (); |
| 593 | p = (char *) env; |
| 594 | while (*p) { |
| 595 | WCMD_output ("%s\n", p); |
| 596 | p += lstrlen(p) + 1; |
| 597 | } |
| 598 | } |
| 599 | else { |
| 600 | p = strchr (s, '='); |
| 601 | if (p == NULL) { |
Jason Edmeades | a5910f4 | 2000-08-01 02:14:33 +0000 | [diff] [blame] | 602 | |
| 603 | /* FIXME: Emulate Win98 for now, ie "SET C" looks ONLY for an |
| 604 | environment variable C, whereas on NT it shows ALL variables |
| 605 | starting with C. |
| 606 | */ |
| 607 | status = GetEnvironmentVariable(s, buffer, sizeof(buffer)); |
| 608 | if (status) { |
| 609 | WCMD_output("%s=%s\n", s, buffer); |
| 610 | } else { |
| 611 | WCMD_output ("Environment variable %s not defined\n", s); |
| 612 | } |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 613 | return; |
| 614 | } |
| 615 | *p++ = '\0'; |
Jason Edmeades | a5910f4 | 2000-08-01 02:14:33 +0000 | [diff] [blame] | 616 | |
| 617 | if (strlen(p) == 0) p = 0x00; |
| 618 | status = SetEnvironmentVariable (s, p); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 619 | if (!status) WCMD_print_error(); |
| 620 | } |
Jason Edmeades | a5910f4 | 2000-08-01 02:14:33 +0000 | [diff] [blame] | 621 | /* WCMD_output (newline); @JED*/ |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /**************************************************************************** |
| 625 | * WCMD_setshow_path |
| 626 | * |
| 627 | * Set/Show the path environment variable |
| 628 | */ |
| 629 | |
| 630 | void WCMD_setshow_path () { |
| 631 | |
| 632 | char string[1024]; |
| 633 | DWORD status; |
| 634 | |
| 635 | if (strlen(param1) == 0) { |
| 636 | status = GetEnvironmentVariable ("PATH", string, sizeof(string)); |
| 637 | if (status != 0) { |
| 638 | WCMD_output ("PATH=%s\n", string); |
| 639 | } |
| 640 | else { |
| 641 | WCMD_output ("PATH not found\n"); |
| 642 | } |
| 643 | } |
| 644 | else { |
| 645 | status = SetEnvironmentVariable ("PATH", param1); |
| 646 | if (!status) WCMD_print_error(); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /**************************************************************************** |
| 651 | * WCMD_setshow_prompt |
| 652 | * |
| 653 | * Set or show the command prompt. |
| 654 | */ |
| 655 | |
| 656 | void WCMD_setshow_prompt () { |
| 657 | |
| 658 | char *s; |
| 659 | |
| 660 | if (strlen(param1) == 0) { |
| 661 | SetEnvironmentVariable ("PROMPT", NULL); |
| 662 | } |
| 663 | else { |
| 664 | s = param1; |
| 665 | while ((*s == '=') || (*s == ' ')) s++; |
| 666 | if (strlen(s) == 0) { |
| 667 | SetEnvironmentVariable ("PROMPT", NULL); |
| 668 | } |
| 669 | else SetEnvironmentVariable ("PROMPT", s); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /**************************************************************************** |
| 674 | * WCMD_setshow_time |
| 675 | * |
| 676 | * Set/Show the system time |
| 677 | * FIXME: Can't change time yet |
| 678 | */ |
| 679 | |
| 680 | void WCMD_setshow_time () { |
| 681 | |
| 682 | char curtime[64], buffer[64]; |
| 683 | DWORD count; |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 684 | SYSTEMTIME st; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 685 | |
| 686 | if (strlen(param1) == 0) { |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 687 | GetLocalTime(&st); |
| 688 | if (GetTimeFormat (LOCALE_USER_DEFAULT, 0, &st, NULL, |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 689 | curtime, sizeof(curtime))) { |
| 690 | WCMD_output ("Current Time is %s\nEnter new time: ", curtime); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 691 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 692 | if (count > 2) { |
| 693 | WCMD_output (nyi); |
| 694 | } |
| 695 | } |
| 696 | else WCMD_print_error (); |
| 697 | } |
| 698 | else { |
| 699 | WCMD_output (nyi); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /**************************************************************************** |
| 704 | * WCMD_shift |
| 705 | * |
| 706 | * Shift batch parameters. |
| 707 | */ |
| 708 | |
| 709 | void WCMD_shift () { |
| 710 | |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 711 | if (context != NULL) context -> shift_count++; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 712 | |
| 713 | } |
| 714 | |
| 715 | /**************************************************************************** |
| 716 | * WCMD_type |
| 717 | * |
| 718 | * Copy a file to standard output. |
| 719 | */ |
| 720 | |
| 721 | void WCMD_type () { |
| 722 | |
| 723 | HANDLE h; |
| 724 | char buffer[512]; |
| 725 | DWORD count; |
| 726 | |
| 727 | h = CreateFile (param1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, |
François Gouget | 12b3526 | 2001-01-02 20:40:58 +0000 | [diff] [blame] | 728 | FILE_ATTRIBUTE_NORMAL, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 729 | if (h == INVALID_HANDLE_VALUE) { |
| 730 | WCMD_print_error (); |
| 731 | return; |
| 732 | } |
| 733 | while (ReadFile (h, buffer, sizeof(buffer), &count, NULL)) { |
| 734 | if (count == 0) break; /* ReadFile reports success on EOF! */ |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 735 | WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), buffer, count, &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 736 | } |
| 737 | CloseHandle (h); |
| 738 | } |
| 739 | |
| 740 | /**************************************************************************** |
| 741 | * WCMD_verify |
| 742 | * |
| 743 | * Display verify flag. |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 744 | * FIXME: We don't actually do anything with the verify flag other than toggle |
| 745 | * it... |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 746 | */ |
| 747 | |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 748 | void WCMD_verify (char *command) { |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 749 | |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 750 | static char *von = "Verify is ON\n", *voff = "Verify is OFF\n"; |
| 751 | int count; |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 752 | |
Dave Pickles | 5f8f4f7 | 1999-06-26 10:24:08 +0000 | [diff] [blame] | 753 | count = strlen(command); |
| 754 | if (count == 0) { |
| 755 | if (verify_mode) WCMD_output (von); |
| 756 | else WCMD_output (voff); |
| 757 | return; |
| 758 | } |
| 759 | if (lstrcmpi(command, "ON") == 0) { |
| 760 | verify_mode = 1; |
| 761 | return; |
| 762 | } |
| 763 | else if (lstrcmpi(command, "OFF") == 0) { |
| 764 | verify_mode = 0; |
| 765 | return; |
| 766 | } |
| 767 | else WCMD_output ("Verify must be ON or OFF\n"); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /**************************************************************************** |
| 771 | * WCMD_version |
| 772 | * |
| 773 | * Display version info. |
| 774 | */ |
| 775 | |
| 776 | void WCMD_version () { |
| 777 | |
| 778 | WCMD_output (version_string); |
| 779 | |
| 780 | } |
| 781 | |
| 782 | /**************************************************************************** |
| 783 | * WCMD_volume |
| 784 | * |
| 785 | * Display volume info and/or set volume label. Returns 0 if error. |
| 786 | */ |
| 787 | |
| 788 | int WCMD_volume (int mode, char *path) { |
| 789 | |
| 790 | DWORD count, serial; |
| 791 | char string[MAX_PATH], label[MAX_PATH], curdir[MAX_PATH]; |
| 792 | BOOL status; |
| 793 | static char syntax[] = "Syntax Error\n\n"; |
| 794 | |
| 795 | if (lstrlen(path) == 0) { |
| 796 | status = GetCurrentDirectory (sizeof(curdir), curdir); |
| 797 | if (!status) { |
| 798 | WCMD_print_error (); |
| 799 | return 0; |
| 800 | } |
| 801 | status = GetVolumeInformation (NULL, label, sizeof(label), &serial, NULL, |
| 802 | NULL, NULL, 0); |
| 803 | } |
| 804 | else { |
| 805 | if ((path[1] != ':') || (lstrlen(path) != 2)) { |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 806 | WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), syntax, strlen(syntax), &count, NULL); |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 807 | return 0; |
| 808 | } |
| 809 | wsprintf (curdir, "%s\\", path); |
| 810 | status = GetVolumeInformation (curdir, label, sizeof(label), &serial, NULL, |
| 811 | NULL, NULL, 0); |
| 812 | } |
| 813 | if (!status) { |
| 814 | WCMD_print_error (); |
| 815 | return 0; |
| 816 | } |
| 817 | WCMD_output ("Volume in drive %c is %s\nVolume Serial Number is %04x-%04x\n\n", |
| 818 | curdir[0], label, HIWORD(serial), LOWORD(serial)); |
| 819 | if (mode) { |
| 820 | WCMD_output ("Volume label (11 characters, ENTER for none)?"); |
Dave Pickles | 036a9f7 | 1999-07-10 11:36:45 +0000 | [diff] [blame] | 821 | ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL); |
| 822 | if (count > 1) { |
| 823 | string[count-1] = '\0'; /* ReadFile output is not null-terminated! */ |
| 824 | if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */ |
| 825 | } |
Dave Pickles | 74f440e | 1999-06-06 15:24:04 +0000 | [diff] [blame] | 826 | if (lstrlen(path) != 0) { |
| 827 | if (!SetVolumeLabel (curdir, string)) WCMD_print_error (); |
| 828 | } |
| 829 | else { |
| 830 | if (!SetVolumeLabel (NULL, string)) WCMD_print_error (); |
| 831 | } |
| 832 | } |
| 833 | return 1; |
| 834 | } |