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.
29 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
30 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
31 * GNUATTACH support added by Ben Wing <wing@xemacs.org>.
32 * Please see the note at the end of the README file for details.
34 * (If gnuserv came bundled with your emacs, the README file is probably
35 * ../etc/gnuserv.README relative to the directory containing this file)
40 char gnuserv_version[] = "gnuclient version " GNUSERV_VERSION;
48 #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)
64 int main(int argc, char *argv[])
66 fprintf(stderr, "Sorry, the Emacs server is only "
67 "supported on systems that have\n");
68 fprintf(stderr, "Unix Domain sockets, Internet Domain "
69 "sockets or System V IPC.\n");
72 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
74 static char cwd[MAXPATHLEN + 2]; /* current working directory when calculated */
75 static char *cp = NULL; /* ptr into valid bit of cwd above */
77 static pid_t emacs_pid; /* Process id for emacs process */
80 initialize_signals(void);
82 static void tell_emacs_to_resume(int sig)
84 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 */
93 signal(SIGCONT, tell_emacs_to_resume);
96 connect_type = make_connection(NULL, 0, &s);
98 SNPRINTF(sz, buffer, sizeof(buffer),
99 "(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
100 send_string(s, buffer);
103 if (connect_type == (int)CONN_IPC)
104 disconnect_from_ipc_server(s, msgp, FALSE);
105 #else /* !SYSV_IPC */
106 if (connect_type != (int)CONN_IPC)
107 disconnect_from_server(s, FALSE);
108 #endif /* !SYSV_IPC */
112 pass_signal_to_emacs(int sig)
114 if (kill(emacs_pid, sig) == -1) {
116 "gnuclient: Could not pass signal to emacs process\n");
119 initialize_signals();
123 initialize_signals(void)
125 /* Set up signal handler to pass relevant signals to emacs process.
126 We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
127 Emacs, but I think it's better not to. I can see no reason why
128 Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
129 signal(SIGQUIT, pass_signal_to_emacs);
130 signal(SIGINT, pass_signal_to_emacs);
132 signal(SIGWINCH, pass_signal_to_emacs);
136 /* We want emacs to realize that we are resuming */
137 signal(SIGCONT, tell_emacs_to_resume);
142 get_current_working_directory -- return the cwd.
144 static char *get_current_working_directory(void)
146 if (cp == NULL) { /* haven't calculated it yet */
148 if (getcwd(cwd, MAXPATHLEN) == NULL)
151 #endif /* HAVE_GETCWD */
155 "%s: unable to get current working directory\n",
161 /* on some systems, cwd can look like '@machine/' ... */
162 /* ignore everything before the first '/' */
163 for (cp = cwd; *cp && *cp != '/'; ++cp) ;
169 } /* get_current_working_directory */
172 filename_expand -- try to convert the given filename into a fully-qualified
176 filename_expand(char *fullpath, char *filename, size_t fullsize)
178 /* fullpath - returned full pathname */
179 /* filename - filename to expand */
183 if (filename[0] && filename[0] == '/') {
184 /* Absolute (unix-style) pathname. Do nothing */
185 strncat(fullpath, filename, fullsize-1);
187 /* Assume relative Unix style path. Get the current directory
188 * and prepend it. FIXME: need to fix the case of DOS paths
189 * like "\foo", where we need to get the current drive. */
190 strncat(fullpath, get_current_working_directory(), fullsize-1);
191 len = strlen(fullpath);
193 /* trailing slash already? */
194 if (len > 0 && fullpath[len - 1] == '/') {
197 } else if (len < fullsize-1) {
198 /* nope, append trailing slash */
199 strcat(fullpath, "/");
201 /* Don't forget to add the filename! */
202 strncat(fullpath, filename, fullsize - len - 1);
207 /* Encase the string in quotes, escape all the backslashes and quotes
209 static char *clean_string(const char *s)
216 for (const_p = s; *const_p; const_p++, i++) {
217 if (*const_p == '\\' || *const_p == '\"')
219 else if (*const_p == '\004')
224 p = res = (char *)malloc(i + 2 + 1);
226 for (; *s; p++, s++) {
251 #define GET_ARGUMENT(var, desc) \
257 fprintf(stderr, "%s: `%s' must be " \
258 "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];
315 #ifdef INTERNET_DOMAIN_SOCKETS
316 memset(remotepath, 0, sizeof(remotepath));
317 #endif /* INTERNET_DOMAIN_SOCKETS */
319 progname = strrchr(argv[0], '/');
326 tmpdir = getenv("TMPDIR");
331 display = getenv("DISPLAY");
333 display = my_strdup(display);
335 suppress_windows_system = 1;
337 for (i = 1; argv[i] && !errflg; i++) {
340 else if (*argv[i] == '-'
341 && (*(argv[i] + 1) == '\0'
342 || (*(argv[i] + 1) == '-'
343 && *(argv[i] + 2) == '\0'))) {
349 if (!strcmp(argv[i], "-batch") || !strcmp(argv[i], "--batch"))
351 else if (!strcmp(argv[i], "-eval")
352 || !strcmp(argv[i], "--eval")) {
355 "%s: `-eval' must be followed by an argument\n",
360 } else if (!strcmp(argv[i], "-display")
361 || !strcmp(argv[i], "--display")) {
362 suppress_windows_system = 0;
365 "%s: `-display' must be followed by an argument\n",
371 /* no need to strdup. */
373 } else if (!strcmp(argv[i], "-nw"))
374 suppress_windows_system = 1;
376 /* Iterate over one-letter options. */
379 for (p = argv[i] + 1; *p && !over; p++) {
388 GET_ARGUMENT(eval_function, "-f");
391 GET_ARGUMENT(load_library, "-l");
393 #ifdef INTERNET_DOMAIN_SOCKETS
395 GET_ARGUMENT(hostarg, "-h");
398 GET_ARGUMENT(portarg, "-p");
399 port = atoi(portarg);
402 GET_ARGUMENT(remotearg, "-r");
403 strncpy(remotepath, remotearg, sizeof(remotepath));
404 remotepath[sizeof(remotepath)-1]='\0';
407 #endif /* INTERNET_DOMAIN_SOCKETS */
417 #ifdef INTERNET_DOMAIN_SOCKETS
418 "Usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
419 " [-batch] [-f function] [-eval form]\n"
420 " [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
421 #else /* !INTERNET_DOMAIN_SOCKETS */
422 "Usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
423 "[[+line] path] ...\n",
424 #endif /* !INTERNET_DOMAIN_SOCKETS */
428 if (batch && argv[i]) {
429 fprintf(stderr, "%s: Cannot specify `-batch' with file names\n",
433 #if defined(INTERNET_DOMAIN_SOCKETS)
434 if (suppress_windows_system && hostarg) {
435 fprintf(stderr, "%s: Remote editing is available only on X\n",
441 if (eval_function || eval_form || load_library) {
442 #if defined(INTERNET_DOMAIN_SOCKETS)
443 connect_type = make_connection(hostarg, port, &s);
445 connect_type = make_connection(NULL, 0, &s);
447 SNPRINTF(sz, command, sizeof(command),
448 "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
449 send_string(s, command);
451 send_string(s, "(load-library ");
452 send_string(s, clean_string(load_library));
453 send_string(s, ") ");
456 send_string(s, eval_form);
460 send_string(s, eval_function);
463 send_string(s, "))");
464 /* disconnect already sends EOT_STR */
466 if (connect_type == (int)CONN_IPC)
467 disconnect_from_ipc_server(s, msgp, batch && !quick);
468 #else /* !SYSV_IPC */
469 if (connect_type != (int)CONN_IPC)
470 disconnect_from_server(s, batch && !quick);
471 #endif /* !SYSV_IPC */
472 } /* eval_function || eval_form || load_library */
474 /* no sexp on the command line, so read it from stdin */
477 #if defined(INTERNET_DOMAIN_SOCKETS)
478 connect_type = make_connection(hostarg, port, &s);
480 connect_type = make_connection(NULL, 0, &s);
482 SNPRINTF(sz, command, sizeof(command),
483 "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
484 send_string(s, command);
486 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ - 1)) > 0) {
488 send_string(s, buffer);
490 send_string(s, "))");
491 /* disconnect already sends EOT_STR */
493 if (connect_type == (int)CONN_IPC)
494 disconnect_from_ipc_server(s, msgp, batch && !quick);
495 #else /* !SYSV_IPC */
496 if (connect_type != (int)CONN_IPC)
497 disconnect_from_server(s, batch && !quick);
498 #endif /* !SYSV_IPC */
502 if (suppress_windows_system) {
505 fprintf(stderr, "%s: Not connected to a tty",
509 #if defined(INTERNET_DOMAIN_SOCKETS)
510 connect_type = make_connection(hostarg, port, &s);
512 connect_type = make_connection(NULL, 0, &s);
514 send_string(s, "(gnuserv-eval '(emacs-pid))");
515 send_string(s, EOT_STR);
517 if (read_line(s, buffer) == 0) {
519 "%s: Could not establish Emacs process id\n",
523 /* Don't do disconnect_from_server because we have already read
524 data, and disconnect doesn't do anything else. */
526 if (connect_type == (int)CONN_IPC)
527 disconnect_from_ipc_server(s, msgp, FALSE);
528 #endif /* !SYSV_IPC */
530 emacs_pid = (pid_t) atol(buffer);
531 initialize_signals();
533 /* suppress_windows_system */
534 #if defined(INTERNET_DOMAIN_SOCKETS)
535 connect_type = make_connection(hostarg, port, &s);
537 connect_type = make_connection(NULL, 0, &s);
540 #ifdef INTERNET_DOMAIN_SOCKETS
541 if (connect_type == (int)CONN_INTERNET) {
543 gethostname(thishost, HOSTNAMSZ);
544 if (!rflg) { /* attempt to generate a path
546 if ((ptr = getenv("GNU_NODE")) != NULL) {
547 /* user specified a path */
548 strncpy(remotepath, ptr, sizeof(remotepath)-1);
549 remotepath[sizeof(remotepath)-1]='\0';
552 #if 0 /* This is really bogus... re-enable it if you must have it! */
553 #if defined (hp9000s300) || defined (hp9000s800)
554 else if (strcmp(thishost, hostarg)) { /* try /net/thishost */
555 strcpy(remotepath, "/net/"); /* (this fails using internet
557 strcat(remotepath, thishost);
561 } else { /* same machines, no need for path */
562 remotepath[0] = '\0'; /* default is the empty path */
564 #endif /* INTERNET_DOMAIN_SOCKETS */
567 if ((msgp = (struct msgbuf *)
568 malloc(sizeof *msgp + GSERV_BUFSZ)) == NULL) {
570 "%s: not enough memory for message buffer\n",
575 msgp->mtext[0] = '\0'; /* ready for later strcats */
576 #endif /* SYSV_IPC */
578 if (suppress_windows_system) {
579 char *term = getenv("TERM");
580 pid_t pid = getpid();
583 fprintf(stderr, "%s: unknown terminal type\n",
587 SNPRINTF(sz, command, sizeof(command),
588 "(gnuserv-edit-files '(tty %s %s %d) '(",
589 clean_string(tty), clean_string(term),
591 } else { /* !suppress_windows_system */
594 #ifdef HAVE_X_WINDOWS
596 SNPRINTF(sz, command, sizeof(command),
597 "(gnuserv-edit-files '(x %s) '(",
598 clean_string(display));
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++]);
613 /* If the last argument is +something, treat it as a
619 filename_expand(fullpath, argv[i], sizeof(fullpath));
620 #ifdef INTERNET_DOMAIN_SOCKETS
621 msz = strlen(remotepath) + strlen(fullpath) + 1;
622 path = (char*)malloc(msz);
623 SNPRINTF(sz, path, msz, "%s%s", remotepath, fullpath);
624 #else /* !INTERNET_DOMAIN_SOCKETS */
625 path = my_strdup(fullpath);
626 #endif /* INTERNET_DOMAIN_SOCKETS */
627 SNPRINTF(sz, command, sizeof(command),
628 "(%d . %s)", starting_line, clean_string(path));
629 send_string(s, command);
633 SNPRINTF(sz, command, sizeof(command), ")%s%s",
634 (quick || (nofiles && !suppress_windows_system))
637 view ? " 'view" : "");
638 send_string(s, command);
642 if (connect_type == (int)CONN_IPC)
643 disconnect_from_ipc_server(s, msgp, FALSE);
644 #else /* !SYSV_IPC */
645 if (connect_type != (int)CONN_IPC)
646 disconnect_from_server(s, FALSE);
647 #endif /* !SYSV_IPC */
655 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */