summaryrefslogtreecommitdiff
path: root/fpcsrc/utils/sim_pasc/pass3.c
blob: 83eff6808c51a386f584b9f5e1140cdf6e67bdf5 (plain)
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
/*	This file is part of the software similarity tester SIM.
	Written by Dick Grune, Vrije Universiteit, Amsterdam.
	$Id: pass3.c,v 2.11 2005/02/20 17:03:03 dick Exp $
*/

#include	<stdio.h>
#include	<string.h>
#include	<malloc.h>

#include	"system.par"
#include	"debug.par"
#include	"sim.h"
#include	"runs.h"
#include	"error.h"
#include	"options.h"
#include	"pass3.h"
#include	"percentages.h"

#ifdef	DB_RUN
#include	"tokenarray.h"
static void db_run(const struct run *);
#endif

static FILE *open_chunk(const struct chunk *);
static void fill_line(FILE *, char []);
static void clear_line(char []);
static void show_runs(void);
static void show_run(const struct run *);
static void show_2C_line(const char [], const char []);
static void show_1C_line(FILE *, const char *);
static int prhead(const struct chunk *);
static int prs(const char *);
static int pru(unsigned int);
static int unslen(unsigned int);

static int maxline;			/* Actual maximum line length */
static char *line0;			/* by malloc() */
static char *line1;

void
Pass3(void) {
	if (option_set('p')) {
		show_percentages();
	}
	else {
		show_runs();
	}
}

static void
show_runs(void) {
	AisoIter iter;
	struct run *run;

	maxline = PageWidth / 2 - 2;
	line0 = malloc((unsigned int)((maxline + 1) * sizeof (char)));
	line1 = malloc((unsigned int)((maxline + 1) * sizeof (char)));
	if (!line0 || !line1) fatal("out of memory");

	OpenIter(&iter);
	while (GetAisoItem(&iter, &run)) {
#ifdef	DB_RUN
		db_run(run);
#endif	/* DB_RUN */
		show_run(run);
		fprintf(OutputFile, "\n");
	}
	CloseIter(&iter);

	free(line0); line0 = 0;
	free(line1); line1 = 0;
}

static void
show_run(const struct run *run) {
	/* The animals came in two by two ... */
	register const struct chunk *cnk0 = &run->rn_cn0;
	register const struct chunk *cnk1 = &run->rn_cn1;
	register unsigned int nl_cnt0 =
			cnk0->ch_last.ps_nl_cnt - cnk0->ch_first.ps_nl_cnt;
	register unsigned int nl_cnt1 =
			cnk1->ch_last.ps_nl_cnt - cnk1->ch_first.ps_nl_cnt;
	FILE *f0;
	FILE *f1;

	/* display heading of chunk */
	if (!option_set('d')) {
		/* no assumptions about the lengths of the file names! */
		register unsigned int size = run->rn_size;
		register int pos = 0;

		pos += prhead(cnk0);
		while (pos < maxline + 1) {
			pos += prs(" ");
		}
		pos += prs("|");
		pos += prhead(cnk1);
		while (pos < 2*maxline - unslen(size)) {
			pos += prs(" ");
		}
		fprintf(OutputFile, "[%u]\n", size);
	}
	else {
		(void)prhead(cnk0);
		fprintf(OutputFile, "\n");
		(void)prhead(cnk1);
		fprintf(OutputFile, "\n");
	}

	/* stop if that suffices */
	if (option_set('n'))
		return;			/* ... had enough so soon ... */

	/* open the files that hold the chunks */
	f0 = open_chunk(cnk0);
	f1 = open_chunk(cnk1);

	/* display the chunks in the required format */
	if (!option_set('d')) {
		/* fill 2-column lines and print them */
		while (nl_cnt0 != 0 || nl_cnt1 != 0) {
			if (nl_cnt0) {
				fill_line(f0, line0);
				nl_cnt0--;
			}
			else {
				clear_line(line0);
			}
			if (nl_cnt1) {
				fill_line(f1, line1);
				nl_cnt1--;
			}
			else {
				clear_line(line1);
			}
			show_2C_line(line0, line1);
		}
	}
	else {
		/* display the lines in a diff(1)-like format */
		while (nl_cnt0--) {
			show_1C_line(f0, "<");
		}
		fprintf(OutputFile, "---\n");
		while (nl_cnt1--) {
			show_1C_line(f1, ">");
		}
	}

	/* close the pertinent files */
	fclose(f0);
	fclose(f1);
}

static int
prhead(const struct chunk *cnk) {
	register int pos = 0;

	pos += prs(cnk->ch_text->tx_fname);
	pos += prs(": line ");
	pos += pru(cnk->ch_first.ps_nl_cnt);
	pos += prs("-");
	pos += pru(cnk->ch_last.ps_nl_cnt - 1);
	return pos;
}

static int
prs(const char *str) {
	fprintf(OutputFile, "%s", str);
	return strlen(str);
}

static int
pru(unsigned int u) {
	fprintf(OutputFile, "%u", u);
	return unslen(u);
}

