home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d191 / ispell.lha / ISpell / src.zoo / term.c < prev    next >
C/C++ Source or Header  |  1989-02-22  |  5KB  |  264 lines

  1. /* -*- Mode:Text -*- */
  2.  
  3. /*
  4.  * term.c - deal with termcap, and unix terminal mode settings
  5.  *
  6.  * Pace Willisson, 1983
  7.  */
  8.  
  9. #include <stdio.h>
  10. #ifdef USG
  11. #include <termio.h>
  12. #else
  13. #include <sgtty.h>
  14. #endif
  15. #include <signal.h>
  16. #include "config.h"
  17. #include "ispell.h"
  18.  
  19. int putch();
  20.  
  21. erase ()
  22. {
  23.     if (cl)
  24.         tputs(cl, li, putch);
  25.     else {
  26.         if (ho)
  27.             tputs(ho, 100, putch);
  28.         else if (cm)
  29.             tputs(tgoto(cm, 0, 0), 100, putch);
  30.         tputs(cd, li, putch);
  31.     }
  32. }
  33.  
  34. move (row, col)
  35. {
  36.     tputs (tgoto (cm, col, row), 100, putch);
  37. }
  38.  
  39. inverse ()
  40. {
  41.     tputs (so, 10, putch);
  42. }
  43.  
  44. normal ()
  45. {
  46.     tputs (se, 10, putch);
  47. }
  48.  
  49. backup ()
  50. {
  51.     if (BC)
  52.         tputs (BC, 1, putch);
  53.     else
  54.         putchar ('\b');
  55. }
  56.  
  57. putch (c)
  58. {
  59.     putchar (c);
  60. }
  61.  
  62. #ifdef USG
  63. struct termio sbuf, osbuf;
  64. #else
  65. struct sgttyb sbuf, osbuf;
  66. struct ltchars ltc, oltc;
  67. #endif
  68. static termchanged = 0;
  69. static int (*oldint) ();
  70. static int (*oldterm) ();
  71. #ifdef SIGTTIN
  72. static int (*oldttin) ();
  73. static int (*oldttou) ();
  74. static int (*oldtstp) ();
  75. #endif
  76.  
  77. terminit ()
  78. {
  79.     int done();
  80.  
  81. #ifdef USG
  82.     if (!isatty(0)) {
  83.         fprintf (stderr, "Can't deal with non interactive use yet.\n");
  84.         exit (1);
  85.     }
  86.     ioctl (0, TCGETA, &osbuf);
  87.     termchanged = 1;
  88.  
  89.     sbuf = osbuf;
  90.     sbuf.c_lflag &= ~(ECHO | ECHOK | ECHONL | ICANON);
  91.     sbuf.c_oflag &= ~(OPOST);
  92.     sbuf.c_iflag &= ~(INLCR | IGNCR | ICRNL);
  93.     sbuf.c_cc[VMIN] = 1;
  94.     sbuf.c_cc[VTIME] = 1;
  95.     ioctl (0, TCSETAW, &sbuf);
  96.  
  97.     erasechar = osbuf.c_cc[VERASE];
  98.     killchar = osbuf.c_cc[VKILL];
  99.  
  100. #else
  101.     int tpgrp;
  102.     int onstop();
  103.     extern short ospeed;
  104.  
  105. retry:
  106. #ifdef SIGTSTP
  107.     sigsetmask(1<<(SIGTSTP-1) | 1<<(SIGTTIN-1) | 1<<(SIGTTOU-1));
  108. #endif
  109. #ifdef TIOCGPGRP
  110.     if (ioctl(0, TIOCGPGRP, &tpgrp) != 0) {
  111.         fprintf (stderr, "Can't deal with non interactive use yet.\n");
  112.         exit (1);
  113.     }
  114. #endif
  115. #ifdef SIGTSTP
  116.     if (tpgrp != getpgrp(0)) { /* not in foreground */
  117.         sigsetmask(1<<(SIGTSTP-1) | 1<<(SIGTTIN-1));
  118.         signal(SIGTTOU, SIG_DFL);
  119.         kill(0, SIGTTOU);
  120.         /* job stops here waiting for SIGCONT */
  121.         goto retry;
  122.     }
  123. #endif
  124.  
  125.     ioctl (0, TIOCGETP, &osbuf);
  126.     ioctl (0, TIOCGLTC, &oltc);
  127.     termchanged = 1;
  128.  
  129.     sbuf = osbuf;
  130.     sbuf.sg_flags &= ~ECHO;
  131.     sbuf.sg_flags |= TERM_MODE;
  132.     ioctl (0, TIOCSETP, &sbuf);
  133.  
  134.     erasechar = sbuf.sg_erase;
  135.     killchar = sbuf.sg_kill;
  136.     ospeed = sbuf.sg_ospeed;
  137.  
  138.     ltc = oltc;
  139.     ltc.t_suspc = -1;
  140.     ioctl (0, TIOCSLTC, <c);
  141.  
  142.     if ((oldint = signal (SIGINT, SIG_IGN)) != SIG_IGN)
  143.         signal (SIGINT, done);
  144.     if ((oldterm = signal (SIGTERM, SIG_IGN)) != SIG_IGN)
  145.         signal (SIGTERM, done);
  146.  
  147. #ifdef SIGTTIN
  148.     sigsetmask(0);
  149.     if (signal (SIGTTIN, SIG_IGN) != SIG_IGN)
  150.         signal(SIGTTIN, onstop);
  151.     if (signal (SIGTTOU, SIG_IGN) != SIG_IGN)
  152.         signal(SIGTTOU, onstop);
  153.     if (signal (SIGTSTP, SIG_IGN) != SIG_IGN)
  154.         signal(SIGTSTP, onstop);
  155. #endif
  156. #endif
  157.  
  158.     tgetent(termcap, getenv("TERM"));
  159.     termptr = termstr;
  160.     bs = tgetflag("bs");
  161.     BC = tgetstr("bc", &termptr);
  162.     UP = tgetstr("up", &termptr);
  163.     cd = tgetstr("cd", &termptr);
  164.     ce = tgetstr("ce", &termptr);    
  165.     cl = tgetstr("cl", &termptr);
  166.     cm = tgetstr("cm", &termptr);
  167.     dc = tgetstr("dc", &termptr);
  168.     dl = tgetstr("dl", &termptr);
  169.     dm = tgetstr("dm", &termptr);
  170.     ed = tgetstr("ed", &termptr);
  171.     ei = tgetstr("ei", &termptr);
  172.     ho = tgetstr("ho", &termptr);
  173.     ic = tgetstr("ic", &termptr);
  174.     il = tgetstr("al", &termptr);
  175.     im = tgetstr("im", &termptr);
  176.     ip = tgetstr("ip", &termptr);
  177.     nd = tgetstr("nd", &termptr);
  178.     vb = tgetstr("vb", &termptr);
  179.     so = tgetstr("so", &termptr);    /* inverse video on */
  180.     se = tgetstr("se", &termptr);    /* inverse video off */
  181.     co = tgetnum("co");
  182.     li = tgetnum("li");    
  183.  
  184. }
  185.  
  186. done ()
  187. {
  188.     unlink (tempfile);
  189.     if (termchanged)
  190. #ifdef USG
  191.         ioctl (0, TCSETAW, &osbuf);
  192. #else
  193.         ioctl (0, TIOCSETP, &osbuf);
  194.         ioctl (0, TIOCSLTC, &oltc);
  195. #endif
  196.     exit (0);
  197. }
  198.  
  199. #ifndef USG
  200. onstop(signo)
  201. int signo;
  202. {
  203.     ioctl (0, TIOCSETP, &osbuf);
  204.     ioctl (0, TIOCSLTC, &oltc);
  205.     signal(signo, SIG_DFL);
  206.     sigsetmask(sigblock(0) & ~(1 << (signo-1)));
  207.     kill(0, signo);
  208.     /* stop here until continued */
  209.     signal(signo, onstop);
  210.     ioctl (0, TIOCSETP, &sbuf);
  211.     ioctl (0, TIOCSLTC, <c);
  212. }
  213.  
  214. stop ()
  215. {
  216. #ifdef SIGTSTP
  217.     onstop (SIGTSTP);
  218. #endif
  219.     ;
  220. }
  221. #endif
  222.  
  223. shellescape (buf)
  224. char *buf;
  225. {
  226.  
  227. #ifdef USG
  228.     ioctl (0, TCSETAW, &osbuf);
  229. #else
  230.     ioctl (0, TIOCSETP, &osbuf);
  231.     ioctl (0, TIOCSLTC, &oltc);
  232. #endif
  233.     signal (SIGINT, oldint);
  234.     signal (SIGTERM, oldterm);
  235. #ifdef SIGTTIN
  236.     oldttin = signal(SIGTTIN, SIG_DFL);
  237.     oldttou = signal(SIGTTOU, SIG_DFL);
  238.     oldtstp = signal(SIGTSTP, SIG_DFL);
  239. #endif
  240.  
  241.     system (buf);
  242.  
  243.     if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  244.         signal (SIGINT, done);
  245.     if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  246.         signal (SIGTERM, done);
  247.  
  248. #ifdef SIGTTIN
  249.     signal(SIGTTIN, oldttin);
  250.     signal(SIGTTOU, oldttou);
  251.     signal(SIGTSTP, oldtstp);
  252. #endif
  253.  
  254. #ifdef USG
  255.     ioctl (0, TCSETAW, &sbuf);
  256. #else
  257.     ioctl (0, TIOCSETP, &sbuf);
  258.     ioctl (0, TIOCSLTC, <c);
  259. #endif
  260.     printf ("\n-- Type space to continue --");
  261.     fflush (stdout);
  262.     getchar ();
  263. }
  264.