addpm.c 5.35 KB
Newer Older
Geoff Voelker's avatar
Geoff Voelker committed
1
/* Add entries to the GNU Emacs Program Manager folder.
2
   Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005,
Glenn Morris's avatar
Glenn Morris committed
3
      2006, 2007, 2008  Free Software Foundation, Inc.
Geoff Voelker's avatar
Geoff Voelker committed
4

Erik Naggum's avatar
Erik Naggum committed
5 6 7 8
This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 3, or (at your option)
Erik Naggum's avatar
Erik Naggum committed
10 11 12 13 14 15 16 17 18
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
Lute Kamstra's avatar
Lute Kamstra committed
19 20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Geoff Voelker's avatar
Geoff Voelker committed
21 22

/****************************************************************************
Richard M. Stallman's avatar
Richard M. Stallman committed
23 24 25 26
 *
 * Program: addpm	(adds emacs to the Windows program manager)
 *
 * Usage:
Geoff Voelker's avatar
Geoff Voelker committed
27
 *   	argv[1] = install path for emacs
Richard M. Stallman's avatar
Richard M. Stallman committed
28 29
 *	argv[2] = full path to icon for emacs (optional)
 */
Geoff Voelker's avatar
Geoff Voelker committed
30

Richard M. Stallman's avatar
Richard M. Stallman committed
31 32 33 34
#include <windows.h>
#include <ddeml.h>
#include <stdlib.h>
#include <stdio.h>
Geoff Voelker's avatar
Geoff Voelker committed
35

36
HDDEDATA CALLBACK
Geoff Voelker's avatar
Geoff Voelker committed
37 38 39
DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
	     HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
	     DWORD dwData1, DWORD dwData2)
Richard M. Stallman's avatar
Richard M. Stallman committed
40
{
Geoff Voelker's avatar
Geoff Voelker committed
41
  return ((HDDEDATA) NULL);
Richard M. Stallman's avatar
Richard M. Stallman committed
42
}
Geoff Voelker's avatar
Geoff Voelker committed
43

Richard M. Stallman's avatar
Richard M. Stallman committed
44
#define DdeCommand(str) 	\
Geoff Voelker's avatar
Geoff Voelker committed
45
	DdeClientTransaction (str, strlen (str)+1, HConversation, (HSZ)NULL, \
Richard M. Stallman's avatar
Richard M. Stallman committed
46
		              CF_TEXT, XTYP_EXECUTE, 30000, NULL)
Geoff Voelker's avatar
Geoff Voelker committed
47

48
#define REG_ROOT "SOFTWARE\\GNU\\Emacs"
Geoff Voelker's avatar
Geoff Voelker committed
49 50 51 52 53

static struct entry
{
  char *name;
  char *value;
54 55
}
env_vars[] =
Geoff Voelker's avatar
Geoff Voelker committed
56
{
57
  {"emacs_dir", NULL},
58
  {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
59
  {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
60 61
  {"EMACSDATA", "%emacs_dir%/etc"},
  {"EMACSPATH", "%emacs_dir%/bin"},
62 63 64
  /* We no longer set INFOPATH because Info-default-directory-list
     is then ignored.  */
  /*  {"INFOPATH", "%emacs_dir%/info"},  */
65
  {"EMACSDOC", "%emacs_dir%/etc"},
Geoff Voelker's avatar
Geoff Voelker committed
66 67 68
  {"TERM", "cmd"}
};

69
BOOL
Geoff Voelker's avatar
Geoff Voelker committed
70 71 72 73 74 75
add_registry (path)
     char *path;
{
  HKEY hrootkey = NULL;
  int i;
  BOOL ok = TRUE;
76

77 78 79 80 81
  /* Previous versions relied on registry settings, but we do not need
     them any more.  If registry settings are installed from a previous
     version, replace them to ensure they are the current settings.
     Otherwise, do nothing.  */

82
  /* Check both the current user and the local machine to see if we
Geoff Voelker's avatar
Geoff Voelker committed
83
     have any resources.  */
84

85 86 87 88 89 90
  if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT,
		      REG_OPTION_NON_VOLATILE,
		      KEY_WRITE, &hrootkey) != ERROR_SUCCESS
      && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT,
			 REG_OPTION_NON_VOLATILE,
			 KEY_WRITE, &hrootkey) != ERROR_SUCCESS)
Geoff Voelker's avatar
Geoff Voelker committed
91 92 93
    {
      return FALSE;
    }
94 95

  for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
Geoff Voelker's avatar
Geoff Voelker committed
96 97
    {
      char * value = env_vars[i].value ? env_vars[i].value : path;
98

Geoff Voelker's avatar
Geoff Voelker committed
99 100 101 102
      if (RegSetValueEx (hrootkey, env_vars[i].name,
			 0, REG_EXPAND_SZ,
			 value, lstrlen (value) + 1) != ERROR_SUCCESS)
	ok = FALSE;
103 104
    }

Geoff Voelker's avatar
Geoff Voelker committed
105
  RegCloseKey (hrootkey);
106

Geoff Voelker's avatar
Geoff Voelker committed
107 108 109 110
  return (ok);
}

int
Richard M. Stallman's avatar
Richard M. Stallman committed
111 112
main (argc, argv)
     int argc;
113
     char *argv[];
Geoff Voelker's avatar
Geoff Voelker committed
114
{
115
  DWORD idDde = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
116 117
  HCONV HConversation;
  HSZ ProgMan;
118
  char modname[MAX_PATH];
Geoff Voelker's avatar
Geoff Voelker committed
119
  char additem[MAX_PATH*2 + 100];
120
  char *prog_name;
121 122
  char *emacs_path;
  char *p;
123
  int quiet = 0;
Geoff Voelker's avatar
Geoff Voelker committed
124

125 126
  /* If no args specified, use our location to set emacs_path.  */
#if 0
Richard M. Stallman's avatar
Richard M. Stallman committed
127
  if (argc < 2 || argc > 3)
Geoff Voelker's avatar
Geoff Voelker committed
128
    {
129
      fprintf (stderr, "usage: addpm [-q] [emacs_path [icon_path]]\n");
Geoff Voelker's avatar
Geoff Voelker committed
130
      exit (1);
Geoff Voelker's avatar
Geoff Voelker committed
131
    }
132
#endif
Geoff Voelker's avatar
Geoff Voelker committed
133

134 135 136
  if (argc > 1
      && (argv[1][0] == '/' || argv[1][0] == '-')
      && argv[1][1] == 'q')
137 138 139 140 141 142
    {
      quiet = 1;
      --argc;
      ++argv;
    }

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  if (argc > 1)
    emacs_path = argv[1];
  else
    {
      if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
	  (p = strrchr (modname, '\\')) == NULL)
	{
	  fprintf (stderr, "fatal error");
	  exit (1);
	}
      *p = 0;

      /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin".  */
      if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
	{
	  *p = 0;
	  emacs_path = modname;
	}
      else
	{
	  fprintf (stderr, "usage: addpm emacs_path [icon_path]\n");
	  exit (1);
	}

      /* Tell user what we are going to do.  */
168 169 170 171 172 173 174 175 176 177 178 179 180 181
      if (!quiet)
	{
	  int result;

	  char msg[ MAX_PATH ];
	  sprintf (msg, "Install Emacs at %s?\n", emacs_path);
	  result = MessageBox (NULL, msg, "Install Emacs",
			       MB_OKCANCEL | MB_ICONQUESTION);
	  if (result != IDOK)
	    {
	      fprintf (stderr, "Install cancelled\n");
	      exit (1);
	    }
	}
182 183
    }

184 185
  add_registry (emacs_path);
  prog_name =  "runemacs.exe";
Geoff Voelker's avatar
Geoff Voelker committed
186

Richard M. Stallman's avatar
Richard M. Stallman committed
187
  DdeInitialize (&idDde, (PFNCALLBACK)DdeCallback, APPCMD_CLIENTONLY, 0);
Geoff Voelker's avatar
Geoff Voelker committed
188

Richard M. Stallman's avatar
Richard M. Stallman committed
189
  ProgMan = DdeCreateStringHandle (idDde, "PROGMAN", CP_WINANSI);
Geoff Voelker's avatar
Geoff Voelker committed
190

191 192
  HConversation = DdeConnect (idDde, ProgMan, ProgMan, NULL);
  if (HConversation != 0)
Geoff Voelker's avatar
Geoff Voelker committed
193
    {
194
      DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
Geoff Voelker's avatar
Geoff Voelker committed
195
      DdeCommand ("[ReplaceItem (Emacs)]");
196 197 198 199 200 201
      if (argc > 2)
	sprintf (additem, "[AddItem (\"%s\\bin\\%s\", Emacs, \"%s\")]",
		 emacs_path, prog_name, argv[2]);
      else
	sprintf (additem, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
		 emacs_path, prog_name);
Richard M. Stallman's avatar
Richard M. Stallman committed
202 203 204
      DdeCommand (additem);

      DdeDisconnect (HConversation);
Geoff Voelker's avatar
Geoff Voelker committed
205 206
    }

Richard M. Stallman's avatar
Richard M. Stallman committed
207 208 209
  DdeFreeStringHandle (idDde, ProgMan);

  DdeUninitialize (idDde);
Geoff Voelker's avatar
Geoff Voelker committed
210

Richard M. Stallman's avatar
Richard M. Stallman committed
211
  return (0);
Geoff Voelker's avatar
Geoff Voelker committed
212
}
Miles Bader's avatar
Miles Bader committed
213 214 215

/* arch-tag: f923609d-b781-4ef4-abce-ca0da29cbbf0
   (do not change this comment) */