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