1*454af1c0Sdsl /* $NetBSD: keywait.c,v 1.2 2009/03/14 15:36:04 dsl Exp $ */
2b24ea33cSleo
3b24ea33cSleo /*
4b24ea33cSleo * Copyright (c) 1995 Waldi Ravens.
5b24ea33cSleo * All rights reserved.
6b24ea33cSleo *
7b24ea33cSleo * Redistribution and use in source and binary forms, with or without
8b24ea33cSleo * modification, are permitted provided that the following conditions
9b24ea33cSleo * are met:
10b24ea33cSleo * 1. Redistributions of source code must retain the above copyright
11b24ea33cSleo * notice, this list of conditions and the following disclaimer.
12b24ea33cSleo * 2. Redistributions in binary form must reproduce the above copyright
13b24ea33cSleo * notice, this list of conditions and the following disclaimer in the
14b24ea33cSleo * documentation and/or other materials provided with the distribution.
15b24ea33cSleo * 3. All advertising materials mentioning features or use of this software
16b24ea33cSleo * must display the following acknowledgement:
17b24ea33cSleo * This product includes software developed by Waldi Ravens.
18b24ea33cSleo * 4. The name of the author may not be used to endorse or promote products
19b24ea33cSleo * derived from this software without specific prior written permission.
20b24ea33cSleo *
21b24ea33cSleo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22b24ea33cSleo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23b24ea33cSleo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24b24ea33cSleo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25b24ea33cSleo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26b24ea33cSleo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27b24ea33cSleo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28b24ea33cSleo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29b24ea33cSleo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30b24ea33cSleo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31b24ea33cSleo */
32b24ea33cSleo
33b24ea33cSleo #include <stdio.h>
34b24ea33cSleo #include <stdlib.h>
35b24ea33cSleo #include <unistd.h>
36b24ea33cSleo #include <termios.h>
37b24ea33cSleo #include "libtos.h"
38b24ea33cSleo
39b24ea33cSleo int
key_wait(char * text)40*454af1c0Sdsl key_wait(char *text)
41b24ea33cSleo {
42b24ea33cSleo struct termios term_attr;
43b24ea33cSleo tcflag_t lfl_orig;
44b24ea33cSleo int any_key;
45b24ea33cSleo char c;
46b24ea33cSleo
47b24ea33cSleo any_key = (text == NULL);
48b24ea33cSleo if (any_key)
49b24ea33cSleo text = "Press any key...";
50b24ea33cSleo
51b24ea33cSleo fprintf(stderr, text);
52b24ea33cSleo fflush(stderr);
53b24ea33cSleo
54b24ea33cSleo tcgetattr(STDERR_FILENO, &term_attr);
55b24ea33cSleo lfl_orig = term_attr.c_lflag;
56b24ea33cSleo if (any_key)
57b24ea33cSleo term_attr.c_lflag &= ~ECHO;
58b24ea33cSleo term_attr.c_lflag &= ~ICANON;
59b24ea33cSleo tcsetattr(STDERR_FILENO, TCSAFLUSH, &term_attr);
60b24ea33cSleo read(STDERR_FILENO, &c, 1);
61b24ea33cSleo term_attr.c_lflag = lfl_orig;
62b24ea33cSleo tcsetattr(STDERR_FILENO, TCSAFLUSH, &term_attr);
63b24ea33cSleo
64b24ea33cSleo fprintf(stderr, (any_key ? "\r" : "\r\n"));
65b24ea33cSleo fflush(stderr);
66b24ea33cSleo return(c);
67b24ea33cSleo }
68