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.
7 This file is part of XEmacs.
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
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
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.
24 Author: Andy Norman (ange@hplb.hpl.hp.com), based on
25 'etc/emacsclient.c' from the GNU Emacs 18.52 distribution.
27 Please mail bugs and suggestions to the XEmacs maintainer.
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.
36 * (If gnuserv came bundled with your emacs, the README file is probably
37 * ../etc/gnuserv.README relative to the directory containing this file)
41 /* Hand-munged RCS header */
42 static char rcsid [] = "!Header: gnuclient.c,v 2.2 95/12/12 01:39:21 wing nene !";
50 #include <sys/types.h>
51 #define DONT_ENCAPSULATE
56 #endif /* HAVE_STRING_H */
60 #endif /* HAVE_UNISTD_H */
64 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
65 !defined(INTERNET_DOMAIN_SOCKETS)
67 main (int argc, char *argv[])
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");
75 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
77 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
78 static char *cp = NULL; /* ptr into valid bit of cwd above */
80 static pid_t emacs_pid; /* Process id for emacs process */
82 void initialize_signals (void);
85 tell_emacs_to_resume (int sig)
87 char buffer[GSERV_BUFSZ+1];
88 int s; /* socket / msqid to server */
89 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
92 /* Why is SYSV so retarded? */
93 /* We want emacs to realize that we are resuming */
95 signal(SIGCONT, tell_emacs_to_resume);
98 connect_type = make_connection (NULL, (u_short) 0, &s);
100 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
101 send_string(s, buffer);
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 */
113 pass_signal_to_emacs (int sig)
115 if (kill (emacs_pid, sig) == -1)
117 fprintf (stderr, "gnuattach: Could not pass signal to emacs process\n");
120 initialize_signals ();
124 initialize_signals (void)
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);
133 signal (SIGWINCH, pass_signal_to_emacs);
137 /* We want emacs to realize that we are resuming */
138 signal (SIGCONT, tell_emacs_to_resume);
144 get_current_working_directory -- return the cwd.
147 get_current_working_directory (void)
150 { /* haven't calculated it yet */
152 if (getwd (cwd) == 0)
154 if (getcwd (cwd,MAXPATHLEN) == NULL)
158 fprintf (stderr, "%s: unable to get current working directory\n",
163 /* on some systems, cwd can look like '@machine/' ... */
164 /* ignore everything before the first '/' */
165 for (cp = cwd; *cp && *cp != '/'; ++cp)
172 } /* get_current_working_directory */
176 filename_expand -- try to convert the given filename into a fully-qualified
180 filename_expand (char *fullpath, char *filename)
181 /* fullpath - returned full pathname */
182 /* filename - filename to expand */
188 if (filename[0] && filename[0] == '/')
190 /* Absolute (unix-style) pathname. Do nothing */
191 strcat (fullpath, filename);
194 else if (filename[0] && filename[0] == '\\' &&
195 filename[1] && filename[1] == '\\')
197 /* This path includes the server name (something like
198 "\\server\path"), so we assume it's absolute. Do nothing to
200 strcat (fullpath, filename);
202 else if (filename[0] &&
203 filename[1] && filename[1] == ':' &&
204 filename[2] && filename[2] == '\\')
206 /* Absolute pathname with drive letter. Convert "<drive>:"
208 strcat (fullpath, "//");
209 strncat (fullpath, filename, 1);
210 strcat (fullpath, &filename[2]);
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. */
219 strcat (fullpath, get_current_working_directory ());
220 len = strlen (fullpath);
222 if (len > 0 && fullpath[len-1] == '/') /* trailing slash already? */
225 strcat (fullpath, "/"); /* nope, append trailing slash */
226 /* Don't forget to add the filename! */
227 strcat (fullpath,filename);
229 } /* filename_expand */
231 /* Encase the string in quotes, escape all the backslashes and quotes
234 clean_string (const char *s)
241 for (const_p = s; *const_p; const_p++, i++)
243 if (*const_p == '\\' || *const_p == '\"')
245 else if (*const_p == '\004')
250 p = res = (char *) malloc (i + 2 + 1);
279 #define GET_ARGUMENT(var, desc) do { \
280 if (*(p + 1)) (var) = p + 1; \
285 fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
294 /* A strdup imitation. */
296 my_strdup (const char *s)
298 char *new_s = (char *) malloc (strlen (s) + 1);
305 main (int argc, char *argv[])
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
315 int batch = 0; /* batch mode */
316 int view = 0; /* view only. */
318 int errflg = 0; /* option error */
319 int s; /* socket / msqid to server */
320 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
322 int suppress_windows_system = 0;
323 char *display = NULL;
324 #ifdef INTERNET_DOMAIN_SOCKETS
325 char *hostarg = NULL; /* remote hostname */
327 char thishost[HOSTNAMSZ]; /* this hostname */
328 char remotepath[MAXPATHLEN+1]; /* remote pathname */
330 int rflg = 0; /* pathname given on cmdline */
332 u_short port = 0; /* port to server */
333 #endif /* INTERNET_DOMAIN_SOCKETS */
335 struct msgbuf *msgp; /* message */
336 #endif /* SYSV_IPC */
338 char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
339 char result[GSERV_BUFSZ + 1];
342 #ifdef INTERNET_DOMAIN_SOCKETS
343 memset (remotepath, 0, sizeof (remotepath));
344 #endif /* INTERNET_DOMAIN_SOCKETS */
346 progname = strrchr (argv[0], '/');
353 tmpdir = getenv ("TMPDIR");
358 display = getenv ("DISPLAY");
360 display = my_strdup (display);
361 #ifndef HAVE_MS_WINDOWS
363 suppress_windows_system = 1;
366 for (i = 1; argv[i] && !errflg; i++)
370 else if (*argv[i] == '-'
371 && (*(argv[i] + 1) == '\0'
372 || (*(argv[i] + 1) == '-' && *(argv[i] + 2) == '\0')))
379 if (!strcmp (argv[i], "-batch") || !strcmp (argv[i], "--batch"))
381 else if (!strcmp (argv[i], "-eval") || !strcmp (argv[i], "--eval"))
385 fprintf (stderr, "%s: `-eval' must be followed by an argument\n",
391 else if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "--display"))
393 suppress_windows_system = 0;
397 "%s: `-display' must be followed by an argument\n",
403 /* no need to strdup. */
406 else if (!strcmp (argv[i], "-nw"))
407 suppress_windows_system = 1;
410 /* Iterate over one-letter options. */
413 for (p = argv[i] + 1; *p && !over; p++)
424 GET_ARGUMENT (eval_function, "-f");
427 GET_ARGUMENT (load_library, "-l");
429 #ifdef INTERNET_DOMAIN_SOCKETS
431 GET_ARGUMENT (hostarg, "-h");
434 GET_ARGUMENT (portarg, "-p");
435 port = atoi (portarg);
438 GET_ARGUMENT (remotearg, "-r");
439 strcpy (remotepath, remotearg);
442 #endif /* INTERNET_DOMAIN_SOCKETS */
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 */
464 if (batch && argv[i])
466 fprintf (stderr, "%s: Cannot specify `-batch' with file names\n",
470 if (suppress_windows_system && hostarg)
472 fprintf (stderr, "%s: Remote editing is available only on X\n",
478 if (eval_function || eval_form || load_library)
480 #if defined(INTERNET_DOMAIN_SOCKETS)
481 connect_type = make_connection (hostarg, port, &s);
483 connect_type = make_connection (NULL, (u_short) 0, &s);
485 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
486 send_string (s, command);
489 send_string (s , "(load-library ");
490 send_string (s, clean_string(load_library));
491 send_string (s, ") ");
495 send_string (s, eval_form);
499 send_string (s, "(");
500 send_string (s, eval_function);
501 send_string (s, ")");
503 send_string (s, "))");
504 /* disconnect already sends EOT_STR */
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 */
515 /* no sexp on the command line, so read it from stdin */
518 #if defined(INTERNET_DOMAIN_SOCKETS)
519 connect_type = make_connection (hostarg, port, &s);
521 connect_type = make_connection (NULL, (u_short) 0, &s);
523 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
524 send_string (s, command);
526 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ-1)) > 0)
529 send_string(s, buffer);
532 /* disconnect already sends EOT_STR */
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 */
544 if (suppress_windows_system)
549 fprintf (stderr, "%s: Not connected to a tty", progname);
552 #if defined(INTERNET_DOMAIN_SOCKETS)
553 connect_type = make_connection (hostarg, port, &s);
555 connect_type = make_connection (NULL, (u_short) 0, &s);
557 send_string (s, "(gnuserv-eval '(emacs-pid))");
558 send_string (s, EOT_STR);
560 if (read_line (s, buffer) == 0)
562 fprintf (stderr, "%s: Could not establish Emacs process id\n",
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 */
573 emacs_pid = (pid_t)atol(buffer);
574 initialize_signals();
575 } /* suppress_windows_system */
577 #if defined(INTERNET_DOMAIN_SOCKETS)
578 connect_type = make_connection (hostarg, port, &s);
580 connect_type = make_connection (NULL, (u_short) 0, &s);
583 #ifdef INTERNET_DOMAIN_SOCKETS
584 if (connect_type == (int) CONN_INTERNET)
587 gethostname (thishost, HOSTNAMSZ);
589 { /* attempt to generate a path
591 if ((ptr = getenv ("GNU_NODE")) != NULL)
592 /* user specified a path */
593 strcpy (remotepath, ptr);
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
601 strcat (remotepath, thishost);
607 { /* same machines, no need for path */
608 remotepath[0] = '\0'; /* default is the empty path */
610 #endif /* INTERNET_DOMAIN_SOCKETS */
613 if ((msgp = (struct msgbuf *)
614 malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
616 fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
620 msgp->mtext[0] = '\0'; /* ready for later strcats */
621 #endif /* SYSV_IPC */
623 if (suppress_windows_system)
625 char *term = getenv ("TERM");
628 fprintf (stderr, "%s: unknown terminal type\n", progname);
631 sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(",
632 clean_string (tty), clean_string (term), (int)getpid ());
634 else /* !suppress_windows_system */
637 sprintf (command, "(gnuserv-edit-files '(x %s) '(",
638 clean_string (display));
639 #ifdef HAVE_MS_WINDOWS
641 sprintf (command, "(gnuserv-edit-files '(mswindows nil) '(");
643 } /* !suppress_windows_system */
644 send_string (s, command);
651 if (i < argc - 1 && *argv[i] == '+')
652 starting_line = atoi (argv[i++]);
655 /* If the last argument is +something, treat it as a file. */
661 filename_expand (fullpath, argv[i]);
662 #ifdef INTERNET_DOMAIN_SOCKETS
663 path = (char *) malloc (strlen (remotepath) + strlen (fullpath) + 1);
664 sprintf (path, "%s%s", remotepath, fullpath);
666 path = my_strdup (fullpath);
668 sprintf (command, "(%d . %s)", starting_line, clean_string (path));
669 send_string (s, command);
673 sprintf (command, ")%s%s",
674 (quick || (nofiles && !suppress_windows_system)) ? " 'quick" : "",
675 view ? " 'view" : "");
676 send_string (s, command);
677 send_string (s, ")");
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 */
693 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */