xref: /vim-8.2.3635/src/vimrun.c (revision bb76f24a)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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>
20 #include <conio.h>
21 #ifndef WIN32_LEAN_AND_MEAN
22 # define WIN32_LEAN_AND_MEAN
23 #endif
24 #include <windows.h>
25 
26 #ifdef __BORLANDC__
27 # define _kbhit kbhit
28 # define _getch getch
29 #endif
30 
31     int
32 main(void)
33 {
34     const wchar_t   *p;
35     int		    retval;
36     int		    inquote = 0;
37     int		    silent = 0;
38     HANDLE	    hstdout;
39     DWORD	    written;
40 
41     p = (const wchar_t *)GetCommandLineW();
42 
43     /*
44      * Skip the executable name, which might be in "".
45      */
46     while (*p)
47     {
48 	if (*p == L'"')
49 	    inquote = !inquote;
50 	else if (!inquote && *p == L' ')
51 	{
52 	    ++p;
53 	    break;
54 	}
55 	++p;
56     }
57     while (*p == L' ')
58         ++p;
59 
60     /*
61      * "-s" argument: don't wait for a key hit.
62      */
63     if (p[0] == L'-' && p[1] == L's' && p[2] == L' ')
64     {
65 	silent = 1;
66 	p += 3;
67 	while (*p == L' ')
68 	    ++p;
69     }
70 
71     /* Print the command, including quotes and redirection. */
72     hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
73     WriteConsoleW(hstdout, p, wcslen(p), &written, NULL);
74     WriteConsoleW(hstdout, L"\r\n", 2, &written, NULL);
75 
76     /*
77      * Do it!
78      */
79     retval = _wsystem(p);
80 
81     if (retval == -1)
82 	perror("vimrun system(): ");
83     else if (retval != 0)
84 	printf("shell returned %d\n", retval);
85 
86     if (!silent)
87     {
88 	puts("Hit any key to close this window...");
89 
90 	while (_kbhit())
91 	    (void)_getch();
92 	(void)_getch();
93     }
94 
95     return retval;
96 }
97