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 extern void cygwin_conv_to_posix_path(const char *path, char *posix_path);
57 char gnuserv_version[] = "gnuclient version " GNUSERV_VERSION;
63 #include <sys/types.h>
68 #endif /* HAVE_STRING_H */
72 #endif /* HAVE_UNISTD_H */
76 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
77 !defined(INTERNET_DOMAIN_SOCKETS)
79 main (int argc, char *argv[])
81 fprintf (stderr, "Sorry, the Emacs server is only "
82 "supported on systems that have\n");
83 fprintf (stderr, "Unix Domain sockets, Internet Domain "
84 "sockets or System V IPC.\n");
87 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
89 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
90 static char *cp = NULL; /* ptr into valid bit of cwd above */
92 static pid_t emacs_pid; /* Process id for emacs process */
94 void initialize_signals (void);
97 tell_emacs_to_resume (int sig)
99 char buffer[GSERV_BUFSZ+1];
100 int s; /* socket / msqid to server */
101 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
104 /* Why is SYSV so retarded? */
105 /* We want emacs to realize that we are resuming */
107 signal(SIGCONT, tell_emacs_to_resume);
110 connect_type = make_connection (NULL, 0, &s);
112 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
113 send_string(s, buffer);
116 if (connect_type == (int) CONN_IPC)
117 disconnect_from_ipc_server (s, msgp, FALSE);
118 #else /* !SYSV_IPC */
119 if (connect_type != (int) CONN_IPC)
120 disconnect_from_server (s, FALSE);
121 #endif /* !SYSV_IPC */
125 pass_signal_to_emacs (int sig)
127 if (kill (emacs_pid, sig) == -1)
129 fprintf (stderr, "gnuattach: Could not pass signal to emacs process\n");
132 initialize_signals ();
136 initialize_signals (void)
138 /* Set up signal handler to pass relevant signals to emacs process.
139 We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
140 Emacs, but I think it's better not to. I can see no reason why
141 Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
142 signal (SIGQUIT, pass_signal_to_emacs);
143 signal (SIGINT, pass_signal_to_emacs);
145 signal (SIGWINCH, pass_signal_to_emacs);
149 /* We want emacs to realize that we are resuming */
150 signal (SIGCONT, tell_emacs_to_resume);
156 get_current_working_directory -- return the cwd.
159 get_current_working_directory (void)
162 { /* haven't calculated it yet */
164 if (getcwd (cwd,MAXPATHLEN) == NULL)
166 if (getwd (cwd) == 0)
167 #endif /* HAVE_GETCWD */
170 fprintf (stderr, "%s: unable to get current working directory\n",
175 /* on some systems, cwd can look like '@machine/' ... */
176 /* ignore everything before the first '/' */
177 for (cp = cwd; *cp && *cp != '/'; ++cp)
184 } /* get_current_working_directory */
188 filename_expand -- try to convert the given filename into a fully-qualified
192 filename_expand (char *fullpath, char *filename)
193 /* fullpath - returned full pathname */
194 /* filename - filename to expand */
197 char cygwinFilename[MAXPATHLEN+1];
205 If we're in cygwin, just convert it and let the unix stuff handle it.
207 cygwin_conv_to_posix_path(filename, cygwinFilename);
208 filename = cygwinFilename;
211 if (filename[0] && filename[0] == '/')
213 /* Absolute (unix-style) pathname. Do nothing */
214 strcat (fullpath, filename);
218 /* Assume relative Unix style path. Get the current directory
219 and prepend it. FIXME: need to fix the case of DOS paths like
220 "\foo", where we need to get the current drive. */
222 strcat (fullpath, get_current_working_directory ());
223 len = strlen (fullpath);
225 if (len > 0 && fullpath[len-1] == '/') /* trailing slash already? */
228 strcat (fullpath, "/"); /* nope, append trailing slash */
229 /* Don't forget to add the filename! */
230 strcat (fullpath,filename);
232 } /* filename_expand */
234 /* Encase the string in quotes, escape all the backslashes and quotes
237 clean_string (const char *s)
244 for (const_p = s; *const_p; const_p++, i++)
246 if (*const_p == '\\' || *const_p == '\"')
248 else if (*const_p == '\004')
253 p = res = (char *) malloc (i + 2 + 1);
282 #define GET_ARGUMENT(var, desc) do { \
283 if (*(p + 1)) (var) = p + 1; \
288 fprintf (stderr, "%s: `%s' must be followed by an argument\n", \
297 /* A strdup imitation. */
299 my_strdup (const char *s)
301 char *new_s = (char *) malloc (strlen (s) + 1);
308 main (int argc, char *argv[])
310 int starting_line = 1; /* line to start editing at */
311 char command[MAXPATHLEN+50]; /* emacs command buffer */
312 char fullpath[MAXPATHLEN+1]; /* full pathname to file */
313 char *eval_form = NULL; /* form to evaluate with `-eval' */
314 char *eval_function = NULL; /* function to evaluate with `-f' */
315 char *load_library = NULL; /* library to load */
316 int quick = 0; /* quick edit, don't wait for user to
318 int batch = 0; /* batch mode */
319 int view = 0; /* view only. */
321 int errflg = 0; /* option error */
322 int s; /* socket / msqid to server */
323 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
325 int suppress_windows_system = 0;
326 char *display = NULL;
327 #ifdef INTERNET_DOMAIN_SOCKETS
328 char *hostarg = NULL; /* remote hostname */
330 char thishost[HOSTNAMSZ]; /* this hostname */
331 char remotepath[MAXPATHLEN+1]; /* remote pathname */
333 int rflg = 0; /* pathname given on cmdline */
335 unsigned short port = 0; /* port to server */
336 #endif /* INTERNET_DOMAIN_SOCKETS */
338 struct msgbuf *msgp; /* message */
339 #endif /* SYSV_IPC */
341 char buffer[GSERV_BUFSZ + 1]; /* buffer to read pid */
342 char result[GSERV_BUFSZ + 1];
345 #ifdef INTERNET_DOMAIN_SOCKETS
346 memset (remotepath, 0, sizeof (remotepath));
347 #endif /* INTERNET_DOMAIN_SOCKETS */
349 progname = strrchr (argv[0], '/');
356 tmpdir = getenv ("TMPDIR");
361 display = getenv ("DISPLAY");
363 display = my_strdup (display);
364 #ifndef HAVE_MS_WINDOWS
366 suppress_windows_system = 1;
369 for (i = 1; argv[i] && !errflg; i++)
373 else if (*argv[i] == '-'
374 && (*(argv[i] + 1) == '\0'
375 || (*(argv[i] + 1) == '-' && *(argv[i] + 2) == '\0')))
382 if (!strcmp (argv[i], "-batch") || !strcmp (argv[i], "--batch"))
384 else if (!strcmp (argv[i], "-eval") || !strcmp (argv[i], "--eval"))
388 fprintf (stderr, "%s: `-eval' must be followed by an argument\n",
394 else if (!strcmp (argv[i], "-display") || !strcmp (argv[i], "--display"))
396 suppress_windows_system = 0;
400 "%s: `-display' must be followed by an argument\n",
406 /* no need to strdup. */
409 else if (!strcmp (argv[i], "-nw"))
410 suppress_windows_system = 1;
413 /* Iterate over one-letter options. */
416 for (p = argv[i] + 1; *p && !over; p++)
427 GET_ARGUMENT (eval_function, "-f");
430 GET_ARGUMENT (load_library, "-l");
432 #ifdef INTERNET_DOMAIN_SOCKETS
434 GET_ARGUMENT (hostarg, "-h");
437 GET_ARGUMENT (portarg, "-p");
438 port = atoi (portarg);
441 GET_ARGUMENT (remotearg, "-r");
442 strcpy (remotepath, remotearg);
445 #endif /* INTERNET_DOMAIN_SOCKETS */
456 #ifdef INTERNET_DOMAIN_SOCKETS
457 "Usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
458 " [-batch] [-f function] [-eval form]\n"
459 " [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
460 #else /* !INTERNET_DOMAIN_SOCKETS */
461 "Usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
462 "[[+line] path] ...\n",
463 #endif /* !INTERNET_DOMAIN_SOCKETS */
467 if (batch && argv[i])
469 fprintf (stderr, "%s: Cannot specify `-batch' with file names\n",
473 if (suppress_windows_system && hostarg)
475 fprintf (stderr, "%s: Remote editing is available only on X\n",
481 if (eval_function || eval_form || load_library)
483 #if defined(INTERNET_DOMAIN_SOCKETS)
484 connect_type = make_connection (hostarg, port, &s);
486 connect_type = make_connection (NULL, 0, &s);
488 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
489 send_string (s, command);
492 send_string (s , "(load-library ");
493 send_string (s, clean_string(load_library));
494 send_string (s, ") ");
498 send_string (s, eval_form);
502 send_string (s, "(");
503 send_string (s, eval_function);
504 send_string (s, ")");
506 send_string (s, "))");
507 /* disconnect already sends EOT_STR */
509 if (connect_type == (int) CONN_IPC)
510 disconnect_from_ipc_server (s, msgp, batch && !quick);
511 #else /* !SYSV_IPC */
512 if (connect_type != (int) CONN_IPC)
513 disconnect_from_server (s, batch && !quick);
514 #endif /* !SYSV_IPC */
515 } /* eval_function || eval_form || load_library */
518 /* no sexp on the command line, so read it from stdin */
521 #if defined(INTERNET_DOMAIN_SOCKETS)
522 connect_type = make_connection (hostarg, port, &s);
524 connect_type = make_connection (NULL, 0, &s);
526 sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
527 send_string (s, command);
529 while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ-1)) > 0)
532 send_string(s, buffer);
535 /* disconnect already sends EOT_STR */
537 if (connect_type == (int) CONN_IPC)
538 disconnect_from_ipc_server (s, msgp, batch && !quick);
539 #else /* !SYSV_IPC */
540 if (connect_type != (int) CONN_IPC)
541 disconnect_from_server (s, batch && !quick);
542 #endif /* !SYSV_IPC */
547 if (suppress_windows_system)
552 fprintf (stderr, "%s: Not connected to a tty", progname);
555 #if defined(INTERNET_DOMAIN_SOCKETS)
556 connect_type = make_connection (hostarg, port, &s);
558 connect_type = make_connection (NULL, 0, &s);
560 send_string (s, "(gnuserv-eval '(emacs-pid))");
561 send_string (s, EOT_STR);
563 if (read_line (s, buffer) == 0)
565 fprintf (stderr, "%s: Could not establish Emacs process id\n",
569 /* Don't do disconnect_from_server because we have already read
570 data, and disconnect doesn't do anything else. */
571 #ifndef INTERNET_DOMAIN_SOCKETS
572 if (connect_type == (int) CONN_IPC)
573 disconnect_from_ipc_server (s, msgp, FALSE);
574 #endif /* !SYSV_IPC */
576 emacs_pid = (pid_t)atol(buffer);
577 initialize_signals();
578 } /* suppress_windows_system */
580 #if defined(INTERNET_DOMAIN_SOCKETS)
581 connect_type = make_connection (hostarg, port, &s);
583 connect_type = make_connection (NULL, 0, &s);
586 #ifdef INTERNET_DOMAIN_SOCKETS
587 if (connect_type == (int) CONN_INTERNET)
590 gethostname (thishost, HOSTNAMSZ);
592 { /* attempt to generate a path
594 if ((ptr = getenv ("GNU_NODE")) != NULL)
595 /* user specified a path */
596 strcpy (remotepath, ptr);
598 #if 0 /* This is really bogus... re-enable it if you must have it! */
599 #if defined (hp9000s300) || defined (hp9000s800)
600 else if (strcmp (thishost,hostarg))
601 { /* try /net/thishost */
602 strcpy (remotepath, "/net/"); /* (this fails using internet
604 strcat (remotepath, thishost);
610 { /* same machines, no need for path */
611 remotepath[0] = '\0'; /* default is the empty path */
613 #endif /* INTERNET_DOMAIN_SOCKETS */
616 if ((msgp = (struct msgbuf *)
617 malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
619 fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
623 msgp->mtext[0] = '\0'; /* ready for later strcats */
624 #endif /* SYSV_IPC */
626 if (suppress_windows_system)
628 char *term = getenv ("TERM");
631 fprintf (stderr, "%s: unknown terminal type\n", progname);
634 sprintf (command, "(gnuserv-edit-files '(tty %s %s %d) '(",
635 clean_string (tty), clean_string (term), (int)getpid ());
637 else /* !suppress_windows_system */
641 #ifdef HAVE_X_WINDOWS
643 sprintf (command, "(gnuserv-edit-files '(x %s) '(",
644 clean_string (display));
648 strcpy (command, "(gnuserv-edit-files '(gtk nil) '(");
650 #ifdef HAVE_MS_WINDOWS
652 sprintf (command, "(gnuserv-edit-files '(mswindows nil) '(");
654 } /* !suppress_windows_system */
655 send_string (s, command);
662 if (i < argc - 1 && *argv[i] == '+')
663 starting_line = atoi (argv[i++]);
666 /* If the last argument is +something, treat it as a file. */
672 filename_expand (fullpath, argv[i]);
673 #ifdef INTERNET_DOMAIN_SOCKETS
674 path = (char *) malloc (strlen (remotepath) + strlen (fullpath) + 1);
675 sprintf (path, "%s%s", remotepath, fullpath);
677 path = my_strdup (fullpath);
679 sprintf (command, "(%d . %s)", starting_line, clean_string (path));
680 send_string (s, command);
684 sprintf (command, ")%s%s",
685 (quick || (nofiles && !suppress_windows_system)) ? " 'quick" : "",
686 view ? " 'view" : "");
687 send_string (s, command);
688 send_string (s, ")");
691 if (connect_type == (int) CONN_IPC)
692 disconnect_from_ipc_server (s, msgp, FALSE);
693 #else /* !SYSV_IPC */
694 if (connect_type != (int) CONN_IPC)
695 disconnect_from_server (s, FALSE);
696 #endif /* !SYSV_IPC */
704 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */