File: cv.c

package info (click to toggle)
multitail 4.2.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 660 kB
  • ctags: 812
  • sloc: ansic: 12,046; makefile: 91; sh: 19
file content (256 lines) | stat: -rw-r--r-- 6,696 bytes parent folder | download
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
247
248
249
250
251
252
253
254
255
256
#define _LARGEFILE64_SOURCE     /* required for GLIBC to enable stat64 and friends */
#include <sys/types.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>

#include "mt.h"
#include "mem.h"
#include "error.h"
#include "globals.h"


int cv_offsets_compare(const void *a, const void *b)
{
	cv_off *pa = (cv_off *)a, *pb = (cv_off *)b;

	if (pa -> start > pb -> start)
		return -1;
	else if (pa -> start == pb -> start)
	{
		if (pa -> end > pb -> end)
			return -1;
	}

	return 0;
}

char * epoch_to_str(time_t epoch)
{
	char *new_string;
	struct tm *ptm = localtime(&epoch);
	if (!ptm)
		return NULL;

	new_string = mymalloc(4096, "epoch_to_str: new string");
	if (!strftime(new_string, 4096, cnv_ts_format, ptm))
		error_exit("epoch_to_str: error converting timestamp format '%s'\n", cnv_ts_format);

	return new_string;
}

