]> git.saurik.com Git - apple/boot.git/blob - gen/libsaio/gets.c
boot-111.tar.gz
[apple/boot.git] / gen / libsaio / gets.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright 1993, NeXT Computer Inc.
27 * All rights reserved.
28 */
29
30 #import "libsaio.h"
31
32 int gets(char *buf);
33
34 /*
35 * return a string in buf iff typing is begun within timeout units
36 */
37 int
38 Gets(
39 char *buf,
40 int timeout,
41 char *prompt,
42 char *message
43 )
44 {
45 int ch = 0;
46 int next_second;
47
48 printf("%s",prompt);
49 if (message)
50 printf("%s",message);
51 if (timeout) {
52 #if 0
53 printf("[");
54 for (i = 0; i < timeout; i++)
55 putchar('-');
56 printf("]\b\b");
57 #endif
58
59 for(next_second = time18() + 18; timeout; ) {
60 if (ch = readKeyboardStatus()) {
61 break;
62 }
63 if (time18() >= next_second) {
64 next_second += 18;
65 timeout--;
66 #if 0
67 printf(" \b\b");
68 #endif
69 }
70 }
71 #if 0
72 putchar('\r');
73 printf("%s",prompt);
74 for (i = 0; i < strlen(message) + orig_timeout + 4; i++)
75 putchar(' ');
76 putchar('\r');
77 printf("%s",prompt);
78 #endif
79
80 if (ch == 0) {
81 printf("\n");
82 return 0;
83 }
84 }
85 return(gets(buf));
86 }
87
88 int
89 gets(
90 char *buf
91 )
92 {
93 char *lp;
94 int c;
95
96 lp = buf;
97 for (;;) {
98 c = getchar() & 0x7f;
99 if (c < ' ' && c != '\n' && c != '\b') c = 0;
100 if (c == 0x7f) c = '\b';
101
102 switch(c) {
103 case '\0':
104 continue;
105 case '\n':
106 *lp++ = '\0';
107 putchar('\n');
108 return 1;
109 case '\b':
110 if (lp > buf) {
111 lp--;
112 putchar('\b');
113 putchar(' ');
114 putchar('\b');
115 }
116 continue;
117 default:
118 *lp++ = c;
119 }
120 }
121 }