sysdep.c 89.3 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Interfaces to system-dependent kernel and library entries.
2
   Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
Glenn Morris's avatar
Glenn Morris committed
3
                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
Glenn Morris's avatar
Glenn Morris committed
4
                 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
9
it under the terms of the GNU General Public License as published by
10 11
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
12 13 14 15 16 17 18

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
20

Pavel Janík's avatar
Pavel Janík committed
21 22 23
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
Jim Blandy's avatar
Jim Blandy committed
24

25
#include <ctype.h>
Jim Blandy's avatar
Jim Blandy committed
26
#include <signal.h>
27
#include <stdio.h>
Jim Blandy's avatar
Jim Blandy committed
28
#include <setjmp.h>
29 30 31 32 33 34 35
#ifdef HAVE_PWD_H
#include <pwd.h>
#include <grp.h>
#endif /* HAVE_PWD_H */
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif /* HAVE_LIMITS_H */
36 37 38
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
Dan Nicolaescu's avatar
Dan Nicolaescu committed
39

Jim Blandy's avatar
Jim Blandy committed
40
#include "lisp.h"
41 42 43
/* Including stdlib.h isn't necessarily enough to get srandom
   declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2.  */

44 45 46
/* The w32 build defines select stuff in w32.h, which is included by
   sys/select.h (included below).   */
#ifndef WINDOWSNT
47
#include "sysselect.h"
48
#endif
49

50
#include "blockinput.h"
Jim Blandy's avatar
Jim Blandy committed
51

52
#ifdef WINDOWSNT
53 54
#define read sys_read
#define write sys_write
55
#include <windows.h>
56 57 58
#ifndef NULL
#define NULL 0
#endif
59 60
#endif /* not WINDOWSNT */

Jim Blandy's avatar
Jim Blandy committed
61 62 63 64
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

65
#ifdef HAVE_SETPGID
Dan Nicolaescu's avatar
Dan Nicolaescu committed
66
#if !defined (USG)
67
#undef setpgrp
68 69
#define setpgrp setpgid
#endif
70
#endif
71

72 73 74 75 76
/* Get SI_SRPC_DOMAIN, if it is available.  */
#ifdef HAVE_SYS_SYSTEMINFO_H
#include <sys/systeminfo.h>
#endif

77 78 79 80 81
#ifdef MSDOS	/* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
#include <dos.h>
#include "dosfns.h"
#include "msdos.h"
#include <sys/param.h>
82 83 84 85

extern int etext;
extern unsigned start __asm__ ("start");
#endif
86

87
#include <sys/file.h>
88

89
#ifdef HAVE_FCNTL_H
90 91
#include <fcntl.h>
#endif
Jim Blandy's avatar
Jim Blandy committed
92

93
#ifndef MSDOS
Jim Blandy's avatar
Jim Blandy committed
94
#include <sys/ioctl.h>
95
#endif
96

97
#include "systty.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
98
#include "syswait.h"
Jim Blandy's avatar
Jim Blandy committed
99

Dan Nicolaescu's avatar
Dan Nicolaescu committed
100
#if defined (USG)
Jim Blandy's avatar
Jim Blandy committed
101 102
#include <sys/utsname.h>
#include <memory.h>
Dan Nicolaescu's avatar
Dan Nicolaescu committed
103
#endif /* USG */
Jim Blandy's avatar
Jim Blandy committed
104 105 106

extern int quit_char;

107
#include "keyboard.h"
Jim Blandy's avatar
Jim Blandy committed
108
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
109 110 111 112 113 114
#include "window.h"
#include "termhooks.h"
#include "termchar.h"
#include "termopts.h"
#include "dispextern.h"
#include "process.h"
Karoly Lorentey's avatar
Karoly Lorentey committed
115
#include "cm.h"  /* for reset_sys_modes */
Jim Blandy's avatar
Jim Blandy committed
116

Glenn Morris's avatar
Glenn Morris committed
117
/* For serial_configure and serial_open.  */
118 119 120 121
extern Lisp_Object QCport, QCspeed, QCprocess;
extern Lisp_Object QCbytesize, QCstopbits, QCparity, Qodd, Qeven;
extern Lisp_Object QCflowcontrol, Qhw, Qsw, QCsummary;

122 123 124 125
#ifdef WINDOWSNT
#include <direct.h>
/* In process.h which conflicts with the local copy.  */
#define _P_WAIT 0
126 127
int _cdecl _spawnlp (int, const char *, const char *, ...);
int _cdecl _getpid (void);
128
extern char *getwd (char *);
129 130
#endif

