comparison lib-src/gnuclient.c @ 70:131b0175ea99 r20-0b30

Import from CVS: tag r20-0b30
author cvs
date Mon, 13 Aug 2007 09:02:59 +0200
parents ec9a17fef872
children c7528f8e288d
comparison
equal deleted inserted replaced
69:804d1389bcd6 70:131b0175ea99
50 #include <sys/types.h> 50 #include <sys/types.h>
51 #include <unistd.h> 51 #include <unistd.h>
52 52
53 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \ 53 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
54 !defined(INTERNET_DOMAIN_SOCKETS) 54 !defined(INTERNET_DOMAIN_SOCKETS)
55 int 55 main ()
56 main (int argc, char *argv[])
57 { 56 {
58 fprintf (stderr, "Sorry, the Emacs server is only " 57 fprintf (stderr, "Sorry, the Emacs server is only "
59 "supported on systems that have\n"); 58 "supported on systems that have\n");
60 fprintf (stderr, "Unix Domain sockets, Internet Domain " 59 fprintf (stderr, "Unix Domain sockets, Internet Domain "
61 "sockets or System V IPC.\n"); 60 "sockets or System V IPC.\n");
63 } /* main */ 62 } /* main */
64 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */ 63 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
65 64
66 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */ 65 static char cwd[MAXPATHLEN+2]; /* current working directory when calculated */
67 static char *cp = NULL; /* ptr into valid bit of cwd above */ 66 static char *cp = NULL; /* ptr into valid bit of cwd above */
68
69 #ifdef GNUATTACH
70 #include <signal.h>
71
72 static pid_t emacs_pid; /* Process id for emacs process */
73
74 void tell_emacs_to_resume(int sig)
75 {
76 char buffer[GSERV_BUFSZ+1];
77 int s; /* socket / msqid to server */
78 int connect_type; /* CONN_UNIX, CONN_INTERNET, or
79 * CONN_IPC */
80
81 /* Why is SYSV so retarded? */
82 /* We want emacs to realize that we are resuming */
83 signal(SIGCONT, tell_emacs_to_resume);
84
85 connect_type = make_connection (NULL, (u_short) 0, &s);
86
87 sprintf(buffer,"(server-eval '(resume-pid-console %d))", getpid());
88 send_string(s, buffer);
89
90 #ifdef SYSV_IPC
91 if (connect_type == (int) CONN_IPC)
92 disconnect_from_ipc_server (s, msgp, FALSE);
93 #else /* !SYSV_IPC */
94 if (connect_type != (int) CONN_IPC)
95 disconnect_from_server (s, FALSE);
96 #endif /* !SYSV_IPC */
97 }
98
99 void pass_signal_to_emacs(int sig)
100 {
101 if (kill(emacs_pid, sig) == -1) {
102 fprintf(stderr, "gnuattach: Could not pass signal to emacs process\n");
103 exit(1);
104 }
105 }
106
107 void initialize_signals()
108 {
109 /* Set up signal handler to pass relevant signals to emacs process */
110 signal(SIGHUP, pass_signal_to_emacs);
111 signal(SIGQUIT, pass_signal_to_emacs);
112 signal(SIGILL, pass_signal_to_emacs);
113 signal(SIGTRAP, pass_signal_to_emacs);
114 signal(SIGSEGV, pass_signal_to_emacs);
115 signal(SIGPIPE, pass_signal_to_emacs);
116 signal(SIGTERM, pass_signal_to_emacs);
117 #ifdef SIGBUS
118 signal(SIGBUS, pass_signal_to_emacs);
119 #endif
120 #ifdef SIGIOT
121 signal(SIGIOT, pass_signal_to_emacs);
122 #endif
123
124 /* We want emacs to realize that we are resuming */
125 signal(SIGCONT, tell_emacs_to_resume);
126 }
127
128 #endif /* GNUATTACH */
129 67
130 68
131 /* 69 /*
132 get_current_working_directory -- return the cwd. 70 get_current_working_directory -- return the cwd.
133 */ 71 */
186 124
187 strcat (fullpath,filename); 125 strcat (fullpath,filename);
188 126
189 } /* filename_expand */ 127 } /* filename_expand */
190 128
191 int 129 void
192 main (int argc, char *argv[]) 130 main (int argc, char **argv)
193 { 131 {
194 int starting_line = 1; /* line to start editing at */ 132 int starting_line = 1; /* line to start editing at */
195 char command[MAXPATHLEN+50]; /* emacs command buffer */ 133 char command[MAXPATHLEN+50]; /* emacs command buffer */
196 char fullpath[MAXPATHLEN+1]; /* full pathname to file */ 134 char fullpath[MAXPATHLEN+1]; /* full pathname to file */
197 #ifndef GNUATTACH 135 #ifndef GNUATTACH
198 int qflg = 0; /* quick edit, don't wait for 136 int qflg = 0; /* quick edit, don't wait for
199 * user to finish */ 137 * user to finish */
200 #endif 138 #endif
214 #ifdef SYSV_IPC 152 #ifdef SYSV_IPC
215 struct msgbuf *msgp; /* message */ 153 struct msgbuf *msgp; /* message */
216 #endif /* SYSV_IPC */ 154 #endif /* SYSV_IPC */
217 #ifdef GNUATTACH 155 #ifdef GNUATTACH
218 char *tty; 156 char *tty;
219 char buffer[GSERV_BUFSZ+1]; /* buffer to read pid */
220 #endif 157 #endif
221 158
222 #ifdef INTERNET_DOMAIN_SOCKETS 159 #ifdef INTERNET_DOMAIN_SOCKETS
223 memset (remotepath, 0, sizeof (remotepath)); 160 memset (remotepath, 0, sizeof (remotepath));
224 #endif /* INTERNET_DOMAIN_SOCKETS */ 161 #endif /* INTERNET_DOMAIN_SOCKETS */
226 progname = argv[0]; 163 progname = argv[0];
227 164
228 while ((c = getopt (argc, argv, 165 while ((c = getopt (argc, argv,
229 166
230 #ifdef INTERNET_DOMAIN_SOCKETS 167 #ifdef INTERNET_DOMAIN_SOCKETS
168 # ifdef GNUATTACH
169 "h:p:r"
170 # else
231 "h:p:r:q" 171 "h:p:r:q"
172 # endif
232 #else /* !INTERNET_DOMAIN_SOCKETS */ 173 #else /* !INTERNET_DOMAIN_SOCKETS */
233 # ifdef GNUATTACH 174 # ifdef GNUATTACH
234 "" 175 ""
235 # else 176 # else
236 "q" 177 "q"
265 206
266 if (errflg) 207 if (errflg)
267 { 208 {
268 fprintf (stderr, 209 fprintf (stderr,
269 #ifdef INTERNET_DOMAIN_SOCKETS 210 #ifdef INTERNET_DOMAIN_SOCKETS
211 # ifdef GNUATTACH
212 "usage: %s [-h hostname] [-p port] [-r pathname] "
213 "[[+line] path] ...\n",
214 # else
270 "usage: %s [-q] [-h hostname] [-p port] [-r pathname] " 215 "usage: %s [-q] [-h hostname] [-p port] [-r pathname] "
271 "[[+line] path] ...\n", 216 "[[+line] path] ...\n",
217 # endif
272 #else /* !INTERNET_DOMAIN_SOCKETS */ 218 #else /* !INTERNET_DOMAIN_SOCKETS */
273 # ifdef GNUATTACH 219 # ifdef GNUATTACH
274 "usage: %s [[+line] path] ...\n", 220 "usage: %s [[+line] path] ...\n",
275 # else 221 # else
276 "usage: %s [-q] [[+line] path] ...\n", 222 "usage: %s [-q] [[+line] path] ...\n",
285 if (!tty) 231 if (!tty)
286 { 232 {
287 fprintf (stderr, "%s: Not connected to a tty", progname); 233 fprintf (stderr, "%s: Not connected to a tty", progname);
288 exit (1); 234 exit (1);
289 } 235 }
290 236 #endif
291 /* This next stuff added in an attempt to make handling of 237
292 the tty do the right thing when dealing with signals. 238 #ifdef INTERNET_DOMAIN_SOCKETS
293 Idea is to pass all the appropriate signals to the emacs process
294 */
295
296 connect_type = make_connection (NULL, (u_short) 0, &s);
297
298 send_string(s,"(server-eval '(emacs-pid))");
299 send_string(s,EOT_STR);
300
301 if (read_line(s,buffer) == 0) {
302 fprintf(stderr, "%s: Could not establish emacs procces id\n",progname);
303 exit(1);
304 }
305 /* don't do disconnect_from_server becasue we have already read data,
306 and disconnect doesn't do anything else
307 */
308 #ifdef SYSV_IPC
309 if (connect_type == (int) CONN_IPC)
310 disconnect_from_ipc_server (s, msgp, FALSE);
311 #endif /* !SYSV_IPC */
312
313 emacs_pid = (pid_t)atol(buffer);
314 initialize_signals();
315
316 #endif /*GNUATTACH */
317
318 #if defined(INTERNET_DOMAIN_SOCKETS) && !defined(GNUATTACH)
319 connect_type = make_connection (hostarg, portarg, &s); 239 connect_type = make_connection (hostarg, portarg, &s);
320 #else 240 #else
321 connect_type = make_connection (NULL, (u_short) 0, &s); 241 connect_type = make_connection (NULL, (u_short) 0, &s);
322 #endif 242 #endif
323 243
365 if (!ptr) 285 if (!ptr)
366 { 286 {
367 fprintf (stderr, "%s: unknown terminal type\n", progname); 287 fprintf (stderr, "%s: unknown terminal type\n", progname);
368 exit (1); 288 exit (1);
369 } 289 }
370 sprintf (command, "(server-tty-edit-files \"%s\" \"%s\" %d '(", 290 sprintf (command, "(server-tty-edit-files \"%s\" \"%s\" '(", tty, ptr);
371 tty, ptr, getpid());
372 send_string (s, command); 291 send_string (s, command);
373 #else 292 #else
374 if (qflg) 293 if (qflg)
375 { 294 {
376 send_string (s, "(server-edit-files-quickly '("); 295 send_string (s, "(server-edit-files-quickly '(");
409 #else /* !SYSV_IPC */ 328 #else /* !SYSV_IPC */
410 if (connect_type != (int) CONN_IPC) 329 if (connect_type != (int) CONN_IPC)
411 disconnect_from_server (s, FALSE); 330 disconnect_from_server (s, FALSE);
412 #endif /* !SYSV_IPC */ 331 #endif /* !SYSV_IPC */
413 332
414 return 0; 333 exit (0);
415 334
416 } /* main */ 335 } /* main */
417 336
418 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */ 337 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */