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