Jim Blandy's avatar
Jim Blandy committed
131 132
#include "syssignal.h"
#include "systime.h"
133 134 135 136 137 138 139 140 141 142 143 144 145 146
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif

#ifndef HAVE_UTIMES
#ifndef HAVE_STRUCT_UTIMBUF
/* We want to use utime rather than utimes, but we couldn't find the
   structure declaration.  We'll use the traditional one.  */
struct utimbuf {
  long actime;
  long modtime;
};
#endif
#endif
Jim Blandy's avatar
Jim Blandy committed
147

148 149 150 151 152
/* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits.  */
#ifndef LPASS8
#define LPASS8 0
#endif

153
static const int baud_convert[] =
Jim Blandy's avatar
Jim Blandy committed
154 155 156 157 158
  {
    0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
    1800, 2400, 4800, 9600, 19200, 38400
  };

159 160 161
#ifdef HAVE_SPEED_T
#include <termios.h>
#else
162 163
#if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
#else
164
#if defined (HAVE_TERMIOS_H) && defined (GNU_LINUX)
Richard M. Stallman's avatar
Richard M. Stallman committed
165
#include <termios.h>
166
#endif
167
#endif
168
#endif
Jim Blandy's avatar
Jim Blandy committed
169

Gerd Moellmann's avatar
Gerd Moellmann committed
170 171
int emacs_ospeed;

172
void croak P_ ((char *)) NO_RETURN;
173

174 175 176 177
/* Temporary used by `sigblock' when defined in terms of signprocmask.  */

SIGMASKTYPE sigprocmask_set;

178

179
#if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
180

181
/* Return the current working directory.  Returns NULL on errors.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
   Any other returned value must be freed with free. This is used
   only when get_current_dir_name is not defined on the system.  */
char*
get_current_dir_name ()
{
  char *buf;
  char *pwd;
  struct stat dotstat, pwdstat;
  /* If PWD is accurate, use it instead of calling getwd.  PWD is
     sometimes a nicer name, and using it may avoid a fatal error if a
     parent directory is searchable but not readable.  */
    if ((pwd = getenv ("PWD")) != 0
      && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
      && stat (pwd, &pwdstat) == 0
      && stat (".", &dotstat) == 0
      && dotstat.st_ino == pwdstat.st_ino
      && dotstat.st_dev == pwdstat.st_dev
#ifdef MAXPATHLEN
      && strlen (pwd) < MAXPATHLEN
#endif
      )
    {
      buf = (char *) malloc (strlen (pwd) + 1);
      if (!buf)
        return NULL;
      strcpy (buf, pwd);
    }
#ifdef HAVE_GETCWD
  else
    {
      size_t buf_size = 1024;
213
      buf = (char *) malloc (buf_size);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
      if (!buf)
        return NULL;
      for (;;)
        {
          if (getcwd (buf, buf_size) == buf)
            break;
          if (errno != ERANGE)
            {
              int tmp_errno = errno;
              free (buf);
              errno = tmp_errno;
              return NULL;
            }
          buf_size *= 2;
          buf = (char *) realloc (buf, buf_size);
          if (!buf)
            return NULL;
        }
    }
#else
  else
    {
      /* We need MAXPATHLEN here.  */
      buf = (char *) malloc (MAXPATHLEN + 1);
      if (!buf)
        return NULL;
      if (getwd (buf) == NULL)
        {
          int tmp_errno = errno;
          free (buf);
          errno = tmp_errno;
          return NULL;
        }
    }
#endif
  return buf;
}
#endif

253

254
/* Discard pending input on all input descriptors.  */
Jim Blandy's avatar
Jim Blandy committed
255

256
void
Jim Blandy's avatar
Jim Blandy committed
257 258
discard_tty_input ()
{
259
#ifndef WINDOWSNT
Jim Blandy's avatar
Jim Blandy committed
260
  struct emacs_tty buf;
Jim Blandy's avatar
Jim Blandy committed
261 262 263 264

  if (noninteractive)
    return;

265
#ifdef MSDOS    /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
266
  while (dos_keyread () != -1)
267
    ;
268
#else /* not MSDOS */
269
  {
270
    struct tty_display_info *tty;
271 272
    for (tty = tty_list; tty; tty = tty->next)
      {
273 274 275 276 277
        if (tty->input)         /* Is the device suspended? */
          {
            EMACS_GET_TTY (fileno (tty->input), &buf);
            EMACS_SET_TTY (fileno (tty->input), &buf, 0);
          }
278 279
      }
  }
280
#endif /* not MSDOS */
281
#endif /* not WINDOWSNT */
Jim Blandy's avatar
Jim Blandy committed
282 283
}

