Commit | Line | Data |
---|---|---|
ec5c7841 | 1 | /* |
d99e6414 | 2 | * Copyright (c) 1980 Regents of the University of California. |
ec5c7841 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
d99e6414 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
ec5c7841 KB |
16 | */ |
17 | ||
18 | #ifndef lint | |
d99e6414 | 19 | static char sccsid[] = "@(#)getinp.c 5.2 (Berkeley) %G%"; |
ec5c7841 KB |
20 | #endif /* not lint */ |
21 | ||
22 | # include <stdio.h> | |
23 | # include <ctype.h> | |
24 | ||
25 | # define reg register | |
26 | ||
27 | # define LINE 70 | |
28 | ||
29 | static char buf[257]; | |
30 | ||
31 | getinp(prompt, list) | |
32 | char *prompt, *list[]; { | |
33 | ||
34 | reg int i, n_match, match; | |
35 | char *sp; | |
36 | int plen; | |
37 | ||
38 | ||
39 | for (;;) { | |
40 | inter: | |
41 | printf(prompt); | |
42 | for (sp = buf; (*sp=getchar()) != '\n'; ) | |
43 | if (*sp == -1) /* check for interupted system call */ | |
44 | goto inter; | |
45 | else if (sp != buf || *sp != ' ') | |
46 | sp++; | |
47 | if (buf[0] == '?' && buf[1] == '\n') { | |
48 | printf("Valid inputs are: "); | |
49 | for (i = 0, match = 18; list[i]; i++) { | |
50 | if ((match+=(n_match=strlen(list[i]))) > LINE) { | |
51 | printf("\n\t"); | |
52 | match = n_match + 8; | |
53 | } | |
54 | if (*list[i] == '\0') { | |
55 | match += 8; | |
56 | printf("<RETURN>"); | |
57 | } | |
58 | else | |
59 | printf(list[i]); | |
60 | if (list[i+1]) | |
61 | printf(", "); | |
62 | else | |
63 | putchar('\n'); | |
64 | match += 2; | |
65 | } | |
66 | continue; | |
67 | } | |
68 | *sp = '\0'; | |
69 | for (sp = buf; *sp; sp++) | |
70 | if (isupper(*sp)) | |
71 | *sp = tolower(*sp); | |
72 | for (i = n_match = 0; list[i]; i++) | |
73 | if (comp(list[i])) { | |
74 | n_match++; | |
75 | match = i; | |
76 | } | |
77 | if (n_match == 1) | |
78 | return match; | |
79 | else if (buf[0] != '\0') | |
80 | printf("Illegal response: \"%s\". Use '?' to get list of valid answers\n", buf); | |
81 | } | |
82 | } | |
83 | ||
84 | static | |
85 | comp(s1) | |
86 | char *s1; { | |
87 | ||
88 | reg char *sp, *tsp, c; | |
89 | ||
90 | if (buf[0] != '\0') | |
91 | for (sp = buf, tsp = s1; *sp; ) { | |
92 | c = isupper(*tsp) ? tolower(*tsp) : *tsp; | |
93 | tsp++; | |
94 | if (c != *sp++) | |
95 | return 0; | |
96 | } | |
97 | else if (*s1 != '\0') | |
98 | return 0; | |
99 | return 1; | |
100 | } |