File: term_events.c

package info (click to toggle)
haproxy 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 24,584 kB
  • sloc: ansic: 275,085; sh: 3,607; xml: 1,756; python: 1,345; makefile: 1,162; perl: 168; cpp: 21
file content (233 lines) | stat: -rw-r--r-- 6,608 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>

#include <haproxy/connection-t.h>
#include <haproxy/intops.h>

struct tevt_info {
	const char *loc;
	const char **types;
};


/* will be sufficient for even largest flag names */
static char buf[4096];
static size_t bsz = sizeof(buf);


static const char *tevt_unknown_types[16] = {
	[ 0] = "-", [ 1] = "-", [ 2] = "-", [ 3] = "-",
	[ 4] = "-", [ 5] = "-", [ 6] = "-", [ 7] = "-",
	[ 8] = "-", [ 9] = "-", [10] = "-", [11] = "-",
	[12] = "-", [13] = "-", [14] = "-", [15] = "-",
};

static const char *tevt_fd_types[16] = {
	[ 0] = "-",           [ 1] = "shutw",         [ 2] = "shutr",    [ 3] = "rcv_err",
	[ 4] = "snd_err",     [ 5] = "-",             [ 6] = "-",        [ 7] = "conn_err",
	[ 8] = "intercepted", [ 9] = "conn_poll_err", [10] = "poll_err", [11] = "poll_hup",
	[12] = "-",           [13] = "-",             [14] = "-",        [15] = "-",
};

static const char *tevt_hs_types[16] = {
	[ 0] = "-",       [ 1] = "-", [ 2] = "-", [ 3] = "rcv_err",
	[ 4] = "snd_err", [ 5] = "-", [ 6] = "-", [ 7] = "-",
	[ 8] = "-",       [ 9] = "-", [10] = "-", [11] = "-",
	[12] = "-",       [13] = "-", [14] = "-", [15] = "-",
};

static const char *tevt_xprt_types[16] = {
	[ 0] = "-",       [ 1] = "shutw", [ 2] = "shutr", [ 3] = "rcv_err",
	[ 4] = "snd_err", [ 5] = "-",     [ 6] = "-",     [ 7] = "-",
	[ 8] = "-",       [ 9] = "-",     [10] = "-",     [11] = "-",
	[12] = "-",       [13] = "-",     [14] = "-",     [15] = "-",
};

static const char *tevt_muxc_types[16] = {
	[ 0] = "-",             [ 1] = "shutw",           [ 2] = "shutr",             [ 3] = "rcv_err",
	[ 4] = "snd_err",       [ 5] = "truncated_shutr", [ 6] = "truncated_rcv_err", [ 7] = "tout",
	[ 8] = "goaway_rcvd",   [ 9] = "proto_err",       [10] = "internal_err",      [11] = "other_err",
	[12] = "graceful_shut", [13] = "-",               [14] = "-",                 [15] = "-",
};

static const char *tevt_se_types[16] = {
	[ 0] = "-",         [ 1] = "shutw",         [ 2] = "eos",               [ 3] = "rcv_err",
	[ 4] = "snd_err",   [ 5] = "truncated_eos", [ 6] = "truncated_rcv_err", [ 7] = "-",
	[ 8] = "rst_rcvd",  [ 9] = "proto_err",     [10] = "internal_err",      [11] = "other_err",
	[12] = "cancelled", [13] = "-",             [14] = "-",                 [15] = "-",
};

static const char *tevt_strm_types[16] = {
	[ 0] = "-",           [ 1] = "shutw",         [ 2] = "eos",               [ 3] = "rcv_err",
	[ 4] = "snd_err",     [ 5] = "truncated_eos", [ 6] = "truncated_rcv_err", [ 7] = "tout",
	[ 8] = "intercepted", [ 9] = "proto_err",     [10] = "internal_err",      [11] = "other_err",
	[12] = "aborted",     [13] = "-",             [14] = "-",                 [15] = "-",
};

