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.

Macros

#define _(String)
#define bindtextdomain(Package, Directory)
#define N_(String)
#define textdomain(Domain)

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.

Macro Definition Documentation

◆ _

#define _ ( String)
Value:
Definition cm.h:409

Definition at line 24 of file enterchr.c.

◆ bindtextdomain

#define bindtextdomain ( Package,
Directory )

Definition at line 27 of file enterchr.c.

◆ N_

#define N_ ( String)
Value:

Definition at line 25 of file enterchr.c.

◆ textdomain

#define textdomain ( Domain)

Definition at line 26 of file enterchr.c.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Capture the current terminal settings for later restoration.

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.

Definition at line 50 of file enterchr.c.

50 {
51 char c = 'Y';
52 char *msg;
53 struct termios raw_tioctl;
54 char emsg[128];
55
56 if (argc < 2)
57 strcpy(emsg, _("Press any key"));
58 else
59 strcpy(emsg, argv[1]);
61 raw_tioctl = shell_tioctl;
62 mk_raw_tioctl(&raw_tioctl);
63 tcflush(0, TCOFLUSH);
64 while (1) {
65 msg = emsg;
66 while (*msg) {
67 write(2, msg++, 1);
68 }
69 if (read(0, &c, 1) > 0) {
70 write(1, &c, 1);
71 break;
72 }
73 }
75 if (c == (char)0x1b)
76 return (1);
77 return (0);
78}
#define _(String)
Definition enterchr.c:24
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:132
bool restore_shell_tioctl()
restore_shell_tioctl() - restore shell terminal settings
Definition scriou.c:57

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

Here is the call graph for this function: