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)
52 /* Hand-munged RCS header */
53 static char rcsid [] = "!Header: gnuclient.c,v 2.2 95/12/12 01:39:21 wing nene !";
61 #include <sys/types.h>
66 #endif /* HAVE_STRING_H */
70 #endif /* HAVE_UNISTD_H */
74 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
75 !defined(INTERNET_DOMAIN_SOCKETS)
77 main (int argc, char *argv[])
79 fprintf (stderr, "Sorry, the Emacs server is only "
80 "supported on systems that have\n");
81 fprintf (stderr, "Unix Domain sockets, Internet Domain "
82 "sockets or System V IPC.\n");
85 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
87 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
88 static char *cp = NULL; /* ptr into valid bit of cwd above */
90 static pid_t emacs_pid; /* Process id for emacs process */
92 void initialize_signals (void);
95 tell_emacs_to_resume (int sig)
97 char buffer[GSERV_BUFSZ+1];
98 int s; /* socket / msqid to server */
99 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
102 /* Why is SYSV so retarded? */
103 /* We want emacs to realize that we are resuming */
105 signal(SIGCONT, tell_emacs_to_resume);
108 connect_type = make_connection (NULL, (u_short) 0, &s);
110 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
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 */
123 pass_signal_to_emacs (int sig)
125 if (kill (emacs_pid, sig) == -1)
127 fprintf (stderr, "gnuattach: Could not pass signal to emacs process\n");
130 initialize_signals ();
134 initialize_signals (void)
136 /* Set up signal handler to pass relevant signals to emacs process.
137 We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
138 Emacs, but I think it's better not to. I can see no reason why
139 Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
140 signal (SIGQUIT, pass_signal_to_emacs);
141 signal (SIGINT, pass_signal_to_emacs);
143 signal (SIGWINCH, pass_signal_to_emacs);
147 /* We want emacs to realize that we are resuming */
148 signal (SIGCONT, tell_emacs_to_resume);
154 get_current_working_directory -- return the cwd.
157 get_current_working_directory (void)
160 { /* haven't calculated it yet */
162 if (getwd (cwd) == 0)
164 if (getcwd (cwd,MAXPATHLEN) == NULL)
168 fprintf (stderr, "%s: unable to get current working directory\n",
173 /* on some systems, cwd can look like '@machine/' ... */
174 /* ignore everything before the first '/' */
175 for (cp = cwd; *cp && *cp != '/'; ++cp)
182 } /* get_current_working_directory */
186 filename_expand -- try to convert the given filename into a fully-qualified
190 filename_expand (char *fullpath, char *filename)
191 /* fullpath - returned full pathname */
192 /* filename - filename to expand */
198 if (filename[0] && filename[0] == '/')
200 /* Absolute (unix-style) pathname. Do nothing */
201 strcat (fullpath, filename);
204 else if (filename[0] && filename[0] == '\\' &&
205 filename[1] && filename[1] == '\\')
207 /* This path includes the server name (something like
208 "\\server\path"), so we assume it's absolute. Do nothing to
210 strcat (fullpath, filename);
212 else if (filename[0] &&
213 filename[1] && filename[1] == ':' &&
214 filename[2] && filename[2] == '\\')
216 /* Absolute pathname with drive letter. Convert "<drive>:"
218 strcat (fullpath, "//");
219 strncat (fullpath, filename, 1);
220 strcat (fullpath, &filename[2]);
225 /* Assume relative Unix style path. Get the current directory
226 and prepend it. FIXME: need to fix the case of DOS paths like
227 "\foo", where we need to get the current drive. */
229 strcat (fullpath, get_current_working_directory ());
230 len = strlen (fullpath);
232 if (len > 0 && fullpath[len-1] == '/') /* trailing slash already? */
235 strcat (fullpath, "/"); /* nope, append trailing slash */
236 /* Don't forget to add the filename! */
237 strcat (fullpath,filename);
239 } /* filename_expand */
241 /* Encase the string in quotes, escape all the backslashes and quotes
244 clean_string (const char *s)
251 for (const_p = s; *const_p; const_p++, i++)
253 if (*const_p == '\\' || *const_p == '\"')
255 else if (*const_p == '\004')
260 p = res = (char *) malloc (i + 2 + 1);
289 #define GET_ARGUMENT(var, desc) do { \
290 if (*(p + 1)) (var) = p + 1; \
295 fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
304 /* A strdup imitation. */
306 my_strdup (const char *s)
308 char *new_s = (char *) malloc (strlen (s) + 1);
315 main (int argc, char *argv[])
317 int starting_line = 1; /* line to start editing at */
318 char command[MAXPATHLEN+50]; /* emacs command buffer */
319 char fullpath[MAXPATHLEN+1]; /* full pathname to file */
320 char *eval_form = NULL; /* form to evaluate with `-eval' */
321 char *eval_function = NULL; /* function to evaluate with `-f' */
322 char *load_library = NULL; /* library to load */
323 int quick = 0; /* quick edit, don't wait for user to
325 int batch = 0; /* batch mode */
326 int view = 0; /* view only. */
328 int errflg = 0; /* option error */
329 int s; /* socket / msqid to server */
330 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
332 int suppress_windows_system = 0;
333 char *display = NULL;
334 #ifdef INTERNET_DOMAIN_SOCKETS
335 char *hostarg = NULL; /* remote hostname */
337 char thishost[HOSTNAMSZ]; /* this hostname */
338 char remotepath[MAXPATHLEN+1]; /* remote pathname */
340 int rflg = 0; /* pathname given on cmdline */
342 u_short port = 0; /* port to server */
343 #endif /* INTERNET_DOMAIN_SOCKETS */
345 struct msgbuf *msgp; /* message */
346 #endif /* SYSV_IPC */
348 char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
349 char result[GSERV_BUFSZ + 1];
352 #ifdef INTERNET_DOMAIN_SOCKETS
353 memset (remotepath, 0, sizeof (remotepath));
354 #endif /* INTERNET_DOMAIN_SOCKETS */
356 progname = strrchr (argv[0], '/');
363 tmpdir = getenv ("TMPDIR");
368 display = getenv ("DISPLAY");
370 display = my_strdup (display);
371 #ifndef HAVE_MS_WINDOWS
373 suppress_windows_system = 1;
376 for (i = 1; argv[i] && !errflg; i++)
380 else if (*argv[i] == '-'
381 && (*(argv[i] + 1) == '\0'
382 || (*(argv[i] + 1) == '-' && *(argv[i] + 2) == '\0')))
389 if (!strcmp (argv[i], "-batch") || !strcmp (argv[i], "--batch"))
391 else if (!strcmp (argv[i], "-eval") || !strcmp (argv[i], "--eval"))
395 fprintf (stderr, "%s: `-eval' must be followed by an argument\n",
401 else if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "--display"))
403 suppress_windows_system = 0;
407 "%s: `-display' must be followed by an argument\n",
413 /* no need to strdup. */
416 else if (!strcmp (argv[i], "-nw"))
417 suppress_windows_system = 1;
420 /* Iterate over one-letter options. */
423 for (p = argv[i] + 1; *p && !over; p++)
434 GET_ARGUMENT (eval_function, "-f");
437 GET_ARGUMENT (load_library, "-l");
439 #ifdef INTERNET_DOMAIN_SOCKETS
441 GET_ARGUMENT (hostarg, "-h");
444 GET_ARGUMENT (portarg, "-p");
445 port = atoi (portarg);
448 GET_ARGUMENT (remotearg, "-r");
449 strcpy (remotepath, remotearg);
452 #endif /* INTERNET_DOMAIN_SOCKETS */
463 #ifdef INTERNET_DOMAIN_SOCKETS
464 "usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
465 " [-batch] [-f function] [-eval form]\n"
466 " [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
467 #else /* !INTERNET_DOMAIN_SOCKETS */
468 "usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
469 "[[+line] path] ...\n",
470 #endif /* !INTERNET_DOMAIN_SOCKETS */
474 if (batch && argv[i])
476 fprintf (stderr, "%s: Cannot specify `-batch' with file names\n",
480 if (suppress_windows_system && hostarg)
482 fprintf (stderr, "%s: Remote editing is available only on X\n",
488 if (eval_function || eval_form || load_library)
490 #if defined(INTERNET_DOMAIN_SOCKETS)
491 connect_type = make_connection (hostarg, port, &s);
493 connect_type = make_connection (NULL, (u_short) 0, &s);
495 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
496 send_string (s, command);
499 send_string (s , "(load-library ");
500 send_string (s, clean_string(load_library));
501 send_string (s, ") ");
505 send_string (s, eval_form);
509 send_string (s, "(");
510 send_string (s, eval_function);
511 send_string (s, ")");
513 send_string (s, "))");
514 /* disconnect already sends EOT_STR */
516 if (connect_type == (int) CONN_IPC)
517 disconnect_from_ipc_server (s, msgp, batch && !quick);
518 #else /* !SYSV_IPC */
519 if (connect_type != (int) CONN_IPC)
520 disconnect_from_server (s, batch && !quick);
521 #endif /* !SYSV_IPC */
522 } /* eval_function || eval_form || load_library */
525 /* no sexp on the command line, so read it from stdin */
528 #if defined(INTERNET_DOMAIN_SOCKETS)
529 connect_type = make_connection (hostarg, port, &s);
531 connect_type = make_connection (NULL, (u_short) 0, &s);
533 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
534 send_string (s, command);
536 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ-1)) > 0)
539 send_string(s, buffer);
542 /* disconnect already sends EOT_STR */
544 if (connect_type == (int) CONN_IPC)
545 disconnect_from_ipc_server (s, msgp, batch && !quick);
546 #else /* !SYSV_IPC */
547 if (connect_type != (int) CONN_IPC)
548 disconnect_from_server (s, batch && !quick);
549 #endif /* !SYSV_IPC */
554 if (suppress_windows_system)
559 fprintf (stderr, "%s: Not connected to a tty", progname);
562 #if defined(INTERNET_DOMAIN_SOCKETS)
563 connect_type = make_connection (hostarg, port, &s);
565 connect_type = make_connection (NULL, (u_short) 0, &s);
567 send_string (s, "(gnuserv-eval '(emacs-pid))");
568 send_string (s, EOT_STR);
570 if (read_line (s, buffer) == 0)
572 fprintf (stderr, "%s: Could not establish Emacs process id\n",
576 /* Don't do disconnect_from_server becasue we have already read
577 data, and disconnect doesn't do anything else. */
578 #ifndef INTERNET_DOMAIN_SOCKETS
579 if (connect_type == (int) CONN_IPC)
580 disconnect_from_ipc_server (s, msgp, FALSE);
581 #endif /* !SYSV_IPC */
583 emacs_pid = (pid_t)atol(buffer);
584 initialize_signals();
585 } /* suppress_windows_system */
587 #if defined(INTERNET_DOMAIN_SOCKETS)
588 connect_type = make_connection (hostarg, port, &s);
590 connect_type = make_connection (NULL, (u_short) 0, &s);
593 #ifdef INTERNET_DOMAIN_SOCKETS
594 if (connect_type == (int) CONN_INTERNET)
597 gethostname (thishost, HOSTNAMSZ);
599 { /* attempt to generate a path
601 if ((ptr = getenv ("GNU_NODE")) != NULL)
602 /* user specified a path */
603 strcpy (remotepath, ptr);
605 #if 0 /* This is really bogus... re-enable it if you must have it! */
606 #if defined (hp9000s300) || defined (hp9000s800)
607 else if (strcmp (thishost,hostarg))
608 { /* try /net/thishost */
609 strcpy (remotepath, "/net/"); /* (this fails using internet
611 strcat (remotepath, thishost);
617 { /* same machines, no need for path */
618 remotepath[0] = '\0'; /* default is the empty path */
620 #endif /* INTERNET_DOMAIN_SOCKETS */
623 if ((msgp = (struct msgbuf *)
624 malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
626 fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
630 msgp->mtext[0] = '\0'; /* ready for later strcats */
631 #endif /* SYSV_IPC */
633 if (suppress_windows_system)
635 char *term = getenv ("TERM");
638 fprintf (stderr, "%s: unknown terminal type\n", progname);
641 sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(",
642 clean_string (tty), clean_string (term), (int)getpid ());
644 else /* !suppress_windows_system */
647 sprintf (command, "(gnuserv-edit-files '(x %s) '(",
648 clean_string (display));
649 #ifdef HAVE_MS_WINDOWS
651 sprintf (command, "(gnuserv-edit-files '(mswindows nil) '(");
653 } /* !suppress_windows_system */
654 send_string (s, command);
661 if (i < argc - 1 && *argv[i] == '+')
662 starting_line = atoi (argv[i++]);
665 /* If the last argument is +something, treat it as a file. */
671 filename_expand (fullpath, argv[i]);
672 #ifdef INTERNET_DOMAIN_SOCKETS
673 path = (char *) malloc (strlen (remotepath) + strlen (fullpath) + 1);
674 sprintf (path, "%s%s", remotepath, fullpath);
676 path = my_strdup (fullpath);
678 sprintf (command, "(%d . %s)", starting_line, clean_string (path));
679 send_string (s, command);
683 sprintf (command, ")%s%s",
684 (quick || (nofiles && !suppress_windows_system)) ? " 'quick" : "",
685 view ? " 'view" : "");
686 send_string (s, command);
687 send_string (s, ")");
690 if (connect_type == (int) CONN_IPC)
691 disconnect_from_ipc_server (s, msgp, FALSE);
692 #else /* !SYSV_IPC */
693 if (connect_type != (int) CONN_IPC)
694 disconnect_from_server (s, FALSE);
695 #endif /* !SYSV_IPC */
703 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */