blob: ece20f83926e36e7871701f3ffbec814e6f341bb [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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>
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020020#include <conio.h>
21#ifndef WIN32_LEAN_AND_MEAN
22# define WIN32_LEAN_AND_MEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#endif
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020024#include <windows.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 int
27main(void)
28{
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020029 const wchar_t *p;
30 int retval;
31 int inquote = 0;
32 int silent = 0;
33 HANDLE hstdout;
34 DWORD written;
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020036 p = (const wchar_t *)GetCommandLineW();
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 /*
39 * Skip the executable name, which might be in "".
40 */
41 while (*p)
42 {
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020043 if (*p == L'"')
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 inquote = !inquote;
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020045 else if (!inquote && *p == L' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 {
47 ++p;
48 break;
49 }
50 ++p;
51 }
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020052 while (*p == L' ')
Bram Moolenaar792f0e32018-02-27 17:27:13 +010053 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55 /*
56 * "-s" argument: don't wait for a key hit.
57 */
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020058 if (p[0] == L'-' && p[1] == L's' && p[2] == L' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 {
60 silent = 1;
61 p += 3;
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020062 while (*p == L' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 ++p;
64 }
65
66 /* Print the command, including quotes and redirection. */
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020067 hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
68 WriteConsoleW(hstdout, p, wcslen(p), &written, NULL);
69 WriteConsoleW(hstdout, L"\r\n", 2, &written, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71 /*
72 * Do it!
73 */
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020074 retval = _wsystem(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaar9964e462007-05-05 17:54:07 +000076 if (retval == -1)
77 perror("vimrun system(): ");
78 else if (retval != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 printf("shell returned %d\n", retval);
80
81 if (!silent)
82 {
83 puts("Hit any key to close this window...");
84
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 while (_kbhit())
86 (void)_getch();
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 (void)_getch();
88 }
89
90 return retval;
91}