Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
output.cpp
Go to the documentation of this file.
1
27#include <assert.h>
28#include <errno.h>
29#include <stdio.h> /* for printf, puts, FILE, f* */
30#include <stdlib.h> /* for exit, free */
31#include <string.h> /* for str* */
32
33#include <sys/types.h>
34
35#include <sys/stat.h>
36
37#include "cmdopt.h"
38#include "display.h"
39#include "util.h"
40
41#include "output.h"
42
43#ifndef ENOENT
44# define ENOENT 2
45#endif /* ENOENT */
46
47
58static void
59check_test (FILE* data, struct target_status* status)
60{
61 unsigned r_lvl = 0; /* diagnostic severity level */
62 unsigned r_active = 0; /* number of active diagnostics */
63 unsigned r_total = 0; /* total number of diagnostics */
64
65 int fmt_ok = 0; /* is output format okay? */
66
67 unsigned fsm = 0; /* state */
68 char tok; /* current character */
69
70 const char* const target_name = get_target ();
71
72 assert (0 != target_name);
73 assert (0 != data);
74 assert (0 != status);
75
76 tok = fgetc (data);
77
78 if (feof (data)) {
79 /* target produced no output (regression test?) */
80 status->status = ST_NO_OUTPUT;
81 return;
82 }
83
84 for ( ; fsm < 6 && !feof (data); tok = fgetc (data)) {
85 switch (tok) {
86 case '\n':
87 fsm = 1;
88 break;
89 case '#':
90 fsm = (1 == fsm) ? 2 : 0;
91 break;
92 case ' ':
93 fsm = (2 == fsm) ? 3 : ((4 == fsm) ? 5 : 0);
94 break;
95 case '|':
96 fsm = (3 == fsm) ? 4 : 0;
97 break;
98 case '(':
99 if (5 == fsm) {
100 fseek (data, -6, SEEK_CUR);
101 fsm++;
102 break;
103 }
104 default:
105 fsm = 0;
106 }
107 }
108
109 if (!feof (data)) {
110 while (3 == fscanf (data, "# | (S%u) %*s | %u | %u | %*u%% |\n",
111 &r_lvl, &r_active, &r_total)) {
112 if (6 < r_lvl) {
113 /* The 0.new tests produces errors, and are all
114 expected to be active, so invert the total */
115 if (8 == r_lvl && 0 == strcmp (target_name, "0.new"))
116 r_active = r_total-r_active;
117 status->failed += r_active;
118 status->assert += r_total;
119 if ( status->failed < r_active
120 || status->assert < r_total
121 || (unsigned int)-1 == status->assert) {
122 status->status = ST_OVERFLOW;
123 return;
124 }
125 }
126 else if (1 < r_lvl) {
127 status->t_warn += r_active;
128 if (status->t_warn < r_active) {
129 status->t_warn = (unsigned int)-1;
130 }
131 }
132 fmt_ok = 1;
133 }
134 }
135
136 if (!fmt_ok)
137 status->status = ST_FORMAT;
138}
139
150static void
151check_compat_test (FILE* data, struct target_status* status)
152{
153 int read = 0;
154 unsigned fsm = 0;
155 char tok;
156
157 assert (0 != data);
158 assert (0 != status);
159
160 tok = fgetc (data);
161
162 if (feof (data)) {
163 /* target produced no output (regression test?) */
164 status->status = ST_NO_OUTPUT;
165 return;
166 }
167
168 for ( ; !feof (data); tok = fgetc (data)) {
169 switch (tok) {
170 case '\n':
171 fsm = 1;
172 break;
173 case '#':
174 if (1 == fsm || 2 == fsm)
175 ++fsm;
176 else
177 fsm = 0;
178 break;
179 case ' ':
180 if (3 == fsm)
181 ++fsm;
182 else
183 fsm = 0;
184 break;
185 case 'W':
186 if (4 == fsm && !feof (data)) /* leading "## W" eaten */
187 read = fscanf (data, "arnings = %u\n## Assertions = %u\n"
188 "## FailedAssertions = %u",
189 &status->t_warn, &status->assert, &status->failed);
190 default:
191 fsm = 0;
192 }
193 }
194 if (3 != read) {
195 status->status = ST_FORMAT;
196 }
197}
198
204#define DELTA_BUF_LEN 64
205
225static void
226check_example (const char* const data_dir, char* const out_name, FILE* fout,
227 struct target_status* status)
228{
229 struct stat file_info;
230 char* ref_name;
231
232 FILE* fref; /* reference file (expected output) */
233
234 assert (0 != out_name);
235 assert (0 != fout);
236 assert (0 != status);
237
238 /* Mark as an example by setting assertions to -1 */
239 status->assert = (unsigned)-1;
240
241 /* Try data_dir/manual/out/target_name.out */
242 ref_name = reference_name (data_dir, "manual", "out");
243
244 if (0 > stat (ref_name, &file_info)) {
245 if (ENOENT != errno) {
246 warn ("stat (%s) error: %s\n", exe_name, ref_name,
247 strerror (errno));
248 status->status = ST_BAD_REF;
249 free (ref_name);
250 return;
251 }
252
253 /* If that doesn't exist, try
254 data_dir/tutorial/out/target_name.out */
255 free (ref_name);
256 ref_name = reference_name (data_dir, "tutorial", "out");
257
258 if (0 > stat (ref_name, &file_info)) {
259 if (ENOENT != errno) {
260 warn ("stat (%s) error: %s\n", exe_name, ref_name,
261 strerror (errno));
262 status->status = ST_BAD_REF;
263 }
264 else
265 status->status = ST_NO_REF;
266
267 free (ref_name);
268 return;
269 }
270 }
271
272 fref = fopen (ref_name, "r");
273
274 if (0 == fref) {
275 int cache = errno; /* caching errno, as puts could alter it */
276 if (ENOENT != cache)
277 warn ("Error opening %s: %s\n", ref_name, strerror (cache));
278 status->status = ST_BAD_REF;
279 }
280 else {
281 int match = 1; /* do the two files match? */
282
283 while (!feof (fref) && !feof (fout)) {
284
285 char buf [2][DELTA_BUF_LEN];
286
287 size_t nread [2]; /* bytes read from the output/ref file */
288
289 /* First, read a block from the files into the buffer */
290 nread [0] = fread (buf [0], 1, sizeof buf [0], fout);
291 if (ferror (fout)) {
292 warn ("Error reading %s: %s\n", out_name, strerror (errno));
293 match = 0;
294 break;
295 }
296
297 nread [1] = fread (buf [1], 1, sizeof buf [1], fref);
298 if (ferror (fref)) {
299 warn ("Error reading %s: %s\n", ref_name, strerror (errno));
300 match = 0;
301 break;
302 }
303
304 /* Then, check if the amount of data read or the state of the
305 files differs
306 */
307 if (nread [0] != nread [1] || feof (fref) != feof (fout)) {
308 match = 0;
309 break;
310 }
311
312 /* Finally, check if the contents of the buffers differ */
313 if (0 != memcmp (buf [0], buf [1], nread [0])) {
314 match = 0;
315 break;
316 }
317 }
318
319 if (!match)
320 status->status = ST_BAD_OUTPUT;
321
322 fclose (fref);
323 }
324 free (ref_name);
325}
326
327void
328parse_output (const struct target_opts* options, struct target_status* status)
329{
330 char* out_name;
331 FILE* data;
332
333 assert (0 != status);
334 assert (0 != options->argv [0]);
335 out_name = output_name (options->argv [0]);
336
337 data = fopen (out_name, "r");
338
339 if (0 == data) {
340 if (ENOENT != errno)
341 warn ("Error opening %s: %s\n", out_name, strerror (errno));
342 status->status = ST_NO_OUTPUT;
343 }
344 else {
345 if (0 == options->data_dir || '\0' == *options->data_dir) {
346 /* If there is not an input directory, look at the assertion tags */
347
348 if (!options->compat)
349 check_test (data, status);
350 else
351 check_compat_test (data, status);
352 }
353 else {
354 /* Otherwise, diff against the output file */
355 check_example (options->data_dir, out_name, data, status);
356 }
357 fclose (data);
358 }
359 free (out_name);
360}
#define buf
Definition 17.names.cpp:452
const char *const data[]
#define assert(expr)
Definition _cassert.h:40
#define FILE
#define SEEK_CUR
const char * get_target()
Definition runall.cpp:411
#define ENOENT
Definition output.cpp:44
#define DELTA_BUF_LEN
Definition output.cpp:204
void parse_output(const struct target_opts *options, struct target_status *status)
Definition output.cpp:328
char ** argv
Definition target.h:76
const char * data_dir
Definition target.h:78
int compat
Definition target.h:83
enum ProcessStatus status
Definition target.h:123
unsigned t_warn
Definition target.h:129
unsigned assert
Definition target.h:130
unsigned failed
Definition target.h:131
@ ST_OVERFLOW
Definition target.h:109
@ ST_BAD_OUTPUT
Definition target.h:107
@ ST_FORMAT
Definition target.h:108
@ ST_NO_OUTPUT
Definition target.h:104
@ ST_BAD_REF
Definition target.h:106
@ ST_NO_REF
Definition target.h:105
const char * exe_name
Definition cmdopt.cpp:55
char * reference_name(const char *data_dir, const char *subdir, const char *mode)
Definition util.cpp:132
char * output_name(const char *target)
Definition util.cpp:227
void warn(const char *const format,...)
Definition util.cpp:58