284

Jim Blandy's avatar
Jim Blandy committed
285 286
#ifdef SIGTSTP

287
/* Arrange for character C to be read as the next input from
288 289 290
   the terminal.
   XXX What if we have multiple ttys?
*/
291

Andreas Schwab's avatar
Andreas Schwab committed
292
void
293
stuff_char (char c)
Jim Blandy's avatar
Jim Blandy committed
294
{
295
  if (! FRAME_TERMCAP_P (SELECTED_FRAME ()))
296 297
    return;

Jim Blandy's avatar
Jim Blandy committed
298 299
/* Should perhaps error if in batch mode */
#ifdef TIOCSTI
300
  ioctl (fileno (CURTTY()->input), TIOCSTI, &c);
Jim Blandy's avatar
Jim Blandy committed
301
#else /* no TIOCSTI */
302
  error ("Cannot stuff terminal input characters in this version of Unix");
Jim Blandy's avatar
Jim Blandy committed
303 304 305 306
#endif /* no TIOCSTI */
}

#endif /* SIGTSTP */
307

308
void
309
init_baud_rate (int fd)
Jim Blandy's avatar
Jim Blandy committed
310 311
{
  if (noninteractive)
Gerd Moellmann's avatar
Gerd Moellmann committed
312
    emacs_ospeed = 0;
Jim Blandy's avatar
Jim Blandy committed
313 314
  else
    {
315
#ifdef DOS_NT
Gerd Moellmann's avatar
Gerd Moellmann committed
316
    emacs_ospeed = 15;
317
#else  /* not DOS_NT */
318 319
#ifdef HAVE_TERMIOS
      struct termios sg;
Jim Blandy's avatar
Jim Blandy committed
320

321
      sg.c_cflag = B9600;
322
      tcgetattr (fd, &sg);
Gerd Moellmann's avatar
Gerd Moellmann committed
323
      emacs_ospeed = cfgetospeed (&sg);
Dan Nicolaescu's avatar
Dan Nicolaescu committed
324
#else /* not TERMIOS */
325 326
#ifdef HAVE_TERMIO
      struct termio sg;
Jim Blandy's avatar
Jim Blandy committed
327

328
      sg.c_cflag = B9600;
329
#ifdef HAVE_TCATTR
330
      tcgetattr (fd, &sg);
331
#else
332
      ioctl (fd, TCGETA, &sg);
333
#endif
Gerd Moellmann's avatar
Gerd Moellmann committed
334
      emacs_ospeed = sg.c_cflag & CBAUD;
Dan Nicolaescu's avatar
Dan Nicolaescu committed
335
#else /* neither TERMIOS nor TERMIO */
Jim Blandy's avatar
Jim Blandy committed
336
      struct sgttyb sg;
337

Jim Blandy's avatar
Jim Blandy committed
338
      sg.sg_ospeed = B9600;
339
      if (ioctl (fd, TIOCGETP, &sg) < 0)
340
	abort ();
Gerd Moellmann's avatar
Gerd Moellmann committed
341
      emacs_ospeed = sg.sg_ospeed;
Jim Blandy's avatar
Jim Blandy committed
342
#endif /* not HAVE_TERMIO */
343
#endif /* not HAVE_TERMIOS */
344
#endif /* not DOS_NT */
Jim Blandy's avatar
Jim Blandy committed
345
    }
346

Gerd Moellmann's avatar
Gerd Moellmann committed
347 348
  baud_rate = (emacs_ospeed < sizeof baud_convert / sizeof baud_convert[0]
	       ? baud_convert[emacs_ospeed] : 9600);
Jim Blandy's avatar
Jim Blandy committed
349 350 351 352
  if (baud_rate == 0)
    baud_rate = 1200;
}

353

Jim Blandy's avatar
Jim Blandy committed
354
/*ARGSUSED*/
Andreas Schwab's avatar
Andreas Schwab committed
355
void
Jim Blandy's avatar
Jim Blandy committed
356 357 358 359 360 361 362 363
set_exclusive_use (fd)
     int fd;
{
#ifdef FIOCLEX
  ioctl (fd, FIOCLEX, 0);
#endif
  /* Ok to do nothing if this feature does not exist */
}
364

Jim Blandy's avatar
Jim Blandy committed
365 366 367 368 369 370 371 372 373 374 375
#ifndef subprocesses

