Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/cmd/wcmd.h
8559 views
1
/*
2
* CMD - Wine-compatible command line interface.
3
*
4
* Copyright (C) 1999 D A Pickles
5
* Copyright (C) 2007 J Edmeades
6
*
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.
11
*
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.
16
*
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
20
*/
21
22
#define IDI_ICON1 1
23
#include <windows.h>
24
#include <windef.h>
25
#include <winternl.h>
26
#ifndef RC_INVOKED
27
#include <string.h>
28
#include <stdlib.h>
29
#include <stdarg.h>
30
#include <stdio.h>
31
#include <ctype.h>
32
#include <wchar.h>
33
34
/* msdn specified max for Win XP */
35
#define MAXSTRING 8192
36
37
/* Data structure to express a redirection */
38
typedef struct _CMD_REDIRECTION
39
{
40
enum CMD_REDIRECTION_KIND {REDIR_READ_FROM, REDIR_WRITE_TO, REDIR_WRITE_APPEND, REDIR_WRITE_CLONE} kind;
41
unsigned short fd;
42
struct _CMD_REDIRECTION *next;
43
union
44
{
45
unsigned short clone; /* only if kind is REDIR_WRITE_CLONE */
46
WCHAR file[1]; /* only if kind is READ_FROM, WRITE or WRITE_APPEND */
47
};
48
} CMD_REDIRECTION;
49
50
/* Data structure to hold commands delimiters/separators */
51
52
typedef enum _CMD_OPERATOR
53
{
54
CMD_SINGLE, /* single command */
55
CMD_CONCAT, /* & */
56
CMD_ONFAILURE, /* || */
57
CMD_ONSUCCESS, /* && */
58
CMD_PIPE, /* Single | */
59
CMD_BLOCK, /* ( block ) */
60
CMD_IF, /* IF command */
61
CMD_FOR, /* FOR command */
62
} CMD_OPERATOR;
63
64
/* Data structure to hold commands to be processed */
65
66
enum cond_operator {CMD_IF_ERRORLEVEL, CMD_IF_EXIST, CMD_IF_DEFINED,
67
CMD_IF_BINOP_EQUAL /* == */, CMD_IF_BINOP_LSS, CMD_IF_BINOP_LEQ, CMD_IF_BINOP_EQU,
68
CMD_IF_BINOP_NEQ, CMD_IF_BINOP_GEQ, CMD_IF_BINOP_GTR};
69
typedef struct _CMD_IF_CONDITION
70
{
71
unsigned case_insensitive : 1,
72
negated : 1,
73
op;
74
union
75
{
76
/* CMD_IF_ERRORLEVEL, CMD_IF_EXIST, CMD_IF_DEFINED */
77
const WCHAR *operand;
78
/* CMD_BINOP_EQUAL, CMD_BINOP_LSS, CMD_BINOP_LEQ, CMD_BINOP_EQU, CMD_BINOP_NEQ, CMD_BINOP_GEQ, CMD_BINOP_GTR */
79
struct
80
{
81
const WCHAR *left;
82
const WCHAR *right;
83
};
84
};
85
} CMD_IF_CONDITION;
86
87
#define CMD_FOR_FLAG_TREE_RECURSE (1u << 0)
88
#define CMD_FOR_FLAG_TREE_INCLUDE_FILES (1u << 1)
89
#define CMD_FOR_FLAG_TREE_INCLUDE_DIRECTORIES (1u << 2)
90
91
typedef struct _CMD_FOR_CONTROL
92
{
93
enum for_control_operator {CMD_FOR_FILETREE, CMD_FOR_FILE_SET /* /F */,
94
CMD_FOR_NUMBERS /* /L */} operator;
95
unsigned flags; /* |-ed CMD_FOR_FLAG_* */
96
unsigned variable_index;
97
const WCHAR *set;
98
union
99
{
100
const WCHAR *root_dir; /* for CMD_FOR_FILETREE */
101
struct /* for CMD_FOR_FILE_SET */
102
{
103
WCHAR eol;
104
BOOL use_backq;
105
int num_lines_to_skip;
106
const WCHAR *delims;
107
const WCHAR *tokens;
108
};
109
};
110
} CMD_FOR_CONTROL;
111
112
typedef struct _CMD_NODE
113
{
114
CMD_OPERATOR op; /* operator */
115
BOOL do_echo;
116
CMD_REDIRECTION *redirects; /* Redirections */
117
union
118
{
119
WCHAR *command; /* CMD_SINGLE */
120
struct /* binary operator (CMD_CONCAT, ONFAILURE, ONSUCCESS, PIPE) */
121
{
122
struct _CMD_NODE *left;
123
struct _CMD_NODE *right;
124
};
125
struct /* CMD_IF */
126
{
127
CMD_IF_CONDITION condition;
128
struct _CMD_NODE *then_block;
129
struct _CMD_NODE *else_block;
130
};
131
struct /* CMD_FOR */
132
{
133
CMD_FOR_CONTROL for_ctrl;
134
struct _CMD_NODE *do_block;
135
};
136
struct /* CMD_BLOCK */
137
{
138
struct _CMD_NODE *block;
139
};
140
};
141
} CMD_NODE;
142
143
struct _DIRECTORY_STACK;
144
void WCMD_add_dirstowalk(struct _DIRECTORY_STACK *dirsToWalk);
145
struct _DIRECTORY_STACK *WCMD_dir_stack_create(const WCHAR *dir, const WCHAR *file);
146
struct _DIRECTORY_STACK *WCMD_dir_stack_free(struct _DIRECTORY_STACK *dir);
147
148
/* The return code:
149
* - some of them are directly mapped to kernel32's errors
150
* - some others are cmd.exe specific
151
* - ABORTED if used to break out of FOR/IF blocks (to handle GOTO, EXIT commands)
152
*/
153
typedef int RETURN_CODE;
154
#define RETURN_CODE_SYNTAX_ERROR 255
155
#define RETURN_CODE_CANT_LAUNCH 9009
156
#define RETURN_CODE_ABORTED (-999999) /* generated for exit /b so that all loops (and al.) are exited*/
157
#define RETURN_CODE_GOTO (-999998) /* generated when changing file position (and break from if/for instructions) */
158
#define RETURN_CODE_EXITED (-999997) /* generated when batch file terminates because child has terminated */
159
/* to test if one shall break from instruction within a batch file */
160
static inline BOOL WCMD_is_break(RETURN_CODE return_code)
161
{
162
return return_code == RETURN_CODE_ABORTED || return_code == RETURN_CODE_GOTO;
163
}
164
165
BOOL WCMD_print_volume_information(const WCHAR *);
166
167
RETURN_CODE WCMD_assoc(const WCHAR *, BOOL);
168
RETURN_CODE WCMD_call(WCHAR *command);
169
RETURN_CODE WCMD_choice(WCHAR *);
170
RETURN_CODE WCMD_clear_screen(void);
171
RETURN_CODE WCMD_color(void);
172
RETURN_CODE WCMD_copy(WCHAR *);
173
RETURN_CODE WCMD_create_dir(WCHAR *);
174
RETURN_CODE WCMD_delete(WCHAR *);
175
RETURN_CODE WCMD_directory(WCHAR *);
176
RETURN_CODE WCMD_echo(const WCHAR *);
177
RETURN_CODE WCMD_endlocal(void);
178
void WCMD_enter_paged_mode(const WCHAR *);
179
RETURN_CODE WCMD_exit(void);
180
BOOL WCMD_get_fullpath(const WCHAR *, SIZE_T, WCHAR *, WCHAR **);
181
RETURN_CODE WCMD_give_help(WCHAR *args);
182
RETURN_CODE WCMD_goto(void);
183
RETURN_CODE WCMD_label(void);
184
void WCMD_leave_paged_mode(void);
185
RETURN_CODE WCMD_more(WCHAR *);
186
RETURN_CODE WCMD_move (void);
187
WCHAR* WINAPIV WCMD_format_string(const WCHAR *format, ...);
188
void WINAPIV WCMD_output(const WCHAR *format, ...);
189
void WINAPIV WCMD_output_stderr(const WCHAR *format, ...);
190
RETURN_CODE WCMD_output_asis(const WCHAR *message);
191
RETURN_CODE WCMD_output_flush(void);
192
RETURN_CODE WCMD_output_asis_stderr(const WCHAR *message);
193
RETURN_CODE WCMD_pause(void);
194
RETURN_CODE WCMD_popd(void);
195
void WCMD_print_error (void);
196
RETURN_CODE WCMD_pushd(const WCHAR *args);
197
RETURN_CODE WCMD_remove_dir(WCHAR *command);
198
RETURN_CODE WCMD_rename(void);
199
RETURN_CODE WCMD_setlocal(WCHAR *args);
200
RETURN_CODE WCMD_setshow_date(void);
201
RETURN_CODE WCMD_setshow_default(const WCHAR *args);
202
RETURN_CODE WCMD_setshow_env(WCHAR *command);
203
RETURN_CODE WCMD_setshow_path(const WCHAR *args);
204
RETURN_CODE WCMD_setshow_prompt(void);
205
RETURN_CODE WCMD_setshow_time(void);
206
RETURN_CODE WCMD_shift(const WCHAR *args);
207
RETURN_CODE WCMD_start(WCHAR *args);
208
RETURN_CODE WCMD_title(const WCHAR *);
209
RETURN_CODE WCMD_type(WCHAR *);
210
RETURN_CODE WCMD_verify(void);
211
RETURN_CODE WCMD_version(void);
212
RETURN_CODE WCMD_volume(void);
213
RETURN_CODE WCMD_mklink(WCHAR *args);
214
RETURN_CODE WCMD_change_drive(WCHAR drive);
215
216
WCHAR *WCMD_fgets (WCHAR *buf, DWORD n, HANDLE stream);
217
WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **start, BOOL raw, BOOL wholecmdline);
218
WCHAR *WCMD_parameter_with_delims (WCHAR *s, int n, WCHAR **start, BOOL raw,
219
BOOL wholecmdline, const WCHAR *delims);
220
WCHAR *WCMD_skip_leading_spaces (WCHAR *string);
221
BOOL WCMD_keyword_ws_found(const WCHAR *keyword, const WCHAR *ptr);
222
void WCMD_HandleTildeModifiers(WCHAR **start, BOOL atExecute);
223
224
WCHAR *WCMD_strip_quotes(WCHAR *cmd);
225
WCHAR *WCMD_LoadMessage(UINT id);
226
WCHAR *WCMD_strsubstW(WCHAR *start, const WCHAR* next, const WCHAR* insert, int len);
227
RETURN_CODE WCMD_wait_for_input(HANDLE hIn);
228
RETURN_CODE WCMD_wait_for_console_input(void);
229
BOOL WCMD_read_console(const HANDLE hInput, WCHAR *inputBuffer, const DWORD inputBufferLength, LPDWORD numRead);
230
231
enum read_parse_line {RPL_SUCCESS, RPL_EOF, RPL_SYNTAXERROR};
232
enum read_parse_line WCMD_ReadAndParseLine(CMD_NODE **output);
233
void node_dispose_tree(CMD_NODE *cmds);
234
RETURN_CODE node_execute(CMD_NODE *node);
235
236
RETURN_CODE WCMD_call_batch(const WCHAR *, WCHAR *);
237
RETURN_CODE WCMD_call_command(WCHAR *command);
238
RETURN_CODE WCMD_run_builtin_command(int cmd_index, WCHAR *cmd);
239
240
BOOL WCMD_find_label(HANDLE h, const WCHAR*, LARGE_INTEGER *pos);
241
void WCMD_set_label_end(WCHAR *string);
242
243
RETURN_CODE WCMD_ctrlc_status(void);
244
245
void *xrealloc(void *, size_t) __WINE_ALLOC_SIZE(2) __WINE_DEALLOC(free);
246
247
static inline void *xalloc(size_t sz) __WINE_MALLOC;
248
static inline void *xalloc(size_t sz)
249
{
250
return xrealloc(NULL, sz);
251
}
252
253
static inline WCHAR *xstrdupW(const WCHAR *str)
254
{
255
WCHAR *ret = NULL;
256
257
if(str) {
258
size_t size;
259
260
size = (lstrlenW(str)+1)*sizeof(WCHAR);
261
ret = xalloc(size);
262
memcpy(ret, str, size);
263
}
264
265
return ret;
266
}
267
268
static inline BOOL ends_with_backslash( const WCHAR *path )
269
{
270
return path[0] && path[lstrlenW(path) - 1] == '\\';
271
}
272
273
int evaluate_if_condition(WCHAR *p, WCHAR **command, int *test, int *negate);
274
275
/* Data structure to store information about a batch file */
276
struct batch_file
277
{
278
unsigned ref_count; /* number of BATCH_CONTEXT attached to this */
279
WCHAR *path_name; /* Name of self */
280
FILETIME last_modified;
281
struct
282
{
283
LARGE_INTEGER from;
284
LARGE_INTEGER position;
285
unsigned age;
286
const WCHAR *label;
287
} cache[8];
288
};
289
290
struct batch_context
291
{
292
WCHAR *command; /* The command which invoked the batch file */
293
LARGE_INTEGER file_position;
294
int shift_count[10]; /* Offset in terms of shifts for %0 - %9 */
295
struct batch_context *prev_context; /* Pointer to the previous context block */
296
struct batch_file *batch_file; /* Reference to the file itself */
297
};
298
299
#define WCMD_FILE_POSITION_EOF (~(DWORD64)0)
300
301
/* Data structure to handle building lists during recursive calls */
302
303
struct env_stack
304
{
305
struct batch_context *context;
306
struct env_stack *next;
307
union
308
{
309
int stackdepth; /* Only used for pushd and popd */
310
WCHAR cwd; /* Only used for set/endlocal */
311
} u;
312
WCHAR *strings;
313
BOOL delayedsubst; /* Is delayed substitution in effect */
314
};
315
316
/* Data structure to save setlocal and pushd information */
317
318
typedef struct _DIRECTORY_STACK
319
{
320
struct _DIRECTORY_STACK *next;
321
WCHAR *dirName;
322
WCHAR *fileName;
323
} DIRECTORY_STACK;
324
325
static inline const char *debugstr_for_var(WCHAR ch)
326
{
327
static char tmp[16];
328
if (iswprint(ch))
329
sprintf(tmp, "%%%lc", ch);
330
else
331
sprintf(tmp, "%%[%x]", ch);
332
return tmp;
333
}
334
335
typedef struct _FOR_CONTEXT
336
{
337
struct _FOR_CONTEXT *previous;
338
WCHAR *variable[128];
339
} FOR_CONTEXT;
340
341
extern FOR_CONTEXT *forloopcontext;
342
static inline BOOL for_var_is_valid(WCHAR ch) {return ch && ch < ARRAY_SIZE(forloopcontext->variable);}
343
344
void WCMD_save_for_loop_context(BOOL reset);
345
void WCMD_restore_for_loop_context(void);
346
void WCMD_set_for_loop_variable(unsigned varidx, const WCHAR *value);
347
348
/*
349
* Global variables quals, param1, param2 contain the current qualifiers
350
* (uppercased and concatenated) and parameters entered, with environment
351
* variables and batch parameters substitution already done.
352
*/
353
extern WCHAR quals[MAXSTRING], param1[MAXSTRING], param2[MAXSTRING];
354
extern int errorlevel;
355
extern struct batch_context *context;
356
extern BOOL delayedsubst;
357
358
static inline BOOL WCMD_is_in_context(const WCHAR *ext)
359
{
360
size_t c_len, e_len;
361
if (!context || !context->batch_file) return FALSE;
362
if (!ext) return TRUE;
363
c_len = wcslen(context->batch_file->path_name);
364
e_len = wcslen(ext);
365
return (c_len > e_len) && !wcsicmp(&context->batch_file->path_name[c_len - e_len], ext);
366
}
367
368
#endif /* !RC_INVOKED */
369
370
/*
371
* Serial nos of builtin commands. These constants must be in step with
372
* the list of strings defined in wcmd.rc, and WCMD_EXIT *must* always be
373
* the last one.
374
*
375
* Yes it *would* be nice to use an enumeration here, but the Resource
376
* Compiler won't accept resource IDs from enumerations :-(
377
*/
378
379
#define WCMD_CALL 0
380
#define WCMD_CD 1
381
#define WCMD_CHDIR 2
382
#define WCMD_CLS 3
383
#define WCMD_COPY 4
384
/* no longer used slot */
385
#define WCMD_DATE 6
386
#define WCMD_DEL 7
387
#define WCMD_DIR 8
388
#define WCMD_ECHO 9
389
#define WCMD_ERASE 10
390
#define WCMD_FOR 11
391
#define WCMD_GOTO 12
392
#define WCMD_HELP 13
393
#define WCMD_IF 14
394
#define WCMD_LABEL 15
395
#define WCMD_MD 16
396
#define WCMD_MKDIR 17
397
#define WCMD_MOVE 18
398
#define WCMD_PATH 19
399
#define WCMD_PAUSE 20
400
#define WCMD_PROMPT 21
401
#define WCMD_REM 22
402
#define WCMD_REN 23
403
#define WCMD_RENAME 24
404
#define WCMD_RD 25
405
#define WCMD_RMDIR 26
406
#define WCMD_SET 27
407
#define WCMD_SHIFT 28
408
#define WCMD_START 29
409
#define WCMD_TIME 30
410
#define WCMD_TITLE 31
411
#define WCMD_TYPE 32
412
#define WCMD_VERIFY 33
413
#define WCMD_VER 34
414
#define WCMD_VOL 35
415
416
#define WCMD_ENDLOCAL 36
417
#define WCMD_SETLOCAL 37
418
#define WCMD_PUSHD 38
419
#define WCMD_POPD 39
420
#define WCMD_ASSOC 40
421
#define WCMD_COLOR 41
422
#define WCMD_FTYPE 42
423
#define WCMD_MORE 43
424
#define WCMD_CHOICE 44
425
#define WCMD_MKLINK 45
426
#define WCMD_CHGDRIVE 46
427
428
/* Must be last in list */
429
#define WCMD_EXIT 47
430
431
/* Some standard messages */
432
extern WCHAR anykey[];
433
extern WCHAR version_string[];
434
435
/* Translated messages */
436
#define WCMD_ALLHELP 1000
437
#define WCMD_CONFIRM 1001
438
#define WCMD_YES 1002
439
#define WCMD_NO 1003
440
#define WCMD_NOASSOC 1004
441
#define WCMD_NOFTYPE 1005
442
#define WCMD_OVERWRITE 1006
443
#define WCMD_MORESTR 1007
444
#define WCMD_TRUNCATEDLINE 1008
445
#define WCMD_NYI 1009
446
#define WCMD_NOARG 1010
447
#define WCMD_SYNTAXERR 1011
448
#define WCMD_FILENOTFOUND 1012
449
#define WCMD_NOCMDHELP 1013
450
#define WCMD_NOTARGET 1014
451
#define WCMD_CURRENTDATE 1015
452
#define WCMD_CURRENTTIME 1016
453
#define WCMD_NEWDATE 1017
454
#define WCMD_NEWTIME 1018
455
#define WCMD_MISSINGENV 1019
456
#define WCMD_READFAIL 1020
457
#define WCMD_CALLINSCRIPT 1021
458
#define WCMD_ALL 1022
459
#define WCMD_DELPROMPT 1023
460
#define WCMD_ECHOPROMPT 1024
461
#define WCMD_VERIFYPROMPT 1025
462
#define WCMD_VERIFYERR 1026
463
#define WCMD_ARGERR 1027
464
#define WCMD_VOLUMESERIALNO 1028
465
#define WCMD_VOLUMEPROMPT 1029
466
#define WCMD_ANYKEY 1031
467
#define WCMD_CONSTITLE 1032
468
#define WCMD_VERSION 1033
469
#define WCMD_MOREPROMPT 1034
470
#define WCMD_LINETOOLONG 1035
471
#define WCMD_VOLUMELABEL 1036
472
#define WCMD_VOLUMENOLABEL 1037
473
#define WCMD_YESNO 1038
474
#define WCMD_YESNOALL 1039
475
#define WCMD_NO_COMMAND_FOUND 1040
476
#define WCMD_DIVIDEBYZERO 1041
477
#define WCMD_NOOPERAND 1042
478
#define WCMD_NOOPERATOR 1043
479
#define WCMD_BADPAREN 1044
480
#define WCMD_BADHEXOCT 1045
481
#define WCMD_FILENAMETOOLONG 1046
482
#define WCMD_BADTOKEN 1047
483
#define WCMD_ENDOFLINE 1048
484
#define WCMD_ENDOFFILE 1049
485
#define WCMD_NUMCOPIED 1050
486
#define WCMD_NOCOPYTOSELF 1051
487
488