File: compare.c

package info (click to toggle)
termdebug 2.2+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 456 kB
  • ctags: 685
  • sloc: ansic: 5,008; sh: 560; makefile: 48; lex: 46
file content (384 lines) | stat: -rw-r--r-- 11,739 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* Copyright (C) 2013 G.P. Halkes
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3, as
   published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <float.h>
#include <time.h>
#include <errno.h>

#include "common.h"
#include "optionMacros.h"
#include "replay.h"
#include "input.h"

#if defined(HAVE_CURSES) && defined(HAVE_UNISTRING)
#include "write_stringlist.h"
#endif

static const char *inputA, *inputB;
static ExpNode *scriptA, *scriptB;
static td_bool stop_on_difference;
#if defined(HAVE_CURSES) && defined(HAVE_UNISTRING)
static td_bool describe;
static td_bool visual_compare;
static window_t *winA, *winB;
#ifdef HAVE_CAIRO
static const char *picture_name;
static const char *picture_series;
#endif
#endif

// FIXME: add option to check equality of sent strings as well

static void printUsage(void) {
	printf("Usage: tdcompare [OPTIONS] <recording A> <recording B>\n");
#if defined(HAVE_CURSES) && defined(HAVE_UNISTRING)
	printf("  -v, --visual-compare                Check for visual equality\n");
	printf("  -D, --describe                      Print a description of the first\n"
	       "                                        difference found\n");
#ifdef HAVE_CAIRO
	printf("  -p<name>, --picture=<name>          Write a picture of the differences to\n"
	       "                                        <name>\n");
	printf("  -f<font>, --font=<font>             Use <font> to show text in picture.\n");
	printf("  -W<width>, --cell-width=<width>     Use <width> as cell width in picture.\n");
	printf("  -H<height>, --cell-height=<height>  Use <height> as cell width in picture.\n");
	printf("  -P<name>, --picture-series=<name>   Create a series of picutes use <name> as\n"
	       "                                        template\n");
	printf("  -s, --stop-on-difference            Stop when the first visual difference is\n"
	       "                                        found\n");
#endif
#endif
	printf("  -V, --version                       Print version and copyright information\n");
	exit(EXIT_SUCCESS);
}

static PARSE_FUNCTION(parse_options)
	OPTIONS
		OPTION('h', "help", NO_ARG)
			printUsage();
		END_OPTION
		OPTION('V', "version", NO_ARG)
			fputs("tdcompare " VERSION_STRING "\nCopyright (C) 2013 G.P. Halkes\nLicensed under the GNU General Public License version 3\n", stdout); /* @copyright */
			exit(0);
		END_OPTION
		OPTION('v', "visual-compare", NO_ARG)
#if defined(HAVE_CURSES) && defined(HAVE_UNISTRING)
			visual_compare = td_true;
#else
			fatal("termdebug was built without (n)curses and/or libunistring, and therefore tdcompare does not provide " OPTFMT "\n", OPTPRARG);
#endif
		END_OPTION
		OPTION('D', "describe", NO_ARG)
#if defined(HAVE_CURSES) && defined(HAVE_UNISTRING)
			describe = td_true;
#else
			fatal("termdebug was built without (n)curses and/or libunistring, and therefore tdcompare does not provide " OPTFMT "\n", OPTPRARG);
#endif
		END_OPTION
		OPTION('p', "picture", REQUIRED_ARG)
#ifdef HAVE_CAIRO
			picture_name = optArg;
#else
			fatal("termdebug was built without cairo, and therefore tdcompare does not provide " OPTFMT "\n", OPTPRARG);
#endif
		END_OPTION
		OPTION('f', "font", REQUIRED_ARG)
#ifdef HAVE_CAIRO
			option_font = optArg;
#else
			fatal("termdebug was built without cairo, and therefore tdcompare does not provide " OPTFMT "\n", OPTPRARG);
#endif
		END_OPTION
		OPTION('W', "cell-width", REQUIRED_ARG)
#ifdef HAVE_CAIRO
			PARSE_INT(cell_width, 6, 16);
#else
			fatal("termdebug was built without cairo, and therefore tdcompare does not provide " OPTFMT "\n", OPTPRARG);
#endif
		END_OPTION
		OPTION('H', "cell-heigth", REQUIRED_ARG)
#ifdef HAVE_CAIRO
			PARSE_INT(cell_width, 6, 24);
