diff options
| author | Juanma Barranquero | 2007-10-26 15:39:49 +0000 |
|---|---|---|
| committer | Juanma Barranquero | 2007-10-26 15:39:49 +0000 |
| commit | 70cd9104ca2f1f7443cda004d9ab576d49503aca (patch) | |
| tree | e8c5ba95394da3d681b902acb4be929a80d770f9 /lib-src | |
| parent | b1ac90c50af8342f341b22d532aa3304c99147cf (diff) | |
| download | emacs-70cd9104ca2f1f7443cda004d9ab576d49503aca.tar.gz emacs-70cd9104ca2f1f7443cda004d9ab576d49503aca.zip | |
Add a wrapper for getenv so it also checks the registry on Windows.
Suggestion and algorithm by Eli Zaretskii.
Code partially based on w32_get_resource and init_environment (w32.c).
(xmalloc): New function by Károly Lőrentey (backported from the trunk).
(quote_file_name): Use it.
(egetenv): New wrapper for getenv.
(get_current_dir_name, decode_options, get_server_config, set_local_socket,
set_socket, main): Use egetenv, not getenv.
(w32_get_resource, w32_getenv) [WINDOWSNT]: New functions.
Diffstat (limited to 'lib-src')
| -rw-r--r-- | lib-src/ChangeLog | 12 | ||||
| -rw-r--r-- | lib-src/emacsclient.c | 123 |
2 files changed, 128 insertions, 7 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog index c9599fee81d..a4e519e8954 100644 --- a/lib-src/ChangeLog +++ b/lib-src/ChangeLog | |||
| @@ -1,3 +1,15 @@ | |||
| 1 | 2007-10-26 Juanma Barranquero <lekktu@gmail.com> | ||
| 2 | |||
| 3 | * emacsclient.c: Add a wrapper for getenv so it also checks the | ||
| 4 | registry on Windows. Suggestion and algorithm by Eli Zaretskii. | ||
| 5 | Code partially based on w32_get_resource and init_environment (w32.c). | ||
| 6 | (xmalloc): New function by K,Aa(Broly L$,1 q(Brentey (backported from the trunk). | ||
| 7 | (quote_file_name): Use it. | ||
| 8 | (egetenv): New wrapper for getenv. | ||
| 9 | (get_current_dir_name, decode_options, get_server_config) | ||
| 10 | (set_local_socket, set_socket, main): Use egetenv, not getenv. | ||
| 11 | (w32_get_resource, w32_getenv) [WINDOWSNT]: New functions. | ||
| 12 | |||
| 1 | 2007-10-25 Jason Rumney <jasonr@gnu.org> | 13 | 2007-10-25 Jason Rumney <jasonr@gnu.org> |
| 2 | 14 | ||
| 3 | * emacsclient.c (sock_err_message): New function. | 15 | * emacsclient.c (sock_err_message): New function. |
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c index adc580e4768..d51712c41c4 100644 --- a/lib-src/emacsclient.c +++ b/lib-src/emacsclient.c | |||
| @@ -81,6 +81,13 @@ Boston, MA 02110-1301, USA. */ | |||
| 81 | char *getenv (), *getwd (); | 81 | char *getenv (), *getwd (); |
| 82 | char *(getcwd) (); | 82 | char *(getcwd) (); |
| 83 | 83 | ||
| 84 | #ifdef WINDOWSNT | ||
| 85 | char *w32_getenv (); | ||
| 86 | #define egetenv(VAR) w32_getenv(VAR) | ||
| 87 | #else | ||
| 88 | #define egetenv(VAR) getenv(VAR) | ||
| 89 | #endif | ||
| 90 | |||
| 84 | #ifndef VERSION | 91 | #ifndef VERSION |
| 85 | #define VERSION "unspecified" | 92 | #define VERSION "unspecified" |
| 86 | #endif | 93 | #endif |
| @@ -150,9 +157,111 @@ struct option longopts[] = | |||
| 150 | { 0, 0, 0, 0 } | 157 | { 0, 0, 0, 0 } |
| 151 | }; | 158 | }; |
| 152 | 159 | ||
| 160 | |||
| 161 | /* Like malloc but get fatal error if memory is exhausted. */ | ||
| 162 | |||
| 163 | long * | ||
| 164 | xmalloc (size) | ||
| 165 | unsigned int size; | ||
| 166 | { | ||
| 167 | long *result = (long *) malloc (size); | ||
| 168 | if (result == NULL) | ||
| 169 | { | ||
| 170 | perror ("malloc"); | ||
| 171 | exit (EXIT_FAILURE); | ||
| 172 | } | ||
| 173 | return result; | ||
| 174 | } | ||
| 175 | |||
| 153 | /* Message functions. */ | 176 | /* Message functions. */ |
| 154 | 177 | ||
| 155 | #ifdef WINDOWSNT | 178 | #ifdef WINDOWSNT |
| 179 | |||
| 180 | #define REG_ROOT "SOFTWARE\\GNU\\Emacs" | ||
| 181 | |||
| 182 | /* Retrieve an environment variable from the Emacs subkeys of the registry. | ||
| 183 | Return NULL if the variable was not found, or it was empty. | ||
| 184 | This code is based on w32_get_resource (w32.c). */ | ||
| 185 | char * | ||
| 186 | w32_get_resource (predefined, key, type) | ||
| 187 | HKEY predefined; | ||
| 188 | char *key; | ||
| 189 | LPDWORD type; | ||
| 190 | { | ||
| 191 | HKEY hrootkey = NULL; | ||
| 192 | char *result = NULL; | ||
| 193 | DWORD cbData; | ||
| 194 | |||
| 195 | if (RegOpenKeyEx (predefined, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS) | ||
| 196 | { | ||
| 197 | if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS) | ||
| 198 | { | ||
| 199 | result = (char *) xmalloc (cbData); | ||
| 200 | |||
| 201 | if ((RegQueryValueEx (hrootkey, key, NULL, type, result, &cbData) != ERROR_SUCCESS) || | ||
| 202 | (*result == 0)) | ||
| 203 | { | ||
| 204 | free (result); | ||
| 205 | result = NULL; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | RegCloseKey (hrootkey); | ||
| 210 | } | ||
| 211 | |||
| 212 | return result; | ||
| 213 | } | ||
| 214 | |||
| 215 | /* | ||
| 216 | getenv wrapper for Windows | ||
| 217 | |||
| 218 | This is needed to duplicate Emacs's behavior, which is to look for enviroment | ||
| 219 | variables in the registry if they don't appear in the environment. | ||
| 220 | */ | ||
| 221 | char * | ||
| 222 | w32_getenv (envvar) | ||
| 223 | char *envvar; | ||
| 224 | { | ||
| 225 | char *value; | ||
| 226 | DWORD dwType; | ||
| 227 | |||
| 228 | if (value = getenv (envvar)) | ||
| 229 | /* Found in the environment. */ | ||
| 230 | return value; | ||
| 231 | |||
| 232 | if (! (value = w32_get_resource (HKEY_CURRENT_USER, envvar, &dwType)) && | ||
| 233 | ! (value = w32_get_resource (HKEY_LOCAL_MACHINE, envvar, &dwType))) | ||
| 234 | /* Not found in the registry. */ | ||
| 235 | return NULL; | ||
| 236 | |||
| 237 | if (dwType == REG_SZ) | ||
| 238 | /* Registry; no need to expand. */ | ||
| 239 | return value; | ||
| 240 | |||
| 241 | if (dwType == REG_EXPAND_SZ) | ||
| 242 | { | ||
| 243 | DWORD size; | ||
| 244 | |||
| 245 | if (size = ExpandEnvironmentStrings (value, NULL, 0)) | ||
| 246 | { | ||
| 247 | char *buffer = (char *) xmalloc (size); | ||
| 248 | if (ExpandEnvironmentStrings (value, buffer, size)) | ||
| 249 | { | ||
| 250 | /* Found and expanded. */ | ||
| 251 | free (value); | ||
| 252 | return buffer; | ||
| 253 | } | ||
| 254 | |||
| 255 | /* Error expanding. */ | ||
| 256 | free (buffer); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | /* Not the right type, or not correctly expanded. */ | ||
| 261 | free (value); | ||
| 262 | return NULL; | ||
| 263 | } | ||
| 264 | |||
| 156 | int | 265 | int |
| 157 | w32_window_app () | 266 | w32_window_app () |
| 158 | { | 267 | { |
| @@ -208,7 +317,7 @@ decode_options (argc, argv) | |||
| 208 | int argc; | 317 | int argc; |
| 209 | char **argv; | 318 | char **argv; |
| 210 | { | 319 | { |
| 211 | alternate_editor = getenv ("ALTERNATE_EDITOR"); | 320 | alternate_editor = egetenv ("ALTERNATE_EDITOR"); |
| 212 | 321 | ||
| 213 | while (1) | 322 | while (1) |
| 214 | { | 323 | { |
| @@ -465,7 +574,7 @@ quote_file_name (s, name) | |||
| 465 | HSOCKET s; | 574 | HSOCKET s; |
| 466 | char *name; | 575 | char *name; |
| 467 | { | 576 | { |
| 468 | char *copy = (char *) malloc (strlen (name) * 2 + 1); | 577 | char *copy = (char *) xmalloc (strlen (name) * 2 + 1); |
| 469 | char *p, *q; | 578 | char *p, *q; |
| 470 | 579 | ||
| 471 | p = name; | 580 | p = name; |
| @@ -598,7 +707,7 @@ get_server_config (server, authentication) | |||
| 598 | config = fopen (server_file, "rb"); | 707 | config = fopen (server_file, "rb"); |
| 599 | else | 708 | else |
| 600 | { | 709 | { |
| 601 | char *home = getenv ("HOME"); | 710 | char *home = egetenv ("HOME"); |
| 602 | 711 | ||
| 603 | if (home) | 712 | if (home) |
| 604 | { | 713 | { |
| @@ -607,7 +716,7 @@ get_server_config (server, authentication) | |||
| 607 | config = fopen (path, "rb"); | 716 | config = fopen (path, "rb"); |
| 608 | } | 717 | } |
| 609 | #ifdef WINDOWSNT | 718 | #ifdef WINDOWSNT |
| 610 | if (!config && (home = getenv ("APPDATA"))) | 719 | if (!config && (home = egetenv ("APPDATA"))) |
| 611 | { | 720 | { |
| 612 | char *path = alloca (32 + strlen (home) + strlen (server_file)); | 721 | char *path = alloca (32 + strlen (home) + strlen (server_file)); |
| 613 | sprintf (path, "%s/.emacs.d/server/%s", home, server_file); | 722 | sprintf (path, "%s/.emacs.d/server/%s", home, server_file); |
| @@ -775,10 +884,10 @@ set_local_socket () | |||
| 775 | associated with the name. This is reminiscent of the logic | 884 | associated with the name. This is reminiscent of the logic |
| 776 | that init_editfns uses to set the global Vuser_full_name. */ | 885 | that init_editfns uses to set the global Vuser_full_name. */ |
| 777 | 886 | ||
| 778 | char *user_name = (char *) getenv ("LOGNAME"); | 887 | char *user_name = (char *) egetenv ("LOGNAME"); |
| 779 | 888 | ||
| 780 | if (!user_name) | 889 | if (!user_name) |
| 781 | user_name = (char *) getenv ("USER"); | 890 | user_name = (char *) egetenv ("USER"); |
| 782 | 891 | ||
| 783 | if (user_name) | 892 | if (user_name) |
| 784 | { | 893 | { |
| @@ -868,7 +977,7 @@ set_socket () | |||
| 868 | 977 | ||
| 869 | /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */ | 978 | /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */ |
| 870 | if (!server_file) | 979 | if (!server_file) |
| 871 | server_file = getenv ("EMACS_SERVER_FILE"); | 980 | server_file = egetenv ("EMACS_SERVER_FILE"); |
| 872 | 981 | ||
| 873 | if (server_file) | 982 | if (server_file) |
| 874 | { | 983 | { |