]> git.saurik.com Git - apple/boot.git/blame - i386/libsaio/gets.c
boot-83.2.tar.gz
[apple/boot.git] / i386 / libsaio / gets.c
CommitLineData
14c7c974
A
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#include "libsaio.h"
30
31int gets( char * buf, int len )
32{
33 char *lp = buf, *end = buf + len - 1;
34 int c;
35
36 flushdev(); // XXX
37
38 for (;;) {
39 c = getchar() & 0x7f;
40 if (c < ' ' && c != '\n' && c != '\b') c = 0;
41 if (c == 0x7f) c = '\b';
42
43 switch(c)
44 {
45 case '\0':
46 continue;
47 case '\n':
48 *lp++ = '\0';
49 putchar('\n');
50 return 1;
51 case '\b':
52 if (lp > buf) {
53 lp--;
54 putchar('\b');
55 putchar(' ');
56 putchar('\b');
57 }
58 continue;
59 default:
60 if (lp < end)
61 *lp++ = c;
62 else
63 {
64 putchar('\b');
65 putchar(' ');
66 putchar('\b');
67 putchar('\007');
68 }
69 }
70 }
71}
72
73/*
74 * Return a string in buf if typing has begun within timeout units.
75 */
76int
77Gets( char * buf,
78 int len,
79 int timeout,
80 char * prompt,
81 char * message )
82{
83 int ch = 0;
84 int next_second;
85
86 flushdev(); // XXX
87
88 printf("%s", prompt);
89
90 if (message)
91 printf("%s", message);
92
93 if (timeout)
94 {
95 for ( next_second = time18() + 18; timeout; )
96 {
97 if (ch = readKeyboardStatus())
98 {
99 break;
100 }
101 if ( time18() >= next_second )
102 {
103 next_second += 18;
104 timeout--;
105 }
106 }
107
108 if (ch == 0)
109 {
110 printf("\n");
111 return 0;
112 }
113 }
114 return ( gets(buf, len) );
115}