home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / grabchars1.3 / sys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  4.3 KB  |  220 lines

  1. /*
  2. **    $Header: sys.c,v 1.3 88/12/05 19:46:14 daniel grabchars_1_3 $
  3. **
  4. **    sys.c - terminal routines for grabchars
  5. **
  6. **    Dan Smith (daniel@island.uu.net), November 29, 1988
  7. **
  8. **    History:
  9. **
  10. **    December 2, 1988
  11. **    made #ifdefs for DV_ERASE and BSD, wrote notes in handle_erase ()
  12. **    for changes and improvements
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <sgtty.h>
  17. #include <signal.h>
  18. #include "grabchars.h"
  19.  
  20. struct sgttyb orig, new;
  21.  
  22. /* all declared in globals.c */
  23. extern FILE *outfile, *otherout;
  24. extern FLAG *flags;
  25. extern int exit_stat;
  26. extern char default_string[128];
  27.  
  28. #ifdef DV_ERASE
  29. extern char *erase_buf;
  30. #endif
  31.  
  32. /* initialize global flags */
  33. init_flags ()
  34. {
  35.     flags->both = 0;
  36.     flags->check = 0;
  37.     flags->dflt = 0;
  38.     flags->flush = 0;
  39.     flags->ret_key = 0;
  40.     flags->silent = 0;
  41.     flags->erase = 0;
  42. }
  43.  
  44. /*
  45. **    initialize tty
  46. **
  47. **    this really needs SYS V code...any patchers out there?
  48. */
  49. init_term ()
  50. {
  51.     /*    play havoc with the terminal :-) */
  52.  
  53. #ifdef BSD
  54.     ioctl (0, TIOCGETP, &orig);
  55.     new = orig;
  56.     new.sg_flags &= ~ECHO;
  57.     new.sg_flags |= CBREAK;
  58.  
  59.     (flags->flush) ? ioctl (0, TIOCSETP, &new) :    /* to flush... */
  60.         ioctl (0, TIOCSETN, &new);        /* ...or not to flush */
  61. #endif
  62. }
  63.  
  64. /* handle the outside world */
  65. init_signal ()
  66. {
  67.     int lets_go ();
  68.  
  69.     signal (SIGINT, lets_go);
  70.     signal (SIGTSTP, lets_go);
  71.     signal (SIGQUIT, lets_go);
  72. }
  73.  
  74. /*
  75. **    something's up with the user...give a useful exit status so
  76. **    we can ask things like "do you need help?"
  77. */
  78. int overtime ()
  79. {
  80.     int lets_go ();
  81.     void handle_default ();
  82.  
  83.     /* does not return */
  84.     if (exit_stat == -1 && flags->dflt)
  85.         handle_default ();
  86.  
  87.     exit_stat = -2;
  88.     lets_go ();
  89. }
  90.  
  91. /*
  92. **    the default_flag is set, and the user either typed a return
  93. **    or timed out.  This routine does not return.
  94. */
  95. void handle_default ()
  96. {
  97.     int lets_go ();
  98.  
  99.     if (! flags->silent) {
  100.         fputs (default_string, outfile);
  101.         if (flags->both)
  102.             fputs (default_string, otherout);
  103.     }
  104.     exit_stat = strlen (default_string);
  105.     lets_go ();
  106. }
  107.  
  108. /*    clean up and get out of here... */
  109. int lets_go ()
  110. {
  111. #ifdef BSD
  112.     ioctl (0, TIOCSETP, &orig);
  113. #endif
  114.     exit (exit_stat);
  115. }
  116.  
  117. #ifdef DV_ERASE
  118.  
  119. /*
  120. **    December 2, 1988
  121. **    Bucko's (daniel) in progress notes for changing this...
  122. **
  123. **    first time through processing should be called as its' own
  124. **    function from the -E case in the main (getopt ()) switch...
  125. **
  126. **    stdout and stderr should never be affected by any erasures...
  127. **    (they probably are not now, I haven't thoroughly tested this...)
  128. **
  129. **    I need to drag in my word erase routine; never can tell how
  130. **    long some people are going to want their lines with -n! :-)
  131. **
  132. **    If someone wants a control char (via literal (^V)), we should
  133. **    give it to them...this would be more compatible with $< (csh)
  134. **    and read (sh)... grabchars almost completely replaces these
  135. **    now.
  136. **
  137. **    we can also be sensitive to pipe/no pipe via isatty ()...
  138. */
  139.  
  140. /*    DV: handle erase characters, kill characters, etc. */
  141. handle_erase (ch, cnt)
  142. char ch;
  143. int *cnt;
  144. {
  145.     static char first = 1;
  146.     static char erasec, killc, werasec, lnextc, rprntc;
  147.     static char lnextflg = 0;
  148.     static char *cp;
  149.     static FILE *tty;
  150.     int i;
  151.  
  152.     if (first) {
  153.         /* initialize static things */
  154.         struct sgttyb sb;
  155.         struct ltchars ltc;
  156.  
  157.         first = 0;
  158.         cp = erase_buf;
  159.         tty = fopen ("/dev/tty", "w");
  160.  
  161.         /* . this isn't going to do... what if -e is set?...dan */
  162.         if (tty == NULL)
  163.             tty = stderr;
  164.         ioctl (0, TIOCGETP, &sb);
  165.         ioctl (0, TIOCGLTC, <c);
  166.         erasec = sb.sg_erase;
  167.         killc = sb.sg_kill;
  168.         werasec = ltc.t_werasc;
  169.         lnextc = ltc.t_lnextc;
  170.         rprntc = ltc.t_rprntc;
  171.     }
  172.  
  173.     if (lnextflg) {
  174.         ch |= 0x80;
  175.         lnextflg = 0;
  176.     }
  177.     (*cnt) --;
  178.     if (ch == erasec) {
  179.         if (*cnt < 0)
  180.             return;
  181.         fprintf (tty, "\b \b");
  182.         (*cnt) --;
  183.         *--cp = 0;
  184.     } else if (ch == killc) {
  185.         while (*cnt >= 0) {
  186.             fprintf (tty, "\b \b");
  187.             (*cnt) --;
  188.             *--cp = 0;
  189.         }
  190.     } else if (ch == werasec) {
  191.         if (*cnt < 0)
  192.             return;
  193.         while ((cp[-1] == ' ' || cp[-1] == '\t') && (*cnt) >= 0) {
  194.             fprintf (tty, "\b");
  195.             (*cnt) --;
  196.             *--cp = 0;
  197.         }
  198.         while (cp[-1] != ' ' && cp[-1] != '\t' && (*cnt) >= 0) {
  199.             fprintf (tty, "\b \b");
  200.             (*cnt) --;
  201.             *--cp = 0;
  202.         }
  203.     } else if (ch == lnextc) {
  204.         lnextflg = 1;
  205.         fprintf (tty, "^\b");
  206.     } else if (ch == rprntc) {
  207.         for (i = strlen (erase_buf); i > 0; i--)
  208.             putc ('\b', tty);
  209.         fprintf (tty, "%s", erase_buf);
  210.     } else {
  211.         ch &= 0x7f;
  212.         fprintf (tty, "%c", ch);
  213.         *cp++ = ch;
  214.         (*cnt) ++;
  215.     }
  216.     fflush (tty);
  217.     return;
  218. }
  219. #endif
  220.