|
|
1.1 root 1: /*
2: * Copyright (c) 1980 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: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: static char sccsid[] = "@(#)getinp.c 5.3 (Berkeley) 6/1/90";
22: #endif /* not lint */
23:
24: # include <stdio.h>
25: # include <ctype.h>
26:
27: # define reg register
28:
29: # define LINE 70
30:
31: static char buf[257];
32:
33: getinp(prompt, list)
34: char *prompt, *list[]; {
35:
36: reg int i, n_match, match;
37: char *sp;
38: int plen;
39:
40:
41: for (;;) {
42: inter:
43: printf(prompt);
44: for (sp = buf; (*sp=getchar()) != '\n'; )
45: if (*sp == -1) /* check for interupted system call */
46: goto inter;
47: else if (sp != buf || *sp != ' ')
48: sp++;
49: if (buf[0] == '?' && buf[1] == '\n') {
50: printf("Valid inputs are: ");
51: for (i = 0, match = 18; list[i]; i++) {
52: if ((match+=(n_match=strlen(list[i]))) > LINE) {
53: printf("\n\t");
54: match = n_match + 8;
55: }
56: if (*list[i] == '\0') {
57: match += 8;
58: printf("<RETURN>");
59: }
60: else
61: printf(list[i]);
62: if (list[i+1])
63: printf(", ");
64: else
65: putchar('\n');
66: match += 2;
67: }
68: continue;
69: }
70: *sp = '\0';
71: for (sp = buf; *sp; sp++)
72: if (isupper(*sp))
73: *sp = tolower(*sp);
74: for (i = n_match = 0; list[i]; i++)
75: if (comp(list[i])) {
76: n_match++;
77: match = i;
78: }
79: if (n_match == 1)
80: return match;
81: else if (buf[0] != '\0')
82: printf("Illegal response: \"%s\". Use '?' to get list of valid answers\n", buf);
83: }
84: }
85:
86: static
87: comp(s1)
88: char *s1; {
89:
90: reg char *sp, *tsp, c;
91:
92: if (buf[0] != '\0')
93: for (sp = buf, tsp = s1; *sp; ) {
94: c = isupper(*tsp) ? tolower(*tsp) : *tsp;
95: tsp++;
96: if (c != *sp++)
97: return 0;
98: }
99: else if (*s1 != '\0')
100: return 0;
101: return 1;
102: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.