annotate lib-src/emacsclient.c @ 73806:fbfa7d8c0a64

(get_server_config): Extract also the Emacs pid from the server file. On Windows, try to force the Emacs frame to the foreground.
author Juanma Barranquero <lekktu@gmail.com>
date Tue, 07 Nov 2006 10:43:45 +0000
parents bcb5c0f9a466
children cbc1f46b6590
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
493
8b101799ff37 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 478
diff changeset
1 /* Client process that communicates with GNU Emacs acting as server.
64769
6358e3c6075c Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 64083
diff changeset
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2002, 2003, 2004,
68647
3661e9b3c48f Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 64769
diff changeset
3 2005, 2006 Free Software Foundation, Inc.
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 This file is part of GNU Emacs.
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 GNU Emacs is free software; you can redistribute it and/or modify
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 it under the terms of the GNU General Public License as published by
5522
64a936b21f74 Don't declare sys_errlist; declare strerror instead.
Roland McGrath <roland@gnu.org>
parents: 4696
diff changeset
9 the Free Software Foundation; either version 2, or (at your option)
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 any later version.
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 GNU Emacs is distributed in the hope that it will be useful,
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 GNU General Public License for more details.
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 along with GNU Emacs; see the file COPYING. If not, write to
64083
23a17af379b1 Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 60718
diff changeset
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23a17af379b1 Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 60718
diff changeset
20 Boston, MA 02110-1301, USA. */
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 #define NO_SHORTNAMES
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
24
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
25 #ifdef HAVE_CONFIG_H
42412
83c1951257f1 Include <config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 42176
diff changeset
26 #include <config.h>
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
27 #endif
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
28
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
29 #ifdef WINDOWSNT
73585
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
30
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
31 # include <malloc.h>
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
32 # include <stdlib.h>
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
33
73585
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
34 # define HAVE_SOCKETS
73617
7af864af10b6 [WINDOWSNT]: Define HAVE_INET_SOCKETS.
Juanma Barranquero <lekktu@gmail.com>
parents: 73601
diff changeset
35 # define HAVE_INET_SOCKETS
73585
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
36 # define NO_SOCKETS_IN_FILE_SYSTEM
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
37
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
38 # define HSOCKET SOCKET
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
39 # define CLOSE_SOCKET closesocket
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
40 # define INITIALIZE() (initialize_sockets ())
73585
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
41
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
42 #else /* !WINDOWSNT */
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
43
73617
7af864af10b6 [WINDOWSNT]: Define HAVE_INET_SOCKETS.
Juanma Barranquero <lekktu@gmail.com>
parents: 73601
diff changeset
44 # ifdef HAVE_INET_SOCKETS
7af864af10b6 [WINDOWSNT]: Define HAVE_INET_SOCKETS.
Juanma Barranquero <lekktu@gmail.com>
parents: 73601
diff changeset
45 # include <netinet/in.h>
7af864af10b6 [WINDOWSNT]: Define HAVE_INET_SOCKETS.
Juanma Barranquero <lekktu@gmail.com>
parents: 73601
diff changeset
46 # endif
73585
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
47
73578
e193b6a3f379 Fixes to emacsclient.c for GNU/Linux.
Juanma Barranquero <lekktu@gmail.com>
parents: 73562
diff changeset
48 # define INVALID_SOCKET -1
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
49 # define HSOCKET int
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
50 # define CLOSE_SOCKET close
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
51 # define INITIALIZE()
73585
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
52
7d264c036b25 [WINDOWSNT]: Include <malloc.h> and <stdlib.h>.
Juanma Barranquero <lekktu@gmail.com>
parents: 73583
diff changeset
53 #endif /* !WINDOWSNT */
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
54
3595
e10f7473d2e3 * emacsserver.c (main): When we're passing a `struct sockaddr_un'
Jim Blandy <jimb@redhat.com>
parents: 1031
diff changeset
55 #undef signal
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
57 #include <ctype.h>
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
58 #include <stdio.h>
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
59 #include "getopt.h"
21431
a080769e8f7d Move inclusion of unistd.h to top, else fails on
Dave Love <fx@gnu.org>
parents: 21387
diff changeset
60 #ifdef HAVE_UNISTD_H
a080769e8f7d Move inclusion of unistd.h to top, else fails on
Dave Love <fx@gnu.org>
parents: 21387
diff changeset
61 #include <unistd.h>
a080769e8f7d Move inclusion of unistd.h to top, else fails on
Dave Love <fx@gnu.org>
parents: 21387
diff changeset
62 #endif
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
64 #ifdef VMS
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
65 # include "vms-pwd.h"
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
66 #else /* not VMS */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
67 #ifdef WINDOWSNT
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
68 # include <io.h>
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
69 #else /* not WINDOWSNT */
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
70 # include <pwd.h>
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
71 #endif /* not WINDOWSNT */
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
72 #endif /* not VMS */
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
73
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
74 char *getenv (), *getwd ();
60718
96023ae0af92 * emacsclient.c: Avoid expansion of getcwd when defined as a macro.
Jan Djärv <jan.h.d@swipnet.se>
parents: 56446
diff changeset
75 char *(getcwd) ();
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
76
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
77 #ifndef VERSION
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
78 #define VERSION "unspecified"
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
79 #endif
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
80
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
81 #define SEND_STRING(data) (send_to_emacs (s, (data)))
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
82 #define SEND_QUOTED(data) (quote_file_name (s, (data)))
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
83
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
84 #ifndef EXIT_SUCCESS
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
85 #define EXIT_SUCCESS 0
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
86 #endif
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
87
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
88 #ifndef EXIT_FAILURE
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
89 #define EXIT_FAILURE 1
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
90 #endif
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
91
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
92 #ifndef FALSE
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
93 #define FALSE 0
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
94 #endif
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
95
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
96 #ifndef TRUE
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
97 #define TRUE 1
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
98 #endif
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
99
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
100 #ifndef NO_RETURN
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
101 #define NO_RETURN
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
102 #endif
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
103
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
104 /* Name used to invoke this program. */
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
105 char *progname;
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
106
16146
a649abfefbb1 (longopts): Change nowait to no-wait.
Richard M. Stallman <rms@gnu.org>
parents: 16074
diff changeset
107 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
108 int nowait = 0;
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
109
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
110 /* Nonzero means args are expressions to be evaluated. --eval. */
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
111 int eval = 0;
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
112
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
113 /* The display on which Emacs should work. --display. */
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
114 char *display = NULL;
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
115
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
116 /* If non-NULL, the name of an editor to fallback to if the server
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
117 is not running. --alternate-editor. */
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
118 const char *alternate_editor = NULL;
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
119
52532
cd53bfef4ab1 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 52479
diff changeset
120 /* If non-NULL, the filename of the UNIX socket. */
52479
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
121 char *socket_name = NULL;
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
122
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
123 /* If non-NULL, the filename of the authentication file. */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
124 char *server_file = NULL;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
125
72005
da0099bc0ba4 * ebrowse.c (usage, version): Mark as NO_RETURN.
Dan Nicolaescu <dann@ics.uci.edu>
parents: 68647
diff changeset
126 void print_help_and_exit () NO_RETURN;
21387
4812cf228971 Include <stdlib.h> and <unistd.h> if available.
Andreas Schwab <schwab@suse.de>
parents: 19582
diff changeset
127
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
128 struct option longopts[] =
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
129 {
16146
a649abfefbb1 (longopts): Change nowait to no-wait.
Richard M. Stallman <rms@gnu.org>
parents: 16074
diff changeset
130 { "no-wait", no_argument, NULL, 'n' },
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
131 { "eval", no_argument, NULL, 'e' },
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
132 { "help", no_argument, NULL, 'H' },
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
133 { "version", no_argument, NULL, 'V' },
47515
6ee937dcfe16 (quote_file_name): Quote \n.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42412
diff changeset
134 { "alternate-editor", required_argument, NULL, 'a' },
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
135 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
52479
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
136 { "socket-name", required_argument, NULL, 's' },
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
137 #endif
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
138 { "server-file", required_argument, NULL, 'f' },
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
139 { "display", required_argument, NULL, 'd' },
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
140 { 0, 0, 0, 0 }
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
141 };
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
142
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
143 /* Decode the options from argv and argc.
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
144 The global variable `optind' will say how many arguments we used up. */
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
145
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
146 void
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
147 decode_options (argc, argv)
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
148 int argc;
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
149 char **argv;
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
150 {
53813
ddc4b99f4f8c (decode_options): Fix handling of alternate editor.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53734
diff changeset
151 alternate_editor = getenv ("ALTERNATE_EDITOR");
ddc4b99f4f8c (decode_options): Fix handling of alternate editor.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53734
diff changeset
152
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
153 while (1)
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
154 {
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
155 int opt = getopt_long (argc, argv,
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
156 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
157 "VHnea:s:f:d:",
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
158 #else
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
159 "VHnea:f:d:",
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
160 #endif
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
161 longopts, 0);
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
162
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
163 if (opt == EOF)
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
164 break;
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
165
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
166 switch (opt)
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
167 {
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
168 case 0:
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
169 /* If getopt returns 0, then it has already processed a
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
170 long-named option. We should do nothing. */
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
171 break;
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
172
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
173 case 'a':
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
174 alternate_editor = optarg;
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
175 break;
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
176
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
177 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
52479
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
178 case 's':
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
179 socket_name = optarg;
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
180 break;
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
181 #endif
52479
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
182
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
183 case 'f':
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
184 server_file = optarg;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
185 break;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
186
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
187 case 'd':
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
188 display = optarg;
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
189 break;
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
190
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
191 case 'n':
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
192 nowait = 1;
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
193 break;
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
194
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
195 case 'e':
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
196 eval = 1;
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
197 break;
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
198
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
199 case 'V':
50101
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
200 printf ("emacsclient %s\n", VERSION);
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
201 exit (EXIT_SUCCESS);
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
202 break;
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
203
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
204 case 'H':
50101
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
205 print_help_and_exit ();
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
206 break;
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
207
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
208 default:
50101
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
209 fprintf (stderr, "Try `%s --help' for more information\n", progname);
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
210 exit (EXIT_FAILURE);
50101
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
211 break;
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
212 }
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
213 }
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
214 }
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
215
21387
4812cf228971 Include <stdlib.h> and <unistd.h> if available.
Andreas Schwab <schwab@suse.de>
parents: 19582
diff changeset
216 void
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
217 print_help_and_exit ()
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
218 {
50101
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
219 printf (
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
220 "Usage: %s [OPTIONS] FILE...\n\
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
221 Tell the Emacs server to visit the specified files.\n\
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
222 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
50101
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
223 \n\
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
224 The following OPTIONS are accepted:\n\
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
225 -V, --version Just print a version info and return\n\
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
226 -H, --help Print this usage information message\n\
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
227 -n, --no-wait Don't wait for the server to return\n\
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
228 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
229 -d, --display=DISPLAY Visit the file in the given display\n"
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
230 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
231 "-s, --socket-name=FILENAME\n\
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
232 Set the filename of the UNIX socket for communication\n"
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
233 #endif
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
234 "-f, --server-file=FILENAME\n\
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
235 Set the filename of the TCP configuration file\n\
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
236 -a, --alternate-editor=EDITOR\n\
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
237 Editor to fallback to if the server is not running\n\
50101
e5f56d47b52c (print_help_and_exit): Print to stdout. Exit successfully. Added some blank
Juanma Barranquero <lekktu@gmail.com>
parents: 48627
diff changeset
238 \n\
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
239 Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
240 exit (EXIT_SUCCESS);
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
241 }
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
242
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
243
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
244 /*
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
245 Try to run a different command, or --if no alternate editor is
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
246 defined-- exit with an errorcode.
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
247 */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
248 void
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
249 fail (argc, argv)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
250 int argc;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
251 char **argv;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
252 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
253 if (alternate_editor)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
254 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
255 int i = optind - 1;
73601
e243e9889b8b [WINDOWSNT]: Force the first argv passed to execvp to point to alternate_editor
Juanma Barranquero <lekktu@gmail.com>
parents: 73585
diff changeset
256 #ifdef WINDOWSNT
e243e9889b8b [WINDOWSNT]: Force the first argv passed to execvp to point to alternate_editor
Juanma Barranquero <lekktu@gmail.com>
parents: 73585
diff changeset
257 argv[i] = (char *)alternate_editor;
e243e9889b8b [WINDOWSNT]: Force the first argv passed to execvp to point to alternate_editor
Juanma Barranquero <lekktu@gmail.com>
parents: 73585
diff changeset
258 #endif
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
259 execvp (alternate_editor, argv + i);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
260 fprintf (stderr, "%s: error executing alternate editor \"%s\"\n",
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
261 progname, alternate_editor);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
262 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
263 exit (EXIT_FAILURE);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
264 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
265
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
266
73617
7af864af10b6 [WINDOWSNT]: Define HAVE_INET_SOCKETS.
Juanma Barranquero <lekktu@gmail.com>
parents: 73601
diff changeset
267 #if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS)
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
268
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
269 int
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
270 main (argc, argv)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
271 int argc;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
272 char **argv;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
273 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
274 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
275 argv[0]);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
276 fprintf (stderr, "on systems with Berkeley sockets.\n");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
277
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
278 fail (argc, argv);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
279 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
280
73617
7af864af10b6 [WINDOWSNT]: Define HAVE_INET_SOCKETS.
Juanma Barranquero <lekktu@gmail.com>
parents: 73601
diff changeset
281 #else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
282
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
283 #ifdef WINDOWSNT
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
284 # include <winsock2.h>
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
285 #else
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
286 # include <sys/types.h>
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
287 # include <sys/socket.h>
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
288 # include <sys/un.h>
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
289 # include <sys/stat.h>
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
290 # include <errno.h>
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
291 #endif
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
292
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
293 #define AUTH_KEY_LENGTH 64
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
294 #define SEND_BUFFER_SIZE 4096
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
295
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
296 extern char *strerror ();
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
297 extern int errno;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
298
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
299 /* Buffer to accumulate data to send in TCP connections. */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
300 char send_buffer[SEND_BUFFER_SIZE + 1];
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
301 int sblen = 0; /* Fill pointer for the send buffer. */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
302
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
303 /* Let's send the data to Emacs when either
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
304 - the data ends in "\n", or
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
305 - the buffer is full (but this shouldn't happen)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
306 Otherwise, we just accumulate it. */
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
307 void
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
308 send_to_emacs (s, data)
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
309 HSOCKET s;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
310 char *data;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
311 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
312 while (data)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
313 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
314 int dlen = strlen (data);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
315 if (dlen + sblen >= SEND_BUFFER_SIZE)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
316 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
317 int part = SEND_BUFFER_SIZE - sblen;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
318 strncpy (&send_buffer[sblen], data, part);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
319 data += part;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
320 sblen = SEND_BUFFER_SIZE;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
321 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
322 else if (dlen)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
323 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
324 strcpy (&send_buffer[sblen], data);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
325 data = NULL;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
326 sblen += dlen;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
327 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
328 else
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
329 break;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
330
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
331 if (sblen == SEND_BUFFER_SIZE
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
332 || (sblen > 0 && send_buffer[sblen-1] == '\n'))
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
333 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
334 int sent = send (s, send_buffer, sblen, 0);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
335 if (sent != sblen)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
336 strcpy (send_buffer, &send_buffer[sent]);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
337 sblen -= sent;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
338 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
339 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
340 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
341
52848
5c74e66d6c36 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 52635
diff changeset
342 /* In NAME, insert a & before each &, each space, each newline, and
5c74e66d6c36 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 52635
diff changeset
343 any initial -. Change spaces to underscores, too, so that the
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
344 return value never contains a space. */
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
345 void
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
346 quote_file_name (s, name)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
347 HSOCKET s;
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
348 char *name;
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
349 {
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
350 char *copy = (char *) malloc (strlen (name) * 2 + 1);
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
351 char *p, *q;
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
352
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
353 p = name;
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
354 q = copy;
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
355 while (*p)
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
356 {
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
357 if (*p == ' ')
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
358 {
16074
25cfff7ffced (quote_file_name): Quote with &, not \.
Richard M. Stallman <rms@gnu.org>
parents: 16061
diff changeset
359 *q++ = '&';
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
360 *q++ = '_';
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
361 p++;
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
362 }
47515
6ee937dcfe16 (quote_file_name): Quote \n.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42412
diff changeset
363 else if (*p == '\n')
6ee937dcfe16 (quote_file_name): Quote \n.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42412
diff changeset
364 {
6ee937dcfe16 (quote_file_name): Quote \n.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42412
diff changeset
365 *q++ = '&';
6ee937dcfe16 (quote_file_name): Quote \n.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42412
diff changeset
366 *q++ = 'n';
6ee937dcfe16 (quote_file_name): Quote \n.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42412
diff changeset
367 p++;
6ee937dcfe16 (quote_file_name): Quote \n.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42412
diff changeset
368 }
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
369 else
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
370 {
16074
25cfff7ffced (quote_file_name): Quote with &, not \.
Richard M. Stallman <rms@gnu.org>
parents: 16061
diff changeset
371 if (*p == '&' || (*p == '-' && p == name))
25cfff7ffced (quote_file_name): Quote with &, not \.
Richard M. Stallman <rms@gnu.org>
parents: 16061
diff changeset
372 *q++ = '&';
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
373 *q++ = *p++;
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
374 }
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
375 }
16074
25cfff7ffced (quote_file_name): Quote with &, not \.
Richard M. Stallman <rms@gnu.org>
parents: 16061
diff changeset
376 *q++ = 0;
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
377
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
378 SEND_STRING (copy);
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
379
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
380 free (copy);
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
381 }
18701
9c7e0c926ed5 [C_ALLOCA] (xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16218
diff changeset
382
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
383 int
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
384 file_name_absolute_p (filename)
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
385 const unsigned char *filename;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
386 {
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
387 /* Sanity check, it shouldn't happen. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
388 if (! filename) return FALSE;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
389
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
390 /* /xxx is always an absolute path. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
391 if (filename[0] == '/') return TRUE;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
392
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
393 /* Empty filenames (which shouldn't happen) are relative. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
394 if (filename[0] == '\0') return FALSE;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
395
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
396 #ifdef WINDOWSNT
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
397 /* X:\xxx is always absolute; X:xxx is an error and will fail. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
398 if (islower (tolower (filename[0]))
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
399 && filename[1] == ':' && filename[2] == '\\')
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
400 return TRUE;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
401
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
402 /* Both \xxx and \\xxx\yyy are absolute. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
403 if (filename[0] == '\\') return TRUE;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
404 #endif
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
405
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
406 return FALSE;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
407 }
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
408
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
409 #ifdef WINDOWSNT
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
410 /* Wrapper to make WSACleanup a cdecl, as required by atexit(). */
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
411 void
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
412 __cdecl close_winsock ()
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
413 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
414 WSACleanup ();
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
415 }
18701
9c7e0c926ed5 [C_ALLOCA] (xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16218
diff changeset
416
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
417 /* Initialize the WinSock2 library. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
418 void
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
419 initialize_sockets ()
18701
9c7e0c926ed5 [C_ALLOCA] (xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16218
diff changeset
420 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
421 WSADATA wsaData;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
422
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
423 if (WSAStartup (MAKEWORD (2, 0), &wsaData))
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
424 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
425 fprintf (stderr, "%s: error initializing WinSock2", progname);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
426 exit (EXIT_FAILURE);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
427 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
428
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
429 atexit (close_winsock);
73578
e193b6a3f379 Fixes to emacsclient.c for GNU/Linux.
Juanma Barranquero <lekktu@gmail.com>
parents: 73562
diff changeset
430 }
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
431 #endif /* WINDOWSNT */
16030
fb38057473c8 (decode_options): New function.
Richard M. Stallman <rms@gnu.org>
parents: 15995
diff changeset
432
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
433 /*
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
434 * Read the information needed to set up a TCP comm channel with
73806
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
435 * the Emacs server: host, port, pid and authentication string.
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
436 */
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
437 int
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
438 get_server_config (server, authentication)
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
439 struct sockaddr_in *server;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
440 char *authentication;
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
441 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
442 char dotted[32];
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
443 char *port;
73806
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
444 char *pid;
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
445 FILE *config = NULL;
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
446
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
447 if (file_name_absolute_p (server_file))
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
448 config = fopen (server_file, "rb");
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
449 else
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
450 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
451 char *home = getenv ("HOME");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
452 #ifdef WINDOWSNT
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
453 if (! home)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
454 home = getenv ("APPDATA");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
455 #endif
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
456 if (home)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
457 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
458 char *path = alloca (32 + strlen (home) + strlen (server_file));
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
459 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
460 config = fopen (path, "rb");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
461 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
462 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
463
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
464 if (! config)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
465 return FALSE;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
466
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
467 if (fgets (dotted, sizeof dotted, config)
73806
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
468 && (port = strchr (dotted, ':'))
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
469 && (pid = strchr (port, ' ')))
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
470 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
471 *port++ = '\0';
73806
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
472 *pid++ = '\0';
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
473 }
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
474 else
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
475 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
476 fprintf (stderr, "%s: invalid configuration info", progname);
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
477 exit (EXIT_FAILURE);
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
478 }
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
479
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
480 server->sin_family = AF_INET;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
481 server->sin_addr.s_addr = inet_addr (dotted);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
482 server->sin_port = htons (atoi (port));
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
483
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
484 if (! fread (authentication, AUTH_KEY_LENGTH, 1, config))
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
485 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
486 fprintf (stderr, "%s: cannot read authentication info", progname);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
487 exit (EXIT_FAILURE);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
488 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
489
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
490 fclose (config);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
491
73806
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
492 #ifdef WINDOWSNT
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
493 /*
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
494 Modern Windows restrict which processes can set the foreground window.
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
495 So, for emacsclient to be able to force Emacs into the foreground, we
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
496 have to call AllowSetForegroundWindow(). Unfortunately, older Windows
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
497 (W95, W98 and NT) don't have this function, so we have to check first.
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
498
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
499 We're doing this here because it has to be done before sending info
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
500 to Emacs, and otherwise we'll need a global variable just to pass around
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
501 the pid, which is also inelegant.
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
502 */
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
503 {
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
504 HMODULE hUser32;
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
505
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
506 if (hUser32 = LoadLibrary ("user32.dll"))
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
507 {
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
508 void (*set_fg)(DWORD);
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
509 if (set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow"))
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
510 set_fg (atoi (pid));
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
511 FreeLibrary (hUser32);
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
512 }
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
513 }
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
514 #endif
fbfa7d8c0a64 (get_server_config): Extract also the Emacs pid from the server file.
Juanma Barranquero <lekktu@gmail.com>
parents: 73765
diff changeset
515
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
516 return TRUE;
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
517 }
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
518
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
519 HSOCKET
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
520 set_tcp_socket ()
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
521 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
522 HSOCKET s;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
523 struct sockaddr_in server;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
524 struct linger l_arg = {1, 1};
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
525 char auth_string[AUTH_KEY_LENGTH + 1];
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
526
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
527 if (! get_server_config (&server, auth_string))
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
528 return INVALID_SOCKET;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
529
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
530 if (server.sin_addr.s_addr != inet_addr ("127.0.0.1"))
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
531 fprintf (stderr, "%s: connected to remote socket at %s\n",
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
532 progname, inet_ntoa (server.sin_addr));
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
533
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
534 /*
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
535 * Open up an AF_INET socket
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
536 */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
537 if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
538 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
539 fprintf (stderr, "%s: ", progname);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
540 perror ("socket");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
541 return INVALID_SOCKET;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
542 }
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
543
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
544 /*
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
545 * Set up the socket
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
546 */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
547 if (connect (s, (struct sockaddr *) &server, sizeof server) < 0)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
548 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
549 fprintf (stderr, "%s: ", progname);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
550 perror ("connect");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
551 return INVALID_SOCKET;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
552 }
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
553
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
554 setsockopt (s, SOL_SOCKET, SO_LINGER, (char *) &l_arg, sizeof l_arg);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
555
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
556 /*
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
557 * Send the authentication
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
558 */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
559 auth_string[AUTH_KEY_LENGTH] = '\0';
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
560
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
561 SEND_STRING ("-auth ");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
562 SEND_STRING (auth_string);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
563 SEND_STRING ("\n");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
564
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
565 return s;
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
566 }
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
567
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
568 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
569
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
570 /* Three possibilities:
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
571 2 - can't be `stat'ed (sets errno)
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
572 1 - isn't owned by us
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
573 0 - success: none of the above */
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
574
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
575 static int
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
576 socket_status (socket_name)
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
577 char *socket_name;
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
578 {
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
579 struct stat statbfr;
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
580
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
581 if (stat (socket_name, &statbfr) == -1)
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
582 return 2;
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
583
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
584 if (statbfr.st_uid != geteuid ())
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
585 return 1;
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
586
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
587 return 0;
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
588 }
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
589
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
590 HSOCKET
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
591 set_local_socket ()
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
592 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
593 HSOCKET s;
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
594 struct sockaddr_un server;
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
595
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
596 /*
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
597 * Open up an AF_UNIX socket in this person's home directory
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 */
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
600 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
601 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
602 fprintf (stderr, "%s: ", progname);
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
603 perror ("socket");
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
604 return INVALID_SOCKET;
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
605 }
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
606
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
607 server.sun_family = AF_UNIX;
25261
89be69860eeb (main): Move the dynamic allocation of
Karl Heuer <kwzh@gnu.org>
parents: 24652
diff changeset
608
493
8b101799ff37 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 478
diff changeset
609 {
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
610 int sock_status = 0;
53646
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
611 int default_sock = !socket_name;
53543
fec82b542db2 (main): Save errno from socket_status.
Andreas Schwab <schwab@suse.de>
parents: 53497
diff changeset
612 int saved_errno;
53734
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
613 char *server_name = "server";
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
614
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
615 if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\'))
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
616 { /* socket_name is a file name component. */
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
617 server_name = socket_name;
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
618 socket_name = NULL;
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
619 default_sock = 1; /* Try both UIDs. */
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
620 }
24084
c0b0893dd5a6 (main): Eliminate arbitrary limit on length of system_name.
Richard M. Stallman <rms@gnu.org>
parents: 23412
diff changeset
621
53646
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
622 if (default_sock)
52479
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
623 {
53734
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
624 socket_name = alloca (100 + strlen (server_name));
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
625 sprintf (socket_name, "/tmp/emacs%d/%s",
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
626 (int) geteuid (), server_name);
52479
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
627 }
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
628
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
629 if (strlen (socket_name) < sizeof (server.sun_path))
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
630 strcpy (server.sun_path, socket_name);
bc2610a13fbf (socket_name): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 52401
diff changeset
631 else
53646
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
632 {
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
633 fprintf (stderr, "%s: socket-name %s too long",
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
634 progname, socket_name);
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
635 exit (EXIT_FAILURE);
53646
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
636 }
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
637
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
638 /* See if the socket exists, and if it's owned by us. */
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
639 sock_status = socket_status (server.sun_path);
53543
fec82b542db2 (main): Save errno from socket_status.
Andreas Schwab <schwab@suse.de>
parents: 53497
diff changeset
640 saved_errno = errno;
53646
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
641 if (sock_status && default_sock)
493
8b101799ff37 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 478
diff changeset
642 {
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
643 /* Failing that, see if LOGNAME or USER exist and differ from
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
644 our euid. If so, look for a socket based on the UID
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
645 associated with the name. This is reminiscent of the logic
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
646 that init_editfns uses to set the global Vuser_full_name. */
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
647
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
648 char *user_name = (char *) getenv ("LOGNAME");
53693
04e9ebd16fcd (main): Restore errno from saved_errno,
Richard M. Stallman <rms@gnu.org>
parents: 53646
diff changeset
649
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
650 if (!user_name)
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
651 user_name = (char *) getenv ("USER");
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
652
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
653 if (user_name)
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
654 {
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
655 struct passwd *pw = getpwnam (user_name);
53693
04e9ebd16fcd (main): Restore errno from saved_errno,
Richard M. Stallman <rms@gnu.org>
parents: 53646
diff changeset
656
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
657 if (pw && (pw->pw_uid != geteuid ()))
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
658 {
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
659 /* We're running under su, apparently. */
53734
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
660 socket_name = alloca (100 + strlen (server_name));
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
661 sprintf (socket_name, "/tmp/emacs%d/%s",
132739917566 (main): Don't use the hostname in the socket name.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53693
diff changeset
662 (int) pw->pw_uid, server_name);
53646
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
663
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
664 if (strlen (socket_name) < sizeof (server.sun_path))
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
665 strcpy (server.sun_path, socket_name);
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
666 else
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
667 {
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
668 fprintf (stderr, "%s: socket-name %s too long",
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
669 progname, socket_name);
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
670 exit (EXIT_FAILURE);
53646
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
671 }
8c55b1b400d5 (main): Stop if socket name too long.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 53543
diff changeset
672
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
673 sock_status = socket_status (server.sun_path);
53543
fec82b542db2 (main): Save errno from socket_status.
Andreas Schwab <schwab@suse.de>
parents: 53497
diff changeset
674 saved_errno = errno;
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
675 }
53693
04e9ebd16fcd (main): Restore errno from saved_errno,
Richard M. Stallman <rms@gnu.org>
parents: 53646
diff changeset
676 else
04e9ebd16fcd (main): Restore errno from saved_errno,
Richard M. Stallman <rms@gnu.org>
parents: 53646
diff changeset
677 errno = saved_errno;
27474
ed3668c0295a (socket_status): New function.
Gerd Moellmann <gerd@gnu.org>
parents: 27305
diff changeset
678 }
493
8b101799ff37 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 478
diff changeset
679 }
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
680
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
681 switch (sock_status)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
682 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
683 case 1:
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
684 /* There's a socket, but it isn't owned by us. This is OK if
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
685 we are root. */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
686 if (0 != geteuid ())
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
687 {
73578
e193b6a3f379 Fixes to emacsclient.c for GNU/Linux.
Juanma Barranquero <lekktu@gmail.com>
parents: 73562
diff changeset
688 fprintf (stderr, "%s: Invalid socket owner\n", progname);
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
689 return INVALID_SOCKET;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
690 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
691 break;
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
692
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
693 case 2:
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
694 /* `stat' failed */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
695 if (saved_errno == ENOENT)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
696 fprintf (stderr,
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
697 "%s: can't find socket; have you started the server?\n\
48627
c91408365d35 (main): Tell user how to start server within Emacs if socket could not
Stephen Eglen <stephen@gnu.org>
parents: 48617
diff changeset
698 To start the server in Emacs, type \"M-x server-start\".\n",
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
699 progname);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
700 else
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
701 fprintf (stderr, "%s: can't stat %s: %s\n",
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
702 progname, server.sun_path, strerror (saved_errno));
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
703 return INVALID_SOCKET;
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
704 }
493
8b101799ff37 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 478
diff changeset
705 }
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
706
3595
e10f7473d2e3 * emacsserver.c (main): When we're passing a `struct sockaddr_un'
Jim Blandy <jimb@redhat.com>
parents: 1031
diff changeset
707 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
e10f7473d2e3 * emacsserver.c (main): When we're passing a `struct sockaddr_un'
Jim Blandy <jimb@redhat.com>
parents: 1031
diff changeset
708 < 0)
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
709 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
710 fprintf (stderr, "%s: ", progname);
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
711 perror ("connect");
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
712 return INVALID_SOCKET;
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
713 }
15758
7e712d42d371 (main) [HAVE_SOCKETS]: Use two separate stdio
Richard M. Stallman <rms@gnu.org>
parents: 14610
diff changeset
714
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
715 return s;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
716 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
717 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
718
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
719 HSOCKET
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
720 set_socket ()
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
721 {
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
722 HSOCKET s;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
723
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
724 INITIALIZE ();
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
725
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
726 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
727 /* Explicit --socket-name argument. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
728 if (socket_name)
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
729 {
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
730 s = set_local_socket ();
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
731 if ((s != INVALID_SOCKET) || alternate_editor)
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
732 return s;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
733
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
734 fprintf (stderr, "%s: error accessing socket \"%s\"",
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
735 progname, socket_name);
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
736 exit (EXIT_FAILURE);
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
737 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
738 #endif
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
739
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
740 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
741 if (!server_file)
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
742 server_file = getenv ("EMACS_SERVER_FILE");
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
743
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
744 if (server_file)
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
745 {
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
746 s = set_tcp_socket ();
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
747 if ((s != INVALID_SOCKET) || alternate_editor)
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
748 return s;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
749
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
750 fprintf (stderr, "%s: error accessing server file \"%s\"",
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
751 progname, server_file);
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
752 exit (EXIT_FAILURE);
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
753 }
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
754
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
755 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
756 /* Implicit local socket. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
757 s = set_local_socket ();
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
758 if (s != INVALID_SOCKET)
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
759 return s;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
760 #endif
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
761
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
762 /* Implicit server file. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
763 server_file = "server";
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
764 s = set_tcp_socket ();
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
765 if ((s != INVALID_SOCKET) || alternate_editor)
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
766 return s;
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
767
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
768 /* No implicit or explicit socket, and no alternate editor. */
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
769 fprintf (stderr, "%s: No socket or alternate editor. Please use:\n\n"
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
770 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
771 "\t--socket-name\n"
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
772 #endif
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
773 "\t--server-file (or environment variable EMACS_SERVER_FILE)\n\
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
774 \t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n",
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
775 progname);
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
776 exit (EXIT_FAILURE);
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
777 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
778
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
779 int
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
780 main (argc, argv)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
781 int argc;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
782 char **argv;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
783 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
784 HSOCKET s;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
785 int i, rl, needlf = 0;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
786 char *cwd;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
787 char string[BUFSIZ+1];
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
788
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
789 progname = argv[0];
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
790
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
791 /* Process options. */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
792 decode_options (argc, argv);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
793
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
794 if ((argc - optind < 1) && !eval)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
795 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
796 fprintf (stderr, "%s: file name or argument required\n", progname);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
797 fprintf (stderr, "Try `%s --help' for more information\n", progname);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
798 exit (EXIT_FAILURE);
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
799 }
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
800
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
801 if ((s = set_socket ()) == INVALID_SOCKET)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
802 fail (argc, argv);
15758
7e712d42d371 (main) [HAVE_SOCKETS]: Use two separate stdio
Richard M. Stallman <rms@gnu.org>
parents: 14610
diff changeset
803
48617
802b2a15e451 (main): Test HAVE_GETCWD rather than BSD_SYSTEM.
Richard M. Stallman <rms@gnu.org>
parents: 47717
diff changeset
804 #ifdef HAVE_GETCWD
802b2a15e451 (main): Test HAVE_GETCWD rather than BSD_SYSTEM.
Richard M. Stallman <rms@gnu.org>
parents: 47717
diff changeset
805 cwd = getcwd (string, sizeof string);
802b2a15e451 (main): Test HAVE_GETCWD rather than BSD_SYSTEM.
Richard M. Stallman <rms@gnu.org>
parents: 47717
diff changeset
806 #else
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
807 cwd = getwd (string);
11030
3345f6426f49 Use BSD sockets whenever available, even if HAVE_SYSVIPC.
Richard M. Stallman <rms@gnu.org>
parents: 10124
diff changeset
808 #endif
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
809 if (cwd == 0)
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
810 {
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
811 /* getwd puts message in STRING if it fails. */
48617
802b2a15e451 (main): Test HAVE_GETCWD rather than BSD_SYSTEM.
Richard M. Stallman <rms@gnu.org>
parents: 47717
diff changeset
812 #ifdef HAVE_GETCWD
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
813 fprintf (stderr, "%s: %s (%s)\n", progname,
52069
76d453ac3c4f (main): Fix having macros in a printf statement.
Juanma Barranquero <lekktu@gmail.com>
parents: 50566
diff changeset
814 "Cannot get current working directory", strerror (errno));
48617
802b2a15e451 (main): Test HAVE_GETCWD rather than BSD_SYSTEM.
Richard M. Stallman <rms@gnu.org>
parents: 47717
diff changeset
815 #else
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
816 fprintf (stderr, "%s: %s (%s)\n", progname, string, strerror (errno));
19582
1289a4779e68 [HAVE_SYSVIPC]: Include errno.h, as in the other case.
Richard M. Stallman <rms@gnu.org>
parents: 18701
diff changeset
817 #endif
27305
4161adef5fd3 Add option -a EDITOR and environment variable
Gerd Moellmann <gerd@gnu.org>
parents: 26083
diff changeset
818 fail (argc, argv);
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
819 }
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
820
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
821 if (nowait)
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
822 SEND_STRING ("-nowait ");
16061
a1290c4159b5 (quote_file_name): New function.
Richard M. Stallman <rms@gnu.org>
parents: 16030
diff changeset
823
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
824 if (eval)
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
825 SEND_STRING ("-eval ");
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
826
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
827 if (display)
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
828 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
829 SEND_STRING ("-display ");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
830 SEND_QUOTED (display);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
831 SEND_STRING (" ");
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
832 }
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
833
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
834 if ((argc - optind > 0))
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
835 {
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
836 for (i = optind; i < argc; i++)
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
837 {
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
838 if (eval)
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
839 ; /* Don't prepend any cwd or anything like that. */
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
840 else if (*argv[i] == '+')
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
841 {
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
842 char *p = argv[i] + 1;
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
843 while (isdigit ((unsigned char) *p) || *p == ':') p++;
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
844 if (*p != 0)
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
845 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
846 SEND_QUOTED (cwd);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
847 SEND_STRING ("/");
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
848 }
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
849 }
73765
bcb5c0f9a466 (longopts) [! NO_SOCKETS_IN_FILE_SYSTEM]: Don't show option --socket-name.
Juanma Barranquero <lekktu@gmail.com>
parents: 73657
diff changeset
850 else if (! file_name_absolute_p (argv[i]))
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
851 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
852 SEND_QUOTED (cwd);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
853 SEND_STRING ("/");
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
854 }
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
855
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
856 SEND_QUOTED (argv[i]);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
857 SEND_STRING (" ");
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
858 }
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
859 }
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
860 else
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
861 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
862 while (fgets (string, BUFSIZ, stdin))
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
863 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
864 SEND_QUOTED (string);
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
865 }
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
866 SEND_STRING (" ");
52635
9963e9da5850 (quote_file_name): Print the result instead of
Eli Zaretskii <eliz@gnu.org>
parents: 52532
diff changeset
867 }
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
868
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
869 SEND_STRING ("\n");
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
870
15995
7284f973fc42 (both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
parents: 15758
diff changeset
871 /* Maybe wait for an answer. */
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
872 if (!nowait)
47647
5498210ad40d Remove SYSV support.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 47515
diff changeset
873 {
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
874 if (!eval)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
875 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
876 printf ("Waiting for Emacs...");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
877 needlf = 2;
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
878 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
879 fflush (stdout);
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
880
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
881 /* Now, wait for an answer and print any messages. */
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
882 while ((rl = recv (s, string, BUFSIZ, 0)) > 0)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
883 {
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
884 string[rl] = '\0';
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
885 if (needlf == 2)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
886 printf ("\n");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
887 printf ("%s", string);
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
888 needlf = string[0] == '\0' ? needlf : string[strlen (string) - 1] != '\n';
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
889 }
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
890
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
891 if (needlf)
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
892 printf ("\n");
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
893 fflush (stdout);
47515
6ee937dcfe16 (quote_file_name): Quote \n.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 42412
diff changeset
894 }
42176
1f5dbeefa536 Include "config.h", not <../src/config.h>.
Pavel Janík <Pavel@Janik.cz>
parents: 39723
diff changeset
895
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
896 CLOSE_SOCKET (s);
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
897 return EXIT_SUCCESS;
412
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
898 }
3cc2c5136064 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
899
73617
7af864af10b6 [WINDOWSNT]: Define HAVE_INET_SOCKETS.
Juanma Barranquero <lekktu@gmail.com>
parents: 73601
diff changeset
900 #endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
73562
368665ce6f35 Add support for TCP sockets.
Juanma Barranquero <lekktu@gmail.com>
parents: 72005
diff changeset
901
5527
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
902 #ifndef HAVE_STRERROR
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
903 char *
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
904 strerror (errnum)
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
905 int errnum;
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
906 {
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
907 extern char *sys_errlist[];
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
908 extern int sys_nerr;
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
909
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
910 if (errnum >= 0 && errnum < sys_nerr)
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
911 return sys_errlist[errnum];
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
912 return (char *) "Unknown error";
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
913 }
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
914
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5522
diff changeset
915 #endif /* ! HAVE_STRERROR */
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 52069
diff changeset
916
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 52069
diff changeset
917 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 52069
diff changeset
918 (do not change this comment) */
55442
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
919
a47704955f8d Throughout, replace 0 destined for `exit' arg with `EXIT_SUCCESS'.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents: 53813
diff changeset
920 /* emacsclient.c ends here */