static const struct tevt_info tevt_location[26] = {
	[ 0] = {.loc = "-",    .types = tevt_unknown_types}, [ 1] = {.loc = "-",    .types = tevt_unknown_types},
	[ 2] = {.loc = "-",    .types = tevt_unknown_types}, [ 3] = {.loc = "-",    .types = tevt_unknown_types},
	[ 4] = {.loc = "se",   .types = tevt_se_types},      [ 5] = {.loc = "fd",   .types = tevt_fd_types},
	[ 6] = {.loc = "-",    .types = tevt_unknown_types}, [ 7] = {.loc = "hs",   .types = tevt_hs_types},
	[ 8] = {.loc = "-",    .types = tevt_unknown_types}, [ 9] = {.loc = "-",    .types = tevt_unknown_types},
	[10] = {.loc = "-",    .types = tevt_unknown_types}, [11] = {.loc = "-",    .types = tevt_unknown_types},
	[12] = {.loc = "muxc", .types = tevt_muxc_types},    [13] = {.loc = "-",    .types = tevt_unknown_types},
	[14] = {.loc = "-",    .types = tevt_unknown_types}, [15] = {.loc = "-",    .types = tevt_unknown_types},
	[16] = {.loc = "-",    .types = tevt_unknown_types}, [17] = {.loc = "-",    .types = tevt_unknown_types},
	[18] = {.loc = "strm", .types = tevt_strm_types},    [19] = {.loc = "-",    .types = tevt_unknown_types},
	[20] = {.loc = "-",    .types = tevt_unknown_types}, [21] = {.loc = "-",    .types = tevt_unknown_types},
	[22] = {.loc = "-",    .types = tevt_unknown_types}, [23] = {.loc = "xprt", .types = tevt_xprt_types},
	[24] = {.loc = "-",    .types = tevt_unknown_types}, [25] = {.loc = "-",    .types = tevt_unknown_types},
};

void usage_exit(const char *name)
{
	fprintf(stderr, "Usage: %s { value* | - }\n", name);
	exit(1);
}

char *to_upper(char *dst, const char *src)
{
	int i;

	for (i = 0; src[i]; i++)
		dst[i] = toupper(src[i]);
	dst[i] = 0;
	return dst;
}

char *tevt_show_events(char *buf, size_t len, const char *delim, const char *value)
{
	char loc[5];
	int ret;

	if (!value || !*value) {
		snprintf(buf, len, "##NONE");
		goto end;
	}
	if (strcmp(value, "-") == 0) {
		snprintf(buf, len, "##UNK");
		goto end;
	}

	if (strlen(value) % 2 != 0) {
		snprintf(buf, len, "##INV");
		goto end;
	}

	while (*value) {
		struct tevt_info info;
		char l = value[0];
		char t = value[1];

		if (!isalpha(l) || !isxdigit(t)) {
			snprintf(buf, len, "##INV");
			goto end;
		}

		info = tevt_location[tolower(l) - 'a'];
		ret = snprintf(buf, len, "%s:%s%s",
			       isupper(l) ? to_upper(loc, info.loc) : info.loc,
			       info.types[hex2i(t)],
			       value[2] != 0 ? delim : "");
		if (ret < 0)
			break;
		len -= ret;
		buf += ret;
		value += 2;
	}

  end:
	return buf;
}

char *tevt_show_tuple_events(char *buf, size_t len, char *value)
{
	char *p = value;

	/* skip '{' */
	p++;
	while (*p) {
		char *v;
		char c;

		while (*p == ' ' || *p == '\t')
			p++;

		v = p;
		while (*p && *p != ',' && *p != '}')
			p++;
		c = *p;
		*p = 0;

		tevt_show_events(buf, len, " > ", v);
		printf("\t- %s\n", buf);

		*p = c;
		if (*p == ',')
			p++;
		else if (*p == '}')
			break;
		else {
			printf("\t- ##INV\n");
			break;
		}
	}

	*buf = 0;
	return buf;
}

int main(int argc, char **argv)
{
	const char *name = argv[0];
	char line[128];
	char *value;
	int multi = 0;
	int use_stdin = 0;
	char *err;

	while (argc == 1)
		usage_exit(name);

	argv++; argc--;
	if (argc > 1)
		multi = 1;

	if (strcmp(argv[0], "-") == 0)
		use_stdin = 1;

	while (argc > 0) {
		if (use_stdin) {
			value = fgets(line, sizeof(line), stdin);
			if (!value)
				break;

			/* skip common leading delimiters that slip from copy-paste */
			while (*value == ' ' || *value == '\t' || *value == ':' || *value == '=')
				value++;

			err = value;
			while (*err && *err != '\n')
				err++;
			*err = 0;
		}
		else {
			value = argv[0];
			argv++; argc--;
		}

		if (multi)
			printf("### %-8s : ", value);

		if (*value == '{') {
			if (!use_stdin)
				printf("\n");
			tevt_show_tuple_events(buf, bsz, value);
		}
		else
			tevt_show_events(buf, bsz, " > ", value);
		printf("%s\n", buf);
	}
	return 0;
}