(change-log-goto-source): Avoid wrong-type-argument
[emacs.git] / nt / addpm.c
blobc053810baaa733fbf18640c8a9dfb2917d8c33fb
1 /* Add entries to the GNU Emacs Program Manager folder.
2 Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /****************************************************************************
22 * Program: addpm (adds emacs to the Windows program manager)
24 * Usage:
25 * argv[1] = install path for emacs
26 * argv[2] = full path to icon for emacs (optional)
29 #include <windows.h>
30 #include <ddeml.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <malloc.h>
35 HDDEDATA CALLBACK
36 DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
37 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
38 DWORD dwData1, DWORD dwData2)
40 return ((HDDEDATA) NULL);
43 #define DdeCommand(str) \
44 DdeClientTransaction (str, strlen (str)+1, HConversation, (HSZ)NULL, \
45 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
47 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
48 #define REG_GTK "SOFTWARE\\GTK\\2.0"
49 #define REG_APP_PATH \
50 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
52 static struct entry
54 char *name;
55 char *value;
57 env_vars[] =
59 {"emacs_dir", NULL},
60 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
61 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
62 {"EMACSDATA", "%emacs_dir%/etc"},
63 {"EMACSPATH", "%emacs_dir%/bin"},
64 /* We no longer set INFOPATH because Info-default-directory-list
65 is then ignored. */
66 /* {"INFOPATH", "%emacs_dir%/info"}, */
67 {"EMACSDOC", "%emacs_dir%/etc"},
68 {"TERM", "cmd"}
71 BOOL
72 add_registry (path)
73 char *path;
75 HKEY hrootkey = NULL;
76 int i;
77 BOOL ok = TRUE;
78 DWORD size;
80 /* Record the location of Emacs to the App Paths key if we have
81 sufficient permissions to do so. This helps Windows find emacs quickly
82 if the user types emacs.exe in the "Run Program" dialog without having
83 emacs on their path. Some applications also use the same registry key
84 to discover the installation directory for programs they are looking for.
85 Multiple installations cannot be handled by this method, but it does not
86 affect the general operation of other installations of Emacs, and we
87 are blindly overwriting the Start Menu entries already.
89 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, "",
90 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
91 &hrootkey, NULL) == ERROR_SUCCESS)
93 int len;
94 char *emacs_path;
95 HKEY gtk_key = NULL;
97 len = strlen (path) + 15; /* \bin\emacs.exe + terminator. */
98 emacs_path = (char *) alloca (len);
99 sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
101 RegSetValueEx (hrootkey, NULL, 0, REG_SZ, emacs_path, len);
103 /* Look for a GTK installation. If found, add it to the library search
104 path for Emacs so that the image libraries it provides are available
105 to Emacs regardless of whether it is in the path or not. */
106 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_GTK, REG_OPTION_NON_VOLATILE,
107 KEY_READ, &gtk_key) == ERROR_SUCCESS)
109 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
110 NULL, &size) == ERROR_SUCCESS)
112 char *gtk_path = (char *) alloca (size);
113 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
114 gtk_path, &size) == ERROR_SUCCESS)
116 /* Make sure the emacs bin directory continues to be searched
117 first by including it as well. */
118 char *dll_paths;
119 len = strlen (path) + 5 + size;
120 dll_paths = (char *) alloca (size + strlen (path) + 1);
121 sprintf (dll_paths, "%s\\bin;%s", path, gtk_path);
122 RegSetValueEx (hrootkey, "Path", 0, REG_SZ, dll_paths, len);
125 RegCloseKey (gtk_key);
127 RegCloseKey (hrootkey);
130 /* Previous versions relied on registry settings, but we do not need
131 them any more. If registry settings are installed from a previous
132 version, replace them to ensure they are the current settings.
133 Otherwise, do nothing. */
135 /* Check both the current user and the local machine to see if we
136 have any resources. */
138 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT,
139 REG_OPTION_NON_VOLATILE,
140 KEY_WRITE, &hrootkey) != ERROR_SUCCESS
141 && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT,
142 REG_OPTION_NON_VOLATILE,
143 KEY_WRITE, &hrootkey) != ERROR_SUCCESS)
145 return FALSE;
148 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
150 char * value = env_vars[i].value ? env_vars[i].value : path;
152 if (RegSetValueEx (hrootkey, env_vars[i].name,
153 0, REG_EXPAND_SZ,
154 value, lstrlen (value) + 1) != ERROR_SUCCESS)
155 ok = FALSE;
158 RegCloseKey (hrootkey);
160 return (ok);
164 main (argc, argv)
165 int argc;
166 char *argv[];
168 DWORD idDde = 0;
169 HCONV HConversation;
170 HSZ ProgMan;
171 char modname[MAX_PATH];
172 char additem[MAX_PATH*2 + 100];
173 char *prog_name;
174 char *emacs_path;
175 char *p;
176 int quiet = 0;
178 /* If no args specified, use our location to set emacs_path. */
179 #if 0
180 if (argc < 2 || argc > 3)
182 fprintf (stderr, "usage: addpm [-q] [emacs_path [icon_path]]\n");
183 exit (1);
185 #endif
187 if (argc > 1
188 && (argv[1][0] == '/' || argv[1][0] == '-')
189 && argv[1][1] == 'q')
191 quiet = 1;
192 --argc;
193 ++argv;
196 if (argc > 1)
197 emacs_path = argv[1];
198 else
200 if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
201 (p = strrchr (modname, '\\')) == NULL)
203 fprintf (stderr, "fatal error");
204 exit (1);
206 *p = 0;
208 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
209 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
211 *p = 0;
212 emacs_path = modname;
214 else
216 fprintf (stderr, "usage: addpm emacs_path [icon_path]\n");
217 exit (1);
220 /* Tell user what we are going to do. */
221 if (!quiet)
223 int result;
225 char msg[ MAX_PATH ];
226 sprintf (msg, "Install Emacs at %s?\n", emacs_path);
227 result = MessageBox (NULL, msg, "Install Emacs",
228 MB_OKCANCEL | MB_ICONQUESTION);
229 if (result != IDOK)
231 fprintf (stderr, "Install cancelled\n");
232 exit (1);
237 add_registry (emacs_path);
238 prog_name = "runemacs.exe";
240 DdeInitialize (&idDde, (PFNCALLBACK)DdeCallback, APPCMD_CLIENTONLY, 0);
242 ProgMan = DdeCreateStringHandle (idDde, "PROGMAN", CP_WINANSI);
244 HConversation = DdeConnect (idDde, ProgMan, ProgMan, NULL);
245 if (HConversation != 0)
247 DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
248 DdeCommand ("[ReplaceItem (Emacs)]");
249 if (argc > 2)
250 sprintf (additem, "[AddItem (\"%s\\bin\\%s\", Emacs, \"%s\")]",
251 emacs_path, prog_name, argv[2]);
252 else
253 sprintf (additem, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
254 emacs_path, prog_name);
255 DdeCommand (additem);
257 DdeDisconnect (HConversation);
260 DdeFreeStringHandle (idDde, ProgMan);
262 DdeUninitialize (idDde);
264 return (0);
267 /* arch-tag: f923609d-b781-4ef4-abce-ca0da29cbbf0
268 (do not change this comment) */