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