wait_without_blocking ()
{
  croak ("wait_without_blocking");
  synch_process_alive = 0;
}

#endif /* not subprocesses */

int wait_debugging;   /* Set nonzero to make following function work under dbx
376
			 (at least for bsd).  */
Jim Blandy's avatar
Jim Blandy committed
377 378 379 380 381 382 383 384

SIGTYPE
wait_for_termination_signal ()
{}

/* Wait for subprocess with process id `pid' to terminate and
   make sure it will get eliminated (not remain forever as a zombie) */

385
void
Jim Blandy's avatar
Jim Blandy committed
386 387 388 389 390 391
wait_for_termination (pid)
     int pid;
{
  while (1)
    {
#ifdef subprocesses
Dan Nicolaescu's avatar
Dan Nicolaescu committed
392
#if defined (BSD_SYSTEM) || defined (HPUX)
393 394 395 396 397 398 399 400 401
      /* Note that kill returns -1 even if the process is just a zombie now.
	 But inevitably a SIGCHLD interrupt should be generated
	 and child_sig will do wait3 and make the process go away. */
      /* There is some indication that there is a bug involved with
	 termination of subprocesses, perhaps involving a kernel bug too,
	 but no idea what it is.  Just as a hunch we signal SIGCHLD to see
	 if that causes the problem to go away or get worse.  */
      sigsetmask (sigmask (SIGCHLD));
      if (0 > kill (pid, 0))
402
	{
403 404 405 406 407 408 409 410
	  sigsetmask (SIGEMPTYMASK);
	  kill (getpid (), SIGCHLD);
	  break;
	}
      if (wait_debugging)
	sleep (1);
      else
	sigpause (SIGEMPTYMASK);
411
#else /* not BSD_SYSTEM, and not HPUX version >= 6 */
412 413 414 415
#ifdef WINDOWSNT
      wait (0);
      break;
#else /* not WINDOWSNT */
416
      sigblock (sigmask (SIGCHLD));
417 418
      errno = 0;
      if (kill (pid, 0) == -1 && errno == ESRCH)
419 420 421 422
	{
	  sigunblock (sigmask (SIGCHLD));
	  break;
	}
423

424
      sigsuspend (&empty_mask);
425
#endif /* not WINDOWSNT */
426
#endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
Jim Blandy's avatar
Jim Blandy committed
427
#else /* not subprocesses */
428
      break;
Jim Blandy's avatar
Jim Blandy committed
429 430 431 432 433 434 435 436 437 438
#endif /* not subprocesses */
    }
}

#ifdef subprocesses

/*
 *	flush any pending output
 *      (may flush input as well; it does not matter the way we use it)
 */
439

440
void
Jim Blandy's avatar
Jim Blandy committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
flush_pending_output (channel)
     int channel;
{
#ifdef HAVE_TERMIOS
  /* If we try this, we get hit with SIGTTIN, because
     the child's tty belongs to the child's pgrp. */
#else
#ifdef TCFLSH
  ioctl (channel, TCFLSH, 1);
#else
#ifdef TIOCFLUSH
  int zero = 0;
  /* 3rd arg should be ignored
     but some 4.2 kernels actually want the address of an int
     and nonzero means something different.  */
  ioctl (channel, TIOCFLUSH, &zero);
#endif
#endif
#endif
}
461

Jim Blandy's avatar
Jim Blandy committed
462 463 464 465 466
/*  Set up the terminal at the other end of a pseudo-terminal that
    we will be controlling an inferior through.
    It should not echo or do line-editing, since that is done
    in Emacs.  No padding needed for insertion into an Emacs buffer.  */

467
void
Jim Blandy's avatar
Jim Blandy committed
468 469 470
child_setup_tty (out)
     int out;
{
471
#ifndef DOS_NT
Jim Blandy's avatar
Jim Blandy committed
472 473 474
  struct emacs_tty s;

  EMACS_GET_TTY (out, &s);
Jim Blandy's avatar
Jim Blandy committed
475

476
#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
Jim Blandy's avatar
Jim Blandy committed
477 478
  s.main.c_oflag |= OPOST;	/* Enable output postprocessing */
  s.main.c_oflag &= ~ONLCR;	/* Disable map of NL to CR-NL on output */
479
#ifdef NLDLY
480 481 482
  /* http://lists.gnu.org/archive/html/emacs-devel/2008-05/msg00406.html
     Some versions of GNU Hurd do not have FFDLY?  */
#ifdef FFDLY
Jim Blandy's avatar
Jim Blandy committed
483 484
  s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
  				/* No output delays */
485 486 487 488
#else
  s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY);
  				/* No output delays */
