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