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.
30 /* #### This file should be a windows-mode, not console-mode program under
31 Windows. (i.e. its entry point should be WinMain.) gnuattach functionality,
32 to the extent it's used at all, should be retrieved using a script that
33 calls the i.exe wrapper program, to obtain stdio handles.
35 #### For that matter, both the functionality of gnuclient and gnuserv
36 should be merged into XEmacs itself using a -remote arg, just like
37 Netscape and other modern programs.
42 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
43 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
44 * GNUATTACH support added by Ben Wing <wing@xemacs.org>.
45 * Please see the note at the end of the README file for details.
47 * (If gnuserv came bundled with your emacs, the README file is probably
48 * ../etc/gnuserv.README relative to the directory containing this file)
53 char gnuserv_version[] = "gnuclient version " GNUSERV_VERSION;
59 #include <sys/types.h>
64 #endif /* HAVE_STRING_H */
68 #endif /* HAVE_UNISTD_H */
72 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
73 !defined(INTERNET_DOMAIN_SOCKETS)
75 main (int argc, char *argv[])
77 fprintf (stderr, "Sorry, the Emacs server is only "
78 "supported on systems that have\n");
79 fprintf (stderr, "Unix Domain sockets, Internet Domain "
80 "sockets or System V IPC.\n");
83 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
85 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
86 static char *cp = NULL; /* ptr into valid bit of cwd above */
88 static pid_t emacs_pid; /* Process id for emacs process */
90 void initialize_signals (void);
93 tell_emacs_to_resume (int sig)
95 char buffer[GSERV_BUFSZ+1];
96 int s; /* socket / msqid to server */
97 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
100 /* Why is SYSV so retarded? */
101 /* We want emacs to realize that we are resuming */
103 signal(SIGCONT, tell_emacs_to_resume);
106 connect_type = make_connection (NULL, 0, &s);
108 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
109 send_string(s, buffer);
112 if (connect_type == (int) CONN_IPC)
113 disconnect_from_ipc_server (s, msgp, FALSE);
114 #else /* !SYSV_IPC */
115 if (connect_type != (int) CONN_IPC)
116 disconnect_from_server (s, FALSE);
117 #endif /* !SYSV_IPC */
121 pass_signal_to_emacs (int sig)
123 if (kill (emacs_pid, sig) == -1)
125 fprintf (stderr, "gnuattach: Could not pass signal to emacs process\n");
128 initialize_signals ();
132 initialize_signals (void)
134 /* Set up signal handler to pass relevant signals to emacs process.
135 We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
136 Emacs, but I think it's better not to. I can see no reason why
137 Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
138 signal (SIGQUIT, pass_signal_to_emacs);
139 signal (SIGINT, pass_signal_to_emacs);
141 signal (SIGWINCH, pass_signal_to_emacs);
145 /* We want emacs to realize that we are resuming */
146 signal (SIGCONT, tell_emacs_to_resume);
152 get_current_working_directory -- return the cwd.
155 get_current_working_directory (void)
158 { /* haven't calculated it yet */
160 if (getcwd (cwd,MAXPATHLEN) == NULL)
162 if (getwd (cwd) == 0)
163 #endif /* HAVE_GETCWD */
166 fprintf (stderr, "%s: unable to get current working directory\n",
171 /* on some systems, cwd can look like '@machine/' ... */
172 /* ignore everything before the first '/' */
173 for (cp = cwd; *cp && *cp != '/'; ++cp)
180 } /* get_current_working_directory */
184 filename_expand -- try to convert the given filename into a fully-qualified
188 filename_expand (char *fullpath, char *filename)
189 /* fullpath - returned full pathname */
190 /* filename - filename to expand */
196 if (filename[0] && filename[0] == '/')
198 /* Absolute (unix-style) pathname. Do nothing */
199 strcat (fullpath, filename);
202 else if (filename[0] && filename[0] == '\\' &&
203 filename[1] && filename[1] == '\\')
205 /* This path includes the server name (something like
206 "\\server\path"), so we assume it's absolute. Do nothing to
208 strcat (fullpath, filename);
210 else if (filename[0] &&
211 filename[1] && filename[1] == ':' &&
212 filename[2] && filename[2] == '\\')
214 /* Absolute pathname with drive letter. Convert "<drive>:"
216 strcat (fullpath, "//");
217 strncat (fullpath, filename, 1);
218 strcat (fullpath, &filename[2]);
223 /* Assume relative Unix style path. Get the current directory
224 and prepend it. FIXME: need to fix the case of DOS paths like
225 "\foo", where we need to get the current drive. */
227 strcat (fullpath, get_current_working_directory ());
228 len = strlen (fullpath);
230 if (len > 0 && fullpath[len-1] == '/') /* trailing slash already? */
233 strcat (fullpath, "/"); /* nope, append trailing slash */
234 /* Don't forget to add the filename! */
235 strcat (fullpath,filename);
237 } /* filename_expand */
239 /* Encase the string in quotes, escape all the backslashes and quotes
242 clean_string (const char *s)
249 for (const_p = s; *const_p; const_p++, i++)
251 if (*const_p == '\\' || *const_p == '\"')
253 else if (*const_p == '\004')
258 p = res = (char *) malloc (i + 2 + 1);
287 #define GET_ARGUMENT(var, desc) do { \
288 if (*(p + 1)) (var) = p + 1; \
293 fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
302 /* A strdup imitation. */
304 my_strdup (const char *s)
306 char *new_s = (char *) malloc (strlen (s) + 1);
313 main (int argc, char *argv[])
315 int starting_line = 1; /* line to start editing at */
316 char command[MAXPATHLEN+50]; /* emacs command buffer */
317 char fullpath[MAXPATHLEN+1]; /* full pathname to file */
318 char *eval_form = NULL; /* form to evaluate with `-eval' */
319 char *eval_function = NULL; /* function to evaluate with `-f' */
320 char *load_library = NULL; /* library to load */
321 int quick = 0; /* quick edit, don't wait for user to
323 int batch = 0; /* batch mode */
324 int view = 0; /* view only. */
326 int errflg = 0; /* option error */
327 int s; /* socket / msqid to server */
328 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
330 int suppress_windows_system = 0;
331 char *display = NULL;
332 #ifdef INTERNET_DOMAIN_SOCKETS
333 char *hostarg = NULL; /* remote hostname */
335 char thishost[HOSTNAMSZ]; /* this hostname */
336 char remotepath[MAXPATHLEN+1]; /* remote pathname */
338 int rflg = 0; /* pathname given on cmdline */
340 unsigned short port = 0; /* port to server */
341 #endif /* INTERNET_DOMAIN_SOCKETS */
343 struct msgbuf *msgp; /* message */
344 #endif /* SYSV_IPC */
346 char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
347 char result[GSERV_BUFSZ + 1];
350 #ifdef INTERNET_DOMAIN_SOCKETS
351 memset (remotepath, 0, sizeof (remotepath));
352 #endif /* INTERNET_DOMAIN_SOCKETS */
354 progname = strrchr (argv[0], '/');
361 tmpdir = getenv ("TMPDIR");
366 display = getenv ("DISPLAY");
368 display = my_strdup (display);
369 #ifndef HAVE_MS_WINDOWS
371 suppress_windows_system = 1;
374 for (i = 1; argv[i] && !errflg; i++)
378 else if (*argv[i] == '-'
379 && (*(argv[i] + 1) == '\0'
380 || (*(argv[i] + 1) == '-' && *(argv[i] + 2) == '\0')))
387 if (!strcmp (argv[i], "-batch") || !strcmp (argv[i], "--batch"))
389 else if (!strcmp (argv[i], "-eval") || !strcmp (argv[i], "--eval"))
393 fprintf (stderr, "%s: `-eval' must be followed by an argument\n",
399 else if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "--display"))
401 suppress_windows_system = 0;
405 "%s: `-display' must be followed by an argument\n",
411 /* no need to strdup. */
414 else if (!strcmp (argv[i], "-nw"))
415 suppress_windows_system = 1;
418 /* Iterate over one-letter options. */
421 for (p = argv[i] + 1; *p && !over; p++)
432 GET_ARGUMENT (eval_function, "-f");
435 GET_ARGUMENT (load_library, "-l");
437 #ifdef INTERNET_DOMAIN_SOCKETS
439 GET_ARGUMENT (hostarg, "-h");
442 GET_ARGUMENT (portarg, "-p");
443 port = atoi (portarg);
446 GET_ARGUMENT (remotearg, "-r");
447 strcpy (remotepath, remotearg);
450 #endif /* INTERNET_DOMAIN_SOCKETS */
461 #ifdef INTERNET_DOMAIN_SOCKETS
462 "Usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
463 " [-batch] [-f function] [-eval form]\n"
464 " [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
465 #else /* !INTERNET_DOMAIN_SOCKETS */
466 "Usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
467 "[[+line] path] ...\n",
468 #endif /* !INTERNET_DOMAIN_SOCKETS */
472 if (batch && argv[i])
474 fprintf (stderr, "%s: Cannot specify `-batch' with file names\n",
478 if (suppress_windows_system && hostarg)
480 fprintf (stderr, "%s: Remote editing is available only on X\n",
486 if (eval_function || eval_form || load_library)
488 #if defined(INTERNET_DOMAIN_SOCKETS)
489 connect_type = make_connection (hostarg, port, &s);
491 connect_type = make_connection (NULL, 0, &s);
493 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
494 send_string (s, command);
497 send_string (s , "(load-library ");
498 send_string (s, clean_string(load_library));
499 send_string (s, ") ");
503 send_string (s, eval_form);
507 send_string (s, "(");
508 send_string (s, eval_function);
509 send_string (s, ")");
511 send_string (s, "))");
512 /* disconnect already sends EOT_STR */
514 if (connect_type == (int) CONN_IPC)
515 disconnect_from_ipc_server (s, msgp, batch && !quick);
516 #else /* !SYSV_IPC */
517 if (connect_type != (int) CONN_IPC)
518 disconnect_from_server (s, batch && !quick);
519 #endif /* !SYSV_IPC */
520 } /* eval_function || eval_form || load_library */
523 /* no sexp on the command line, so read it from stdin */
526 #if defined(INTERNET_DOMAIN_SOCKETS)
527 connect_type = make_connection (hostarg, port, &s);
529 connect_type = make_connection (NULL, 0, &s);
531 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
532 send_string (s, command);
534 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ-1)) > 0)
537 send_string(s, buffer);
540 /* disconnect already sends EOT_STR */
542 if (connect_type == (int) CONN_IPC)
543 disconnect_from_ipc_server (s, msgp, batch && !quick);
544 #else /* !SYSV_IPC */
545 if (connect_type != (int) CONN_IPC)
546 disconnect_from_server (s, batch && !quick);
547 #endif /* !SYSV_IPC */
552 if (suppress_windows_system)
557 fprintf (stderr, "%s: Not connected to a tty", progname);
560 #if defined(INTERNET_DOMAIN_SOCKETS)
561 connect_type = make_connection (hostarg, port, &s);
563 connect_type = make_connection (NULL, 0, &s);
565 send_string (s, "(gnuserv-eval '(emacs-pid))");
566 send_string (s, EOT_STR);
568 if (read_line (s, buffer) == 0)
570 fprintf (stderr, "%s: Could not establish Emacs process id\n",
574 /* Don't do disconnect_from_server because we have already read
575 data, and disconnect doesn't do anything else. */
576 #ifndef INTERNET_DOMAIN_SOCKETS
577 if (connect_type == (int) CONN_IPC)
578 disconnect_from_ipc_server (s, msgp, FALSE);
579 #endif /* !SYSV_IPC */
581 emacs_pid = (pid_t)atol(buffer);
582 initialize_signals();
583 } /* suppress_windows_system */
585 #if defined(INTERNET_DOMAIN_SOCKETS)
586 connect_type = make_connection (hostarg, port, &s);
588 connect_type = make_connection (NULL, 0, &s);
591 #ifdef INTERNET_DOMAIN_SOCKETS
592 if (connect_type == (int) CONN_INTERNET)
595 gethostname (thishost, HOSTNAMSZ);
597 { /* attempt to generate a path
599 if ((ptr = getenv ("GNU_NODE")) != NULL)
600 /* user specified a path */
601 strcpy (remotepath, ptr);
603 #if 0 /* This is really bogus... re-enable it if you must have it! */
604 #if defined (hp9000s300) || defined (hp9000s800)
605 else if (strcmp (thishost,hostarg))
606 { /* try /net/thishost */
607 strcpy (remotepath, "/net/"); /* (this fails using internet
609 strcat (remotepath, thishost);
615 { /* same machines, no need for path */
616 remotepath[0] = '\0'; /* default is the empty path */
618 #endif /* INTERNET_DOMAIN_SOCKETS */
621 if ((msgp = (struct msgbuf *)
622 malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
624 fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
628 msgp->mtext[0] = '\0'; /* ready for later strcats */
629 #endif /* SYSV_IPC */
631 if (suppress_windows_system)
633 char *term = getenv ("TERM");
636 fprintf (stderr, "%s: unknown terminal type\n", progname);
639 sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(",
640 clean_string (tty), clean_string (term), (int)getpid ());
642 else /* !suppress_windows_system */
646 #ifdef HAVE_X_WINDOWS
648 sprintf (command, "(gnuserv-edit-files '(x %s) '(",
649 clean_string (display));
653 strcpy (command, "(gnuserv-edit-files '(gtk nil) '(");
655 #ifdef HAVE_MS_WINDOWS
657 sprintf (command, "(gnuserv-edit-files '(mswindows nil) '(");
659 } /* !suppress_windows_system */
660 send_string (s, command);
667 if (i < argc - 1 && *argv[i] == '+')
668 starting_line = atoi (argv[i++]);
671 /* If the last argument is +something, treat it as a file. */
677 filename_expand (fullpath, argv[i]);
678 #ifdef INTERNET_DOMAIN_SOCKETS
679 path = (char *) malloc (strlen (remotepath) + strlen (fullpath) + 1);
680 sprintf (path, "%s%s", remotepath, fullpath);
682 path = my_strdup (fullpath);
684 sprintf (command, "(%d . %s)", starting_line, clean_string (path));
685 send_string (s, command);
689 sprintf (command, ")%s%s",
690 (quick || (nofiles && !suppress_windows_system)) ? " 'quick" : "",
691 view ? " 'view" : "");
692 send_string (s, command);
693 send_string (s, ")");
696 if (connect_type == (int) CONN_IPC)
697 disconnect_from_ipc_server (s, msgp, FALSE);
698 #else /* !SYSV_IPC */
699 if (connect_type != (int) CONN_IPC)
700 disconnect_from_server (s, FALSE);
701 #endif /* !SYSV_IPC */
709 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */