home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / lib_term / PickOne.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.4 KB  |  135 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <strings.h>
  4.  
  5. int PickOne(ChoiceList, ChoiceCount)
  6. char **ChoiceList;
  7. int ChoiceCount;
  8.  
  9. /*
  10.  ---------------------------------------------------------------------------
  11.  
  12.    Last revision - 
  13.     16 November 1984 - GWS
  14.     Ignore XON, XOFF
  15.  
  16.      12 April 1984 - GWS
  17.  
  18.  
  19.    NAME
  20.      PickOne - "crash-proof" routine for picking one of a list of
  21.           list of strings
  22.  
  23.    SYNOPSIS
  24.     int PickOne(ChoiceList, ChoiceCount)
  25.     char **ChoiceList;
  26.     int ChoiceCount;
  27.  
  28.    DESCRIPTION
  29.     This routine prompts and nudges the user through selection of a
  30.     string from a table of strings - useful for choosing an item
  31.     from a menu for instance.  The options are displayed one at a
  32.     time.  The current item is selected by pressing return.  The
  33.     space bar advances, the up-arrow backs up.  The return
  34.     value of the function is the index of the chosen string.
  35.  
  36.    SEE ALSO
  37.  
  38.  
  39.    DIAGNOSTICS
  40.     none 
  41.  
  42.    BUGS
  43.     none known
  44.  
  45.    AUTHOR
  46.      George W. Sherouse
  47.      11 April 1984
  48.  
  49.  ---------------------------------------------------------------------------
  50. */
  51.  
  52. {
  53.     int c;
  54.     char erase;
  55.     int val;
  56.     int cookie;
  57.     int biggest;
  58.     int temp;
  59.     int loop;
  60.     char Format[80];
  61.  
  62.     void underline();
  63.     int tgetnum();
  64.     char TermSetUp();
  65.     void TermRewind();
  66.     int strlen();
  67.  
  68. /*
  69. In the silly case where there is only one choice, just print it
  70. and return the index 0.
  71. */
  72.     if (ChoiceCount == 1)
  73.     {
  74.     underline(1);
  75.     printf("%s", ChoiceList[0]);
  76.     underline(0);
  77.     return(0);
  78.     }
  79. /*
  80. Find the longest string in the bunch
  81. */
  82.     biggest = strlen(ChoiceList[0]);
  83.     for (loop = 1; loop < ChoiceCount; loop++)
  84.     if ((temp = strlen(ChoiceList[loop])) > biggest)
  85.         biggest = temp;
  86.  
  87. /*
  88. Find out if we need to cope with magic cookies.
  89. */
  90.     if ((cookie = tgetnum("ug")) < 0)
  91.     cookie = 0;
  92.  
  93.     underline(1);
  94.     for (loop = 0; loop < biggest; loop++)
  95.     printf(" ");
  96.  
  97.     if (cookie)
  98.     {
  99.     underline(0);
  100.     TermRewind(cookie);
  101.     }
  102.  
  103.     erase = TermSetUp();    /* set no echo, single char input */
  104.                     /* get erase character */
  105.     sprintf(Format, "%%-%ds", biggest);
  106.  
  107.     val = 0;
  108.     while (1)
  109.     {
  110.     TermRewind(biggest);
  111.     printf(Format, ChoiceList[val]);
  112.  
  113.     switch (c = (getchar() & 0177))
  114.     {
  115.     case '\015':
  116.         underline(0);
  117.         TermSetUp();
  118.         return(val);
  119.     case ' ':
  120.         if (++val == ChoiceCount)
  121.         val = 0;
  122.         break;
  123.     case '^':
  124.         if (--val < 0)
  125.         val = ChoiceCount - 1;
  126.         break;
  127.     case '\021':
  128.     case '\023':
  129.         break;
  130.     default:
  131.         printf("%c", '\007');
  132.     }
  133.     }
  134. }
  135.