#endif
489
#endif
Jim Blandy's avatar
Jim Blandy committed
490 491
  s.main.c_lflag &= ~ECHO;	/* Disable echo */
  s.main.c_lflag |= ISIG;	/* Enable signals */
492 493 494
#ifdef IUCLC
  s.main.c_iflag &= ~IUCLC;	/* Disable downcasing on input.  */
#endif
495 496 497
#ifdef ISTRIP
  s.main.c_iflag &= ~ISTRIP;	/* don't strip 8th bit on input */
#endif
498
#ifdef OLCUC
499 500
  s.main.c_oflag &= ~OLCUC;	/* Disable upcasing on output.  */
#endif
501
  s.main.c_oflag &= ~TAB3;	/* Disable tab expansion */
502
  s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
503 504
  s.main.c_cc[VERASE] = CDISABLE;	/* disable erase processing */
  s.main.c_cc[VKILL] = CDISABLE;	/* disable kill processing */
Jim Blandy's avatar
Jim Blandy committed
505

Jim Blandy's avatar
Jim Blandy committed
506
#ifdef HPUX
Jim Blandy's avatar
Jim Blandy committed
507
  s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
Jim Blandy's avatar
Jim Blandy committed
508
#endif /* HPUX */
Jim Blandy's avatar
Jim Blandy committed
509

510 511 512
#ifdef SIGNALS_VIA_CHARACTERS
  /* the QUIT and INTR character are used in process_send_signal
     so set them here to something useful.  */
513
  if (s.main.c_cc[VQUIT] == CDISABLE)
514
    s.main.c_cc[VQUIT] = '\\'&037;	/* Control-\ */
515
  if (s.main.c_cc[VINTR] == CDISABLE)
516 517 518
    s.main.c_cc[VINTR] = 'C'&037;	/* Control-C */
#endif /* not SIGNALS_VIA_CHARACTERS */

Jim Blandy's avatar
Jim Blandy committed
519 520 521
#ifdef AIX
  /* Also, PTY overloads NUL and BREAK.
     don't ignore break, but don't signal either, so it looks like NUL.  */
Jim Blandy's avatar
Jim Blandy committed
522 523
  s.main.c_iflag &= ~IGNBRK;
  s.main.c_iflag &= ~BRKINT;
524 525 526
  /* rms: Formerly it set s.main.c_cc[VINTR] to 0377 here
     unconditionally.  Then a SIGNALS_VIA_CHARACTERS conditional
     would force it to 0377.  That looks like duplicated code.  */
Jim Blandy's avatar
Jim Blandy committed
527
  s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
Jim Blandy's avatar
Jim Blandy committed
528 529
#endif /* AIX */

530 531 532 533 534 535 536 537 538 539 540 541
  /* We used to enable ICANON (and set VEOF to 04), but this leads to
     problems where process.c wants to send EOFs every once in a while
     to force the output, which leads to weird effects when the
     subprocess has disabled ICANON and ends up seeing those spurious
     extra EOFs.  So we don't send EOFs any more in
     process.c:send_process, and instead we disable ICANON by default,
     so if a subsprocess sets up ICANON, it's his problem (or the Elisp
     package that talks to it) to deal with lines that are too long.  */
  s.main.c_lflag &= ~ICANON;	/* Disable line editing and eof processing */
  s.main.c_cc[VMIN] = 1;
  s.main.c_cc[VTIME] = 0;

542 543 544 545 546 547 548 549 550
#else /* not HAVE_TERMIO */

  s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
		       | CBREAK | TANDEM);
  s.main.sg_flags |= LPASS8;
  s.main.sg_erase = 0377;
  s.main.sg_kill = 0377;
  s.lmode = LLITOUT | s.lmode;        /* Don't strip 8th bit */

Jim Blandy's avatar
Jim Blandy committed
551 552
#endif /* not HAVE_TERMIO */

Jim Blandy's avatar
Jim Blandy committed
553
  EMACS_SET_TTY (out, &s, 0);
Jim Blandy's avatar
Jim Blandy committed
554

555
#endif /* not DOS_NT */
Jim Blandy's avatar
Jim Blandy committed
556 557 558
}

#endif /* subprocesses */
559

Jim Blandy's avatar
Jim Blandy committed
560 561 562 563
/* Record a signal code and the handler for it.  */
struct save_signal
{
  int code;
Andreas Schwab's avatar
Andreas Schwab committed
564
  SIGTYPE (*handler) P_ ((int));
Jim Blandy's avatar
Jim Blandy committed
565 566
};

Andreas Schwab's avatar
Andreas Schwab committed
567 568 569
static void save_signal_handlers P_ ((struct save_signal *));
static void restore_signal_handlers P_ ((struct save_signal *));

Jim Blandy's avatar
Jim Blandy committed
570 571
/* Suspend the Emacs process; give terminal to its superior.  */

572
void
Jim Blandy's avatar
Jim Blandy committed
573 574
sys_suspend ()
{
575
#if defined (SIGTSTP) && !defined (MSDOS)
Jim Blandy's avatar
Jim Blandy committed
576

577
  {
578
    int pgrp = EMACS_GETPGRP (0);
579 580
    EMACS_KILLPG (pgrp, SIGTSTP);
  }
Jim Blandy's avatar
Jim Blandy committed
581 582 583 584 585

#else /* No SIGTSTP */
/* On a system where suspending is not implemented,
   instead fork a subshell and let it talk directly to the terminal
   while we wait.  */
586 587 588 589 590 591 592
  sys_subshell ();

#endif /* no SIGTSTP */
}

/* Fork a subshell.  */

593
void
594 595
sys_subshell ()
{
596
#ifdef DOS_NT	/* Demacs 1.1.2 91/10/20 Manabu Higashida */
597 598 599
  int st;
  char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS.  */
#endif
600
  int pid;
Jim Blandy's avatar
Jim Blandy committed
601
  struct save_signal saved_handlers[5];
602 603 604
  Lisp_Object dir;
  unsigned char *str = 0;
  int len;
Jim Blandy's avatar
Jim Blandy committed
605 606 607 608 609 610 611 612 613 614 615

  saved_handlers[0].code = SIGINT;
  saved_handlers[1].code = SIGQUIT;
  saved_handlers[2].code = SIGTERM;
#ifdef SIGIO
  saved_handlers[3].code = SIGIO;
  saved_handlers[4].code = 0;
#else
  saved_handlers[3].code = 0;
#endif

616 617 618 619
  /* Mentioning current_buffer->buffer would mean including buffer.h,
     which somehow wedges the hp compiler.  So instead...  */

  dir = intern ("default-directory");
620
  if (NILP (Fboundp (dir)))
621 622
    goto xyzzy;
  dir = Fsymbol_value (dir);
623
  if (!STRINGP (dir))
624 625 626
    goto xyzzy;

  dir = expand_and_dir_to_file (Funhandled_file_name_directory (dir), Qnil);
627 628 629
  str = (unsigned char *) alloca (SCHARS (dir) + 2);
  len = SCHARS (dir);
  bcopy (SDATA (dir), str, len);
630 631 632 633
  if (str[len - 1] != '/') str[len++] = '/';
  str[len] = 0;
 xyzzy:

634
#ifdef DOS_NT
635
  pid = 0;
636 637
  save_signal_handlers (saved_handlers);
  synch_process_alive = 1;
638
#else
639
  pid = vfork ();
Jim Blandy's avatar
Jim Blandy committed
640 641
  if (pid == -1)
    error ("Can't spawn subshell");
642 643
#endif

Jim Blandy's avatar
Jim Blandy committed
644 645
  if (pid == 0)
    {
646
      char *sh = 0;
Jim Blandy's avatar
Jim Blandy committed
647

648
#ifdef DOS_NT    /* MW, Aug 1993 */
649
      getwd (oldwd);
650 651
      if (sh == 0)
	sh = (char *) egetenv ("SUSPEND");	/* KFS, 1994-12-14 */
652
#endif
653 654
      if (sh == 0)
	sh = (char *) egetenv ("SHELL");
Jim Blandy's avatar
Jim Blandy committed
655 656
      if (sh == 0)
	sh = "sh";
657

Jim Blandy's avatar
Jim Blandy committed
658
      /* Use our buffer's default directory for the subshell.  */
659
      if (str)
660
	chdir ((char *) str);
661

Jim Blandy's avatar
Jim Blandy committed
662 663 664
#ifdef subprocesses
      close_process_descs ();	/* Close Emacs's pipes/ptys */
#endif
665

666
#ifdef SET_EMACS_PRIORITY
667
      {
668
	extern EMACS_INT emacs_priority;
669

670
	if (emacs_priority < 0)
671 672 673 674
	  nice (-emacs_priority);
      }
#endif

675
#ifdef MSDOS    /* Demacs 1.1.2 91/10/20 Manabu Higashida */
676
      {
677 678
	char *epwd = getenv ("PWD");
	char old_pwd[MAXPATHLEN+1+4];
679 680

	/* If PWD is set, pass it with corrected value.  */
681
	if (epwd)
682
	  {
683
	    strcpy (old_pwd, epwd);
684 685 686 687 688 689
	    if (str[len - 1] == '/')
	      str[len - 1] = '\0';
	    setenv ("PWD", str, 1);
	  }
	st = system (sh);
	chdir (oldwd);
690
	if (epwd)
691 692
	  putenv (old_pwd);	/* restore previous value */
      }
693
#else /* not MSDOS */
694 695 696
#ifdef  WINDOWSNT
      /* Waits for process completion */
      pid = _spawnlp (_P_WAIT, sh, sh, NULL);
697
      chdir (oldwd);
698 699 700
      if (pid == -1)
	write (1, "Can't execute subshell", 22);
#else   /* not WINDOWSNT */
701
      execlp (sh, sh, (char *) 0);
Jim Blandy's avatar
Jim Blandy committed
702 703
      write (1, "Can't execute subshell", 22);
      _exit (1);
704
#endif  /* not WINDOWSNT */
705
#endif /* not MSDOS */
Jim Blandy's avatar
Jim Blandy committed
706 707
    }

708
  /* Do this now if we did not do it before.  */
709
#ifndef MSDOS
Jim Blandy's avatar
Jim Blandy committed
710
  save_signal_handlers (saved_handlers);
711
  synch_process_alive = 1;
712 713
#endif

714
#ifndef DOS_NT
Jim Blandy's avatar
Jim Blandy committed
715
  wait_for_termination (pid);
716
#endif
Jim Blandy's avatar
Jim Blandy committed
717
  restore_signal_handlers (saved_handlers);
718
  synch_process_alive = 0;
Jim Blandy's avatar
Jim Blandy committed
719 720
}

