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