-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.c
246 lines (207 loc) · 5.63 KB
/
interface.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <locale.h>
#include <ncurses.h>
#include <menu.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "data.h"
#include "reflow.h"
#include "utils.h"
WINDOW *list_win = NULL;
MENU *list_menu = NULL;
ITEM **items = NULL;
WINDOW *report_win = NULL;
static void
create_list_window ()
{
list_win = newwin (LINES-1, COLS / 3, 0, 0);
wattron (list_win, COLOR_PAIR (1));
keypad (list_win, TRUE);
box (list_win, 0, 0);
wrefresh (list_win);
}
static void
create_report_window ()
{
report_win = newwin (LINES-1, COLS / 3 * 2, 0, COLS / 3 + 1);
wattron (report_win, COLOR_PAIR (1));
keypad (report_win, TRUE);
box (report_win, 0, 0);
wrefresh (report_win);
}
/*
* Fill the left menu with the names of vulnerabilities found.
*/
static void
populate_list (vulnerability_t vulnerabilities[MAX_VULNERABILITY_COUNT], size_t vulnerabilities_count)
{
if (vulnerabilities_count == 0)
return;
items = xalloc ((vulnerabilities_count + 1) * sizeof (ITEM *));
for (size_t i = 0; i < vulnerabilities_count; i++)
items[i] = new_item (vulnerabilities[i].title, NULL);
list_menu = new_menu (items);
set_menu_win (list_menu, list_win);
set_menu_back (list_menu, COLOR_PAIR (1));
WINDOW *sub = derwin (list_win, LINES - 3, COLS / 3 - 2, 1, 1);
set_menu_sub (list_menu, sub);
set_menu_format (list_menu, LINES - 3, 1);
post_menu (list_menu);
wrefresh (list_win);
refresh ();
}
/*
* Display given vulnerability in main window.
*/
static int
show_report (vulnerability_t *vulnerability, size_t y)
{
int err = 0;
wclear (report_win);
size_t max_width = (COLS / 3 * 2) - 2;
size_t max_height = LINES - 2;
line_t lines[MAX_LINES] = {0};
size_t line_count = 0;
char *err_msg = NULL;
err = reflow (max_width, vulnerability, lines, &line_count, &err_msg);
if (err)
{
mvwprintw (report_win, 1, 1, "%s", err_msg);
wrefresh (report_win);
fprintf (stderr, "interface.c : show_report() : can't format lines.\n");
goto cleanup;
}
if (y >= line_count - 2)
y = line_count - 2;
for (size_t i = 0; i < max_height && i + y < line_count; i++)
{
if (lines[i + y].heading)
{
wattron (report_win, COLOR_PAIR (2));
wattron (report_win, A_BOLD);
}
mvwprintw (report_win, i + 1, 1, "%s", lines[i + y].content);
if (lines[i + y].heading)
{
wattroff (report_win, A_BOLD);
wattroff (report_win, COLOR_PAIR (2));
wattron (report_win, COLOR_PAIR (1));
}
}
box (report_win, 0, 0);
wrefresh (report_win);
cleanup:
for (size_t i = 0; i < line_count; i++)
free (lines[i].content);
return err;
}
/*
* Get ncurses interface ready.
*/
void
init_ncurses (vulnerability_t vulnerabilities[MAX_VULNERABILITY_COUNT], size_t vulnerabilities_count)
{
setlocale(LC_CTYPE, "");
initscr ();
cbreak ();
keypad (stdscr, true);
noecho ();
start_color ();
init_pair (1, COLOR_WHITE, COLOR_BLACK);
init_pair (2, COLOR_YELLOW, COLOR_BLACK);
attron (COLOR_PAIR (1));
refresh ();
create_list_window ();
create_report_window ();
mvprintw (LINES - 1, 1, "Press q to quit, J/K/tab/S-tab to navigate reports, j/k/DOWN/UP to scroll down/up the report");
populate_list (vulnerabilities, vulnerabilities_count);
if (vulnerabilities_count > 0)
show_report (&vulnerabilities[0], 0);
else
{
mvwprintw (report_win, 1, 1, "No vulnerability found.");
wrefresh (report_win);
}
move (LINES - 1, COLS - 1);
refresh ();
}
/*
* Handle user input.
*
* Returns true if the program needs to quit.
*/
bool
handle_key (vulnerability_t vulnerabilities[MAX_VULNERABILITY_COUNT], size_t vulnerabilities_count, size_t *current_vulnerability, size_t *current_line)
{
int key = getch ();
switch (key)
{
case 'q':
return true;
case 'j':
case KEY_DOWN:
if (vulnerabilities_count > 0)
{
(*current_line)++;
show_report (&vulnerabilities[*current_vulnerability], *current_line);
move (LINES - 1, COLS - 1);
}
break;
case 'k':
case KEY_UP:
if (vulnerabilities_count > 0)
{
if (*current_line > 0)
{
(*current_line)--;
show_report (&vulnerabilities[*current_vulnerability], *current_line);
move (LINES - 1, COLS - 1);
}
}
break;
case 'J':
case '\t':
if (vulnerabilities_count > 0)
if (*current_vulnerability < vulnerabilities_count - 1)
{
(*current_vulnerability)++;
*current_line = 0;
menu_driver (list_menu, REQ_DOWN_ITEM);
wrefresh (list_win);
show_report (&vulnerabilities[*current_vulnerability], *current_line);
move (LINES - 1, COLS - 1);
}
break;
case 'K':
case KEY_BTAB:
if (vulnerabilities_count > 0)
if (*current_vulnerability > 0)
{
(*current_vulnerability)--;
*current_line = 0;
menu_driver (list_menu, REQ_UP_ITEM);
wrefresh (list_win);
show_report (&vulnerabilities[*current_vulnerability], *current_line);
move (LINES - 1, COLS - 1);
}
break;
}
return false;
}
void
cleanup_ncurses ()
{
endwin ();
if (list_menu) free_menu (list_menu);
if (items)
{
for (size_t i = 0; i < MAX_VULNERABILITY_COUNT; i++)
{
ITEM *item = items[i];
if (!item)
break;
free_item (item);
}
free (items);
}
}