home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume3 / libc_term / get_string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.3 KB  |  142 lines

  1. #include <curses.h>
  2. #include <ctype.h>
  3. #include "c_term.h"
  4.  
  5. void get_string(string, size, nullOK)
  6. char *string;
  7. int size;
  8. int nullOK;
  9.  
  10. /*
  11.  ---------------------------------------------------------------------------
  12.  
  13.    Last revision - 
  14.     6 January 1985 - GWS
  15.     Change to use curses
  16.  
  17.     16 November 1984 - GWS
  18.     Ignore XON, XOFF
  19.  
  20.      11 April 1984 - GWS
  21.  
  22.  
  23.    NAME
  24.      get_string - "crash-proof" routine for terminal input of string
  25.  
  26.    SYNOPSIS
  27.     void get_string(string, size, nullOK)
  28.     char *string;
  29.     int size;
  30.     int nullOK;
  31.  
  32.    DESCRIPTION
  33.     This routine prompts and nudges the user through entry of a
  34.     character string.  'Size' is the number of characters in'string'.
  35.     'String' should therefore be dimensioned to size + 1.
  36.     'NullOK' is a boolean that tells us whether a null string is
  37.     acceptable.
  38.  
  39.    SEE ALSO
  40.  
  41.  
  42.    DIAGNOSTICS
  43.     none 
  44.  
  45.    BUGS
  46.     none known
  47.  
  48.    AUTHOR
  49.      George W. Sherouse
  50.      11 April 1984
  51.  
  52.  ---------------------------------------------------------------------------
  53. */
  54.  
  55. {
  56.     int c;
  57.     int loop;
  58.     int count,
  59.     where_x,
  60.     where_y,
  61.     x,
  62.     y;
  63.     char    Format[30];
  64.  
  65.     standout();
  66.     getyx(stdscr, where_y, where_x);
  67.  
  68.     for (loop = 0; loop < size; loop++)
  69.     {
  70.     printw(" ");
  71.     string[loop] = (char) 0;
  72.     }
  73.     string[size] = (char) 0;
  74.     move(where_y, where_x);
  75.     refresh();
  76.  
  77.     count = 0;
  78.     while (1)
  79.     {
  80.     switch (c = (getch() & 0177))
  81.     {
  82. #ifdef ABORT_CHAR
  83.     case ABORT_CHAR:
  84.         clear();
  85.         standend();
  86.         mvprintw(0, 0, "Program aborted at your request...");
  87.         move(LINES - 1, 0);
  88.         refresh();
  89.         endwin();
  90.         exit(0);
  91.         break;
  92. #endif ABORT_CHAR
  93.  
  94.     case '\015':
  95.         if (count || nullOK)
  96.         {
  97.         standend();
  98.         sprintf(Format, "%%-%ds", size);
  99.         mvprintw(where_y, where_x, Format, string);
  100.         refresh();
  101.         return;
  102.         }
  103.         else
  104.         {
  105.         fprintf(stderr, "%c", '\007');
  106.         break;
  107.         }
  108.     case '\030':
  109.         while (count)
  110.         {
  111.         string[--count] = (char) 0;
  112.         getyx(stdscr, y, x);
  113.         mvprintw(y, x-1, " ");
  114.         move(y, x - 1);
  115.         }
  116.         break;
  117.     case '\021':
  118.     case '\023':
  119.         break;
  120.     default:
  121.         if (count && c == erase_char)
  122.         {
  123.         getyx(stdscr, y, x);
  124.         mvprintw(y, x-1, " ");
  125.         move(y, x - 1);
  126.         string[--count] = (char) 0;
  127.         break;
  128.         }
  129.  
  130.         if (isprint(c) && count < size)
  131.         {
  132.         printw("%c", c);
  133.         string[count++] = c;
  134.         break;
  135.         }
  136.  
  137.         fprintf(stderr, "%c", '\007');
  138.     }
  139.     refresh();
  140.     }
  141. }
  142.