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