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>
54 #endif /* HAVE_STRING_H */
58 #endif /* HAVE_UNISTD_H */
62 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
63 !defined(INTERNET_DOMAIN_SOCKETS)
65 main (int argc, char *argv[])
67 fprintf (stderr, "Sorry, the Emacs server is only "
68 "supported on systems that have\n");
69 fprintf (stderr, "Unix Domain sockets, Internet Domain "
70 "sockets or System V IPC.\n");
73 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
75 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
76 static char *cp = NULL; /* ptr into valid bit of cwd above */
78 static pid_t emacs_pid; /* Process id for emacs process */
80 void initialize_signals (void);
83 tell_emacs_to_resume (int sig)
85 char buffer[GSERV_BUFSZ+1];
86 int s; /* socket / msqid to server */
87 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
90 /* Why is SYSV so retarded? */
91 /* We want emacs to realize that we are resuming */
92 signal(SIGCONT, tell_emacs_to_resume);
94 connect_type = make_connection (NULL, (u_short) 0, &s);
96 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
97 send_string(s, buffer);
100 if (connect_type == (int) CONN_IPC)
101 disconnect_from_ipc_server (s, msgp, FALSE);
102 #else /* !SYSV_IPC */
103 if (connect_type != (int) CONN_IPC)
104 disconnect_from_server (s, FALSE);
105 #endif /* !SYSV_IPC */
109 pass_signal_to_emacs (int sig)
111 if (kill (emacs_pid, sig) == -1)
113 fprintf (stderr, "gnuattach: Could not pass signal to emacs process\n");
116 initialize_signals ();
120 initialize_signals ()
122 /* Set up signal handler to pass relevant signals to emacs process.
123 We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
124 Emacs, but I think it's better not to. I can see no reason why
125 Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
126 signal (SIGQUIT, pass_signal_to_emacs);
127 signal (SIGINT, pass_signal_to_emacs);
129 signal (SIGWINCH, pass_signal_to_emacs);
132 /* We want emacs to realize that we are resuming */
133 signal (SIGCONT, tell_emacs_to_resume);
138 get_current_working_directory -- return the cwd.
141 get_current_working_directory (void)
144 { /* haven't calculated it yet */
146 if (getwd (cwd) == 0)
148 if (getcwd (cwd,MAXPATHLEN) == NULL)
152 fprintf (stderr, "%s: unable to get current working directory\n",
157 /* on some systems, cwd can look like '@machine/' ... */
158 /* ignore everything before the first '/' */
159 for (cp = cwd; *cp && *cp != '/'; ++cp)
166 } /* get_current_working_directory */
170 filename_expand -- try to convert the given filename into a fully-qualified
174 filename_expand (char *fullpath, char *filename)
175 /* fullpath - returned full pathname */
176 /* filename - filename to expand */
182 if (filename[0] && filename[0] == '/')
184 /* Absolute (unix-style) pathname. Do nothing */
185 strcat (fullpath, filename);
188 else if (filename[0] && filename[0] == '\\' &&
189 filename[1] && filename[1] == '\\')
191 /* This path includes the server name (something like
192 "\\server\path"), so we assume it's absolute. Do nothing to
194 strcat (fullpath, filename);
196 else if (filename[0] &&
197 filename[1] && filename[1] == ':' &&
198 filename[2] && filename[2] == '\\')
200 /* Absolute pathname with drive letter. Convert "<drive>:"
202 strcat (fullpath, "//");
203 strncat (fullpath, filename, 1);
204 strcat (fullpath, &filename[2]);
209 /* Assume relative Unix style path. Get the current directory
210 and prepend it. FIXME: need to fix the case of DOS paths like
211 "\foo", where we need to get the current drive. */
213 strcat (fullpath, get_current_working_directory ());
214 len = strlen (fullpath);
216 if (len > 0 && fullpath[len-1] == '/') /* trailing slash already? */
219 strcat (fullpath, "/"); /* nope, append trailing slash */
221 } /* filename_expand */
223 /* Encase the string in quotes, escape all the backslashes and quotes
226 clean_string (CONST char *s)
233 for (const_p = s; *const_p; const_p++, i++)
235 if (*const_p == '\\' || *const_p == '\"')
237 else if (*const_p == '\004')
242 p = res = (char *) malloc (i + 2 + 1);
271 #define GET_ARGUMENT(var, desc) do { \
272 if (*(p + 1)) (var) = p + 1; \
277 fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
286 /* A strdup immitation. */
288 my_strdup (CONST char *s)
290 char *new = malloc (strlen (s) + 1);
297 main (int argc, char *argv[])
299 int starting_line = 1; /* line to start editing at */
300 char command[MAXPATHLEN+50]; /* emacs command buffer */
301 char fullpath[MAXPATHLEN+1]; /* full pathname to file */
302 char *eval_form = NULL; /* form to evaluate with `-eval' */
303 char *eval_function = NULL; /* function to evaluate with `-f' */
304 char *load_library = NULL; /* library to load */
305 int quick = 0; /* quick edit, don't wait for user to
307 int batch = 0; /* batch mode */
308 int view = 0; /* view only. */
310 int errflg = 0; /* option error */
311 int s; /* socket / msqid to server */
312 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
314 int suppress_windows_system = 0;
315 char *display = NULL;
316 #ifdef INTERNET_DOMAIN_SOCKETS
317 char *hostarg = NULL; /* remote hostname */
319 char thishost[HOSTNAMSZ]; /* this hostname */
320 char remotepath[MAXPATHLEN+1]; /* remote pathname */
322 int rflg = 0; /* pathname given on cmdline */
324 u_short port = 0; /* port to server */
325 #endif /* INTERNET_DOMAIN_SOCKETS */
327 struct msgbuf *msgp; /* message */
328 #endif /* SYSV_IPC */
330 char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
331 char result[GSERV_BUFSZ + 1];
334 #ifdef INTERNET_DOMAIN_SOCKETS
335 memset (remotepath, 0, sizeof (remotepath));
336 #endif /* INTERNET_DOMAIN_SOCKETS */
338 progname = strrchr (argv[0], '/');
345 tmpdir = getenv ("TMPDIR");
350 display = getenv ("DISPLAY");
352 display = my_strdup (display);
353 #ifndef HAVE_MS_WINDOWS
355 suppress_windows_system = 1;
358 for (i = 1; argv[i] && !errflg; i++)
362 else if (*argv[i] == '-'
363 && (*(argv[i] + 1) == '\0'
364 || (*(argv[i] + 1) == '-' && *(argv[i] + 2) == '\0')))
371 if (!strcmp (argv[i], "-batch") || !strcmp (argv[i], "--batch"))
373 else if (!strcmp (argv[i], "-eval") || !strcmp (argv[i], "--eval"))
377 fprintf (stderr, "%s: `-eval' must be followed by an argument\n",
383 else if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "--display"))
385 suppress_windows_system = 0;
389 "%s: `-display' must be followed by an argument\n",
395 /* no need to strdup. */
398 else if (!strcmp (argv[i], "-nw"))
399 suppress_windows_system = 1;
402 /* Iterate over one-letter options. */
405 for (p = argv[i] + 1; *p && !over; p++)
416 GET_ARGUMENT (eval_function, "-f");
419 GET_ARGUMENT (load_library, "-l");
421 #ifdef INTERNET_DOMAIN_SOCKETS
423 GET_ARGUMENT (hostarg, "-h");
426 GET_ARGUMENT (portarg, "-p");
427 port = atoi (portarg);
430 GET_ARGUMENT (remotearg, "-r");
431 strcpy (remotepath, remotearg);
434 #endif /* INTERNET_DOMAIN_SOCKETS */
445 #ifdef INTERNET_DOMAIN_SOCKETS
446 "usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
447 " [-batch] [-f function] [-eval form]\n"
448 " [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
449 #else /* !INTERNET_DOMAIN_SOCKETS */
450 "usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
451 "[[+line] path] ...\n",
452 #endif /* !INTERNET_DOMAIN_SOCKETS */
456 if (batch && argv[i])
458 fprintf (stderr, "%s: Cannot specify `-batch' with file names\n",
462 if (suppress_windows_system && hostarg)
464 fprintf (stderr, "%s: Remote editing is available only on X\n",
470 if (eval_function || eval_form || load_library)
472 #if defined(INTERNET_DOMAIN_SOCKETS)
473 connect_type = make_connection (hostarg, port, &s);
475 connect_type = make_connection (NULL, (u_short) 0, &s);
477 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
478 send_string (s, command);
481 send_string (s , "(load-library ");
482 send_string (s, clean_string(load_library));
483 send_string (s, ") ");
487 send_string (s, eval_form);
491 send_string (s, "(");
492 send_string (s, eval_function);
493 send_string (s, ")");
495 send_string (s, "))");
496 /* disconnect already sends EOT_STR */
498 if (connect_type == (int) CONN_IPC)
499 disconnect_from_ipc_server (s, msgp, batch && !quick);
500 #else /* !SYSV_IPC */
501 if (connect_type != (int) CONN_IPC)
502 disconnect_from_server (s, batch && !quick);
503 #endif /* !SYSV_IPC */
504 } /* eval_function || eval_form || load_library */
507 /* no sexp on the command line, so read it from stdin */
510 #if defined(INTERNET_DOMAIN_SOCKETS)
511 connect_type = make_connection (hostarg, port, &s);
513 connect_type = make_connection (NULL, (u_short) 0, &s);
515 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
516 send_string (s, command);
518 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ-1)) > 0)
521 send_string(s, buffer);
524 /* disconnect already sends EOT_STR */
526 if (connect_type == (int) CONN_IPC)
527 disconnect_from_ipc_server (s, msgp, batch && !quick);
528 #else /* !SYSV_IPC */
529 if (connect_type != (int) CONN_IPC)
530 disconnect_from_server (s, batch && !quick);
531 #endif /* !SYSV_IPC */
536 if (suppress_windows_system)
541 fprintf (stderr, "%s: Not connected to a tty", progname);
544 #if defined(INTERNET_DOMAIN_SOCKETS)
545 connect_type = make_connection (hostarg, port, &s);
547 connect_type = make_connection (NULL, (u_short) 0, &s);
549 send_string (s, "(gnuserv-eval '(emacs-pid))");
550 send_string (s, EOT_STR);
552 if (read_line (s, buffer) == 0)
554 fprintf (stderr, "%s: Could not establish Emacs procces id\n",
558 /* Don't do disconnect_from_server becasue we have already read
559 data, and disconnect doesn't do anything else. */
560 #ifndef INTERNET_DOMAIN_SOCKETS
561 if (connect_type == (int) CONN_IPC)
562 disconnect_from_ipc_server (s, msgp, FALSE);
563 #endif /* !SYSV_IPC */
565 emacs_pid = (pid_t)atol(buffer);
566 initialize_signals();
567 } /* suppress_windows_system */
569 #if defined(INTERNET_DOMAIN_SOCKETS)
570 connect_type = make_connection (hostarg, port, &s);
572 connect_type = make_connection (NULL, (u_short) 0, &s);
575 #ifdef INTERNET_DOMAIN_SOCKETS
576 if (connect_type == (int) CONN_INTERNET)
579 gethostname (thishost, HOSTNAMSZ);
581 { /* attempt to generate a path
583 if ((ptr = getenv ("GNU_NODE")) != NULL)
584 /* user specified a path */
585 strcpy (remotepath, ptr);
587 #if 0 /* This is really bogus... re-enable it if you must have it! */
588 #if defined (hp9000s300) || defined (hp9000s800)
589 else if (strcmp (thishost,hostarg))
590 { /* try /net/thishost */
591 strcpy (remotepath, "/net/"); /* (this fails using internet
593 strcat (remotepath, thishost);
599 { /* same machines, no need for path */
600 remotepath[0] = '\0'; /* default is the empty path */
602 #endif /* INTERNET_DOMAIN_SOCKETS */
605 if ((msgp = (struct msgbuf *)
606 malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
608 fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
612 msgp->mtext[0] = '\0'; /* ready for later strcats */
613 #endif /* SYSV_IPC */
615 if (suppress_windows_system)
617 char *term = getenv ("TERM");
620 fprintf (stderr, "%s: unknown terminal type\n", progname);
623 sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(",
624 clean_string (tty), clean_string (term), (int)getpid ());
626 else /* !suppress_windows_system */
629 sprintf (command, "(gnuserv-edit-files '(x %s) '(",
630 clean_string (display));
631 #ifdef HAVE_MS_WINDOWS
633 sprintf (command, "(gnuserv-edit-files '(mswindows nil) '(");
635 } /* !suppress_windows_system */
636 send_string (s, command);
643 if (i < argc - 1 && *argv[i] == '+')
644 starting_line = atoi (argv[i++]);
647 /* If the last argument is +something, treat it as a file. */
653 filename_expand (fullpath, argv[i]);
654 #ifdef INTERNET_DOMAIN_SOCKETS
655 path = malloc (strlen (remotepath) + strlen (fullpath) + 1);
656 sprintf (path, "%s%s", remotepath, fullpath);
658 path = my_strdup (fullpath);
660 sprintf (command, "(%d . %s)", starting_line, clean_string (path));
661 send_string (s, command);
665 sprintf (command, ")%s%s",
666 (quick || (nofiles && !suppress_windows_system)) ? " 'quick" : "",
667 view ? " 'view" : "");
668 send_string (s, command);
669 send_string (s, ")");
672 if (connect_type == (int) CONN_IPC)
673 disconnect_from_ipc_server (s, msgp, FALSE);
674 #else /* !SYSV_IPC */
675 if (connect_type != (int) CONN_IPC)
676 disconnect_from_server (s, FALSE);
677 #endif /* !SYSV_IPC */
685 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */