-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui.c
112 lines (94 loc) · 2.34 KB
/
tui.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
108
109
110
111
112
/*
* tui.c:
*
* Based on ui.c from the original iftop sources.
*
* This user interface does not make use of curses. Instead, it prints its
* output to STDOUT. This output is activated by providing the '-t' flag.
*
*/
#include "config.h"
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#if defined(HAVE_TERMIOS_H)
#include <termios.h>
#elif defined(HAVE_SGTTY_H) && defined(TIOCSETP)
#include <sys/ioctl.h>
#include <sgtty.h>
#elif defined(HAVE_TERMIO_H)
#include <sys/ioctl.h>
#include <termio.h>
#else
#include <stdlib.h>
#endif
#include "sorted_list.h"
#include "options.h"
#include "ui_common.h"
/* Width of the host column in the output */
#define PRINT_WIDTH 40
/*
* UI print function
*/
//void tui_print() { /* function */}
/*
* Text interface data structure initializations.
*/
// void tui_init()
/*
* Tick function indicating screen refresh
*/
// void tui_tick(int print)
/*
* Main UI loop. Code any interactive character inputs here.
*/
void loop() {
int i;
extern sig_atomic_t foad;
#if defined(HAVE_TERMIOS_H)
struct termios new_termios, old_termios;
tcgetattr(STDIN_FILENO, &old_termios);
new_termios = old_termios;
new_termios.c_lflag &= ~(ICANON|ECHO);
new_termios.c_cc[VMIN] = 1;
new_termios.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
#elif defined(HAVE_SGTTY_H) && defined(TIOCSETP)
struct sgttyb new_tty, old_tty;
ioctl(STDIN_FILENO, TIOCGETP, &old_tty);
new_tty = old_tty;
new_tty.sg_flags &= ~(ICANON|ECHO);
ioctl(STDIN_FILENO, TIOCSETP, &new_tty);
#elif defined(HAVE_TERMIO_H)
struct termio new_termio, old_termio;
ioctl(0, TCGETA, &old_termio);
new_termio = old_termio;
new_termio.c_lflag &= ~(ICANON|ECHO);
new_termio.c_cc[VMIN] = 1;
new_termio.c_cc[VTIME] = 0;
ioctl(0, TCSETA, &new_termio);
#else
system("/bin/stty cbreak -echo >/dev/null 2>&1");
#endif
while ((i = getchar()) != 'q' && foad == 0) {
switch (i) {
case 'u':
tick(0, time(NULL));
break;
default:
break;
}
}
#if defined(HAVE_TERMIOS_H)
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
#elif defined(HAVE_SGTTY_H) && defined(TIOCSETP)
ioctl(0, TIOCSETP, &old_tty);
#elif defined(HAVE_TERMIO_H)
ioctl(0, TCSETA, &old_termio);
#else
system("/bin/stty -cbreak echo >/dev/null 2>&1");
#endif
}