* net/tramp.el (tramp-default-file-modes) Remove execute permissions.
[bpt/emacs.git] / nt / runemacs.c
CommitLineData
f5d0ac07 1/* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
62eda0e2 2 Free Software Foundation, Inc.
b65d8176
TTN
3
4This file is part of GNU Emacs.
5
eef0be9e 6GNU Emacs is free software: you can redistribute it and/or modify
b65d8176 7it under the terms of the GNU General Public License as published by
eef0be9e
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
b65d8176
TTN
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
eef0be9e 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
b65d8176
TTN
18
19
c911543b
GV
20/*
21 Simple program to start Emacs with its console window hidden.
22
23 This program is provided purely for convenience, since most users will
24 use Emacs in windowing (GUI) mode, and will not want to have an extra
25 console window lying around. */
26
662463d9
RS
27/*
28 You may want to define this if you want to be able to install updated
29 emacs binaries even when other users are using the current version.
30 The problem with some file servers (notably Novell) is that an open
31 file cannot be overwritten, deleted, or even renamed. So if someone
32 is running emacs.exe already, you cannot install a newer version.
33 By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
34 something else which matches "emacs*.exe", and runemacs will
4da0d3f7 35 automatically select the newest emacs executable in the bin directory.
662463d9
RS
36 (So you'll probably be able to delete the old version some hours/days
37 later).
38*/
39
40/* #define CHOOSE_NEWEST_EXE */
41
c911543b
GV
42#include <windows.h>
43#include <string.h>
44#include <malloc.h>
45
46int WINAPI
47WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
48{
49 STARTUPINFO start;
50 SECURITY_ATTRIBUTES sec_attrs;
c911543b
GV
51 PROCESS_INFORMATION child;
52 int wait_for_child = FALSE;
05c4be3c 53 DWORD priority_class = NORMAL_PRIORITY_CLASS;
c911543b 54 DWORD ret_code = 0;
662463d9
RS
55 char *new_cmdline;
56 char *p;
c911543b
GV
57 char modname[MAX_PATH];
58
59 if (!GetModuleFileName (NULL, modname, MAX_PATH))
60 goto error;
61 if ((p = strrchr (modname, '\\')) == NULL)
62 goto error;
63 *p = 0;
64
0ac7bf6c
JR
65 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 3);
66 /* Quote executable name in case of spaces in the path. */
67 *new_cmdline = '"';
68 strcpy (new_cmdline + 1, modname);
c911543b 69
662463d9 70#ifdef CHOOSE_NEWEST_EXE
c911543b 71 {
662463d9
RS
72 /* Silly hack to allow new versions to be installed on
73 server even when current version is in use. */
74
75 char * best_name = alloca (MAX_PATH + 1);
76 FILETIME best_time = {0,0};
77 WIN32_FIND_DATA wfd;
78 HANDLE fh;
79 p = new_cmdline + strlen (new_cmdline);
0ac7bf6c 80 strcpy (p, "\\emacs*.exe\" ");
662463d9
RS
81 fh = FindFirstFile (new_cmdline, &wfd);
82 if (fh == INVALID_HANDLE_VALUE)
83 goto error;
84 do
85 {
4da0d3f7
JB
86 if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
87 || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
88 && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
89 {
90 best_time = wfd.ftLastWriteTime;
91 strcpy (best_name, wfd.cFileName);
92 }
662463d9
RS
93 }
94 while (FindNextFile (fh, &wfd));
95 FindClose (fh);
96 *p++ = '\\';
97 strcpy (p, best_name);
98 strcat (p, " ");
c911543b 99 }
662463d9 100#else
0ac7bf6c 101 strcat (new_cmdline, "\\emacs.exe\" ");
662463d9
RS
102#endif
103
05c4be3c
GV
104 /* Append original arguments if any; first look for arguments we
105 recognise (-wait, -high, and -low), and apply them ourselves. */
106 while (cmdline[0] == '-' || cmdline[0] == '/')
662463d9 107 {
05c4be3c
GV
108 if (strncmp (cmdline+1, "wait", 4) == 0)
109 {
4da0d3f7
JB
110 wait_for_child = TRUE;
111 cmdline += 5;
112 }
05c4be3c
GV
113 else if (strncmp (cmdline+1, "high", 4) == 0)
114 {
115 priority_class = HIGH_PRIORITY_CLASS;
116 cmdline += 5;
117 }
118 else if (strncmp (cmdline+1, "low", 3) == 0)
119 {
120 priority_class = IDLE_PRIORITY_CLASS;
121 cmdline += 4;
122 }
123 else
124 break;
4da0d3f7
JB
125 /* Look for next argument. */
126 while (*++cmdline == ' ');
05c4be3c 127 }
4da0d3f7 128
c911543b
GV
129 strcat (new_cmdline, cmdline);
130
662463d9 131 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
c911543b 132 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
662463d9
RS
133 {
134 *p = 0;
135 for (p = modname; *p; p++)
136 if (*p == '\\') *p = '/';
137 SetEnvironmentVariable ("emacs_dir", modname);
138 }
c911543b
GV
139
140 memset (&start, 0, sizeof (start));
141 start.cb = sizeof (start);
3bb69bbd 142 start.dwFlags = STARTF_USESHOWWINDOW | STARTF_USECOUNTCHARS;
c911543b 143 start.wShowWindow = SW_HIDE;
3bb69bbd
JR
144 /* Ensure that we don't waste memory if the user has specified a huge
145 default screen buffer for command windows. */
146 start.dwXCountChars = 80;
147 start.dwYCountChars = 25;
c911543b
GV
148
149 sec_attrs.nLength = sizeof (sec_attrs);
150 sec_attrs.lpSecurityDescriptor = NULL;
151 sec_attrs.bInheritHandle = FALSE;
152
05c4be3c 153 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, priority_class,
1e3c9713 154 NULL, NULL, &start, &child))
c911543b
GV
155 {
156 if (wait_for_child)
157 {
158 WaitForSingleObject (child.hProcess, INFINITE);
159 GetExitCodeProcess (child.hProcess, &ret_code);
160 }
161 CloseHandle (child.hThread);
162 CloseHandle (child.hProcess);
163 }
164 else
165 goto error;
166 return (int) ret_code;
167
168error:
169 MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
170 return 1;
171}
ab5796a9
MB
172
173/* arch-tag: 7e02df73-4df7-4aa0-baea-99c6d047a384
174 (do not change this comment) */