static int
unslen(unsigned int u) {
	register int res = 1;

	while (u > 9) {
		u /= 10, res++;
	}
	return res;
}

static FILE *
open_chunk(const struct chunk *cnk) {
	/*	opens the file in which the chunk resides, positions the
		file at the beginning of the chunk and returns the file pointer
	*/
	register char *fname = cnk->ch_text->tx_fname;
	register FILE *f = fopen(fname, "r");
	register unsigned int nl_cnt;

	if (!f) {
		fprintf(stderr, ">>>> File %s disappeared <<<<\n", fname);
		f = fopen(NULLFILE, "r");
	}

	nl_cnt = cnk->ch_first.ps_nl_cnt;
	while (nl_cnt > 1) {
		int ch = getc(f);

		if (ch < 0) break;
		if (ch == '\n') {
			nl_cnt--;
		}
	}

	return f;
}

static void
fill_line(FILE *f, char ln[]) {
	/*	Reads one line from f and puts it in condensed form in ln.
	*/
	register int indent = 0, lpos = 0;
	register int ch;

	/* condense and skip initial blank */
	while ((ch = getc(f)), ch == ' ' || ch == '\t') {
		if (ch == '\t') {
			indent = 8;
		}
		else {
			indent++;
		}
		if (indent == 8) {
			/* every eight blanks give one blank */
			if (lpos < maxline) {
				ln[lpos++] = ' ';
			}
			indent = 0;
		}
	}

	/* store the rest */
	while (ch >= 0 && ch != '\n') {
		if (ch == '\t') {
			/* replace tabs by blanks */
			ch = ' ';
		}
		if (lpos < maxline) {
			ln[lpos++] = ch;
		}
		ch = getc(f);
	}
	ln[lpos] = '\0';		/* always room for this one */
}

static void
clear_line(char ln[]) {
	/* a simple null byte will suffice */
	ln[0] = '\0';
}

static void
show_2C_line(const char ln0[], const char ln1[]) {
	/*	displays the contents of the two lines in a two-column
		format
	*/
	register int i;

	for (i = 0; i < maxline && ln0[i] != '\0'; i++) {
		fputc(ln0[i], OutputFile);
	}
	for (; i < maxline; i++) {
		fputc(' ', OutputFile);
	}
	fprintf(OutputFile, " |");

	for (i = 0; i < maxline && ln1[i] != '\0'; i++) {
		fputc(ln1[i], OutputFile);
	}
	fprintf(OutputFile, "\n");
}

static void
show_1C_line(FILE *f, const char *marker) {
	/*	displays one line from f, preceded by the marker
	*/
	register int ch;

	fprintf(OutputFile, "%s", marker);
	while ((ch = getc(f)), ch > 0 && ch != '\n') {
		fputc(ch, OutputFile);
	}
	fputc('\n', OutputFile);
}

#ifdef	DB_RUN

static void db_chunk(const struct chunk *);

static void
db_run(const struct run *run) {
	/* prints detailed data about a run */
	register const struct chunk *cnk0 = &run->rn_cn0;
	register const struct chunk *cnk1 = &run->rn_cn1;

	fprintf(DebugFile, "File %s / file %s:\n",
		cnk0->ch_text->tx_fname,
		cnk1->ch_text->tx_fname
	);
	fprintf(DebugFile, "from token %u/%u to %u/%u:",
		cnk0->ch_first.ps_tk_cnt, cnk1->ch_first.ps_tk_cnt,
		cnk0->ch_last.ps_tk_cnt, cnk1->ch_last.ps_tk_cnt
	);
	fprintf(DebugFile, " from lines %u/%u to %u/%u:",
		cnk0->ch_first.ps_nl_cnt, cnk1->ch_first.ps_nl_cnt,
		cnk0->ch_last.ps_nl_cnt, cnk1->ch_last.ps_nl_cnt
	);
	fprintf(DebugFile, " %u %s\n",
		run->rn_size,
		(run->rn_size == 1 ? "token" : "tokens")
	);

	db_chunk(cnk0);
	db_chunk(cnk1);
}

static void
db_chunk(const struct chunk *cnk) {
	/*	print the tokens in the chunk, with a one-char margin
	*/
	unsigned int i;
	const struct position *first = &cnk->ch_first;
	const struct position *last = &cnk->ch_last;
	unsigned int start = cnk->ch_text->tx_start;

	if (first->ps_tk_cnt > 0) {
		fprintf(DebugFile, "...");
		print_token(stdout, TokenArray[start + first->ps_tk_cnt - 1]);
		fprintf(DebugFile, "  ");
	}
	else {	/* create same offset as above */
		fprintf(DebugFile, "       ");
	}

	for (i = first->ps_tk_cnt; i <= last->ps_tk_cnt; i++) {
		print_token(stdout, TokenArray[start + i]);
	}

	if (start + last->ps_tk_cnt + 1 < cnk->ch_text->tx_limit) {
		fprintf(DebugFile, "  ");
		print_token(stdout, TokenArray[start + last->ps_tk_cnt + 1]);
		fprintf(DebugFile, "...");
	}

	fprintf(DebugFile, "\n");
}

#endif	/* DB_RUN */