Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / misc / fep / patches / patch-aj
blob48c6d032c8a56f873b042f73a3fd8b97987b21b6
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
5 @@ -6,15 +6,27 @@
6 -#endif lint
7 +#endif /* lint */
8  
9  #include <stdio.h>
10  #include <signal.h>
11  #include <sys/types.h>
12  #include <sys/stat.h>
13  #include <sys/ioctl.h>
14 -#include <sys/file.h>
15 +#ifdef TERMIOS
16 +#include <stdlib.h>
17 +#include <fcntl.h>
18 +#include <termios.h>
19 +#ifdef __sun
20 +#include <sys/ttold.h>
21 +#endif
22 +#ifndef _POSIX_VDISABLE
23 +#define _POSIX_VDISABLE '\0'
24 +#endif
25 +#else
26  #include <sgtty.h>
27 +#include <sys/file.h>
28 +#endif
29  #include <sys/time.h>
30  #include <sys/resource.h>
31 -#include <sys/errno.h>
32 +#include <errno.h>
34  #include "fep_defs.h"
35  #include "fep_glob.h"
36 @@ -30,7 +42,7 @@ static char rcsid[]=
37  #ifdef STAT
38    static char fep_statrc[] = FEP_STAT;
39  #endif
40 -#endif lint
41 +#endif /* lint */
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 */
68 +#ifdef TERMIOS
69 +#define ttystruct termios
70 +#elif defined(TIOCSETN)
71 +#define ttystruct sgttyb
72 +#endif
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 */
85 +#ifdef TIOCSETN
86  struct tchars tchars_buf;              /* tty characters */
87  struct ltchars ltchars_buf;            /* tty characters */
88 +#endif
89  int    lmode_buf;                      /* local mode */
90  int    line_desc;                      /* line descipline */
91  #ifdef KANJI
92  struct jtchars jtchars_buf;            /* kanji tty characters */
93  int    kmode_buf;                      /* kanji mode */
94 -#endif KANJI
95 +#endif /* KANJI */
97  char   master_tty[16];                 /* master tty name */
98  char   slave_tty[16];                  /* slave tty name */
99 @@ -112,6 +131,10 @@ main(argc, argv)
101      myself = argv[0];
103 +    setvbuf(stdin, NULL, _IONBF, 0);
104 +    setvbuf(stdout, NULL, _IONBF, 0);
105 +    setvbuf(stderr, NULL, _IONBF, 0);
107      /*
108       * Initialize binding table
109       */
110 @@ -172,7 +195,7 @@ main(argc, argv)
111                     histlen = atoi (argv[1]);
112                 }
113                 else {
114 -                   histlen = atoi (&argv[1] + 2);
115 +                   histlen = atoi (argv[1] + 2);
116                 }
117                 break;
119 @@ -276,7 +299,7 @@ DEFAULT:
120  fix_signal ()
122  #ifdef SIGWINCH
123 -    int sigwinch();
124 +    void sigwinch(int);
125  #endif
127      sighup = signal (SIGHUP, terminate);
128 @@ -298,7 +321,7 @@ recover_signal ()
129  input_handler()
131      char   *inputline;
132 -    char   *getline ();
133 +    char   *get_line ();
135      /*
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)
139         perror ("open");
141 -    while (inputline = getline ()) {
142 +    while (inputline = get_line ()) {
143         /*
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()
148         /*
149          * NOTE:
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.
153          */
155 @@ -336,7 +359,7 @@ input_handler()
156         if (Through == OFF && Transparency == OFF && script_fp)
157             fwrite (inputline, sizeof(CHAR), strlen (inputline), script_fp);
158      }
159 -    terminate ();
160 +    terminate (0);
163  #define INPUT_BUFFER_SIZE 1024
164 @@ -384,21 +407,19 @@ getcharacter()
166      char c;
167      int n;
168 -    int nfound, readfd, writefd = 0, execptfd = 0;
169 +    int nfound;
170 +    fd_set readfd, writefd, exceptfd;
171  #ifdef USE_TIMEOUT
172      struct timeval **timeout = auto_tty_fix ? timeout_list : notimeout;
173  #else
174      struct timeval **timeout = notimeout;
175  #endif
176 -    extern int errno;
179      /*
180       * Sorry, this cording depends to an implementation of getc().
181       */
182 -#   define CHAR_IN_BUFFER (stdin->_cnt)
183 -    if (CHAR_IN_BUFFER)
184 -       goto RETURNCHAR;
185 +    FD_ZERO(&writefd);
186 +    FD_ZERO(&exceptfd);
188  RETRY:
189      readfd = selectmask;
190 @@ -409,13 +430,13 @@ RETRY:
191      while ((nfound = select (selectnfds, &readfd, 0, 0, *timeout)) < 0)
192         if (errno != EINTR) {
193                 perror ("select");
194 -               terminate();
195 +               terminate(1);
196         }
198      /*
199       * Found output from pty.
200       */
201 -    if (readfd & mastermask) {
202 +    if (FD_ISSET(master, &readfd)) {
203         int nbyte;
205         /*
206 @@ -451,7 +472,7 @@ RETRY:
207      /*
208       * Found input from terminal
209       */
210 -    if (CHAR_IN_BUFFER || readfd & stdinmask) {
211 +    if (FD_ISSET(fileno(stdin), &readfd)) {
213  #ifndef USE_TIMEOUT
214         /*
215 @@ -467,8 +488,8 @@ RETRY:
216         RETURNCHAR:
217         if ((c = getc (stdin)) == EOF) {
218             if (debug)
219 -               printf ("EOF chatched\n");
220 -           terminate ();
221 +               printf ("EOF catched\n");
222 +           terminate (1);
223         }
224         else
225             return (c & CHARMASK);
226 @@ -504,7 +525,7 @@ int set_buffer (bp, size)
227      BUFFER *bp;
228      int size;
230 -    char *newbuf, *malloc(), *realloc();
231 +    char *newbuf;
233      if (bp->b_buf)
234         newbuf = (char *) realloc (bp->b_buf, size);
235 @@ -596,7 +617,7 @@ buf_put (bp, s)
237  swallow_output()
239 -    int readfd = mastermask;
240 +    fd_set readfd = mastermask;
241      int r;
242      int nbyte;
243      int ncount = 10;
244 @@ -604,7 +625,7 @@ swallow_output()
245      while (
246         ncount-- &&
247         select (selectnfds, &readfd, 0, 0, TIMEOUT_NOBLOCK) > 0 &&
248 -       readfd & mastermask
249 +       FD_ISSET(master, &mastermask)
250      ) {
251         nbyte = buf_read (master, output_buffer);
252         if (nbyte > 0) {
253 @@ -636,21 +657,23 @@ swallow_output()
254  #include <sys/m_wait.h>
255  #endif
257 -catchsig()
258 +void
259 +catchsig(n)
260 +    int n;
262 -    union wait status;
263 +    int status;
264      struct rusage   ru;
266      if (wait3 (&status, WNOHANG | WUNTRACED, &ru) != child_pid)
267         return;
268      if (WIFSTOPPED (status) /* || WIFSIGNALED (status) */) {
269         if (debug) {
270 -           message ("Child has sttoped!!\n");
271 +           message ("Child has stopped!!\n");
272         }
273         suspend ();
274         return;
275      }
276 -    terminate ();
277 +    terminate (WEXITSTATUS(status));
280  exec_to_command(argv)
281 @@ -675,13 +698,37 @@ exec_to_command(argv)
282      dup2 (slave, 2);
283      (void) close (slave);
285 +#ifdef TERMIOS
286 +    tcsetattr(0, TCSANOW, &slave_ttymode);
287 +#elif defined(TIOCSETN)
288      ioctl (0, TIOCSETN, (char *) & slave_ttymode);
290 +#endif
291      execvp (*argv, argv, 0);
292      perror (*argv);
293      exit (1);
296 +#ifdef TERMIOS
297 +fix_tty()
299 +    int i;
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)
317  fix_tty()
319      struct tchars tcbuf;
320 @@ -719,6 +766,7 @@ fix_tty()
321      ioctl (0, TIOCSETC, (char *) & tcbuf);
322      ioctl (0, TIOCSLTC, (char *) & lcbuf);
324 +#endif
326  kill_process()
328 @@ -727,9 +775,10 @@ kill_process()
329         (void) killpg (child_pid, SIGTERM);
332 -terminate()
333 +void
334 +terminate(n)
335 +    int n;
337 -    extern int errno;
339      /*
340       * Save history if 'history-file' is set
341 @@ -759,10 +808,14 @@ terminate()
342             if (killpg (child_pid, SIGKILL) < 0)
343                 perror ("kill");
345 +#ifdef TERMIOS
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);
351 -    exit (0);
352 +#endif
353 +    exit (n);
356  get_pty_master()
357 @@ -775,6 +828,41 @@ get_pty_master()
358         master = 1;
359         return;
360      }
361 +#ifdef HAVE_PTMX
362 +    if ((master = open("/dev/ptmx", O_RDWR)) == -1) {
363 +       perror ("Couldn't open pseudo tty");
364 +       kill_process ();
365 +       exit (1);
366 +    }
367 +    if (grantpt (master) == -1) {
368 +       perror ("grantpt");
369 +       kill_process ();
370 +       exit (1);
371 +    }
372 +    if (unlockpt (master) == -1) {
373 +       perror ("grantpt");
374 +       kill_process ();
375 +       exit (1);
376 +    }
377 +    {
378 +#ifdef __linux__
379 +       if (ptsname_r (master, slave_tty, sizeof(slave_tty)) == -1) {
380 +           perror ("ptsname_r");
381 +           kill_process ();
382 +           exit (1);
383 +       }
384 +#else
385 +       char *ptr;
386 +       if ((ptr = ptsname (master)) == NULL) {
387 +           perror ("ptsname");
388 +           kill_process ();
389 +           exit (1);
390 +       }
391 +       (void)strncpy (slave_tty, ptr, sizeof(slave_tty));
392 +       slave_tty[sizeof(slave_tty) - 1] = '\0';
393 +#endif
394 +    }
395 +#else
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()
400      }
402   FOUND:
403 +#endif
404 +#ifdef TERMIOS
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 *) &ltchars_buf);
411      ioctl (0, TIOCLGET, (char *) &lmode_buf);
412 +#endif
414  #ifdef TIOCGWINSZ
415      {
416 @@ -820,11 +913,15 @@ get_pty_master()
417  # if defined(TIOCKGETC) && defined(TIOCKSETC)
418      ioctl (0, TIOCKGETC, (char *) &jtchars_buf);
419  # endif
420 -#endif KANJI
421 +#endif /* KANJI */
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;
435      return;
436 @@ -838,11 +935,20 @@ get_pty_slave()
437         perror (slave_tty);
438         exit (1);
439      }
440 +    if (setsid() == -1)
441 +       perror ("setsid");
442 +    if (ioctl (slave, TIOCSCTTY, 1) == -1)
443 +       perror ("ioctl");
445 +#ifdef TERMIOS
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 *) &ltchars_buf);
451      ioctl (slave, TIOCLSET, (char *) &lmode_buf);
452      ioctl (slave, TIOCSETD, (char *) &line_desc);
453 +#endif
455  #ifdef KANJI
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);
460  # endif
461 -#endif KANJI
462 +#endif /* KANJI */
464  #ifdef TIOCSWINSZ
465      {
466 @@ -866,37 +972,40 @@ get_pty_slave()
468  recover_tty()
471 +#ifdef TERMIOS
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);
477 +#endif
480  suspend()
482      long       pid;
483 -    void       (*func) ();
484 -    int                omask;
485 -    extern int errno;
486 +    void       (*func) (int);
487 +    sigset_t    set, oset;
489      pid = getpid ();
490   /* reset signal handler so kill below stops us */
491      func = signal (SIGCHLD, SIG_IGN);
492      signal (SIGTSTP, SIG_DFL);
493      recover_tty();
494 -#define        mask(s) (1 << ((s)-1))
495 -    omask = sigsetmask (sigblock (0) & ~mask (SIGTSTP));
496 +    sigemptyset(&set);
497 +    sigaddset(&set, SIGTSTP);
498 +    sigprocmask (SIG_BLOCK, &set, &oset);
499      kill (0, SIGTSTP);
501      if (kill (child_pid, SIGCONT) < 0 && errno == ESRCH) {
502         printf ("Where my child has gone?!\n");
503 -       terminate ();
504 +       terminate (1);
505      }
506      killpg (child_pid, SIGCONT);
507      kill (0, SIGCONT);
508      signal (SIGCHLD, func);
509      signal (SIGTSTP, SIG_IGN);
510 -    sigblock (mask (SIGTSTP));
511 +    sigprocmask (SIG_BLOCK, &set, &oset);
512      fix_tty ();
513      if (look_var ("auto-repaint"))
514         fep_repaint(0);
515 @@ -933,7 +1042,7 @@ usageAndExit()
516  /*
517   * Propagate window size changes to the slave tty.
518   */
519 -sigwinch()
520 +void sigwinch(int num)
522  #ifdef TIOCGWINSZ                      /* 4.3BSD */
523      struct winsize win;