Mercurial > vim
annotate src/vimrun.c @ 22499:8db2a5f8c40a
Added tag v8.2.1797 for changeset 04a9b1399d8eb0ac1f3d87e9fd2eef292cfa6d2e
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 04 Oct 2020 14:30:05 +0200 |
parents | 2559dc02bd64 |
children |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9840
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * this file by Vince Negri | |
5 * | |
6 * Do ":help uganda" in Vim to read copying and usage conditions. | |
7 * Do ":help credits" in Vim to see a list of people who contributed. | |
8 * See README.txt for an overview of the Vim source code. | |
9 */ | |
10 | |
11 /* | |
12 * vimrun.c - Tiny Win32 program to safely run an external command in a | |
13 * DOS console. | |
14 * This program is required to avoid that typing CTRL-C in the DOS | |
15 * console kills Vim. Now it only kills vimrun. | |
16 */ | |
17 | |
18 #include <stdio.h> | |
19 #include <stdlib.h> | |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
20 #include <conio.h> |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
21 #ifndef WIN32_LEAN_AND_MEAN |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
22 # define WIN32_LEAN_AND_MEAN |
7 | 23 #endif |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
24 #include <windows.h> |
7 | 25 |
26 int | |
27 main(void) | |
28 { | |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
29 const wchar_t *p; |
18241
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
30 wchar_t *cmd; |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
31 size_t cmdlen; |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
32 int retval; |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
33 int inquote = 0; |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
34 int silent = 0; |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
35 HANDLE hstdout; |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
36 DWORD written; |
7 | 37 |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
38 p = (const wchar_t *)GetCommandLineW(); |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
39 |
7 | 40 /* |
41 * Skip the executable name, which might be in "". | |
42 */ | |
43 while (*p) | |
44 { | |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
45 if (*p == L'"') |
7 | 46 inquote = !inquote; |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
47 else if (!inquote && *p == L' ') |
7 | 48 { |
49 ++p; | |
50 break; | |
51 } | |
52 ++p; | |
53 } | |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
54 while (*p == L' ') |
13353
8412df1479a3
patch 8.0.1550: various small problems in source files
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
55 ++p; |
7 | 56 |
57 /* | |
58 * "-s" argument: don't wait for a key hit. | |
59 */ | |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
60 if (p[0] == L'-' && p[1] == L's' && p[2] == L' ') |
7 | 61 { |
62 silent = 1; | |
63 p += 3; | |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
64 while (*p == L' ') |
7 | 65 ++p; |
66 } | |
67 | |
18241
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
68 // Print the command, including quotes and redirection. |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
69 hstdout = GetStdHandle(STD_OUTPUT_HANDLE); |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
70 WriteConsoleW(hstdout, p, wcslen(p), &written, NULL); |
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
71 WriteConsoleW(hstdout, L"\r\n", 2, &written, NULL); |
7 | 72 |
18241
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
73 // If the command starts and ends with double quotes, |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
74 // Enclose the command in parentheses. |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
75 cmd = NULL; |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
76 cmdlen = wcslen(p); |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
77 if (cmdlen >= 2 && p[0] == L'"' && p[cmdlen - 1] == L'"') |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
78 { |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
79 cmdlen += 3; |
21915
2559dc02bd64
patch 8.2.1507: using malloc() directly
Bram Moolenaar <Bram@vim.org>
parents:
18241
diff
changeset
|
80 cmd = malloc(cmdlen * sizeof(wchar_t)); |
18241
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
81 if (cmd == NULL) |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
82 { |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
83 perror("vimrun malloc(): "); |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
84 return -1; |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
85 } |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
86 _snwprintf(cmd, cmdlen, L"(%s)", p); |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
87 p = cmd; |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
88 } |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
89 |
7 | 90 /* |
91 * Do it! | |
92 */ | |
9840
da7307987ed1
commit https://github.com/vim/vim/commit/bcc1dcc981dfc092587d4fbd1327d82a03426c57
Christian Brabandt <cb@256bit.org>
parents:
7166
diff
changeset
|
93 retval = _wsystem(p); |
7 | 94 |
18241
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
95 if (cmd) |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
96 free(cmd); |
85160a3649b9
patch 8.1.2115: MS-Windows: shell commands fail if &shell contains a space
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
97 |
1121 | 98 if (retval == -1) |
99 perror("vimrun system(): "); | |
100 else if (retval != 0) | |
7 | 101 printf("shell returned %d\n", retval); |
102 | |
103 if (!silent) | |
104 { | |
105 puts("Hit any key to close this window..."); | |
106 | |
107 while (_kbhit()) | |
108 (void)_getch(); | |
109 (void)_getch(); | |
110 } | |
111 | |
112 return retval; | |
113 } |