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>
65 #endif /* HAVE_STRING_H */
69 #endif /* HAVE_UNISTD_H */
73 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
74 !defined(INTERNET_DOMAIN_SOCKETS)
75 int 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);
92 static void tell_emacs_to_resume(int sig)
94 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 sz = snprintf(buffer, sizeof(buffer), "(gnuserv-eval '(resume-pid-console %d))",
110 assert(sz>=0 && sz<sizeof(buffer));
111 send_string(s, buffer);
114 if (connect_type == (int)CONN_IPC)
115 disconnect_from_ipc_server(s, msgp, FALSE);
116 #else /* !SYSV_IPC */
117 if (connect_type != (int)CONN_IPC)
118 disconnect_from_server(s, FALSE);
119 #endif /* !SYSV_IPC */
122 static void pass_signal_to_emacs(int sig)
124 if (kill(emacs_pid, sig) == -1) {
126 "gnuattach: Could not pass signal to emacs process\n");
129 initialize_signals();
132 void 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);
151 get_current_working_directory -- return the cwd.
153 static char *get_current_working_directory(void)
155 if (cp == NULL) { /* haven't calculated it yet */
157 if (getcwd(cwd, MAXPATHLEN) == NULL)
160 #endif /* HAVE_GETCWD */
164 "%s: unable to get current working directory\n",
170 /* on some systems, cwd can look like '@machine/' ... */
171 /* ignore everything before the first '/' */
172 for (cp = cwd; *cp && *cp != '/'; ++cp) ;
178 } /* get_current_working_directory */
181 filename_expand -- try to convert the given filename into a fully-qualified
184 static void filename_expand(char *fullpath, char *filename, size_t fullsize)
185 /* fullpath - returned full pathname */
186 /* filename - filename to expand */
191 if (filename[0] && filename[0] == '/') {
192 /* Absolute (unix-style) pathname. Do nothing */
193 strncat(fullpath, filename, fullsize-1);
195 /* Assume relative Unix style path. Get the current directory
196 and prepend it. FIXME: need to fix the case of DOS paths like
197 "\foo", where we need to get the current drive. */
199 strncat(fullpath, get_current_working_directory(), fullsize-1);
200 len = strlen(fullpath);
202 if (len > 0 && fullpath[len - 1] == '/') /* trailing slash already? */
204 else if (len < fullsize-1)
205 strcat(fullpath, "/"); /* nope, append trailing slash */
206 /* Don't forget to add the filename! */
207 strncat(fullpath, filename, fullsize-len-1);
209 } /* filename_expand */
211 /* Encase the string in quotes, escape all the backslashes and quotes
213 static char *clean_string(const char *s)
220 for (const_p = s; *const_p; const_p++, i++) {
221 if (*const_p == '\\' || *const_p == '\"')
223 else if (*const_p == '\004')
228 p = res = (char *)malloc(i + 2 + 1);
230 for (; *s; p++, s++) {
255 #define GET_ARGUMENT(var, desc) do { \
256 if (*(p + 1)) (var) = p + 1; \
261 fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
270 /* A strdup imitation. */
271 static char *my_strdup(const char *s)
273 char *new_s = (char *)malloc(strlen(s) + 1);
279 int main(int argc, char *argv[])
281 int starting_line = 1; /* line to start editing at */
282 char command[MAXPATHLEN + 50]; /* emacs command buffer */
283 char fullpath[MAXPATHLEN + 1]; /* full pathname to file */
284 char *eval_form = NULL; /* form to evaluate with `-eval' */
285 char *eval_function = NULL; /* function to evaluate with `-f' */
286 char *load_library = NULL; /* library to load */
287 int quick = 0; /* quick edit, don't wait for user to
289 int batch = 0; /* batch mode */
290 int view = 0; /* view only. */
292 int errflg = 0; /* option error */
293 int s; /* socket / msqid to server */
294 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
296 int suppress_windows_system = 0;
297 char *display = NULL;
298 #ifdef INTERNET_DOMAIN_SOCKETS
299 char *hostarg = NULL; /* remote hostname */
301 char thishost[HOSTNAMSZ]; /* this hostname */
302 char remotepath[MAXPATHLEN + 1]; /* remote pathname */
303 int rflg = 0; /* pathname given on cmdline */
305 unsigned short port = 0; /* port to server */
306 #endif /* INTERNET_DOMAIN_SOCKETS */
307 char *path; /* used indiscriminately */
309 struct msgbuf *msgp; /* message */
310 #endif /* SYSV_IPC */
312 char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
313 char result[GSERV_BUFSZ + 1];
317 #ifdef INTERNET_DOMAIN_SOCKETS
318 memset(remotepath, 0, sizeof(remotepath));
319 #endif /* INTERNET_DOMAIN_SOCKETS */
321 progname = strrchr(argv[0], '/');
328 tmpdir = getenv("TMPDIR");
333 display = getenv("DISPLAY");
335 display = my_strdup(display);
337 suppress_windows_system = 1;
339 for (i = 1; argv[i] && !errflg; i++) {
342 else if (*argv[i] == '-'
343 && (*(argv[i] + 1) == '\0'
344 || (*(argv[i] + 1) == '-'
345 && *(argv[i] + 2) == '\0'))) {
351 if (!strcmp(argv[i], "-batch") || !strcmp(argv[i], "--batch"))
353 else if (!strcmp(argv[i], "-eval")
354 || !strcmp(argv[i], "--eval")) {
357 "%s: `-eval' must be followed by an argument\n",
362 } else if (!strcmp(argv[i], "-display")
363 || !strcmp(argv[i], "--display")) {
364 suppress_windows_system = 0;
367 "%s: `-display' must be followed by an argument\n",
373 /* no need to strdup. */
375 } else if (!strcmp(argv[i], "-nw"))
376 suppress_windows_system = 1;
378 /* Iterate over one-letter options. */
381 for (p = argv[i] + 1; *p && !over; p++) {
390 GET_ARGUMENT(eval_function, "-f");
393 GET_ARGUMENT(load_library, "-l");
395 #ifdef INTERNET_DOMAIN_SOCKETS
397 GET_ARGUMENT(hostarg, "-h");
400 GET_ARGUMENT(portarg, "-p");
401 port = atoi(portarg);
404 GET_ARGUMENT(remotearg, "-r");
405 strncpy(remotepath, remotearg, sizeof(remotepath));
406 remotepath[sizeof(remotepath)-1]='\0';
409 #endif /* INTERNET_DOMAIN_SOCKETS */
419 #ifdef INTERNET_DOMAIN_SOCKETS
420 "Usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
421 " [-batch] [-f function] [-eval form]\n"
422 " [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
423 #else /* !INTERNET_DOMAIN_SOCKETS */
424 "Usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
425 "[[+line] path] ...\n",
426 #endif /* !INTERNET_DOMAIN_SOCKETS */
430 if (batch && argv[i]) {
431 fprintf(stderr, "%s: Cannot specify `-batch' with file names\n",
435 #if defined(INTERNET_DOMAIN_SOCKETS)
436 if (suppress_windows_system && hostarg) {
437 fprintf(stderr, "%s: Remote editing is available only on X\n",
443 if (eval_function || eval_form || load_library) {
444 #if defined(INTERNET_DOMAIN_SOCKETS)
445 connect_type = make_connection(hostarg, port, &s);
447 connect_type = make_connection(NULL, 0, &s);
449 sz = snprintf(command, sizeof(command), "(gnuserv-eval%s '(progn ",
450 quick ? "-quickly" : "");
451 assert(sz>=0 && sz<sizeof(command));
452 send_string(s, command);
454 send_string(s, "(load-library ");
455 send_string(s, clean_string(load_library));
456 send_string(s, ") ");
459 send_string(s, eval_form);
463 send_string(s, eval_function);
466 send_string(s, "))");
467 /* disconnect already sends EOT_STR */
469 if (connect_type == (int)CONN_IPC)
470 disconnect_from_ipc_server(s, msgp, batch && !quick);
471 #else /* !SYSV_IPC */
472 if (connect_type != (int)CONN_IPC)
473 disconnect_from_server(s, batch && !quick);
474 #endif /* !SYSV_IPC */
475 } /* eval_function || eval_form || load_library */
477 /* no sexp on the command line, so read it from stdin */
480 #if defined(INTERNET_DOMAIN_SOCKETS)
481 connect_type = make_connection(hostarg, port, &s);
483 connect_type = make_connection(NULL, 0, &s);
485 sz = snprintf(command, sizeof(command),
486 "(gnuserv-eval%s '(progn ",
487 quick ? "-quickly" : "");
488 assert(sz>=0 && sz<sizeof(command));
489 send_string(s, command);
491 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ - 1)) > 0) {
493 send_string(s, buffer);
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 */
507 if (suppress_windows_system) {
510 fprintf(stderr, "%s: Not connected to a tty",
514 #if defined(INTERNET_DOMAIN_SOCKETS)
515 connect_type = make_connection(hostarg, port, &s);
517 connect_type = make_connection(NULL, 0, &s);
519 send_string(s, "(gnuserv-eval '(emacs-pid))");
520 send_string(s, EOT_STR);
522 if (read_line(s, buffer) == 0) {
524 "%s: Could not establish Emacs process id\n",
528 /* Don't do disconnect_from_server because we have already read
529 data, and disconnect doesn't do anything else. */
531 if (connect_type == (int)CONN_IPC)
532 disconnect_from_ipc_server(s, msgp, FALSE);
533 #endif /* !SYSV_IPC */
535 emacs_pid = (pid_t) atol(buffer);
536 initialize_signals();
538 /* suppress_windows_system */
539 #if defined(INTERNET_DOMAIN_SOCKETS)
540 connect_type = make_connection(hostarg, port, &s);
542 connect_type = make_connection(NULL, 0, &s);
545 #ifdef INTERNET_DOMAIN_SOCKETS
546 if (connect_type == (int)CONN_INTERNET) {
548 gethostname(thishost, HOSTNAMSZ);
549 if (!rflg) { /* attempt to generate a path
551 if ((ptr = getenv("GNU_NODE")) != NULL) {
552 /* user specified a path */
553 strncpy(remotepath, ptr, sizeof(remotepath)-1);
554 remotepath[sizeof(remotepath)-1]='\0';
557 #if 0 /* This is really bogus... re-enable it if you must have it! */
558 #if defined (hp9000s300) || defined (hp9000s800)
559 else if (strcmp(thishost, hostarg)) { /* try /net/thishost */
560 strcpy(remotepath, "/net/"); /* (this fails using internet
562 strcat(remotepath, thishost);
566 } else { /* same machines, no need for path */
567 remotepath[0] = '\0'; /* default is the empty path */
569 #endif /* INTERNET_DOMAIN_SOCKETS */
572 if ((msgp = (struct msgbuf *)
573 malloc(sizeof *msgp + GSERV_BUFSZ)) == NULL) {
575 "%s: not enough memory for message buffer\n",
580 msgp->mtext[0] = '\0'; /* ready for later strcats */
581 #endif /* SYSV_IPC */
583 if (suppress_windows_system) {
584 char *term = getenv("TERM");
587 fprintf(stderr, "%s: unknown terminal type\n",
591 sz = snprintf(command, sizeof(command),
592 "(gnuserv-edit-files '(tty %s %s %d) '(",
593 clean_string(tty), clean_string(term),
595 assert(sz>=0 && sz<sizeof(command));
596 } else { /* !suppress_windows_system */
599 #ifdef HAVE_X_WINDOWS
601 int sz = snprintf(command, sizeof(command),
602 "(gnuserv-edit-files '(x %s) '(",
603 clean_string(display));
604 assert(sz>=0 && sz<sizeof(command));
610 "(gnuserv-edit-files '(gtk nil) '(");
612 } /* !suppress_windows_system */
613 send_string(s, command);
618 for (; argv[i]; i++) {
619 if (i < argc - 1 && *argv[i] == '+')
620 starting_line = atoi(argv[i++]);
623 /* If the last argument is +something, treat it as a file. */
628 filename_expand(fullpath, argv[i], sizeof(fullpath));
629 #ifdef INTERNET_DOMAIN_SOCKETS
630 msz = strlen(remotepath) + strlen(fullpath) + 1;
631 path = (char *)malloc(msz);
632 sz = snprintf(path, msz, "%s%s", remotepath, fullpath);
633 assert(sz>=0 && sz<msz);
635 path = my_strdup(fullpath);
637 sz = snprintf(command, sizeof(command),
638 "(%d . %s)", starting_line,
640 assert(sz>=0 && sz<sizeof(command));
641 send_string(s, command);
645 sz = snprintf(command, sizeof(command),
649 && !suppress_windows_system)) ? " 'quick" : "",
650 view ? " 'view" : "");
651 assert(sz>=0 && sz<sizeof(command));
652 send_string(s, command);
656 if (connect_type == (int)CONN_IPC)
657 disconnect_from_ipc_server(s, msgp, FALSE);
658 #else /* !SYSV_IPC */
659 if (connect_type != (int)CONN_IPC)
660 disconnect_from_server(s, FALSE);
661 #endif /* !SYSV_IPC */
669 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */