comparison lib-src/gnuclient.c @ 0:376386a54a3c r19-14

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