Andreas Schwab's avatar
Andreas Schwab committed
721
static void
Jim Blandy's avatar
Jim Blandy committed
722 723 724 725 726
save_signal_handlers (saved_handlers)
     struct save_signal *saved_handlers;
{
  while (saved_handlers->code)
    {
Joseph Arceneaux's avatar
Joseph Arceneaux committed
727
      saved_handlers->handler
Andreas Schwab's avatar
Andreas Schwab committed
728
	= (SIGTYPE (*) P_ ((int))) signal (saved_handlers->code, SIG_IGN);
Jim Blandy's avatar
Jim Blandy committed
729 730 731 732
      saved_handlers++;
    }
}

Andreas Schwab's avatar
Andreas Schwab committed
733
static void
Jim Blandy's avatar
Jim Blandy committed
734 735 736 737 738 739 740 741 742 743
restore_signal_handlers (saved_handlers)
     struct save_signal *saved_handlers;
{
  while (saved_handlers->code)
    {
      signal (saved_handlers->code, saved_handlers->handler);
      saved_handlers++;
    }
}

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
#ifndef SIGIO
/* If SIGIO is broken, don't do anything. */
void
init_sigio (int fd)
{
}

void
reset_sigio (int fd)
{
}

void
request_sigio (void)
{
}

void
unrequest_sigio (void)
{
}

#else
Jim Blandy's avatar
Jim Blandy committed
767 768
#ifdef F_SETFL

769
int old_fcntl_flags[MAXDESC];
Jim Blandy's avatar
Jim Blandy committed
770

771
void
772 773
init_sigio (fd)
     int fd;
Jim Blandy's avatar
Jim Blandy committed
774 775
{
#ifdef FASYNC
776 777
  old_fcntl_flags[fd] = fcntl (fd, F_GETFL, 0) & ~FASYNC;
  fcntl (fd, F_SETFL, old_fcntl_flags[fd] | FASYNC);
Jim Blandy's avatar
Jim Blandy committed
778
#endif
779
  interrupts_deferred = 0;
Jim Blandy's avatar
Jim Blandy committed
780 781
}

Andreas Schwab's avatar
Andreas Schwab committed
782
void
Karoly Lorentey's avatar
Karoly Lorentey committed
783 784
reset_sigio (fd)
     int fd;
Jim Blandy's avatar
Jim Blandy committed
785
{
786
#ifdef FASYNC
787
  fcntl (fd, F_SETFL, old_fcntl_flags[fd]);
788
#endif
Jim Blandy's avatar
Jim Blandy committed
789 790
}