#else
			fatal("termdebug was built without cairo, and therefore tdcompare does not provide " OPTFMT "\n", OPTPRARG);
#endif
		END_OPTION
		OPTION('P', "picture-series", REQUIRED_ARG)
#ifdef HAVE_CAIRO
			if (!check_fmt(optArg))
				fatal(OPTFMT " argument must have exactly one d-type printf conversion\n", OPTPRARG);
			picture_series = optArg;
#else
			fatal("termdebug was built without cairo, and therefore tdcompare does not provide " OPTFMT "\n", OPTPRARG);
#endif
		END_OPTION
		OPTION('s', "stop-on-difference", NO_ARG)
#ifdef HAVE_CAIRO
			stop_on_difference = td_true;
#else
			fatal("termdebug was built without cairo, and therefore tdcompare does not provide " OPTFMT "\n", OPTPRARG);
#endif
		END_OPTION

		DOUBLE_DASH
			NO_MORE_OPTIONS;
		END_OPTION

		fatal("Unknown option " OPTFMT "\n", OPTPRARG);
	NO_OPTION
		if (inputA != NULL) {
			if (inputB != NULL)
				fatal("More than two recordings specified\n");
			inputB = optcurrent;
		} else {
			inputA = optcurrent;
		}
	END_OPTIONS
	if (inputA == NULL || inputB == NULL)
		fatal("Two recordings should be specified\n");
#ifdef HAVE_CAIRO
	if (picture_series == NULL)
		stop_on_difference = td_true;
#else
	stop_on_difference = td_true;
#endif
END_FUNCTION

#if defined(HAVE_CURSES) && defined(HAVE_UNISTRING)
static td_bool advance_script(ExpNode **script, window_t *win) {
	while (*script != NULL) {
		switch ((*script)->type) {
			case EXP_START:
			case EXP_INTERACT:
			case EXP_ENV:
				/* Ignore stuff that isn't relavant to simply viewing the replay. */
				break;
			case EXP_EXPECT:
				win_write_stringlist(win, (*script)->un.args);
				break;
			case EXP_SEND:
				*script = (*script)->next;
				return td_true;
			case EXP_EXPECT_EXIT:
				*script = (*script)->next;
				return td_true;
			case EXP_WINDOW_SIZE:
				win_resize(win, (*script)->un.size.rows, (*script)->un.size.columns);
				*script = (*script)->next;
				return td_true;
			case EXP_EXPECT_SUSPEND:
				win_set_paint(win, 0, 0);
				win_clrtobot(win);
				return td_true;
			case EXP_REQUIRE_VERSION:
				break;
			default:
				fatal("Internal error\n");
		}
		*script = (*script)->next;
	}
	return td_false;
}

static void do_visual_compare(void) {
	td_bool difference_reported = td_false;
	const char *winAterm, *winBterm;
	int step_count = 0;

	init_attr_map();

	if ((winA = win_new(24, 80)) == NULL)
		fatal("Out of memory\n");
	if ((winB = win_new(24, 80)) == NULL)
		fatal("Out of memory\n");

	winAterm = get_script_env(scriptA, "TERM");
	winBterm = get_script_env(scriptB, "TERM");
	if (winAterm == NULL)
		fatal("Recording %s does not contain a TERM environment variable\n", inputA);
	if (winBterm == NULL)
		fatal("Recording %s does not contain a TERM environment variable\n", inputB);
	if (strcmp(winAterm, winBterm) != 0)
		fatal("Recordings have a different TERM environment variable\n");
	setup_ti_strings(winAterm, get_script_env(scriptA, "LANG"));

	while (td_true) {
		if (!advance_script(&scriptA, winA))
			break;
		if (!advance_script(&scriptB, winB))
			break;

#ifdef HAVE_CAIRO
		if (picture_series != NULL) {
			char output_name_buffer[1024];
			snprintf(output_name_buffer, sizeof(output_name_buffer), picture_series, step_count);
			win_write_image(winA, winB, output_name_buffer);
		}
#endif

		if (!win_compare(winA, winB, describe ? stderr : NULL)) {
#ifdef HAVE_CAIRO
			if (picture_name != NULL) {
				win_write_image(winA, winB, picture_name);
				picture_name = NULL;
			}
#endif
			if (!difference_reported) {
				fprintf(stderr, "Recordings have different results\n");
				difference_reported = td_true;
			}
			/* stop_on_difference will be set to false if picture_series == NULL. */
			if (stop_on_difference)
				exit(EXIT_FAILURE);
			describe = td_false;
		}
		step_count++;
	}

	if (scriptA != scriptB && !difference_reported)
		fatal("Recordings have different results\n");

	free_attr_map();
}
#endif

