annotate nt/runemacs.c @ 183:e121b013d1f0 r20-3b18

Import from CVS: tag r20-3b18
author cvs
date Mon, 13 Aug 2007 09:54:23 +0200
parents 4be1180a9e89
children c9fe270a4101
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
183
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
1 /*
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
2 Simple program to start Emacs with its console window hidden.
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
3
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
4 This program is provided purely for convenience, since most users will
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
5 use Emacs in windowing (GUI) mode, and will not want to have an extra
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
6 console window lying around. */
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
7
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
8 /*
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
9 You may want to define this if you want to be able to install updated
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
10 emacs binaries even when other users are using the current version.
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
11 The problem with some file servers (notably Novell) is that an open
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
12 file cannot be overwritten, deleted, or even renamed. So if someone
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
13 is running emacs.exe already, you cannot install a newer version.
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
14 By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
15 something else which matches "emacs*.exe", and runemacs will
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
16 automatically select the newest emacs executeable in the bin directory.
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
17 (So you'll probably be able to delete the old version some hours/days
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
18 later).
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
19 */
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
20
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
21 /* #define CHOOSE_NEWEST_EXE */
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
22
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
23 #define WIN32
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
24
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
25 #include <windows.h>
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
26 #include <string.h>
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
27 #include <malloc.h>
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
28
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
29 int WINAPI
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
30 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
31 {
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
32 STARTUPINFO start;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
33 SECURITY_ATTRIBUTES sec_attrs;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
34 SECURITY_DESCRIPTOR sec_desc;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
35 PROCESS_INFORMATION child;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
36 int wait_for_child = FALSE;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
37 DWORD ret_code = 0;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
38 char *new_cmdline;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
39 char *p;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
40 char modname[MAX_PATH];
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
41
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
42 if (!GetModuleFileName (NULL, modname, MAX_PATH))
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
43 goto error;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
44 if ((p = strrchr (modname, '\\')) == NULL)
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
45 goto error;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
46 *p = 0;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
47
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
48 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 1);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
49 strcpy (new_cmdline, modname);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
50
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
51 #ifdef CHOOSE_NEWEST_EXE
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
52 {
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
53 /* Silly hack to allow new versions to be installed on
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
54 server even when current version is in use. */
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
55
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
56 char * best_name = alloca (MAX_PATH + 1);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
57 FILETIME best_time = {0,0};
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
58 WIN32_FIND_DATA wfd;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
59 HANDLE fh;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
60 p = new_cmdline + strlen (new_cmdline);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
61 strcpy (p, "\\xemacs*.exe ");
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
62 fh = FindFirstFile (new_cmdline, &wfd);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
63 if (fh == INVALID_HANDLE_VALUE)
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
64 goto error;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
65 do
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
66 {
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
67 if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
68 || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
69 && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
70 {
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
71 best_time = wfd.ftLastWriteTime;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
72 strcpy (best_name, wfd.cFileName);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
73 }
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
74 }
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
75 while (FindNextFile (fh, &wfd));
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
76 FindClose (fh);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
77 *p++ = '\\';
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
78 strcpy (p, best_name);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
79 strcat (p, " ");
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
80 }
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
81 #else
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
82 strcat (new_cmdline, "\\xemacs.exe ");
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
83 #endif
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
84
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
85 /* Append original arguments if any; first look for -wait as first
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
86 argument, and apply that ourselves. */
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
87 if (strncmp (cmdline, "-wait", 5) == 0)
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
88 {
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
89 wait_for_child = TRUE;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
90 cmdline += 5;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
91 }
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
92 strcat (new_cmdline, cmdline);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
93
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
94 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
95 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
96 {
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
97 *p = 0;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
98 for (p = modname; *p; p++)
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
99 if (*p == '\\') *p = '/';
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
100 SetEnvironmentVariable ("emacs_dir", modname);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
101 }
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
102
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
103 memset (&start, 0, sizeof (start));
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
104 start.cb = sizeof (start);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
105 start.dwFlags = STARTF_USESHOWWINDOW;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
106 start.wShowWindow = SW_HIDE;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
107
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
108 sec_attrs.nLength = sizeof (sec_attrs);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
109 sec_attrs.lpSecurityDescriptor = NULL;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
110 sec_attrs.bInheritHandle = FALSE;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
111
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
112 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, 0,
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
113 NULL, NULL, &start, &child))
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
114 {
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
115 if (wait_for_child)
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
116 {
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
117 WaitForSingleObject (child.hProcess, INFINITE);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
118 GetExitCodeProcess (child.hProcess, &ret_code);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
119 }
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
120 CloseHandle (child.hThread);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
121 CloseHandle (child.hProcess);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
122 }
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
123 else
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
124 goto error;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
125 return (int) ret_code;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
126
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
127 error:
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
128 MessageBox (NULL, "Could not start XEmacs.", "Error", MB_ICONSTOP);
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
129 return 1;
e121b013d1f0 Import from CVS: tag r20-3b18
cvs
parents: 100
diff changeset
130 }