1 /* Client process that communicates with GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <../src/config.h>
30 #if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
37 fprintf (stderr
, "%s: Sorry, the Emacs server is supported only\n",
39 fprintf (stderr
, "on systems with Berkeley sockets or System V IPC.\n");
43 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
45 #if ! defined (HAVE_SYSVIPC)
46 /* BSD code is very different from SYSV IPC code */
48 #include <sys/types.h>
49 #include <sys/socket.h>
55 extern char *strerror ();
65 struct sockaddr_un server
;
66 char *homedir
, *cwd
, *str
;
69 char *getenv (), *getwd ();
74 fprintf (stderr
, "Usage: %s [+linenumber] filename\n", argv
[0]);
79 * Open up an AF_UNIX socket in this person's home directory
82 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
84 fprintf (stderr
, "%s: ", argv
[0]);
88 server
.sun_family
= AF_UNIX
;
89 #ifndef SERVER_HOME_DIR
93 gethostname (system_name
, sizeof (system_name
));
94 sprintf (server
.sun_path
, "/tmp/esrv%d-%s", geteuid (), system_name
);
96 if (stat (server
.sun_path
, &statbfr
) == -1)
100 "Can't find socket; have you started the server?\n");
105 if (statbfr
.st_uid
!= geteuid())
107 fprintf (stderr
, "Illegal socket owner\n");
112 if ((homedir
= getenv ("HOME")) == NULL
)
114 fprintf (stderr
, "%s: No home directory\n", argv
[0]);
117 strcpy (server
.sun_path
, homedir
);
118 strcat (server
.sun_path
, "/.emacs_server");
121 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
124 fprintf (stderr
, "%s: ", argv
[0]);
128 if ((out
= fdopen (s
, "r+")) == NULL
)
130 fprintf (stderr
, "%s: ", argv
[0]);
135 cwd
= getwd (string
);
138 /* getwd puts message in STRING if it fails. */
139 fprintf (stderr
, "%s: %s (%s)\n", argv
[0], string
, strerror (errno
));
143 for (i
= 1; i
< argc
; i
++)
147 char *p
= argv
[i
] + 1;
148 while (*p
>= '0' && *p
<= '9') p
++;
150 fprintf (out
, "%s/", cwd
);
152 else if (*argv
[i
] != '/')
153 fprintf (out
, "%s/", cwd
);
154 fprintf (out
, "%s ", argv
[i
]);
159 printf ("Waiting for Emacs...");
162 rewind (out
); /* re-read the output */
163 str
= fgets (string
, BUFSIZ
, out
);
165 /* Now, wait for an answer and print any messages. */
167 while (str
= fgets (string
, BUFSIZ
, out
))
173 #else /* This is the SYSV IPC section */
175 #include <sys/types.h>
180 char *getwd (), *getcwd (), *getenv ();
188 /* Size of text allocated in MSGP. */
189 int size_allocated
= BUFSIZ
;
190 /* Amount of text used in MSGP. */
193 = (struct msgbuf
*) malloc (sizeof (struct msgbuf
) + size_allocated
);
194 struct msqid_ds
* msg_st
;
195 char *homedir
, buf
[BUFSIZ
];
202 fprintf (stderr
, "Usage: %s [+linenumber] filename\n", argv
[0]);
207 * Create a message queue using ~/.emacs_server as the path for ftok
209 if ((homedir
= getenv ("HOME")) == NULL
)
211 fprintf (stderr
, "%s: No home directory\n", argv
[0]);
214 strcpy (buf
, homedir
);
215 strcat (buf
, "/.emacs_server");
217 key
= ftok (buf
, 1); /* unlikely to be anyone else using it */
218 s
= msgget (key
, 0600 | IPC_CREAT
);
221 fprintf (stderr
, "%s: ", argv
[0]);
226 /* Determine working dir, so we can prefix it to all the arguments. */
228 temp
= getwd (gwdirb
);
230 temp
= getcwd (gwdirb
, sizeof gwdirb
);
236 /* On some systems, cwd can look like `@machine/...';
237 ignore everything before the first slash in such a case. */
238 while (*cwd
&& *cwd
!= '/')
244 fprintf (stderr
, cwd
);
256 char *p
= argv
[0] + 1;
257 while (*p
>= '0' && *p
<= '9') p
++;
261 else if (*argv
[0] != '/')
265 used
+= strlen (cwd
);
266 used
+= strlen (argv
[0]) + 1;
267 while (used
+ 2 > size_allocated
)
270 msgp
= (struct msgbuf
*) realloc (msgp
,
271 (sizeof (struct msgbuf
)
276 strcat (msgp
->mtext
, cwd
);
278 strcat (msgp
->mtext
, argv
[0]);
279 strcat (msgp
->mtext
, " ");
282 strcat (msgp
->mtext
, "\n");
283 #ifdef HPUX /* HPUX has a bug. */
284 if (strlen (msgp
->mtext
) >= 512)
286 fprintf (stderr
, "emacsclient: args too long for msgsnd\n");
291 if (msgsnd (s
, msgp
, strlen (msgp
->mtext
)+1, 0) < 0)
293 fprintf (stderr
, "%s: ", argv
[0]);
298 * Now, wait for an answer
300 printf ("Waiting for Emacs...");
303 msgrcv (s
, msgp
, BUFSIZ
, getpid (), 0); /* wait for anything back */
304 strcpy (buf
, msgp
->mtext
);
306 printf ("\n%s\n", buf
);
310 #endif /* HAVE_SYSVIPC */
312 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
314 #ifndef HAVE_STRERROR
319 extern char *sys_errlist
[];
322 if (errnum
>= 0 && errnum
< sys_nerr
)
323 return sys_errlist
[errnum
];
324 return (char *) "Unknown error";
327 #endif /* ! HAVE_STRERROR */