static ExpNode *find_start(ExpNode *script) {
	for (; script != NULL && script->type != EXP_START; script = script->next) {}
	return script;
}

static void compare_expects(void) {
	StringListNode *stringA = scriptA->un.args;
	StringListNode *stringB = scriptB->un.args;
	int stringA_offset = 0;
	int stringB_offset = 0;
	int min_length;


	while (1) {
		min_length = MIN(stringA->length - stringA_offset, stringB->length - stringB_offset);
		/* FIXME: explain which string and at which offset */
		if (memcmp(stringA->string + stringA_offset, stringB->string + stringB_offset, min_length) != 0)
			fatal("Different strings expected at %d/%d\n", scriptA->line, scriptB->line);
		stringA_offset += min_length;
		if (stringA->length <= stringA_offset) {
			stringA = stringA->next;
			stringA_offset = 0;
		}
		stringB_offset += min_length;
		if (stringB->length <= stringB_offset) {
			stringB = stringB->next;
			stringB_offset = 0;
		}

		if (stringA == NULL && stringB == NULL) {
			scriptA = scriptA->next;
			scriptB = scriptB->next;
			return;
		}

		if (stringA == NULL) {
			/* FIXME: explain which string and at which offset */
			if (scriptA->next->type != EXP_EXPECT)
				fatal("Different strings expected at %d/%d\n", scriptA->line, scriptB->line);
			scriptA = scriptA->next;
			stringA = scriptA->un.args;
		}
		if (stringB == NULL) {
			/* FIXME: explain which string and at which offset */
			if (scriptB->next->type != EXP_EXPECT)
				fatal("Different strings expected at %d/%d\n", scriptA->line, scriptB->line);
			scriptB = scriptB->next;
			stringB = scriptB->un.args;
		}
	}
}

static void compare(void) {
	static const char *exp2string[] = {
		"start", "env", "window_size", "expect", "send", "expect_exit",
		"interact", "expect_suspend", "require_version"
	};

	scriptA = find_start(scriptA);
	scriptB = find_start(scriptB);

	if (scriptA == NULL)
		fatal("Recording '%s' does not have a start directive\n", inputA);
	if (scriptB == NULL)
		fatal("Recording '%s' does not have a start directive\n", inputB);

	while (scriptA != NULL && scriptB != NULL) {
		if (scriptA->type != scriptB->type)
			fatal("Recordings have different actions (%s/%s) at line %d/%d\n",
				exp2string[scriptA->type], exp2string[scriptB->type],
				scriptA->line, scriptB->line);

		switch (scriptA->type) {
			case EXP_START:
			case EXP_INTERACT:
				/* Ignore stuff that isn't relavant to simply viewing the replay. */
				break;
			case EXP_EXPECT:
				compare_expects();
				break;
			case EXP_SEND:
				break;
			case EXP_WINDOW_SIZE:
				if (scriptA->un.size.columns != scriptB->un.size.columns ||
						scriptA->un.size.rows != scriptB->un.size.rows)
					fatal("Recordings set different window size (%dx%d/%dx%d) at line %d/%d\n",
						scriptA->un.size.columns, scriptA->un.size.rows,
						scriptB->un.size.columns, scriptB->un.size.rows,
						scriptA->line, scriptB->line);
				break;
			case EXP_EXPECT_EXIT:
				if (scriptA->un.number != scriptB->un.number)
					fatal("Expecting different exit value (%d/%d) at line %d/%d\n",
						scriptA->un.number, scriptB->un.number, scriptA->line, scriptB->line);
				break;
			case EXP_EXPECT_SUSPEND:
				break;
			default:
				fatal("Internal error\n");
		}

		scriptA = scriptA->next;
		scriptB = scriptB->next;
	}
}

int tdcompare_main(int argc, char *argv[]) {

	parse_options(argc, argv);

	reset_lexer(inputA);
	parse();
	scriptA = script;
	script = NULL;

	reset_lexer(inputB);
	parse();
	scriptB = script;

#if defined(HAVE_CURSES) && defined(HAVE_UNISTRING)
	if (visual_compare)
		do_visual_compare();
	else
#endif
		compare();
	return EXIT_SUCCESS;
}