xref: /vim-8.2.3635/src/vimrun.c (revision cf2d8dee)
1 /* vi:set ts=8 sts=4 sw=4:
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 #ifndef __CYGWIN__
21 # include <conio.h>
22 #endif
23 
24 #ifdef __BORLANDC__
25 extern char *
26 #ifdef _RTLDLL
27 __import
28 #endif
29 _oscmd;
30 # define _kbhit kbhit
31 # define _getch getch
32 #else
33 # ifdef __MINGW32__
34 #  ifndef WIN32_LEAN_AND_MEAN
35 #   define WIN32_LEAN_AND_MEAN
36 #  endif
37 #  include <windows.h>
38 # else
39 #  ifdef __CYGWIN__
40 #   ifndef WIN32_LEAN_AND_MEAN
41 #    define WIN32_LEAN_AND_MEAN
42 #   endif
43 #   include <windows.h>
44 #   define _getch getchar
45 #  else
46 extern char *_acmdln;
47 #  endif
48 # endif
49 #endif
50 
51     int
52 main(void)
53 {
54     const char	*p;
55     int		retval;
56     int		inquote = 0;
57     int		silent = 0;
58 
59 #ifdef __BORLANDC__
60     p = _oscmd;
61 #else
62 # if defined(__MINGW32__) || defined(__CYGWIN__)
63     p = (const char *)GetCommandLine();
64 # else
65     p = _acmdln;
66 # endif
67 #endif
68     /*
69      * Skip the executable name, which might be in "".
70      */
71     while (*p)
72     {
73 	if (*p == '"')
74 	    inquote = !inquote;
75 	else if (!inquote && *p == ' ')
76 	{
77 	    ++p;
78 	    break;
79 	}
80 	++p;
81     }
82     while (*p == ' ')
83         ++p;
84 
85     /*
86      * "-s" argument: don't wait for a key hit.
87      */
88     if (p[0] == '-' && p[1] == 's' && p[2] == ' ')
89     {
90 	silent = 1;
91 	p += 3;
92 	while (*p == ' ')
93 	    ++p;
94     }
95 
96     /* Print the command, including quotes and redirection. */
97     puts(p);
98 
99     /*
100      * Do it!
101      */
102     retval = system(p);
103 
104     if (retval == -1)
105 	perror("vimrun system(): ");
106     else if (retval != 0)
107 	printf("shell returned %d\n", retval);
108 
109     if (!silent)
110     {
111 	puts("Hit any key to close this window...");
112 
113 #ifndef __CYGWIN__
114 	while (_kbhit())
115 	    (void)_getch();
116 #endif
117 	(void)_getch();
118     }
119 
120     return retval;
121 }
122