2 Simple program to start Emacs with its console window hidden.
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. */
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
21 /* #define CHOOSE_NEWEST_EXE */
29 #if defined(__CYGWIN32__)
30 #include <sys/types.h>
35 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
38 SECURITY_ATTRIBUTES sec_attrs;
39 SECURITY_DESCRIPTOR sec_desc;
40 PROCESS_INFORMATION child;
41 int wait_for_child = FALSE;
46 char modname[MAX_PATH];
48 if (!GetModuleFileName (NULL, modname, MAX_PATH))
50 if ((p = strrchr (modname, '\\')) == NULL)
53 basename = alloca(strlen(p) + 1);
54 strcpy (basename, p + 1);
58 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 1);
59 strcpy (new_cmdline, modname);
61 #ifdef CHOOSE_NEWEST_EXE
63 /* Silly hack to allow new versions to be installed on
64 server even when current version is in use. */
66 char * best_name = alloca (MAX_PATH + 1);
67 FILETIME best_time = {0,0};
70 p = new_cmdline + strlen (new_cmdline);
71 strcpy (p, "\\xemacs*.exe ");
72 fh = FindFirstFile (new_cmdline, &wfd);
73 if (fh == INVALID_HANDLE_VALUE)
77 if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
78 || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
79 && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
81 best_time = wfd.ftLastWriteTime;
82 strcpy (best_name, wfd.cFileName);
85 while (FindNextFile (fh, &wfd));
88 strcpy (p, best_name);
92 #if defined(__CYGWIN32__)
95 char sym_link_name[MAX_PATH+1], real_name[MAX_PATH+1];
97 strcpy(sym_link_name, new_cmdline);
98 if (strcmp(basename, "rungnuclient.exe") == 0)
99 strcat(new_cmdline, "\\gnuclient.exe ");
100 else if (strcmp(basename, "runemacs.exe") == 0)
102 strcat(sym_link_name, "\\xemacs");
104 if (lstat(sym_link_name, &stbuf) == 0)
106 if ((stbuf.st_mode & S_IFLNK) == S_IFLNK)
108 if (readlink(sym_link_name, real_name, sizeof(real_name)) == -1)
110 MessageBox (NULL, "Error reading symbolic link for xemacs",
111 "Error", MB_ICONSTOP);
116 strcat(new_cmdline, "\\");
117 strcat(new_cmdline, real_name);
118 strcat(new_cmdline, " ");
122 strcat(new_cmdline, "\\xemacs ");
126 MessageBox (NULL, "can't locate XEmacs executable",
127 "Error", MB_ICONSTOP);
133 if (strcmp(basename, "rungnuclient.exe") == 0)
134 strcat (new_cmdline, "\\gnuclient.exe ");
136 strcat (new_cmdline, "\\xemacs.exe ");
140 /* Append original arguments if any; first look for -wait as first
141 argument, and apply that ourselves. */
142 if (strncmp (cmdline, "-wait", 5) == 0)
144 wait_for_child = TRUE;
147 strcat (new_cmdline, cmdline);
149 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
150 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
153 for (p = modname; *p; p++)
154 if (*p == '\\') *p = '/';
155 SetEnvironmentVariable ("emacs_dir", modname);
158 memset (&start, 0, sizeof (start));
159 start.cb = sizeof (start);
160 start.dwFlags = STARTF_USESHOWWINDOW;
161 start.wShowWindow = SW_HIDE;
163 sec_attrs.nLength = sizeof (sec_attrs);
164 sec_attrs.lpSecurityDescriptor = NULL;
165 sec_attrs.bInheritHandle = FALSE;
167 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, 0,
168 NULL, NULL, &start, &child))
172 WaitForSingleObject (child.hProcess, INFINITE);
173 GetExitCodeProcess (child.hProcess, &ret_code);
175 CloseHandle (child.hThread);
176 CloseHandle (child.hProcess);
180 return (int) ret_code;
183 MessageBox (NULL, "Could not start XEmacs or gnuclient.", "Error", MB_ICONSTOP);