428
|
1 /* Interfaces to system-dependent kernel and library entries.
|
|
2 Copyright (C) 1985-1988, 1992-1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Tinker Systems.
|
777
|
4 Copyright (C) 2000, 2001, 2002 Ben Wing.
|
428
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
771
|
23
|
428
|
24 /* Synched up with: FSF 19.30 except for some Windows-NT crap. */
|
|
25
|
771
|
26 /* Authorship:
|
|
27
|
|
28 Current primary author: Various
|
|
29
|
|
30 Originally from FSF. Major changes at various times.
|
|
31 Substantially cleaned up by Ben Wing, Dec. 1994 / Jan. 1995.
|
|
32 SIGIO stuff ripped apart and redone by Ben Wing. (during 19.14 devel?)
|
|
33 Signal stuff totally redone by Ben Wing. (during 19.14 devel? that would
|
|
34 be Dec 1995 - Apr 1996.)
|
|
35 Controlling terminal stuff redone by Ben Wing for 19.13.
|
|
36 System call encapsulation stuff written by Ben Wing for 19.12. (1995)
|
|
37 Ripped up and redone avoiding preprocessor tricks Aug - Sep 2001 during
|
|
38 Mule-on-Windows development.
|
|
39 */
|
428
|
40
|
|
41 #include <config.h>
|
|
42 #include "lisp.h"
|
|
43
|
|
44 /* ------------------------------- */
|
|
45 /* basic includes */
|
|
46 /* ------------------------------- */
|
|
47
|
|
48 #ifdef HAVE_TTY
|
|
49 #include "console-tty.h"
|
|
50 #else
|
|
51 #include "syssignal.h"
|
|
52 #include "systty.h"
|
|
53 #endif /* HAVE_TTY */
|
|
54
|
|
55 #include "console-stream.h"
|
|
56
|
|
57 #include "buffer.h"
|
|
58 #include "events.h"
|
|
59 #include "frame.h"
|
|
60 #include "redisplay.h"
|
|
61 #include "process.h"
|
|
62 #include "sysdep.h"
|
|
63 #include "window.h"
|
|
64
|
|
65 #include <setjmp.h>
|
|
66 #include "sysfile.h"
|
|
67 #include "syswait.h"
|
|
68 #include "sysdir.h"
|
|
69 #include "systime.h"
|
|
70 #include "syssignal.h"
|
771
|
71 #include "syspwd.h"
|
442
|
72 #include "sysproc.h"
|
|
73
|
|
74 #ifdef WIN32_NATIVE
|
771
|
75 #include "syswindows.h"
|
428
|
76 #endif
|
|
77
|
|
78 /* ------------------------------- */
|
|
79 /* TTY definitions */
|
|
80 /* ------------------------------- */
|
|
81
|
|
82 #ifdef USG
|
|
83 #include <sys/utsname.h>
|
|
84 #if defined (TIOCGWINSZ) || defined (ISC4_0)
|
|
85 #ifdef NEED_SIOCTL
|
|
86 #include <sys/sioctl.h>
|
|
87 #endif
|
|
88 #ifdef NEED_PTEM_H
|
|
89 #include <sys/stream.h>
|
|
90 #include <sys/ptem.h>
|
|
91 #endif
|
|
92 #endif /* TIOCGWINSZ or ISC4_0 */
|
|
93 #endif /* USG */
|
|
94
|
|
95 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
|
|
96 #ifndef LPASS8
|
|
97 #define LPASS8 0
|
|
98 #endif
|
|
99
|
|
100 #ifndef HAVE_H_ERRNO
|
|
101 int h_errno;
|
|
102 #endif
|
|
103
|
|
104 #ifdef HAVE_TTY
|
|
105
|
|
106 static int baud_convert[] =
|
|
107 #ifdef BAUD_CONVERT
|
|
108 BAUD_CONVERT;
|
|
109 #else
|
|
110 {
|
|
111 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
|
|
112 1800, 2400, 4800, 9600, 19200, 38400
|
|
113 };
|
|
114 #endif
|
|
115
|
|
116 #endif
|
|
117
|
|
118 #ifdef AIXHFT
|
|
119 static void hft_init (struct console *c);
|
|
120 static void hft_reset (struct console *c);
|
|
121 #include <sys/termio.h>
|
|
122 #endif
|
|
123
|
|
124
|
|
125 /************************************************************************/
|
|
126 /* subprocess control */
|
|
127 /************************************************************************/
|
|
128
|
|
129 #ifdef HAVE_TTY
|
|
130
|
|
131 #ifdef SIGTSTP
|
|
132
|
|
133 /* Arrange for character C to be read as the next input from
|
|
134 the terminal. */
|
|
135 void
|
|
136 stuff_char (struct console *con, int c)
|
|
137 {
|
|
138 int input_fd;
|
|
139
|
|
140 assert (CONSOLE_TTY_P (con));
|
|
141 input_fd = CONSOLE_TTY_DATA (con)->infd;
|
|
142 /* Should perhaps error if in batch mode */
|
|
143 #ifdef TIOCSTI
|
|
144 ioctl (input_fd, TIOCSTI, &c);
|
|
145 #else /* no TIOCSTI */
|
563
|
146 invalid_operation ("Cannot stuff terminal input characters in this version of Unix.", Qunbound);
|
428
|
147 #endif /* no TIOCSTI */
|
|
148 }
|
|
149
|
|
150 #endif /* SIGTSTP */
|
|
151
|
|
152 #endif /* HAVE_TTY */
|
|
153
|
|
154 void
|
|
155 set_exclusive_use (int fd)
|
|
156 {
|
|
157 #ifdef FIOCLEX
|
|
158 ioctl (fd, FIOCLEX, 0);
|
|
159 #endif
|
|
160 /* Ok to do nothing if this feature does not exist */
|
|
161 }
|
|
162
|
|
163 void
|
|
164 set_descriptor_non_blocking (int fd)
|
|
165 {
|
|
166 /* Stride people say it's a mystery why this is needed
|
|
167 as well as the O_NDELAY, but that it fails without this. */
|
|
168 /* For AIX: Apparently need this for non-blocking reads on sockets.
|
|
169 It seems that O_NONBLOCK applies only to FIFOs? From
|
|
170 lowry@watson.ibm.com (Andy Lowry). */
|
|
171 /* #### Should this be conditionalized on FIONBIO? */
|
535
|
172 #if defined (STRIDE) || defined (pfa) || defined (AIX)
|
428
|
173 {
|
|
174 int one = 1;
|
|
175 ioctl (fd, FIONBIO, &one);
|
|
176 }
|
|
177 #endif
|
|
178
|
|
179 #ifdef F_SETFL
|
|
180 fcntl (fd, F_SETFL, O_NONBLOCK);
|
|
181 #endif
|
|
182 }
|
|
183
|
|
184 #if defined (NO_SUBPROCESSES)
|
|
185
|
|
186 #ifdef BSD
|
|
187 void
|
|
188 wait_without_blocking (void)
|
|
189 {
|
|
190 wait3 (0, WNOHANG | WUNTRACED, 0);
|
|
191 synch_process_alive = 0;
|
|
192 }
|
|
193 #endif /* BSD */
|
|
194
|
|
195 #endif /* NO_SUBPROCESSES */
|
|
196
|
|
197
|
442
|
198 #ifdef WIN32_NATIVE
|
432
|
199 void wait_for_termination (HANDLE pHandle)
|
|
200 #else
|
|
201 void wait_for_termination (int pid)
|
|
202 #endif
|
428
|
203 {
|
|
204 /* #### With the new improved SIGCHLD handling stuff, there is much
|
|
205 less danger of race conditions and some of the comments below
|
|
206 don't apply. This should be updated. */
|
|
207
|
|
208 #if defined (NO_SUBPROCESSES)
|
|
209 while (1)
|
|
210 {
|
|
211 /* No need to be tricky like below; we can just call wait(). */
|
|
212 /* #### should figure out how to write a wait_allowing_quit().
|
|
213 Since hardly any systems don't have subprocess support,
|
|
214 however, there doesn't seem to be much point. */
|
|
215 if (wait (0) == pid)
|
|
216 return;
|
|
217 }
|
|
218 #elif defined (HAVE_WAITPID)
|
|
219 /* Note that, whenever any subprocess terminates (asynch. or synch.),
|
|
220 the SIGCHLD handler will be called and it will call wait(). Thus
|
|
221 we cannot just call wait() ourselves, and we can't block SIGCHLD
|
|
222 and then call wait(), because then if an asynch. process dies
|
|
223 while we're waiting for our synch. process, Emacs will never
|
|
224 notice that the asynch. process died.
|
|
225
|
|
226 So, the general approach we take is to repeatedly block until a
|
|
227 signal arrives, and then check if our process died using kill
|
|
228 (pid, 0). (We could also check the value of `synch_process_alive',
|
|
229 since the SIGCHLD handler will reset that and we know that we're
|
|
230 only being called on synchronous processes, but this approach is
|
|
231 safer. I don't trust the proper delivery of SIGCHLD.
|
|
232
|
|
233 Note also that we cannot use any form of waitpid(). A loop with
|
|
234 WNOHANG will chew up CPU time; better to use sleep(). A loop
|
|
235 without WNOWAIT will screw up the SIGCHLD handler (actually this
|
|
236 is not true, if you duplicate the exit-status-reaping code; see
|
|
237 below). A loop with WNOWAIT will result in a race condition if
|
|
238 the process terminates between the process-status check and the
|
|
239 call to waitpid(). */
|
|
240
|
|
241 /* Formerly, immediate_quit was set around this function call, but
|
|
242 that could lead to problems if the QUIT happened when SIGCHLD was
|
|
243 blocked -- it would remain blocked. Yet another reason why
|
|
244 immediate_quit is a bad idea. In any case, there is no reason to
|
|
245 resort to this because either the SIGIO or the SIGALRM will stop
|
|
246 the block in EMACS_WAIT_FOR_SIGNAL(). */
|
|
247
|
|
248 /* Apparently there are bugs on some systems with the second method
|
|
249 used below (the EMACS_BLOCK_SIGNAL method), whereby zombie
|
|
250 processes get left around. It appears in those cases that the
|
|
251 SIGCHLD handler is never getting invoked. It's not clear whether
|
|
252 this is an Emacs bug or a kernel bug or both: on HPUX this
|
|
253 problem is observed only with XEmacs, but under Solaris 2.4 all
|
|
254 sorts of different programs have problems with zombies. The
|
|
255 method we use here does not require a working SIGCHLD (but will
|
|
256 not break if it is working), and should be safe. */
|
|
257 /*
|
|
258 We use waitpid(), contrary to the remarks above. There is no
|
|
259 race condition, because the three situations when sigchld_handler
|
|
260 is invoked should be handled OK:
|
|
261
|
|
262 - handler invoked before waitpid(): In this case, subprocess
|
|
263 status will be set by sigchld_handler. waitpid() here will
|
|
264 return -1 with errno set to ECHILD, which is a valid exit
|
|
265 condition.
|
|
266
|
|
267 - handler invoked during waitpid(): as above, except that errno
|
|
268 here will be set to EINTR. This will cause waitpid() to be
|
|
269 called again, and this time it will exit with ECHILD.
|
|
270
|
|
271 - handler invoked after waitpid(): The following code will reap
|
|
272 the subprocess. In the handler, wait() will return -1 because
|
|
273 there is no child to reap, and the handler will exit without
|
|
274 modifying child subprocess status. */
|
|
275 int ret, status;
|
|
276
|
|
277 /* Because the SIGCHLD handler can potentially reap the synchronous
|
|
278 subprocess, we should take care of that. */
|
|
279
|
|
280 /* Will stay in the do loop as long as:
|
|
281 1. Process is alive
|
|
282 2. Ctrl-G is not pressed */
|
|
283 do
|
|
284 {
|
|
285 QUIT;
|
|
286 ret = waitpid (pid, &status, 0);
|
|
287 /* waitpid returns 0 if the process is still alive. */
|
|
288 }
|
|
289 while (ret == 0 || (ret == -1 && errno == EINTR));
|
|
290
|
|
291 if (ret == pid) /* Success */
|
|
292 /* Set synch process globals. This is can also happen
|
|
293 in sigchld_handler, and that code is duplicated. */
|
|
294 {
|
|
295 synch_process_alive = 0;
|
|
296 if (WIFEXITED (status))
|
|
297 synch_process_retcode = WEXITSTATUS (status);
|
|
298 else if (WIFSIGNALED (status))
|
|
299 synch_process_death = signal_name (WTERMSIG (status));
|
|
300 }
|
|
301 /* On exiting the loop, ret will be -1, with errno set to ECHILD if
|
|
302 the child has already been reaped, e.g. in the signal handler. */
|
|
303
|
|
304 /* Otherwise, we've had some error condition here.
|
|
305 Per POSIX, the only other possibilities are:
|
|
306 - EFAULT (bus error accessing arg 2) or
|
|
307 - EINVAL (incorrect arguments),
|
|
308 which are both program bugs.
|
|
309
|
|
310 Since implementations may add their own error indicators on top,
|
|
311 we ignore it by default. */
|
442
|
312 #elif defined (WIN32_NATIVE)
|
432
|
313 int ret = 0, status = 0;
|
|
314 if (pHandle == NULL)
|
|
315 {
|
|
316 warn_when_safe (Qprocess, Qwarning, "Cannot wait for unknown process to terminate");
|
|
317 return;
|
|
318 }
|
|
319 do
|
|
320 {
|
|
321 QUIT;
|
|
322 ret = WaitForSingleObject(pHandle, 100);
|
|
323 }
|
|
324 while (ret == WAIT_TIMEOUT);
|
|
325 if (ret == WAIT_FAILED)
|
|
326 {
|
|
327 warn_when_safe (Qprocess, Qwarning, "waiting for process failed");
|
|
328 }
|
|
329 if (ret == WAIT_ABANDONED)
|
|
330 {
|
|
331 warn_when_safe (Qprocess, Qwarning,
|
|
332 "process to wait for has been abandoned");
|
|
333 }
|
|
334 if (ret == WAIT_OBJECT_0)
|
|
335 {
|
|
336 ret = GetExitCodeProcess(pHandle, &status);
|
|
337 if (ret)
|
|
338 {
|
|
339 synch_process_alive = 0;
|
|
340 synch_process_retcode = status;
|
|
341 }
|
|
342 else
|
|
343 {
|
|
344 /* GetExitCodeProcess() didn't return a valid exit status,
|
|
345 nothing to do. APA */
|
|
346 warn_when_safe (Qprocess, Qwarning,
|
|
347 "failure to obtain process exit value");
|
|
348 }
|
|
349 }
|
440
|
350 if (pHandle != NULL && !CloseHandle(pHandle))
|
432
|
351 {
|
|
352 warn_when_safe (Qprocess, Qwarning,
|
|
353 "failure to close unknown process");
|
|
354 }
|
428
|
355 #elif defined (EMACS_BLOCK_SIGNAL) && !defined (BROKEN_WAIT_FOR_SIGNAL) && defined (SIGCHLD)
|
|
356 while (1)
|
|
357 {
|
|
358 static int wait_debugging = 0; /* Set nonzero to make following
|
|
359 function work under dbx (at least for bsd). */
|
|
360 QUIT;
|
|
361 if (wait_debugging)
|
|
362 return;
|
|
363
|
|
364 EMACS_BLOCK_SIGNAL (SIGCHLD);
|
|
365 /* Block SIGCHLD from happening during this check,
|
|
366 to avoid race conditions. */
|
|
367 if (kill (pid, 0) < 0)
|
|
368 {
|
|
369 EMACS_UNBLOCK_SIGNAL (SIGCHLD);
|
|
370 return;
|
|
371 }
|
|
372 else
|
|
373 /* WARNING: Whatever this macro does *must* not allow SIGCHLD
|
|
374 to happen between the time that it's reenabled and when we
|
|
375 begin to block. Otherwise we may end up blocking for a
|
|
376 signal that has already arrived and isn't coming again.
|
|
377 Can you say "race condition"?
|
|
378
|
|
379 I assume that the system calls sigpause() or sigsuspend()
|
|
380 to provide this atomicness. If you're getting hangs in
|
|
381 sigpause()/sigsuspend(), then your OS doesn't implement
|
|
382 this properly (this applies under hpux9, for example).
|
|
383 Try defining BROKEN_WAIT_FOR_SIGNAL. */
|
|
384 EMACS_WAIT_FOR_SIGNAL (SIGCHLD);
|
|
385 }
|
442
|
386 #else /* not HAVE_WAITPID and not WIN32_NATIVE and (not EMACS_BLOCK_SIGNAL or BROKEN_WAIT_FOR_SIGNAL) */
|
428
|
387 /* This approach is kind of cheesy but is guaranteed(?!) to work
|
|
388 for all systems. */
|
|
389 while (1)
|
|
390 {
|
|
391 QUIT;
|
|
392 if (kill (pid, 0) < 0)
|
|
393 return;
|
771
|
394 stop_interrupts ();
|
|
395 sleep (1);
|
|
396 start_interrupts ();
|
428
|
397 }
|
|
398 #endif /* OS features */
|
|
399 }
|
|
400
|
|
401
|
|
402 #if !defined (NO_SUBPROCESSES)
|
|
403
|
|
404 /*
|
|
405 * flush any pending output
|
|
406 * (may flush input as well; it does not matter the way we use it)
|
|
407 */
|
|
408
|
|
409 void
|
|
410 flush_pending_output (int channel)
|
|
411 {
|
|
412 #ifdef HAVE_TERMIOS
|
|
413 /* If we try this, we get hit with SIGTTIN, because
|
|
414 the child's tty belongs to the child's pgrp. */
|
|
415 #elif defined (TCFLSH)
|
|
416 ioctl (channel, TCFLSH, 1);
|
|
417 #elif defined (TIOCFLUSH)
|
|
418 int zero = 0;
|
|
419 /* 3rd arg should be ignored
|
|
420 but some 4.2 kernels actually want the address of an int
|
|
421 and nonzero means something different. */
|
|
422 ioctl (channel, TIOCFLUSH, &zero);
|
|
423 #endif
|
|
424 }
|
|
425
|
442
|
426 #ifndef WIN32_NATIVE
|
428
|
427 /* Set up the terminal at the other end of a pseudo-terminal that
|
|
428 we will be controlling an inferior through.
|
|
429 It should not echo or do line-editing, since that is done
|
|
430 in Emacs. No padding needed for insertion into an Emacs buffer. */
|
|
431
|
|
432 void
|
|
433 child_setup_tty (int out)
|
|
434 {
|
|
435 struct emacs_tty s;
|
430
|
436 emacs_get_tty (out, &s);
|
428
|
437
|
|
438 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
|
|
439 assert (isatty(out));
|
|
440 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
|
|
441 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
|
513
|
442
|
|
443 {
|
|
444 /* Disable all output delays. */
|
|
445 tcflag_t delay_mask = 0;
|
428
|
446 #ifdef NLDLY
|
513
|
447 delay_mask |= NLDLY;
|
|
448 #endif
|
|
449 #ifdef CRDLY
|
|
450 delay_mask |= CRDLY;
|
|
451 #endif
|
|
452 #ifdef TABDLY
|
|
453 delay_mask |= TABDLY; /* Also disables tab expansion (Posix). */
|
|
454 #endif
|
|
455 #ifdef BSDLY
|
|
456 delay_mask |= BSDLY;
|
|
457 #endif
|
|
458 #ifdef VTDLY
|
|
459 delay_mask |= VTDLY;
|
428
|
460 #endif
|
513
|
461 #ifdef FFDLY
|
|
462 delay_mask |= FFDLY;
|
|
463 #endif
|
|
464 s.main.c_oflag &= ~delay_mask;
|
|
465 }
|
|
466
|
|
467 #ifdef OXTABS
|
|
468 /* Posix defines the TAB3 value for TABDLY to mean: expand tabs to spaces.
|
|
469 On those systems tab expansion would be disabled by the above code.
|
|
470 BSD systems use an independent flag, OXTABS. */
|
|
471 s.main.c_oflag &= ~OXTABS; /* Disable tab expansion */
|
|
472 #endif
|
|
473
|
428
|
474 s.main.c_lflag &= ~ECHO; /* Disable echo */
|
|
475 s.main.c_lflag |= ISIG; /* Enable signals */
|
|
476 #ifdef IUCLC
|
|
477 s.main.c_iflag &= ~IUCLC; /* Disable downcasing on input. */
|
|
478 #endif
|
|
479 #ifdef OLCUC
|
|
480 s.main.c_oflag &= ~OLCUC; /* Disable upcasing on output. */
|
|
481 #endif
|
513
|
482
|
428
|
483 #if defined (CSIZE) && defined (CS8)
|
|
484 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
|
|
485 #endif
|
|
486 #ifdef ISTRIP
|
|
487 s.main.c_iflag &= ~ISTRIP; /* Don't strip 8th bit on input */
|
|
488 #endif
|
|
489 #if 0
|
|
490 /* Unnecessary as long as ICANON is set */
|
|
491 s.main.c_cc[VMIN] = 1; /* minimum number of characters to accept */
|
|
492 s.main.c_cc[VTIME] = 0; /* wait forever for at least 1 character */
|
|
493 #endif /* 0 */
|
|
494
|
|
495 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
|
|
496 s.main.c_cc[VEOF] = 04; /* ensure that EOF is Control-D */
|
|
497 s.main.c_cc[VERASE] = _POSIX_VDISABLE; /* disable erase processing */
|
|
498 s.main.c_cc[VKILL] = _POSIX_VDISABLE; /* disable kill processing */
|
|
499
|
|
500 #ifdef HPUX
|
|
501 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
|
|
502 #endif /* HPUX */
|
|
503
|
|
504 #ifdef AIX
|
|
505 #ifndef IBMR2AIX
|
|
506 /* AIX enhanced edit loses NULs, so disable it. */
|
|
507 s.main.c_line = 0;
|
|
508 s.main.c_iflag &= ~ASCEDIT;
|
|
509 #endif /* IBMR2AIX */
|
|
510 /* Also, PTY overloads NUL and BREAK.
|
|
511 don't ignore break, but don't signal either, so it looks like NUL.
|
|
512 This really serves a purpose only if running in an XTERM window
|
|
513 or via TELNET or the like, but does no harm elsewhere. */
|
|
514 s.main.c_iflag &= ~IGNBRK;
|
|
515 s.main.c_iflag &= ~BRKINT;
|
|
516 #endif /* AIX */
|
|
517 #ifdef SIGNALS_VIA_CHARACTERS
|
|
518 /* TTY `special characters' are used in process_send_signal
|
|
519 so set them here to something useful. */
|
|
520 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
|
|
521 s.main.c_cc[VINTR] = 'C' &037; /* Control-C */
|
|
522 s.main.c_cc[VSUSP] = 'Z' &037; /* Control-Z */
|
|
523 #else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
|
|
524 /* TTY `special characters' work better as signals, so disable
|
|
525 character forms */
|
|
526 s.main.c_cc[VQUIT] = _POSIX_VDISABLE;
|
|
527 s.main.c_cc[VINTR] = _POSIX_VDISABLE;
|
|
528 s.main.c_cc[VSUSP] = _POSIX_VDISABLE;
|
|
529 s.main.c_lflag &= ~ISIG;
|
|
530 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
|
|
531 s.main.c_cc[VEOL] = _POSIX_VDISABLE;
|
|
532 #if defined (CBAUD)
|
440
|
533 /* <mdiers> #### This is not portable. ###
|
428
|
534 POSIX does not specify CBAUD, and 4.4BSD does not have it.
|
|
535 Instead, POSIX suggests to use cfset{i,o}speed().
|
|
536 [cf. D. Lewine, POSIX Programmer's Guide, Chapter 8: Terminal
|
|
537 I/O, O'Reilly 1991] */
|
|
538 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
|
|
539 #else
|
|
540 /* <mdiers> What to do upon failure? Just ignoring rc is probably
|
|
541 not acceptable, is it? */
|
|
542 if (cfsetispeed (&s.main, B9600) == -1) /* ignore */;
|
|
543 if (cfsetospeed (&s.main, B9600) == -1) /* ignore */;
|
|
544 #endif /* defined (CBAUD) */
|
|
545
|
|
546 #else /* not HAVE_TERMIO */
|
|
547
|
|
548 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
|
|
549 | CBREAK | TANDEM);
|
|
550 s.main.sg_flags |= LPASS8;
|
|
551 s.main.sg_erase = 0377;
|
|
552 s.main.sg_kill = 0377;
|
|
553 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */
|
|
554
|
|
555 #endif /* not HAVE_TERMIO */
|
430
|
556 emacs_set_tty (out, &s, 0);
|
428
|
557
|
|
558 #ifdef RTU
|
|
559 {
|
|
560 int zero = 0;
|
|
561 ioctl (out, FIOASYNC, &zero);
|
|
562 }
|
|
563 #endif /* RTU */
|
|
564 }
|
442
|
565 #endif /* WIN32_NATIVE */
|
428
|
566
|
|
567 #endif /* not NO_SUBPROCESSES */
|
|
568
|
|
569
|
|
570 #if !defined (SIGTSTP) && !defined (USG_JOBCTRL)
|
|
571
|
|
572 #if defined(__STDC__) || defined(_MSC_VER)
|
|
573 #define SIG_PARAM_TYPE int
|
|
574 #else
|
|
575 #define SIG_PARAM_TYPE
|
|
576 #endif
|
|
577
|
|
578 /* Record a signal code and the handler for it. */
|
|
579 struct save_signal
|
|
580 {
|
|
581 int code;
|
|
582 SIGTYPE (*handler) (SIG_PARAM_TYPE);
|
|
583 };
|
|
584
|
|
585 static void
|
|
586 save_signal_handlers (struct save_signal *saved_handlers)
|
|
587 {
|
|
588 while (saved_handlers->code)
|
|
589 {
|
|
590 saved_handlers->handler
|
613
|
591 = (SIGTYPE (*) (SIG_PARAM_TYPE)) EMACS_SIGNAL (saved_handlers->code, SIG_IGN);
|
428
|
592 saved_handlers++;
|
|
593 }
|
|
594 }
|
|
595
|
|
596 static void
|
|
597 restore_signal_handlers (struct save_signal *saved_handlers)
|
|
598 {
|
|
599 while (saved_handlers->code)
|
|
600 {
|
613
|
601 EMACS_SIGNAL (saved_handlers->code, saved_handlers->handler);
|
428
|
602 saved_handlers++;
|
|
603 }
|
|
604 }
|
|
605
|
|
606
|
|
607 /* Fork a subshell. */
|
|
608 static void
|
|
609 sys_subshell (void)
|
|
610 {
|
|
611 Lisp_Object dir;
|
771
|
612 Intbyte *str = 0;
|
|
613 Bytecount len;
|
428
|
614 struct gcpro gcpro1;
|
771
|
615 Intbyte *sh = 0;
|
|
616 Extbyte *shext;
|
|
617
|
|
618 /* Use our buffer's default directory for the subshell. */
|
|
619
|
|
620 /* Note: These calls are spread out to insure that the return values
|
|
621 of the calls (which may be newly-created strings) are properly
|
|
622 GC-protected. */
|
|
623
|
428
|
624 GCPRO1 (dir);
|
771
|
625
|
|
626 dir = current_buffer->directory;
|
|
627 /* If the current dir has no terminating slash, we'll get undesirable
|
|
628 results, so put the slash back. */
|
|
629 dir = Ffile_name_as_directory (dir);
|
428
|
630 dir = Funhandled_file_name_directory (dir);
|
|
631 dir = expand_and_dir_to_file (dir, Qnil);
|
771
|
632
|
|
633 str = (Intbyte *) alloca (XSTRING_LENGTH (dir) + 2);
|
428
|
634 len = XSTRING_LENGTH (dir);
|
|
635 memcpy (str, XSTRING_DATA (dir), len);
|
442
|
636 if (!IS_ANY_SEP (str[len - 1]))
|
|
637 str[len++] = DIRECTORY_SEP;
|
428
|
638 str[len] = 0;
|
771
|
639
|
|
640 if (sh == 0)
|
|
641 sh = egetenv ("SHELL");
|
|
642 if (sh == 0)
|
|
643 sh = "sh";
|
|
644
|
|
645 C_STRING_TO_EXTERNAL (sh, shext, Qfile_name);
|
|
646
|
|
647 UNGCPRO;
|
428
|
648
|
442
|
649 #ifdef WIN32_NATIVE
|
|
650
|
771
|
651 if (str)
|
|
652 qxe_chdir (str);
|
|
653
|
|
654 /* Waits for process completion */
|
|
655 if (_spawnlp (_P_WAIT, shext, shext, NULL) != 0)
|
|
656 report_process_error ("Can't spawn subshell", Qunbound);
|
|
657 else
|
|
658 return; /* we're done, no need to wait for termination */
|
|
659
|
|
660 #else /* not WIN32_NATIVE */
|
|
661
|
|
662 {
|
|
663 int pid;
|
|
664 struct save_signal saved_handlers[5];
|
|
665
|
|
666 saved_handlers[0].code = SIGINT;
|
|
667 saved_handlers[1].code = SIGQUIT;
|
|
668 saved_handlers[2].code = SIGTERM;
|
|
669 #ifdef SIGIO
|
|
670 saved_handlers[3].code = SIGIO;
|
|
671 saved_handlers[4].code = 0;
|
|
672 #else
|
|
673 saved_handlers[3].code = 0;
|
|
674 #endif
|
|
675
|
|
676 pid = fork ();
|
|
677
|
|
678 if (pid == -1)
|
563
|
679 report_process_error ("Can't spawn subshell", Qunbound);
|
771
|
680 if (pid == 0)
|
|
681 {
|
|
682 if (str)
|
|
683 qxe_chdir (str);
|
442
|
684
|
|
685 #if !defined (NO_SUBPROCESSES)
|
771
|
686 close_process_descs (); /* Close Emacs's pipes/ptys */
|
428
|
687 #endif
|
|
688
|
|
689 #ifdef SET_EMACS_PRIORITY
|
771
|
690 if (emacs_priority != 0)
|
|
691 nice (-emacs_priority); /* Give the new shell the default priority */
|
428
|
692 #endif
|
|
693
|
771
|
694 execlp (shext, shext, 0);
|
|
695 retry_write (1, "Can't execute subshell", 22);
|
|
696 _exit (1);
|
|
697 }
|
|
698
|
|
699 save_signal_handlers (saved_handlers);
|
|
700 synch_process_alive = 1;
|
|
701 wait_for_termination (pid);
|
|
702 restore_signal_handlers (saved_handlers);
|
428
|
703 }
|
|
704
|
442
|
705 #endif /* not WIN32_NATIVE */
|
428
|
706 }
|
|
707
|
|
708 #endif /* !defined (SIGTSTP) && !defined (USG_JOBCTRL) */
|
|
709
|
|
710
|
|
711
|
|
712 /* Suspend the Emacs process; give terminal to its superior. */
|
|
713 void
|
|
714 sys_suspend (void)
|
|
715 {
|
|
716 #if defined (SIGTSTP)
|
|
717 {
|
|
718 int pgrp = EMACS_GET_PROCESS_GROUP ();
|
|
719 EMACS_KILLPG (pgrp, SIGTSTP);
|
|
720 }
|
|
721
|
|
722 #elif defined (USG_JOBCTRL)
|
|
723 /* If you don't know what this is don't mess with it */
|
|
724 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
|
|
725 kill (getpid (), SIGQUIT);
|
|
726
|
|
727 #else /* No SIGTSTP or USG_JOBCTRL */
|
|
728
|
|
729 /* On a system where suspending is not implemented,
|
|
730 instead fork a subshell and let it talk directly to the terminal
|
|
731 while we wait. */
|
|
732 sys_subshell ();
|
|
733
|
|
734 #endif
|
|
735 }
|
|
736
|
|
737 /* Suspend a process if possible; give terminal to its superior. */
|
|
738 void
|
|
739 sys_suspend_process (int process)
|
|
740 {
|
|
741 /* I don't doubt that it is possible to suspend processes on
|
|
742 * VMS machines or thost that use USG_JOBCTRL,
|
|
743 * but I don't know how to do it, so...
|
|
744 */
|
|
745 #if defined (SIGTSTP)
|
|
746 kill(process, SIGTSTP);
|
|
747 #endif
|
|
748 }
|
|
749
|
|
750
|
|
751 /* Given FD, obtain pty buffer size. When no luck, a good guess is made,
|
442
|
752 so that the function works even when fd is not a pty. */
|
428
|
753
|
|
754 int
|
|
755 get_pty_max_bytes (int fd)
|
|
756 {
|
442
|
757 /* DEC OSF 4.0 fpathconf returns 255, but xemacs hangs on long shell
|
|
758 input lines if we return 253. 252 is OK!. So let's leave a bit
|
|
759 of slack for the newline that xemacs will insert, and for those
|
|
760 inevitable vendor off-by-one-or-two-or-three bugs. */
|
|
761 #define MAX_CANON_SLACK 10
|
|
762 #define SAFE_MAX_CANON (127 - MAX_CANON_SLACK)
|
428
|
763 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
|
442
|
764 {
|
|
765 int max_canon = fpathconf (fd, _PC_MAX_CANON);
|
|
766 #ifdef __hpux__
|
|
767 /* HP-UX 10.20 fpathconf returns 768, but this results in
|
|
768 truncated input lines, while 255 works. */
|
|
769 if (max_canon > 255) max_canon = 255;
|
428
|
770 #endif
|
442
|
771 return (max_canon < 0 ? SAFE_MAX_CANON :
|
|
772 max_canon > SAFE_MAX_CANON ? max_canon - MAX_CANON_SLACK :
|
|
773 max_canon);
|
|
774 }
|
|
775 #elif defined (_POSIX_MAX_CANON)
|
|
776 return (_POSIX_MAX_CANON > SAFE_MAX_CANON ?
|
|
777 _POSIX_MAX_CANON - MAX_CANON_SLACK :
|
|
778 _POSIX_MAX_CANON);
|
|
779 #else
|
|
780 return SAFE_MAX_CANON;
|
|
781 #endif
|
428
|
782 }
|
|
783
|
|
784 /* Figure out the eof character for the FD. */
|
|
785
|
665
|
786 Intbyte
|
428
|
787 get_eof_char (int fd)
|
|
788 {
|
665
|
789 const Intbyte ctrl_d = (Intbyte) '\004';
|
428
|
790
|
|
791 if (!isatty (fd))
|
|
792 return ctrl_d;
|
|
793 #ifdef HAVE_TERMIOS
|
|
794 {
|
|
795 struct termios t;
|
|
796 tcgetattr (fd, &t);
|
|
797 #if 0
|
|
798 /* What is the following line designed to do??? -mrb */
|
647
|
799 if ((int) strlen ((const char *) t.c_cc) < (VEOF + 1))
|
428
|
800 return ctrl_d;
|
|
801 else
|
665
|
802 return (Intbyte) t.c_cc[VEOF];
|
428
|
803 #endif
|
665
|
804 return t.c_cc[VEOF] == _POSIX_VDISABLE ? ctrl_d : (Intbyte) t.c_cc[VEOF];
|
428
|
805 }
|
|
806 #else /* ! HAVE_TERMIOS */
|
|
807 /* On Berkeley descendants, the following IOCTL's retrieve the
|
|
808 current control characters. */
|
|
809 #if defined (TIOCGETC)
|
|
810 {
|
|
811 struct tchars c;
|
|
812 ioctl (fd, TIOCGETC, &c);
|
665
|
813 return (Intbyte) c.t_eofc;
|
428
|
814 }
|
|
815 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
|
|
816 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
|
|
817 characters. */
|
|
818 #ifdef TCGETA
|
|
819 {
|
|
820 struct termio t;
|
|
821 ioctl (fd, TCGETA, &t);
|
647
|
822 if ((int) strlen ((const char *) t.c_cc) < (VINTR + 1))
|
428
|
823 return ctrl_d;
|
|
824 else
|
665
|
825 return (Intbyte) t.c_cc[VINTR];
|
428
|
826 }
|
|
827 #else /* ! defined (TCGETA) */
|
|
828 /* Rather than complain, we'll just guess ^D, which is what
|
|
829 * earlier emacsen always used. */
|
|
830 return ctrl_d;
|
|
831 #endif /* ! defined (TCGETA) */
|
|
832 #endif /* ! defined (TIOCGETC) */
|
|
833 #endif /* ! defined (HAVE_TERMIOS) */
|
|
834 }
|
|
835
|
|
836 /* Set the logical window size associated with descriptor FD
|
|
837 to HEIGHT and WIDTH. This is used mainly with ptys. */
|
|
838
|
|
839 int
|
|
840 set_window_size (int fd, int height, int width)
|
|
841 {
|
|
842 #ifdef TIOCSWINSZ
|
|
843
|
|
844 /* BSD-style. */
|
|
845 struct winsize size;
|
|
846 size.ws_row = height;
|
|
847 size.ws_col = width;
|
|
848
|
|
849 if (ioctl (fd, TIOCSWINSZ, &size) == -1)
|
|
850 return 0; /* error */
|
|
851 else
|
|
852 return 1;
|
|
853
|
|
854 #elif defined (TIOCSSIZE)
|
|
855
|
|
856 /* SunOS - style. */
|
|
857 struct ttysize size;
|
|
858 size.ts_lines = height;
|
|
859 size.ts_cols = width;
|
|
860
|
|
861 if (ioctl (fd, TIOCGSIZE, &size) == -1)
|
|
862 return 0;
|
|
863 else
|
|
864 return 1;
|
|
865 #else
|
|
866 return -1;
|
|
867 #endif
|
|
868 }
|
|
869
|
|
870 /* Set up the proper status flags for use of a pty. */
|
|
871
|
|
872 void
|
|
873 setup_pty (int fd)
|
|
874 {
|
|
875 #ifdef IBMRTAIX
|
|
876 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
|
|
877 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
|
|
878 /* cause EMACS not to die when it should, i.e., when its own controlling */
|
|
879 /* tty goes away. I've complained to the AIX developers, and they may */
|
|
880 /* change this behavior, but I'm not going to hold my breath. */
|
613
|
881 EMACS_SIGNAL (SIGHUP, SIG_IGN);
|
535
|
882 #endif /* IBMRTAIX */
|
|
883
|
428
|
884 #ifdef TIOCPKT
|
|
885 /* In some systems (Linux through 2.0.0, at least), packet mode doesn't
|
|
886 get cleared when a pty is closed, so we need to clear it here.
|
|
887 Linux pre2.0.13 contained an attempted fix for this (from Ted Ts'o,
|
|
888 tytso@mit.edu), but apparently it messed up rlogind and telnetd, so he
|
|
889 removed the fix in pre2.0.14. - dkindred@cs.cmu.edu
|
|
890 */
|
|
891 {
|
|
892 int off = 0;
|
|
893 ioctl (fd, TIOCPKT, (char *)&off);
|
|
894 }
|
535
|
895 #endif /* TIOCPKT */
|
428
|
896 }
|
|
897
|
|
898
|
|
899 /************************************************************************/
|
|
900 /* TTY control */
|
|
901 /************************************************************************/
|
|
902
|
|
903 /* ------------------------------------------------------ */
|
|
904 /* get baud rate */
|
|
905 /* ------------------------------------------------------ */
|
|
906
|
|
907 /* It really makes more sense for the baud-rate to be console-specific
|
|
908 and not device-specific, but it's (at least potentially) used for output
|
|
909 decisions. */
|
|
910
|
|
911 void
|
|
912 init_baud_rate (struct device *d)
|
|
913 {
|
|
914 if (DEVICE_WIN_P (d) || DEVICE_STREAM_P (d))
|
|
915 {
|
|
916 DEVICE_BAUD_RATE (d) = 38400;
|
|
917 return;
|
|
918 }
|
|
919
|
|
920 #ifdef HAVE_TTY
|
|
921 assert (DEVICE_TTY_P (d));
|
|
922 {
|
647
|
923 struct console *con = XCONSOLE (DEVICE_CONSOLE (d));
|
428
|
924 int input_fd = CONSOLE_TTY_DATA (con)->infd;
|
442
|
925 #if defined (WIN32_NATIVE)
|
428
|
926 DEVICE_TTY_DATA (d)->ospeed = 15;
|
|
927 #elif defined (HAVE_TERMIOS)
|
|
928 struct termios sg;
|
|
929
|
|
930 sg.c_cflag = B9600;
|
|
931 tcgetattr (input_fd, &sg);
|
|
932 DEVICE_TTY_DATA (d)->ospeed = cfgetospeed (&sg);
|
|
933 # if defined (USE_GETOBAUD) && defined (getobaud)
|
|
934 /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
|
|
935 if (DEVICE_TTY_DATA (d)->ospeed == 0)
|
|
936 DEVICE_TTY_DATA (d)->ospeed = getobaud (sg.c_cflag);
|
|
937 # endif
|
|
938 #elif defined (HAVE_TERMIO)
|
|
939 struct termio sg;
|
|
940
|
|
941 sg.c_cflag = B9600;
|
|
942 # ifdef HAVE_TCATTR
|
|
943 tcgetattr (input_fd, &sg);
|
|
944 # else
|
|
945 ioctl (input_fd, TCGETA, &sg);
|
|
946 # endif
|
|
947 DEVICE_TTY_DATA (d)->ospeed = sg.c_cflag & CBAUD;
|
|
948 #else /* neither TERMIOS nor TERMIO */
|
|
949 struct sgttyb sg;
|
|
950
|
|
951 sg.sg_ospeed = B9600;
|
|
952 if (ioctl (input_fd, TIOCGETP, &sg) < 0)
|
|
953 abort ();
|
|
954 DEVICE_TTY_DATA (d)->ospeed = sg.sg_ospeed;
|
|
955 #endif
|
|
956 }
|
|
957
|
|
958 DEVICE_BAUD_RATE (d) =
|
|
959 (DEVICE_TTY_DATA (d)->ospeed < countof (baud_convert)
|
|
960 ? baud_convert[DEVICE_TTY_DATA (d)->ospeed]
|
|
961 : 9600);
|
|
962
|
|
963 if (DEVICE_BAUD_RATE (d) == 0)
|
|
964 DEVICE_BAUD_RATE (d) = 1200;
|
|
965 #endif /* HAVE_TTY */
|
|
966 }
|
|
967
|
|
968
|
|
969 /* ------------------------------------------------------ */
|
|
970 /* SIGIO control */
|
|
971 /* ------------------------------------------------------ */
|
|
972
|
|
973 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
|
|
974
|
|
975 static void
|
|
976 init_sigio_on_device (struct device *d)
|
|
977 {
|
|
978 int filedesc = DEVICE_INFD (d);
|
|
979
|
|
980 #if defined (FIOSSAIOOWN)
|
|
981 { /* HPUX stuff */
|
|
982 int owner = getpid ();
|
|
983 int ioctl_status;
|
|
984 if (DEVICE_TTY_P (d))
|
|
985 {
|
|
986 ioctl_status = ioctl (filedesc, FIOGSAIOOWN,
|
|
987 &DEVICE_OLD_FCNTL_OWNER (d));
|
|
988 ioctl_status = ioctl (filedesc, FIOSSAIOOWN, &owner);
|
|
989 }
|
|
990 #ifdef HAVE_WINDOW_SYSTEM
|
|
991 else if (!DEVICE_STREAM_P (d))
|
|
992 {
|
|
993 ioctl_status = ioctl (filedesc, SIOCGPGRP,
|
|
994 &DEVICE_OLD_FCNTL_OWNER (d));
|
|
995 ioctl_status = ioctl (filedesc, SIOCSPGRP, &owner);
|
|
996 }
|
|
997 #endif
|
|
998 }
|
|
999 #elif defined (F_SETOWN) && !defined (F_SETOWN_BUG)
|
|
1000 DEVICE_OLD_FCNTL_OWNER (d) = fcntl (filedesc, F_GETOWN, 0);
|
|
1001 # ifdef F_SETOWN_SOCK_NEG
|
|
1002 /* stdin is a socket here */
|
|
1003 fcntl (filedesc, F_SETOWN, -getpid ());
|
|
1004 # else
|
|
1005 fcntl (filedesc, F_SETOWN, getpid ());
|
|
1006 # endif
|
|
1007 #endif
|
|
1008 }
|
|
1009
|
|
1010 static void
|
|
1011 reset_sigio_on_device (struct device *d)
|
|
1012 {
|
|
1013 int filedesc = DEVICE_INFD (d);
|
|
1014
|
|
1015 #if defined (FIOSSAIOOWN)
|
|
1016 { /* HPUX stuff */
|
|
1017 int ioctl_status;
|
|
1018 if (DEVICE_TTY_P (d))
|
|
1019 {
|
|
1020 ioctl_status = ioctl (filedesc, FIOSSAIOOWN,
|
|
1021 &DEVICE_OLD_FCNTL_OWNER (d));
|
|
1022 }
|
|
1023 #ifdef HAVE_WINDOW_SYSTEM
|
|
1024 else if (!DEVICE_STREAM_P (d))
|
|
1025 {
|
|
1026 ioctl_status = ioctl (filedesc, SIOCSPGRP,
|
|
1027 &DEVICE_OLD_FCNTL_OWNER (d));
|
|
1028 }
|
|
1029 #endif
|
|
1030 }
|
|
1031 #elif defined (F_SETOWN) && !defined (F_SETOWN_BUG)
|
|
1032 fcntl (filedesc, F_SETOWN, DEVICE_OLD_FCNTL_OWNER (d));
|
|
1033 #endif
|
|
1034 }
|
|
1035
|
|
1036 static void
|
|
1037 request_sigio_on_device (struct device *d)
|
|
1038 {
|
|
1039 int filedesc = DEVICE_INFD (d);
|
|
1040
|
502
|
1041 /* NOTE: It appears that Linux has its own mechanism for requesting
|
|
1042 SIGIO, using the F_GETSIG and F_SETSIG commands to fcntl().
|
|
1043 These let you pick which signal you want sent (not just SIGIO),
|
|
1044 and if you do this, you get additional info which tells you which
|
|
1045 file descriptor has input ready on it. The man page says:
|
|
1046
|
|
1047 Using these mechanisms, a program can implement fully
|
|
1048 asynchronous I/O without using select(2) or poll(2) most
|
|
1049 of the time.
|
|
1050
|
|
1051 The use of O_ASYNC, F_GETOWN, F_SETOWN is specific to BSD
|
|
1052 and Linux. F_GETSIG and F_SETSIG are Linux-specific.
|
|
1053 POSIX has asynchronous I/O and the aio_sigevent structure
|
|
1054 to achieve similar things; these are also available in
|
|
1055 Linux as part of the GNU C Library (Glibc).
|
|
1056
|
|
1057 But it appears that Linux also supports O_ASYNC, so I see no
|
|
1058 particular need to switch. --ben
|
|
1059 */
|
|
1060
|
|
1061 #if defined (I_SETSIG) && !defined (HPUX10) && !defined (LINUX)
|
428
|
1062 {
|
502
|
1063 int events = 0;
|
428
|
1064 ioctl (filedesc, I_GETSIG, &events);
|
|
1065 ioctl (filedesc, I_SETSIG, events | S_INPUT);
|
|
1066 }
|
502
|
1067 #elif defined (O_ASYNC)
|
|
1068 /* Generally FASYNC and O_ASYNC are both defined, and both equal;
|
|
1069 but let's not depend on that. O_ASYNC appears to be more
|
|
1070 standard (at least the Linux include files think so), so
|
|
1071 check it first. */
|
|
1072 fcntl (filedesc, F_SETFL, fcntl (filedesc, F_GETFL, 0) | O_ASYNC);
|
428
|
1073 #elif defined (FASYNC)
|
|
1074 fcntl (filedesc, F_SETFL, fcntl (filedesc, F_GETFL, 0) | FASYNC);
|
|
1075 #elif defined (FIOSSAIOSTAT)
|
|
1076 {
|
|
1077 /* DG: Changed for HP-UX. HP-UX uses different IOCTLs for
|
|
1078 sockets and other devices for some bizarre reason. We guess
|
|
1079 that an X device is a socket, and tty devices aren't. We then
|
|
1080 use the following crud to do the appropriate thing. */
|
|
1081 int on = 1;
|
|
1082 int ioctl_status; /* ####DG: check if IOCTL succeeds here. */
|
|
1083
|
|
1084 if (DEVICE_TTY_P (d))
|
|
1085 {
|
|
1086 ioctl_status = ioctl (filedesc, FIOSSAIOSTAT, &on);
|
|
1087 }
|
|
1088 #ifdef HAVE_WINDOW_SYSTEM
|
|
1089 else if (!DEVICE_STREAM_P (d))
|
|
1090 {
|
|
1091 ioctl_status = ioctl (filedesc, FIOASYNC, &on);
|
|
1092 }
|
|
1093 #endif
|
|
1094 }
|
|
1095 #elif defined (FIOASYNC)
|
|
1096 {
|
|
1097 int on = 1;
|
|
1098 ioctl (filedesc, FIOASYNC, &on);
|
|
1099 }
|
|
1100 #endif
|
|
1101
|
|
1102 #if defined (_CX_UX) /* #### Is this crap necessary? */
|
|
1103 EMACS_UNBLOCK_SIGNAL (SIGIO);
|
|
1104 #endif
|
|
1105 }
|
|
1106
|
|
1107 static void
|
|
1108 unrequest_sigio_on_device (struct device *d)
|
|
1109 {
|
|
1110 int filedesc = DEVICE_INFD (d);
|
|
1111
|
502
|
1112 #if defined (I_SETSIG) && !defined (HPUX10) && !defined (LINUX)
|
428
|
1113 {
|
502
|
1114 int events = 0;
|
428
|
1115 ioctl (filedesc, I_GETSIG, &events);
|
|
1116 ioctl (filedesc, I_SETSIG, events & ~S_INPUT);
|
|
1117 }
|
502
|
1118 #elif defined (O_ASYNC)
|
|
1119 fcntl (filedesc, F_SETFL, fcntl (filedesc, F_GETFL, 0) & ~O_ASYNC);
|
428
|
1120 #elif defined (FASYNC)
|
|
1121 fcntl (filedesc, F_SETFL, fcntl (filedesc, F_GETFL, 0) & ~FASYNC);
|
|
1122 #elif defined (FIOSSAIOSTAT)
|
|
1123 {
|
|
1124 /* DG: Changed for HP-UX. HP-UX uses different IOCTLs for
|
|
1125 sockets and other devices for some bizarre reason. We guess
|
|
1126 that an X device is a socket, and tty devices aren't. We then
|
|
1127 use the following crud to do the appropriate thing. */
|
|
1128
|
|
1129 int off = 0;
|
|
1130 int ioctl_status;
|
|
1131
|
|
1132 /* See comment for request_sigio_on_device */
|
|
1133
|
|
1134 if (DEVICE_TTY_P (d))
|
|
1135 {
|
|
1136 ioctl_status = ioctl (filedesc, FIOSSAIOSTAT, &off);
|
|
1137 }
|
|
1138 else
|
|
1139 {
|
|
1140 ioctl_status = ioctl (filedesc, FIOASYNC, &off);
|
|
1141 }
|
|
1142 }
|
|
1143 #elif defined (FIOASYNC)
|
|
1144 {
|
|
1145 int off = 0;
|
|
1146 ioctl (filedesc, FIOASYNC, &off);
|
|
1147 }
|
|
1148 #endif
|
|
1149 }
|
|
1150
|
|
1151 void
|
|
1152 request_sigio (void)
|
|
1153 {
|
|
1154 Lisp_Object devcons, concons;
|
|
1155
|
|
1156 DEVICE_LOOP_NO_BREAK (devcons, concons)
|
|
1157 {
|
|
1158 struct device *d;
|
|
1159
|
|
1160 d = XDEVICE (XCAR (devcons));
|
|
1161
|
|
1162 if (!DEVICE_STREAM_P (d))
|
|
1163 request_sigio_on_device (d);
|
|
1164 }
|
|
1165 }
|
|
1166
|
|
1167 void
|
|
1168 unrequest_sigio (void)
|
|
1169 {
|
|
1170 Lisp_Object devcons, concons;
|
|
1171
|
|
1172 DEVICE_LOOP_NO_BREAK (devcons, concons)
|
|
1173 {
|
|
1174 struct device *d;
|
|
1175
|
|
1176 d = XDEVICE (XCAR (devcons));
|
|
1177
|
|
1178 if (!DEVICE_STREAM_P (d))
|
|
1179 unrequest_sigio_on_device (d);
|
|
1180 }
|
|
1181 }
|
|
1182
|
|
1183 #endif /* SIGIO */
|
|
1184
|
|
1185 /* ------------------------------------------------------ */
|
|
1186 /* Changing Emacs's process group */
|
|
1187 /* ------------------------------------------------------ */
|
|
1188
|
|
1189 /* Saving and restoring the process group of Emacs's terminal. */
|
|
1190
|
|
1191 /* On some systems, apparently (?!) Emacs must be in its own process
|
|
1192 group in order to receive SIGIO correctly. On other systems
|
|
1193 (e.g. Solaris), it's not required and doing it makes things
|
|
1194 get fucked up. So, we only do it when
|
|
1195 SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP is defined. Basically,
|
|
1196 this is only required for BSD 4.2 systems. (Actually, I bet
|
|
1197 we don't have to do this at all -- those systems also
|
|
1198 required interrupt input, which we don't support.)
|
|
1199
|
|
1200 If Emacs was in its own process group (i.e. inherited_pgroup ==
|
|
1201 getpid ()), then we know we're running under a shell with job
|
|
1202 control (Emacs would never be run as part of a pipeline).
|
|
1203 Everything is fine.
|
|
1204
|
|
1205 If Emacs was not in its own process group, then we know we're
|
|
1206 running under a shell (or a caller) that doesn't know how to
|
|
1207 separate itself from Emacs (like sh). Emacs must be in its own
|
|
1208 process group in order to receive SIGIO correctly. In this
|
|
1209 situation, we put ourselves in our own pgroup, forcibly set the
|
|
1210 tty's pgroup to our pgroup, and make sure to restore and reinstate
|
|
1211 the tty's pgroup just like any other terminal setting. If
|
|
1212 inherited_group was not the tty's pgroup, then we'll get a
|
|
1213 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
|
|
1214 it goes foreground in the future, which is what should happen. */
|
|
1215
|
|
1216 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP
|
|
1217
|
442
|
1218 static pid_t inherited_pgroup;
|
|
1219 static pid_t inherited_tty_pgroup;
|
428
|
1220
|
|
1221 #endif
|
|
1222
|
|
1223 void
|
|
1224 munge_tty_process_group (void)
|
|
1225 {
|
|
1226 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP
|
|
1227 if (noninteractive)
|
|
1228 return;
|
|
1229
|
|
1230 /* Only do this munging if we have a device on the controlling
|
|
1231 terminal. See the large comment below. */
|
|
1232
|
|
1233 if (CONSOLEP (Vcontrolling_terminal) &&
|
|
1234 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal)))
|
|
1235 {
|
|
1236 int fd = open ("/dev/tty", O_RDWR, 0);
|
442
|
1237 pid_t me = getpid ();
|
428
|
1238 EMACS_BLOCK_SIGNAL (SIGTTOU);
|
|
1239 EMACS_SET_TTY_PROCESS_GROUP (fd, &me);
|
|
1240 EMACS_UNBLOCK_SIGNAL (SIGTTOU);
|
771
|
1241 retry_close (fd);
|
428
|
1242 }
|
|
1243 #endif
|
|
1244 }
|
|
1245
|
|
1246 /* Split off the foreground process group to Emacs alone.
|
|
1247 When we are in the foreground, but not started in our own process
|
|
1248 group, redirect the TTY to point to our own process group. We need
|
|
1249 to be in our own process group to receive SIGIO properly. */
|
|
1250 static void
|
|
1251 munge_process_groups (void)
|
|
1252 {
|
|
1253 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP
|
|
1254 if (noninteractive)
|
|
1255 return;
|
|
1256
|
|
1257 EMACS_SEPARATE_PROCESS_GROUP ();
|
|
1258
|
|
1259 munge_tty_process_group ();
|
|
1260 #endif
|
|
1261 }
|
|
1262
|
|
1263 void
|
|
1264 unmunge_tty_process_group (void)
|
|
1265 {
|
|
1266 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP
|
|
1267 {
|
|
1268 int fd = open ("/dev/tty", O_RDWR, 0);
|
|
1269 EMACS_BLOCK_SIGNAL (SIGTTOU);
|
|
1270 EMACS_SET_TTY_PROCESS_GROUP (fd, &inherited_tty_pgroup);
|
|
1271 EMACS_UNBLOCK_SIGNAL (SIGTTOU);
|
771
|
1272 retry_close (fd);
|
428
|
1273 }
|
|
1274 #endif
|
|
1275 }
|
|
1276
|
|
1277 /* Set the tty to our original foreground group.
|
|
1278 Also restore the original process group (put us back into sh's
|
|
1279 process group), so that ^Z will suspend both us and sh. */
|
|
1280 static void
|
|
1281 unmunge_process_groups (void)
|
|
1282 {
|
|
1283 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP
|
|
1284 if (noninteractive)
|
|
1285 return;
|
|
1286
|
|
1287 unmunge_tty_process_group ();
|
|
1288
|
|
1289 EMACS_SET_PROCESS_GROUP (inherited_pgroup);
|
|
1290 #endif
|
|
1291 }
|
|
1292
|
|
1293 /* According to some old wisdom, we need to be in a separate process
|
|
1294 group for SIGIO to work correctly (at least on some systems ...).
|
|
1295 So go ahead and put ourselves into our own process group. This
|
|
1296 will fail if we're already in our own process group, but who cares.
|
|
1297 Also record whether we were in our own process group. (In general,
|
|
1298 we will already be in our own process group if we were started from
|
|
1299 a job-control shell like csh, but not if we were started from sh).
|
|
1300
|
|
1301 If we succeeded in changing our process group, then we will no
|
|
1302 longer be in the foreground process group of our controlling
|
|
1303 terminal. Therefore, if we have a console open onto this terminal,
|
|
1304 we have to change the controlling terminal's foreground process
|
|
1305 group (otherwise we will get stopped with a SIGTTIN signal when
|
|
1306 attempting to read from the terminal). It's important,
|
|
1307 however, that we do this *only* when we have a console open onto
|
|
1308 the terminal. It's a decidedly bad idea to do so otherwise,
|
|
1309 especially if XEmacs was started from the background. */
|
|
1310
|
|
1311 void
|
|
1312 init_process_group (void)
|
|
1313 {
|
|
1314 #ifdef SIGIO_REQUIRES_SEPARATE_PROCESS_GROUP
|
|
1315 if (! noninteractive)
|
|
1316 {
|
|
1317 int fd = open ("/dev/tty", O_RDWR, 0);
|
|
1318 inherited_pgroup = EMACS_GET_PROCESS_GROUP ();
|
|
1319 EMACS_GET_TTY_PROCESS_GROUP (fd, &inherited_tty_pgroup);
|
771
|
1320 retry_close (fd);
|
428
|
1321 EMACS_SEPARATE_PROCESS_GROUP ();
|
|
1322 }
|
|
1323 #endif
|
|
1324 }
|
|
1325
|
|
1326 void
|
|
1327 disconnect_controlling_terminal (void)
|
|
1328 {
|
|
1329 # ifdef HAVE_SETSID
|
|
1330 /* Controlling terminals are attached to a session.
|
|
1331 Create a new session for us; it will have no controlling
|
|
1332 terminal. This also, of course, puts us in our own
|
|
1333 process group. */
|
|
1334 setsid ();
|
|
1335 # else
|
|
1336 /* Put us in our own process group. */
|
|
1337 EMACS_SEPARATE_PROCESS_GROUP ();
|
|
1338 # if defined (TIOCNOTTY)
|
|
1339 /* This is the older way of disconnecting the controlling
|
|
1340 terminal, on 4.3 BSD. We must open /dev/tty; using
|
|
1341 filedesc 0 is not sufficient because it could be
|
|
1342 something else (e.g. our stdin was redirected to
|
|
1343 another terminal).
|
|
1344 */
|
|
1345 {
|
|
1346 int j = open ("/dev/tty", O_RDWR, 0);
|
|
1347 ioctl (j, TIOCNOTTY, 0);
|
771
|
1348 retry_close (j);
|
428
|
1349 }
|
|
1350 # endif /* TIOCNOTTY */
|
|
1351 /*
|
|
1352 On systems without TIOCNOTTY and without
|
|
1353 setsid(), we don't need to do anything more to
|
|
1354 disconnect our controlling terminal. Here is
|
|
1355 what the man page for termio(7) from a SYSV 3.2
|
|
1356 system says:
|
|
1357
|
|
1358 "The first terminal file opened by the process group leader
|
|
1359 of a terminal file not already associated with a process
|
|
1360 group becomes the control terminal for that process group.
|
|
1361 The control terminal plays a special role in handling quit
|
|
1362 and interrupt signals, as discussed below. The control
|
|
1363 terminal is inherited by a child process during a fork(2).
|
|
1364 A process can break this association by changing its process
|
|
1365 group using setpgrp(2)."
|
|
1366
|
|
1367 */
|
|
1368 # endif /* not HAVE_SETSID */
|
|
1369 }
|
|
1370
|
|
1371
|
|
1372 /* ------------------------------------------------------ */
|
|
1373 /* Getting and setting emacs_tty structures */
|
|
1374 /* ------------------------------------------------------ */
|
|
1375
|
|
1376 /* It's wrong to encase these into #ifdef HAVE_TTY because we need
|
|
1377 them for child TTY processes. */
|
|
1378 /* However, this does break NT support while we don't do child TTY processes */
|
442
|
1379 #ifndef WIN32_NATIVE
|
428
|
1380
|
|
1381 /* Set *TC to the parameters associated with the terminal FD.
|
|
1382 Return zero if all's well, or -1 if we ran into an error we
|
|
1383 couldn't deal with. */
|
|
1384 int
|
|
1385 emacs_get_tty (int fd, struct emacs_tty *settings)
|
|
1386 {
|
|
1387 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
|
|
1388 #ifdef HAVE_TCATTR
|
|
1389 /* We have those nifty POSIX tcmumbleattr functions. */
|
|
1390 if (tcgetattr (fd, &settings->main) < 0)
|
|
1391 return -1;
|
|
1392
|
|
1393 #elif defined HAVE_TERMIO
|
|
1394 /* The SYSV-style interface? */
|
|
1395 if (ioctl (fd, TCGETA, &settings->main) < 0)
|
|
1396 return -1;
|
|
1397
|
442
|
1398 #elif !defined (WIN32_NATIVE)
|
428
|
1399 /* I give up - I hope you have the BSD ioctls. */
|
|
1400 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
|
|
1401 return -1;
|
|
1402 #endif /* HAVE_TCATTR */
|
|
1403
|
|
1404 /* Suivant - Do we have to get struct ltchars data? */
|
|
1405 #ifdef HAVE_LTCHARS
|
|
1406 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
|
|
1407 return -1;
|
|
1408 #endif
|
|
1409
|
|
1410 /* How about a struct tchars and a wordful of lmode bits? */
|
|
1411 #ifdef HAVE_TCHARS
|
|
1412 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
|
|
1413 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
|
|
1414 return -1;
|
|
1415 #endif
|
|
1416
|
|
1417 /* We have survived the tempest. */
|
|
1418 return 0;
|
|
1419 }
|
|
1420
|
|
1421 /* Set the parameters of the tty on FD according to the contents of
|
|
1422 *SETTINGS. If FLUSHP is non-zero, we discard input.
|
430
|
1423 Return 0 if all went well, and -1 if anything failed.
|
|
1424 #### All current callers use FLUSHP == 0. */
|
428
|
1425
|
|
1426 int
|
|
1427 emacs_set_tty (int fd, struct emacs_tty *settings, int flushp)
|
|
1428 {
|
|
1429 /* Set the primary parameters - baud rate, character size, etcetera. */
|
|
1430 #ifdef HAVE_TCATTR
|
|
1431 int i;
|
|
1432 /* We have those nifty POSIX tcmumbleattr functions.
|
|
1433 William J. Smith <wjs@wiis.wang.com> writes:
|
|
1434 "POSIX 1003.1 defines tcsetattr() to return success if it was
|
|
1435 able to perform any of the requested actions, even if some
|
|
1436 of the requested actions could not be performed.
|
|
1437 We must read settings back to ensure tty setup properly.
|
|
1438 AIX requires this to keep tty from hanging occasionally." */
|
|
1439 /* This makes sure that we don't loop indefinitely in here. */
|
|
1440 for (i = 0 ; i < 10 ; i++)
|
|
1441 if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
|
|
1442 {
|
|
1443 if (errno == EINTR)
|
|
1444 continue;
|
|
1445 else
|
|
1446 return -1;
|
|
1447 }
|
|
1448 else
|
|
1449 {
|
|
1450 struct termios new;
|
|
1451
|
|
1452 /* Get the current settings, and see if they're what we asked for. */
|
|
1453 tcgetattr (fd, &new);
|
|
1454 /* We cannot use memcmp on the whole structure here because under
|
|
1455 * aix386 the termios structure has some reserved field that may
|
|
1456 * not be filled in.
|
|
1457 */
|
|
1458 if ( new.c_iflag == settings->main.c_iflag
|
|
1459 && new.c_oflag == settings->main.c_oflag
|
|
1460 && new.c_cflag == settings->main.c_cflag
|
|
1461 && new.c_lflag == settings->main.c_lflag
|
|
1462 && memcmp(new.c_cc, settings->main.c_cc, NCCS) == 0)
|
|
1463 break;
|
|
1464 else
|
|
1465 continue;
|
|
1466 }
|
|
1467 #elif defined HAVE_TERMIO
|
|
1468 /* The SYSV-style interface? */
|
|
1469 if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0)
|
|
1470 return -1;
|
|
1471
|
442
|
1472 #elif !defined (WIN32_NATIVE)
|
428
|
1473 /* I give up - I hope you have the BSD ioctls. */
|
|
1474 if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
|
|
1475 return -1;
|
|
1476 #endif /* HAVE_TCATTR */
|
|
1477
|
|
1478 /* Suivant - Do we have to get struct ltchars data? */
|
|
1479 #ifdef HAVE_LTCHARS
|
|
1480 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
|
|
1481 return -1;
|
|
1482 #endif
|
|
1483
|
|
1484 /* How about a struct tchars and a wordful of lmode bits? */
|
|
1485 #ifdef HAVE_TCHARS
|
|
1486 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
|
|
1487 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
|
|
1488 return -1;
|
|
1489 #endif
|
|
1490
|
|
1491 /* We have survived the tempest. */
|
|
1492 return 0;
|
|
1493 }
|
|
1494
|
442
|
1495 #endif /* WIN32_NATIVE */
|
428
|
1496
|
|
1497 /* ------------------------------------------------------ */
|
|
1498 /* Initializing a device */
|
|
1499 /* ------------------------------------------------------ */
|
|
1500
|
|
1501 #ifdef HAVE_TTY
|
|
1502
|
|
1503 /* This may also be defined in stdio,
|
|
1504 but if so, this does no harm,
|
|
1505 and using the same name avoids wasting the other one's space. */
|
|
1506
|
|
1507 #if ((defined(USG) || defined(DGUX)) && !defined(__STDC__))
|
|
1508 char _sobuf[BUFSIZ+8];
|
|
1509 #elif (defined(USG) && !defined(LINUX) && !defined(_SCO_DS)) || defined(IRIX5)
|
|
1510 extern unsigned char _sobuf[BUFSIZ+8];
|
|
1511 #else
|
|
1512 char _sobuf[BUFSIZ];
|
|
1513 #endif
|
|
1514
|
|
1515 #if defined (TIOCGLTC) && defined (HAVE_LTCHARS) /* HAVE_LTCHARS */
|
|
1516 static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
|
|
1517 #endif
|
|
1518 #ifdef TIOCGETC /* HAVE_TCHARS */
|
|
1519 #ifdef HAVE_TCHARS
|
|
1520 static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
|
|
1521 #endif
|
|
1522 #endif
|
|
1523
|
|
1524 static void
|
|
1525 tty_init_sys_modes_on_device (struct device *d)
|
|
1526 {
|
|
1527 struct emacs_tty tty;
|
|
1528 int input_fd, output_fd;
|
|
1529 struct console *con = XCONSOLE (DEVICE_CONSOLE (d));
|
|
1530
|
|
1531 input_fd = CONSOLE_TTY_DATA (con)->infd;
|
|
1532 output_fd = CONSOLE_TTY_DATA (con)->outfd;
|
|
1533
|
430
|
1534 emacs_get_tty (input_fd, &CONSOLE_TTY_DATA (con)->old_tty);
|
428
|
1535 tty = CONSOLE_TTY_DATA (con)->old_tty;
|
|
1536
|
|
1537 con->tty_erase_char = Qnil;
|
|
1538
|
|
1539 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
|
|
1540 /* after all those years... */
|
|
1541 con->tty_erase_char = make_char (tty.main.c_cc[VERASE]);
|
|
1542 #ifdef DGUX
|
|
1543 /* This allows meta to be sent on 8th bit. */
|
|
1544 tty.main.c_iflag &= ~INPCK; /* don't check input for parity */
|
|
1545 #endif
|
|
1546 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
|
|
1547 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
|
|
1548 #ifdef ISTRIP
|
|
1549 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
|
|
1550 #endif
|
|
1551 tty.main.c_lflag &= ~ECHO; /* Disable echo */
|
|
1552 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
|
|
1553 #ifdef IEXTEN
|
|
1554 tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters. */
|
|
1555 #endif
|
|
1556 tty.main.c_lflag |= ISIG; /* Enable signals */
|
|
1557 if (TTY_FLAGS (con).flow_control)
|
|
1558 {
|
|
1559 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
|
|
1560 #ifdef IXANY
|
|
1561 tty.main.c_iflag &= ~IXANY;
|
|
1562 #endif /* IXANY */
|
|
1563 }
|
|
1564 else
|
|
1565 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
|
|
1566 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
|
|
1567 on output */
|
513
|
1568
|
|
1569 #if 0
|
|
1570 /* We used to disable tab expansion here, but this is the user's decision. */
|
|
1571 #if defined (TABDLY) && defined (TAB3)
|
|
1572 if ((tty.main.c_oflag & TABDLY) == TAB3)
|
|
1573 tty.main.c_oflag &= ~TABDLY; /* Disable tab expansion (Posix). */
|
|
1574 #elif defined (OXTABS)
|
|
1575 tty.main.c_oflag &= ~OXTABS; /* Disable tab expansion (BSD). */
|
|
1576 #endif
|
|
1577 #endif /* 0 */
|
|
1578
|
428
|
1579 #ifdef CS8
|
|
1580 if (TTY_FLAGS (con).meta_key)
|
|
1581 {
|
|
1582 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
|
|
1583 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
|
|
1584 }
|
|
1585 #endif
|
|
1586 if (CONSOLE_TTY_DATA (con)->controlling_terminal)
|
|
1587 {
|
|
1588 tty.main.c_cc[VINTR] =
|
|
1589 CONSOLE_QUIT_CHAR (con); /* C-g (usually) gives SIGINT */
|
|
1590 /* Set up C-g for both SIGQUIT and SIGINT.
|
|
1591 We don't know which we will get, but we handle both alike
|
|
1592 so which one it really gives us does not matter. */
|
|
1593 tty.main.c_cc[VQUIT] = CONSOLE_QUIT_CHAR (con);
|
|
1594 }
|
|
1595 else
|
|
1596 {
|
|
1597 tty.main.c_cc[VINTR] = _POSIX_VDISABLE;
|
|
1598 tty.main.c_cc[VQUIT] = _POSIX_VDISABLE;
|
|
1599 }
|
|
1600 tty.main.c_cc[VMIN] = 1; /* Input should wait for at
|
|
1601 least 1 char */
|
|
1602 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
|
|
1603 #ifdef VSWTCH
|
|
1604 tty.main.c_cc[VSWTCH] = _POSIX_VDISABLE; /* Turn off shell layering use
|
|
1605 of C-z */
|
|
1606 #endif /* VSWTCH */
|
|
1607 /* There was some conditionalizing here on (mips or TCATTR), but
|
|
1608 I think that's wrong. There was one report of C-y (DSUSP) not being
|
|
1609 disabled on HP9000s700 systems, and this might fix it. */
|
|
1610 #ifdef VSUSP
|
|
1611 tty.main.c_cc[VSUSP] = _POSIX_VDISABLE; /* Turn off mips handling of C-z. */
|
|
1612 #endif /* VSUSP */
|
|
1613 #ifdef V_DSUSP
|
|
1614 tty.main.c_cc[V_DSUSP] = _POSIX_VDISABLE; /* Turn off mips handling of C-y. */
|
|
1615 #endif /* V_DSUSP */
|
|
1616 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
|
|
1617 tty.main.c_cc[VDSUSP] = _POSIX_VDISABLE;
|
|
1618 #endif /* VDSUSP */
|
|
1619 #ifdef VLNEXT
|
|
1620 tty.main.c_cc[VLNEXT] = _POSIX_VDISABLE;
|
|
1621 #endif /* VLNEXT */
|
|
1622 #ifdef VREPRINT
|
|
1623 tty.main.c_cc[VREPRINT] = _POSIX_VDISABLE;
|
|
1624 #endif /* VREPRINT */
|
|
1625 #ifdef VWERASE
|
|
1626 tty.main.c_cc[VWERASE] = _POSIX_VDISABLE;
|
|
1627 #endif /* VWERASE */
|
|
1628 #ifdef VDISCARD
|
|
1629 tty.main.c_cc[VDISCARD] = _POSIX_VDISABLE;
|
|
1630 #endif /* VDISCARD */
|
|
1631 #ifdef VSTART
|
|
1632 tty.main.c_cc[VSTART] = _POSIX_VDISABLE;
|
|
1633 #endif /* VSTART */
|
|
1634 #ifdef VSTRT
|
|
1635 tty.main.c_cc[VSTRT] = _POSIX_VDISABLE; /* called VSTRT on some systems */
|
|
1636 #endif /* VSTART */
|
|
1637 #ifdef VSTOP
|
|
1638 tty.main.c_cc[VSTOP] = _POSIX_VDISABLE;
|
|
1639 #endif /* VSTOP */
|
|
1640 #ifdef SET_LINE_DISCIPLINE
|
|
1641 /* Need to explicitly request TERMIODISC line discipline or
|
|
1642 Ultrix's termios does not work correctly. */
|
|
1643 tty.main.c_line = SET_LINE_DISCIPLINE;
|
|
1644 #endif
|
|
1645
|
|
1646 #ifdef AIX
|
|
1647 #ifndef IBMR2AIX
|
|
1648 /* AIX enhanced edit loses NULs, so disable it. */
|
|
1649 tty.main.c_line = 0;
|
|
1650 tty.main.c_iflag &= ~ASCEDIT;
|
|
1651 #else
|
|
1652 tty.main.c_cc[VSTRT] = 255;
|
|
1653 tty.main.c_cc[VSTOP] = 255;
|
|
1654 tty.main.c_cc[VSUSP] = 255;
|
|
1655 tty.main.c_cc[VDSUSP] = 255;
|
|
1656 #endif /* IBMR2AIX */
|
|
1657 /* Also, PTY overloads NUL and BREAK.
|
|
1658 don't ignore break, but don't signal either, so it looks like NUL.
|
|
1659 This really serves a purpose only if running in an XTERM window
|
|
1660 or via TELNET or the like, but does no harm elsewhere. */
|
|
1661 tty.main.c_iflag &= ~IGNBRK;
|
|
1662 tty.main.c_iflag &= ~BRKINT;
|
|
1663 #endif /* AIX */
|
|
1664 #else /* if not HAVE_TERMIO */
|
442
|
1665 #if !defined (WIN32_NATIVE)
|
428
|
1666 con->tty_erase_char = make_char (tty.main.sg_erase);
|
|
1667 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
|
|
1668 if (TTY_FLAGS (con).meta_key)
|
|
1669 tty.main.sg_flags |= ANYP;
|
|
1670 /* #### should we be using RAW mode here? */
|
|
1671 tty.main.sg_flags |= /* interrupt_input ? RAW : */ CBREAK;
|
442
|
1672 #endif /* not WIN32_NATIVE */
|
428
|
1673 #endif /* not HAVE_TERMIO */
|
|
1674
|
|
1675 /* If going to use CBREAK mode, we must request C-g to interrupt
|
|
1676 and turn off start and stop chars, etc. If not going to use
|
|
1677 CBREAK mode, do this anyway so as to turn off local flow
|
|
1678 control for user coming over network on 4.2; in this case,
|
|
1679 only t_stopc and t_startc really matter. */
|
|
1680 #ifndef HAVE_TERMIO
|
|
1681 #ifdef HAVE_TCHARS
|
|
1682 /* Note: if not using CBREAK mode, it makes no difference how we
|
|
1683 set this */
|
|
1684 tty.tchars = new_tchars;
|
|
1685 tty.tchars.t_intrc = CONSOLE_QUIT_CHAR (con);
|
|
1686 if (TTY_FLAGS (con).flow_control)
|
|
1687 {
|
|
1688 tty.tchars.t_startc = '\021';
|
|
1689 tty.tchars.t_stopc = '\023';
|
|
1690 }
|
|
1691
|
|
1692 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH |
|
|
1693 CONSOLE_TTY_DATA (con)->old_tty.lmode;
|
|
1694
|
|
1695 #if defined (ultrix) || defined (__bsdi__)
|
|
1696 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
|
|
1697 anything, and leaving it in breaks the meta key. Go figure. */
|
|
1698 /* Turning off ONLCR is enough under BSD/386. Leave the general
|
|
1699 output post-processing flag alone since for some reason it
|
|
1700 doesn't get reset after XEmacs goes away. */
|
|
1701 tty.lmode &= ~LLITOUT;
|
|
1702 #endif
|
|
1703
|
|
1704 #endif /* HAVE_TCHARS */
|
|
1705 #endif /* not HAVE_TERMIO */
|
|
1706
|
|
1707 #ifdef HAVE_LTCHARS
|
|
1708 tty.ltchars = new_ltchars;
|
|
1709 #endif /* HAVE_LTCHARS */
|
|
1710
|
430
|
1711 emacs_set_tty (input_fd, &tty, 0);
|
428
|
1712
|
|
1713 /* This code added to insure that, if flow-control is not to be used,
|
|
1714 we have an unlocked terminal at the start. */
|
|
1715
|
|
1716 #ifdef TCXONC
|
|
1717 if (!TTY_FLAGS (con).flow_control) ioctl (input_fd, TCXONC, 1);
|
|
1718 #endif
|
|
1719 #ifdef TIOCSTART
|
|
1720 if (!TTY_FLAGS (con).flow_control) ioctl (input_fd, TIOCSTART, 0);
|
|
1721 #endif
|
|
1722
|
|
1723 #if defined (HAVE_TERMIOS) || defined (HPUX9)
|
|
1724 #ifdef TCOON
|
|
1725 if (!TTY_FLAGS (con).flow_control) tcflow (input_fd, TCOON);
|
|
1726 #endif
|
|
1727 #endif
|
|
1728 #ifdef AIXHFT
|
|
1729 hft_init (con);
|
|
1730 #ifdef IBMR2AIX
|
|
1731 {
|
|
1732 /* IBM's HFT device usually thinks a ^J should be LF/CR.
|
|
1733 We need it to be only LF. This is the way that is
|
|
1734 done. */
|
|
1735 struct termio tty;
|
|
1736
|
|
1737 if (ioctl (output_fd, HFTGETID, &tty) != -1)
|
771
|
1738 retry_write (output_fd, "\033[20l", 5);
|
428
|
1739 }
|
|
1740 #endif
|
|
1741 #endif
|
|
1742
|
|
1743 #if 0 /* We do our own buffering with lstreams. */
|
|
1744 #ifdef _IOFBF
|
|
1745 /* This symbol is defined on recent USG systems.
|
|
1746 Someone says without this call USG won't really buffer the file
|
|
1747 even with a call to setbuf. */
|
647
|
1748 setvbuf (CONSOLE_TTY_DATA (con)->outfd, (char *) _sobuf, _IOFBF,
|
|
1749 sizeof (_sobuf));
|
428
|
1750 #else
|
|
1751 setbuf (CONSOLE_TTY_DATA (con)->outfd, (char *) _sobuf);
|
|
1752 #endif
|
|
1753 #endif
|
|
1754 set_tty_modes (con);
|
|
1755 }
|
|
1756
|
|
1757 #endif /* HAVE_TTY */
|
|
1758
|
|
1759 void
|
|
1760 init_one_device (struct device *d)
|
|
1761 {
|
|
1762 #ifdef HAVE_TTY
|
|
1763 if (DEVICE_TTY_P (d))
|
|
1764 tty_init_sys_modes_on_device (d);
|
|
1765 #endif
|
|
1766 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
|
|
1767 if (!DEVICE_STREAM_P (d))
|
|
1768 {
|
|
1769 init_sigio_on_device (d);
|
|
1770 request_sigio_on_device (d);
|
|
1771 }
|
|
1772 #endif
|
|
1773 }
|
|
1774
|
|
1775 void
|
|
1776 init_one_console (struct console *con)
|
|
1777 {
|
|
1778 Lisp_Object devcons;
|
|
1779
|
|
1780 CONSOLE_DEVICE_LOOP (devcons, con)
|
|
1781 {
|
|
1782 struct device *d = XDEVICE (XCAR (devcons));
|
|
1783
|
|
1784 init_one_device (d);
|
|
1785 }
|
|
1786 }
|
|
1787
|
|
1788 void
|
|
1789 reinit_initial_console (void)
|
|
1790 {
|
|
1791 munge_process_groups ();
|
|
1792 if (CONSOLEP (Vcontrolling_terminal) &&
|
|
1793 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal)))
|
|
1794 init_one_console (XCONSOLE (Vcontrolling_terminal));
|
|
1795 }
|
|
1796
|
|
1797
|
|
1798 /* ------------------------------------------------------ */
|
|
1799 /* Other TTY functions */
|
|
1800 /* ------------------------------------------------------ */
|
|
1801
|
|
1802 #ifdef HAVE_TTY
|
|
1803
|
|
1804 #if 0 /* not currently used */
|
|
1805
|
|
1806 /* Return nonzero if safe to use tabs in output.
|
|
1807 At the time this is called, init_sys_modes has not been done yet. */
|
|
1808
|
|
1809 int
|
|
1810 tabs_safe_p (struct device *d)
|
|
1811 {
|
|
1812 #ifdef HAVE_TTY
|
|
1813 if (DEVICE_TTY_P (d))
|
|
1814 {
|
|
1815 struct emacs_tty tty;
|
|
1816
|
430
|
1817 emacs_get_tty (DEVICE_INFD (d), &tty);
|
428
|
1818 return EMACS_TTY_TABS_OK (&tty);
|
|
1819 }
|
|
1820 #endif
|
|
1821 return 1;
|
|
1822 }
|
|
1823
|
|
1824 #endif /* 0 */
|
|
1825
|
|
1826 /* Get terminal size from system.
|
|
1827 Store number of lines into *heightp and width into *widthp.
|
|
1828 If zero or a negative number is stored, the value is not valid. */
|
|
1829
|
|
1830 void
|
|
1831 get_tty_device_size (struct device *d, int *widthp, int *heightp)
|
|
1832 {
|
|
1833 int input_fd = DEVICE_INFD (d);
|
|
1834
|
|
1835 assert (DEVICE_TTY_P (d));
|
|
1836
|
|
1837 #ifdef TIOCGWINSZ
|
|
1838 {
|
|
1839 /* BSD-style. */
|
|
1840 struct winsize size;
|
|
1841
|
|
1842 if (ioctl (input_fd, TIOCGWINSZ, &size) == -1)
|
|
1843 *widthp = *heightp = 0;
|
|
1844 else
|
|
1845 {
|
|
1846 *widthp = size.ws_col;
|
|
1847 *heightp = size.ws_row;
|
|
1848 }
|
|
1849 }
|
|
1850 #elif defined TIOCGSIZE
|
|
1851 {
|
|
1852 /* SunOS - style. */
|
|
1853 struct ttysize size;
|
|
1854
|
|
1855 if (ioctl (input_fd, TIOCGSIZE, &size) == -1)
|
|
1856 *widthp = *heightp = 0;
|
|
1857 else
|
|
1858 {
|
|
1859 *widthp = size.ts_cols;
|
|
1860 *heightp = size.ts_lines;
|
|
1861 }
|
|
1862 }
|
|
1863 #else /* system doesn't know size */
|
|
1864
|
|
1865 *widthp = 0;
|
|
1866 *heightp = 0;
|
|
1867
|
|
1868 #endif /* not !TIOCGWINSZ */
|
|
1869 }
|
|
1870
|
|
1871 #endif /* HAVE_TTY */
|
|
1872
|
|
1873
|
|
1874 /* ------------------------------------------------------ */
|
|
1875 /* Is device 8 bit ? */
|
|
1876 /* ------------------------------------------------------ */
|
|
1877
|
|
1878 #ifdef HAVE_TTY
|
|
1879
|
|
1880 int
|
|
1881 eight_bit_tty (struct device *d)
|
|
1882 {
|
|
1883 struct emacs_tty s;
|
|
1884 int input_fd;
|
|
1885 int eight_bit = 0;
|
|
1886
|
|
1887 assert (DEVICE_TTY_P (d));
|
|
1888 input_fd = DEVICE_INFD (d);
|
|
1889
|
430
|
1890 emacs_get_tty (input_fd, &s);
|
428
|
1891
|
|
1892 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
|
|
1893 eight_bit = (s.main.c_cflag & CSIZE) == CS8;
|
|
1894 #else
|
|
1895 eight_bit = 0; /* I don't know how to do it */
|
|
1896 #endif
|
|
1897 return eight_bit;
|
|
1898 }
|
|
1899
|
|
1900 #endif /* HAVE_TTY */
|
|
1901
|
|
1902
|
|
1903 /* ------------------------------------------------------ */
|
|
1904 /* Resetting a device */
|
|
1905 /* ------------------------------------------------------ */
|
|
1906
|
|
1907 #ifdef HAVE_TTY
|
|
1908
|
|
1909 /* Prepare the terminal for exiting Emacs; move the cursor to the
|
|
1910 bottom of the frame, turn off interrupt-driven I/O, etc. */
|
|
1911 static void
|
|
1912 tty_reset_sys_modes_on_device (struct device *d)
|
|
1913 {
|
|
1914 int input_fd, output_fd;
|
|
1915 struct console *con = XCONSOLE (DEVICE_CONSOLE (d));
|
|
1916
|
|
1917 input_fd = CONSOLE_TTY_DATA (con)->infd;
|
|
1918 output_fd = CONSOLE_TTY_DATA (con)->outfd;
|
|
1919
|
|
1920 #if defined (IBMR2AIX) && defined (AIXHFT)
|
|
1921 {
|
|
1922 /* HFT consoles normally use ^J as a LF/CR. We forced it to
|
|
1923 do the LF only. Now, we need to reset it. */
|
|
1924 struct termio tty;
|
|
1925
|
|
1926 if (ioctl (output_fd, HFTGETID, &tty) != -1)
|
771
|
1927 retry_write (output_fd, "\033[20h", 5);
|
428
|
1928 }
|
|
1929 #endif
|
|
1930
|
|
1931 tty_redisplay_shutdown (con);
|
|
1932 /* reset_tty_modes() flushes the connection at its end. */
|
|
1933 reset_tty_modes (con);
|
|
1934
|
|
1935 #if defined (BSD)
|
|
1936 /* Avoid possible loss of output when changing terminal modes. */
|
|
1937 fsync (output_fd);
|
|
1938 #endif
|
|
1939
|
430
|
1940 while (emacs_set_tty (input_fd, &CONSOLE_TTY_DATA (con)->old_tty, 0)
|
428
|
1941 < 0 && errno == EINTR)
|
|
1942 ;
|
|
1943
|
|
1944 #ifdef SET_LINE_DISCIPLINE
|
|
1945 /* Ultrix's termios *ignores* any line discipline except TERMIODISC.
|
|
1946 A different old line discipline is therefore not restored, yet.
|
|
1947 Restore the old line discipline by hand. */
|
|
1948 ioctl (input_fd, TIOCSETD, &old_tty.main.c_line);
|
|
1949 #endif
|
|
1950
|
|
1951 #ifdef AIXHFT
|
|
1952 hft_reset (con);
|
|
1953 #endif
|
|
1954
|
|
1955 }
|
|
1956
|
|
1957 #endif /* HAVE_TTY */
|
|
1958
|
|
1959 void
|
|
1960 reset_one_device (struct device *d)
|
|
1961 {
|
|
1962 #ifdef HAVE_TTY
|
|
1963 if (DEVICE_TTY_P (d))
|
|
1964 tty_reset_sys_modes_on_device (d);
|
|
1965 else
|
|
1966 #endif
|
|
1967 if (DEVICE_STREAM_P (d))
|
|
1968 fflush (CONSOLE_STREAM_DATA (XCONSOLE (DEVICE_CONSOLE (d)))->out);
|
|
1969 #if defined(SIGIO) && !defined(BROKEN_SIGIO)
|
|
1970 if (!DEVICE_STREAM_P (d))
|
|
1971 {
|
|
1972 unrequest_sigio_on_device (d);
|
|
1973 reset_sigio_on_device (d);
|
|
1974 }
|
|
1975 #endif
|
|
1976 }
|
|
1977
|
|
1978 void
|
|
1979 reset_one_console (struct console *con)
|
|
1980 {
|
|
1981 /* Note: this can be called during GC. */
|
|
1982 Lisp_Object devcons;
|
|
1983
|
|
1984 CONSOLE_DEVICE_LOOP (devcons, con)
|
|
1985 {
|
|
1986 struct device *d = XDEVICE (XCAR (devcons));
|
|
1987
|
|
1988 reset_one_device (d);
|
|
1989 }
|
|
1990 }
|
|
1991
|
|
1992 void
|
|
1993 reset_all_consoles (void)
|
|
1994 {
|
|
1995 /* Note: this can be called during GC. */
|
|
1996 Lisp_Object concons;
|
|
1997
|
|
1998 CONSOLE_LOOP (concons)
|
|
1999 {
|
|
2000 struct console *con = XCONSOLE (XCAR (concons));
|
|
2001
|
|
2002 reset_one_console (con);
|
|
2003 }
|
|
2004
|
|
2005 unmunge_process_groups ();
|
|
2006 }
|
|
2007
|
|
2008 void
|
|
2009 reset_initial_console (void)
|
|
2010 {
|
|
2011 if (CONSOLEP (Vcontrolling_terminal) &&
|
|
2012 CONSOLE_LIVE_P (XCONSOLE (Vcontrolling_terminal)))
|
|
2013 reset_one_console (XCONSOLE (Vcontrolling_terminal));
|
|
2014 unmunge_process_groups ();
|
|
2015 }
|
|
2016
|
|
2017
|
|
2018 /* ------------------------------------------------------ */
|
|
2019 /* extra TTY stuff under AIX */
|
|
2020 /* ------------------------------------------------------ */
|
|
2021
|
|
2022 #ifdef AIXHFT
|
|
2023
|
|
2024 /* Called from init_sys_modes. */
|
|
2025 static void
|
|
2026 hft_init (struct console *con)
|
|
2027 {
|
|
2028 int junk;
|
|
2029 int input_fd;
|
|
2030
|
|
2031 assert (CONSOLE_TTY_P (con));
|
|
2032 input_fd = CONSOLE_TTY_DATA (con)->infd;
|
|
2033
|
|
2034 /* If we're not on an HFT we shouldn't do any of this. We determine
|
|
2035 if we are on an HFT by trying to get an HFT error code. If this
|
|
2036 call fails, we're not on an HFT. */
|
|
2037 #ifdef IBMR2AIX
|
|
2038 if (ioctl (input_fd, HFQERROR, &junk) < 0)
|
|
2039 return;
|
|
2040 #else /* not IBMR2AIX */
|
|
2041 if (ioctl (input_fd, HFQEIO, 0) < 0)
|
|
2042 return;
|
|
2043 #endif /* not IBMR2AIX */
|
|
2044
|
|
2045 /* On AIX the default hft keyboard mapping uses backspace rather than delete
|
|
2046 as the rubout key's ASCII code. Here this is changed. The bug is that
|
|
2047 there's no way to determine the old mapping, so in reset_one_console
|
|
2048 we need to assume that the normal map had been present. Of course, this
|
|
2049 code also doesn't help if on a terminal emulator which doesn't understand
|
|
2050 HFT VTD's. */
|
|
2051 {
|
|
2052 struct hfbuf buf;
|
|
2053 struct hfkeymap keymap;
|
|
2054
|
|
2055 buf.hf_bufp = (char *)&keymap;
|
|
2056 buf.hf_buflen = sizeof (keymap);
|
|
2057 keymap.hf_nkeys = 2;
|
|
2058 keymap.hfkey[0].hf_kpos = 15;
|
|
2059 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
|
|
2060 #ifdef IBMR2AIX
|
|
2061 keymap.hfkey[0].hf_keyidh = '<';
|
|
2062 #else /* not IBMR2AIX */
|
|
2063 keymap.hfkey[0].hf_page = '<';
|
|
2064 #endif /* not IBMR2AIX */
|
|
2065 keymap.hfkey[0].hf_char = 127;
|
|
2066 keymap.hfkey[1].hf_kpos = 15;
|
|
2067 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
|
|
2068 #ifdef IBMR2AIX
|
|
2069 keymap.hfkey[1].hf_keyidh = '<';
|
|
2070 #else /* not IBMR2AIX */
|
|
2071 keymap.hfkey[1].hf_page = '<';
|
|
2072 #endif /* not IBMR2AIX */
|
|
2073 keymap.hfkey[1].hf_char = 127;
|
|
2074 hftctl (input_fd, HFSKBD, &buf);
|
|
2075 }
|
|
2076 /* #### Should probably set a console TTY flag here. */
|
|
2077 #if 0
|
|
2078 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
|
|
2079 at times. */
|
|
2080 line_ins_del_ok = char_ins_del_ok = 0;
|
|
2081 #endif /* 0 */
|
|
2082 }
|
|
2083
|
|
2084 /* Reset the rubout key to backspace. */
|
|
2085
|
|
2086 static void
|
|
2087 hft_reset (struct console *con)
|
|
2088 {
|
|
2089 struct hfbuf buf;
|
|
2090 struct hfkeymap keymap;
|
|
2091 int junk;
|
|
2092 int input_fd;
|
|
2093
|
|
2094 assert (CONSOLE_TTY_P (con));
|
|
2095 input_fd = CONSOLE_TTY_DATA (con)->infd;
|
|
2096
|
|
2097 #ifdef IBMR2AIX
|
|
2098 if (ioctl (input_fd, HFQERROR, &junk) < 0)
|
|
2099 return;
|
|
2100 #else /* not IBMR2AIX */
|
|
2101 if (ioctl (input_fd, HFQEIO, 0) < 0)
|
|
2102 return;
|
|
2103 #endif /* not IBMR2AIX */
|
|
2104
|
|
2105 buf.hf_bufp = (char *)&keymap;
|
|
2106 buf.hf_buflen = sizeof (keymap);
|
|
2107 keymap.hf_nkeys = 2;
|
|
2108 keymap.hfkey[0].hf_kpos = 15;
|
|
2109 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
|
|
2110 #ifdef IBMR2AIX
|
|
2111 keymap.hfkey[0].hf_keyidh = '<';
|
|
2112 #else /* not IBMR2AIX */
|
|
2113 keymap.hfkey[0].hf_page = '<';
|
|
2114 #endif /* not IBMR2AIX */
|
|
2115 keymap.hfkey[0].hf_char = 8;
|
|
2116 keymap.hfkey[1].hf_kpos = 15;
|
|
2117 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
|
|
2118 #ifdef IBMR2AIX
|
|
2119 keymap.hfkey[1].hf_keyidh = '<';
|
|
2120 #else /* not IBMR2AIX */
|
|
2121 keymap.hfkey[1].hf_page = '<';
|
|
2122 #endif /* not IBMR2AIX */
|
|
2123 keymap.hfkey[1].hf_char = 8;
|
|
2124 hftctl (input_fd, HFSKBD, &buf);
|
|
2125 }
|
|
2126
|
|
2127 #endif /* AIXHFT */
|
|
2128
|
|
2129
|
|
2130 /************************************************************************/
|
|
2131 /* limits of text/data segments */
|
|
2132 /************************************************************************/
|
|
2133
|
440
|
2134 #if !defined(CANNOT_DUMP) && !defined(PDUMP)
|
428
|
2135 #define NEED_STARTS
|
|
2136 #endif
|
|
2137
|
|
2138 #ifndef SYSTEM_MALLOC
|
|
2139 #ifndef NEED_STARTS
|
|
2140 #define NEED_STARTS
|
|
2141 #endif
|
|
2142 #endif
|
|
2143
|
|
2144 #ifdef NEED_STARTS
|
|
2145 /* Some systems that cannot dump also cannot implement these. */
|
|
2146
|
|
2147 /*
|
|
2148 * Return the address of the start of the text segment prior to
|
|
2149 * doing an unexec. After unexec the return value is undefined.
|
|
2150 * See crt0.c for further explanation and _start.
|
|
2151 *
|
|
2152 */
|
|
2153
|
440
|
2154 #if !defined(HAVE_TEXT_START) && !defined(PDUMP)
|
|
2155
|
442
|
2156 EXTERN_C int _start (void);
|
428
|
2157
|
|
2158 char *
|
|
2159 start_of_text (void)
|
|
2160 {
|
|
2161 #ifdef TEXT_START
|
442
|
2162 return (char *) TEXT_START;
|
428
|
2163 #else
|
442
|
2164 return (char *) _start;
|
428
|
2165 #endif /* TEXT_START */
|
|
2166 }
|
440
|
2167 #endif /* !defined(HAVE_TEXT_START) && !defined(PDUMP) */
|
428
|
2168
|
|
2169 /*
|
|
2170 * Return the address of the start of the data segment prior to
|
|
2171 * doing an unexec. After unexec the return value is undefined.
|
442
|
2172 * See ecrt0.c for further information and definition of data_start.
|
428
|
2173 *
|
|
2174 * Apparently, on BSD systems this is etext at startup. On
|
|
2175 * USG systems (swapping) this is highly mmu dependent and
|
|
2176 * is also dependent on whether or not the program is running
|
|
2177 * with shared text. Generally there is a (possibly large)
|
|
2178 * gap between end of text and start of data with shared text.
|
|
2179 *
|
|
2180 * On Uniplus+ systems with shared text, data starts at a
|
|
2181 * fixed address. Each port (from a given oem) is generally
|
|
2182 * different, and the specific value of the start of data can
|
|
2183 * be obtained via the UniPlus+ specific "uvar" system call,
|
|
2184 * however the method outlined in crt0.c seems to be more portable.
|
|
2185 *
|
|
2186 * Probably what will have to happen when a USG unexec is available,
|
|
2187 * at least on UniPlus, is temacs will have to be made unshared so
|
|
2188 * that text and data are contiguous. Then once loadup is complete,
|
|
2189 * unexec will produce a shared executable where the data can be
|
|
2190 * at the normal shared text boundary and the startofdata variable
|
|
2191 * will be patched by unexec to the correct value.
|
|
2192 *
|
|
2193 */
|
|
2194
|
442
|
2195 #if defined(ORDINARY_LINK) && !defined(MINGW)
|
428
|
2196 extern char **environ;
|
|
2197 #endif
|
|
2198
|
|
2199 void *
|
|
2200 start_of_data (void)
|
|
2201 {
|
|
2202 #ifdef DATA_START
|
|
2203 return ((char *) DATA_START);
|
|
2204 #else
|
452
|
2205 #if defined (ORDINARY_LINK) || defined(PDUMP)
|
428
|
2206 /*
|
|
2207 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
|
|
2208 * data_start isn't defined. We take the address of environ, which
|
|
2209 * is known to live at or near the start of the system crt0.c, and
|
|
2210 * we don't sweat the handful of bytes that might lose.
|
|
2211 */
|
442
|
2212 #if defined (HEAP_IN_DATA) && !defined(PDUMP)
|
428
|
2213 extern char* static_heap_base;
|
|
2214 if (!initialized)
|
|
2215 return static_heap_base;
|
|
2216 #endif
|
|
2217 return((char *) &environ);
|
|
2218 #else
|
|
2219 extern int data_start;
|
|
2220 return ((char *) &data_start);
|
|
2221 #endif /* ORDINARY_LINK */
|
|
2222 #endif /* DATA_START */
|
|
2223 }
|
|
2224 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
|
|
2225
|
|
2226
|
|
2227 /************************************************************************/
|
|
2228 /* get the system name */
|
|
2229 /************************************************************************/
|
|
2230
|
|
2231 /* init_system_name sets up the string for the Lisp function
|
|
2232 system-name to return. */
|
|
2233
|
|
2234 extern Lisp_Object Vsystem_name;
|
|
2235
|
|
2236 void
|
|
2237 init_system_name (void)
|
|
2238 {
|
442
|
2239 #if defined (WIN32_NATIVE)
|
771
|
2240 Extbyte hostname[MAX_XETCHAR_SIZE * (MAX_COMPUTERNAME_LENGTH + 1)];
|
|
2241 DWORD size = sizeof (hostname) / XETCHAR_SIZE;
|
|
2242 qxeGetComputerName (hostname, &size);
|
|
2243 Vsystem_name = build_tstr_string (hostname);
|
428
|
2244 #elif !defined (HAVE_GETHOSTNAME)
|
|
2245 struct utsname uts;
|
|
2246 uname (&uts);
|
|
2247 Vsystem_name = build_string (uts.nodename);
|
|
2248 #else /* HAVE_GETHOSTNAME */
|
647
|
2249 int hostname_size = 256;
|
428
|
2250 char *hostname = (char *) alloca (hostname_size);
|
|
2251
|
|
2252 /* Try to get the host name; if the buffer is too short, try
|
|
2253 again. Apparently, the only indication gethostname gives of
|
|
2254 whether the buffer was large enough is the presence or absence
|
|
2255 of a '\0' in the string. Eech. */
|
|
2256 for (;;)
|
|
2257 {
|
|
2258 gethostname (hostname, hostname_size - 1);
|
|
2259 hostname[hostname_size - 1] = '\0';
|
|
2260
|
|
2261 /* Was the buffer large enough for the '\0'? */
|
647
|
2262 if ((int) strlen (hostname) < (hostname_size - 1))
|
428
|
2263 break;
|
|
2264
|
|
2265 hostname_size <<= 1;
|
|
2266 hostname = (char *) alloca (hostname_size);
|
|
2267 }
|
558
|
2268 # if defined( HAVE_SOCKETS)
|
428
|
2269 /* Turn the hostname into the official, fully-qualified hostname.
|
|
2270 Don't do this if we're going to dump; this can confuse system
|
|
2271 libraries on some machines and make the dumped emacs core dump. */
|
|
2272 # ifndef CANNOT_DUMP
|
|
2273 if (initialized)
|
|
2274 # endif /* not CANNOT_DUMP */
|
|
2275 if (!strchr (hostname, '.'))
|
|
2276 {
|
440
|
2277 # if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETNAMEINFO))
|
428
|
2278 struct hostent *hp = NULL;
|
|
2279 int count;
|
440
|
2280 # ifdef TRY_AGAIN
|
428
|
2281 for (count = 0; count < 10; count++)
|
|
2282 {
|
|
2283 h_errno = 0;
|
440
|
2284 # endif
|
428
|
2285 /* Some systems can't handle SIGALARM/SIGIO in gethostbyname(). */
|
|
2286 stop_interrupts ();
|
|
2287 hp = gethostbyname (hostname);
|
|
2288 start_interrupts ();
|
440
|
2289 # ifdef TRY_AGAIN
|
428
|
2290 if (! (hp == 0 && h_errno == TRY_AGAIN))
|
|
2291 break;
|
|
2292 Fsleep_for (make_int (1));
|
|
2293 }
|
440
|
2294 # endif
|
428
|
2295 if (hp)
|
|
2296 {
|
442
|
2297 const char *fqdn = (const char *) hp->h_name;
|
428
|
2298
|
|
2299 if (!strchr (fqdn, '.'))
|
|
2300 {
|
|
2301 /* We still don't have a fully qualified domain name.
|
|
2302 Try to find one in the list of alternate names */
|
|
2303 char **alias = hp->h_aliases;
|
|
2304 while (*alias && !strchr (*alias, '.'))
|
|
2305 alias++;
|
|
2306 if (*alias)
|
|
2307 fqdn = *alias;
|
|
2308 }
|
|
2309 hostname = (char *) alloca (strlen (fqdn) + 1);
|
|
2310 strcpy (hostname, fqdn);
|
|
2311 }
|
440
|
2312 # else /* !(HAVE_GETADDRINFO && HAVE_GETNAMEINFO) */
|
|
2313 struct addrinfo hints, *res;
|
|
2314
|
|
2315 xzero (hints);
|
|
2316 hints.ai_flags = AI_CANONNAME;
|
724
|
2317 #ifdef IPV6_CANONICALIZE
|
440
|
2318 hints.ai_family = AF_UNSPEC;
|
724
|
2319 #else
|
|
2320 hints.ai_family = PF_INET;
|
|
2321 #endif
|
440
|
2322 hints.ai_socktype = SOCK_STREAM;
|
|
2323 hints.ai_protocol = 0;
|
|
2324 if (!getaddrinfo (hostname, NULL, &hints, &res))
|
|
2325 {
|
|
2326 hostname = (char *) alloca (strlen (res->ai_canonname) + 1);
|
|
2327 strcpy (hostname, res->ai_canonname);
|
|
2328
|
|
2329 freeaddrinfo (res);
|
|
2330 }
|
|
2331 # endif /* !(HAVE_GETADDRINFO && HAVE_GETNAMEINFO) */
|
428
|
2332 }
|
|
2333 # endif /* HAVE_SOCKETS */
|
|
2334 Vsystem_name = build_string (hostname);
|
|
2335 #endif /* HAVE_GETHOSTNAME */
|
|
2336 {
|
665
|
2337 Intbyte *p;
|
428
|
2338 Bytecount i;
|
|
2339
|
|
2340 for (i = 0, p = XSTRING_DATA (Vsystem_name);
|
|
2341 i < XSTRING_LENGTH (Vsystem_name);
|
|
2342 i++, p++)
|
|
2343 {
|
|
2344 if (*p == ' ' || *p == '\t')
|
|
2345 *p = '-';
|
|
2346 }
|
|
2347 }
|
|
2348 }
|
|
2349
|
|
2350
|
|
2351 /************************************************************************/
|
|
2352 /* Emulation of select() */
|
|
2353 /************************************************************************/
|
|
2354
|
|
2355 #ifndef HAVE_SELECT
|
|
2356
|
|
2357 ERROR: XEmacs requires a working select().
|
|
2358
|
|
2359 #endif /* not HAVE_SELECT */
|
|
2360
|
|
2361
|
|
2362 /************************************************************************/
|
|
2363 /* Emulation of signal stuff */
|
|
2364 /************************************************************************/
|
|
2365
|
|
2366 /* BSD 4.1 crap deleted. 4.2 was released in 1983, for God's sake! I
|
|
2367 can't imagine that anyone is actually running that OS any more.
|
|
2368 You can't use X under it (I think) because there's no select().
|
|
2369 Anyway, the signal stuff has all been changed. If someone wants to
|
|
2370 get this stuff working again, look in the FSF Emacs sources. */
|
|
2371
|
|
2372 /* POSIX signals support - DJB */
|
|
2373
|
|
2374 #ifdef HAVE_SIGPROCMASK
|
|
2375
|
|
2376 /* #### Is there any reason this is static global rather than local? */
|
|
2377 static struct sigaction new_action, old_action;
|
|
2378
|
|
2379 signal_handler_t
|
613
|
2380 qxe_reliable_signal (int signal_number, signal_handler_t action)
|
428
|
2381 {
|
|
2382 #if 0
|
|
2383
|
|
2384 /* XEmacs works better if system calls are *not* restarted.
|
|
2385 This allows C-g to interrupt reads and writes, on most systems.
|
|
2386
|
|
2387 #### Another possibility is to just longjmp() out of the signal
|
|
2388 handler. According to W.R. Stevens, this should be OK on all
|
|
2389 systems. However, I don't want to deal with the potential
|
|
2390 evil ramifications of this at this point. */
|
|
2391
|
|
2392 #ifdef DGUX
|
|
2393 /* This gets us restartable system calls for efficiency.
|
|
2394 The "else" code will work as well. */
|
|
2395 return (berk_signal (signal_number, action));
|
|
2396 #else
|
|
2397 sigemptyset (&new_action.sa_mask);
|
|
2398 new_action.sa_handler = action;
|
|
2399 #if defined (SA_RESTART)
|
|
2400 /* Emacs mostly works better with restartable system services. If this
|
|
2401 * flag exists, we probably want to turn it on here.
|
|
2402 */
|
|
2403 new_action.sa_flags = SA_RESTART;
|
|
2404 #else
|
|
2405 new_action.sa_flags = 0;
|
|
2406 #endif
|
|
2407 sigaction (signal_number, &new_action, &old_action);
|
|
2408 return (old_action.sa_handler);
|
|
2409 #endif /* DGUX */
|
|
2410
|
|
2411 #else /* not 0 */
|
|
2412
|
|
2413 sigemptyset (&new_action.sa_mask);
|
|
2414 new_action.sa_handler = action;
|
|
2415 #if defined (SA_INTERRUPT) /* don't restart system calls, under SunOS */
|
|
2416 new_action.sa_flags = SA_INTERRUPT;
|
|
2417 #else
|
|
2418 new_action.sa_flags = 0;
|
|
2419 #endif
|
|
2420 sigaction (signal_number, &new_action, &old_action);
|
|
2421 return (signal_handler_t) (old_action.sa_handler);
|
|
2422
|
|
2423 #endif /* not 0 */
|
|
2424 }
|
|
2425
|
|
2426 #elif defined (HAVE_SIGBLOCK)
|
|
2427
|
|
2428 /* We use sigvec() rather than signal() if we have it, because
|
|
2429 it lets us specify interruptible system calls. */
|
|
2430 signal_handler_t
|
613
|
2431 qxe_reliable_signal (int signal_number, signal_handler_t action)
|
428
|
2432 {
|
|
2433 struct sigvec vec, ovec;
|
|
2434
|
|
2435 vec.sv_handler = action;
|
|
2436 vec.sv_mask = 0;
|
|
2437 #ifdef SV_INTERRUPT /* don't restart system calls */
|
|
2438 vec.sv_flags = SV_INTERRUPT;
|
|
2439 #else
|
|
2440 vec.sv_flags = 0;
|
|
2441 #endif
|
|
2442
|
|
2443 sigvec (signal_number, &vec, &ovec);
|
|
2444
|
|
2445 return (ovec.sv_handler);
|
|
2446 }
|
|
2447
|
|
2448 #endif /* HAVE_SIGBLOCK (HAVE_SIGPROCMASK) */
|
|
2449
|
|
2450
|
|
2451 /************************************************************************/
|
|
2452 /* Emulation of strerror() and errno support */
|
|
2453 /************************************************************************/
|
|
2454
|
|
2455 #ifndef HAVE_STRERROR
|
|
2456
|
|
2457 #if !defined(NeXT) && !defined(__alpha) && !defined(MACH) && !defined(LINUX) && !defined(IRIX) && !defined(__NetBSD__)
|
|
2458 /* Linux added here by Raymond L. Toy <toy@alydar.crd.ge.com> for XEmacs. */
|
|
2459 /* Irix added here by gparker@sni-usa.com for XEmacs. */
|
|
2460 /* NetBSD added here by James R Grinter <jrg@doc.ic.ac.uk> for XEmacs */
|
442
|
2461 extern const char *sys_errlist[];
|
428
|
2462 extern int sys_nerr;
|
|
2463 #endif
|
|
2464
|
|
2465 #ifdef __NetBSD__
|
|
2466 extern char *sys_errlist[];
|
|
2467 extern int sys_nerr;
|
|
2468 #endif
|
|
2469
|
|
2470
|
442
|
2471 const char *
|
428
|
2472 strerror (int errnum)
|
|
2473 {
|
|
2474 if (errnum >= 0 && errnum < sys_nerr)
|
|
2475 return sys_errlist[errnum];
|
442
|
2476 return ((const char *) GETTEXT ("Unknown error"));
|
428
|
2477 }
|
|
2478
|
|
2479 #endif /* ! HAVE_STRERROR */
|
|
2480
|
|
2481
|
|
2482 /************************************************************************/
|
|
2483 /* Encapsulations of system calls */
|
|
2484 /************************************************************************/
|
|
2485
|
771
|
2486 #ifdef WIN32_NATIVE
|
|
2487 #define PATHNAME_CONVERT_OUT(path, pathout) C_STRING_TO_TSTR (path, pathout)
|
|
2488 #else
|
|
2489 #define PATHNAME_CONVERT_OUT(path, pathout) \
|
|
2490 C_STRING_TO_EXTERNAL (path, pathout, Qfile_name)
|
|
2491 #endif
|
428
|
2492
|
|
2493 /***************** low-level calls ****************/
|
|
2494
|
|
2495 /*
|
|
2496 * On USG systems the system calls are INTERRUPTIBLE by signals
|
|
2497 * that the user program has elected to catch. Thus the system call
|
|
2498 * must be retried in these cases. To handle this without massive
|
|
2499 * changes in the source code, we remap the standard system call names
|
|
2500 * to names for our own functions in sysdep.c that do the system call
|
|
2501 * with retries. Actually, for portability reasons, it is good
|
|
2502 * programming practice, as this example shows, to limit all actual
|
|
2503 * system calls to a single occurrence in the source. Sure, this
|
|
2504 * adds an extra level of function call overhead but it is almost
|
|
2505 * always negligible. Fred Fish, Unisoft Systems Inc.
|
|
2506 */
|
|
2507
|
|
2508 /* Ben sez: read Dick Gabriel's essay about the Worse Is Better
|
|
2509 approach to programming and its connection to the silly
|
|
2510 interruptible-system-call business. To find it, look on
|
|
2511 Jamie's home page (http://www.jwz.org/worse-is-better.html). */
|
|
2512
|
771
|
2513 #ifdef WIN32_NATIVE
|
|
2514
|
|
2515 static int
|
|
2516 underlying_open_1 (const Extbyte *path, int oflag, int mode)
|
|
2517 {
|
|
2518 if (XEUNICODE_P)
|
|
2519 return _wopen ((const wchar_t *) path, oflag, mode);
|
|
2520 else
|
|
2521 return _open (path, oflag, mode);
|
|
2522 }
|
|
2523
|
|
2524 #endif /* WIN32_NATIVE */
|
|
2525
|
|
2526 /* Just one call with normal open() semantics. */
|
|
2527
|
|
2528 static int
|
|
2529 underlying_open (const Extbyte *path, int oflag, int mode)
|
|
2530 {
|
|
2531 #ifdef WIN32_NATIVE
|
|
2532 {
|
|
2533 /* Try to open file without _O_CREAT, to be able to write to hidden
|
|
2534 and system files. Force all file handles to be
|
|
2535 non-inheritable. */
|
|
2536 int res = underlying_open_1 (path, (oflag & ~_O_CREAT) | _O_NOINHERIT,
|
|
2537 mode);
|
|
2538 if (res >= 0)
|
|
2539 return res;
|
|
2540 return underlying_open_1 (path, oflag | _O_NOINHERIT, mode);
|
|
2541 }
|
|
2542 #else
|
|
2543 return open (path, oflag, mode);
|
|
2544 #endif /* WIN32_NATIVE */
|
|
2545 }
|
|
2546
|
|
2547 /* Like qxe_open() below but operates on externally-encoded filenames. */
|
|
2548
|
428
|
2549 int
|
771
|
2550 retry_open (const Extbyte *path, int oflag, ...)
|
428
|
2551 {
|
|
2552 int mode;
|
|
2553 va_list ap;
|
|
2554
|
|
2555 va_start (ap, oflag);
|
|
2556 mode = va_arg (ap, int);
|
|
2557 va_end (ap);
|
|
2558
|
440
|
2559 #ifdef INTERRUPTIBLE_OPEN
|
428
|
2560 {
|
|
2561 int rtnval;
|
771
|
2562 while ((rtnval = underlying_open (path, oflag, mode)) == -1
|
428
|
2563 && (errno == EINTR))
|
|
2564 DO_NOTHING;
|
|
2565 return rtnval;
|
|
2566 }
|
|
2567 #else
|
771
|
2568 return underlying_open (path, oflag, mode);
|
428
|
2569 #endif
|
|
2570 }
|
771
|
2571
|
|
2572 /* The basic external entry point to open(). Handles conversion to
|
|
2573 external encoding, interruptions, etc. */
|
|
2574
|
|
2575 int
|
|
2576 qxe_open (const Intbyte *path, int oflag, ...)
|
|
2577 {
|
|
2578 Extbyte *pathout;
|
|
2579 int mode;
|
|
2580 va_list ap;
|
|
2581
|
|
2582 va_start (ap, oflag);
|
|
2583 mode = va_arg (ap, int);
|
|
2584 va_end (ap);
|
|
2585
|
|
2586 PATHNAME_CONVERT_OUT (path, pathout);
|
|
2587 return retry_open (pathout, oflag, mode);
|
|
2588 }
|
|
2589
|
|
2590 /* Like qxe_open, only when open() is interrupted by EINTR, check for
|
428
|
2591 QUIT. This allows the callers of this function to be interrupted
|
|
2592 with C-g when, say, reading from named pipes. However, this should
|
|
2593 be used with caution, as it can GC.
|
|
2594
|
|
2595 This function will not function as expected on systems where open()
|
|
2596 is not interrupted by C-g. However, the worst that can happen is
|
|
2597 the fallback to simple open(). */
|
|
2598 int
|
771
|
2599 qxe_interruptible_open (const Intbyte *path, int oflag, int mode)
|
428
|
2600 {
|
|
2601 /* This function can GC */
|
771
|
2602 Extbyte *pathout;
|
|
2603
|
|
2604 PATHNAME_CONVERT_OUT (path, pathout);
|
428
|
2605
|
442
|
2606 #ifdef WIN32_NATIVE
|
440
|
2607 /* Make all handles non-inheritable */
|
|
2608 oflag |= _O_NOINHERIT;
|
|
2609 #endif
|
|
2610
|
428
|
2611 for (;;)
|
|
2612 {
|
771
|
2613 int rtnval = underlying_open (pathout, oflag, mode);
|
428
|
2614 if (!(rtnval == -1 && errno == EINTR))
|
|
2615 return rtnval;
|
|
2616 /* open() was interrupted. Was QUIT responsible? */
|
|
2617 QUIT;
|
|
2618 }
|
|
2619 }
|
|
2620
|
|
2621 int
|
771
|
2622 retry_close (int filedes)
|
428
|
2623 {
|
|
2624 #ifdef INTERRUPTIBLE_CLOSE
|
|
2625 int did_retry = 0;
|
|
2626 REGISTER int rtnval;
|
|
2627
|
|
2628 while ((rtnval = close (filedes)) == -1
|
|
2629 && (errno == EINTR))
|
|
2630 did_retry = 1;
|
|
2631
|
|
2632 /* If close is interrupted SunOS 4.1 may or may not have closed the
|
|
2633 file descriptor. If it did the second close will fail with
|
|
2634 errno = EBADF. That means we have succeeded. */
|
|
2635 if (rtnval == -1 && did_retry && errno == EBADF)
|
|
2636 return 0;
|
|
2637
|
|
2638 return rtnval;
|
|
2639 #else
|
|
2640 return close (filedes);
|
|
2641 #endif
|
|
2642 }
|
771
|
2643
|
|
2644 static ssize_t
|
|
2645 retry_read_1 (int fildes, void *buf, size_t nbyte, int allow_quit)
|
428
|
2646 {
|
|
2647 ssize_t rtnval;
|
|
2648
|
|
2649 /* No harm in looping regardless of the INTERRUPTIBLE_IO setting. */
|
|
2650 while ((rtnval = read (fildes, buf, nbyte)) == -1
|
|
2651 && (errno == EINTR))
|
|
2652 {
|
|
2653 if (allow_quit)
|
|
2654 REALLY_QUIT;
|
|
2655 }
|
|
2656 return rtnval;
|
|
2657 }
|
|
2658
|
|
2659 ssize_t
|
771
|
2660 retry_read (int fildes, void *buf, size_t nbyte)
|
428
|
2661 {
|
771
|
2662 return retry_read_1 (fildes, buf, nbyte, 0);
|
428
|
2663 }
|
771
|
2664
|
|
2665 static ssize_t
|
|
2666 retry_write_1 (int fildes, const void *buf, size_t nbyte, int allow_quit)
|
428
|
2667 {
|
|
2668 ssize_t bytes_written = 0;
|
442
|
2669 const char *b = (const char *) buf;
|
428
|
2670
|
|
2671 /* No harm in looping regardless of the INTERRUPTIBLE_IO setting. */
|
|
2672 while (nbyte > 0)
|
|
2673 {
|
|
2674 ssize_t rtnval = write (fildes, b, nbyte);
|
|
2675
|
|
2676 if (allow_quit)
|
|
2677 REALLY_QUIT;
|
|
2678
|
|
2679 if (rtnval == -1)
|
|
2680 {
|
|
2681 if (errno == EINTR)
|
|
2682 continue;
|
|
2683 else
|
|
2684 return bytes_written ? bytes_written : -1;
|
|
2685 }
|
|
2686 b += rtnval;
|
|
2687 nbyte -= rtnval;
|
|
2688 bytes_written += rtnval;
|
|
2689 }
|
|
2690 return bytes_written;
|
|
2691 }
|
|
2692
|
|
2693 ssize_t
|
771
|
2694 retry_write (int fildes, const void *buf, size_t nbyte)
|
428
|
2695 {
|
771
|
2696 return retry_write_1 (fildes, buf, nbyte, 0);
|
428
|
2697 }
|
771
|
2698
|
|
2699 /* Versions of read() and write() that allow quitting out of the actual
|
|
2700 I/O. We don't use immediate_quit (i.e. direct longjmp() out of the
|
|
2701 signal handler) because that's way too losing.
|
|
2702
|
|
2703 (#### Actually, longjmp()ing out of the signal handler may not be
|
|
2704 as losing as I thought. See qxe_reliable_signal() in sysdep.c.) */
|
|
2705
|
|
2706 Bytecount
|
|
2707 read_allowing_quit (int fildes, void *buf, Bytecount size)
|
|
2708 {
|
|
2709 QUIT;
|
|
2710 return retry_read_1 (fildes, buf, size, 1);
|
|
2711 }
|
|
2712
|
|
2713 Bytecount
|
|
2714 write_allowing_quit (int fildes, const void *buf, Bytecount size)
|
|
2715 {
|
|
2716 QUIT;
|
|
2717 return retry_write_1 (fildes, buf, size, 1);
|
|
2718 }
|
428
|
2719
|
|
2720
|
|
2721 /**************** stdio calls ****************/
|
|
2722
|
|
2723 /* There is at least some evidence that the stdio calls are interruptible
|
|
2724 just like the normal system calls, at least on some systems. In any
|
|
2725 case, it doesn't hurt to encapsulate them. */
|
|
2726
|
|
2727 /* #### Should also encapsulate fflush().
|
|
2728 #### Should conceivably encapsulate getchar() etc. What a pain! */
|
|
2729
|
|
2730 FILE *
|
771
|
2731 retry_fopen (const Extbyte *path, const Char_ASCII *mode)
|
428
|
2732 {
|
771
|
2733 #ifdef WIN32_NATIVE
|
|
2734 int fd;
|
|
2735 int oflag;
|
|
2736 const Char_ASCII *mode_save = mode;
|
|
2737
|
|
2738 /* Force all file handles to be non-inheritable. This is necessary to
|
|
2739 ensure child processes don't unwittingly inherit handles that might
|
|
2740 prevent future file access. */
|
|
2741
|
|
2742 if (mode[0] == 'r')
|
|
2743 oflag = O_RDONLY;
|
|
2744 else if (mode[0] == 'w' || mode[0] == 'a')
|
|
2745 oflag = O_WRONLY | O_CREAT | O_TRUNC;
|
|
2746 else
|
|
2747 return NULL;
|
|
2748
|
|
2749 /* Only do simplistic option parsing. */
|
|
2750 while (*++mode)
|
|
2751 if (mode[0] == '+')
|
|
2752 {
|
|
2753 oflag &= ~(O_RDONLY | O_WRONLY);
|
|
2754 oflag |= O_RDWR;
|
|
2755 }
|
|
2756 else if (mode[0] == 'b')
|
|
2757 {
|
|
2758 oflag &= ~O_TEXT;
|
|
2759 oflag |= O_BINARY;
|
|
2760 }
|
|
2761 else if (mode[0] == 't')
|
|
2762 {
|
|
2763 oflag &= ~O_BINARY;
|
|
2764 oflag |= O_TEXT;
|
|
2765 }
|
|
2766 else break;
|
|
2767
|
|
2768 fd = underlying_open (path, oflag, 0644);
|
|
2769 if (fd < 0)
|
|
2770 return NULL;
|
|
2771
|
|
2772 return _fdopen (fd, mode_save);
|
428
|
2773 #elif defined (INTERRUPTIBLE_OPEN)
|
|
2774 {
|
|
2775 FILE *rtnval;
|
771
|
2776 while (!(rtnval = fopen (path, mode)) && (errno == EINTR))
|
428
|
2777 DO_NOTHING;
|
|
2778 return rtnval;
|
|
2779 }
|
|
2780 #else
|
771
|
2781 return fopen (path, mode);
|
|
2782 #endif /* defined (INTERRUPTIBLE_OPEN) */
|
428
|
2783 }
|
771
|
2784
|
|
2785 FILE *
|
|
2786 qxe_fopen (const Intbyte *path, const Char_ASCII *mode)
|
|
2787 {
|
|
2788 Extbyte *pathout;
|
|
2789 PATHNAME_CONVERT_OUT (path, pathout);
|
|
2790 return retry_fopen (pathout, mode);
|
|
2791 }
|
|
2792
|
428
|
2793 int
|
771
|
2794 retry_fclose (FILE *stream)
|
428
|
2795 {
|
|
2796 #ifdef INTERRUPTIBLE_CLOSE
|
|
2797 int rtnval;
|
|
2798
|
|
2799 while ((rtnval = fclose (stream)) == EOF
|
|
2800 && (errno == EINTR))
|
|
2801 ;
|
|
2802 return rtnval;
|
|
2803 #else
|
|
2804 return fclose (stream);
|
|
2805 #endif
|
|
2806 }
|
771
|
2807
|
428
|
2808 size_t
|
771
|
2809 retry_fread (void *ptr, size_t size, size_t nitem, FILE *stream)
|
428
|
2810 {
|
|
2811 #ifdef INTERRUPTIBLE_IO
|
|
2812 size_t rtnval;
|
|
2813 size_t items_read = 0;
|
|
2814 char *b = (char *) ptr;
|
|
2815
|
|
2816 while (nitem > 0)
|
|
2817 {
|
|
2818 rtnval = fread (b, size, nitem, stream);
|
|
2819 if (rtnval == 0)
|
|
2820 {
|
|
2821 if (ferror (stream) && errno == EINTR)
|
|
2822 continue;
|
|
2823 else
|
|
2824 return items_read;
|
|
2825 }
|
|
2826 b += size*rtnval;
|
|
2827 nitem -= rtnval;
|
|
2828 items_read += rtnval;
|
|
2829 }
|
|
2830 return (items_read);
|
|
2831 #else
|
|
2832 return fread (ptr, size, nitem, stream);
|
|
2833 #endif
|
|
2834 }
|
771
|
2835
|
428
|
2836 size_t
|
771
|
2837 retry_fwrite (const void *ptr, size_t size, size_t nitem, FILE *stream)
|
428
|
2838 {
|
|
2839 #ifdef INTERRUPTIBLE_IO
|
|
2840 size_t rtnval;
|
|
2841 size_t items_written = 0;
|
442
|
2842 const char *b = (const char *) ptr;
|
428
|
2843
|
|
2844 while (nitem > 0)
|
|
2845 {
|
|
2846 rtnval = fwrite (b, size, nitem, stream);
|
|
2847 if (rtnval == 0)
|
|
2848 {
|
|
2849 if (ferror (stream) && errno == EINTR)
|
|
2850 continue;
|
|
2851 else
|
|
2852 return items_written;
|
|
2853 }
|
|
2854 b += size*rtnval;
|
|
2855 nitem -= rtnval;
|
|
2856 items_written += rtnval;
|
|
2857 }
|
|
2858 return (items_written);
|
|
2859 #else
|
|
2860 return fwrite (ptr, size, nitem, stream);
|
|
2861 #endif
|
|
2862 }
|
|
2863
|
|
2864 /********************* directory calls *******************/
|
|
2865
|
|
2866 int
|
771
|
2867 qxe_chdir (const Intbyte *path)
|
428
|
2868 {
|
771
|
2869 Extbyte *pathout;
|
|
2870 PATHNAME_CONVERT_OUT (path, pathout);
|
442
|
2871 #ifdef WIN32_NATIVE
|
771
|
2872 if (XEUNICODE_P)
|
|
2873 return _wchdir ((const wchar_t *) pathout);
|
|
2874 else
|
|
2875 return _chdir (pathout);
|
428
|
2876 #else
|
771
|
2877 return chdir (pathout);
|
428
|
2878 #endif
|
|
2879 }
|
771
|
2880
|
|
2881 int
|
|
2882 qxe_mkdir (const Intbyte *path, mode_t mode)
|
|
2883 {
|
|
2884 Extbyte *pathout;
|
|
2885 PATHNAME_CONVERT_OUT (path, pathout);
|
|
2886 #ifdef WIN32_NATIVE
|
|
2887 if (XEUNICODE_P)
|
|
2888 return _wmkdir ((const wchar_t *) pathout);
|
|
2889 else
|
|
2890 return _mkdir (pathout);
|
|
2891 #else
|
|
2892 return mkdir (pathout, mode);
|
|
2893 #endif
|
|
2894 }
|
|
2895
|
428
|
2896 DIR *
|
771
|
2897 qxe_opendir (const Intbyte *filename)
|
428
|
2898 {
|
771
|
2899 #ifdef WIN32_NATIVE
|
|
2900 return mswindows_opendir (filename);
|
|
2901 #else
|
428
|
2902 DIR *rtnval;
|
771
|
2903 Extbyte *pathout;
|
|
2904 PATHNAME_CONVERT_OUT (filename, pathout);
|
|
2905
|
|
2906 while (!(rtnval = opendir (pathout))
|
428
|
2907 && (errno == EINTR))
|
|
2908 ;
|
|
2909 return rtnval;
|
771
|
2910 #endif /* WIN32_NATIVE */
|
428
|
2911 }
|
771
|
2912
|
428
|
2913 DIRENTRY *
|
771
|
2914 qxe_readdir (DIR *dirp)
|
428
|
2915 {
|
771
|
2916 #ifdef WIN32_NATIVE
|
|
2917 return mswindows_readdir (dirp);
|
|
2918 #else /* not WIN32_NATIVE */
|
428
|
2919 DIRENTRY *rtnval;
|
|
2920
|
|
2921 /* Apparently setting errno is necessary on some systems?
|
|
2922 Maybe readdir() doesn't always set errno ?! */
|
|
2923 while (!(errno = 0, rtnval = readdir (dirp))
|
|
2924 && (errno == EINTR))
|
|
2925 ;
|
|
2926 #ifndef MULE
|
|
2927 return rtnval;
|
|
2928 #else /* MULE */
|
|
2929 if (rtnval == NULL) /* End of directory */
|
|
2930 return NULL;
|
|
2931 {
|
442
|
2932 const Extbyte * const external_name = (const Extbyte *) rtnval->d_name;
|
665
|
2933 Bytecount external_len = strlen (rtnval->d_name);
|
|
2934 const Intbyte *internal_name;
|
462
|
2935 Bytecount internal_len;
|
513
|
2936
|
462
|
2937 TO_INTERNAL_FORMAT (DATA, (external_name, external_len),
|
|
2938 ALLOCA, (internal_name, internal_len),
|
|
2939 Qfile_name);
|
|
2940
|
|
2941 /* check for common case of ASCII filename */
|
|
2942 if (internal_len == external_len &&
|
|
2943 !memcmp (external_name, internal_name, internal_len))
|
428
|
2944 return rtnval;
|
|
2945
|
|
2946 { /* Non-ASCII filename */
|
665
|
2947 static Intbyte_dynarr *internal_DIRENTRY;
|
428
|
2948 if (!internal_DIRENTRY)
|
665
|
2949 internal_DIRENTRY = Dynarr_new (Intbyte);
|
428
|
2950 else
|
|
2951 Dynarr_reset (internal_DIRENTRY);
|
|
2952
|
665
|
2953 Dynarr_add_many (internal_DIRENTRY, (Intbyte *) rtnval,
|
428
|
2954 offsetof (DIRENTRY, d_name));
|
|
2955
|
|
2956
|
|
2957 Dynarr_add_many (internal_DIRENTRY, internal_name, internal_len);
|
444
|
2958 Dynarr_add (internal_DIRENTRY, '\0'); /* NUL-terminate */
|
428
|
2959 return (DIRENTRY *) Dynarr_atp (internal_DIRENTRY, 0);
|
|
2960 }
|
|
2961 }
|
|
2962 #endif /* MULE */
|
771
|
2963 #endif /* WIN32_NATIVE */
|
428
|
2964 }
|
771
|
2965
|
428
|
2966 int
|
771
|
2967 qxe_closedir (DIR *dirp)
|
428
|
2968 {
|
771
|
2969 #ifdef WIN32_NATIVE
|
|
2970 return mswindows_closedir (dirp);
|
|
2971 #else /* not WIN32_NATIVE */
|
428
|
2972 int rtnval;
|
|
2973
|
|
2974 while ((rtnval = closedir (dirp)) == -1
|
|
2975 && (errno == EINTR))
|
|
2976 ;
|
|
2977 return rtnval;
|
771
|
2978 #endif /* WIN32_NATIVE */
|
428
|
2979 }
|
771
|
2980
|
428
|
2981 int
|
771
|
2982 qxe_rmdir (const Intbyte *path)
|
|
2983 {
|
|
2984 Extbyte *pathout;
|
|
2985 PATHNAME_CONVERT_OUT (path, pathout);
|
|
2986 #ifdef WIN32_NATIVE
|
|
2987 if (XEUNICODE_P)
|
|
2988 return _wrmdir ((const wchar_t *) pathout);
|
|
2989 else
|
|
2990 return _rmdir (pathout);
|
|
2991 #else
|
|
2992 return rmdir (pathout);
|
|
2993 #endif
|
|
2994 }
|
|
2995
|
|
2996 Intbyte *
|
|
2997 qxe_allocating_getcwd (void)
|
428
|
2998 {
|
771
|
2999 #ifdef HAVE_GETCWD
|
|
3000 Bytecount cwdsize = 1024;
|
|
3001 Extbyte *cwd = xnew_array (Extbyte, cwdsize);
|
|
3002
|
|
3003 /* Many getcwd()'s can take a NULL argument and malloc() the right amount
|
|
3004 of data, but this is non-standard. */
|
|
3005 while (1)
|
|
3006 {
|
|
3007 #ifdef WIN32_NATIVE
|
|
3008 Extbyte *ret;
|
|
3009
|
|
3010 if (XEUNICODE_P)
|
|
3011 ret = (Extbyte *) _wgetcwd ((wchar_t *) cwd,
|
|
3012 cwdsize / sizeof (wchar_t));
|
|
3013 else
|
|
3014 ret = _getcwd (cwd, cwdsize);
|
|
3015
|
|
3016 if (ret)
|
|
3017 {
|
|
3018 Intbyte *retin;
|
|
3019 TSTR_TO_C_STRING_MALLOC (ret, retin);
|
|
3020 xfree (cwd);
|
|
3021 return retin;
|
|
3022 }
|
|
3023 #else
|
|
3024 Extbyte *ret = getcwd (cwd, cwdsize);
|
|
3025 if (ret)
|
|
3026 {
|
|
3027 Intbyte *retin;
|
|
3028 EXTERNAL_TO_C_STRING_MALLOC (ret, retin, Qfile_name);
|
|
3029 xfree (cwd);
|
|
3030 return retin;
|
|
3031 }
|
|
3032 #endif /* WIN32_NATIVE */
|
|
3033
|
|
3034 if (errno == ERANGE)
|
|
3035 {
|
|
3036 cwdsize *= 2;
|
|
3037 XREALLOC_ARRAY (cwd, Extbyte, cwdsize);
|
|
3038 }
|
|
3039 else
|
|
3040 {
|
|
3041 xfree (cwd);
|
|
3042 return NULL;
|
|
3043 }
|
|
3044 }
|
|
3045 #else
|
|
3046 Extbyte chingame_limitos_arbitrarios[PATH_MAX];
|
|
3047 Intbyte *ret2;
|
|
3048
|
|
3049 if (!getwd (chingame_limitos_arbitrarios))
|
|
3050 return 0;
|
|
3051 EXTERNAL_TO_C_STRING_MALLOC (chingame_limitos_arbitrarios, ret2, Qfile_name);
|
|
3052 return ret2;
|
|
3053 #endif /* HAVE_GETCWD */
|
428
|
3054 }
|
|
3055
|
|
3056 /***************** file-information calls ******************/
|
|
3057
|
|
3058 int
|
771
|
3059 qxe_access (const Intbyte *path, int mode)
|
428
|
3060 {
|
771
|
3061 #ifdef WIN32_NATIVE
|
|
3062 return mswindows_access (path, mode);
|
|
3063 #else /* not WIN32_NATIVE */
|
|
3064 Extbyte *pathout;
|
|
3065 PATHNAME_CONVERT_OUT (path, pathout);
|
|
3066 return access (pathout, mode);
|
|
3067 #endif /* WIN32_NATIVE */
|
428
|
3068 }
|
771
|
3069
|
|
3070 #if defined (HAVE_EACCESS)
|
428
|
3071 int
|
771
|
3072 qxe_eaccess (const Intbyte *path, int mode)
|
428
|
3073 {
|
771
|
3074 Extbyte *pathout;
|
|
3075 PATHNAME_CONVERT_OUT (path, pathout);
|
|
3076 return eaccess (pathout, mode);
|
428
|
3077 }
|
771
|
3078 #endif /* defined (HAVE_EACCESS) */
|
|
3079
|
428
|
3080 int
|
771
|
3081 qxe_lstat (const Intbyte *path, struct stat *buf)
|
428
|
3082 {
|
771
|
3083 /* if system does not have symbolic links, it does not have lstat.
|
|
3084 In that case, use ordinary stat instead. */
|
|
3085 #ifndef S_IFLNK
|
|
3086 return qxe_stat (path, buf);
|
|
3087 #else
|
|
3088 Extbyte *pathout;
|
|
3089 PATHNAME_CONVERT_OUT (path, pathout);
|
|
3090 return lstat (pathout, buf);
|
|
3091 #endif
|
428
|
3092 }
|
771
|
3093
|
|
3094 #if defined (HAVE_READLINK)
|
428
|
3095 int
|
771
|
3096 qxe_readlink (const Intbyte *path, Intbyte *buf, size_t bufsiz)
|
428
|
3097 {
|
771
|
3098 int retval;
|
|
3099 Extbyte *pathout;
|
|
3100
|
|
3101 PATHNAME_CONVERT_OUT (path, pathout);
|
|
3102 retval = readlink (pathout, (char *) buf, bufsiz);
|
|
3103 if (retval < 0)
|
|
3104 return retval;
|
|
3105 {
|
|
3106 Intbyte *intbuf;
|
|
3107 Bytecount tamanho;
|
|
3108
|
|
3109 TO_INTERNAL_FORMAT (DATA, (buf, retval),
|
|
3110 ALLOCA, (intbuf, tamanho), Qfile_name);
|
|
3111 /* the man page says this function does not null-terminate */
|
|
3112 if (tamanho >= (Bytecount) bufsiz)
|
|
3113 tamanho = bufsiz;
|
|
3114 memcpy (buf, intbuf, tamanho);
|
|
3115 return tamanho;
|
|
3116 }
|
428
|
3117 }
|
771
|
3118 #endif /* defined (HAVE_READLINK) */
|
|
3119
|
432
|
3120 int
|
771
|
3121 qxe_fstat (int fd, struct stat *buf)
|
432
|
3122 {
|
442
|
3123 #ifdef WIN32_NATIVE
|
|
3124 return mswindows_fstat (fd, buf);
|
|
3125 #else
|
432
|
3126 return fstat (fd, buf);
|
771
|
3127 #endif /* WIN32_NATIVE */
|
432
|
3128 }
|
|
3129
|
428
|
3130 int
|
771
|
3131 qxe_stat (const Intbyte *path, struct stat *buf)
|
428
|
3132 {
|
442
|
3133 #ifdef WIN32_NATIVE
|
|
3134 return mswindows_stat (path, buf);
|
771
|
3135 #else /* not WIN32_NATIVE */
|
|
3136 Extbyte *pathout;
|
|
3137 PATHNAME_CONVERT_OUT (path, pathout);
|
|
3138 return stat (pathout, buf);
|
|
3139 #endif /* WIN32_NATIVE */
|
428
|
3140 }
|
|
3141
|
771
|
3142
|
428
|
3143 /****************** file-manipulation calls *****************/
|
|
3144
|
|
3145 int
|
771
|
3146 qxe_chmod (const Intbyte *path, mode_t mode)
|
428
|
3147 {
|
771
|
3148 Extbyte *pathout;
|
|
3149 PATHNAME_CONVERT_OUT (path, pathout);
|
|
3150 #ifdef WIN32_NATIVE
|
|
3151 if (XEUNICODE_P)
|
|
3152 return _wchmod ((const wchar_t *) pathout, mode);
|
|
3153 else
|
|
3154 return _chmod (pathout, mode);
|
|
3155 #else
|
|
3156 return chmod (pathout, mode);
|
|
3157 #endif
|
428
|
3158 }
|
771
|
3159
|
|
3160 #if defined (HAVE_LINK)
|
428
|
3161 int
|
771
|
3162 qxe_link (const Intbyte *existing, const Intbyte *new)
|
428
|
3163 {
|
771
|
3164 #ifdef WIN32_NATIVE
|
|
3165 return mswindows_link (existing, new);
|
|
3166 #else /* not WIN32_NATIVE */
|
|
3167 Extbyte *existingout, *newout;
|
|
3168 PATHNAME_CONVERT_OUT (existing, existingout);
|
|
3169 PATHNAME_CONVERT_OUT (new, newout);
|
|
3170 return link (existingout, newout);
|
|
3171 #endif /* WIN32_NATIVE */
|
428
|
3172 }
|
771
|
3173 #endif /* defined (HAVE_LINK) */
|
|
3174
|
428
|
3175 int
|
771
|
3176 qxe_rename (const Intbyte *old, const Intbyte *new)
|
428
|
3177 {
|
442
|
3178 #ifdef WIN32_NATIVE
|
771
|
3179 return mswindows_rename (old, new);
|
|
3180 #else /* not WIN32_NATIVE */
|
|
3181 Extbyte *oldout, *newout;
|
|
3182 PATHNAME_CONVERT_OUT (old, oldout);
|
|
3183 PATHNAME_CONVERT_OUT (new, newout);
|
|
3184 return rename (oldout, newout);
|
442
|
3185 #endif /* WIN32_NATIVE */
|
428
|
3186 }
|
771
|
3187
|
|
3188 #if defined (HAVE_SYMLINK)
|
428
|
3189 int
|
771
|
3190 qxe_symlink (const Intbyte *name1, const Intbyte *name2)
|
428
|
3191 {
|
771
|
3192 Extbyte *name1out, *name2out;
|
|
3193 PATHNAME_CONVERT_OUT (name1, name1out);
|
|
3194 PATHNAME_CONVERT_OUT (name2, name2out);
|
|
3195 return symlink (name1out, name2out);
|
428
|
3196 }
|
771
|
3197 #endif /* defined (HAVE_SYMLINK) */
|
|
3198
|
428
|
3199 int
|
771
|
3200 qxe_unlink (const Intbyte *path)
|
428
|
3201 {
|
771
|
3202 #ifdef WIN32_NATIVE
|
|
3203 return mswindows_unlink (path);
|
|
3204 #else /* not WIN32_NATIVE */
|
|
3205 Extbyte *pathout;
|
|
3206 PATHNAME_CONVERT_OUT (path, pathout);
|
|
3207 return unlink (pathout);
|
|
3208 #endif /* WIN32_NATIVE */
|
428
|
3209 }
|
771
|
3210
|
|
3211
|
|
3212 /****************** process calls *****************/
|
|
3213
|
428
|
3214 int
|
771
|
3215 qxe_execve (const Intbyte *filename, Intbyte * const argv[],
|
|
3216 Intbyte * const envp[])
|
428
|
3217 {
|
771
|
3218 int i, argc, envc;
|
|
3219 Extbyte *pathext;
|
|
3220 Extbyte **new_argv;
|
|
3221 Extbyte **new_envp;
|
|
3222
|
|
3223 PATHNAME_CONVERT_OUT (filename, pathext);
|
|
3224
|
428
|
3225 for (argc = 0; argv[argc]; argc++)
|
|
3226 ;
|
771
|
3227 new_argv = alloca_array (Extbyte *, argc + 1);
|
428
|
3228 for (i = 0; i < argc; i++)
|
771
|
3229 C_STRING_TO_EXTERNAL (argv[i], new_argv[i], Qnative);
|
428
|
3230 new_argv[argc] = NULL;
|
771
|
3231
|
|
3232 for (envc = 0; envp[envc]; envc++)
|
|
3233 ;
|
|
3234 new_envp = alloca_array (Extbyte *, envc + 1);
|
|
3235 for (i = 0; i < envc; i++)
|
|
3236 C_STRING_TO_EXTERNAL (envp[i], new_envp[i], Qnative);
|
|
3237 new_envp[envc] = NULL;
|
|
3238
|
|
3239 return execve (pathext, new_argv, new_envp);
|
|
3240 }
|
|
3241
|
|
3242 pid_t
|
|
3243 qxe_getpid (void)
|
|
3244 {
|
|
3245 #ifdef WIN32_NATIVE
|
|
3246 return abs (getpid ());
|
|
3247 #else
|
|
3248 return getpid ();
|
|
3249 #endif
|
428
|
3250 }
|
771
|
3251
|
|
3252
|
|
3253 /****************** passwd calls *****************/
|
|
3254
|
|
3255 struct passwd cached_pwd;
|
|
3256
|
|
3257 static struct passwd *
|
|
3258 copy_in_passwd (struct passwd *pwd)
|
|
3259 {
|
|
3260 if (!pwd)
|
|
3261 return NULL;
|
|
3262
|
|
3263 if (cached_pwd.pw_name)
|
|
3264 xfree (cached_pwd.pw_name);
|
|
3265 if (cached_pwd.pw_passwd)
|
|
3266 xfree (cached_pwd.pw_passwd);
|
|
3267 if (cached_pwd.pw_gecos)
|
|
3268 xfree (cached_pwd.pw_gecos);
|
|
3269 if (cached_pwd.pw_dir)
|
|
3270 xfree (cached_pwd.pw_dir);
|
|
3271 if (cached_pwd.pw_shell)
|
|
3272 xfree (cached_pwd.pw_shell);
|
|
3273
|
|
3274 cached_pwd = *pwd;
|
|
3275 if (cached_pwd.pw_name)
|
|
3276 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_name,
|
|
3277 C_STRING_MALLOC, cached_pwd.pw_name, Qnative);
|
|
3278 if (cached_pwd.pw_passwd)
|
|
3279 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_passwd,
|
|
3280 C_STRING_MALLOC, cached_pwd.pw_passwd, Qnative);
|
|
3281 if (cached_pwd.pw_gecos)
|
|
3282 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_gecos,
|
|
3283 C_STRING_MALLOC, cached_pwd.pw_gecos, Qnative);
|
|
3284 if (cached_pwd.pw_dir)
|
|
3285 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_dir,
|
|
3286 C_STRING_MALLOC, cached_pwd.pw_dir, Qfile_name);
|
|
3287 if (cached_pwd.pw_shell)
|
|
3288 TO_INTERNAL_FORMAT (C_STRING, cached_pwd.pw_shell,
|
|
3289 C_STRING_MALLOC, cached_pwd.pw_shell, Qfile_name);
|
|
3290 return &cached_pwd;
|
|
3291 }
|
|
3292
|
|
3293 struct passwd *
|
|
3294 qxe_getpwnam (const Intbyte *name)
|
|
3295 {
|
|
3296 #ifdef WIN32_NATIVE
|
|
3297 /* Synthetic versions are defined in nt.c and already do conversion. */
|
|
3298 return getpwnam (name);
|
|
3299 #else
|
|
3300 Extbyte *nameext;
|
|
3301 C_STRING_TO_EXTERNAL (name, nameext, Qnative);
|
|
3302
|
|
3303 return copy_in_passwd (getpwnam (nameext));
|
|
3304 #endif /* WIN32_NATIVE */
|
|
3305 }
|
|
3306
|
|
3307 struct passwd *
|
|
3308 qxe_getpwuid (uid_t uid)
|
|
3309 {
|
|
3310 #ifdef WIN32_NATIVE
|
|
3311 /* Synthetic versions are defined in nt.c and already do conversion. */
|
|
3312 return getpwuid (uid);
|
|
3313 #else
|
|
3314 return copy_in_passwd (getpwuid (uid));
|
|
3315 #endif /* WIN32_NATIVE */
|
|
3316 }
|
|
3317
|
|
3318 #ifndef WIN32_NATIVE
|
|
3319
|
|
3320 struct passwd *
|
|
3321 qxe_getpwent (void)
|
|
3322 {
|
|
3323 /* No WIN32_NATIVE version of this. */
|
|
3324 return copy_in_passwd (getpwent ());
|
|
3325 }
|
|
3326
|
|
3327 #endif /* not WIN32_NATIVE */
|
|
3328
|
|
3329 /****************** time calls *****************/
|
|
3330
|
|
3331 static Intbyte *ctime_static;
|
|
3332
|
|
3333 Intbyte *
|
|
3334 qxe_ctime (const time_t *t)
|
|
3335 {
|
|
3336 Extbyte *str = (Extbyte *) ctime (t);
|
|
3337 if (!str) /* can happen on MS Windows */
|
|
3338 return (Intbyte *) "Sun Jan 01 00:00:00 1970";
|
|
3339 if (ctime_static)
|
|
3340 xfree (ctime_static);
|
|
3341 EXTERNAL_TO_C_STRING_MALLOC (str, ctime_static, Qnative);
|
|
3342 return ctime_static;
|
|
3343 }
|
428
|
3344
|
|
3345
|
|
3346 /************************************************************************/
|
|
3347 /* Emulations of missing system calls */
|
|
3348 /************************************************************************/
|
|
3349
|
|
3350 /***** (these are primarily required for USG, it seems) *****/
|
|
3351
|
|
3352 /*
|
|
3353 * Emulate rename using unlink/link. Note that this is
|
|
3354 * only partially correct. Also, doesn't enforce restriction
|
|
3355 * that files be of same type (regular->regular, dir->dir, etc).
|
|
3356 */
|
|
3357
|
|
3358 #ifndef HAVE_RENAME
|
|
3359 int
|
771
|
3360 rename (const Extbyte *from, const Extbyte *to)
|
428
|
3361 {
|
|
3362 if (access (from, 0) == 0)
|
|
3363 {
|
|
3364 unlink (to);
|
|
3365 if (link (from, to) == 0)
|
|
3366 if (unlink (from) == 0)
|
|
3367 return (0);
|
|
3368 }
|
|
3369 return (-1);
|
|
3370 }
|
|
3371 #endif /* HAVE_RENAME */
|
|
3372
|
|
3373 #ifdef HPUX
|
|
3374 #ifndef HAVE_PERROR
|
|
3375
|
|
3376 /* HPUX curses library references perror, but as far as we know
|
|
3377 it won't be called. Anyway this definition will do for now. */
|
|
3378
|
|
3379 perror (void)
|
|
3380 {
|
|
3381 }
|
|
3382
|
|
3383 #endif /* not HAVE_PERROR */
|
|
3384 #endif /* HPUX */
|
|
3385
|
|
3386 #ifndef HAVE_DUP2
|
|
3387
|
|
3388 /*
|
|
3389 * Emulate BSD dup2. First close newd if it already exists.
|
|
3390 * Then, attempt to dup oldd. If not successful, call dup2 recursively
|
|
3391 * until we are, then close the unsuccessful ones.
|
|
3392 */
|
|
3393
|
|
3394 int
|
|
3395 dup2 (int oldd, int newd)
|
|
3396 {
|
|
3397 int fd, ret;
|
|
3398
|
771
|
3399 retry_close (newd);
|
428
|
3400
|
|
3401 #ifdef F_DUPFD
|
|
3402 fd = fcntl (oldd, F_DUPFD, newd);
|
|
3403 if (fd != newd)
|
563
|
3404 signal_ferror_with_frob (Qfile_error, lisp_strerror (errno),
|
|
3405 "can't dup2 (%i, %i)", oldd, newd);
|
428
|
3406 #else
|
|
3407 fd = dup (old);
|
|
3408 if (fd == -1)
|
|
3409 return -1;
|
|
3410 if (fd == new)
|
|
3411 return new;
|
|
3412 ret = dup2 (old, new);
|
771
|
3413 retry_close (fd);
|
428
|
3414 return ret;
|
|
3415 #endif /* F_DUPFD */
|
|
3416 }
|
|
3417
|
|
3418 #endif /* not HAVE_DUP2 */
|
|
3419
|
|
3420 /*
|
|
3421 * Gettimeofday. Simulate as much as possible. Only accurate
|
|
3422 * to nearest second. Emacs doesn't use tzp so ignore it for now.
|
|
3423 */
|
|
3424
|
|
3425 #if !defined (HAVE_GETTIMEOFDAY)
|
|
3426
|
|
3427 int
|
|
3428 gettimeofday (struct timeval *tp, struct timezone *tzp)
|
|
3429 {
|
|
3430 extern long time ();
|
|
3431
|
|
3432 tp->tv_sec = time ((long *)0);
|
|
3433 tp->tv_usec = 0;
|
|
3434 if (tzp != 0)
|
|
3435 tzp->tz_minuteswest = -1;
|
|
3436 return (0);
|
|
3437 }
|
|
3438
|
|
3439 #endif /* !HAVE_GETTIMEOFDAY */
|
|
3440
|
|
3441 /* No need to encapsulate utime and utimes explicitly because all
|
|
3442 access to those functions goes through the following. */
|
|
3443
|
|
3444 int
|
592
|
3445 set_file_times (Lisp_Object path, EMACS_TIME atime, EMACS_TIME mtime)
|
428
|
3446 {
|
592
|
3447 #if defined (WIN32_NATIVE)
|
460
|
3448 struct utimbuf utb;
|
|
3449 utb.actime = EMACS_SECS (atime);
|
|
3450 utb.modtime = EMACS_SECS (mtime);
|
592
|
3451 return mswindows_utime (path, &utb);
|
|
3452 #elif defined (HAVE_UTIME)
|
|
3453 struct utimbuf utb;
|
|
3454 Extbyte *filename;
|
|
3455 utb.actime = EMACS_SECS (atime);
|
|
3456 utb.modtime = EMACS_SECS (mtime);
|
|
3457 LISP_STRING_TO_EXTERNAL (path, filename, Qfile_name);
|
460
|
3458 return utime (filename, &utb);
|
|
3459 #elif defined (HAVE_UTIMES)
|
428
|
3460 struct timeval tv[2];
|
592
|
3461 Extbyte *filename;
|
428
|
3462 tv[0] = atime;
|
|
3463 tv[1] = mtime;
|
592
|
3464 LISP_STRING_TO_EXTERNAL (path, filename, Qfile_name);
|
428
|
3465 return utimes (filename, tv);
|
460
|
3466 #else
|
|
3467 /* No file times setting function available. */
|
|
3468 return -1;
|
|
3469 #endif
|
428
|
3470 }
|
|
3471
|
|
3472 /* */
|
|
3473
|
|
3474 static long ticks_per_second;
|
|
3475 static long orig_user_ticks, orig_system_ticks;
|
|
3476 EMACS_TIME orig_real_time;
|
|
3477
|
|
3478 static int process_times_available;
|
|
3479
|
|
3480 /* Return the relative user and system tick count. We try to
|
|
3481 maintain calculations in terms of integers as long as possible
|
|
3482 for increased accuracy. */
|
|
3483
|
|
3484 static int
|
|
3485 get_process_times_1 (long *user_ticks, long *system_ticks)
|
|
3486 {
|
442
|
3487 #if defined (_SC_CLK_TCK) || defined (CLK_TCK) && !defined(WIN32_NATIVE)
|
428
|
3488 /* We have the POSIX times() function available. */
|
777
|
3489 /* #### Perhaps we should just use a configure test for times()? */
|
428
|
3490 struct tms tttt;
|
|
3491 times (&tttt);
|
|
3492 *user_ticks = (long) tttt.tms_utime;
|
|
3493 *system_ticks = (long) tttt.tms_stime;
|
|
3494 return 1;
|
|
3495 #elif defined (CLOCKS_PER_SEC)
|
|
3496 *user_ticks = (long) clock ();
|
|
3497 *system_ticks = 0;
|
|
3498 return 1;
|
|
3499 #else
|
|
3500 return 0;
|
|
3501 #endif
|
|
3502 }
|
|
3503
|
|
3504 void
|
|
3505 init_process_times_very_early (void)
|
|
3506 {
|
|
3507 #if defined (_SC_CLK_TCK)
|
|
3508 ticks_per_second = sysconf (_SC_CLK_TCK);
|
|
3509 #elif defined (CLK_TCK)
|
|
3510 ticks_per_second = CLK_TCK;
|
|
3511 #elif defined (CLOCKS_PER_SEC)
|
|
3512 ticks_per_second = CLOCKS_PER_SEC;
|
|
3513 #endif
|
|
3514
|
|
3515 process_times_available = get_process_times_1 (&orig_user_ticks,
|
|
3516 &orig_system_ticks);
|
|
3517 EMACS_GET_TIME (orig_real_time);
|
|
3518 }
|
|
3519
|
|
3520 /* Return the user and system times used up by this process so far. */
|
|
3521 void
|
|
3522 get_process_times (double *user_time, double *system_time, double *real_time)
|
|
3523 {
|
|
3524 EMACS_TIME curr_real_time;
|
|
3525 EMACS_TIME elapsed_time;
|
|
3526 long curr_user_ticks, curr_system_ticks;
|
|
3527
|
|
3528 EMACS_GET_TIME (curr_real_time);
|
|
3529 EMACS_SUB_TIME (elapsed_time, curr_real_time, orig_real_time);
|
|
3530 *real_time = (EMACS_SECS (elapsed_time)
|
|
3531 + ((double) EMACS_USECS (elapsed_time)) / 1000000);
|
|
3532 if (get_process_times_1 (&curr_user_ticks, &curr_system_ticks))
|
|
3533 {
|
|
3534 *user_time = (((double) (curr_user_ticks - orig_user_ticks))
|
|
3535 / ticks_per_second);
|
|
3536 *system_time = (((double) (curr_system_ticks - orig_system_ticks))
|
|
3537 / ticks_per_second);
|
|
3538 }
|
|
3539 else
|
|
3540 {
|
|
3541 /* A lame OS */
|
|
3542 *user_time = *real_time;
|
|
3543 *system_time = 0;
|
|
3544 }
|
|
3545 }
|
|
3546
|
|
3547 #ifndef HAVE_RANDOM
|
|
3548 #ifdef random
|
|
3549 #define HAVE_RANDOM
|
|
3550 #endif
|
|
3551 #endif
|
|
3552
|
|
3553 /* Figure out how many bits the system's random number generator uses.
|
|
3554 `random' and `lrand48' are assumed to return 31 usable bits.
|
|
3555 BSD `rand' returns a 31 bit value but the low order bits are unusable;
|
|
3556 so we'll shift it and treat it like the 15-bit USG `rand'. */
|
|
3557
|
|
3558 #ifndef RAND_BITS
|
|
3559 # ifdef HAVE_RANDOM
|
|
3560 # define RAND_BITS 31
|
|
3561 # else /* !HAVE_RANDOM */
|
|
3562 # ifdef HAVE_LRAND48
|
|
3563 # define RAND_BITS 31
|
|
3564 # define random lrand48
|
|
3565 # else /* !HAVE_LRAND48 */
|
|
3566 # define RAND_BITS 15
|
|
3567 # if RAND_MAX == 32767
|
|
3568 # define random rand
|
|
3569 # else /* RAND_MAX != 32767 */
|
|
3570 # if RAND_MAX == 2147483647
|
|
3571 # define random() (rand () >> 16)
|
|
3572 # else /* RAND_MAX != 2147483647 */
|
|
3573 # ifdef USG
|
|
3574 # define random rand
|
|
3575 # else
|
|
3576 # define random() (rand () >> 16)
|
|
3577 # endif /* !BSD */
|
|
3578 # endif /* RAND_MAX != 2147483647 */
|
|
3579 # endif /* RAND_MAX != 32767 */
|
|
3580 # endif /* !HAVE_LRAND48 */
|
|
3581 # endif /* !HAVE_RANDOM */
|
|
3582 #endif /* !RAND_BITS */
|
|
3583
|
|
3584 void
|
|
3585 seed_random (long arg)
|
|
3586 {
|
|
3587 #ifdef HAVE_RANDOM
|
|
3588 srandom ((unsigned int)arg);
|
|
3589 #else
|
|
3590 # ifdef HAVE_LRAND48
|
|
3591 srand48 (arg);
|
|
3592 # else
|
|
3593 srand ((unsigned int)arg);
|
|
3594 # endif
|
|
3595 #endif
|
|
3596 }
|
|
3597
|
|
3598 /*
|
|
3599 * Build a full Emacs-sized word out of whatever we've got.
|
|
3600 * This suffices even for a 64-bit architecture with a 15-bit rand.
|
|
3601 */
|
|
3602 long
|
|
3603 get_random (void)
|
|
3604 {
|
|
3605 long val = random ();
|
|
3606 #if VALBITS > RAND_BITS
|
|
3607 val = (val << RAND_BITS) ^ random ();
|
|
3608 #if VALBITS > 2*RAND_BITS
|
|
3609 val = (val << RAND_BITS) ^ random ();
|
|
3610 #if VALBITS > 3*RAND_BITS
|
|
3611 val = (val << RAND_BITS) ^ random ();
|
|
3612 #if VALBITS > 4*RAND_BITS
|
|
3613 val = (val << RAND_BITS) ^ random ();
|
|
3614 #endif /* need at least 5 */
|
|
3615 #endif /* need at least 4 */
|
|
3616 #endif /* need at least 3 */
|
|
3617 #endif /* need at least 2 */
|
|
3618 return val & ((1L << VALBITS) - 1);
|
|
3619 }
|
|
3620
|
|
3621
|
|
3622 /************************************************************************/
|
|
3623 /* Strings corresponding to defined signals */
|
|
3624 /************************************************************************/
|
|
3625
|
|
3626 #if !defined (SYS_SIGLIST_DECLARED) && !defined (HAVE_SYS_SIGLIST)
|
|
3627
|
442
|
3628 #if defined(WIN32_NATIVE) || defined(CYGWIN)
|
|
3629 const char *sys_siglist[] =
|
428
|
3630 {
|
771
|
3631 /* $$####begin-snarf */
|
428
|
3632 "bum signal!!",
|
|
3633 "hangup",
|
|
3634 "interrupt",
|
|
3635 "quit",
|
|
3636 "illegal instruction",
|
|
3637 "trace trap",
|
|
3638 "iot instruction",
|
|
3639 "emt instruction",
|
|
3640 "floating point exception",
|
|
3641 "kill",
|
|
3642 "bus error",
|
|
3643 "segmentation violation",
|
|
3644 "bad argument to system call",
|
|
3645 "write on a pipe with no one to read it",
|
|
3646 "alarm clock",
|
|
3647 "software termination signal from kill",
|
|
3648 "status signal",
|
|
3649 "sendable stop signal not from tty",
|
|
3650 "stop signal from tty",
|
|
3651 "continue a stopped process",
|
|
3652 "child status has changed",
|
|
3653 "background read attempted from control tty",
|
|
3654 "background write attempted from control tty",
|
|
3655 "input record available at control tty",
|
|
3656 "exceeded CPU time limit",
|
|
3657 "exceeded file size limit"
|
771
|
3658 /* $$####end-snarf */
|
428
|
3659 };
|
|
3660 #endif
|
|
3661
|
|
3662 #ifdef USG
|
|
3663 #ifdef AIX
|
442
|
3664 const char *sys_siglist[NSIG + 1] =
|
428
|
3665 {
|
|
3666 /* AIX has changed the signals a bit */
|
771
|
3667 /* $$####begin-snarf */
|
|
3668 "bogus signal", /* 0 */
|
|
3669 "hangup", /* 1 SIGHUP */
|
|
3670 "interrupt", /* 2 SIGINT */
|
|
3671 "quit", /* 3 SIGQUIT */
|
|
3672 "illegal instruction", /* 4 SIGILL */
|
|
3673 "trace trap", /* 5 SIGTRAP */
|
|
3674 "IOT instruction", /* 6 SIGIOT */
|
|
3675 "crash likely", /* 7 SIGDANGER */
|
|
3676 "floating point exception", /* 8 SIGFPE */
|
|
3677 "kill", /* 9 SIGKILL */
|
|
3678 "bus error", /* 10 SIGBUS */
|
|
3679 "segmentation violation", /* 11 SIGSEGV */
|
|
3680 "bad argument to system call", /* 12 SIGSYS */
|
|
3681 "write on a pipe with no one to read it", /* 13 SIGPIPE */
|
|
3682 "alarm clock", /* 14 SIGALRM */
|
|
3683 "software termination signum", /* 15 SIGTERM */
|
|
3684 "user defined signal 1", /* 16 SIGUSR1 */
|
|
3685 "user defined signal 2", /* 17 SIGUSR2 */
|
|
3686 "death of a child", /* 18 SIGCLD */
|
|
3687 "power-fail restart", /* 19 SIGPWR */
|
|
3688 "bogus signal", /* 20 */
|
|
3689 "bogus signal", /* 21 */
|
|
3690 "bogus signal", /* 22 */
|
|
3691 "bogus signal", /* 23 */
|
|
3692 "bogus signal", /* 24 */
|
|
3693 "LAN I/O interrupt", /* 25 SIGAIO */
|
|
3694 "PTY I/O interrupt", /* 26 SIGPTY */
|
|
3695 "I/O intervention required", /* 27 SIGIOINT */
|
428
|
3696 #ifdef AIXHFT
|
771
|
3697 "HFT grant", /* 28 SIGGRANT */
|
|
3698 "HFT retract", /* 29 SIGRETRACT */
|
|
3699 "HFT sound done", /* 30 SIGSOUND */
|
|
3700 "HFT input ready", /* 31 SIGMSG */
|
428
|
3701 #endif
|
771
|
3702 /* $$####end-snarf */
|
428
|
3703 0
|
|
3704 };
|
|
3705 #else /* USG, not AIX */
|
442
|
3706 const char *sys_siglist[NSIG + 1] =
|
428
|
3707 {
|
771
|
3708 /* $$####begin-snarf */
|
|
3709 "bogus signal", /* 0 */
|
|
3710 "hangup", /* 1 SIGHUP */
|
|
3711 "interrupt", /* 2 SIGINT */
|
|
3712 "quit", /* 3 SIGQUIT */
|
|
3713 "illegal instruction", /* 4 SIGILL */
|
|
3714 "trace trap", /* 5 SIGTRAP */
|
|
3715 "IOT instruction", /* 6 SIGIOT */
|
|
3716 "EMT instruction", /* 7 SIGEMT */
|
|
3717 "floating point exception", /* 8 SIGFPE */
|
|
3718 "kill", /* 9 SIGKILL */
|
|
3719 "bus error", /* 10 SIGBUS */
|
|
3720 "segmentation violation", /* 11 SIGSEGV */
|
|
3721 "bad argument to system call", /* 12 SIGSYS */
|
|
3722 "write on a pipe with no one to read it", /* 13 SIGPIPE */
|
|
3723 "alarm clock", /* 14 SIGALRM */
|
|
3724 "software termination signum", /* 15 SIGTERM */
|
|
3725 "user defined signal 1", /* 16 SIGUSR1 */
|
|
3726 "user defined signal 2", /* 17 SIGUSR2 */
|
|
3727 "death of a child", /* 18 SIGCLD */
|
|
3728 "power-fail restart", /* 19 SIGPWR */
|
428
|
3729 #ifdef sun
|
771
|
3730 "window size changed", /* 20 SIGWINCH */
|
|
3731 "urgent socket condition", /* 21 SIGURG */
|
|
3732 "pollable event occurred", /* 22 SIGPOLL */
|
|
3733 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */
|
|
3734 "user stop requested from tty", /* 24 SIGTSTP */
|
|
3735 "stopped process has been continued", /* 25 SIGCONT */
|
|
3736 "background tty read attempted", /* 26 SIGTTIN */
|
|
3737 "background tty write attempted", /* 27 SIGTTOU */
|
|
3738 "virtual timer expired", /* 28 SIGVTALRM */
|
|
3739 "profiling timer expired", /* 29 SIGPROF */
|
|
3740 "exceeded cpu limit", /* 30 SIGXCPU */
|
|
3741 "exceeded file size limit", /* 31 SIGXFSZ */
|
|
3742 "process's lwps are blocked", /* 32 SIGWAITING */
|
|
3743 "special signal used by thread library", /* 33 SIGLWP */
|
428
|
3744 #ifdef SIGFREEZE
|
771
|
3745 "special signal used by CPR", /* 34 SIGFREEZE */
|
428
|
3746 #endif
|
|
3747 #ifdef SIGTHAW
|
771
|
3748 "special signal used by CPR", /* 35 SIGTHAW */
|
428
|
3749 #endif
|
|
3750 #endif /* sun */
|
771
|
3751 /* $$####end-snarf */
|
428
|
3752 0
|
|
3753 };
|
|
3754 #endif /* not AIX */
|
|
3755 #endif /* USG */
|
|
3756 #ifdef DGUX
|
442
|
3757 const char *sys_siglist[NSIG + 1] =
|
428
|
3758 {
|
771
|
3759 /* $$####begin-snarf */
|
|
3760 "null signal", /* 0 SIGNULL */
|
|
3761 "hangup", /* 1 SIGHUP */
|
|
3762 "interrupt", /* 2 SIGINT */
|
|
3763 "quit", /* 3 SIGQUIT */
|
|
3764 "illegal instruction", /* 4 SIGILL */
|
|
3765 "trace trap", /* 5 SIGTRAP */
|
|
3766 "abort termination", /* 6 SIGABRT */
|
|
3767 "SIGEMT", /* 7 SIGEMT */
|
|
3768 "floating point exception", /* 8 SIGFPE */
|
|
3769 "kill", /* 9 SIGKILL */
|
|
3770 "bus error", /* 10 SIGBUS */
|
|
3771 "segmentation violation", /* 11 SIGSEGV */
|
|
3772 "bad argument to system call", /* 12 SIGSYS */
|
|
3773 "write on a pipe with no reader", /* 13 SIGPIPE */
|
|
3774 "alarm clock", /* 14 SIGALRM */
|
|
3775 "software termination signal", /* 15 SIGTERM */
|
|
3776 "user defined signal 1", /* 16 SIGUSR1 */
|
|
3777 "user defined signal 2", /* 17 SIGUSR2 */
|
|
3778 "child stopped or terminated", /* 18 SIGCLD */
|
|
3779 "power-fail restart", /* 19 SIGPWR */
|
|
3780 "window size changed", /* 20 SIGWINCH */
|
|
3781 "undefined", /* 21 */
|
|
3782 "pollable event occurred", /* 22 SIGPOLL */
|
|
3783 "sendable stop signal not from tty", /* 23 SIGSTOP */
|
|
3784 "stop signal from tty", /* 24 SIGSTP */
|
|
3785 "continue a stopped process", /* 25 SIGCONT */
|
|
3786 "attempted background tty read", /* 26 SIGTTIN */
|
|
3787 "attempted background tty write", /* 27 SIGTTOU */
|
|
3788 "undefined", /* 28 */
|
|
3789 "undefined", /* 29 */
|
|
3790 "undefined", /* 30 */
|
|
3791 "undefined", /* 31 */
|
|
3792 "undefined", /* 32 */
|
|
3793 "socket (TCP/IP) urgent data arrival", /* 33 SIGURG */
|
|
3794 "I/O is possible", /* 34 SIGIO */
|
|
3795 "exceeded cpu time limit", /* 35 SIGXCPU */
|
|
3796 "exceeded file size limit", /* 36 SIGXFSZ */
|
|
3797 "virtual time alarm", /* 37 SIGVTALRM */
|
|
3798 "profiling time alarm", /* 38 SIGPROF */
|
|
3799 "undefined", /* 39 */
|
|
3800 "file record locks revoked", /* 40 SIGLOST */
|
|
3801 "undefined", /* 41 */
|
|
3802 "undefined", /* 42 */
|
|
3803 "undefined", /* 43 */
|
|
3804 "undefined", /* 44 */
|
|
3805 "undefined", /* 45 */
|
|
3806 "undefined", /* 46 */
|
|
3807 "undefined", /* 47 */
|
|
3808 "undefined", /* 48 */
|
|
3809 "undefined", /* 49 */
|
|
3810 "undefined", /* 50 */
|
|
3811 "undefined", /* 51 */
|
|
3812 "undefined", /* 52 */
|
|
3813 "undefined", /* 53 */
|
|
3814 "undefined", /* 54 */
|
|
3815 "undefined", /* 55 */
|
|
3816 "undefined", /* 56 */
|
|
3817 "undefined", /* 57 */
|
|
3818 "undefined", /* 58 */
|
|
3819 "undefined", /* 59 */
|
|
3820 "undefined", /* 60 */
|
|
3821 "undefined", /* 61 */
|
|
3822 "undefined", /* 62 */
|
|
3823 "undefined", /* 63 */
|
|
3824 "notification message in mess. queue", /* 64 SIGDGNOTIFY */
|
|
3825 /* $$####end-snarf */
|
428
|
3826 0
|
|
3827 };
|
|
3828 #endif /* DGUX */
|
|
3829
|
|
3830 #endif /* ! SYS_SIGLIST_DECLARED && ! HAVE_SYS_SIGLIST */
|
|
3831
|
|
3832
|
|
3833 /************************************************************************/
|
|
3834 /* Directory routines for systems that don't have them */
|
|
3835 /************************************************************************/
|
|
3836
|
|
3837 #ifdef SYSV_SYSTEM_DIR
|
|
3838
|
|
3839 #include <dirent.h>
|
|
3840
|
|
3841 #if defined(BROKEN_CLOSEDIR) || !defined(HAVE_CLOSEDIR)
|
|
3842 int
|
|
3843 closedir (DIR *dirp) /* stream from opendir */
|
|
3844 {
|
|
3845 int rtnval;
|
|
3846
|
771
|
3847 rtnval = retry_close (dirp->dd_fd);
|
428
|
3848
|
|
3849 /* Some systems (like Solaris) allocate the buffer and the DIR all
|
|
3850 in one block. Why in the world are we freeing this ourselves
|
|
3851 anyway? */
|
|
3852 #if ! (defined (sun) && defined (USG5_4))
|
|
3853 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
|
|
3854 #endif
|
|
3855 xfree ((char *) dirp);
|
|
3856 return (rtnval);
|
|
3857 }
|
|
3858 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
|
|
3859 #endif /* SYSV_SYSTEM_DIR */
|
|
3860
|
|
3861 #ifdef NONSYSTEM_DIR_LIBRARY
|
|
3862
|
|
3863 DIR *
|
442
|
3864 opendir (const char *filename) /* name of directory */
|
428
|
3865 {
|
|
3866 DIR *dirp; /* -> malloc'ed storage */
|
|
3867 int fd; /* file descriptor for read */
|
|
3868 struct stat sbuf; /* result of fstat */
|
|
3869
|
771
|
3870 fd = open (filename, O_RDONLY);
|
428
|
3871 if (fd < 0)
|
|
3872 return 0;
|
|
3873
|
|
3874 if (fstat (fd, &sbuf) < 0
|
|
3875 || (sbuf.st_mode & S_IFMT) != S_IFDIR
|
|
3876 || (dirp = (DIR *) malloc (sizeof (DIR))) == 0)
|
|
3877 {
|
771
|
3878 retry_close (fd);
|
428
|
3879 return 0; /* bad luck today */
|
|
3880 }
|
|
3881
|
|
3882 dirp->dd_fd = fd;
|
|
3883 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
|
|
3884
|
|
3885 return dirp;
|
|
3886 }
|
|
3887
|
|
3888 void
|
|
3889 closedir (DIR *dirp) /* stream from opendir */
|
|
3890 {
|
771
|
3891 retry_close (dirp->dd_fd);
|
428
|
3892 xfree (dirp);
|
|
3893 }
|
|
3894
|
|
3895
|
|
3896 #define DIRSIZ 14
|
|
3897 struct olddir
|
|
3898 {
|
|
3899 ino_t od_ino; /* inode */
|
|
3900 char od_name[DIRSIZ]; /* filename */
|
|
3901 };
|
|
3902
|
|
3903 static struct direct dir_static; /* simulated directory contents */
|
|
3904
|
|
3905 /* ARGUSED */
|
|
3906 struct direct *
|
|
3907 readdir (DIR *dirp) /* stream from opendir */
|
|
3908 {
|
|
3909 struct olddir *dp; /* -> directory data */
|
|
3910
|
|
3911 for (; ;)
|
|
3912 {
|
|
3913 if (dirp->dd_loc >= dirp->dd_size)
|
|
3914 dirp->dd_loc = dirp->dd_size = 0;
|
|
3915
|
|
3916 if (dirp->dd_size == 0 /* refill buffer */
|
771
|
3917 && (dirp->dd_size =
|
|
3918 retry_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
|
428
|
3919 return 0;
|
|
3920
|
|
3921 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
|
|
3922 dirp->dd_loc += sizeof (struct olddir);
|
|
3923
|
|
3924 if (dp->od_ino != 0) /* not deleted entry */
|
|
3925 {
|
|
3926 dir_static.d_ino = dp->od_ino;
|
|
3927 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
|
|
3928 dir_static.d_name[DIRSIZ] = '\0';
|
|
3929 dir_static.d_namlen = strlen (dir_static.d_name);
|
|
3930 dir_static.d_reclen = sizeof (struct direct)
|
|
3931 - MAXNAMLEN + 3
|
|
3932 + dir_static.d_namlen - dir_static.d_namlen % 4;
|
|
3933 return &dir_static; /* -> simulated structure */
|
|
3934 }
|
|
3935 }
|
|
3936 }
|
|
3937
|
|
3938
|
|
3939 #endif /* NONSYSTEM_DIR_LIBRARY */
|
|
3940
|
|
3941
|
|
3942 /* mkdir and rmdir functions, for systems which don't have them. */
|
|
3943
|
|
3944 #ifndef HAVE_MKDIR
|
|
3945 /*
|
|
3946 * Written by Robert Rother, Mariah Corporation, August 1985.
|
|
3947 *
|
|
3948 * If you want it, it's yours. All I ask in return is that if you
|
|
3949 * figure out how to do this in a Bourne Shell script you send me
|
|
3950 * a copy.
|
|
3951 * sdcsvax!rmr or rmr@uscd
|
|
3952 *
|
|
3953 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
|
|
3954 * subroutine. 11Mar86; hoptoad!gnu
|
|
3955 *
|
|
3956 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
|
|
3957 * subroutine didn't return EEXIST. It does now.
|
|
3958 */
|
|
3959
|
|
3960 /*
|
|
3961 * Make a directory.
|
|
3962 */
|
|
3963 #ifdef MKDIR_PROTOTYPE
|
|
3964 MKDIR_PROTOTYPE
|
|
3965 #else
|
|
3966 int
|
442
|
3967 mkdir (const char *dpath, int dmode)
|
428
|
3968 #endif
|
|
3969 {
|
|
3970 int cpid, status, fd;
|
|
3971 struct stat statbuf;
|
|
3972
|
442
|
3973 if (stat (dpath, &statbuf) == 0) /* we do want stat() here */
|
428
|
3974 {
|
|
3975 errno = EEXIST; /* Stat worked, so it already exists */
|
|
3976 return -1;
|
|
3977 }
|
|
3978
|
|
3979 /* If stat fails for a reason other than non-existence, return error */
|
|
3980 if (errno != ENOENT)
|
|
3981 return -1;
|
|
3982
|
|
3983 synch_process_alive = 1;
|
|
3984 switch (cpid = fork ())
|
|
3985 {
|
|
3986
|
|
3987 case -1: /* Error in fork() */
|
|
3988 return -1; /* Errno is set already */
|
|
3989
|
|
3990 case 0: /* Child process */
|
|
3991 {
|
|
3992 /*
|
|
3993 * Cheap hack to set mode of new directory. Since this
|
|
3994 * child process is going away anyway, we zap its umask.
|
|
3995 * ####, this won't suffice to set SUID, SGID, etc. on this
|
|
3996 * directory. Does anybody care?
|
|
3997 */
|
|
3998 status = umask (0); /* Get current umask */
|
|
3999 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
|
771
|
4000 fd = open ("/dev/null", O_RDWR);
|
428
|
4001 if (fd >= 0)
|
|
4002 {
|
|
4003 if (fd != STDIN_FILENO) dup2 (fd, STDIN_FILENO);
|
|
4004 if (fd != STDOUT_FILENO) dup2 (fd, STDOUT_FILENO);
|
|
4005 if (fd != STDERR_FILENO) dup2 (fd, STDERR_FILENO);
|
|
4006 }
|
|
4007 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
|
|
4008 _exit (-1); /* Can't exec /bin/mkdir */
|
|
4009 }
|
|
4010
|
|
4011 default: /* Parent process */
|
|
4012 wait_for_termination (cpid);
|
|
4013 }
|
|
4014
|
|
4015 if (synch_process_death != 0 || synch_process_retcode != 0)
|
|
4016 {
|
|
4017 errno = EIO; /* We don't know why, but */
|
|
4018 return -1; /* /bin/mkdir failed */
|
|
4019 }
|
|
4020
|
|
4021 return 0;
|
|
4022 }
|
|
4023 #endif /* not HAVE_MKDIR */
|
|
4024
|
|
4025 #ifndef HAVE_RMDIR
|
|
4026 int
|
442
|
4027 rmdir (const char *dpath)
|
428
|
4028 {
|
|
4029 int cpid, status, fd;
|
|
4030 struct stat statbuf;
|
|
4031
|
442
|
4032 if (stat (dpath, &statbuf) != 0) /* we do want stat() here */
|
428
|
4033 {
|
|
4034 /* Stat just set errno. We don't have to */
|
|
4035 return -1;
|
|
4036 }
|
|
4037
|
|
4038 synch_process_alive = 1;
|
|
4039 switch (cpid = fork ())
|
|
4040 {
|
|
4041
|
|
4042 case -1: /* Error in fork() */
|
|
4043 return (-1); /* Errno is set already */
|
|
4044
|
|
4045 case 0: /* Child process */
|
771
|
4046 fd = open ("/dev/null", O_RDWR);
|
428
|
4047 if (fd >= 0)
|
|
4048 {
|
|
4049 if (fd != STDIN_FILENO) dup2 (fd, STDIN_FILENO);
|
|
4050 if (fd != STDOUT_FILENO) dup2 (fd, STDOUT_FILENO);
|
|
4051 if (fd != STDERR_FILENO) dup2 (fd, STDERR_FILENO);
|
|
4052 }
|
|
4053 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
|
|
4054 _exit (-1); /* Can't exec /bin/mkdir */
|
|
4055
|
|
4056 default: /* Parent process */
|
|
4057 wait_for_termination (cpid);
|
|
4058 }
|
|
4059
|
|
4060 if (synch_process_death != 0 ||
|
|
4061 synch_process_retcode != 0)
|
|
4062 {
|
|
4063 errno = EIO; /* We don't know why, but */
|
|
4064 return -1; /* /bin/rmdir failed */
|
|
4065 }
|
|
4066
|
|
4067 return 0;
|
|
4068 }
|
|
4069 #endif /* !HAVE_RMDIR */
|
|
4070
|
|
4071
|
|
4072 /************************************************************************/
|
|
4073 /* Misc. SunOS crap */
|
|
4074 /************************************************************************/
|
|
4075
|
|
4076 #ifdef USE_DL_STUBS
|
|
4077
|
|
4078 /* These are included on Sunos 4.1 when we do not use shared libraries.
|
|
4079 X11 libraries may refer to these functions but (we hope) do not
|
|
4080 actually call them. */
|
|
4081
|
|
4082 void *
|
|
4083 dlopen (void)
|
|
4084 {
|
|
4085 return 0;
|
|
4086 }
|
|
4087
|
|
4088 void *
|
|
4089 dlsym (void)
|
|
4090 {
|
|
4091 return 0;
|
|
4092 }
|
|
4093
|
|
4094 int
|
|
4095 dlclose (void)
|
|
4096 {
|
|
4097 return -1;
|
|
4098 }
|
|
4099
|
|
4100 #endif /* USE_DL_STUBS */
|