0
|
1 /* Client process that communicates with GNU Emacs acting as server.
|
|
2 Copyright (C) 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of GNU Emacs.
|
|
5
|
|
6 GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2, or (at your option)
|
|
9 any later version.
|
|
10
|
|
11 GNU Emacs is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 the Free the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.28. */
|
|
22
|
|
23 #define NO_SHORTNAMES
|
|
24 #include <../src/config.h>
|
|
25 #undef read
|
|
26 #undef write
|
|
27 #undef open
|
|
28 #undef close
|
|
29 #undef signal
|
|
30
|
|
31 #if __STDC__ || defined(STDC_HEADERS)
|
|
32 #include <stdlib.h>
|
|
33 #include <unistd.h>
|
|
34 #include <fcntl.h> /* for creat() */
|
|
35 #include <string.h>
|
|
36 #endif
|
|
37
|
|
38 #if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
|
|
39 #include <stdio.h>
|
|
40
|
78
|
41 int
|
|
42 main (int argc, char *argv[])
|
0
|
43 {
|
|
44 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
|
|
45 argv[0]);
|
|
46 fprintf (stderr, "on systems with Berkeley sockets or System V IPC.\n");
|
80
|
47 return 1;
|
0
|
48 }
|
|
49
|
|
50 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
|
|
51
|
|
52 #if ! defined (HAVE_SYSVIPC)
|
|
53 /* BSD code is very different from SYSV IPC code */
|
|
54
|
|
55 #include <sys/types.h>
|
|
56 #include <sys/socket.h>
|
|
57 #include <sys/un.h>
|
|
58 #include <sys/stat.h>
|
|
59 #include <stdio.h>
|
|
60 #include <errno.h>
|
|
61
|
|
62 extern char *strerror ();
|
|
63 extern int errno;
|
|
64
|
78
|
65 int
|
|
66 main (int argc, char *argv[])
|
0
|
67 {
|
|
68 char system_name[256];
|
|
69 int s, i;
|
|
70 FILE *out;
|
|
71 struct sockaddr_un server;
|
|
72 char *cwd, *str;
|
|
73 char string[BUFSIZ];
|
|
74
|
|
75 if (argc < 2)
|
|
76 {
|
|
77 fprintf (stderr, "Usage: %s [+linenumber] filename\n", argv[0]);
|
|
78 exit (1);
|
|
79 }
|
|
80
|
|
81 /*
|
|
82 * Open up an AF_UNIX socket in this person's home directory
|
|
83 */
|
|
84
|
|
85 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
|
|
86 {
|
|
87 fprintf (stderr, "%s: ", argv[0]);
|
|
88 perror ("socket");
|
|
89 exit (1);
|
|
90 }
|
|
91 server.sun_family = AF_UNIX;
|
|
92 #ifndef SERVER_HOME_DIR
|
|
93 {
|
|
94 struct stat statbfr;
|
|
95
|
|
96 gethostname (system_name, sizeof (system_name));
|
|
97 sprintf (server.sun_path, "/tmp/esrv%d-%s", (int) geteuid (), system_name);
|
|
98
|
|
99 if (stat (server.sun_path, &statbfr) == -1)
|
|
100 {
|
|
101 if (errno == ENOENT)
|
|
102 fprintf (stderr,
|
|
103 "Can't find socket; have you started the server?\n");
|
|
104 else
|
|
105 perror ("stat");
|
|
106 exit (1);
|
|
107 }
|
|
108 if (statbfr.st_uid != geteuid())
|
|
109 {
|
|
110 fprintf (stderr, "Illegal socket owner\n");
|
|
111 exit (1);
|
|
112 }
|
|
113 }
|
|
114 #else
|
|
115 if (getenv ("HOME") == NULL)
|
|
116 {
|
|
117 fprintf (stderr, "%s: No home directory\n", argv[0]);
|
|
118 exit (1);
|
|
119 }
|
|
120 sprintf (server.sun_path, "/tmp/esrv%d", geteuid ());
|
|
121 #if 0
|
|
122 strcpy (server.sun_path, getenv ("HOME"));
|
|
123 strcat (server.sun_path, "/.emacs_server");
|
|
124 #endif
|
|
125 #endif /* SERVER_HOME_DIR */
|
|
126 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
|
|
127 < 0)
|
|
128 {
|
|
129 fprintf (stderr, "%s: ", argv[0]);
|
|
130 perror ("connect");
|
|
131 exit (1);
|
|
132 }
|
|
133 if ((out = (FILE *) fdopen (s, "r+")) == NULL)
|
|
134 {
|
|
135 fprintf (stderr, "%s: ", argv[0]);
|
|
136 perror ("fdopen");
|
|
137 exit (1);
|
|
138 }
|
|
139
|
|
140 #ifdef BSD
|
|
141 cwd = (char *) getwd (string);
|
|
142 #else
|
|
143 cwd = (char *) getcwd (string, sizeof (string));
|
|
144 #endif
|
|
145
|
|
146 if (cwd == 0)
|
|
147 {
|
|
148 /* getwd puts message in STRING if it fails. */
|
|
149 fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno));
|
|
150 exit (1);
|
|
151 }
|
|
152
|
|
153 for (i = 1; i < argc; i++)
|
|
154 {
|
|
155 if (*argv[i] == '+')
|
|
156 {
|
|
157 char *p = argv[i] + 1;
|
|
158 while (*p >= '0' && *p <= '9') p++;
|
|
159 if (*p != 0)
|
|
160 fprintf (out, "%s/", cwd);
|
|
161 }
|
|
162 else if (*argv[i] != '/')
|
|
163 fprintf (out, "%s/", cwd);
|
|
164 fprintf (out, "%s ", argv[i]);
|
|
165 }
|
|
166 fprintf (out, "\n");
|
|
167 fflush (out);
|
|
168
|
|
169 printf ("Waiting for Emacs...");
|
|
170 fflush (stdout);
|
|
171
|
|
172 rewind (out); /* re-read the output */
|
|
173 str = fgets (string, BUFSIZ, out);
|
|
174
|
|
175 /* Now, wait for an answer and print any messages. */
|
|
176
|
|
177 while ((str = fgets (string, BUFSIZ, out)))
|
|
178 printf ("%s", str);
|
|
179
|
78
|
180 return 0;
|
0
|
181 }
|
|
182
|
|
183 #else /* This is the SYSV IPC section */
|
|
184
|
|
185 #include <sys/types.h>
|
|
186 #include <sys/ipc.h>
|
|
187 #include <sys/msg.h>
|
|
188 #include <stdio.h>
|
|
189
|
|
190 char *getwd ();
|
|
191 #if !__STDC__ && !defined(STDC_HEADERS)
|
|
192 char *getcwd (), *getenv ();
|
|
193 #endif
|
|
194
|
78
|
195 int
|
|
196 main (int argc, char *argv[])
|
0
|
197 {
|
|
198 int s;
|
|
199 key_t key;
|
|
200 /* Size of text allocated in MSGP. */
|
|
201 int size_allocated = BUFSIZ;
|
|
202 /* Amount of text used in MSGP. */
|
|
203 int used;
|
|
204 struct msgbuf *msgp
|
|
205 = (struct msgbuf *) malloc (sizeof (struct msgbuf) + size_allocated);
|
|
206 #if 0
|
|
207 struct msqid_ds * msg_st;
|
|
208 #endif /* 0 */
|
|
209 char *homedir, buf[BUFSIZ];
|
|
210 char gwdirb[BUFSIZ];
|
|
211 char *cwd;
|
|
212 char *temp;
|
|
213 char progname[BUFSIZ];
|
|
214
|
|
215 strcpy (progname, argv[0]);
|
|
216
|
|
217 if (argc < 2)
|
|
218 {
|
|
219 fprintf (stderr, "Usage: %s [+linenumber] filename\n", argv[0]);
|
|
220 exit (1);
|
|
221 }
|
|
222
|
|
223 /*
|
|
224 * Create a message queue using ~/.emacs_server as the path for ftok
|
|
225 */
|
|
226 if ((homedir = getenv ("HOME")) == NULL)
|
|
227 {
|
|
228 fprintf (stderr, "%s: No home directory\n", argv[0]);
|
|
229 exit (1);
|
|
230 }
|
|
231 strcpy (buf, homedir);
|
|
232 strcat (buf, "/.emacs_server");
|
|
233 creat (buf, 0600);
|
|
234 key = ftok (buf, 1); /* unlikely to be anyone else using it */
|
|
235 s = msgget (key, 0600 | IPC_CREAT);
|
|
236 if (s == -1)
|
|
237 {
|
|
238 fprintf (stderr, "%s: ", argv[0]);
|
|
239 perror ("msgget");
|
|
240 exit (1);
|
|
241 }
|
|
242
|
|
243 /* Determine working dir, so we can prefix it to all the arguments. */
|
|
244 #ifdef BSD
|
|
245 temp = getwd (gwdirb);
|
|
246 #else
|
|
247 temp = getcwd (gwdirb, sizeof gwdirb);
|
|
248 #endif
|
|
249
|
|
250 cwd = gwdirb;
|
|
251 if (temp != 0)
|
|
252 {
|
|
253 /* On some systems, cwd can look like `@machine/...';
|
|
254 ignore everything before the first slash in such a case. */
|
|
255 while (*cwd && *cwd != '/')
|
|
256 cwd++;
|
|
257 strcat (cwd, "/");
|
|
258 }
|
|
259 else
|
|
260 {
|
|
261 fprintf (stderr, cwd);
|
|
262 exit (1);
|
|
263 }
|
|
264
|
|
265 msgp->mtext[0] = 0;
|
|
266 used = 0;
|
|
267 argc--; argv++;
|
|
268 while (argc)
|
|
269 {
|
|
270 int need_cwd = 0;
|
|
271 char *modified_arg = argv[0];
|
|
272 if (*modified_arg == '+')
|
|
273 {
|
|
274 char *p = modified_arg + 1;
|
|
275 while (*p >= '0' && *p <= '9') p++;
|
|
276 if (*p != 0)
|
|
277 need_cwd = 1;
|
|
278 }
|
|
279 else if (*modified_arg != '/')
|
|
280 need_cwd = 1;
|
|
281
|
|
282 if (need_cwd)
|
|
283 used += strlen (cwd);
|
|
284 used += strlen (modified_arg) + 1;
|
|
285 while (used + 2 > size_allocated)
|
|
286 {
|
|
287 size_allocated *= 2;
|
|
288 msgp = (struct msgbuf *) realloc (msgp,
|
|
289 (sizeof (struct msgbuf)
|
|
290 + size_allocated));
|
|
291 }
|
|
292 if (need_cwd)
|
|
293 strcat (msgp->mtext, cwd);
|
|
294
|
|
295 strcat (msgp->mtext, modified_arg);
|
|
296 strcat (msgp->mtext, " ");
|
|
297 argv++; argc--;
|
|
298 }
|
|
299 strcat (msgp->mtext, "\n");
|
|
300 #ifdef HPUX /* HPUX has a bug. */
|
|
301 if (strlen (msgp->mtext) >= 512)
|
|
302 {
|
|
303 fprintf (stderr, "emacsclient: args too long for msgsnd\n");
|
|
304 exit (1);
|
|
305 }
|
|
306 #endif
|
|
307 msgp->mtype = 1;
|
|
308 if (msgsnd (s, msgp, strlen (msgp->mtext)+1, 0) < 0)
|
|
309 {
|
|
310 fprintf (stderr, "%s: ", progname);
|
|
311 perror ("msgsnd");
|
|
312 exit (1);
|
|
313 }
|
|
314 /*
|
|
315 * Now, wait for an answer
|
|
316 */
|
|
317 printf ("Waiting for Emacs...");
|
|
318 fflush (stdout);
|
|
319
|
|
320 msgrcv (s, msgp, BUFSIZ, getpid (), 0); /* wait for anything back */
|
|
321 strcpy (buf, msgp->mtext);
|
|
322
|
|
323 printf ("\n%s\n", buf);
|
80
|
324 return 0;
|
0
|
325 }
|
|
326
|
|
327 #endif /* HAVE_SYSVIPC */
|
|
328
|
|
329 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
|
|
330
|
|
331 #ifndef HAVE_STRERROR
|
|
332 char *
|
78
|
333 strerror (int errnum)
|
0
|
334 {
|
|
335 extern char *sys_errlist[];
|
|
336 extern int sys_nerr;
|
|
337
|
|
338 if (errnum >= 0 && errnum < sys_nerr)
|
|
339 return sys_errlist[errnum];
|
|
340 return (char *) "Unknown error";
|
|
341 }
|
|
342
|
|
343 #endif /* ! HAVE_STRERROR */
|