blob: 2566859fa6d6bf17a4b3050dd70bc4225b1bbbf4 [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
26#ifdef __BORLANDC__
Bram Moolenaar071d4272004-06-13 20:20:40 +000027# define _kbhit kbhit
28# define _getch getch
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#endif
30
31 int
32main(void)
33{
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020034 const wchar_t *p;
35 int retval;
36 int inquote = 0;
37 int silent = 0;
38 HANDLE hstdout;
39 DWORD written;
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020041 p = (const wchar_t *)GetCommandLineW();
42
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 /*
44 * Skip the executable name, which might be in "".
45 */
46 while (*p)
47 {
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020048 if (*p == L'"')
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 inquote = !inquote;
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020050 else if (!inquote && *p == L' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 {
52 ++p;
53 break;
54 }
55 ++p;
56 }
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020057 while (*p == L' ')
Bram Moolenaarf59c73d2015-10-13 17:52:59 +020058 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
60 /*
61 * "-s" argument: don't wait for a key hit.
62 */
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020063 if (p[0] == L'-' && p[1] == L's' && p[2] == L' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 {
65 silent = 1;
66 p += 3;
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020067 while (*p == L' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 ++p;
69 }
70
71 /* Print the command, including quotes and redirection. */
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020072 hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
73 WriteConsoleW(hstdout, p, wcslen(p), &written, NULL);
74 WriteConsoleW(hstdout, L"\r\n", 2, &written, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76 /*
77 * Do it!
78 */
Bram Moolenaarbcc1dcc2016-08-10 22:02:40 +020079 retval = _wsystem(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar9964e462007-05-05 17:54:07 +000081 if (retval == -1)
82 perror("vimrun system(): ");
83 else if (retval != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 printf("shell returned %d\n", retval);
85
86 if (!silent)
87 {
88 puts("Hit any key to close this window...");
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090 while (_kbhit())
91 (void)_getch();
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 (void)_getch();
93 }
94
95 return retval;
96}