home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume7 / my_wgetstr / my_wgetstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-03  |  3.9 KB  |  174 lines

  1. /*
  2.  * A possible replacement for curses' wgetstr(). Allows
  3.  * line editing as in csh. 
  4.  * Recognizes the erase, kill chars, word deleting.
  5.  *
  6.  * By Hung Le (mott@ucscb.ucsc.edu  ...!ucbvax!ucscc!ucscb!mott)
  7.  * History:
  8.  *    01/18/89 - Initial version 
  9.  *    03/20/89 - Add control-w to delete word
  10.  *  05/17/89 - add option to return immediately or to bell()
  11.  *            when buffer is full.
  12.  *
  13.  * Release to Public Domain 
  14.  */
  15. #include <curses.h>
  16. #include <sys/ioctl.h>
  17. #include <sgtty.h>
  18.  
  19. #define STD_INPUT 0        /* standard input */
  20. #define NEWLINE '\n'
  21. #define SPACE ' '
  22. /* default for erase and kill characters */
  23. #define ERASE '\010'
  24. #define KILL '\025'
  25. #define WORD '\027'
  26.  
  27. /* You bugger !!! you did something wrong */
  28. #define bell() fprintf(stderr,"%c", '\007')
  29.  
  30. /*
  31.  * my_wgetstr(WINDOW *win, char *str, int size, int ret)
  32.  *    win: the concerned window
  33.  *     str: buffer holding input
  34.  *    size: (size -1) is the maximum chars to get
  35.  *    ret: flag indicating what to do when (size -1) is reached
  36.  *        0: ring bell()
  37.  *        1: returns
  38.  *        two macros are defined in "my_wgetstr.h" as RET=1 and NO_RET=0
  39.  *
  40.  * works same as wgetstr() in curses but allows editing.
  41.  * Recognizes the erase character and the kill character and
  42.  * WORD as the word deleting char.
  43.  * Will try to get the erase and kill char from the terminal setting.
  44.  * If failed, will use the default ERASE and KILL definitions.
  45.  * Word char is set to WORD.
  46.  *
  47.  * DOES NOT check for illegal scrolling.
  48.  * Returns
  49.  *    . when received a new line character 
  50.  *    . if ( (strlen(str) == (size - 1)) and (ret) )        
  51.  *
  52.  * str[size - 1] must be set to '\0' to end the string properly 
  53.  * so my_wgetstr(errwin, str, 8) will get at most 7 characters.
  54.  *
  55.  * Returned value is the number of chars read. 
  56.  */
  57.  
  58. my_wgetstr(win, str, size, ret)
  59. WINDOW *win;
  60. char *str;
  61. int size;
  62. int ret;
  63. {
  64.     struct sgttyb ttyb;
  65.     char erase_char, kill_char;
  66.     register int x_pos, y_pos, index, in;
  67.  
  68.     if (ioctl(STD_INPUT, TIOCGETP, &ttyb) == -1)
  69.     {
  70.         /*
  71.          * failed to get terminal setting. Let's use the default
  72.          * ERASE and KILL 
  73.          */
  74.         erase_char = ERASE;
  75.         kill_char = KILL;
  76.     }
  77.     else
  78.     {
  79.         erase_char = ttyb.sg_erase;
  80.         kill_char = ttyb.sg_kill;
  81.     }
  82.  
  83.     /*
  84.      * since we are going to set noecho() and crmode() let's be safe and
  85.      * save the current tty 
  86.      */
  87.     savetty();
  88.     noecho();
  89.     crmode();
  90.  
  91.     /* get current position */
  92.     getyx(win, y_pos, x_pos);
  93.     /* intializing */
  94.     index = 0;
  95.     str[index] = '\0';
  96.  
  97.     /* while input char is not NEWLINE */
  98.     while ((in = getch() & 0177) != NEWLINE)
  99.     {
  100.         /* if buffer is full (size -1) */
  101.         if (index >= size - 1)
  102.             if (ret)/* return flag set, return immediately */
  103.                 break;
  104.             else    /* allows editing chars to pass through */
  105.             if ((in != erase_char) && (in != kill_char) && (in != WORD))
  106.             {
  107.                 /* warns user that buffer is full */
  108.                 bell();
  109.                 continue;
  110.             }
  111.  
  112.         if (in == erase_char)    /* ERASING */
  113.         {
  114.             if (index > 0)
  115.             {
  116.                 mvwaddch(win, y_pos, --x_pos, SPACE);
  117.                 str[--index] = SPACE;
  118.                 wmove(win, y_pos, x_pos);
  119.             }
  120.             else
  121.                 bell();
  122.         }
  123.         else
  124.         if (in == kill_char)    /* KILLING */
  125.         {
  126.             if (index > 0)
  127.                 while (index > 0)
  128.                 {
  129.                     mvwaddch(win, y_pos, --x_pos, SPACE);
  130.                     str[--index] = SPACE;
  131.                     wmove(win, y_pos, x_pos);
  132.                 }
  133.             else
  134.                 bell();
  135.         }
  136.         else
  137.         if (in == WORD)    /* WORD DELETING */
  138.         {
  139.             if (index > 0)
  140.             {
  141.                 /* throw away all spaces */
  142.                 while ((index > 0) && (str[index - 1] == SPACE))
  143.                 {
  144.                     mvwaddch(win, y_pos, --x_pos, SPACE);
  145.                     str[--index] = SPACE;
  146.                     wmove(win, y_pos, x_pos);
  147.                 }
  148.                 /* move back until see another space */
  149.                 while ((index > 0) && (str[index - 1] != SPACE))
  150.                 {
  151.                     mvwaddch(win, y_pos, --x_pos, SPACE);
  152.                     str[--index] = SPACE;
  153.                     wmove(win, y_pos, x_pos);
  154.                 }
  155.             }
  156.             else
  157.                 bell();
  158.         }
  159.         else
  160.         {
  161.             mvwaddch(win, y_pos, x_pos++, in);
  162.             str[index++] = in;
  163.         }
  164.         /* show result */
  165.         wrefresh(win);
  166.     }
  167.     /* ends the string properly */
  168.     str[index] = '\0';
  169.     /* restore the tty */
  170.     resetty();
  171.     /* returns number of chars read */
  172.     return (index);
  173. }
  174.