Fix bug #17334 with overrunning string bounds when PATH is broken.
[bpt/emacs.git] / nt / addpm.c
CommitLineData
cef9e134 1/* Add entries to the GNU Emacs Program Manager folder.
ba318903 2 Copyright (C) 1995, 2001-2014 Free Software Foundation, Inc.
cef9e134 3
bf2b146b
EN
4This file is part of GNU Emacs.
5
eef0be9e 6GNU Emacs is free software: you can redistribute it and/or modify
bf2b146b 7it under the terms of the GNU General Public License as published by
eef0be9e
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
bf2b146b
EN
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
eef0be9e 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
cef9e134 18
cef9e134 19/****************************************************************************
6fbcbee7
RS
20 *
21 * Program: addpm (adds emacs to the Windows program manager)
22 *
23 * Usage:
2cc1905e 24 * argv[1] = install path for emacs
84ef4ca2
JR
25 *
26 * argv[2] used to be an optional argument for setting the icon.
27 * But now Emacs has a professional looking icon of its own.
28 * If users really want to change it, they can go into the settings of
29 * the shortcut that is created and do it there.
6fbcbee7 30 */
cef9e134 31
84ef4ca2
JR
32/* Use parts of shell API that were introduced by the merge of IE4
33 into the desktop shell. If Windows 95 or NT4 users do not have IE4
34 installed, then the DDE fallback for creating icons the Windows 3.1
35 progman way will be used instead, but that is prone to lockups
36 caused by other applications not servicing their message queues. */
c8e9d2b4
EZ
37#include <stdlib.h>
38#include <stdio.h>
39#include <malloc.h>
40
b88b62de
EZ
41/* MinGW64 defines _W64 and barfs if _WIN32_IE is defined to anything
42 below 0x500. */
43#ifndef _W64
84ef4ca2 44#define _WIN32_IE 0x400
b88b62de 45#endif
84ef4ca2
JR
46/* Request C Object macros for COM interfaces. */
47#define COBJMACROS 1
48
6fbcbee7 49#include <windows.h>
84ef4ca2 50#include <shlobj.h>
6fbcbee7 51#include <ddeml.h>
cef9e134 52
7d9fb4de
EZ
53#ifndef OLD_PATHS
54#include "../src/epaths.h"
55#endif
56
177c0ea7 57HDDEDATA CALLBACK
2cc1905e
GV
58DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
59 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
60 DWORD dwData1, DWORD dwData2)
6fbcbee7 61{
2cc1905e 62 return ((HDDEDATA) NULL);
6fbcbee7 63}
cef9e134 64
6fbcbee7 65#define DdeCommand(str) \
84ef4ca2 66 DdeClientTransaction (str, strlen (str)+1, conversation, (HSZ)NULL, \
6fbcbee7 67 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
cef9e134 68
c6e63684 69#define REG_ROOT "SOFTWARE\\GNU\\Emacs"
f1cefe09
JR
70#define REG_GTK "SOFTWARE\\GTK\\2.0"
71#define REG_APP_PATH \
72 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
856a6b77
JR
73#define REG_RUNEMACS_PATH \
74 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\runemacs.exe"
2cc1905e
GV
75
76static struct entry
77{
0597ab06
JB
78 const char *name;
79 const char *value;
177c0ea7
JB
80}
81env_vars[] =
2cc1905e 82{
7d9fb4de 83#ifdef OLD_PATHS
c6e63684 84 {"emacs_dir", NULL},
cb6c95a3 85 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp"},
0655d4d4 86 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
26b430b7
RS
87 {"EMACSDATA", "%emacs_dir%/etc"},
88 {"EMACSPATH", "%emacs_dir%/bin"},
9296c0e8
GV
89 /* We no longer set INFOPATH because Info-default-directory-list
90 is then ignored. */
91 /* {"INFOPATH", "%emacs_dir%/info"}, */
26b430b7 92 {"EMACSDOC", "%emacs_dir%/etc"},
2cc1905e 93 {"TERM", "cmd"}
7d9fb4de
EZ
94#else /* !OLD_PATHS */
95 {"emacs_dir", NULL},
96 {"EMACSLOADPATH", PATH_SITELOADSEARCH ";" PATH_LOADSEARCH},
97 {"SHELL", PATH_EXEC "/cmdproxy.exe"},
98 {"EMACSDATA", PATH_DATA},
99 {"EMACSPATH", PATH_EXEC},
100 /* We no longer set INFOPATH because Info-default-directory-list
101 is then ignored. */
102 /* {"INFOPATH", "%emacs_dir%/info"}, */
103 {"EMACSDOC", PATH_DOC},
104 {"TERM", "cmd"}
105#endif
2cc1905e
GV
106};
107
177c0ea7 108BOOL
0597ab06 109add_registry (const char *path)
2cc1905e
GV
110{
111 HKEY hrootkey = NULL;
2cc1905e
GV
112 int i;
113 BOOL ok = TRUE;
f1cefe09
JR
114 DWORD size;
115
116 /* Record the location of Emacs to the App Paths key if we have
117 sufficient permissions to do so. This helps Windows find emacs quickly
118 if the user types emacs.exe in the "Run Program" dialog without having
119 emacs on their path. Some applications also use the same registry key
120 to discover the installation directory for programs they are looking for.
121 Multiple installations cannot be handled by this method, but it does not
122 affect the general operation of other installations of Emacs, and we
123 are blindly overwriting the Start Menu entries already.
124 */
c44b4b46 125 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, "",
f1cefe09
JR
126 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
127 &hrootkey, NULL) == ERROR_SUCCESS)
128 {
129 int len;
130 char *emacs_path;
131 HKEY gtk_key = NULL;
132
133 len = strlen (path) + 15; /* \bin\emacs.exe + terminator. */
c44b4b46 134 emacs_path = (char *) alloca (len);
f1cefe09
JR
135 sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
136
926cd98c 137 RegSetValueEx (hrootkey, NULL, 0, REG_EXPAND_SZ, emacs_path, len);
f1cefe09
JR
138
139 /* Look for a GTK installation. If found, add it to the library search
140 path for Emacs so that the image libraries it provides are available
141 to Emacs regardless of whether it is in the path or not. */
142 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_GTK, REG_OPTION_NON_VOLATILE,
143 KEY_READ, &gtk_key) == ERROR_SUCCESS)
144 {
145 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
146 NULL, &size) == ERROR_SUCCESS)
147 {
148 char *gtk_path = (char *) alloca (size);
149 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
150 gtk_path, &size) == ERROR_SUCCESS)
151 {
152 /* Make sure the emacs bin directory continues to be searched
153 first by including it as well. */
154 char *dll_paths;
856a6b77 155 HKEY runemacs_key = NULL;
f1cefe09
JR
156 len = strlen (path) + 5 + size;
157 dll_paths = (char *) alloca (size + strlen (path) + 1);
158 sprintf (dll_paths, "%s\\bin;%s", path, gtk_path);
926cd98c
JB
159 RegSetValueEx (hrootkey, "Path", 0, REG_EXPAND_SZ,
160 dll_paths, len);
856a6b77
JR
161
162 /* Set the same path for runemacs.exe, as the Explorer shell
163 looks this up, so the above does not take effect when
164 emacs.exe is spawned from runemacs.exe. */
165 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_RUNEMACS_PATH,
166 0, "", REG_OPTION_NON_VOLATILE,
167 KEY_WRITE, NULL, &runemacs_key, NULL)
168 == ERROR_SUCCESS)
169 {
926cd98c 170 RegSetValueEx (runemacs_key, "Path", 0, REG_EXPAND_SZ,
856a6b77
JR
171 dll_paths, len);
172
173 RegCloseKey (runemacs_key);
174 }
f1cefe09
JR
175 }
176 }
177 RegCloseKey (gtk_key);
178 }
179 RegCloseKey (hrootkey);
180 }
177c0ea7 181
ebe98f49
JR
182 /* Previous versions relied on registry settings, but we do not need
183 them any more. If registry settings are installed from a previous
184 version, replace them to ensure they are the current settings.
185 Otherwise, do nothing. */
186
177c0ea7 187 /* Check both the current user and the local machine to see if we
2cc1905e 188 have any resources. */
177c0ea7 189
ebe98f49
JR
190 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT,
191 REG_OPTION_NON_VOLATILE,
192 KEY_WRITE, &hrootkey) != ERROR_SUCCESS
193 && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT,
194 REG_OPTION_NON_VOLATILE,
195 KEY_WRITE, &hrootkey) != ERROR_SUCCESS)
2cc1905e
GV
196 {
197 return FALSE;
198 }
177c0ea7
JB
199
200 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
2cc1905e 201 {
0597ab06 202 const char * value = env_vars[i].value ? env_vars[i].value : path;
177c0ea7 203
2cc1905e
GV
204 if (RegSetValueEx (hrootkey, env_vars[i].name,
205 0, REG_EXPAND_SZ,
206 value, lstrlen (value) + 1) != ERROR_SUCCESS)
207 ok = FALSE;
177c0ea7
JB
208 }
209
2cc1905e 210 RegCloseKey (hrootkey);
177c0ea7 211
2cc1905e
GV
212 return (ok);
213}
214
215int
7c3320d8 216main (int argc, char *argv[])
cef9e134 217{
84ef4ca2
JR
218 char start_folder[MAX_PATH + 1];
219 int shortcuts_created = 0;
220 int com_available = 1;
65cd6687 221 char modname[MAX_PATH];
0597ab06
JB
222 const char *prog_name;
223 const char *emacs_path;
65cd6687 224 char *p;
5205d900 225 int quiet = 0;
84ef4ca2
JR
226 HRESULT result;
227 IShellLinkA *shortcut;
cef9e134 228
65cd6687 229 /* If no args specified, use our location to set emacs_path. */
cef9e134 230
f4c6fac4
JR
231 if (argc > 1
232 && (argv[1][0] == '/' || argv[1][0] == '-')
233 && argv[1][1] == 'q')
5205d900
AI
234 {
235 quiet = 1;
236 --argc;
237 ++argv;
238 }
239
65cd6687
GV
240 if (argc > 1)
241 emacs_path = argv[1];
242 else
243 {
244 if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
245 (p = strrchr (modname, '\\')) == NULL)
246 {
247 fprintf (stderr, "fatal error");
248 exit (1);
249 }
250 *p = 0;
251
252 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
253 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
254 {
255 *p = 0;
256 emacs_path = modname;
257 }
258 else
259 {
84ef4ca2 260 fprintf (stderr, "usage: addpm emacs_path\n");
65cd6687
GV
261 exit (1);
262 }
263
264 /* Tell user what we are going to do. */
5205d900
AI
265 if (!quiet)
266 {
267 int result;
268
269 char msg[ MAX_PATH ];
270 sprintf (msg, "Install Emacs at %s?\n", emacs_path);
271 result = MessageBox (NULL, msg, "Install Emacs",
272 MB_OKCANCEL | MB_ICONQUESTION);
273 if (result != IDOK)
274 {
c80e3b4a 275 fprintf (stderr, "Install canceled\n");
5205d900
AI
276 exit (1);
277 }
278 }
65cd6687
GV
279 }
280
5205d900
AI
281 add_registry (emacs_path);
282 prog_name = "runemacs.exe";
2cc1905e 283
84ef4ca2 284 /* Try to install globally. */
cef9e134 285
84ef4ca2
JR
286 if (!SUCCEEDED (CoInitialize (NULL))
287 || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink, NULL,
288 CLSCTX_INPROC_SERVER, &IID_IShellLinkA,
289 (void **) &shortcut)))
290 {
291 com_available = 0;
292 }
cef9e134 293
84ef4ca2
JR
294 if (com_available
295 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
cef9e134 296 {
84ef4ca2
JR
297 if (strlen (start_folder) < (MAX_PATH - 20))
298 {
84ef4ca2
JR
299 strcat (start_folder, "\\Gnu Emacs");
300 if (CreateDirectory (start_folder, NULL)
301 || GetLastError () == ERROR_ALREADY_EXISTS)
302 {
303 char full_emacs_path[MAX_PATH + 1];
304 IPersistFile *lnk;
305 strcat (start_folder, "\\Emacs.lnk");
306 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
307 IShellLinkA_SetPath (shortcut, full_emacs_path);
308 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
309 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
310 (void **) &lnk);
311 if (SUCCEEDED (result))
312 {
313 wchar_t unicode_path[MAX_PATH];
314 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
315 unicode_path, MAX_PATH);
316 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
317 shortcuts_created = 1;
318 IPersistFile_Release (lnk);
319 }
320 }
321 }
322 }
323
324 if (!shortcuts_created && com_available
325 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_PROGRAMS, 0))
326 {
327 /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
328 if (strlen (start_folder) < (MAX_PATH - 20))
329 {
84ef4ca2
JR
330 strcat (start_folder, "\\Gnu Emacs");
331 if (CreateDirectory (start_folder, NULL)
332 || GetLastError () == ERROR_ALREADY_EXISTS)
333 {
334 char full_emacs_path[MAX_PATH + 1];
335 IPersistFile *lnk;
336 strcat (start_folder, "\\Emacs.lnk");
337 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
338 IShellLinkA_SetPath (shortcut, full_emacs_path);
339 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
340 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
341 (void **) &lnk);
342 if (SUCCEEDED (result))
343 {
344 wchar_t unicode_path[MAX_PATH];
345 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
346 unicode_path, MAX_PATH);
347 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
348 shortcuts_created = 1;
349 IPersistFile_Release (lnk);
7c3320d8 350
84ef4ca2
JR
351 }
352 }
7c3320d8 353 }
cef9e134
GV
354 }
355
84ef4ca2
JR
356 if (com_available)
357 IShellLinkA_Release (shortcut);
358
359 /* Need to call uninitialize, even if ComInitialize failed. */
360 CoUninitialize ();
361
362 /* Fallback on old DDE method if the above failed. */
363 if (!shortcuts_created)
364 {
365 DWORD dde = 0;
366 HCONV conversation;
367 HSZ progman;
368 char add_item[MAX_PATH*2 + 100];
369
370 DdeInitialize (&dde, (PFNCALLBACK) DdeCallback, APPCMD_CLIENTONLY, 0);
371 progman = DdeCreateStringHandle (dde, "PROGMAN", CP_WINANSI);
372 conversation = DdeConnect (dde, progman, progman, NULL);
373 if (conversation)
374 {
375 DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
376 DdeCommand ("[ReplaceItem (Emacs)]");
377 sprintf (add_item, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
378 emacs_path, prog_name);
379 DdeCommand (add_item);
380
381 DdeDisconnect (conversation);
382 }
6fbcbee7 383
84ef4ca2
JR
384 DdeFreeStringHandle (dde, progman);
385 DdeUninitialize (dde);
386 }
cef9e134 387
84ef4ca2 388 return 0;
cef9e134 389}