C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
enterchr.c File Reference

Enter a single character from keyboard. More...

#include <cm.h>
#include <fcntl.h>
#include <stdbool.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
Include dependency graph for enterchr.c:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 Capture the current terminal settings for later restoration.

Detailed Description

Enter a single character from keyboard.

Author
Bill Waller Copyright (c) 2025 MIT License billx.nosp@m.wall.nosp@m.er@gm.nosp@m.ail..nosp@m.com
Date
2026-02-09

Definition in file enterchr.c.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Capture the current terminal settings for later restoration.

Note
can be used in scripts to get a single character response from a user. For example: response=$(./enterchr "Enter Y or N: ") if [ "$response" = "Y" ]; then echo "You entered Yes" else echo "You entered No" fi
This function saves the current terminal settings into a global variable so that they can be restored later. It should be called before modifying the terminal settings to ensure that the original settings can be restored when the program exits or is interrupted.
Examples
/usr/local/src/C-Menu/src/enterchr.c, and /usr/local/src/C-Menu/src/enterstr.c.

Definition at line 38 of file enterchr.c.

38 {
39 char c = 'Y';
40 char *msg;
41 struct termios raw_tioctl;
42 char errmsg[128];
43
44 if (argc < 2)
45 strcpy(errmsg, "Press any key");
46 else
47 strcpy(errmsg, argv[1]);
49 raw_tioctl = shell_tioctl;
50 mk_raw_tioctl(&raw_tioctl);
51 tcflush(0, TCOFLUSH);
52 while (1) {
53 msg = errmsg;
54 while (*msg) {
55 write(2, msg++, 1);
56 }
57 if (read(0, &c, 1) > 0) {
58 write(1, &c, 1);
59 break;
60 }
61 }
63 if (c == (char)0x1b)
64 return (1);
65 return (0);
66}
char errmsg[]
Definition futil.c:84
struct termios shell_tioctl
Definition scriou.c:22
bool capture_shell_tioctl()
capture_shell_tioctl() - capture shell terminal settings
Definition scriou.c:43
bool mk_raw_tioctl(struct termios *)
mk_raw_tioctl() - set terminal to raw mode
Definition scriou.c:126
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:56

References capture_shell_tioctl(), mk_raw_tioctl(), restore_shell_tioctl(), and shell_tioctl.

Here is the call graph for this function: