1 $NetBSD: patch-aj,v 1.11 2012/03/01 22:00:44 hans Exp $
3 --- fep_main.c.orig 1993-06-10 02:53:06.000000000 +0000
4 +++ fep_main.c 2011-09-29 20:16:05.000000000 +0000
11 #include <sys/types.h>
13 #include <sys/ioctl.h>
14 -#include <sys/file.h>
20 +#include <sys/ttold.h>
22 +#ifndef _POSIX_VDISABLE
23 +#define _POSIX_VDISABLE '\0'
27 +#include <sys/file.h>
30 #include <sys/resource.h>
31 -#include <sys/errno.h>
36 @@ -30,7 +42,7 @@ static char rcsid[]=
38 static char fep_statrc[] = FEP_STAT;
43 char *myself; /* the command name */
44 char *prompt = ""; /* prompt string */
45 @@ -38,9 +50,9 @@ char *delimiters = DEFAULT_DELIMITERS;
46 /* delimiter characters */
47 int master; /* file discriptor for pty master */
48 int slave; /* file discriptor for pty slave */
49 -int mastermask; /* 1<<master */
50 -int stdinmask; /* 1<<fileno(stdin) */
51 -int selectmask; /* stdinmask | mastermask */
52 +fd_set mastermask; /* 1<<master */
53 +fd_set stdinmask; /* 1<<fileno(stdin) */
54 +fd_set selectmask; /* stdinmask | mastermask */
55 int selectnfds; /* max (fileno(stdin), master) + 1*/
56 int child_pid; /* child pid */
57 int ptyflag = ON; /* flag to use pty or not */
58 @@ -48,27 +60,34 @@ int histlen = -1; /* history length */
59 int debug = OFF; /* debug switch */
60 int auto_tty_fix = ON; /* fix tty mode automaticaly */
61 FILE *script_fp = NULL; /* script file pointer */
62 -int catchsig(); /* function take care SIGCHILD */
63 +void catchsig (int); /* function take care SIGCHILD */
65 -struct sgttyb initial_ttymode; /* initial tty mode */
66 -struct sgttyb master_ttymode; /* master tty mode */
67 -struct sgttyb slave_ttymode; /* slave tty mode */
69 +#define ttystruct termios
70 +#elif defined(TIOCSETN)
71 +#define ttystruct sgttyb
73 +struct ttystruct initial_ttymode; /* initial tty mode */
74 +struct ttystruct master_ttymode; /* master tty mode */
75 +struct ttystruct slave_ttymode; /* slave tty mode */
77 int lines; /* terminal line size */
78 int columns; /* terminal coulumn size */
79 char *term_clear; /* terminal clear code */
81 -void (*sighup)(), (*sigchld)(), (*sigtstp)();
82 +void (*sighup)(int), (*sigchld)(int), (*sigtstp)(int);
83 /* function buffer for signal */
86 struct tchars tchars_buf; /* tty characters */
87 struct ltchars ltchars_buf; /* tty characters */
89 int lmode_buf; /* local mode */
90 int line_desc; /* line descipline */
92 struct jtchars jtchars_buf; /* kanji tty characters */
93 int kmode_buf; /* kanji mode */
97 char master_tty[16]; /* master tty name */
98 char slave_tty[16]; /* slave tty name */
99 @@ -112,6 +131,10 @@ main(argc, argv)
103 + setvbuf(stdin, NULL, _IONBF, 0);
104 + setvbuf(stdout, NULL, _IONBF, 0);
105 + setvbuf(stderr, NULL, _IONBF, 0);
108 * Initialize binding table
110 @@ -172,7 +195,7 @@ main(argc, argv)
111 histlen = atoi (argv[1]);
114 - histlen = atoi (&argv[1] + 2);
115 + histlen = atoi (argv[1] + 2);
119 @@ -276,7 +299,7 @@ DEFAULT:
124 + void sigwinch(int);
127 sighup = signal (SIGHUP, terminate);
128 @@ -298,7 +321,7 @@ recover_signal ()
136 * Get slave tty descriptor for auto-tty-fix
137 @@ -306,7 +329,7 @@ input_handler()
138 if ((slave = open (slave_tty, O_RDONLY)) < 0)
141 - while (inputline = getline ()) {
142 + while (inputline = get_line ()) {
144 * XXX: nbyte should be greater than 0 only for ^@ input in emacs.
145 * This solution is very ugly.. but it will takes a half day
146 @@ -322,7 +345,7 @@ input_handler()
150 - * Saving command line to output buffer is done in getline().
151 + * Saving command line to output buffer is done in get_line().
152 * Because inputline here is converted by alias.
155 @@ -336,7 +359,7 @@ input_handler()
156 if (Through == OFF && Transparency == OFF && script_fp)
157 fwrite (inputline, sizeof(CHAR), strlen (inputline), script_fp);
163 #define INPUT_BUFFER_SIZE 1024
164 @@ -384,21 +407,19 @@ getcharacter()
168 - int nfound, readfd, writefd = 0, execptfd = 0;
170 + fd_set readfd, writefd, exceptfd;
172 struct timeval **timeout = auto_tty_fix ? timeout_list : notimeout;
174 struct timeval **timeout = notimeout;
180 * Sorry, this cording depends to an implementation of getc().
182 -# define CHAR_IN_BUFFER (stdin->_cnt)
183 - if (CHAR_IN_BUFFER)
186 + FD_ZERO(&exceptfd);
190 @@ -409,13 +430,13 @@ RETRY:
191 while ((nfound = select (selectnfds, &readfd, 0, 0, *timeout)) < 0)
192 if (errno != EINTR) {
199 * Found output from pty.
201 - if (readfd & mastermask) {
202 + if (FD_ISSET(master, &readfd)) {
206 @@ -451,7 +472,7 @@ RETRY:
208 * Found input from terminal
210 - if (CHAR_IN_BUFFER || readfd & stdinmask) {
211 + if (FD_ISSET(fileno(stdin), &readfd)) {
215 @@ -467,8 +488,8 @@ RETRY:
217 if ((c = getc (stdin)) == EOF) {
219 - printf ("EOF chatched\n");
221 + printf ("EOF catched\n");
225 return (c & CHARMASK);
226 @@ -504,7 +525,7 @@ int set_buffer (bp, size)
230 - char *newbuf, *malloc(), *realloc();
234 newbuf = (char *) realloc (bp->b_buf, size);
235 @@ -596,7 +617,7 @@ buf_put (bp, s)
239 - int readfd = mastermask;
240 + fd_set readfd = mastermask;
244 @@ -604,7 +625,7 @@ swallow_output()
247 select (selectnfds, &readfd, 0, 0, TIMEOUT_NOBLOCK) > 0 &&
248 - readfd & mastermask
249 + FD_ISSET(master, &mastermask)
251 nbyte = buf_read (master, output_buffer);
253 @@ -636,21 +657,23 @@ swallow_output()
254 #include <sys/m_wait.h>
266 if (wait3 (&status, WNOHANG | WUNTRACED, &ru) != child_pid)
268 if (WIFSTOPPED (status) /* || WIFSIGNALED (status) */) {
270 - message ("Child has sttoped!!\n");
271 + message ("Child has stopped!!\n");
277 + terminate (WEXITSTATUS(status));
280 exec_to_command(argv)
281 @@ -675,13 +698,37 @@ exec_to_command(argv)
283 (void) close (slave);
286 + tcsetattr(0, TCSANOW, &slave_ttymode);
287 +#elif defined(TIOCSETN)
288 ioctl (0, TIOCSETN, (char *) & slave_ttymode);
291 execvp (*argv, argv, 0);
300 + master_ttymode = initial_ttymode;
301 + slave_ttymode = initial_ttymode;
302 + master_ttymode.c_lflag &= ~(ECHO|ECHOE|ECHOK|ICANON);
304 + for (i = 0; i < NCCS; i++)
305 + master_ttymode.c_cc[i] = _POSIX_VDISABLE;
307 + master_ttymode.c_cc[VMIN] = 1;
308 + master_ttymode.c_cc[VTIME] = 0;
309 + slave_ttymode.c_lflag &= ~(ECHO|ECHOE|ECHOK);
310 + slave_ttymode.c_iflag &= ~(ICRNL);
311 + slave_ttymode.c_oflag &= ~(ONLCR);
312 + tcsetattr(0, TCSANOW, &master_ttymode);
315 +#elif defined(TIOCSETN)
320 @@ -719,6 +766,7 @@ fix_tty()
321 ioctl (0, TIOCSETC, (char *) & tcbuf);
322 ioctl (0, TIOCSLTC, (char *) & lcbuf);
328 @@ -727,9 +775,10 @@ kill_process()
329 (void) killpg (child_pid, SIGTERM);
340 * Save history if 'history-file' is set
341 @@ -759,10 +808,14 @@ terminate()
342 if (killpg (child_pid, SIGKILL) < 0)
346 + tcsetattr(0, TCSANOW, &initial_ttymode);
347 +#elif defined(TIOCSETN)
348 ioctl (0, TIOCSETN, (char *) & initial_ttymode);
349 ioctl (0, TIOCSETC, (char *) & tchars_buf);
350 ioctl (0, TIOCSLTC, (char *) & ltchars_buf);
357 @@ -775,6 +828,41 @@ get_pty_master()
362 + if ((master = open("/dev/ptmx", O_RDWR)) == -1) {
363 + perror ("Couldn't open pseudo tty");
367 + if (grantpt (master) == -1) {
368 + perror ("grantpt");
372 + if (unlockpt (master) == -1) {
373 + perror ("grantpt");
379 + if (ptsname_r (master, slave_tty, sizeof(slave_tty)) == -1) {
380 + perror ("ptsname_r");
386 + if ((ptr = ptsname (master)) == NULL) {
387 + perror ("ptsname");
391 + (void)strncpy (slave_tty, ptr, sizeof(slave_tty));
392 + slave_tty[sizeof(slave_tty) - 1] = '\0';
396 for (c = 'p'; c <= 's'; c++) {
397 for (i = 0; i < 16; i++) {
398 sprintf (master_tty, "/dev/pty%c%x", c, i);
399 @@ -796,11 +884,16 @@ get_pty_master()
405 + tcgetattr(0, &initial_ttymode);
406 +#elif defined(TIOCSETN)
407 ioctl (0, TIOCGETP, (char *) &initial_ttymode);
408 ioctl (0, TIOCGETC, (char *) &tchars_buf);
409 ioctl (0, TIOCGETD, (char *) &line_desc);
410 ioctl (0, TIOCGLTC, (char *) <chars_buf);
411 ioctl (0, TIOCLGET, (char *) &lmode_buf);
416 @@ -820,11 +913,15 @@ get_pty_master()
417 # if defined(TIOCKGETC) && defined(TIOCKSETC)
418 ioctl (0, TIOCKGETC, (char *) &jtchars_buf);
423 - stdinmask = 1 << fileno (stdin);
424 - mastermask = 1 << master;
425 - selectmask = stdinmask | mastermask;
426 + FD_ZERO(&stdinmask);
427 + FD_ZERO(&mastermask);
428 + FD_ZERO(&selectmask);
429 + FD_SET(fileno(stdin), &stdinmask);
430 + FD_SET(master, &mastermask);
431 + FD_SET(fileno(stdin), &selectmask);
432 + FD_SET(master, &selectmask);
433 selectnfds = max (fileno(stdin), master) + 1;
436 @@ -838,11 +935,20 @@ get_pty_slave()
440 + if (setsid() == -1)
442 + if (ioctl (slave, TIOCSCTTY, 1) == -1)
446 + tcsetattr(slave, TCSANOW, &initial_ttymode);
447 +#elif defined(TIOCSETN)
448 ioctl (slave, TIOCSETN, (char *) &initial_ttymode);
449 ioctl (slave, TIOCSETC, (char *) &tchars_buf);
450 ioctl (slave, TIOCSLTC, (char *) <chars_buf);
451 ioctl (slave, TIOCLSET, (char *) &lmode_buf);
452 ioctl (slave, TIOCSETD, (char *) &line_desc);
456 # if defined(TIOCKGET) && defined(TIOCKSET)
457 @@ -851,7 +957,7 @@ get_pty_slave()
458 # if defined(TIOCKGETC) && defined(TIOCKSETC)
459 ioctl (slave, TIOCKSETC, (char *) &jtchars_buf);
466 @@ -866,37 +972,40 @@ get_pty_slave()
472 + tcsetattr(0, TCSANOW, &initial_ttymode);
473 +#elif defined(TIOCSETN)
474 ioctl (0, TIOCSETN, (char *) & initial_ttymode);
475 ioctl (0, TIOCSETC, (char *) & tchars_buf);
476 ioctl (0, TIOCSLTC, (char *) & ltchars_buf);
486 + void (*func) (int);
487 + sigset_t set, oset;
490 /* reset signal handler so kill below stops us */
491 func = signal (SIGCHLD, SIG_IGN);
492 signal (SIGTSTP, SIG_DFL);
494 -#define mask(s) (1 << ((s)-1))
495 - omask = sigsetmask (sigblock (0) & ~mask (SIGTSTP));
497 + sigaddset(&set, SIGTSTP);
498 + sigprocmask (SIG_BLOCK, &set, &oset);
501 if (kill (child_pid, SIGCONT) < 0 && errno == ESRCH) {
502 printf ("Where my child has gone?!\n");
506 killpg (child_pid, SIGCONT);
508 signal (SIGCHLD, func);
509 signal (SIGTSTP, SIG_IGN);
510 - sigblock (mask (SIGTSTP));
511 + sigprocmask (SIG_BLOCK, &set, &oset);
513 if (look_var ("auto-repaint"))
515 @@ -933,7 +1042,7 @@ usageAndExit()
517 * Propagate window size changes to the slave tty.
520 +void sigwinch(int num)
522 #ifdef TIOCGWINSZ /* 4.3BSD */