comparison lib-src/emacsclient.c @ 91073:4bc33ffdda1a

Merge from emacs--devo--0 Patches applied: * emacs--devo--0 (patch 902-908) - Update from CVS - Merge from emacs--rel--22 * emacs--rel--22 (patch 131-137) - Update from CVS - Merge from gnus--rel--5.10 * gnus--rel--5.10 (patch 261-262) - Update from CVS Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-278
author Miles Bader <miles@gnu.org>
date Sat, 27 Oct 2007 09:12:07 +0000
parents bdb3fe0ba9fa b210bba3f477
children 880960b70474
comparison
equal deleted inserted replaced
91072:74ab3ea909f9 91073:4bc33ffdda1a
84 84
85 85
86 char *getenv (), *getwd (); 86 char *getenv (), *getwd ();
87 char *(getcwd) (); 87 char *(getcwd) ();
88 88
89 #ifdef WINDOWSNT
90 char *w32_getenv ();
91 #define egetenv(VAR) w32_getenv(VAR)
92 #else
93 #define egetenv(VAR) getenv(VAR)
94 #endif
95
89 #ifndef VERSION 96 #ifndef VERSION
90 #define VERSION "unspecified" 97 #define VERSION "unspecified"
91 #endif 98 #endif
92 99
93 100
229 char *pwd; 236 char *pwd;
230 struct stat dotstat, pwdstat; 237 struct stat dotstat, pwdstat;
231 /* If PWD is accurate, use it instead of calling getwd. PWD is 238 /* If PWD is accurate, use it instead of calling getwd. PWD is
232 sometimes a nicer name, and using it may avoid a fatal error if a 239 sometimes a nicer name, and using it may avoid a fatal error if a
233 parent directory is searchable but not readable. */ 240 parent directory is searchable but not readable. */
234 if ((pwd = getenv ("PWD")) != 0 241 if ((pwd = egetenv ("PWD")) != 0
235 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1]))) 242 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
236 && stat (pwd, &pwdstat) == 0 243 && stat (pwd, &pwdstat) == 0
237 && stat (".", &dotstat) == 0 244 && stat (".", &dotstat) == 0
238 && dotstat.st_ino == pwdstat.st_ino 245 && dotstat.st_ino == pwdstat.st_ino
239 && dotstat.st_dev == pwdstat.st_dev 246 && dotstat.st_dev == pwdstat.st_dev
292 #endif 299 #endif
293 300
294 /* Message functions. */ 301 /* Message functions. */
295 302
296 #ifdef WINDOWSNT 303 #ifdef WINDOWSNT
304
305 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
306
307 /* Retrieve an environment variable from the Emacs subkeys of the registry.
308 Return NULL if the variable was not found, or it was empty.
309 This code is based on w32_get_resource (w32.c). */
310 char *
311 w32_get_resource (predefined, key, type)
312 HKEY predefined;
313 char *key;
314 LPDWORD type;
315 {
316 HKEY hrootkey = NULL;
317 char *result = NULL;
318 DWORD cbData;
319
320 if (RegOpenKeyEx (predefined, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
321 {
322 if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS)
323 {
324 result = (char *) xmalloc (cbData);
325
326 if ((RegQueryValueEx (hrootkey, key, NULL, type, result, &cbData) != ERROR_SUCCESS) ||
327 (*result == 0))
328 {
329 free (result);
330 result = NULL;
331 }
332 }
333
334 RegCloseKey (hrootkey);
335 }
336
337 return result;
338 }
339
340 /*
341 getenv wrapper for Windows
342
343 This is needed to duplicate Emacs's behavior, which is to look for enviroment
344 variables in the registry if they don't appear in the environment.
345 */
346 char *
347 w32_getenv (envvar)
348 char *envvar;
349 {
350 char *value;
351 DWORD dwType;
352
353 if (value = getenv (envvar))
354 /* Found in the environment. */
355 return value;
356
357 if (! (value = w32_get_resource (HKEY_CURRENT_USER, envvar, &dwType)) &&
358 ! (value = w32_get_resource (HKEY_LOCAL_MACHINE, envvar, &dwType)))
359 /* Not found in the registry. */
360 return NULL;
361
362 if (dwType == REG_SZ)
363 /* Registry; no need to expand. */
364 return value;
365
366 if (dwType == REG_EXPAND_SZ)
367 {
368 DWORD size;
369
370 if (size = ExpandEnvironmentStrings (value, NULL, 0))
371 {
372 char *buffer = (char *) xmalloc (size);
373 if (ExpandEnvironmentStrings (value, buffer, size))
374 {
375 /* Found and expanded. */
376 free (value);
377 return buffer;
378 }
379
380 /* Error expanding. */
381 free (buffer);
382 }
383 }
384
385 /* Not the right type, or not correctly expanded. */
386 free (value);
387 return NULL;
388 }
389
297 int 390 int
298 w32_window_app () 391 w32_window_app ()
299 { 392 {
300 static int window_app = -1; 393 static int window_app = -1;
301 char szTitle[MAX_PATH]; 394 char szTitle[MAX_PATH];
381 void 474 void
382 decode_options (argc, argv) 475 decode_options (argc, argv)
383 int argc; 476 int argc;
384 char **argv; 477 char **argv;
385 { 478 {
386 alternate_editor = getenv ("ALTERNATE_EDITOR"); 479 alternate_editor = egetenv ("ALTERNATE_EDITOR");
387 display = getenv ("DISPLAY"); 480 display = egetenv ("DISPLAY");
388 if (display && strlen (display) == 0) 481 if (display && strlen (display) == 0)
389 display = NULL; 482 display = NULL;
390 483
391 while (1) 484 while (1)
392 { 485 {
571 char send_buffer[SEND_BUFFER_SIZE + 1]; 664 char send_buffer[SEND_BUFFER_SIZE + 1];
572 int sblen = 0; /* Fill pointer for the send buffer. */ 665 int sblen = 0; /* Fill pointer for the send buffer. */
573 /* Socket used to communicate with the Emacs server process. */ 666 /* Socket used to communicate with the Emacs server process. */
574 HSOCKET emacs_socket = 0; 667 HSOCKET emacs_socket = 0;
575 668
669 /* On Windows, the socket library was historically separate from the standard
670 C library, so errors are handled differently. */
671 void
672 sock_err_message (function_name)
673 char *function_name;
674 {
675 #ifdef WINDOWSNT
676 char* msg = NULL;
677
678 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
679 | FORMAT_MESSAGE_ALLOCATE_BUFFER
680 | FORMAT_MESSAGE_ARGUMENT_ARRAY,
681 NULL, WSAGetLastError (), 0, (LPTSTR)&msg, 0, NULL);
682
683 message (TRUE, "%s: %s: %s\n", progname, function_name, msg);
684
685 LocalFree (msg);
686 #else
687 message (TRUE, "%s: %s: %s\n", progname, function_name, strerror (errno));
688 #endif
689 }
690
691
576 /* Let's send the data to Emacs when either 692 /* Let's send the data to Emacs when either
577 - the data ends in "\n", or 693 - the data ends in "\n", or
578 - the buffer is full (but this shouldn't happen) 694 - the buffer is full (but this shouldn't happen)
579 Otherwise, we just accumulate it. */ 695 Otherwise, we just accumulate it. */
580 void 696 void
791 907
792 if (file_name_absolute_p (server_file)) 908 if (file_name_absolute_p (server_file))
793 config = fopen (server_file, "rb"); 909 config = fopen (server_file, "rb");
794 else 910 else
795 { 911 {
796 char *home = getenv ("HOME"); 912 char *home = egetenv ("HOME");
797 913
798 if (home) 914 if (home)
799 { 915 {
800 char *path = alloca (32 + strlen (home) + strlen (server_file)); 916 char *path = alloca (32 + strlen (home) + strlen (server_file));
801 sprintf (path, "%s/.emacs.d/server/%s", home, server_file); 917 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
802 config = fopen (path, "rb"); 918 config = fopen (path, "rb");
803 } 919 }
804 #ifdef WINDOWSNT 920 #ifdef WINDOWSNT
805 if (!config && (home = getenv ("APPDATA"))) 921 if (!config && (home = egetenv ("APPDATA")))
806 { 922 {
807 char *path = alloca (32 + strlen (home) + strlen (server_file)); 923 char *path = alloca (32 + strlen (home) + strlen (server_file));
808 sprintf (path, "%s/.emacs.d/server/%s", home, server_file); 924 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
809 config = fopen (path, "rb"); 925 config = fopen (path, "rb");
810 } 926 }
862 /* 978 /*
863 * Open up an AF_INET socket 979 * Open up an AF_INET socket
864 */ 980 */
865 if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) 981 if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
866 { 982 {
867 message (TRUE, "%s: socket: %s\n", progname, strerror (errno)); 983 sock_err_message ("socket");
868 return INVALID_SOCKET; 984 return INVALID_SOCKET;
869 } 985 }
870 986
871 /* 987 /*
872 * Set up the socket 988 * Set up the socket
873 */ 989 */
874 if (connect (s, (struct sockaddr *) &server, sizeof server) < 0) 990 if (connect (s, (struct sockaddr *) &server, sizeof server) < 0)
875 { 991 {
876 message (TRUE, "%s: connect: %s\n", progname, strerror (errno)); 992 sock_err_message ("connect");
877 return INVALID_SOCKET; 993 return INVALID_SOCKET;
878 } 994 }
879 995
880 setsockopt (s, SOL_SOCKET, SO_LINGER, (char *) &l_arg, sizeof l_arg); 996 setsockopt (s, SOL_SOCKET, SO_LINGER, (char *) &l_arg, sizeof l_arg);
881 997
1064 /* Failing that, see if LOGNAME or USER exist and differ from 1180 /* Failing that, see if LOGNAME or USER exist and differ from
1065 our euid. If so, look for a socket based on the UID 1181 our euid. If so, look for a socket based on the UID
1066 associated with the name. This is reminiscent of the logic 1182 associated with the name. This is reminiscent of the logic
1067 that init_editfns uses to set the global Vuser_full_name. */ 1183 that init_editfns uses to set the global Vuser_full_name. */
1068 1184
1069 char *user_name = (char *) getenv ("LOGNAME"); 1185 char *user_name = (char *) egetenv ("LOGNAME");
1070 1186
1071 if (!user_name) 1187 if (!user_name)
1072 user_name = (char *) getenv ("USER"); 1188 user_name = (char *) egetenv ("USER");
1073 1189
1074 if (user_name) 1190 if (user_name)
1075 { 1191 {
1076 struct passwd *pw = getpwnam (user_name); 1192 struct passwd *pw = getpwnam (user_name);
1077 1193
1156 } 1272 }
1157 #endif 1273 #endif
1158 1274
1159 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */ 1275 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */
1160 if (!server_file) 1276 if (!server_file)
1161 server_file = getenv ("EMACS_SERVER_FILE"); 1277 server_file = egetenv ("EMACS_SERVER_FILE");
1162 1278
1163 if (server_file) 1279 if (server_file)
1164 { 1280 {
1165 s = set_tcp_socket (); 1281 s = set_tcp_socket ();
1166 if ((s != INVALID_SOCKET) || alternate_editor) 1282 if ((s != INVALID_SOCKET) || alternate_editor)
1329 send_to_emacs (emacs_socket, " "); 1445 send_to_emacs (emacs_socket, " ");
1330 } 1446 }
1331 1447
1332 if (tty) 1448 if (tty)
1333 { 1449 {
1334 char *type = getenv ("TERM"); 1450 char *type = egetenv ("TERM");
1335 char *tty_name = NULL; 1451 char *tty_name = NULL;
1336 #ifndef WINDOWSNT 1452 #ifndef WINDOWSNT
1337 tty_name = ttyname (fileno (stdin)); 1453 tty_name = ttyname (fileno (stdin));
1338 #endif 1454 #endif
1339 1455