File: help_page.c

package info (click to toggle)
netdiag 1.2-1
  • links: PTS
  • area: main
  • in suites: buster, sid
  • size: 3,268 kB
  • sloc: ansic: 23,895; sh: 5,544; makefile: 328; awk: 130
file content (223 lines) | stat: -rw-r--r-- 5,086 bytes parent folder | download | duplicates (5)
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
/*
 *	Copyright (c) 2004 Rinet Corp., Novosibirsk, Russia
 *
 * Redistribution and use in source forms, with and without modification,
 * are permitted provided that this entire comment appears intact.
 *
 * THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
 */

#ifdef	HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef	HAVE_SLCURSES
#include <slcurses.h>
#elif	HAVE_NCURSES
#include <ncurses.h>
#else
#include <curses.h>
#endif
#include <stdio.h>
#include <string.h>

#include "trafshow.h"
#include "help_page.h"
#include "getkey.h"
#include "screen.h"
#include "selector.h"

static ShowMode help_mode = HelpPage;

struct help_page_entry {
	int key;
	const char *name;
	const char *descr;
};

static struct help_page_entry Interfaces_help[] = {
 { 'q',		"   Esc",	"Quit the program"	},
 { K_CR,	"  Enter",	"Use Arrow-Keys to select Interface to show" },
 { K_CTRL('L'),	"  Ctrl-L",	"Refresh screen from scratch" },
 { 'r',		"    R",	"Set the screen refresh-period.." },
 { 'p',		"    P",	"Set the expired data purge-period.." },
 { 'f',		"    F",	"Set the filter expression (empty to reset).." },
 { '/',		"    /",	"To search & follow for string in the list.." },
 { K_CTRL('_'),	"  Ctrl-/",	"Turn off search & follow mode" },
 { 'a',		"    A",	"To aggregate/summarize flows totally.." },
 { K_CTRL('R'),	"  Ctrl-R",	"Reset all flow cache totally" },
 { 'n',		"    N",	"Toggle numeric values to names conversion" },

 { 0,0,0 }
};

static struct help_page_entry NetStat_help[] = {
 { 'q',		"   Esc",	"Return to previous page" },
 { K_CR,	"  Enter",	"Use Arrow-Keys to select Flow for detail" },
 { K_LEFT,	"   Left",	"Rotate show mode left" },
 { K_RIGHT,	"  Right",	"Rotate show mode right" },
 { K_TAB,	"   Tab",	"Move cursor to backflow if any" },
 { K_CTRL('L'),	"  Ctrl-L",	"Refresh screen from scratch" },
 { 'r',		"    R",	"Set the screen refresh-period.." },
 { 'p',		"    P",	"Set the expired data purge-period.." },
 { 'f',		"    F",	"Set the filter expression (empty to reset).." },
 { '/',		"    /",	"To search & follow for string in the list.." },
 { K_CTRL('_'),	"  Ctrl-/",	"Turn off search & follow mode" },
 { 'a',		"    A",	"To aggregate/summarize flows in the list.." },
 { K_CTRL('R'),	"  Ctrl-R",	"Reset flow cache on the Interface" },
 { 'n',		"    N",	"Toggle numeric values to names conversion" },

 { 0,0,0 }
};

ShowMode
help_page_mode()
{
	return help_mode;
}

static void
scale_size(name, descr)
	int *name, *descr;
{
	*name	= line_factor * (double)HELP_PAGE_NAME;
	*descr	= line_factor * (double)HELP_PAGE_DESCR;
}

static int
help_page_header(dst, size, unused)
	char *dst;
	int size;
	const void *unused;
{
	int name_sz, desc_sz;

	/* sanity check */
	if (!dst || size < 1 || unused)
		return 0;

	scale_size(&name_sz, &desc_sz);

	snprintf(dst, size,
		 "%-*.*s %-*.*s",
		 name_sz, name_sz,	" KeyPress",
		 desc_sz, desc_sz,	"Action");
	return 0;
}

static int
help_page_line(dst, size, hp, idx)
	char *dst;
	int size;
	const struct help_page_entry *hp;
	int idx;
{
	int name_sz, desc_sz;

	/* sanity check */
	if (!dst || size < 1 || !hp)
		return 0;

	scale_size(&name_sz, &desc_sz);
	snprintf(dst, size,
		 "%-*.*s %-*.*s",
		 name_sz, name_sz,	hp[idx].name,
		 desc_sz, desc_sz,	hp[idx].descr);
	return 0;
}

static int
help_page_footer(dst, size, topic)
	char *dst;
	int size;
	const char *topic;
{
	int i, len;
	SELECTOR *sp = help_page_selector();

	/* sanity check */
	if (!dst || size < 1 || !topic || !sp)
		return 0;
	i = 0;
	len = strlen(topic);
	if (len > 0 && len < sp->COLS) {
		len = sp->COLS/2 - len/2;
		while (i < len) dst[i++] = ' ';
	}
	(void)strncpy(dst + i, topic, size - i);
	dst[size-1] = '\0';
	return 0;
}

SELECTOR *
help_page_selector()
{
	static SELECTOR *sp = 0;
	if (!sp && (sp = selector_init()) != 0) {
		int name_sz, desc_sz;
		scale_size(&name_sz, &desc_sz);

		sp->window_color = A_REVERSE;
		sp->cursor_color = A_NORMAL;
		sp->COLS = MIN(name_sz + desc_sz, COLS);
		sp->LINES = MIN(sp->COLS/3, LINES);
		sp->COL = COLS/2 - sp->COLS/2;
		sp->LINE = LINES/2 - sp->LINES/2;
		sp->get_header = help_page_header;
		sp->get_line = help_page_line;
		sp->get_footer = help_page_footer;
	}
	return sp;
}

int
help_page_key(idx)
	int idx;
{
	int i;
	SELECTOR *sp;
	const struct help_page_entry *hp;

	if ((sp = help_page_selector()) == 0)
		return -1;
	hp = (const struct help_page_entry *)sp->list;
	for (i = 0; hp; hp++) {
		if (i++ == idx) break;
	}
	return (hp ? hp->key : -1);
}

SELECTOR *
help_page_list(mode)
	ShowMode mode;
{
	struct help_page_entry *hp = 0;
	char *topic = 0;
	SELECTOR *sp;

	switch (mode) {
	case Interfaces:
		hp = Interfaces_help;
		topic = "Interface selection Help";
		break;
	case NetStat:
		hp = NetStat_help;
		topic = "Network Flow selection Help";
		break;
	case FlowDump:	/* no help available */
	case HelpPage:	/* help on help?? */
		return 0;
	}

	if ((sp = help_page_selector()) != 0) {
		help_mode = mode;
		sp->header = 0; /* unused */
		sp->footer = topic;
		sp->list = hp;
		sp->items = 0;
		for (; hp && hp->name; hp++)
			sp->items++;
	}
	return sp;
}