Commit | Line | Data |
---|---|---|
efb859b4 | 1 | /* Client process that communicates with GNU Emacs acting as server. |
92af894f | 2 | Copyright (C) 1986, 1987, 1994 Free Software Foundation, Inc. |
46cec291 RS |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
92af894f | 8 | the Free Software Foundation; either version 2, or (at your option) |
46cec291 RS |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
46cec291 RS |
20 | |
21 | ||
22 | #define NO_SHORTNAMES | |
18160b98 | 23 | #include <../src/config.h> |
46cec291 RS |
24 | #undef read |
25 | #undef write | |
26 | #undef open | |
46cec291 | 27 | #undef close |
4e23f2ba | 28 | #undef signal |
46cec291 RS |
29 | |
30 | ||
b135a7cf | 31 | #if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC) |
46cec291 RS |
32 | #include <stdio.h> |
33 | ||
34 | main (argc, argv) | |
35 | int argc; | |
36 | char **argv; | |
37 | { | |
38 | fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n", | |
39 | argv[0]); | |
40 | fprintf (stderr, "on systems with Berkeley sockets or System V IPC.\n"); | |
41 | exit (1); | |
42 | } | |
43 | ||
44 | #else /* HAVE_SOCKETS or HAVE_SYSVIPC */ | |
45 | ||
1e523e3d | 46 | #if defined (HAVE_SOCKETS) && ! defined (NO_SOCKETS_IN_FILE_SYSTEM) |
46cec291 RS |
47 | /* BSD code is very different from SYSV IPC code */ |
48 | ||
49 | #include <sys/types.h> | |
50 | #include <sys/socket.h> | |
51 | #include <sys/un.h> | |
efb859b4 | 52 | #include <sys/stat.h> |
46cec291 RS |
53 | #include <stdio.h> |
54 | #include <errno.h> | |
55 | ||
92af894f | 56 | extern char *strerror (); |
46cec291 RS |
57 | extern int errno; |
58 | ||
340ff9de | 59 | int |
46cec291 RS |
60 | main (argc, argv) |
61 | int argc; | |
62 | char **argv; | |
63 | { | |
64 | char system_name[32]; | |
65 | int s, i; | |
66 | FILE *out; | |
67 | struct sockaddr_un server; | |
68 | char *homedir, *cwd, *str; | |
69 | char string[BUFSIZ]; | |
70 | ||
71 | char *getenv (), *getwd (); | |
7e1251a1 | 72 | char *getcwd (); |
46cec291 RS |
73 | int geteuid (); |
74 | ||
75 | if (argc < 2) | |
76 | { | |
77 | fprintf (stderr, "Usage: %s [+linenumber] filename\n", argv[0]); | |
78 | exit (1); | |
79 | } | |
80 | ||
81 | /* | |
82 | * Open up an AF_UNIX socket in this person's home directory | |
83 | */ | |
84 | ||
85 | if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) | |
86 | { | |
87 | fprintf (stderr, "%s: ", argv[0]); | |
88 | perror ("socket"); | |
89 | exit (1); | |
90 | } | |
91 | server.sun_family = AF_UNIX; | |
92 | #ifndef SERVER_HOME_DIR | |
efb859b4 JB |
93 | { |
94 | struct stat statbfr; | |
95 | ||
96 | gethostname (system_name, sizeof (system_name)); | |
97 | sprintf (server.sun_path, "/tmp/esrv%d-%s", geteuid (), system_name); | |
98 | ||
99 | if (stat (server.sun_path, &statbfr) == -1) | |
100 | { | |
c0acc3e1 JB |
101 | if (errno == ENOENT) |
102 | fprintf (stderr, | |
18a2fa91 KH |
103 | "%s: can't find socket; have you started the server?\n", |
104 | argv[0]); | |
c0acc3e1 | 105 | else |
18a2fa91 KH |
106 | fprintf (stderr, "%s: can't stat %s: %s\n", |
107 | argv[0], server.sun_path, strerror (errno)); | |
efb859b4 JB |
108 | exit (1); |
109 | } | |
b135a7cf | 110 | if (statbfr.st_uid != geteuid ()) |
efb859b4 | 111 | { |
18a2fa91 | 112 | fprintf (stderr, "%s: Invalid socket owner\n", argv[0]); |
efb859b4 JB |
113 | exit (1); |
114 | } | |
115 | } | |
46cec291 RS |
116 | #else |
117 | if ((homedir = getenv ("HOME")) == NULL) | |
118 | { | |
119 | fprintf (stderr, "%s: No home directory\n", argv[0]); | |
120 | exit (1); | |
121 | } | |
122 | strcpy (server.sun_path, homedir); | |
b135a7cf RS |
123 | strcat (server.sun_path, "/.emacs-server-"); |
124 | gethostname (system_name, sizeof (system_name)); | |
125 | strcat (server.sun_path, system_name); | |
46cec291 RS |
126 | #endif |
127 | ||
4e23f2ba JB |
128 | if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) |
129 | < 0) | |
46cec291 RS |
130 | { |
131 | fprintf (stderr, "%s: ", argv[0]); | |
132 | perror ("connect"); | |
133 | exit (1); | |
134 | } | |
135 | if ((out = fdopen (s, "r+")) == NULL) | |
136 | { | |
137 | fprintf (stderr, "%s: ", argv[0]); | |
138 | perror ("fdopen"); | |
139 | exit (1); | |
140 | } | |
141 | ||
ee6a193c | 142 | #ifdef BSD |
46cec291 | 143 | cwd = getwd (string); |
ee6a193c RS |
144 | #else |
145 | cwd = getcwd (string, sizeof string); | |
146 | #endif | |
46cec291 RS |
147 | if (cwd == 0) |
148 | { | |
149 | /* getwd puts message in STRING if it fails. */ | |
92af894f | 150 | fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno)); |
46cec291 RS |
151 | exit (1); |
152 | } | |
153 | ||
154 | for (i = 1; i < argc; i++) | |
155 | { | |
156 | if (*argv[i] == '+') | |
157 | { | |
158 | char *p = argv[i] + 1; | |
159 | while (*p >= '0' && *p <= '9') p++; | |
160 | if (*p != 0) | |
161 | fprintf (out, "%s/", cwd); | |
162 | } | |
163 | else if (*argv[i] != '/') | |
164 | fprintf (out, "%s/", cwd); | |
165 | fprintf (out, "%s ", argv[i]); | |
166 | } | |
167 | fprintf (out, "\n"); | |
168 | fflush (out); | |
169 | ||
170 | printf ("Waiting for Emacs..."); | |
171 | fflush (stdout); | |
172 | ||
173 | rewind (out); /* re-read the output */ | |
174 | str = fgets (string, BUFSIZ, out); | |
175 | ||
176 | /* Now, wait for an answer and print any messages. */ | |
177 | ||
178 | while (str = fgets (string, BUFSIZ, out)) | |
179 | printf ("%s", str); | |
180 | ||
340ff9de | 181 | return 0; |
46cec291 RS |
182 | } |
183 | ||
184 | #else /* This is the SYSV IPC section */ | |
185 | ||
186 | #include <sys/types.h> | |
187 | #include <sys/ipc.h> | |
188 | #include <sys/msg.h> | |
b135a7cf | 189 | #include <sys/utsname.h> |
46cec291 RS |
190 | #include <stdio.h> |
191 | ||
1518a9eb | 192 | char *getwd (), *getcwd (), *getenv (); |
b135a7cf | 193 | struct utsname system_name; |
1518a9eb | 194 | |
46cec291 RS |
195 | main (argc, argv) |
196 | int argc; | |
197 | char **argv; | |
198 | { | |
199 | int s; | |
200 | key_t key; | |
1518a9eb RS |
201 | /* Size of text allocated in MSGP. */ |
202 | int size_allocated = BUFSIZ; | |
203 | /* Amount of text used in MSGP. */ | |
204 | int used; | |
205 | struct msgbuf *msgp | |
206 | = (struct msgbuf *) malloc (sizeof (struct msgbuf) + size_allocated); | |
46cec291 RS |
207 | struct msqid_ds * msg_st; |
208 | char *homedir, buf[BUFSIZ]; | |
209 | char gwdirb[BUFSIZ]; | |
210 | char *cwd; | |
211 | char *temp; | |
defb80d7 | 212 | char *progname = argv[0]; |
46cec291 RS |
213 | |
214 | if (argc < 2) | |
215 | { | |
216 | fprintf (stderr, "Usage: %s [+linenumber] filename\n", argv[0]); | |
217 | exit (1); | |
218 | } | |
219 | ||
220 | /* | |
b135a7cf | 221 | * Create a message queue using ~/.emacs-server as the path for ftok |
46cec291 RS |
222 | */ |
223 | if ((homedir = getenv ("HOME")) == NULL) | |
224 | { | |
225 | fprintf (stderr, "%s: No home directory\n", argv[0]); | |
226 | exit (1); | |
227 | } | |
228 | strcpy (buf, homedir); | |
b135a7cf RS |
229 | #ifndef HAVE_LONG_FILE_NAMES |
230 | /* If file names are short, we can't fit the host name. */ | |
231 | strcat (buf, "/.emacs-server"); | |
232 | #else | |
233 | strcat (buf, "/.emacs-server-"); | |
234 | uname (&system_name); | |
235 | strcat (buf, system_name.nodename); | |
236 | #endif | |
46cec291 RS |
237 | creat (buf, 0600); |
238 | key = ftok (buf, 1); /* unlikely to be anyone else using it */ | |
61a93162 | 239 | s = msgget (key, 0600 | IPC_CREAT); |
46cec291 RS |
240 | if (s == -1) |
241 | { | |
242 | fprintf (stderr, "%s: ", argv[0]); | |
243 | perror ("msgget"); | |
244 | exit (1); | |
245 | } | |
246 | ||
247 | /* Determine working dir, so we can prefix it to all the arguments. */ | |
248 | #ifdef BSD | |
249 | temp = getwd (gwdirb); | |
250 | #else | |
251 | temp = getcwd (gwdirb, sizeof gwdirb); | |
252 | #endif | |
253 | ||
254 | cwd = gwdirb; | |
255 | if (temp != 0) | |
256 | { | |
257 | /* On some systems, cwd can look like `@machine/...'; | |
258 | ignore everything before the first slash in such a case. */ | |
259 | while (*cwd && *cwd != '/') | |
260 | cwd++; | |
261 | strcat (cwd, "/"); | |
262 | } | |
263 | else | |
264 | { | |
26396b1b | 265 | fprintf (stderr, "%s: %s\n", argv[0], cwd); |
46cec291 RS |
266 | exit (1); |
267 | } | |
268 | ||
269 | msgp->mtext[0] = 0; | |
1518a9eb | 270 | used = 0; |
46cec291 RS |
271 | argc--; argv++; |
272 | while (argc) | |
273 | { | |
1518a9eb | 274 | int need_cwd = 0; |
cd33a5a0 RS |
275 | char *modified_arg = argv[0]; |
276 | if (*modified_arg == '+') | |
46cec291 | 277 | { |
cd33a5a0 | 278 | char *p = modified_arg + 1; |
46cec291 RS |
279 | while (*p >= '0' && *p <= '9') p++; |
280 | if (*p != 0) | |
1518a9eb | 281 | need_cwd = 1; |
46cec291 | 282 | } |
cd33a5a0 | 283 | else if (*modified_arg != '/') |
1518a9eb RS |
284 | need_cwd = 1; |
285 | ||
286 | if (need_cwd) | |
287 | used += strlen (cwd); | |
cd33a5a0 | 288 | used += strlen (modified_arg) + 1; |
1518a9eb RS |
289 | while (used + 2 > size_allocated) |
290 | { | |
291 | size_allocated *= 2; | |
292 | msgp = (struct msgbuf *) realloc (msgp, | |
293 | (sizeof (struct msgbuf) | |
294 | + size_allocated)); | |
295 | } | |
296 | ||
297 | if (need_cwd) | |
46cec291 RS |
298 | strcat (msgp->mtext, cwd); |
299 | ||
cd33a5a0 | 300 | strcat (msgp->mtext, modified_arg); |
46cec291 RS |
301 | strcat (msgp->mtext, " "); |
302 | argv++; argc--; | |
303 | } | |
304 | strcat (msgp->mtext, "\n"); | |
1518a9eb RS |
305 | #ifdef HPUX /* HPUX has a bug. */ |
306 | if (strlen (msgp->mtext) >= 512) | |
307 | { | |
18a2fa91 | 308 | fprintf (stderr, "%s: args too long for msgsnd\n", progname); |
1518a9eb RS |
309 | exit (1); |
310 | } | |
311 | #endif | |
46cec291 RS |
312 | msgp->mtype = 1; |
313 | if (msgsnd (s, msgp, strlen (msgp->mtext)+1, 0) < 0) | |
314 | { | |
defb80d7 | 315 | fprintf (stderr, "%s: ", progname); |
46cec291 RS |
316 | perror ("msgsnd"); |
317 | exit (1); | |
318 | } | |
319 | /* | |
320 | * Now, wait for an answer | |
321 | */ | |
322 | printf ("Waiting for Emacs..."); | |
323 | fflush (stdout); | |
324 | ||
325 | msgrcv (s, msgp, BUFSIZ, getpid (), 0); /* wait for anything back */ | |
326 | strcpy (buf, msgp->mtext); | |
327 | ||
328 | printf ("\n%s\n", buf); | |
329 | exit (0); | |
330 | } | |
331 | ||
332 | #endif /* HAVE_SYSVIPC */ | |
333 | ||
334 | #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */ | |
27711600 RM |
335 | \f |
336 | #ifndef HAVE_STRERROR | |
337 | char * | |
338 | strerror (errnum) | |
339 | int errnum; | |
340 | { | |
341 | extern char *sys_errlist[]; | |
342 | extern int sys_nerr; | |
343 | ||
344 | if (errnum >= 0 && errnum < sys_nerr) | |
345 | return sys_errlist[errnum]; | |
346 | return (char *) "Unknown error"; | |
347 | } | |
348 | ||
349 | #endif /* ! HAVE_STRERROR */ |