2 * CMD - Wine-compatible command line interface.
4 * Copyright (C) 1999 D A Pickles
5 * Copyright (C) 2007 J Edmeades
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #include <wine/heap.h>
35 /* msdn specified max for Win XP */
36 #define MAXSTRING 8192
38 /* Data structure to hold commands delimiters/separators */
40 typedef enum _CMDdelimiters
{
41 CMD_NONE
, /* End of line or single & */
42 CMD_ONFAILURE
, /* || */
43 CMD_ONSUCCESS
, /* && */
44 CMD_PIPE
/* Single | */
47 /* Data structure to hold commands to be processed */
49 typedef struct _CMD_LIST
{
50 WCHAR
*command
; /* Command string to execute */
51 WCHAR
*redirects
; /* Redirects in place */
52 struct _CMD_LIST
*nextcommand
; /* Next command string to execute */
53 CMD_DELIMITERS prevDelim
; /* Previous delimiter */
54 int bracketDepth
;/* How deep bracketing have we got to */
55 WCHAR pipeFile
[MAX_PATH
]; /* Where to get input from for pipes */
58 void WCMD_assoc (const WCHAR
*, BOOL
);
59 void WCMD_batch (WCHAR
*, WCHAR
*, BOOL
, WCHAR
*, HANDLE
);
60 void WCMD_call (WCHAR
*command
);
61 void WCMD_change_tty (void);
62 void WCMD_choice (const WCHAR
*);
63 void WCMD_clear_screen (void);
64 void WCMD_color (void);
65 void WCMD_copy (WCHAR
*);
66 void WCMD_create_dir (WCHAR
*);
67 BOOL
WCMD_delete (WCHAR
*);
68 void WCMD_directory (WCHAR
*);
69 void WCMD_echo (const WCHAR
*);
70 void WCMD_endlocal (void);
71 void WCMD_enter_paged_mode(const WCHAR
*);
72 void WCMD_exit (CMD_LIST
**cmdList
);
73 void WCMD_for (WCHAR
*, CMD_LIST
**cmdList
);
74 void WCMD_give_help (const WCHAR
*args
);
75 void WCMD_goto (CMD_LIST
**cmdList
);
76 void WCMD_if (WCHAR
*, CMD_LIST
**cmdList
);
77 void WCMD_leave_paged_mode(void);
78 void WCMD_more (WCHAR
*);
79 void WCMD_move (void);
80 WCHAR
* WINAPIV
WCMD_format_string (const WCHAR
*format
, ...);
81 void WINAPIV
WCMD_output (const WCHAR
*format
, ...);
82 void WINAPIV
WCMD_output_stderr (const WCHAR
*format
, ...);
83 void WCMD_output_asis (const WCHAR
*message
);
84 void WCMD_output_asis_stderr (const WCHAR
*message
);
85 void WCMD_pause (void);
86 void WCMD_popd (void);
87 void WCMD_print_error (void);
88 void WCMD_pushd (const WCHAR
*args
);
89 void WCMD_remove_dir (WCHAR
*command
);
90 void WCMD_rename (void);
91 void WCMD_run_program (WCHAR
*command
, BOOL called
);
92 void WCMD_setlocal (const WCHAR
*args
);
93 void WCMD_setshow_date (void);
94 void WCMD_setshow_default (const WCHAR
*args
);
95 void WCMD_setshow_env (WCHAR
*command
);
96 void WCMD_setshow_path (const WCHAR
*args
);
97 void WCMD_setshow_prompt (void);
98 void WCMD_setshow_time (void);
99 void WCMD_shift (const WCHAR
*args
);
100 void WCMD_start (WCHAR
*args
);
101 void WCMD_title (const WCHAR
*);
102 void WCMD_type (WCHAR
*);
103 void WCMD_verify (const WCHAR
*args
);
104 void WCMD_version (void);
105 int WCMD_volume (BOOL set_label
, const WCHAR
*args
);
106 void WCMD_mklink(WCHAR
*args
);
108 static inline BOOL
WCMD_is_console_handle(HANDLE h
)
110 return (((DWORD_PTR
)h
) & 3) == 3;
112 WCHAR
*WCMD_fgets (WCHAR
*buf
, DWORD n
, HANDLE stream
);
113 WCHAR
*WCMD_parameter (WCHAR
*s
, int n
, WCHAR
**start
, BOOL raw
, BOOL wholecmdline
);
114 WCHAR
*WCMD_parameter_with_delims (WCHAR
*s
, int n
, WCHAR
**start
, BOOL raw
,
115 BOOL wholecmdline
, const WCHAR
*delims
);
116 WCHAR
*WCMD_skip_leading_spaces (WCHAR
*string
);
117 BOOL
WCMD_keyword_ws_found(const WCHAR
*keyword
, int len
, const WCHAR
*ptr
);
118 void WCMD_HandleTildeModifiers(WCHAR
**start
, BOOL atExecute
);
120 WCHAR
*WCMD_strip_quotes(WCHAR
*cmd
);
121 WCHAR
*WCMD_LoadMessage(UINT id
);
122 void WCMD_strsubstW(WCHAR
*start
, const WCHAR
* next
, const WCHAR
* insert
, int len
);
123 BOOL
WCMD_ReadFile(const HANDLE hIn
, WCHAR
*intoBuf
, const DWORD maxChars
, LPDWORD charsRead
);
125 WCHAR
*WCMD_ReadAndParseLine(const WCHAR
*initialcmd
, CMD_LIST
**output
, HANDLE readFrom
);
126 CMD_LIST
*WCMD_process_commands(CMD_LIST
*thisCmd
, BOOL oneBracket
, BOOL retrycall
);
127 void WCMD_free_commands(CMD_LIST
*cmds
);
128 void WCMD_execute (const WCHAR
*orig_command
, const WCHAR
*redirects
,
129 CMD_LIST
**cmdList
, BOOL retrycall
);
131 void *heap_xalloc(size_t);
133 static inline WCHAR
*heap_strdupW(const WCHAR
*str
)
140 size
= (lstrlenW(str
)+1)*sizeof(WCHAR
);
141 ret
= heap_xalloc(size
);
142 memcpy(ret
, str
, size
);
148 static inline BOOL
ends_with_backslash( const WCHAR
*path
)
150 return path
[0] && path
[lstrlenW(path
) - 1] == '\\';
153 int evaluate_if_condition(WCHAR
*p
, WCHAR
**command
, int *test
, int *negate
);
155 /* Data structure to hold context when executing batch files */
157 typedef struct _BATCH_CONTEXT
{
158 WCHAR
*command
; /* The command which invoked the batch file */
159 HANDLE h
; /* Handle to the open batch file */
160 WCHAR
*batchfileW
; /* Name of same */
161 int shift_count
[10]; /* Offset in terms of shifts for %0 - %9 */
162 struct _BATCH_CONTEXT
*prev_context
; /* Pointer to the previous context block */
163 BOOL skip_rest
; /* Skip the rest of the batch program and exit */
164 CMD_LIST
*toExecute
; /* Commands left to be executed */
167 /* Data structure to handle building lists during recursive calls */
171 struct env_stack
*next
;
173 int stackdepth
; /* Only used for pushd and popd */
174 WCHAR cwd
; /* Only used for set/endlocal */
177 HANDLE batchhandle
; /* Used to ensure set/endlocals stay in scope */
178 BOOL delayedsubst
; /* Is delayed substitution in effect */
181 /* Data structure to save setlocal and pushd information */
183 typedef struct _DIRECTORY_STACK
185 struct _DIRECTORY_STACK
*next
;
190 /* Data structure to for loop variables during for body execution, bearing
191 in mind that for loops can be nested */
192 #define MAX_FOR_VARIABLES 52
193 #define FOR_VAR_IDX(c) (((c)>='a'&&(c)<='z')?((c)-'a'):\
194 ((c)>='A'&&(c)<='Z')?(26+(c)-'A'):-1)
196 typedef struct _FOR_CONTEXT
{
197 WCHAR
*variable
[MAX_FOR_VARIABLES
]; /* a-z then A-Z */
201 * Global variables quals, param1, param2 contain the current qualifiers
202 * (uppercased and concatenated) and parameters entered, with environment
203 * variables and batch parameters substitution already done.
205 extern WCHAR quals
[MAXSTRING
], param1
[MAXSTRING
], param2
[MAXSTRING
];
206 extern DWORD errorlevel
;
207 extern BATCH_CONTEXT
*context
;
208 extern FOR_CONTEXT forloopcontext
;
209 extern BOOL delayedsubst
;
211 #endif /* !RC_INVOKED */
214 * Serial nos of builtin commands. These constants must be in step with
215 * the list of strings defined in wcmd.rc, and WCMD_EXIT *must* always be
218 * Yes it *would* be nice to use an enumeration here, but the Resource
219 * Compiler won't accept resource IDs from enumerations :-(
232 #define WCMD_ERASE 10
237 #define WCMD_LABEL 15
239 #define WCMD_MKDIR 17
242 #define WCMD_PAUSE 20
243 #define WCMD_PROMPT 21
246 #define WCMD_RENAME 24
248 #define WCMD_RMDIR 26
250 #define WCMD_SHIFT 28
251 #define WCMD_START 29
253 #define WCMD_TITLE 31
255 #define WCMD_VERIFY 33
259 #define WCMD_ENDLOCAL 36
260 #define WCMD_SETLOCAL 37
261 #define WCMD_PUSHD 38
263 #define WCMD_ASSOC 40
264 #define WCMD_COLOR 41
265 #define WCMD_FTYPE 42
267 #define WCMD_CHOICE 44
268 #define WCMD_MKLINK 45
270 /* Must be last in list */
273 /* Some standard messages */
274 extern const WCHAR newlineW
[];
275 extern const WCHAR spaceW
[];
276 extern const WCHAR nullW
[];
277 extern const WCHAR dotW
[];
278 extern const WCHAR dotdotW
[];
279 extern const WCHAR starW
[];
280 extern const WCHAR slashW
[];
281 extern const WCHAR equalW
[];
282 extern WCHAR anykey
[];
283 extern WCHAR version_string
[];
285 /* Translated messages */
286 #define WCMD_ALLHELP 1000
287 #define WCMD_CONFIRM 1001
288 #define WCMD_YES 1002
290 #define WCMD_NOASSOC 1004
291 #define WCMD_NOFTYPE 1005
292 #define WCMD_OVERWRITE 1006
293 #define WCMD_MORESTR 1007
294 #define WCMD_TRUNCATEDLINE 1008
295 #define WCMD_NYI 1009
296 #define WCMD_NOARG 1010
297 #define WCMD_SYNTAXERR 1011
298 #define WCMD_FILENOTFOUND 1012
299 #define WCMD_NOCMDHELP 1013
300 #define WCMD_NOTARGET 1014
301 #define WCMD_CURRENTDATE 1015
302 #define WCMD_CURRENTTIME 1016
303 #define WCMD_NEWDATE 1017
304 #define WCMD_NEWTIME 1018
305 #define WCMD_MISSINGENV 1019
306 #define WCMD_READFAIL 1020
307 #define WCMD_CALLINSCRIPT 1021
308 #define WCMD_ALL 1022
309 #define WCMD_DELPROMPT 1023
310 #define WCMD_ECHOPROMPT 1024
311 #define WCMD_VERIFYPROMPT 1025
312 #define WCMD_VERIFYERR 1026
313 #define WCMD_ARGERR 1027
314 #define WCMD_VOLUMESERIALNO 1028
315 #define WCMD_VOLUMEPROMPT 1029
316 #define WCMD_NOPATH 1030
317 #define WCMD_ANYKEY 1031
318 #define WCMD_CONSTITLE 1032
319 #define WCMD_VERSION 1033
320 #define WCMD_MOREPROMPT 1034
321 #define WCMD_LINETOOLONG 1035
322 #define WCMD_VOLUMELABEL 1036
323 #define WCMD_VOLUMENOLABEL 1037
324 #define WCMD_YESNO 1038
325 #define WCMD_YESNOALL 1039
326 #define WCMD_NO_COMMAND_FOUND 1040
327 #define WCMD_DIVIDEBYZERO 1041
328 #define WCMD_NOOPERAND 1042
329 #define WCMD_NOOPERATOR 1043
330 #define WCMD_BADPAREN 1044
331 #define WCMD_BADHEXOCT 1045