-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpass3.c
336 lines (290 loc) · 7.01 KB
/
pass3.c
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
/* This file is part of the software similarity tester SIM.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: pass3.c,v 2.20 2012-06-08 06:52:16 Gebruiker Exp $
*/
#include <stdio.h>
#include <string.h>
#include "system.par"
#include "debug.par"
#include "sim.h"
#include "text.h"
#include "token.h"
#include "runs.h"
#include "Malloc.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_run(const struct run *);
static void show_2C_line(const char [], const char []);
static void show_1C_line(FILE *, const char *);
static int pr_head(const struct chunk *);
static int prs(const char *);
static int pru(size_t);
static int unslen(size_t);
static int max_line_length; /* Actual maximum line length */
static char *line0; /* by Malloc() */
static char *line1;
void
Show_Runs(void) {
AisoIter iter;
struct run *run;
#ifdef DB_RUN
fprintf(Debug_File, "Starting Show_Runs()\n");
#endif /* DB_RUN */
max_line_length = Page_Width / 2 - 2;
line0 = Malloc((size_t)(max_line_length + 1) * sizeof (char));
line1 = Malloc((size_t)(max_line_length + 1) * sizeof (char));
OpenIter(&iter);
while (GetAisoItem(&iter, &run)) {
#ifdef DB_RUN
db_run(run);
#endif /* DB_RUN */
show_run(run);
fprintf(Output_File, "\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 ... */
const struct chunk *cnk0 = &run->rn_chunk0;
const struct chunk *cnk1 = &run->rn_chunk1;
size_t nl_cnt0 = cnk0->ch_last.ps_nl_cnt - cnk0->ch_first.ps_nl_cnt;
size_t nl_cnt1 = cnk1->ch_last.ps_nl_cnt - cnk1->ch_first.ps_nl_cnt;
FILE *f0;
FILE *f1;
/* display heading of chunk */
if (!is_set_option('d')) {
/* no assumptions about the lengths of the file names! */
size_t size = run->rn_size;
int pos = 0;
pos += pr_head(cnk0);
while (pos < max_line_length + 1) {
pos += prs(" ");
}
pos += prs("|");
pos += pr_head(cnk1);
while (pos < 2*max_line_length - unslen(size)) {
pos += prs(" ");
}
fprintf(Output_File, "[%zu]\n", size);
}
else {
(void)pr_head(cnk0);
fprintf(Output_File, "\n");
(void)pr_head(cnk1);
fprintf(Output_File, "\n");
}
/* stop if that suffices */
if (is_set_option('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 (!is_set_option('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(Output_File, "---\n");
while (nl_cnt1--) {
show_1C_line(f1, ">");
}
}
/* close the pertinent files */
fclose(f0);
fclose(f1);
}
static int
pr_head(const struct chunk *cnk) {
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(Output_File, "%s", str);
return (int) strlen(str);
}
static int
pru(size_t u) {
fprintf(Output_File, "%zu", u);
return unslen(u);
}
static int
unslen(size_t u) {
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.
Note that we use fopen() here, which opens a character stream,
rather than Open_Text(), which opens a token stream.
*/
const char *fname = cnk->ch_text->tx_fname;
FILE *f = fopen(fname, "r");
size_t 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.
*/
int indent = 0, lpos = 0;
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 < max_line_length) {
ln[lpos++] = ' ';
}
indent = 0;
}
}
/* store the rest */
while (ch >= 0 && ch != '\n') {
if (ch == '\t') {
/* replace tabs by blanks */
ch = ' ';
}
if (lpos < max_line_length) {
ln[lpos++] = (char) 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
*/
int i;
for (i = 0; i < max_line_length && ln0[i] != '\0'; i++) {
fputc(ln0[i], Output_File);
}
for (; i < max_line_length; i++) {
fputc(' ', Output_File);
}
fprintf(Output_File, " |");
for (i = 0; i < max_line_length && ln1[i] != '\0'; i++) {
fputc(ln1[i], Output_File);
}
fprintf(Output_File, "\n");
}
static void
show_1C_line(FILE *f, const char *marker) {
/* displays one line from f, preceded by the marker
*/
int ch;
fprintf(Output_File, "%s", marker);
while ((ch = getc(f)), ch > 0 && ch != '\n') {
fputc(ch, Output_File);
}
fputc('\n', Output_File);
}
#ifdef DB_RUN
static void db_chunk(const struct chunk *);
static void
db_run(const struct run *run) {
/* prints detailed data about a run */
const struct chunk *cnk0 = &run->rn_chunk0;
const struct chunk *cnk1 = &run->rn_chunk1;
db_run_info(0, run, 1);
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
*/
size_t i;
const struct position *first = &cnk->ch_first;
const struct position *last = &cnk->ch_last;
size_t start = cnk->ch_text->tx_start;
if (first->ps_tk_cnt > 0) {
fprintf(Debug_File, "...");
fprint_token(Debug_File,
Token_Array[start + first->ps_tk_cnt - 1]);
fprintf(Debug_File, " ");
}
else { /* create same offset as above */
fprintf(Debug_File, " ");
}
for (i = first->ps_tk_cnt; i <= last->ps_tk_cnt; i++) {
fprintf(Debug_File, " ");
fprint_token(Debug_File, Token_Array[start + i]);
}
if (start + last->ps_tk_cnt + 1 < cnk->ch_text->tx_limit) {
fprintf(Debug_File, " ");
fprint_token(Debug_File,
Token_Array[start + last->ps_tk_cnt + 1]);
fprintf(Debug_File, "...");
}
fprintf(Debug_File, "\n");
}
#endif /* DB_RUN */