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