wcmd.h 14 KB

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