183
|
1 /*
|
|
2 Simple program to start Emacs with its console window hidden.
|
|
3
|
|
4 This program is provided purely for convenience, since most users will
|
|
5 use Emacs in windowing (GUI) mode, and will not want to have an extra
|
|
6 console window lying around. */
|
|
7
|
|
8 /*
|
|
9 You may want to define this if you want to be able to install updated
|
|
10 emacs binaries even when other users are using the current version.
|
|
11 The problem with some file servers (notably Novell) is that an open
|
|
12 file cannot be overwritten, deleted, or even renamed. So if someone
|
|
13 is running emacs.exe already, you cannot install a newer version.
|
|
14 By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
|
|
15 something else which matches "emacs*.exe", and runemacs will
|
|
16 automatically select the newest emacs executeable in the bin directory.
|
|
17 (So you'll probably be able to delete the old version some hours/days
|
|
18 later).
|
|
19 */
|
|
20
|
|
21 /* #define CHOOSE_NEWEST_EXE */
|
|
22
|
|
23 #define WIN32
|
|
24
|
|
25 #include <windows.h>
|
|
26 #include <string.h>
|
|
27 #include <malloc.h>
|
|
28
|
290
|
29 #if defined(__CYGWIN32__)
|
|
30 #include <sys/types.h>
|
|
31 #include <sys/stat.h>
|
|
32 #endif
|
|
33
|
183
|
34 int WINAPI
|
|
35 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
|
|
36 {
|
|
37 STARTUPINFO start;
|
|
38 SECURITY_ATTRIBUTES sec_attrs;
|
|
39 SECURITY_DESCRIPTOR sec_desc;
|
|
40 PROCESS_INFORMATION child;
|
|
41 int wait_for_child = FALSE;
|
|
42 DWORD ret_code = 0;
|
|
43 char *new_cmdline;
|
321
|
44 char *basename;
|
183
|
45 char *p;
|
|
46 char modname[MAX_PATH];
|
|
47
|
|
48 if (!GetModuleFileName (NULL, modname, MAX_PATH))
|
|
49 goto error;
|
|
50 if ((p = strrchr (modname, '\\')) == NULL)
|
|
51 goto error;
|
321
|
52
|
|
53 basename = alloca(strlen(p) + 1);
|
|
54 strcpy (basename, p + 1);
|
|
55
|
183
|
56 *p = 0;
|
|
57
|
|
58 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 1);
|
|
59 strcpy (new_cmdline, modname);
|
|
60
|
|
61 #ifdef CHOOSE_NEWEST_EXE
|
|
62 {
|
|
63 /* Silly hack to allow new versions to be installed on
|
|
64 server even when current version is in use. */
|
|
65
|
|
66 char * best_name = alloca (MAX_PATH + 1);
|
|
67 FILETIME best_time = {0,0};
|
|
68 WIN32_FIND_DATA wfd;
|
|
69 HANDLE fh;
|
|
70 p = new_cmdline + strlen (new_cmdline);
|
|
71 strcpy (p, "\\xemacs*.exe ");
|
|
72 fh = FindFirstFile (new_cmdline, &wfd);
|
|
73 if (fh == INVALID_HANDLE_VALUE)
|
|
74 goto error;
|
|
75 do
|
|
76 {
|
|
77 if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
|
|
78 || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
|
|
79 && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
|
|
80 {
|
|
81 best_time = wfd.ftLastWriteTime;
|
|
82 strcpy (best_name, wfd.cFileName);
|
|
83 }
|
|
84 }
|
|
85 while (FindNextFile (fh, &wfd));
|
|
86 FindClose (fh);
|
|
87 *p++ = '\\';
|
|
88 strcpy (p, best_name);
|
|
89 strcat (p, " ");
|
|
90 }
|
|
91 #else
|
290
|
92 #if defined(__CYGWIN32__)
|
|
93 {
|
|
94 struct stat stbuf;
|
|
95 char sym_link_name[MAX_PATH+1], real_name[MAX_PATH+1];
|
|
96
|
|
97 strcpy(sym_link_name, new_cmdline);
|
321
|
98 if (strcmp(basename, "rungnuclient.exe") == 0)
|
|
99 strcat(new_cmdline, "\\gnuclient.exe ");
|
|
100 else if (strcmp(basename, "runemacs.exe") == 0)
|
290
|
101 {
|
321
|
102 strcat(sym_link_name, "\\xemacs");
|
|
103
|
|
104 if (lstat(sym_link_name, &stbuf) == 0)
|
290
|
105 {
|
321
|
106 if ((stbuf.st_mode & S_IFLNK) == S_IFLNK)
|
290
|
107 {
|
321
|
108 if (readlink(sym_link_name, real_name, sizeof(real_name)) == -1)
|
|
109 {
|
|
110 MessageBox (NULL, "Error reading symbolic link for xemacs",
|
|
111 "Error", MB_ICONSTOP);
|
|
112 return 1;
|
|
113 }
|
|
114 else
|
|
115 {
|
|
116 strcat(new_cmdline, "\\");
|
367
|
117 /* #### Horrible kludge to get round 1.1.6 bug in readlink. */
|
|
118 p = strrchr (real_name, '.');
|
|
119 if (p[1] == 'e' || p[1] == 'E'
|
|
120 && p[2] == 'x' || p[2] == 'X'
|
|
121 && p[3] == 'e' || p[3] == 'E')
|
|
122 p[4] = 0;
|
321
|
123 strcat(new_cmdline, real_name);
|
|
124 strcat(new_cmdline, " ");
|
|
125 }
|
290
|
126 }
|
|
127 else
|
321
|
128 strcat(new_cmdline, "\\xemacs ");
|
290
|
129 }
|
|
130 else
|
321
|
131 {
|
|
132 MessageBox (NULL, "can't locate XEmacs executable",
|
|
133 "Error", MB_ICONSTOP);
|
|
134 return 1;
|
|
135 }
|
290
|
136 }
|
|
137 }
|
|
138 #else
|
321
|
139 if (strcmp(basename, "rungnuclient.exe") == 0)
|
|
140 strcat (new_cmdline, "\\gnuclient.exe ");
|
|
141 else
|
|
142 strcat (new_cmdline, "\\xemacs.exe ");
|
183
|
143 #endif
|
290
|
144 #endif
|
183
|
145
|
|
146 /* Append original arguments if any; first look for -wait as first
|
|
147 argument, and apply that ourselves. */
|
|
148 if (strncmp (cmdline, "-wait", 5) == 0)
|
|
149 {
|
|
150 wait_for_child = TRUE;
|
|
151 cmdline += 5;
|
|
152 }
|
|
153 strcat (new_cmdline, cmdline);
|
|
154
|
|
155 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
|
|
156 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
|
|
157 {
|
|
158 *p = 0;
|
|
159 for (p = modname; *p; p++)
|
|
160 if (*p == '\\') *p = '/';
|
|
161 SetEnvironmentVariable ("emacs_dir", modname);
|
|
162 }
|
|
163
|
|
164 memset (&start, 0, sizeof (start));
|
|
165 start.cb = sizeof (start);
|
|
166 start.dwFlags = STARTF_USESHOWWINDOW;
|
|
167 start.wShowWindow = SW_HIDE;
|
|
168
|
|
169 sec_attrs.nLength = sizeof (sec_attrs);
|
|
170 sec_attrs.lpSecurityDescriptor = NULL;
|
|
171 sec_attrs.bInheritHandle = FALSE;
|
|
172
|
|
173 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, 0,
|
|
174 NULL, NULL, &start, &child))
|
|
175 {
|
|
176 if (wait_for_child)
|
|
177 {
|
|
178 WaitForSingleObject (child.hProcess, INFINITE);
|
|
179 GetExitCodeProcess (child.hProcess, &ret_code);
|
|
180 }
|
|
181 CloseHandle (child.hThread);
|
|
182 CloseHandle (child.hProcess);
|
|
183 }
|
|
184 else
|
|
185 goto error;
|
|
186 return (int) ret_code;
|
|
187
|
|
188 error:
|
321
|
189 MessageBox (NULL, "Could not start XEmacs or gnuclient.", "Error", MB_ICONSTOP);
|
183
|
190 return 1;
|
|
191 }
|