493
|
1 /* Client process that communicates with GNU Emacs acting as server.
|
5522
|
2 Copyright (C) 1986, 1987, 1994 Free Software Foundation, Inc.
|
412
|
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
|
5522
|
8 the Free Software Foundation; either version 2, or (at your option)
|
412
|
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
|
14186
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
412
|
20
|
|
21
|
|
22 #define NO_SHORTNAMES
|
4696
|
23 #include <../src/config.h>
|
412
|
24 #undef read
|
|
25 #undef write
|
|
26 #undef open
|
|
27 #undef close
|
3595
|
28 #undef signal
|
412
|
29
|
16030
|
30 #include <stdio.h>
|
|
31 #include <getopt.h>
|
21387
|
32 #ifdef STDC_HEADERS
|
|
33 #include <stdlib.h>
|
|
34 #endif
|
21431
|
35 #ifdef HAVE_UNISTD_H
|
|
36 #include <unistd.h>
|
|
37 #endif
|
412
|
38
|
16030
|
39 char *getenv (), *getwd ();
|
|
40 char *getcwd ();
|
|
41
|
|
42 /* This is defined with -D from the compilation command,
|
|
43 which extracts it from ../lisp/version.el. */
|
|
44
|
|
45 #ifndef VERSION
|
|
46 #define VERSION "unspecified"
|
|
47 #endif
|
|
48
|
|
49 /* Name used to invoke this program. */
|
|
50 char *progname;
|
|
51
|
16146
|
52 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
|
16030
|
53 int nowait = 0;
|
|
54
|
21387
|
55 void print_help_and_exit ();
|
|
56
|
16030
|
57 struct option longopts[] =
|
|
58 {
|
16146
|
59 { "no-wait", no_argument, NULL, 'n' },
|
16030
|
60 { "help", no_argument, NULL, 'H' },
|
|
61 { "version", no_argument, NULL, 'V' },
|
|
62 { 0 }
|
|
63 };
|
|
64
|
|
65 /* Decode the options from argv and argc.
|
16061
|
66 The global variable `optind' will say how many arguments we used up. */
|
16030
|
67
|
16061
|
68 void
|
16030
|
69 decode_options (argc, argv)
|
|
70 int argc;
|
|
71 char **argv;
|
|
72 {
|
|
73 while (1)
|
|
74 {
|
|
75 int opt = getopt_long (argc, argv,
|
|
76 "VHn", longopts, 0);
|
|
77
|
|
78 if (opt == EOF)
|
|
79 break;
|
|
80
|
|
81 switch (opt)
|
|
82 {
|
|
83 case 0:
|
|
84 /* If getopt returns 0, then it has already processed a
|
|
85 long-named option. We should do nothing. */
|
|
86 break;
|
|
87
|
|
88 case 'n':
|
|
89 nowait = 1;
|
|
90 break;
|
|
91
|
|
92 case 'V':
|
|
93 fprintf (stderr, "Version %s\n", VERSION);
|
|
94 exit (1);
|
|
95 break;
|
|
96
|
|
97 case 'H':
|
|
98 default:
|
|
99 print_help_and_exit ();
|
|
100 }
|
|
101 }
|
|
102 }
|
|
103
|
21387
|
104 void
|
16030
|
105 print_help_and_exit ()
|
|
106 {
|
|
107 fprintf (stderr,
|
16146
|
108 "Usage: %s [-n] [--no-wait] [+LINENUMBER] FILENAME\n",
|
16030
|
109 progname);
|
|
110 fprintf (stderr,
|
|
111 "Report bugs to bug-gnu-emacs@prep.ai.mit.edu.\n");
|
|
112 exit (1);
|
|
113 }
|
16061
|
114
|
16074
|
115 /* Return a copy of NAME, inserting a &
|
|
116 before each &, each space, and any initial -.
|
16061
|
117 Change spaces to underscores, too, so that the
|
|
118 return value never contains a space. */
|
|
119
|
|
120 char *
|
|
121 quote_file_name (name)
|
|
122 char *name;
|
|
123 {
|
|
124 char *copy = (char *) malloc (strlen (name) * 2 + 1);
|
|
125 char *p, *q;
|
|
126
|
|
127 p = name;
|
|
128 q = copy;
|
|
129 while (*p)
|
|
130 {
|
|
131 if (*p == ' ')
|
|
132 {
|
16074
|
133 *q++ = '&';
|
16061
|
134 *q++ = '_';
|
|
135 p++;
|
|
136 }
|
|
137 else
|
|
138 {
|
16074
|
139 if (*p == '&' || (*p == '-' && p == name))
|
|
140 *q++ = '&';
|
16061
|
141 *q++ = *p++;
|
|
142 }
|
|
143 }
|
16074
|
144 *q++ = 0;
|
16061
|
145
|
|
146 return copy;
|
|
147 }
|
18701
|
148
|
|
149 /* Like malloc but get fatal error if memory is exhausted. */
|
|
150
|
24169
|
151 long *
|
18701
|
152 xmalloc (size)
|
|
153 unsigned int size;
|
|
154 {
|
24180
|
155 long *result = (long *) malloc (size);
|
18701
|
156 if (result == NULL)
|
|
157 {
|
|
158 perror ("malloc");
|
|
159 exit (1);
|
|
160 }
|
|
161 return result;
|
|
162 }
|
16030
|
163
|
10124
|
164 #if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
|
412
|
165
|
|
166 main (argc, argv)
|
|
167 int argc;
|
|
168 char **argv;
|
|
169 {
|
|
170 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
|
|
171 argv[0]);
|
|
172 fprintf (stderr, "on systems with Berkeley sockets or System V IPC.\n");
|
|
173 exit (1);
|
|
174 }
|
|
175
|
|
176 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
|
|
177
|
11368
|
178 #if defined (HAVE_SOCKETS) && ! defined (NO_SOCKETS_IN_FILE_SYSTEM)
|
412
|
179 /* BSD code is very different from SYSV IPC code */
|
|
180
|
|
181 #include <sys/types.h>
|
|
182 #include <sys/socket.h>
|
|
183 #include <sys/un.h>
|
493
|
184 #include <sys/stat.h>
|
412
|
185 #include <errno.h>
|
|
186
|
5522
|
187 extern char *strerror ();
|
412
|
188 extern int errno;
|
|
189
|
9491
|
190 int
|
412
|
191 main (argc, argv)
|
|
192 int argc;
|
|
193 char **argv;
|
|
194 {
|
24084
|
195 char *system_name;
|
|
196 int system_name_length;
|
412
|
197 int s, i;
|
15758
|
198 FILE *out, *in;
|
412
|
199 struct sockaddr_un server;
|
|
200 char *homedir, *cwd, *str;
|
|
201 char string[BUFSIZ];
|
|
202
|
16030
|
203 progname = argv[0];
|
|
204
|
|
205 /* Process options. */
|
16061
|
206 decode_options (argc, argv);
|
16030
|
207
|
16061
|
208 if (argc - optind < 1)
|
16030
|
209 print_help_and_exit ();
|
412
|
210
|
|
211 /*
|
|
212 * Open up an AF_UNIX socket in this person's home directory
|
|
213 */
|
|
214
|
|
215 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
|
|
216 {
|
|
217 fprintf (stderr, "%s: ", argv[0]);
|
|
218 perror ("socket");
|
|
219 exit (1);
|
|
220 }
|
|
221 server.sun_family = AF_UNIX;
|
|
222 #ifndef SERVER_HOME_DIR
|
493
|
223 {
|
|
224 struct stat statbfr;
|
24084
|
225 system_name_length = 32;
|
493
|
226
|
24084
|
227 while (1)
|
|
228 {
|
|
229 system_name = (char *) xmalloc (system_name_length + 1);
|
|
230
|
|
231 /* system_name must be null-terminated string. */
|
|
232 system_name[system_name_length] = '\0';
|
|
233
|
|
234 if (gethostname (system_name, system_name_length) == 0)
|
|
235 break;
|
|
236
|
|
237 free (system_name);
|
|
238 system_name_length *= 2;
|
|
239 }
|
|
240
|
493
|
241 sprintf (server.sun_path, "/tmp/esrv%d-%s", geteuid (), system_name);
|
412
|
242
|
493
|
243 if (stat (server.sun_path, &statbfr) == -1)
|
|
244 {
|
816
|
245 if (errno == ENOENT)
|
|
246 fprintf (stderr,
|
11434
|
247 "%s: can't find socket; have you started the server?\n",
|
|
248 argv[0]);
|
816
|
249 else
|
11434
|
250 fprintf (stderr, "%s: can't stat %s: %s\n",
|
|
251 argv[0], server.sun_path, strerror (errno));
|
493
|
252 exit (1);
|
|
253 }
|
10124
|
254 if (statbfr.st_uid != geteuid ())
|
493
|
255 {
|
11434
|
256 fprintf (stderr, "%s: Invalid socket owner\n", argv[0]);
|
493
|
257 exit (1);
|
|
258 }
|
|
259 }
|
412
|
260 #else
|
|
261 if ((homedir = getenv ("HOME")) == NULL)
|
|
262 {
|
|
263 fprintf (stderr, "%s: No home directory\n", argv[0]);
|
|
264 exit (1);
|
|
265 }
|
|
266 strcpy (server.sun_path, homedir);
|
10124
|
267 strcat (server.sun_path, "/.emacs-server-");
|
|
268 gethostname (system_name, sizeof (system_name));
|
|
269 strcat (server.sun_path, system_name);
|
412
|
270 #endif
|
|
271
|
3595
|
272 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
|
|
273 < 0)
|
412
|
274 {
|
|
275 fprintf (stderr, "%s: ", argv[0]);
|
|
276 perror ("connect");
|
|
277 exit (1);
|
|
278 }
|
15758
|
279
|
|
280 /* We use the stream OUT to send our command to the server. */
|
412
|
281 if ((out = fdopen (s, "r+")) == NULL)
|
|
282 {
|
|
283 fprintf (stderr, "%s: ", argv[0]);
|
|
284 perror ("fdopen");
|
|
285 exit (1);
|
|
286 }
|
|
287
|
15758
|
288 /* We use the stream IN to read the response.
|
|
289 We used to use just one stream for both output and input
|
|
290 on the socket, but reversing direction works nonportably:
|
|
291 on some systems, the output appears as the first input;
|
|
292 on other systems it does not. */
|
|
293 if ((in = fdopen (s, "r+")) == NULL)
|
|
294 {
|
|
295 fprintf (stderr, "%s: ", argv[0]);
|
|
296 perror ("fdopen");
|
|
297 exit (1);
|
|
298 }
|
|
299
|
16218
|
300 #ifdef BSD_SYSTEM
|
412
|
301 cwd = getwd (string);
|
11030
|
302 #else
|
|
303 cwd = getcwd (string, sizeof string);
|
|
304 #endif
|
412
|
305 if (cwd == 0)
|
|
306 {
|
|
307 /* getwd puts message in STRING if it fails. */
|
19582
|
308 fprintf (stderr, "%s: %s (%s)\n", argv[0],
|
|
309 #ifdef BSD_SYSTEM
|
|
310 string,
|
|
311 #else
|
|
312 "Cannot get current working directory",
|
|
313 #endif
|
|
314 strerror (errno));
|
412
|
315 exit (1);
|
|
316 }
|
|
317
|
16061
|
318 if (nowait)
|
|
319 fprintf (out, "-nowait ");
|
|
320
|
|
321 for (i = optind; i < argc; i++)
|
412
|
322 {
|
|
323 if (*argv[i] == '+')
|
|
324 {
|
|
325 char *p = argv[i] + 1;
|
|
326 while (*p >= '0' && *p <= '9') p++;
|
|
327 if (*p != 0)
|
24652
|
328 fprintf (out, "%s/", quote_file_name (cwd));
|
412
|
329 }
|
|
330 else if (*argv[i] != '/')
|
24652
|
331 fprintf (out, "%s/", quote_file_name (cwd));
|
16061
|
332
|
|
333 fprintf (out, "%s ", quote_file_name (argv[i]));
|
412
|
334 }
|
|
335 fprintf (out, "\n");
|
|
336 fflush (out);
|
|
337
|
15995
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
338 /* Maybe wait for an answer. */
|
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
339 if (nowait)
|
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
340 return 0;
|
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
341
|
412
|
342 printf ("Waiting for Emacs...");
|
|
343 fflush (stdout);
|
|
344
|
15758
|
345 /* Now, wait for an answer and print any messages. On some systems,
|
|
346 the first line we read will actually be the output we just sent.
|
|
347 We can't predict whether that will happen, so if it does, we
|
|
348 detect it by recognizing `Client: ' at the beginning. */
|
|
349
|
|
350 while (str = fgets (string, BUFSIZ, in))
|
|
351 printf ("%s", str);
|
412
|
352
|
9491
|
353 return 0;
|
412
|
354 }
|
|
355
|
|
356 #else /* This is the SYSV IPC section */
|
|
357
|
|
358 #include <sys/types.h>
|
|
359 #include <sys/ipc.h>
|
|
360 #include <sys/msg.h>
|
10124
|
361 #include <sys/utsname.h>
|
412
|
362 #include <stdio.h>
|
19582
|
363 #include <errno.h>
|
|
364 extern int errno;
|
412
|
365
|
6213
|
366 char *getwd (), *getcwd (), *getenv ();
|
10124
|
367 struct utsname system_name;
|
6213
|
368
|
412
|
369 main (argc, argv)
|
|
370 int argc;
|
|
371 char **argv;
|
|
372 {
|
|
373 int s;
|
|
374 key_t key;
|
6213
|
375 /* Size of text allocated in MSGP. */
|
|
376 int size_allocated = BUFSIZ;
|
|
377 /* Amount of text used in MSGP. */
|
|
378 int used;
|
|
379 struct msgbuf *msgp
|
|
380 = (struct msgbuf *) malloc (sizeof (struct msgbuf) + size_allocated);
|
412
|
381 struct msqid_ds * msg_st;
|
|
382 char *homedir, buf[BUFSIZ];
|
|
383 char gwdirb[BUFSIZ];
|
|
384 char *cwd;
|
|
385 char *temp;
|
16030
|
386
|
|
387 progname = argv[0];
|
|
388
|
|
389 /* Process options. */
|
16061
|
390 decode_options (argc, argv);
|
16030
|
391
|
16061
|
392 if (argc - optind < 1)
|
16030
|
393 print_help_and_exit ();
|
412
|
394
|
|
395 /*
|
10124
|
396 * Create a message queue using ~/.emacs-server as the path for ftok
|
412
|
397 */
|
|
398 if ((homedir = getenv ("HOME")) == NULL)
|
|
399 {
|
|
400 fprintf (stderr, "%s: No home directory\n", argv[0]);
|
|
401 exit (1);
|
|
402 }
|
|
403 strcpy (buf, homedir);
|
10124
|
404 #ifndef HAVE_LONG_FILE_NAMES
|
|
405 /* If file names are short, we can't fit the host name. */
|
|
406 strcat (buf, "/.emacs-server");
|
|
407 #else
|
|
408 strcat (buf, "/.emacs-server-");
|
|
409 uname (&system_name);
|
|
410 strcat (buf, system_name.nodename);
|
|
411 #endif
|
412
|
412 creat (buf, 0600);
|
|
413 key = ftok (buf, 1); /* unlikely to be anyone else using it */
|
1031
|
414 s = msgget (key, 0600 | IPC_CREAT);
|
412
|
415 if (s == -1)
|
|
416 {
|
|
417 fprintf (stderr, "%s: ", argv[0]);
|
|
418 perror ("msgget");
|
|
419 exit (1);
|
|
420 }
|
|
421
|
|
422 /* Determine working dir, so we can prefix it to all the arguments. */
|
16218
|
423 #ifdef BSD_SYSTEM
|
412
|
424 temp = getwd (gwdirb);
|
|
425 #else
|
|
426 temp = getcwd (gwdirb, sizeof gwdirb);
|
|
427 #endif
|
|
428
|
|
429 cwd = gwdirb;
|
|
430 if (temp != 0)
|
|
431 {
|
|
432 /* On some systems, cwd can look like `@machine/...';
|
|
433 ignore everything before the first slash in such a case. */
|
|
434 while (*cwd && *cwd != '/')
|
|
435 cwd++;
|
|
436 strcat (cwd, "/");
|
|
437 }
|
|
438 else
|
|
439 {
|
19582
|
440 #ifdef BSD_SYSTEM
|
11486
|
441 fprintf (stderr, "%s: %s\n", argv[0], cwd);
|
19582
|
442 #else
|
|
443 fprintf (stderr, "%s: Cannot get current working directory: %s\n",
|
|
444 argv[0], strerror (errno));
|
|
445 #endif
|
412
|
446 exit (1);
|
|
447 }
|
|
448
|
|
449 msgp->mtext[0] = 0;
|
6213
|
450 used = 0;
|
16061
|
451
|
|
452 if (nowait)
|
|
453 {
|
|
454 strcat (msgp->mtext, "-nowait ");
|
|
455 used += 8;
|
|
456 }
|
|
457
|
|
458 argc -= optind;
|
|
459 argv += optind;
|
|
460
|
412
|
461 while (argc)
|
|
462 {
|
6213
|
463 int need_cwd = 0;
|
8346
|
464 char *modified_arg = argv[0];
|
15995
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
465
|
16061
|
466 if (*modified_arg == '+')
|
412
|
467 {
|
8346
|
468 char *p = modified_arg + 1;
|
412
|
469 while (*p >= '0' && *p <= '9') p++;
|
|
470 if (*p != 0)
|
6213
|
471 need_cwd = 1;
|
412
|
472 }
|
8346
|
473 else if (*modified_arg != '/')
|
6213
|
474 need_cwd = 1;
|
|
475
|
16061
|
476 modified_arg = quote_file_name (modified_arg);
|
|
477
|
6213
|
478 if (need_cwd)
|
24652
|
479 /* Overestimate in case we have to quote something in CWD. */
|
|
480 used += 2 * strlen (cwd);
|
8346
|
481 used += strlen (modified_arg) + 1;
|
6213
|
482 while (used + 2 > size_allocated)
|
|
483 {
|
|
484 size_allocated *= 2;
|
|
485 msgp = (struct msgbuf *) realloc (msgp,
|
|
486 (sizeof (struct msgbuf)
|
|
487 + size_allocated));
|
|
488 }
|
|
489
|
|
490 if (need_cwd)
|
24652
|
491 strcat (msgp->mtext, quote_file_name (cwd));
|
412
|
492
|
8346
|
493 strcat (msgp->mtext, modified_arg);
|
412
|
494 strcat (msgp->mtext, " ");
|
|
495 argv++; argc--;
|
|
496 }
|
|
497 strcat (msgp->mtext, "\n");
|
6213
|
498 #ifdef HPUX /* HPUX has a bug. */
|
|
499 if (strlen (msgp->mtext) >= 512)
|
|
500 {
|
11434
|
501 fprintf (stderr, "%s: args too long for msgsnd\n", progname);
|
6213
|
502 exit (1);
|
|
503 }
|
|
504 #endif
|
412
|
505 msgp->mtype = 1;
|
|
506 if (msgsnd (s, msgp, strlen (msgp->mtext)+1, 0) < 0)
|
|
507 {
|
8360
|
508 fprintf (stderr, "%s: ", progname);
|
412
|
509 perror ("msgsnd");
|
|
510 exit (1);
|
|
511 }
|
15995
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
512
|
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
513 /* Maybe wait for an answer. */
|
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
514 if (nowait)
|
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
515 return 0;
|
7284f973fc42
(both versions): Handle -nowait and --nowait by sending data to the server.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
516
|
412
|
517 printf ("Waiting for Emacs...");
|
|
518 fflush (stdout);
|
|
519
|
|
520 msgrcv (s, msgp, BUFSIZ, getpid (), 0); /* wait for anything back */
|
|
521 strcpy (buf, msgp->mtext);
|
|
522
|
14610
|
523 printf ("\n");
|
|
524 if (*buf)
|
|
525 printf ("%s\n", buf);
|
412
|
526 exit (0);
|
|
527 }
|
|
528
|
|
529 #endif /* HAVE_SYSVIPC */
|
|
530
|
|
531 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
|
5527
|
532
|
|
533 #ifndef HAVE_STRERROR
|
|
534 char *
|
|
535 strerror (errnum)
|
|
536 int errnum;
|
|
537 {
|
|
538 extern char *sys_errlist[];
|
|
539 extern int sys_nerr;
|
|
540
|
|
541 if (errnum >= 0 && errnum < sys_nerr)
|
|
542 return sys_errlist[errnum];
|
|
543 return (char *) "Unknown error";
|
|
544 }
|
|
545
|
|
546 #endif /* ! HAVE_STRERROR */
|