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