791
#ifdef FASYNC		/* F_SETFL does not imply existence of FASYNC */
792
/* XXX Uhm, FASYNC is not used anymore here. */
793
/* XXX Yeah, but you need it for SIGIO, don't you? */
Jim Blandy's avatar
Jim Blandy committed
794

795
void
Jim Blandy's avatar
Jim Blandy committed
796 797
request_sigio ()
{
798
  if (noninteractive)
799 800
    return;

Jim Blandy's avatar
Jim Blandy committed
801
#ifdef SIGWINCH
Jim Blandy's avatar
Jim Blandy committed
802
  sigunblock (sigmask (SIGWINCH));
Jim Blandy's avatar
Jim Blandy committed
803
#endif
Karoly Lorentey's avatar
Karoly Lorentey committed
804
  sigunblock (sigmask (SIGIO));
Jim Blandy's avatar
Jim Blandy committed
805 806 807 808

  interrupts_deferred = 0;
}

809
void
Karoly Lorentey's avatar
Karoly Lorentey committed
810
unrequest_sigio (void)
811
{ 
812 813 814 815 816
  if (noninteractive)
    return;

#if 0 /* XXX What's wrong with blocking SIGIO under X?  */
  if (x_display_list)
817
    return;
818
#endif
819

Jim Blandy's avatar
Jim Blandy committed
820
#ifdef SIGWINCH
Jim Blandy's avatar
Jim Blandy committed
821
  sigblock (sigmask (SIGWINCH));
Jim Blandy's avatar
Jim Blandy committed
822
#endif
Karoly Lorentey's avatar
Karoly Lorentey committed
823
  sigblock (sigmask (SIGIO));
Jim Blandy's avatar
Jim Blandy committed
824 825 826 827
  interrupts_deferred = 1;
}

#else /* no FASYNC */
828
#ifndef MSDOS
829

830
void
Jim Blandy's avatar
Jim Blandy committed
831 832
request_sigio ()
{
833
  if (noninteractive || read_socket_hook)
834 835
    return;

Jim Blandy's avatar
Jim Blandy committed
836 837
  croak ("request_sigio");
}
838

839
void
Jim Blandy's avatar
Jim Blandy committed
840 841
unrequest_sigio ()
{
842
  if (noninteractive || read_socket_hook)
843 844
    return;

Jim Blandy's avatar
Jim Blandy committed
845 846
  croak ("unrequest_sigio");
}
847 848

#endif /* MSDOS */
Jim Blandy's avatar
Jim Blandy committed
849 850
#endif /* FASYNC */
#endif /* F_SETFL */
851
#endif /* SIGIO */
852 853


854 855 856 857 858 859 860 861 862 863 864 865 866
/* Getting and setting emacs_tty structures.  */

/* Set *TC to the parameters associated with the terminal FD.
   Return zero if all's well, or -1 if we ran into an error we
   couldn't deal with.  */
int
emacs_get_tty (fd, settings)
     int fd;
     struct emacs_tty *settings;
{
  /* Retrieve the primary parameters - baud rate, character size, etcetera.  */
#ifdef HAVE_TCATTR
  /* We have those nifty POSIX tcmumbleattr functions.  */
867
  bzero (&settings->main, sizeof (settings->main));
868 869 870 871 872 873 874 875 876 877
  if (tcgetattr (fd, &settings->main) < 0)
    return -1;

#else
#ifdef HAVE_TERMIO
  /* The SYSV-style interface?  */
  if (ioctl (fd, TCGETA, &settings->main) < 0)
    return -1;

#else
878
#ifndef DOS_NT
879 880 881
  /* I give up - I hope you have the BSD ioctls.  */
  if (ioctl (fd, TIOCGETP, &settings->main) < 0)
    return -1;
882
#endif /* not DOS_NT */
883 884 885 886
#endif
#endif

  /* Suivant - Do we have to get struct ltchars data?  */
887
#ifdef HAVE_LTCHARS
888 889 890 891 892
  if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
    return -1;
#endif

  /* How about a struct tchars and a wordful of lmode bits?  */
893
#ifdef HAVE_TCHARS
894 895 896 897 898 899 900 901 902 903 904
  if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
      || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
    return -1;
#endif

  /* We have survived the tempest.  */
  return 0;
}


/* Set the parameters of the tty on FD according to the contents of
905
   *SETTINGS.  If FLUSHP is non-zero, we discard input.
906
   Return 0 if all went well, and -1 if anything failed.  */