char *do_convert(char *what, int type)
{
	switch(type)
	{
		case CONVTYPE_TAI64NTODATE:
			{
				long long int v2_62 = (long long int)1 << (long long int)62;
				long long int val = 0;
				int loop;

				if (what[0] == '@')
					what++;

				/* FIXME: http://cr.yp.to/libtai/tai64.html#tai64n */

				/* convert to 64 bit integer */
				for(loop=0; loop<(8 * 2); loop++)
				{
					int c = tolower(what[loop]);

					val <<= (long long int)4;
					if (c >= 'a')
						val += 10 + c - 'a';
					else
						val += c - '0';
				}

				if (val >= v2_62) /* 2^63 are reserved, not checking for that, sorry */
				{
					char *new_str = epoch_to_str((time_t)(val - v2_62));

					if (new_str)
						return new_str;
					else
						return mystrdup("cannot convert current 'TAI64N'-date to string", "do_convert: invalid TAI64N timecode");
				}
				else
				{
					/* before 1970/1/1 now what should I do with that? */

					return mystrdup("cannot convert 'TAI64N'-dates before the epoch", "do_convert: TAI64N value to small");
				}
			}

		case CONVTYPE_IP4TOHOST:
			{
				struct hostent *ht;
				in_addr_t addr = inet_addr(what);
				if ((int)addr == -1)
					return mystrdup(what, "do_convert: unaltered string");

				if ((ht = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL)
					return mystrdup(what, "do_convert: unaltered string");

				return mystrdup(ht -> h_name, "do_convert: looked-up hostname");
			}
			break; /* this redundant break-statement is what we call "defensive programming" */

		case CONVTYPE_EPOCHTODATE:
			{
				char *new_str = epoch_to_str((time_t)atoll(what));

				if (new_str)
					return new_str;
				else
					return mystrdup("cannot convert current epoch value", "do_convert: cannot convert epoch");
			}
			break;

		case CONVTYPE_ERRNO:
			{
				return mystrdup(strerror(atoi(what)), "do_convert: error string");
			}

		case CONVTYPE_HEXTODEC:
			{
				long long int result = strtoll(what, NULL, 16);
				char result_str[128];

				snprintf(result_str, sizeof(result_str), "%lld", result);

				return mystrdup(result_str, "do_convert: decimal string");
			}

		case CONVTYPE_DECTOHEX:
			{
				long long int result = atoll(what);
				char result_str[128];

				snprintf(result_str, sizeof(result_str), "%llx", result);

				return mystrdup(result_str, "do_convert: hex string");
			}

		default:
			error_exit("do_convert: internal error, unknown conversion type (%d)\n", type);
	}

	return "do_convert: INTERNAL ERROR";
}

char *convert(proginfo *cur, char *line)
{
	conversion *cur_conv = NULL;
	cv_off *cv_offsets = NULL;
	int conv_index;
	int conv_req;
	int new_len = 0;
	int max_n_cv_matches = 0, cur_n_cv_matches = 0;
	char *new_string = NULL;
	int offset_old = 0, offset_new = 0;
	int old_len = strlen(line);

	if (cur -> n_conversions == 0)
		return line;

	for(conv_req=0; conv_req < cur -> n_conversions; conv_req++)
	{
		cur_conv = &conversions[(cur -> conversions)[conv_req]];

		max_n_cv_matches = cur_conv -> n * MAX_N_RE_MATCHES;
		cv_offsets = (cv_off *)mymalloc(sizeof(cv_off) * max_n_cv_matches, "convert: array of offsets for conversions");

		/* find where they match */
		for(conv_index=0; conv_index<cur_conv -> n && cur_n_cv_matches < max_n_cv_matches; conv_index++)
		{
			regmatch_t matches[MAX_N_RE_MATCHES];

			if (regexec(&(cur_conv -> regex)[conv_index], line, MAX_N_RE_MATCHES, matches, 0) == 0)
			{
				int cur_match_index;
				LOG("convert match\n");
				for(cur_match_index=1; cur_match_index<MAX_N_RE_MATCHES && cur_n_cv_matches < max_n_cv_matches; cur_match_index++)
				{
					char *dummy;
					int dummylen;

					if (matches[cur_match_index].rm_so == -1)
						break;

					(cur_conv -> match_count)[conv_index]++;

					cv_offsets[cur_n_cv_matches].start = matches[cur_match_index].rm_so;
					cv_offsets[cur_n_cv_matches].end   = matches[cur_match_index].rm_eo;

					dummylen = matches[cur_match_index].rm_eo - matches[cur_match_index].rm_so;
					dummy = mymalloc(dummylen + 1, "convert: replace by");
					memcpy(dummy, &line[matches[cur_match_index].rm_so], dummylen);
					dummy[dummylen] = 0x00;
					cv_offsets[cur_n_cv_matches].newstr= do_convert(dummy, (cur_conv -> type)[conv_index]);
					LOG("convert newstr: %s\n", cv_offsets[cur_n_cv_matches].newstr);
					myfree(dummy);

					cur_n_cv_matches++;
				}
			}
		}
	}

	if (cur_n_cv_matches)
	{
		int n_copy;

		/* sort */
		if (cur_n_cv_matches > 1)
			qsort(cv_offsets, cur_n_cv_matches, sizeof(cv_off), cv_offsets_compare);

		/* create new string */
		for(conv_index=0; conv_index < cur_n_cv_matches; conv_index++)
		{
			n_copy = cv_offsets[conv_index].start - offset_old;
			LOG("convert replace by: %s\n", cv_offsets[conv_index].newstr);
			if (n_copy > 0)
			{
				new_string = myrealloc(new_string, new_len + n_copy + 1, "convert: new_string");
				memcpy(&new_string[offset_new], &line[offset_old], n_copy);
				new_string[offset_new + n_copy] = 0x00;
				new_len += n_copy;
				offset_new += n_copy;
			}
			offset_old = cv_offsets[conv_index].end;

			n_copy = strlen(cv_offsets[conv_index].newstr);
			new_string = myrealloc(new_string, new_len + n_copy + 1, "convert: new_string");
			memcpy(&new_string[offset_new], cv_offsets[conv_index].newstr, n_copy);
			new_string[offset_new + n_copy] = 0x00;
			myfree(cv_offsets[conv_index].newstr);
			new_len += n_copy;
			offset_new += n_copy;
		}
		LOG("convert new string: %s\n", new_string);

		n_copy = old_len - offset_old;
		if (n_copy)
		{
			new_string = myrealloc(new_string, new_len + n_copy + 1, "convert: new_string");
			memcpy(&new_string[offset_new], &line[offset_old], n_copy);
			new_string[offset_new + n_copy] = 0x00;
		}
	}
	else
	{
		new_string = line;
	}

	myfree(cv_offsets);

	return new_string;
}