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
|
|
69
|
|
70 /*
|
|
71 get_current_working_directory -- return the cwd.
|
|
72 */
|
|
73 static char *
|
|
74 get_current_working_directory (void)
|
|
75 {
|
|
76 if (cp == NULL)
|
|
77 { /* haven't calculated it yet */
|
|
78 #ifdef BSD
|
|
79 if (getwd (cwd) == 0)
|
|
80 #else /* !BSD */
|
|
81 if (getcwd (cwd,MAXPATHLEN) == NULL)
|
|
82 #endif /* !BSD */
|
|
83 {
|
|
84 perror (progname);
|
|
85 fprintf (stderr, "%s: unable to get current working directory\n",
|
|
86 progname);
|
|
87 exit (1);
|
|
88 } /* if */
|
|
89
|
|
90 /* on some systems, cwd can look like '@machine/' ... */
|
|
91 /* ignore everything before the first '/' */
|
|
92 for (cp = cwd; *cp && *cp != '/'; ++cp)
|
|
93 ;
|
|
94
|
|
95 } /* if */
|
|
96
|
|
97 return cp;
|
|
98
|
|
99 } /* get_current_working_directory */
|
|
100
|
|
101
|
|
102 /*
|
|
103 filename_expand -- try to convert the given filename into a fully-qualified
|
|
104 pathname.
|
|
105 */
|
|
106 static void
|
|
107 filename_expand (char *fullpath, char *filename)
|
|
108 /* fullpath - returned full pathname */
|
|
109 /* filename - filename to expand */
|
|
110 {
|
|
111 int len;
|
|
112
|
|
113 fullpath[0] = '\0';
|
|
114
|
|
115 if (filename[0] && filename[0] != '/')
|
|
116 { /* relative filename */
|
|
117 strcat (fullpath, get_current_working_directory ());
|
|
118 len = strlen (fullpath);
|
|
119
|
|
120 if (len > 0 && fullpath[len-1] == '/') /* trailing slash already? */
|
|
121 ; /* yep */
|
|
122 else
|
|
123 strcat (fullpath, "/"); /* nope, append trailing slash */
|
|
124 } /* if */
|
|
125
|
|
126 strcat (fullpath,filename);
|
|
127
|
|
128 } /* filename_expand */
|
|
129
|
12
|
130 int
|
|
131 main (int argc, char *argv[])
|
0
|
132 {
|
|
133 int starting_line = 1; /* line to start editing at */
|
|
134 char command[MAXPATHLEN+50]; /* emacs command buffer */
|
|
135 char fullpath[MAXPATHLEN+1]; /* full pathname to file */
|
|
136 #ifndef GNUATTACH
|
|
137 int qflg = 0; /* quick edit, don't wait for
|
|
138 * user to finish */
|
|
139 #endif
|
|
140 int errflg = 0; /* option error */
|
|
141 int c; /* char from getopt */
|
|
142 int s; /* socket / msqid to server */
|
|
143 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
|
|
144 * CONN_IPC */
|
|
145 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
146 char *hostarg = NULL; /* remote hostname */
|
|
147 char thishost[HOSTNAMSZ]; /* this hostname */
|
|
148 char remotepath[MAXPATHLEN+1]; /* remote pathname */
|
|
149 int rflg = 0; /* pathname given on cmdline */
|
|
150 u_short portarg = 0; /* port to server */
|
|
151 char *ptr; /* return from getenv */
|
|
152 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
153 #ifdef SYSV_IPC
|
|
154 struct msgbuf *msgp; /* message */
|
|
155 #endif /* SYSV_IPC */
|
|
156 #ifdef GNUATTACH
|
|
157 char *tty;
|
|
158 #endif
|
|
159
|
|
160 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
161 memset (remotepath, 0, sizeof (remotepath));
|
|
162 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
163
|
|
164 progname = argv[0];
|
|
165
|
|
166 while ((c = getopt (argc, argv,
|
|
167
|
|
168 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
169 # ifdef GNUATTACH
|
|
170 "h:p:r"
|
|
171 # else
|
|
172 "h:p:r:q"
|
|
173 # endif
|
|
174 #else /* !INTERNET_DOMAIN_SOCKETS */
|
|
175 # ifdef GNUATTACH
|
|
176 ""
|
|
177 # else
|
|
178 "q"
|
|
179 # endif
|
|
180 #endif /* !INTERNET_DOMAIN_SOCKETS */
|
|
181
|
|
182 )) != EOF)
|
|
183 switch (c)
|
|
184 {
|
|
185 #ifndef GNUATTACH
|
|
186 case 'q': /* quick mode specified */
|
|
187 qflg++;
|
|
188 break;
|
|
189 #endif
|
|
190
|
|
191 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
192 case 'h': /* server host name specified */
|
|
193 hostarg = optarg;
|
|
194 break;
|
|
195 case 'r': /* remote path from server specifed */
|
|
196 strcpy (remotepath,optarg);
|
|
197 rflg++;
|
|
198 break;
|
|
199 case 'p': /* port number specified */
|
|
200 portarg = atoi (optarg);
|
|
201 break;
|
|
202 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
203
|
|
204 case '?':
|
|
205 errflg++;
|
|
206 } /* switch */
|
|
207
|
|
208 if (errflg)
|
|
209 {
|
|
210 fprintf (stderr,
|
|
211 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
212 # ifdef GNUATTACH
|
|
213 "usage: %s [-h hostname] [-p port] [-r pathname] "
|
|
214 "[[+line] path] ...\n",
|
|
215 # else
|
|
216 "usage: %s [-q] [-h hostname] [-p port] [-r pathname] "
|
|
217 "[[+line] path] ...\n",
|
|
218 # endif
|
|
219 #else /* !INTERNET_DOMAIN_SOCKETS */
|
|
220 # ifdef GNUATTACH
|
|
221 "usage: %s [[+line] path] ...\n",
|
|
222 # else
|
|
223 "usage: %s [-q] [[+line] path] ...\n",
|
|
224 # endif
|
|
225 #endif /* !INTERNET_DOMAIN_SOCKETS */
|
|
226 progname);
|
|
227 exit (1);
|
|
228 } /* if */
|
|
229
|
|
230 #ifdef GNUATTACH
|
|
231 tty = ttyname (0);
|
|
232 if (!tty)
|
|
233 {
|
|
234 fprintf (stderr, "%s: Not connected to a tty", progname);
|
|
235 exit (1);
|
|
236 }
|
|
237 #endif
|
|
238
|
|
239 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
240 connect_type = make_connection (hostarg, portarg, &s);
|
|
241 #else
|
|
242 connect_type = make_connection (NULL, (u_short) 0, &s);
|
|
243 #endif
|
|
244
|
|
245 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
246 if (connect_type == (int) CONN_INTERNET)
|
|
247 {
|
|
248 gethostname (thishost, HOSTNAMSZ);
|
|
249 if (!rflg)
|
|
250 { /* attempt to generate a path
|
|
251 * to this machine */
|
|
252 if ((ptr = getenv ("GNU_NODE")) != NULL)
|
|
253 /* user specified a path */
|
|
254 strcpy (remotepath, ptr);
|
|
255 }
|
|
256 #if 0 /* This is really bogus... re-enable it if you must have it! */
|
|
257 #if defined (hp9000s300) || defined (hp9000s800)
|
|
258 else if (strcmp (thishost,hostarg))
|
|
259 { /* try /net/thishost */
|
|
260 strcpy (remotepath, "/net/"); /* (this fails using internet
|
|
261 addresses) */
|
|
262 strcat (remotepath, thishost);
|
|
263 }
|
|
264 #endif
|
|
265 #endif
|
|
266 }
|
|
267 else
|
|
268 { /* same machines, no need for path */
|
|
269 remotepath[0] = '\0'; /* default is the empty path */
|
|
270 }
|
|
271 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
272
|
|
273 #ifdef SYSV_IPC
|
|
274 if ((msgp = (struct msgbuf *)
|
|
275 malloc (sizeof *msgp + GSERV_BUFSZ)) == NULL)
|
|
276 {
|
|
277 fprintf (stderr, "%s: not enough memory for message buffer\n", progname);
|
|
278 exit (1);
|
|
279 } /* if */
|
|
280
|
|
281 msgp->mtext[0] = '\0'; /* ready for later strcats */
|
|
282 #endif /* SYSV_IPC */
|
|
283
|
|
284 #ifdef GNUATTACH
|
|
285 ptr = getenv ("TERM");
|
|
286 if (!ptr)
|
|
287 {
|
|
288 fprintf (stderr, "%s: unknown terminal type\n", progname);
|
|
289 exit (1);
|
|
290 }
|
|
291 sprintf (command, "(server-tty-edit-files \"%s\" \"%s\" '(", tty, ptr);
|
|
292 send_string (s, command);
|
|
293 #else
|
|
294 if (qflg)
|
|
295 {
|
|
296 send_string (s, "(server-edit-files-quickly '(");
|
|
297 }
|
|
298 else
|
|
299 {
|
|
300 send_string (s, "(server-edit-files '(");
|
|
301 }
|
|
302 #endif
|
|
303
|
|
304 for (; optind < argc; optind++)
|
|
305 {
|
|
306 if (*argv[optind] == '+')
|
|
307 starting_line = atoi (argv[optind]);
|
|
308 else
|
|
309 {
|
|
310 filename_expand (fullpath, argv[optind]);
|
|
311 sprintf (command, "(%d . \"%s%s\")", starting_line,
|
|
312
|
|
313 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
314 remotepath,
|
|
315 #else /* !INTERNET_DOMAIN_SOCKETS */
|
|
316 "",
|
|
317 #endif
|
|
318 fullpath);
|
|
319 send_string (s,command);
|
|
320 starting_line = 1;
|
|
321 } /* else */
|
|
322 } /* for */
|
|
323
|
|
324 send_string (s,"))");
|
|
325
|
|
326 #ifdef SYSV_IPC
|
|
327 if (connect_type == (int) CONN_IPC)
|
|
328 disconnect_from_ipc_server (s, msgp, FALSE);
|
|
329 #else /* !SYSV_IPC */
|
|
330 if (connect_type != (int) CONN_IPC)
|
|
331 disconnect_from_server (s, FALSE);
|
|
332 #endif /* !SYSV_IPC */
|
|
333
|
12
|
334 return 0;
|
0
|
335
|
|
336 } /* main */
|
|
337
|
|
338 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
|