0
|
1 /* -*-C-*-
|
|
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.
|
149
|
5 Copyright (C) 1997 Free Software Foundation, Inc.
|
0
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
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
|
|
12 later version.
|
|
13
|
|
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
|
|
17 for more details.
|
|
18
|
|
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.
|
|
23
|
|
24 Author: Andy Norman (ange@hplb.hpl.hp.com), based on
|
|
25 'etc/emacsclient.c' from the GNU Emacs 18.52 distribution.
|
|
26
|
|
27 Please mail bugs and suggestions to the author at the above address.
|
|
28 */
|
|
29
|
|
30 /*
|
|
31 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
|
|
32 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
|
|
33 * GNUATTACH support added by Ben Wing <wing@xemacs.org>.
|
|
34 * Please see the note at the end of the README file for details.
|
|
35 *
|
|
36 * (If gnuserv came bundled with your emacs, the README file is probably
|
|
37 * ../etc/gnuserv.README relative to the directory containing this file)
|
|
38 */
|
|
39
|
|
40 #if 0
|
|
41 /* Hand-munged RCS header */
|
|
42 static char rcsid [] = "!Header: gnuclient.c,v 2.2 95/12/12 01:39:21 wing nene !";
|
|
43 #endif
|
|
44
|
|
45 #include "gnuserv.h"
|
|
46 #include "getopt.h"
|
|
47
|
|
48 #include <stdio.h>
|
|
49 #include <stdlib.h>
|
149
|
50 #ifdef HAVE_STRING_H
|
0
|
51 #include <string.h>
|
149
|
52 #endif /* HAVE_STRING_H */
|
0
|
53 #include <sys/types.h>
|
149
|
54 #ifdef HAVE_UNISTD_H
|
0
|
55 #include <unistd.h>
|
149
|
56 #endif /* HAVE_UNISTD_H */
|
|
57 #include <signal.h>
|
|
58
|
0
|
59
|
|
60 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
|
|
61 !defined(INTERNET_DOMAIN_SOCKETS)
|
78
|
62 int
|
|
63 main (int argc, char *argv[])
|
0
|
64 {
|
|
65 fprintf (stderr, "Sorry, the Emacs server is only "
|
|
66 "supported on systems that have\n");
|
|
67 fprintf (stderr, "Unix Domain sockets, Internet Domain "
|
|
68 "sockets or System V IPC.\n");
|
|
69 exit (1);
|
|
70 } /* main */
|
|
71 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
|
|
72
|
|
73 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
|
|
74 static char *cp = NULL; /* ptr into valid bit of cwd above */
|
|
75
|
108
|
76 static pid_t emacs_pid; /* Process id for emacs process */
|
|
77
|
149
|
78 void initialize_signals (void);
|
|
79
|
|
80 void
|
|
81 tell_emacs_to_resume (int sig)
|
108
|
82 {
|
149
|
83 char buffer[GSERV_BUFSZ+1];
|
|
84 int s; /* socket / msqid to server */
|
|
85 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
|
|
86 ONN_IPC */
|
108
|
87
|
|
88 /* Why is SYSV so retarded? */
|
|
89 /* We want emacs to realize that we are resuming */
|
149
|
90 signal(SIGCONT, tell_emacs_to_resume);
|
108
|
91
|
149
|
92 connect_type = make_connection (NULL, (u_short) 0, &s);
|
108
|
93
|
149
|
94 sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", getpid());
|
|
95 send_string(s, buffer);
|
|
96
|
108
|
97 #ifdef SYSV_IPC
|
|
98 if (connect_type == (int) CONN_IPC)
|
|
99 disconnect_from_ipc_server (s, msgp, FALSE);
|
|
100 #else /* !SYSV_IPC */
|
|
101 if (connect_type != (int) CONN_IPC)
|
|
102 disconnect_from_server (s, FALSE);
|
|
103 #endif /* !SYSV_IPC */
|
|
104 }
|
|
105
|
149
|
106 void
|
|
107 pass_signal_to_emacs (int sig)
|
108
|
108 {
|
149
|
109 if (kill (emacs_pid, sig) == -1)
|
|
110 {
|
|
111 fprintf (stderr, "gnuattach: Could not pass signal to emacs process\n");
|
|
112 exit (1);
|
|
113 }
|
|
114 initialize_signals ();
|
108
|
115 }
|
|
116
|
149
|
117 void
|
|
118 initialize_signals ()
|
108
|
119 {
|
149
|
120 /* Set up signal handler to pass relevant signals to emacs process.
|
|
121 We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
|
|
122 Emacs, but I think it's better not to. I can see no reason why
|
|
123 Emacs should SIGSEGV whenever gnuclient SIGSEGV-s, etc. */
|
|
124 signal (SIGHUP, pass_signal_to_emacs);
|
|
125 signal (SIGQUIT, pass_signal_to_emacs);
|
|
126 signal (SIGINT, pass_signal_to_emacs);
|
108
|
127
|
|
128 /* We want emacs to realize that we are resuming */
|
149
|
129 signal (SIGCONT, tell_emacs_to_resume);
|
108
|
130 }
|
|
131
|
0
|
132
|
|
133 /*
|
|
134 get_current_working_directory -- return the cwd.
|
|
135 */
|
|
136 static char *
|
|
137 get_current_working_directory (void)
|
|
138 {
|
|
139 if (cp == NULL)
|
|
140 { /* haven't calculated it yet */
|
|
141 #ifdef BSD
|
|
142 if (getwd (cwd) == 0)
|
|
143 #else /* !BSD */
|
|
144 if (getcwd (cwd,MAXPATHLEN) == NULL)
|
|
145 #endif /* !BSD */
|
|
146 {
|
|
147 perror (progname);
|
|
148 fprintf (stderr, "%s: unable to get current working directory\n",
|
|
149 progname);
|
|
150 exit (1);
|
|
151 } /* if */
|
|
152
|
|
153 /* on some systems, cwd can look like '@machine/' ... */
|
|
154 /* ignore everything before the first '/' */
|
|
155 for (cp = cwd; *cp && *cp != '/'; ++cp)
|
|
156 ;
|
|
157
|
|
158 } /* if */
|
|
159
|
|
160 return cp;
|
|
161
|
|
162 } /* get_current_working_directory */
|
|
163
|
|
164
|
|
165 /*
|
|
166 filename_expand -- try to convert the given filename into a fully-qualified
|
|
167 pathname.
|
|
168 */
|
|
169 static void
|
|
170 filename_expand (char *fullpath, char *filename)
|
|
171 /* fullpath - returned full pathname */
|
|
172 /* filename - filename to expand */
|
|
173 {
|
|
174 int len;
|
|
175
|
|
176 fullpath[0] = '\0';
|
|
177
|
|
178 if (filename[0] && filename[0] != '/')
|
|
179 { /* relative filename */
|
|
180 strcat (fullpath, get_current_working_directory ());
|
|
181 len = strlen (fullpath);
|
|
182
|
|
183 if (len > 0 && fullpath[len-1] == '/') /* trailing slash already? */
|
|
184 ; /* yep */
|
|
185 else
|
|
186 strcat (fullpath, "/"); /* nope, append trailing slash */
|
|
187 } /* if */
|
|
188
|
|
189 strcat (fullpath,filename);
|
|
190
|
|
191 } /* filename_expand */
|
|
192
|
78
|
193 int
|
|
194 main (int argc, char *argv[])
|
0
|
195 {
|
|
196 int starting_line = 1; /* line to start editing at */
|
149
|
197 char command[MAXPATHLEN+50]; /* emacs command buffer */
|
0
|
198 char fullpath[MAXPATHLEN+1]; /* full pathname to file */
|
|
199 int qflg = 0; /* quick edit, don't wait for
|
|
200 * user to finish */
|
149
|
201 int view = 0; /* view only. */
|
0
|
202 int errflg = 0; /* option error */
|
|
203 int c; /* char from getopt */
|
|
204 int s; /* socket / msqid to server */
|
|
205 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
|
|
206 * CONN_IPC */
|
149
|
207 int suppress_windows_system = 0;
|
|
208 char *display;
|
0
|
209 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
210 char *hostarg = NULL; /* remote hostname */
|
|
211 char thishost[HOSTNAMSZ]; /* this hostname */
|
|
212 char remotepath[MAXPATHLEN+1]; /* remote pathname */
|
|
213 int rflg = 0; /* pathname given on cmdline */
|
|
214 u_short portarg = 0; /* port to server */
|
|
215 char *ptr; /* return from getenv */
|
|
216 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
217 #ifdef SYSV_IPC
|
|
218 struct msgbuf *msgp; /* message */
|
|
219 #endif /* SYSV_IPC */
|
|
220 char *tty;
|
108
|
221 char buffer[GSERV_BUFSZ+1]; /* buffer to read pid */
|
0
|
222
|
|
223 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
224 memset (remotepath, 0, sizeof (remotepath));
|
|
225 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
226
|
|
227 progname = argv[0];
|
|
228
|
149
|
229 display = getenv ("DISPLAY");
|
|
230 if (!display)
|
|
231 suppress_windows_system = 1;
|
|
232
|
0
|
233 while ((c = getopt (argc, argv,
|
|
234
|
|
235 #ifdef INTERNET_DOMAIN_SOCKETS
|
149
|
236 "n:h:p:r:qv"
|
0
|
237 #else /* !INTERNET_DOMAIN_SOCKETS */
|
149
|
238 "n:qv"
|
0
|
239 #endif /* !INTERNET_DOMAIN_SOCKETS */
|
|
240
|
|
241 )) != EOF)
|
|
242 switch (c)
|
|
243 {
|
149
|
244 case 'n':
|
|
245 if (*optarg == 'w')
|
|
246 suppress_windows_system++;
|
|
247 else
|
|
248 errflg++;
|
|
249 break;
|
0
|
250 case 'q': /* quick mode specified */
|
|
251 qflg++;
|
|
252 break;
|
149
|
253 case 'v':
|
|
254 view++;
|
|
255 break;
|
0
|
256
|
|
257 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
258 case 'h': /* server host name specified */
|
|
259 hostarg = optarg;
|
|
260 break;
|
|
261 case 'r': /* remote path from server specifed */
|
|
262 strcpy (remotepath,optarg);
|
|
263 rflg++;
|
|
264 break;
|
|
265 case 'p': /* port number specified */
|
|
266 portarg = atoi (optarg);
|
|
267 break;
|
|
268 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
269
|
|
270 case '?':
|
|
271 errflg++;
|
|
272 } /* switch */
|
|
273
|
|
274 if (errflg)
|
|
275 {
|
|
276 fprintf (stderr,
|
|
277 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
278 "usage: %s [-q] [-h hostname] [-p port] [-r pathname] "
|
|
279 "[[+line] path] ...\n",
|
|
280 #else /* !INTERNET_DOMAIN_SOCKETS */
|
149
|
281 "usage: %s [-nw] [-q] [[+line] path] ...\n",
|
0
|
282 #endif /* !INTERNET_DOMAIN_SOCKETS */
|
|
283 progname);
|
|
284 exit (1);
|
|
285 } /* if */
|
|
286
|
149
|
287 if (suppress_windows_system)
|
0
|
288 {
|
149
|
289 tty = ttyname (0);
|
|
290 if (!tty)
|
|
291 {
|
|
292 fprintf (stderr, "%s: Not connected to a tty", progname);
|
|
293 exit (1);
|
|
294 }
|
0
|
295 }
|
149
|
296 /* This next stuff added in an attempt to make handling of the tty
|
|
297 do the right thing when dealing with signals. The idea is to
|
|
298 pass all the appropriate signals to the emacs process. */
|
108
|
299
|
|
300 connect_type = make_connection (NULL, (u_short) 0, &s);
|
0
|
301
|
149
|
302 send_string (s, "(gnuserv-eval '(emacs-pid))");
|
|
303 send_string (s, EOT_STR);
|
|
304
|
|
305 if (read_line (s, buffer) == 0)
|
|
306 {
|
|
307 fprintf (stderr, "%s: Could not establish emacs procces id\n",
|
|
308 progname);
|
|
309 exit (1);
|
|
310 }
|
|
311 /* Don't do disconnect_from_server becasue we have already read
|
|
312 data, and disconnect doesn't do anything else. */
|
108
|
313 #ifdef SYSV_IPC
|
|
314 if (connect_type == (int) CONN_IPC)
|
|
315 disconnect_from_ipc_server (s, msgp, FALSE);
|
|
316 #endif /* !SYSV_IPC */
|
|
317
|
|
318 emacs_pid = (pid_t)atol(buffer);
|
|
319 initialize_signals();
|
|
320
|
|
321 #if defined(INTERNET_DOMAIN_SOCKETS) && !defined(GNUATTACH)
|
0
|
322 connect_type = make_connection (hostarg, portarg, &s);
|
|
323 #else
|
|
324 connect_type = make_connection (NULL, (u_short) 0, &s);
|
|
325 #endif
|
|
326
|
|
327 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
328 if (connect_type == (int) CONN_INTERNET)
|
|
329 {
|
|
330 gethostname (thishost, HOSTNAMSZ);
|
|
331 if (!rflg)
|
|
332 { /* attempt to generate a path
|
|
333 * to this machine */
|
|
334 if ((ptr = getenv ("GNU_NODE")) != NULL)
|
|
335 /* user specified a path */
|
|
336 strcpy (remotepath, ptr);
|
|
337 }
|
|
338 #if 0 /* This is really bogus... re-enable it if you must have it! */
|
|
339 #if defined (hp9000s300) || defined (hp9000s800)
|
|
340 else if (strcmp (thishost,hostarg))
|
|
341 { /* try /net/thishost */
|
|
342 strcpy (remotepath, "/net/"); /* (this fails using internet
|
|
343 addresses) */
|
|
344 strcat (remotepath, thishost);
|
|
345 }
|
|
346 #endif
|
|
347 #endif
|
|
348 }
|
|
349 else
|
|
350 { /* same machines, no need for path */
|
|
351 remotepath[0] = '\0'; /* default is the empty path */
|
|
352 }
|
|
353 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
354
|
|
355 #ifdef SYSV_IPC
|
|
356 if ((msgp = (struct msgbuf *)
|
|
357 malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
|
|
358 {
|
|
359 fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
|
|
360 exit (1);
|
|
361 } /* if */
|
|
362
|
|
363 msgp->mtext[0] = '\0'; /* ready for later strcats */
|
|
364 #endif /* SYSV_IPC */
|
|
365
|
149
|
366 if (suppress_windows_system)
|
0
|
367 {
|
149
|
368 ptr = getenv ("TERM");
|
|
369 if (!ptr)
|
|
370 {
|
|
371 fprintf (stderr, "%s: unknown terminal type\n", progname);
|
|
372 exit (1);
|
|
373 }
|
|
374 sprintf (command,
|
|
375 "(gnuserv-edit-files '(tty \"%s\" \"%s\" %d) '(",
|
|
376 tty, ptr, getpid ());
|
0
|
377 }
|
149
|
378 else /* !suppress_windows_system */
|
|
379 {
|
|
380 sprintf (command, "(gnuserv-edit-files '(x \"%s\") '(",
|
|
381 display);
|
|
382 } /* !suppress_windows_system */
|
0
|
383 send_string (s, command);
|
149
|
384
|
|
385 if (!suppress_windows_system && (optind == argc))
|
|
386 qflg = 1;
|
0
|
387
|
|
388 for (; optind < argc; optind++)
|
|
389 {
|
149
|
390 if (optind < argc - 1 && *argv[optind] == '+')
|
|
391 starting_line = atoi (argv[optind++]);
|
0
|
392 else
|
149
|
393 starting_line = 1;
|
|
394 /* If the last argument is +something, treat it as a file. */
|
|
395 if (optind == argc)
|
0
|
396 {
|
149
|
397 starting_line = 1;
|
|
398 --optind;
|
|
399 }
|
|
400 filename_expand (fullpath, argv[optind]);
|
|
401 sprintf (command, "(%d . \"%s%s\")", starting_line,
|
0
|
402 #ifdef INTERNET_DOMAIN_SOCKETS
|
149
|
403 remotepath,
|
0
|
404 #else /* !INTERNET_DOMAIN_SOCKETS */
|
149
|
405 "",
|
0
|
406 #endif
|
149
|
407 fullpath);
|
|
408 send_string (s, command);
|
0
|
409 } /* for */
|
|
410
|
149
|
411 sprintf (command, ") %s)", qflg ? "'quick" : (view ? "'view" : ""));
|
|
412 send_string (s, command);
|
0
|
413
|
|
414 #ifdef SYSV_IPC
|
|
415 if (connect_type == (int) CONN_IPC)
|
|
416 disconnect_from_ipc_server (s, msgp, FALSE);
|
|
417 #else /* !SYSV_IPC */
|
|
418 if (connect_type != (int) CONN_IPC)
|
|
419 disconnect_from_server (s, FALSE);
|
|
420 #endif /* !SYSV_IPC */
|
|
421
|
78
|
422 return 0;
|
0
|
423
|
|
424 } /* main */
|
|
425
|
|
426 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
|