comparison lib-src/gnuclient.c @ 428:3ecd8885ac67 r21-2-22

Import from CVS: tag r21-2-22
author cvs
date Mon, 13 Aug 2007 11:28:15 +0200
parents
children 8de8e3f6228a
comparison
equal deleted inserted replaced
427:0a0253eac470 428:3ecd8885ac67
1 /* -*-C-*-
2 Client code to allow local and remote editing of files by XEmacs.
3 Copyright (C) 1989 Free Software Foundation, Inc.
4 Copyright (C) 1995 Sun Microsystems, Inc.
5 Copyright (C) 1997 Free Software Foundation, Inc.
6
7 This file is part of XEmacs.
8
9 XEmacs is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
13
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with XEmacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
23
24 Author: Andy Norman (ange@hplb.hpl.hp.com), based on
25 'etc/emacsclient.c' from the GNU Emacs 18.52 distribution.
26
27 Please mail bugs and suggestions to the XEmacs maintainer.
28 */
29
30 /*
31 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
32 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
33 * GNUATTACH support added by Ben Wing <wing@xemacs.org>.
34 * Please see the note at the end of the README file for details.
35 *
36 * (If gnuserv came bundled with your emacs, the README file is probably
37 * ../etc/gnuserv.README relative to the directory containing this file)
38 */
39
40 #if 0
41 /* Hand-munged RCS header */
42 static char rcsid [] = "!Header: gnuclient.c,v 2.2 95/12/12 01:39:21 wing nene !";
43 #endif
44
45 #include "gnuserv.h"
46 #include "getopt.h"
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <sys/types.h>
51 #define DONT_ENCAPSULATE
52 #include <sysfile.h>
53
54 #ifdef HAVE_STRING_H
55 #include <string.h>
56 #endif /* HAVE_STRING_H */
57
58 #ifdef HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif /* HAVE_UNISTD_H */
61
62 #include <signal.h>
63
64 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
65 !defined(INTERNET_DOMAIN_SOCKETS)
66 int
67 main (int argc, char *argv[])
68 {
69 fprintf (stderr, "Sorry, the Emacs server is only "
70 "supported on systems that have\n");
71 fprintf (stderr, "Unix Domain sockets, Internet Domain "
72 "sockets or System V IPC.\n");
73 exit (1);
74 } /* main */
75 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
76
77 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
78 static char *cp = NULL; /* ptr into valid bit of cwd above */
79
80 static pid_t emacs_pid; /* Process id for emacs process */
81
82 void initialize_signals (void);
83
84 static void
85 tell_emacs_to_resume (int sig)
86 {
87 char buffer[GSERV_BUFSZ+1];
88 int s; /* socket / msqid to server */
89 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
90 ONN_IPC */
91
92 /* Why is SYSV so retarded? */
93 /* We want emacs to realize that we are resuming */
94 #ifdef SIGCONT
95 signal(SIGCONT, tell_emacs_to_resume);
96 #endif
97
98 connect_type = make_connection (NULL, (u_short) 0, &s);
99
100 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
101 send_string(s, buffer);
102
103 #ifdef SYSV_IPC
104 if (connect_type == (int) CONN_IPC)
105 disconnect_from_ipc_server (s, msgp, FALSE);
106 #else /* !SYSV_IPC */
107 if (connect_type != (int) CONN_IPC)
108 disconnect_from_server (s, FALSE);
109 #endif /* !SYSV_IPC */
110 }
111
112 static void
113 pass_signal_to_emacs (int sig)
114 {
115 if (kill (emacs_pid, sig) == -1)
116 {
117 fprintf (stderr, "gnuattach: Could not pass signal to emacs process\n");
118 exit (1);
119 }
120 initialize_signals ();
121 }
122
123 void
124 initialize_signals ()
125 {
126 /* Set up signal handler to pass relevant signals to emacs process.
127 We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
128 Emacs, but I think it's better not to. I can see no reason why
129 Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
130 signal (SIGQUIT, pass_signal_to_emacs);
131 signal (SIGINT, pass_signal_to_emacs);
132 #ifdef SIGWINCH
133 signal (SIGWINCH, pass_signal_to_emacs);
134 #endif
135
136 #ifdef SIGCONT
137 /* We want emacs to realize that we are resuming */
138 signal (SIGCONT, tell_emacs_to_resume);
139 #endif
140 }
141
142
143 /*
144 get_current_working_directory -- return the cwd.
145 */
146 static char *
147 get_current_working_directory (void)
148 {
149 if (cp == NULL)
150 { /* haven't calculated it yet */
151 #ifdef BSD
152 if (getwd (cwd) == 0)
153 #else /* !BSD */
154 if (getcwd (cwd,MAXPATHLEN) == NULL)
155 #endif /* !BSD */
156 {
157 perror (progname);
158 fprintf (stderr, "%s: unable to get current working directory\n",
159 progname);
160 exit (1);
161 } /* if */
162
163 /* on some systems, cwd can look like '@machine/' ... */
164 /* ignore everything before the first '/' */
165 for (cp = cwd; *cp && *cp != '/'; ++cp)
166 ;
167
168 } /* if */
169
170 return cp;
171
172 } /* get_current_working_directory */
173
174
175 /*
176 filename_expand -- try to convert the given filename into a fully-qualified
177 pathname.
178 */
179 static void
180 filename_expand (char *fullpath, char *filename)
181 /* fullpath - returned full pathname */
182 /* filename - filename to expand */
183 {
184 int len;
185
186 fullpath[0] = '\0';
187
188 if (filename[0] && filename[0] == '/')
189 {
190 /* Absolute (unix-style) pathname. Do nothing */
191 strcat (fullpath, filename);
192 }
193 #ifdef __CYGWIN32__
194 else if (filename[0] && filename[0] == '\\' &&
195 filename[1] && filename[1] == '\\')
196 {
197 /* This path includes the server name (something like
198 "\\server\path"), so we assume it's absolute. Do nothing to
199 it. */
200 strcat (fullpath, filename);
201 }
202 else if (filename[0] &&
203 filename[1] && filename[1] == ':' &&
204 filename[2] && filename[2] == '\\')
205 {
206 /* Absolute pathname with drive letter. Convert "<drive>:"
207 to "//<drive>/". */
208 strcat (fullpath, "//");
209 strncat (fullpath, filename, 1);
210 strcat (fullpath, &filename[2]);
211 }
212 #endif
213 else
214 {
215 /* Assume relative Unix style path. Get the current directory
216 and prepend it. FIXME: need to fix the case of DOS paths like
217 "\foo", where we need to get the current drive. */
218
219 strcat (fullpath, get_current_working_directory ());
220 len = strlen (fullpath);
221
222 if (len > 0 && fullpath[len-1] == '/') /* trailing slash already? */
223 ; /* yep */
224 else
225 strcat (fullpath, "/"); /* nope, append trailing slash */
226 /* Don't forget to add the filename! */
227 strcat (fullpath,filename);
228 }
229 } /* filename_expand */
230
231 /* Encase the string in quotes, escape all the backslashes and quotes
232 in string. */
233 static char *
234 clean_string (CONST char *s)
235 {
236 int i = 0;
237 char *p, *res;
238
239 {
240 CONST char *const_p;
241 for (const_p = s; *const_p; const_p++, i++)
242 {
243 if (*const_p == '\\' || *const_p == '\"')
244 ++i;
245 else if (*const_p == '\004')
246 i += 3;
247 }
248 }
249
250 p = res = (char *) malloc (i + 2 + 1);
251 *p++ = '\"';
252 for (; *s; p++, s++)
253 {
254 switch (*s)
255 {
256 case '\\':
257 *p++ = '\\';
258 *p = '\\';
259 break;
260 case '\"':
261 *p++ = '\\';
262 *p = '\"';
263 break;
264 case '\004':
265 *p++ = '\\';
266 *p++ = 'C';
267 *p++ = '-';
268 *p = 'd';
269 break;
270 default:
271 *p = *s;
272 }
273 }
274 *p++ = '\"';
275 *p = '\0';
276 return res;
277 }
278
279 #define GET_ARGUMENT(var, desc) do { \
280 if (*(p + 1)) (var) = p + 1; \
281 else \
282 { \
283 if (!argv[++i]) \
284 { \
285 fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
286 progname, desc); \
287 exit (1); \
288 } \
289 (var) = argv[i]; \
290 } \
291 over = 1; \
292 } while (0)
293
294 /* A strdup immitation. */
295 static char *
296 my_strdup (CONST char *s)
297 {
298 char *new = malloc (strlen (s) + 1);
299 if (new)
300 strcpy (new, s);
301 return new;
302 }
303
304 int
305 main (int argc, char *argv[])
306 {
307 int starting_line = 1; /* line to start editing at */
308 char command[MAXPATHLEN+50]; /* emacs command buffer */
309 char fullpath[MAXPATHLEN+1]; /* full pathname to file */
310 char *eval_form = NULL; /* form to evaluate with `-eval' */
311 char *eval_function = NULL; /* function to evaluate with `-f' */
312 char *load_library = NULL; /* library to load */
313 int quick = 0; /* quick edit, don't wait for user to
314 finish */
315 int batch = 0; /* batch mode */
316 int view = 0; /* view only. */
317 int nofiles = 0;
318 int errflg = 0; /* option error */
319 int s; /* socket / msqid to server */
320 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
321 * CONN_IPC */
322 int suppress_windows_system = 0;
323 char *display = NULL;
324 #ifdef INTERNET_DOMAIN_SOCKETS
325 char *hostarg = NULL; /* remote hostname */
326 char *remotearg;
327 char thishost[HOSTNAMSZ]; /* this hostname */
328 char remotepath[MAXPATHLEN+1]; /* remote pathname */
329 char *path;
330 int rflg = 0; /* pathname given on cmdline */
331 char *portarg;
332 u_short port = 0; /* port to server */
333 #endif /* INTERNET_DOMAIN_SOCKETS */
334 #ifdef SYSV_IPC
335 struct msgbuf *msgp; /* message */
336 #endif /* SYSV_IPC */
337 char *tty = NULL;
338 char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
339 char result[GSERV_BUFSZ + 1];
340 int i;
341
342 #ifdef INTERNET_DOMAIN_SOCKETS
343 memset (remotepath, 0, sizeof (remotepath));
344 #endif /* INTERNET_DOMAIN_SOCKETS */
345
346 progname = strrchr (argv[0], '/');
347 if (progname)
348 ++progname;
349 else
350 progname = argv[0];
351
352 #ifdef USE_TMPDIR
353 tmpdir = getenv ("TMPDIR");
354 #endif
355 if (!tmpdir)
356 tmpdir = "/tmp";
357
358 display = getenv ("DISPLAY");
359 if (display)
360 display = my_strdup (display);
361 #ifndef HAVE_MS_WINDOWS
362 else
363 suppress_windows_system = 1;
364 #endif
365
366 for (i = 1; argv[i] && !errflg; i++)
367 {
368 if (*argv[i] != '-')
369 break;
370 else if (*argv[i] == '-'
371 && (*(argv[i] + 1) == '\0'
372 || (*(argv[i] + 1) == '-' && *(argv[i] + 2) == '\0')))
373 {
374 /* `-' or `--' */
375 ++i;
376 break;
377 }
378
379 if (!strcmp (argv[i], "-batch") || !strcmp (argv[i], "--batch"))
380 batch = 1;
381 else if (!strcmp (argv[i], "-eval") || !strcmp (argv[i], "--eval"))
382 {
383 if (!argv[++i])
384 {
385 fprintf (stderr, "%s: `-eval' must be followed by an argument\n",
386 progname);
387 exit (1);
388 }
389 eval_form = argv[i];
390 }
391 else if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "--display"))
392 {
393 suppress_windows_system = 0;
394 if (!argv[++i])
395 {
396 fprintf (stderr,
397 "%s: `-display' must be followed by an argument\n",
398 progname);
399 exit (1);
400 }
401 if (display)
402 free (display);
403 /* no need to strdup. */
404 display = argv[i];
405 }
406 else if (!strcmp (argv[i], "-nw"))
407 suppress_windows_system = 1;
408 else
409 {
410 /* Iterate over one-letter options. */
411 char *p;
412 int over = 0;
413 for (p = argv[i] + 1; *p && !over; p++)
414 {
415 switch (*p)
416 {
417 case 'q':
418 quick = 1;
419 break;
420 case 'v':
421 view = 1;
422 break;
423 case 'f':
424 GET_ARGUMENT (eval_function, "-f");
425 break;
426 case 'l':
427 GET_ARGUMENT (load_library, "-l");
428 break;
429 #ifdef INTERNET_DOMAIN_SOCKETS
430 case 'h':
431 GET_ARGUMENT (hostarg, "-h");
432 break;
433 case 'p':
434 GET_ARGUMENT (portarg, "-p");
435 port = atoi (portarg);
436 break;
437 case 'r':
438 GET_ARGUMENT (remotearg, "-r");
439 strcpy (remotepath, remotearg);
440 rflg = 1;
441 break;
442 #endif /* INTERNET_DOMAIN_SOCKETS */
443 default:
444 errflg = 1;
445 }
446 } /* for */
447 } /* else */
448 } /* for */
449
450 if (errflg)
451 {
452 fprintf (stderr,
453 #ifdef INTERNET_DOMAIN_SOCKETS
454 "usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
455 " [-batch] [-f function] [-eval form]\n"
456 " [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
457 #else /* !INTERNET_DOMAIN_SOCKETS */
458 "usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
459 "[[+line] path] ...\n",
460 #endif /* !INTERNET_DOMAIN_SOCKETS */
461 progname);
462 exit (1);
463 }
464 if (batch && argv[i])
465 {
466 fprintf (stderr, "%s: Cannot specify `-batch' with file names\n",
467 progname);
468 exit (1);
469 }
470 if (suppress_windows_system && hostarg)
471 {
472 fprintf (stderr, "%s: Remote editing is available only on X\n",
473 progname);
474 exit (1);
475 }
476
477 *result = '\0';
478 if (eval_function || eval_form || load_library)
479 {
480 #if defined(INTERNET_DOMAIN_SOCKETS)
481 connect_type = make_connection (hostarg, port, &s);
482 #else
483 connect_type = make_connection (NULL, (u_short) 0, &s);
484 #endif
485 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
486 send_string (s, command);
487 if (load_library)
488 {
489 send_string (s , "(load-library ");
490 send_string (s, clean_string(load_library));
491 send_string (s, ") ");
492 }
493 if (eval_form)
494 {
495 send_string (s, eval_form);
496 }
497 if (eval_function)
498 {
499 send_string (s, "(");
500 send_string (s, eval_function);
501 send_string (s, ")");
502 }
503 send_string (s, "))");
504 /* disconnect already sends EOT_STR */
505 #ifdef SYSV_IPC
506 if (connect_type == (int) CONN_IPC)
507 disconnect_from_ipc_server (s, msgp, batch && !quick);
508 #else /* !SYSV_IPC */
509 if (connect_type != (int) CONN_IPC)
510 disconnect_from_server (s, batch && !quick);
511 #endif /* !SYSV_IPC */
512 } /* eval_function || eval_form || load_library */
513 else if (batch)
514 {
515 /* no sexp on the command line, so read it from stdin */
516 int nb;
517
518 #if defined(INTERNET_DOMAIN_SOCKETS)
519 connect_type = make_connection (hostarg, port, &s);
520 #else
521 connect_type = make_connection (NULL, (u_short) 0, &s);
522 #endif
523 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
524 send_string (s, command);
525
526 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ-1)) > 0)
527 {
528 buffer[nb] = '\0';
529 send_string(s, buffer);
530 }
531 send_string(s,"))");
532 /* disconnect already sends EOT_STR */
533 #ifdef SYSV_IPC
534 if (connect_type == (int) CONN_IPC)
535 disconnect_from_ipc_server (s, msgp, batch && !quick);
536 #else /* !SYSV_IPC */
537 if (connect_type != (int) CONN_IPC)
538 disconnect_from_server (s, batch && !quick);
539 #endif /* !SYSV_IPC */
540 }
541
542 if (!batch)
543 {
544 if (suppress_windows_system)
545 {
546 tty = ttyname (0);
547 if (!tty)
548 {
549 fprintf (stderr, "%s: Not connected to a tty", progname);
550 exit (1);
551 }
552 #if defined(INTERNET_DOMAIN_SOCKETS)
553 connect_type = make_connection (hostarg, port, &s);
554 #else
555 connect_type = make_connection (NULL, (u_short) 0, &s);
556 #endif
557 send_string (s, "(gnuserv-eval '(emacs-pid))");
558 send_string (s, EOT_STR);
559
560 if (read_line (s, buffer) == 0)
561 {
562 fprintf (stderr, "%s: Could not establish Emacs process id\n",
563 progname);
564 exit (1);
565 }
566 /* Don't do disconnect_from_server becasue we have already read
567 data, and disconnect doesn't do anything else. */
568 #ifndef INTERNET_DOMAIN_SOCKETS
569 if (connect_type == (int) CONN_IPC)
570 disconnect_from_ipc_server (s, msgp, FALSE);
571 #endif /* !SYSV_IPC */
572
573 emacs_pid = (pid_t)atol(buffer);
574 initialize_signals();
575 } /* suppress_windows_system */
576
577 #if defined(INTERNET_DOMAIN_SOCKETS)
578 connect_type = make_connection (hostarg, port, &s);
579 #else
580 connect_type = make_connection (NULL, (u_short) 0, &s);
581 #endif
582
583 #ifdef INTERNET_DOMAIN_SOCKETS
584 if (connect_type == (int) CONN_INTERNET)
585 {
586 char *ptr;
587 gethostname (thishost, HOSTNAMSZ);
588 if (!rflg)
589 { /* attempt to generate a path
590 * to this machine */
591 if ((ptr = getenv ("GNU_NODE")) != NULL)
592 /* user specified a path */
593 strcpy (remotepath, ptr);
594 }
595 #if 0 /* This is really bogus... re-enable it if you must have it! */
596 #if defined (hp9000s300) || defined (hp9000s800)
597 else if (strcmp (thishost,hostarg))
598 { /* try /net/thishost */
599 strcpy (remotepath, "/net/"); /* (this fails using internet
600 addresses) */
601 strcat (remotepath, thishost);
602 }
603 #endif
604 #endif
605 }
606 else
607 { /* same machines, no need for path */
608 remotepath[0] = '\0'; /* default is the empty path */
609 }
610 #endif /* INTERNET_DOMAIN_SOCKETS */
611
612 #ifdef SYSV_IPC
613 if ((msgp = (struct msgbuf *)
614 malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
615 {
616 fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
617 exit (1);
618 } /* if */
619
620 msgp->mtext[0] = '\0'; /* ready for later strcats */
621 #endif /* SYSV_IPC */
622
623 if (suppress_windows_system)
624 {
625 char *term = getenv ("TERM");
626 if (!term)
627 {
628 fprintf (stderr, "%s: unknown terminal type\n", progname);
629 exit (1);
630 }
631 sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(",
632 clean_string (tty), clean_string (term), (int)getpid ());
633 }
634 else /* !suppress_windows_system */
635 {
636 if (display)
637 sprintf (command, "(gnuserv-edit-files '(x %s) '(",
638 clean_string (display));
639 #ifdef HAVE_MS_WINDOWS
640 else
641 sprintf (command, "(gnuserv-edit-files '(mswindows nil) '(");
642 #endif
643 } /* !suppress_windows_system */
644 send_string (s, command);
645
646 if (!argv[i])
647 nofiles = 1;
648
649 for (; argv[i]; i++)
650 {
651 if (i < argc - 1 && *argv[i] == '+')
652 starting_line = atoi (argv[i++]);
653 else
654 starting_line = 1;
655 /* If the last argument is +something, treat it as a file. */
656 if (i == argc)
657 {
658 starting_line = 1;
659 --i;
660 }
661 filename_expand (fullpath, argv[i]);
662 #ifdef INTERNET_DOMAIN_SOCKETS
663 path = malloc (strlen (remotepath) + strlen (fullpath) + 1);
664 sprintf (path, "%s%s", remotepath, fullpath);
665 #else
666 path = my_strdup (fullpath);
667 #endif
668 sprintf (command, "(%d . %s)", starting_line, clean_string (path));
669 send_string (s, command);
670 free (path);
671 } /* for */
672
673 sprintf (command, ")%s%s",
674 (quick || (nofiles && !suppress_windows_system)) ? " 'quick" : "",
675 view ? " 'view" : "");
676 send_string (s, command);
677 send_string (s, ")");
678
679 #ifdef SYSV_IPC
680 if (connect_type == (int) CONN_IPC)
681 disconnect_from_ipc_server (s, msgp, FALSE);
682 #else /* !SYSV_IPC */
683 if (connect_type != (int) CONN_IPC)
684 disconnect_from_server (s, FALSE);
685 #endif /* !SYSV_IPC */
686 } /* not batch */
687
688
689 return 0;
690
691 } /* main */
692
693 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */