home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / mush5.7 / part02 / curs_io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-16  |  5.6 KB  |  240 lines

  1. /* @(#)curs_io.c    (c) copyright 3/18/87 (Dan Heller) */
  2.  
  3. /* curs_io.c -- curses based I/O */
  4. #include "mush.h"
  5.  
  6. #ifdef CURSES
  7. #include "bindings.h"
  8. #endif CURSES
  9.  
  10. #ifdef Addch
  11. #undef Addch
  12. #endif Addch
  13.  
  14. #ifndef CURSES
  15.  
  16. #define Addch(c) if (isoff(glob_flags, ECHO_FLAG)) \
  17.               fputc(c, stdout), fflush(stdout)
  18.  
  19. #else
  20.  
  21. /* see end of Getstr */
  22. #define Addch(c)  \
  23.     if (iscurses) \
  24.     addch(c), refresh(); \
  25.     else if (isoff(glob_flags, ECHO_FLAG)) \
  26.     fputc(c, stdout), fflush(stdout)
  27. #endif CURSES
  28.  
  29. /*
  30.  * get a string of at most 'length' chars.
  31.  * allow backspace-space-backspace, kill word and kill line
  32.  * (options set by user in stty).
  33.  * length is the max length this string can get. offset is from beginning
  34.  * of string.
  35.  * input of ^D returns -1; otherwise, return the number of chars in string.
  36.  */
  37. #define backspace()     { Addch('\b'); Addch(' '); Addch('\b'); }
  38.  
  39. Getstr(String, length, offset)
  40. char String[];
  41. register int length;
  42. {
  43.     register int c;
  44.     register int count = offset;
  45.  
  46.     while ((c = getchar()) != '\n' && c != 4 /* ^D */ && c != EOF &&
  47.     isoff(glob_flags, WAS_INTR)) {
  48.     /* echo isn't set, so whatever the character, enter it */
  49.     if (ison(glob_flags, ECHO_FLAG) && count < length)
  50.         String[count++] = c;
  51.     else if (c == _tty.sg_erase || c == 8 || c == 127) {
  52.         if (count) {
  53.         backspace() /* macro */
  54.         count--;
  55.         } else if (!count && iscurses) {
  56.         String[0] = '\0';
  57.         return -1;
  58.         }
  59.     } else if (c == _tty.sg_kill) {
  60.         if (count) {
  61.         do
  62.             backspace()  /* macro */
  63.         while (--count);
  64.         }
  65.     } else
  66. #ifndef TIOCGLTC
  67.     if (c == '\022') /* system doesn't have ltchars */
  68. #else
  69.     if (c == ltchars.t_rprntc) /* reprint line */
  70. #endif TIOCGLTC
  71.         String[count] = 0, printf("\n%s", String);
  72.         else
  73. #ifndef TIOCGLTC
  74.     if (c == '\027') /* system doesn't have ltchars */
  75. #else
  76.     if (c == ltchars.t_werasc) /* word erase */
  77. #endif TIOCGLTC
  78.         while (count) {
  79.         backspace();
  80.         String[count--] = 0;
  81.         if (!count || (String[count-1]== ' '&& !isspace(String[count])))
  82.             break;
  83.         }
  84.     else if (c == '\t')
  85.         do  {
  86.         Addch(' ');
  87.         String[count] = ' ';
  88.         } while (++count % 8 && count < length);
  89.     else if (count == length)
  90.         bell();
  91.     else if (c > 31 && c != 127) {
  92.         Addch(c);
  93.         String[count++] = c;
  94.     }
  95.     }
  96.     if (c == 4 || c == EOF || ison(glob_flags, WAS_INTR)) {     /* ^D */
  97.     if (feof(stdin))
  98.         clearerr(stdin);
  99.     return -1;
  100.     }
  101.     if (count && String[count-1] == '\\') {
  102.     int count2;
  103.     if (isoff(glob_flags, ECHO_FLAG))
  104.         putchar('\n');
  105.     if ((count2 = Getstr(&String[count-1], length - count + 1, 0)) == -1)
  106.         return -1;
  107.     return count + count2;
  108.     }
  109.     if (!iscurses && isoff(glob_flags, ECHO_FLAG))
  110.     putchar('\n');
  111.     while (count > 0 && isspace(String[count-1]))
  112.     --count;
  113.     String[count] = 0;
  114.     return count;
  115. }
  116.  
  117. #undef backspace
  118. #undef Addch
  119.  
  120. #ifdef CURSES
  121. /* curses based "more" like option */
  122. void
  123. c_more()
  124. {
  125.     register char c;
  126.  
  127.     print_more("--more--");
  128.     while ((c = getchar()) != ' ' && c != '\n')
  129.     bell();
  130.     if (!iscurses && c != '\n')
  131.     while (getchar() != '\n');
  132. }
  133.  
  134. /*
  135.  * prompt for a carriage return, but return whatever user types unless
  136.  * it's a character which he might regret (like 'q' or 'x'). Ignore
  137.  * interrupts (kind of) because we have nowhere to longjmp to.  When we
  138.  * return, we'll setjmp again (top of loop.c)
  139.  */
  140. hit_return()
  141. {
  142.     int c;
  143.  
  144.     turnon(glob_flags, IGN_SIGS);
  145.     iscurses = FALSE;
  146.     (void) check_new_mail();
  147.     iscurses = TRUE;
  148.     mail_status(1), addstr("...continue... "), refresh();
  149.     c = getcmd();
  150.     turnoff(glob_flags, IGN_SIGS);
  151.  
  152.     /* don't let the user type something he might regret */
  153.     if (c == C_QUIT || c == C_EXIT)
  154.     return 0;
  155.     return c;
  156. }
  157.  
  158. curses_msg_list(str, list, m_list)
  159. register char *str, *list;
  160. char m_list[];
  161. {
  162.     register char *p = NULL;
  163.  
  164.     print(str);
  165.     if (Getstr(list, COLS-13, 0) <= 0 || !(p = do_range(list, m_list)) ||
  166.     (p == list && *p && *p != '$' && *p != '^')) {
  167.     if (p)
  168.         print("Invalid message list: %s", p);
  169.     return 0;
  170.     }
  171.     move(LINES-1, 0), refresh();
  172.     return 1;
  173. }
  174.  
  175. curs_vars(which, cntd_cmd)
  176. int which;  /* really, a char */
  177. int *cntd_cmd;
  178. {
  179.     char c, buf[128], buf2[128], *string;
  180.     struct options **list;
  181.  
  182.     switch(which) {
  183.     case C_OWN_HDR : string = "own_hdrs", list = &own_hdrs;
  184.     when C_ALIAS : string = "alias", list = &aliases;
  185.     when C_IGNORE : string = "ignore", list = &ignore_hdr;
  186.     when C_VAR_SET : string = "set", list = &set_options;
  187.     otherwise : clr_bot_line(); return;
  188.     }
  189.  
  190.     print("%s [? Set Unset All]: ", string);
  191.     c = getchar();
  192.     clr_bot_line();
  193.     switch (Lower(c)) {
  194.     /* if help, print help -- if "all", show all settings. */
  195.     case '?' : case 'a' :
  196.         if (c == '?') {
  197.         if (!strcmp(string, "set")) {
  198.             print("which variable? [All <var>]: ");
  199.             if ((c = Getstr(buf, COLS-40, 0)) < 0)
  200.             return;
  201.             clr_bot_line();
  202.             if (c > 0) {
  203.             Lower(*buf);
  204.             if (!strcmp(buf, "a"))
  205.                 (void) strcpy(buf, "all");
  206.             if (!strcmp(buf, "all"))
  207.                 *cntd_cmd = 1;
  208.             (void) cmd_line(sprintf(buf2, "set ?%s", buf),msg_list);
  209.             return;
  210.             }
  211.         }
  212.         /* help returns next command (hit_return) */
  213.         help(0, string, cmd_help);
  214.         *cntd_cmd = 1;
  215.         return;
  216.         }
  217.         *cntd_cmd = 1;
  218.         do_set(*list, NULL);
  219.  
  220.     /* if set, prompt for string and let user type */
  221.     when 's' :
  222.         print("set: ");
  223.         c = Getstr(buf, COLS-18, 0);
  224.         clr_bot_line();
  225.         if (c > 0)
  226.         (void) cmd_line(sprintf(buf2, "%s %s", string, buf), msg_list);
  227.  
  228.     /* if unset, just as easy as set! */
  229.     when 'u' :
  230.         print("unset: ", string);
  231.         if (Getstr(buf, COLS-18, 0) > 0 && !un_set(list, buf)) {
  232.         print("%s isn't set", buf);
  233.         if (*cntd_cmd)
  234.             putchar('\n');
  235.         }
  236.     }
  237. }
  238.  
  239. #endif CURSES
  240.