home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <ctype.h>
- #include <strings.h>
-
- int PickOne(ChoiceList, ChoiceCount)
- char **ChoiceList;
- int ChoiceCount;
-
- /*
- ---------------------------------------------------------------------------
-
- Last revision -
- 16 November 1984 - GWS
- Ignore XON, XOFF
-
- 12 April 1984 - GWS
-
-
- NAME
- PickOne - "crash-proof" routine for picking one of a list of
- list of strings
-
- SYNOPSIS
- int PickOne(ChoiceList, ChoiceCount)
- char **ChoiceList;
- int ChoiceCount;
-
- DESCRIPTION
- This routine prompts and nudges the user through selection of a
- string from a table of strings - useful for choosing an item
- from a menu for instance. The options are displayed one at a
- time. The current item is selected by pressing return. The
- space bar advances, the up-arrow backs up. The return
- value of the function is the index of the chosen string.
-
- SEE ALSO
-
-
- DIAGNOSTICS
- none
-
- BUGS
- none known
-
- AUTHOR
- George W. Sherouse
- 11 April 1984
-
- ---------------------------------------------------------------------------
- */
-
- {
- int c;
- char erase;
- int val;
- int cookie;
- int biggest;
- int temp;
- int loop;
- char Format[80];
-
- void underline();
- int tgetnum();
- char TermSetUp();
- void TermRewind();
- int strlen();
-
- /*
- In the silly case where there is only one choice, just print it
- and return the index 0.
- */
- if (ChoiceCount == 1)
- {
- underline(1);
- printf("%s", ChoiceList[0]);
- underline(0);
- return(0);
- }
- /*
- Find the longest string in the bunch
- */
- biggest = strlen(ChoiceList[0]);
- for (loop = 1; loop < ChoiceCount; loop++)
- if ((temp = strlen(ChoiceList[loop])) > biggest)
- biggest = temp;
-
- /*
- Find out if we need to cope with magic cookies.
- */
- if ((cookie = tgetnum("ug")) < 0)
- cookie = 0;
-
- underline(1);
- for (loop = 0; loop < biggest; loop++)
- printf(" ");
-
- if (cookie)
- {
- underline(0);
- TermRewind(cookie);
- }
-
- erase = TermSetUp(); /* set no echo, single char input */
- /* get erase character */
- sprintf(Format, "%%-%ds", biggest);
-
- val = 0;
- while (1)
- {
- TermRewind(biggest);
- printf(Format, ChoiceList[val]);
-
- switch (c = (getchar() & 0177))
- {
- case '\015':
- underline(0);
- TermSetUp();
- return(val);
- case ' ':
- if (++val == ChoiceCount)
- val = 0;
- break;
- case '^':
- if (--val < 0)
- val = ChoiceCount - 1;
- break;
- case '\021':
- case '\023':
- break;
- default:
- printf("%c", '\007');
- }
- }
- }
-