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