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