/* Communication subprocess for GNU Emacs acting as server.
Copyright (C) 1986, 1987 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* The GNU Emacs edit server process is run as a subprocess of Emacs
under control of the file lisp/server.el.
This program accepts communication from client (program emacsclient.c)
and passes their commands (consisting of keyboard characters)
up to the Emacs which then executes them. */
#include "../src/config.h"
#if !defined(BSD) && !defined(HAVE_SYSVIPC)
fprintf (stderr
, "Sorry, the Emacs server is supported only on Berkeley Unix\n");
fprintf (stderr
, "or System V systems with IPC\n");
#else /* BSD or HAVE_SYSVIPC */
#if defined (BSD) && ! defined (HAVE_SYSVIPC)
/* BSD code is very different from SYSV IPC code */
struct sockaddr_un server
, fromunix
;
char *str
, string
[BUFSIZ
], code
[BUFSIZ
];
openfiles
= (FILE **) malloc (openfiles_size
* sizeof (FILE *));
* Open up an AF_UNIX socket in this person's home directory
if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
server
.sun_family
= AF_UNIX
;
sprintf (server
.sun_path
, "/tmp/esrv%d", geteuid ());
if (unlink (server
.sun_path
) == -1 && errno
!= ENOENT
)
if ((homedir
= getenv ("HOME")) == NULL
)
fprintf (stderr
,"No home directory\n");
strcpy (server
.sun_path
, homedir
);
strcat (server
.sun_path
, "/.emacs_server");
if (bind (s
, &server
, strlen (server
.sun_path
) + 2) < 0)
* Now, just wait for everything to come in..
/* Disable sigpipes in case luser kills client... */
signal (SIGPIPE
, SIG_IGN
);
int rmask
= (1 << s
) + 1;
if (select (s
+ 1, &rmask
, 0, 0, 0) < 0)
if (rmask
& (1 << s
)) /* client sends list of filenames */
fromlen
= sizeof (fromunix
);
fromunix
.sun_family
= AF_UNIX
;
infd
= accept (s
, &fromunix
, &fromlen
); /* open socket fd */
if (errno
== EMFILE
|| errno
== ENFILE
)
printf ("Too many clients.\n");
if (infd
>= openfiles_size
)
openfiles
= (FILE **) realloc (openfiles
,
openfiles_size
* sizeof (FILE *));
infile
= fdopen (infd
, "r+"); /* open stream */
printf ("Too many clients.\n");
write (infd
, "Too many clients.\n", 18);
close (infd
); /* Prevent descriptor leak.. */
str
= fgets (string
, BUFSIZ
, infile
);
close (infd
); /* Prevent descriptor leak.. */
openfiles
[infd
] = infile
;
printf ("Client: %d %s", infd
, string
);
/* If what we read did not end in a newline,
it means there is more. Keep reading from the socket
and outputting to Emacs, until we get the newline. */
while (string
[strlen (string
) - 1] != '\n')
if (fgets (string
, BUFSIZ
, infile
) == 0)
else if (rmask
& 1) /* emacs sends codeword, fd, and string message */
/* Read command codeword and fd */
scanf ("%s %d%*c", code
, &infd
);
/* Transfer text from Emacs to the client, up to a newline. */
infile
= openfiles
[infd
];
if (fgets (string
, BUFSIZ
, stdin
) == 0)
fprintf (infile
, "%s", string
);
if (string
[strlen (string
) - 1] == '\n')
/* If command is close, close connection to client. */
if (strncmp (code
, "Close:", 6) == 0)
#else /* This is the SYSV IPC section */
/* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
Incorrect. This program runs as an inferior of Emacs.
Its stderr always exists--rms. */
(struct msgbuf
*) malloc (sizeof *msgp
+ BUFSIZ
);
char *homedir
, *getenv ();
* Create a message queue using ~/.emacs_server as the path for ftok
if ((homedir
= getenv ("HOME")) == NULL
)
fprintf (stderr
,"No home directory\n");
strcpy (string
, homedir
);
strcat (string
, "/.emacs_server");
key
= ftok (string
, 1); /* unlikely to be anyone else using it */
s
= msgget (key
, 0600 | IPC_CREAT
);
/* Fork so we can close connection even if parent dies */
signal (SIGTERM
, msgcatch
);
signal (SIGINT
, msgcatch
);
/* If parent goes away, remove message box and exit */
setpgrp (); /* Gnu kills process group on exit */
if ((fromlen
= msgrcv (s
, msgp
, BUFSIZ
- 1, 1, 0)) < 0)
msgctl (s
, IPC_STAT
, &msg_st
);
strncpy (string
, msgp
->mtext
, fromlen
);
string
[fromlen
] = 0; /* make sure */
/* Newline is part of string.. */
printf ("Client: %d %s", s
, string
);
/* Now, wait for a wakeup */
fgets (msgp
->mtext
, BUFSIZ
, stdin
);
msgp
->mtext
[strlen (msgp
->mtext
)-1] = 0;
/* strcpy (msgp->mtext, "done");*/
msgp
->mtype
= msg_st
.msg_lspid
;
msgsnd (s
, msgp
, strlen (msgp
->mtext
)+1, 0);