wcmd.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #include <wine/heap.h>
  33. /* msdn specified max for Win XP */
  34. #define MAXSTRING 8192
  35. /* Data structure to hold commands delimiters/separators */
  36. typedef enum _CMDdelimiters {
  37. CMD_NONE, /* End of line or single & */
  38. CMD_ONFAILURE, /* || */
  39. CMD_ONSUCCESS, /* && */
  40. CMD_PIPE /* Single | */
  41. } CMD_DELIMITERS;
  42. /* Data structure to hold commands to be processed */
  43. typedef struct _CMD_LIST {
  44. WCHAR *command; /* Command string to execute */
  45. WCHAR *redirects; /* Redirects in place */
  46. struct _CMD_LIST *nextcommand; /* Next command string to execute */
  47. CMD_DELIMITERS prevDelim; /* Previous delimiter */
  48. int bracketDepth;/* How deep bracketing have we got to */
  49. WCHAR pipeFile[MAX_PATH]; /* Where to get input from for pipes */
  50. } CMD_LIST;
  51. void WCMD_assoc (const WCHAR *, BOOL);
  52. void WCMD_batch (WCHAR *, WCHAR *, BOOL, WCHAR *, HANDLE);
  53. void WCMD_call (WCHAR *command);
  54. void WCMD_change_tty (void);
  55. void WCMD_choice (const WCHAR *);
  56. void WCMD_clear_screen (void);
  57. void WCMD_color (void);
  58. void WCMD_copy (WCHAR *);
  59. void WCMD_create_dir (WCHAR *);
  60. BOOL WCMD_delete (WCHAR *);
  61. void WCMD_directory (WCHAR *);
  62. void WCMD_echo (const WCHAR *);
  63. void WCMD_endlocal (void);
  64. void WCMD_enter_paged_mode(const WCHAR *);
  65. void WCMD_exit (CMD_LIST **cmdList);
  66. void WCMD_for (WCHAR *, CMD_LIST **cmdList);
  67. void WCMD_give_help (const WCHAR *args);
  68. void WCMD_goto (CMD_LIST **cmdList);
  69. void WCMD_if (WCHAR *, CMD_LIST **cmdList);
  70. void WCMD_leave_paged_mode(void);
  71. void WCMD_more (WCHAR *);
  72. void WCMD_move (void);
  73. WCHAR* WINAPIV WCMD_format_string (const WCHAR *format, ...);
  74. void WINAPIV WCMD_output (const WCHAR *format, ...);
  75. void WINAPIV WCMD_output_stderr (const WCHAR *format, ...);
  76. void WCMD_output_asis (const WCHAR *message);
  77. void WCMD_output_asis_stderr (const WCHAR *message);
  78. void WCMD_pause (void);
  79. void WCMD_popd (void);
  80. void WCMD_print_error (void);
  81. void WCMD_pushd (const WCHAR *args);
  82. void WCMD_remove_dir (WCHAR *command);
  83. void WCMD_rename (void);
  84. void WCMD_run_program (WCHAR *command, BOOL called);
  85. void WCMD_setlocal (const WCHAR *args);
  86. void WCMD_setshow_date (void);
  87. void WCMD_setshow_default (const WCHAR *args);
  88. void WCMD_setshow_env (WCHAR *command);
  89. void WCMD_setshow_path (const WCHAR *args);
  90. void WCMD_setshow_prompt (void);
  91. void WCMD_setshow_time (void);
  92. void WCMD_shift (const WCHAR *args);
  93. void WCMD_start (WCHAR *args);
  94. void WCMD_title (const WCHAR *);
  95. void WCMD_type (WCHAR *);
  96. void WCMD_verify (const WCHAR *args);
  97. void WCMD_version (void);
  98. int WCMD_volume (BOOL set_label, const WCHAR *args);
  99. void WCMD_mklink(WCHAR *args);
  100. WCHAR *WCMD_fgets (WCHAR *buf, DWORD n, HANDLE stream);
  101. WCHAR *WCMD_parameter (WCHAR *s, int n, WCHAR **start, BOOL raw, BOOL wholecmdline);
  102. WCHAR *WCMD_parameter_with_delims (WCHAR *s, int n, WCHAR **start, BOOL raw,
  103. BOOL wholecmdline, const WCHAR *delims);
  104. WCHAR *WCMD_skip_leading_spaces (WCHAR *string);
  105. BOOL WCMD_keyword_ws_found(const WCHAR *keyword, const WCHAR *ptr);
  106. void WCMD_HandleTildeModifiers(WCHAR **start, BOOL atExecute);
  107. WCHAR *WCMD_strip_quotes(WCHAR *cmd);
  108. WCHAR *WCMD_LoadMessage(UINT id);
  109. void WCMD_strsubstW(WCHAR *start, const WCHAR* next, const WCHAR* insert, int len);
  110. BOOL WCMD_ReadFile(const HANDLE hIn, WCHAR *intoBuf, const DWORD maxChars, LPDWORD charsRead);
  111. WCHAR *WCMD_ReadAndParseLine(const WCHAR *initialcmd, CMD_LIST **output, HANDLE readFrom);
  112. CMD_LIST *WCMD_process_commands(CMD_LIST *thisCmd, BOOL oneBracket, BOOL retrycall);
  113. void WCMD_free_commands(CMD_LIST *cmds);
  114. void WCMD_execute (const WCHAR *orig_command, const WCHAR *redirects,
  115. CMD_LIST **cmdList, BOOL retrycall);
  116. void *heap_xalloc(size_t);
  117. static inline WCHAR *heap_strdupW(const WCHAR *str)
  118. {
  119. WCHAR *ret = NULL;
  120. if(str) {
  121. size_t size;
  122. size = (lstrlenW(str)+1)*sizeof(WCHAR);
  123. ret = heap_xalloc(size);
  124. memcpy(ret, str, size);
  125. }
  126. return ret;
  127. }
  128. static inline BOOL ends_with_backslash( const WCHAR *path )
  129. {
  130. return path[0] && path[lstrlenW(path) - 1] == '\\';
  131. }
  132. int evaluate_if_condition(WCHAR *p, WCHAR **command, int *test, int *negate);
  133. /* Data structure to hold context when executing batch files */
  134. typedef struct _BATCH_CONTEXT {
  135. WCHAR *command; /* The command which invoked the batch file */
  136. HANDLE h; /* Handle to the open batch file */
  137. WCHAR *batchfileW; /* Name of same */
  138. int shift_count[10]; /* Offset in terms of shifts for %0 - %9 */
  139. struct _BATCH_CONTEXT *prev_context; /* Pointer to the previous context block */
  140. BOOL skip_rest; /* Skip the rest of the batch program and exit */
  141. CMD_LIST *toExecute; /* Commands left to be executed */
  142. } BATCH_CONTEXT;
  143. /* Data structure to handle building lists during recursive calls */
  144. struct env_stack
  145. {
  146. struct env_stack *next;
  147. union {
  148. int stackdepth; /* Only used for pushd and popd */
  149. WCHAR cwd; /* Only used for set/endlocal */
  150. } u;
  151. WCHAR *strings;
  152. HANDLE batchhandle; /* Used to ensure set/endlocals stay in scope */
  153. BOOL delayedsubst; /* Is delayed substitution in effect */
  154. };
  155. /* Data structure to save setlocal and pushd information */
  156. typedef struct _DIRECTORY_STACK
  157. {
  158. struct _DIRECTORY_STACK *next;
  159. WCHAR *dirName;
  160. WCHAR *fileName;
  161. } DIRECTORY_STACK;
  162. /* Data structure to for loop variables during for body execution, bearing
  163. in mind that for loops can be nested */
  164. #define MAX_FOR_VARIABLES 52
  165. #define FOR_VAR_IDX(c) (((c)>='a'&&(c)<='z')?((c)-'a'):\
  166. ((c)>='A'&&(c)<='Z')?(26+(c)-'A'):-1)
  167. typedef struct _FOR_CONTEXT {
  168. WCHAR *variable[MAX_FOR_VARIABLES]; /* a-z then A-Z */
  169. } FOR_CONTEXT;
  170. /*
  171. * Global variables quals, param1, param2 contain the current qualifiers
  172. * (uppercased and concatenated) and parameters entered, with environment
  173. * variables and batch parameters substitution already done.
  174. */
  175. extern WCHAR quals[MAXSTRING], param1[MAXSTRING], param2[MAXSTRING];
  176. extern DWORD errorlevel;
  177. extern BATCH_CONTEXT *context;
  178. extern FOR_CONTEXT forloopcontext;
  179. extern BOOL delayedsubst;
  180. #endif /* !RC_INVOKED */
  181. /*
  182. * Serial nos of builtin commands. These constants must be in step with
  183. * the list of strings defined in wcmd.rc, and WCMD_EXIT *must* always be
  184. * the last one.
  185. *
  186. * Yes it *would* be nice to use an enumeration here, but the Resource
  187. * Compiler won't accept resource IDs from enumerations :-(
  188. */
  189. #define WCMD_CALL 0
  190. #define WCMD_CD 1
  191. #define WCMD_CHDIR 2
  192. #define WCMD_CLS 3
  193. #define WCMD_COPY 4
  194. #define WCMD_CTTY 5
  195. #define WCMD_DATE 6
  196. #define WCMD_DEL 7
  197. #define WCMD_DIR 8
  198. #define WCMD_ECHO 9
  199. #define WCMD_ERASE 10
  200. #define WCMD_FOR 11
  201. #define WCMD_GOTO 12
  202. #define WCMD_HELP 13
  203. #define WCMD_IF 14
  204. #define WCMD_LABEL 15
  205. #define WCMD_MD 16
  206. #define WCMD_MKDIR 17
  207. #define WCMD_MOVE 18
  208. #define WCMD_PATH 19
  209. #define WCMD_PAUSE 20
  210. #define WCMD_PROMPT 21
  211. #define WCMD_REM 22
  212. #define WCMD_REN 23
  213. #define WCMD_RENAME 24
  214. #define WCMD_RD 25
  215. #define WCMD_RMDIR 26
  216. #define WCMD_SET 27
  217. #define WCMD_SHIFT 28
  218. #define WCMD_START 29
  219. #define WCMD_TIME 30
  220. #define WCMD_TITLE 31
  221. #define WCMD_TYPE 32
  222. #define WCMD_VERIFY 33
  223. #define WCMD_VER 34
  224. #define WCMD_VOL 35
  225. #define WCMD_ENDLOCAL 36
  226. #define WCMD_SETLOCAL 37
  227. #define WCMD_PUSHD 38
  228. #define WCMD_POPD 39
  229. #define WCMD_ASSOC 40
  230. #define WCMD_COLOR 41
  231. #define WCMD_FTYPE 42
  232. #define WCMD_MORE 43
  233. #define WCMD_CHOICE 44
  234. #define WCMD_MKLINK 45
  235. /* Must be last in list */
  236. #define WCMD_EXIT 46
  237. /* Some standard messages */
  238. extern WCHAR anykey[];
  239. extern WCHAR version_string[];
  240. /* Translated messages */
  241. #define WCMD_ALLHELP 1000
  242. #define WCMD_CONFIRM 1001
  243. #define WCMD_YES 1002
  244. #define WCMD_NO 1003
  245. #define WCMD_NOASSOC 1004
  246. #define WCMD_NOFTYPE 1005
  247. #define WCMD_OVERWRITE 1006
  248. #define WCMD_MORESTR 1007
  249. #define WCMD_TRUNCATEDLINE 1008
  250. #define WCMD_NYI 1009
  251. #define WCMD_NOARG 1010
  252. #define WCMD_SYNTAXERR 1011
  253. #define WCMD_FILENOTFOUND 1012
  254. #define WCMD_NOCMDHELP 1013
  255. #define WCMD_NOTARGET 1014
  256. #define WCMD_CURRENTDATE 1015
  257. #define WCMD_CURRENTTIME 1016
  258. #define WCMD_NEWDATE 1017
  259. #define WCMD_NEWTIME 1018
  260. #define WCMD_MISSINGENV 1019
  261. #define WCMD_READFAIL 1020
  262. #define WCMD_CALLINSCRIPT 1021
  263. #define WCMD_ALL 1022
  264. #define WCMD_DELPROMPT 1023
  265. #define WCMD_ECHOPROMPT 1024
  266. #define WCMD_VERIFYPROMPT 1025
  267. #define WCMD_VERIFYERR 1026
  268. #define WCMD_ARGERR 1027
  269. #define WCMD_VOLUMESERIALNO 1028
  270. #define WCMD_VOLUMEPROMPT 1029
  271. #define WCMD_NOPATH 1030
  272. #define WCMD_ANYKEY 1031
  273. #define WCMD_CONSTITLE 1032
  274. #define WCMD_VERSION 1033
  275. #define WCMD_MOREPROMPT 1034
  276. #define WCMD_LINETOOLONG 1035
  277. #define WCMD_VOLUMELABEL 1036
  278. #define WCMD_VOLUMENOLABEL 1037
  279. #define WCMD_YESNO 1038
  280. #define WCMD_YESNOALL 1039
  281. #define WCMD_NO_COMMAND_FOUND 1040
  282. #define WCMD_DIVIDEBYZERO 1041
  283. #define WCMD_NOOPERAND 1042
  284. #define WCMD_NOOPERATOR 1043
  285. #define WCMD_BADPAREN 1044
  286. #define WCMD_BADHEXOCT 1045