-
Notifications
You must be signed in to change notification settings - Fork 38
/
common.c
executable file
·107 lines (80 loc) · 1.71 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
common.c
*/
/* $Id: common.c,v 7.2 2003/10/01 20:21:09 sgifford Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <stdarg.h>
#include <unistd.h>
#include "common.h"
#define TTY_STORE 16
static struct termios orig_tty_state[TTY_STORE];
static int sttyfds[TTY_STORE];
void errorf (char *fmt, ...)
{
va_list args;
va_start (args, fmt);
vfprintf (stderr, fmt, args);
exit (1);
}
int fdprintf (int fd, char *fmt, ...)
{
va_list args;
int r;
char str[256];
va_start (args, fmt);
r = vsprintf(str, fmt, args);
write (fd, str, r);
return (r);
}
char *leafname (char *path)
{
int i = 0, j;
for (j = 0; path[j]; j++)
if (path[j] == '/')
i = j + 1;
return (path + i);
}
/* init the stty store array */
void stty_initstore (void)
{
int i;
for (i = 0; i < TTY_STORE; i++)
sttyfds[i] = -1;
}
/* set tty on fd into raw mode */
int stty_raw (int fd)
{
struct termios tty_state;
int i;
if (tcgetattr(fd, &tty_state) < 0)
return (-1);
for (i = 0; i < TTY_STORE; i++)
if (sttyfds[i] == -1)
{
orig_tty_state[i] = tty_state;
sttyfds[i] = fd;
break;
}
tty_state.c_lflag &= ~(ICANON | IEXTEN | ISIG | ECHO);
tty_state.c_iflag &= ~(ICRNL | INPCK | ISTRIP | IXON | BRKINT);
tty_state.c_oflag &= ~OPOST;
tty_state.c_cflag |= CS8;
tty_state.c_cc[VMIN] = 1;
tty_state.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &tty_state) < 0)
return (-1);
return (0);
}
/* restore all altered ttys to their original state */
void stty_orig (void)
{
int i;
for (i = 0; i < TTY_STORE; i++)
if (sttyfds[i] != -1)
{
tcsetattr (sttyfds[i], TCSAFLUSH, &orig_tty_state[i]);
sttyfds[i] = -1;
}
}