C-Menu
0.2.9
A User Interface Toolkit
Toggle main menu visibility
Loading...
Searching...
No Matches
enterstr.c
Go to the documentation of this file.
1
/** @file enterstr.c
2
@brief Enter a string from keyboard
3
@author Bill Waller
4
Copyright (c) 2025
5
MIT License
6
billxwaller@gmail.com
7
@date 2026-02-09
8
*/
9
10
#
include
<
cm
.
h
>
11
#
include
<
stdbool
.
h
>
12
#
include
<
stdio
.
h
>
13
#
include
<
string
.
h
>
14
#
include
<
sys
/
ioctl
.
h
>
15
#
include
<
termios
.
h
>
16
#
include
<
unistd
.
h
>
17
18
/** @example
19
@code
20
USERNAME=$(enterstr "Enter your username: ")
21
PASSWORD=$(enterstr "Enter your password: ")
22
echo "Username: $USERNAME"
23
echo "Password: $PASSWORD"
24
@endcode
25
*/
26
27
/** @brief allows the user to enter a string in cooked mode
28
@note allows line editing and other features
29
@note writes the prompt to stderr and the user's input to stdout
30
@note handles signals to ensure that the terminal settings are restored if
31
the program is interrupted. */
32
int
main
(
int
argc,
char
**argv) {
33
struct
termios new_tioctl;
34
char
in_str[
BUFSIZ
];
35
char
*in_ptr = in_str;
36
char
*msg;
37
char
errmsg[128];
38
39
if
(argc < 2)
40
strcpy(errmsg,
"input:"
);
41
else
42
strcpy(errmsg, argv[1]);
43
capture_shell_tioctl
(
)
;
44
sig_prog_mode
(
)
;
45
new_tioctl =
shell_tioctl
;
46
new_tioctl.c_lflag |= ICANON;
47
tcsetattr(2, TCSAFLUSH, &new_tioctl);
48
while
(1) {
49
msg = errmsg;
50
while
(*msg)
51
write(2, msg++, 1);
52
if
(read(2, in_str,
BUFSIZ
) > -1)
53
break
;
54
}
55
while
(*in_ptr)
56
write(1, in_ptr++, 1);
57
sig_dfl_mode
(
)
;
58
restore_shell_tioctl
(
)
;
59
exit(0);
60
}
BUFSIZ
#define BUFSIZ
Definition
view.h:29
main
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.
Definition
enterchr.c:38
shell_tioctl
struct termios shell_tioctl
Definition
scriou.c:22
capture_shell_tioctl
bool capture_shell_tioctl()
capture_shell_tioctl() - capture shell terminal settings
Definition
scriou.c:43
restore_shell_tioctl
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition
scriou.c:56
sig_dfl_mode
void sig_dfl_mode()
Set signal handlers to default behavior.
Definition
sig.c:42
sig_prog_mode
void sig_prog_mode()
Set up signal handlers for interrupt signals.
Definition
sig.c:62
enterstr.c
Generated by
1.17.0