Commit | Line | Data |
---|---|---|
ec5c7841 KB |
1 | /* |
2 | * Copyright (c) 1987 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that this notice is preserved and that due credit is given | |
7 | * to the University of California at Berkeley. The name of the University | |
8 | * may not be used to endorse or promote products derived from this | |
9 | * software without specific prior written permission. This software | |
10 | * is provided ``as is'' without express or implied warranty. | |
11 | */ | |
12 | ||
13 | #ifndef lint | |
14 | static char sccsid[] = "@(#)getinp.c 5.1 (Berkeley) %G%"; | |
15 | #endif /* not lint */ | |
16 | ||
17 | # include <stdio.h> | |
18 | # include <ctype.h> | |
19 | ||
20 | # define reg register | |
21 | ||
22 | # define LINE 70 | |
23 | ||
24 | static char buf[257]; | |
25 | ||
26 | getinp(prompt, list) | |
27 | char *prompt, *list[]; { | |
28 | ||
29 | reg int i, n_match, match; | |
30 | char *sp; | |
31 | int plen; | |
32 | ||
33 | ||
34 | for (;;) { | |
35 | inter: | |
36 | printf(prompt); | |
37 | for (sp = buf; (*sp=getchar()) != '\n'; ) | |
38 | if (*sp == -1) /* check for interupted system call */ | |
39 | goto inter; | |
40 | else if (sp != buf || *sp != ' ') | |
41 | sp++; | |
42 | if (buf[0] == '?' && buf[1] == '\n') { | |
43 | printf("Valid inputs are: "); | |
44 | for (i = 0, match = 18; list[i]; i++) { | |
45 | if ((match+=(n_match=strlen(list[i]))) > LINE) { | |
46 | printf("\n\t"); | |
47 | match = n_match + 8; | |
48 | } | |
49 | if (*list[i] == '\0') { | |
50 | match += 8; | |
51 | printf("<RETURN>"); | |
52 | } | |
53 | else | |
54 | printf(list[i]); | |
55 | if (list[i+1]) | |
56 | printf(", "); | |
57 | else | |
58 | putchar('\n'); | |
59 | match += 2; | |
60 | } | |
61 | continue; | |
62 | } | |
63 | *sp = '\0'; | |
64 | for (sp = buf; *sp; sp++) | |
65 | if (isupper(*sp)) | |
66 | *sp = tolower(*sp); | |
67 | for (i = n_match = 0; list[i]; i++) | |
68 | if (comp(list[i])) { | |
69 | n_match++; | |
70 | match = i; | |
71 | } | |
72 | if (n_match == 1) | |
73 | return match; | |
74 | else if (buf[0] != '\0') | |
75 | printf("Illegal response: \"%s\". Use '?' to get list of valid answers\n", buf); | |
76 | } | |
77 | } | |
78 | ||
79 | static | |
80 | comp(s1) | |
81 | char *s1; { | |
82 | ||
83 | reg char *sp, *tsp, c; | |
84 | ||
85 | if (buf[0] != '\0') | |
86 | for (sp = buf, tsp = s1; *sp; ) { | |
87 | c = isupper(*tsp) ? tolower(*tsp) : *tsp; | |
88 | tsp++; | |
89 | if (c != *sp++) | |
90 | return 0; | |
91 | } | |
92 | else if (*s1 != '\0') | |
93 | return 0; | |
94 | return 1; | |
95 | } |