annotate lib-src/emacsclient.c @ 82:6a378aca36af r20-0b91

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