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