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 SXEmacs.
9 SXEmacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 SXEmacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 Author: Andy Norman (ange@hplb.hpl.hp.com), based on
23 'etc/emacsclient.c' from the GNU Emacs 18.52 distribution.
25 Please mail bugs and suggestions to the XEmacs maintainer.
28 /* #### This file should be a windows-mode, not console-mode program under
29 Windows. (i.e. its entry point should be WinMain.) gnuattach functionality,
30 to the extent it's used at all, should be retrieved using a script that
31 calls the i.exe wrapper program, to obtain stdio handles.
33 #### For that matter, both the functionality of gnuclient and gnuserv
34 should be merged into XEmacs itself using a -remote arg, just like
35 Netscape and other modern programs.
40 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
41 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
42 * GNUATTACH support added by Ben Wing <wing@xemacs.org>.
43 * Please see the note at the end of the README file for details.
45 * (If gnuserv came bundled with your emacs, the README file is probably
46 * ../etc/gnuserv.README relative to the directory containing this file)
51 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)
74 int main(int argc, char *argv[])
76 fprintf(stderr, "Sorry, the Emacs server is only "
77 "supported on systems that have\n");
78 fprintf(stderr, "Unix Domain sockets, Internet Domain "
79 "sockets or System V IPC.\n");
82 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
84 static char cwd[MAXPATHLEN + 2]; /* current working directory when calculated */
85 static char *cp = NULL; /* ptr into valid bit of cwd above */
87 static pid_t emacs_pid; /* Process id for emacs process */
89 void initialize_signals(void);
91 static void tell_emacs_to_resume(int sig)
93 char buffer[GSERV_BUFSZ + 1];
94 int s; /* socket / msqid to server */
95 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
98 /* Why is SYSV so retarded? */
99 /* We want emacs to realize that we are resuming */
101 signal(SIGCONT, tell_emacs_to_resume);
104 connect_type = make_connection(NULL, 0, &s);
106 sprintf(buffer, "(gnuserv-eval '(resume-pid-console %d))",
108 send_string(s, buffer);
111 if (connect_type == (int)CONN_IPC)
112 disconnect_from_ipc_server(s, msgp, FALSE);
113 #else /* !SYSV_IPC */
114 if (connect_type != (int)CONN_IPC)
115 disconnect_from_server(s, FALSE);
116 #endif /* !SYSV_IPC */
119 static void pass_signal_to_emacs(int sig)
121 if (kill(emacs_pid, sig) == -1) {
123 "gnuattach: Could not pass signal to emacs process\n");
126 initialize_signals();
129 void initialize_signals(void)
131 /* Set up signal handler to pass relevant signals to emacs process.
132 We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
133 Emacs, but I think it's better not to. I can see no reason why
134 Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
135 signal(SIGQUIT, pass_signal_to_emacs);
136 signal(SIGINT, pass_signal_to_emacs);
138 signal(SIGWINCH, pass_signal_to_emacs);
142 /* We want emacs to realize that we are resuming */
143 signal(SIGCONT, tell_emacs_to_resume);
148 get_current_working_directory -- return the cwd.
150 static char *get_current_working_directory(void)
152 if (cp == NULL) { /* haven't calculated it yet */
154 if (getcwd(cwd, MAXPATHLEN) == NULL)
157 #endif /* HAVE_GETCWD */
161 "%s: unable to get current working directory\n",
167 /* on some systems, cwd can look like '@machine/' ... */
168 /* ignore everything before the first '/' */
169 for (cp = cwd; *cp && *cp != '/'; ++cp) ;
175 } /* get_current_working_directory */
178 filename_expand -- try to convert the given filename into a fully-qualified
181 static void filename_expand(char *fullpath, char *filename, size_t fullsize)
182 /* fullpath - returned full pathname */
183 /* filename - filename to expand */
188 if (filename[0] && filename[0] == '/') {
189 /* Absolute (unix-style) pathname. Do nothing */
190 strncat(fullpath, filename, fullsize-1);
192 /* Assume relative Unix style path. Get the current directory
193 and prepend it. FIXME: need to fix the case of DOS paths like
194 "\foo", where we need to get the current drive. */
196 strncat(fullpath, get_current_working_directory(), fullsize-1);
197 len = strlen(fullpath);
199 if (len > 0 && fullpath[len - 1] == '/') /* trailing slash already? */
201 else if (len < fullsize-1)
202 strcat(fullpath, "/"); /* nope, append trailing slash */
203 /* Don't forget to add the filename! */
204 strncat(fullpath, filename, fullsize-len-1);
206 } /* filename_expand */
208 /* Encase the string in quotes, escape all the backslashes and quotes
210 static char *clean_string(const char *s)
217 for (const_p = s; *const_p; const_p++, i++) {
218 if (*const_p == '\\' || *const_p == '\"')
220 else if (*const_p == '\004')
225 p = res = (char *)malloc(i + 2 + 1);
227 for (; *s; p++, s++) {
252 #define GET_ARGUMENT(var, desc) do { \
253 if (*(p + 1)) (var) = p + 1; \
258 fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
267 /* A strdup imitation. */
268 static char *my_strdup(const char *s)
270 char *new_s = (char *)malloc(strlen(s) + 1);
276 int main(int argc, char *argv[])
278 int starting_line = 1; /* line to start editing at */
279 char command[MAXPATHLEN + 50]; /* emacs command buffer */
280 char fullpath[MAXPATHLEN + 1]; /* full pathname to file */
281 char *eval_form = NULL; /* form to evaluate with `-eval' */
282 char *eval_function = NULL; /* function to evaluate with `-f' */
283 char *load_library = NULL; /* library to load */
284 int quick = 0; /* quick edit, don't wait for user to
286 int batch = 0; /* batch mode */
287 int view = 0; /* view only. */
289 int errflg = 0; /* option error */
290 int s; /* socket / msqid to server */
291 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
293 int suppress_windows_system = 0;
294 char *display = NULL;
295 #ifdef INTERNET_DOMAIN_SOCKETS
296 char *hostarg = NULL; /* remote hostname */
298 char thishost[HOSTNAMSZ]; /* this hostname */
299 char remotepath[MAXPATHLEN + 1]; /* remote pathname */
300 int rflg = 0; /* pathname given on cmdline */
302 unsigned short port = 0; /* port to server */
303 #endif /* INTERNET_DOMAIN_SOCKETS */
304 char *path; /* used indiscriminately */
306 struct msgbuf *msgp; /* message */
307 #endif /* SYSV_IPC */
309 char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
310 char result[GSERV_BUFSZ + 1];
313 #ifdef INTERNET_DOMAIN_SOCKETS
314 memset(remotepath, 0, sizeof(remotepath));
315 #endif /* INTERNET_DOMAIN_SOCKETS */
317 progname = strrchr(argv[0], '/');
324 tmpdir = getenv("TMPDIR");
329 display = getenv("DISPLAY");
331 display = my_strdup(display);
333 suppress_windows_system = 1;
335 for (i = 1; argv[i] && !errflg; i++) {
338 else if (*argv[i] == '-'
339 && (*(argv[i] + 1) == '\0'
340 || (*(argv[i] + 1) == '-'
341 && *(argv[i] + 2) == '\0'))) {
347 if (!strcmp(argv[i], "-batch") || !strcmp(argv[i], "--batch"))
349 else if (!strcmp(argv[i], "-eval")
350 || !strcmp(argv[i], "--eval")) {
353 "%s: `-eval' must be followed by an argument\n",
358 } else if (!strcmp(argv[i], "-display")
359 || !strcmp(argv[i], "--display")) {
360 suppress_windows_system = 0;
363 "%s: `-display' must be followed by an argument\n",
369 /* no need to strdup. */
371 } else if (!strcmp(argv[i], "-nw"))
372 suppress_windows_system = 1;
374 /* Iterate over one-letter options. */
377 for (p = argv[i] + 1; *p && !over; p++) {
386 GET_ARGUMENT(eval_function, "-f");
389 GET_ARGUMENT(load_library, "-l");
391 #ifdef INTERNET_DOMAIN_SOCKETS
393 GET_ARGUMENT(hostarg, "-h");
396 GET_ARGUMENT(portarg, "-p");
397 port = atoi(portarg);
400 GET_ARGUMENT(remotearg, "-r");
401 strncpy(remotepath, remotearg, sizeof(remotepath));
402 remotepath[sizeof(remotepath)-1]='\0';
405 #endif /* INTERNET_DOMAIN_SOCKETS */
415 #ifdef INTERNET_DOMAIN_SOCKETS
416 "Usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
417 " [-batch] [-f function] [-eval form]\n"
418 " [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
419 #else /* !INTERNET_DOMAIN_SOCKETS */
420 "Usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
421 "[[+line] path] ...\n",
422 #endif /* !INTERNET_DOMAIN_SOCKETS */
426 if (batch && argv[i]) {
427 fprintf(stderr, "%s: Cannot specify `-batch' with file names\n",
431 #if defined(INTERNET_DOMAIN_SOCKETS)
432 if (suppress_windows_system && hostarg) {
433 fprintf(stderr, "%s: Remote editing is available only on X\n",
439 if (eval_function || eval_form || load_library) {
440 #if defined(INTERNET_DOMAIN_SOCKETS)
441 connect_type = make_connection(hostarg, port, &s);
443 connect_type = make_connection(NULL, 0, &s);
445 sprintf(command, "(gnuserv-eval%s '(progn ",
446 quick ? "-quickly" : "");
447 send_string(s, command);
449 send_string(s, "(load-library ");
450 send_string(s, clean_string(load_library));
451 send_string(s, ") ");
454 send_string(s, eval_form);
458 send_string(s, eval_function);
461 send_string(s, "))");
462 /* disconnect already sends EOT_STR */
464 if (connect_type == (int)CONN_IPC)
465 disconnect_from_ipc_server(s, msgp, batch && !quick);
466 #else /* !SYSV_IPC */
467 if (connect_type != (int)CONN_IPC)
468 disconnect_from_server(s, batch && !quick);
469 #endif /* !SYSV_IPC */
470 } /* eval_function || eval_form || load_library */
472 /* no sexp on the command line, so read it from stdin */
475 #if defined(INTERNET_DOMAIN_SOCKETS)
476 connect_type = make_connection(hostarg, port, &s);
478 connect_type = make_connection(NULL, 0, &s);
480 sprintf(command, "(gnuserv-eval%s '(progn ",
481 quick ? "-quickly" : "");
482 send_string(s, command);
484 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ - 1)) > 0) {
486 send_string(s, buffer);
488 send_string(s, "))");
489 /* disconnect already sends EOT_STR */
491 if (connect_type == (int)CONN_IPC)
492 disconnect_from_ipc_server(s, msgp, batch && !quick);
493 #else /* !SYSV_IPC */
494 if (connect_type != (int)CONN_IPC)
495 disconnect_from_server(s, batch && !quick);
496 #endif /* !SYSV_IPC */
500 if (suppress_windows_system) {
503 fprintf(stderr, "%s: Not connected to a tty",
507 #if defined(INTERNET_DOMAIN_SOCKETS)
508 connect_type = make_connection(hostarg, port, &s);
510 connect_type = make_connection(NULL, 0, &s);
512 send_string(s, "(gnuserv-eval '(emacs-pid))");
513 send_string(s, EOT_STR);
515 if (read_line(s, buffer) == 0) {
517 "%s: Could not establish Emacs process id\n",
521 /* Don't do disconnect_from_server because we have already read
522 data, and disconnect doesn't do anything else. */
524 if (connect_type == (int)CONN_IPC)
525 disconnect_from_ipc_server(s, msgp, FALSE);
526 #endif /* !SYSV_IPC */
528 emacs_pid = (pid_t) atol(buffer);
529 initialize_signals();
531 /* suppress_windows_system */
532 #if defined(INTERNET_DOMAIN_SOCKETS)
533 connect_type = make_connection(hostarg, port, &s);
535 connect_type = make_connection(NULL, 0, &s);
538 #ifdef INTERNET_DOMAIN_SOCKETS
539 if (connect_type == (int)CONN_INTERNET) {
541 gethostname(thishost, HOSTNAMSZ);
542 if (!rflg) { /* attempt to generate a path
544 if ((ptr = getenv("GNU_NODE")) != NULL) {
545 /* user specified a path */
546 strncpy(remotepath, ptr, sizeof(remotepath)-1);
547 remotepath[sizeof(remotepath)-1]='\0';
550 #if 0 /* This is really bogus... re-enable it if you must have it! */
551 #if defined (hp9000s300) || defined (hp9000s800)
552 else if (strcmp(thishost, hostarg)) { /* try /net/thishost */
553 strcpy(remotepath, "/net/"); /* (this fails using internet
555 strcat(remotepath, thishost);
559 } else { /* same machines, no need for path */
560 remotepath[0] = '\0'; /* default is the empty path */
562 #endif /* INTERNET_DOMAIN_SOCKETS */
565 if ((msgp = (struct msgbuf *)
566 malloc(sizeof *msgp + GSERV_BUFSZ)) == NULL) {
568 "%s: not enough memory for message buffer\n",
573 msgp->mtext[0] = '\0'; /* ready for later strcats */
574 #endif /* SYSV_IPC */
576 if (suppress_windows_system) {
577 char *term = getenv("TERM");
579 fprintf(stderr, "%s: unknown terminal type\n",
584 "(gnuserv-edit-files '(tty %s %s %d) '(",
585 clean_string(tty), clean_string(term),
587 } else { /* !suppress_windows_system */
590 #ifdef HAVE_X_WINDOWS
593 "(gnuserv-edit-files '(x %s) '(",
594 clean_string(display));
599 "(gnuserv-edit-files '(gtk nil) '(");
601 } /* !suppress_windows_system */
602 send_string(s, command);
607 for (; argv[i]; i++) {
608 if (i < argc - 1 && *argv[i] == '+')
609 starting_line = atoi(argv[i++]);
612 /* If the last argument is +something, treat it as a file. */
617 filename_expand(fullpath, argv[i], sizeof(fullpath));
618 #ifdef INTERNET_DOMAIN_SOCKETS
620 (char *)malloc(strlen(remotepath) +
621 strlen(fullpath) + 1);
622 sprintf(path, "%s%s", remotepath, fullpath);
624 path = my_strdup(fullpath);
626 sprintf(command, "(%d . %s)", starting_line,
628 send_string(s, command);
632 sprintf(command, ")%s%s",
635 && !suppress_windows_system)) ? " 'quick" : "",
636 view ? " 'view" : "");
637 send_string(s, command);
641 if (connect_type == (int)CONN_IPC)
642 disconnect_from_ipc_server(s, msgp, FALSE);
643 #else /* !SYSV_IPC */
644 if (connect_type != (int)CONN_IPC)
645 disconnect_from_server(s, FALSE);
646 #endif /* !SYSV_IPC */
654 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */