summaryrefslogtreecommitdiff
path: root/src/tet3/dtet2lib/rescode.c
blob: a9e2e8aebda3a71049f64b90b016a3ad65a2badc (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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
 *      SCCS:  @(#)rescode.c	1.6 (96/11/04) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 X/Open Company Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 *
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/************************************************************************

SCCS:   	@(#)rescode.c	1.6 96/11/04 TETware release 3.3
NAME:		rescode.c
PRODUCT:	TETware
AUTHOR:		David Sawyer, UniSoft Ltd.
DATE CREATED:	August 1992

DESCRIPTION:
	open result codes file and generate table.
	
	return result codes string matching result code etc.

	This file is based on parts of apilib/dresfile.c

MODIFICATIONS:
	Denis McConalogue, UniSoft Limited, August 1993
	changed dtet/ to dtet2/ in #include

	Andrew Dingwall, UniSoft Ltd., December 1993
	added tracing to malloc() calls
	sorted out loop terminator problems in read_codes()

	Andrew Dingwall, UniSoft Ltd., August 1996
	made this file generic and moved it from xresd to here

	Andrew Dingwall, UniSoft Ltd., September 1996
	tet_getrescode() renamed as tet_getresname()
	complementary function tet_getrescode() added

	Geoff Clare, UniSoft Ltd., Sept 1996
	Moved tet_addresult() to here from xresd.

	Aaron Plattner, April 2010
	Fixed warnings when compiled with GCC's -Wall option.

************************************************************************/

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "dtmac.h"
#include "error.h"
#include "restab.h"
#include "dtetlib.h"
#include "tet_api.h"

#ifndef NOTRACE
#include "ltoa.h"
#endif


/* default result code table */
static struct restab restab_dflt[] = {
    { "PASS",           TET_PASS,               0 },
    { "FAIL",           TET_FAIL,               0 },
    { "UNRESOLVED",     TET_UNRESOLVED,         0 },
    { "NOTINUSE",       TET_NOTINUSE,           0 },
    { "UNSUPPORTED",    TET_UNSUPPORTED,        0 },
    { "UNTESTED",       TET_UNTESTED,           0 },
    { "UNINITIATED",    TET_UNINITIATED,        0 },
    { "NORESULT",       TET_NORESULT,           0 },
};

#define Nrestab_dflt	(sizeof restab_dflt / sizeof restab_dflt[0])

static char invalid_result[] = "INVALID RESULT";

/* the result code table, its length and the number of entries in it */
struct restab *tet_restab;
int tet_nrestab;
static int lrestab;

/* structure of an entry in the status code table */
struct statustab {
	char *st_name;	/* status name */
	int st_code;	/* status code */
};

/* default status code table */
static struct statustab tet_statustab[] = {
    { "PASS",     TET_EXIT_SUCCESS      },
    { "FAIL",     TET_EXIT_FAILURE      },
    { "SKIP",     TET_EXIT_SKIP         },
};

#define Ntet_statustab	(sizeof(tet_statustab) / sizeof(tet_statustab[0]))

static char invalid_status[] = "INVALID STATUS";

/* static function declarations */
static void badresline PROTOLIST((char *, int, char *));
static struct restab *getrtbycode PROTOLIST((int));
static struct restab *getrtbyname PROTOLIST((char *));
static char **procline PROTOLIST((char *));
static int rtaddupdate PROTOLIST((struct restab *));


/*
**	tet_getresname() - look up result code in the rescode table;
**		return name if found, otherwise a default string
**
**	if abortflag is not NULL, set *abortflag to true or false depending
**	on whether or not the corresponding action is to abort
*/

char *tet_getresname(result, abortflag)
int result;
int *abortflag;
{
	register struct restab *rtp;
	register char *name;
	register int abrt;

	if (!tet_restab && tet_initrestab() < 0) {
		name = "UNKNOWN";
		abrt = 0;
	}
	else if ((rtp = getrtbycode(result)) == (struct restab *) 0) {
		name = invalid_result;
		abrt = 0;
	}
	else {
		name = rtp->rt_name;
		abrt = rtp->rt_abrt;
	}

	if (abortflag)
		*abortflag = abrt;
	return(name);
}

/*
**	tet_getrescode() - look up result name in the rescode table
**
**	return the result code corresponding to name if found
**	or -1 if not found or an error occurs
**
**	if abortflag is not NULL, set *abortflag to true or false depending
**	on whether or not the corresponding action is to abort
*/

int tet_getrescode(name, abortflag)
char *name;
int *abortflag;
{
	register struct restab *rtp;
	register int code, abrt;

	if (
		(tet_restab || tet_initrestab() == 0) &&
		(rtp = getrtbyname(name)) != (struct restab *) 0
	) {
		code = rtp->rt_code;
		abrt = rtp->rt_abrt;
	}
	else {
		code = -1;
		abrt = 0;
	}

	if (abortflag)
		*abortflag = abrt;
	return(code);
}

/*
**	tet_readrescodes() - read result codes from the tet_code file into
**		the results code table
**
**	return 0 if successful or -1 on error
*/

int tet_readrescodes(fname)
char *fname;
{
	FILE *fp;
	char **argv;
	char buf[BUFSIZ];
	struct restab rtmp;
	register char *p, **ap;
	register int line;
	int rc = 0;

	/* install the default table first time through */
	if (!tet_restab && tet_initrestab() < 0)
		return(-1);

	/* open the result codes file */
	ASSERT(fname && *fname);
	if ((fp = fopen(fname, "r")) == NULL) {
		error(errno, "can't open result code file", fname);
		return(-1);
	}

	/* process each line in the file */
	line = 0;
	while (fgets(buf, sizeof buf, fp) != NULL) {
		line++;
		if (*(argv = procline(buf)) == (char *) 0)
			continue;
		/* establish defaults */
		rtmp.rt_code = 0;
		rtmp.rt_name = invalid_result;
		rtmp.rt_abrt = 0;
		/* process each field */
		for (ap = argv; *ap; ap++)
			switch (ap - argv) {
			case 0:
				/* result code */
				rtmp.rt_code = atoi(*ap);
				break;
			case 1:
				/* quoted result name */
				if (**ap != '"' ||
					*(p = *ap + strlen(*ap) - 1) != '"') {
						badresline("quotes missing",
							line, fname);
						break;
					}
				*p = '\0';
				if ((p = tet_strstore(++*ap)) == (char *) 0) {
					rc = -1;
					break;
				}
				rtmp.rt_name = p;
				for (p = rtmp.rt_name; *p; p++)
					if (*p == '"') {
						badresline("quotes unexpected",
							line, fname);
						break;
					}
				break;
			case 2:
				/* result action indicator */
				if (strcmp(*ap, "Continue") == 0)
					break;
				else if (strcmp(*ap, "Abort") == 0)
					rtmp.rt_abrt = 1;
				else
					badresline("invalid action field",
						line, fname);
				break;
			case 3:
				/* junk field */
				badresline("extra field(s) ignored",
					line, fname);
				break;
			}
		if (rc < 0 || (rc = rtaddupdate(&rtmp)) < 0)
			break;
	}

	(void) fclose(fp);
	return(rc);
}

/*
**	procline() - split a result codes line into fields
**		and return a pointer to a null-terminated array of
**		string field pointers
**
**	double quotes may be used to protect spaces embedded in fields
**	and will be retained in the returned strings
*/

/* number of fields on an input line -
	field 1 = result code
	field 2 = result code name
	field 3 = continue/abort
	field 4 = any trailing junk
*/
#define NFLDS	4

static char **procline(s)
char *s;
{
	static char *argv[NFLDS + 1];
	register char *p, **ap;
	register int argc, new, quote;

	/* strip comments and a trailing newline */
	for (p = s; *p; p++)
		if (*p == '\n' || *p == '#') {
			*p = '\0';
			break;
		}

	/* clear the argv array */
	for (ap = argv; ap < &argv[NFLDS]; ap++)
		*ap = (char *) 0;

	/* split the line into at most NFLDS fields */
	ap = argv;
	argc = quote = 0;
	new = 1;
	for (p = s; *p; p++) {
		if (!quote && isspace(*p)) {
			*p = '\0';
			new = 1;
			continue;
		}
		if (new && argc++ < NFLDS) {
			*ap++ = p;
			new = 0;
		}
		if (*p == '"')
			quote = !quote;
	}

	*ap = (char *) 0;

	return(argv);
}

/*
**	badresline() - complain about a bad results code line
*/

static void badresline(msg, line, file)
char *msg, *file;
int line;
{
	char buf[128];

	(void) sprintf(buf, "%s in line %d, file", msg, line);
	error(0, buf, file);
}

/*
**	rtaddupdate() - add a new entry to the restab or update an existing one
**
**	return 0 if successful or -1 on error
*/

static int rtaddupdate(rtp1)
register struct restab *rtp1;
{
	register struct restab *rtp2;

	if ((rtp2 = getrtbycode(rtp1->rt_code)) == (struct restab *) 0) {
		if (BUFCHK((char **) &tet_restab, &lrestab, (int) ((tet_nrestab + 1) * sizeof *tet_restab)) < 0)
			return(-1);
		*(tet_restab + tet_nrestab++) = *rtp1;
	}
	else {
		ASSERT(rtp2->rt_name);
		if (rtp2->rt_name != invalid_result) {
			TRACE2(tet_Tbuf, 6, "free restab name = %s",
				tet_i2x(rtp2->rt_name));
			free(rtp2->rt_name);
		}
		rtp2->rt_name = rtp1->rt_name;
		rtp2->rt_abrt = rtp1->rt_abrt;
	}

	return(0);
}

/*
**	getrtbycode() - return pointer to restab entry corresponding to
**		the specified result code
**
**	return (struct restab *) 0 if no entry for code can be found
*/

static struct restab *getrtbycode(code)
register int code;
{
	register struct restab *rtp;

	for (rtp = tet_restab; rtp < tet_restab + tet_nrestab; rtp++)
		if (rtp->rt_code == code)
			return(rtp);

	return((struct restab *) 0);
}

/*
**	getrtbyname() - return pointer to restab entry corresponding to
**		the specified result name
**
**	return (struct restab *) 0 if no entry for name can be found
*/

static struct restab *getrtbyname(name)
register char *name;
{
	register struct restab *rtp;

	for (rtp = tet_restab; rtp < tet_restab + tet_nrestab; rtp++)
		if (!strcmp(rtp->rt_name, name))
			return(rtp);

	return((struct restab *) 0);
}

/*
**	tet_initrestab() - copy the default result code values into
**		the result code table
**
**	return 0 if successful or -1 on error
*/

int tet_initrestab()
{
	register struct restab *rtp;
	struct restab rtmp;

	for (rtp = restab_dflt; rtp < restab_dflt + Nrestab_dflt; rtp++) {
		rtmp.rt_code = rtp->rt_code;
		rtmp.rt_abrt = rtp->rt_abrt;
		if ((rtmp.rt_name = tet_strstore(rtp->rt_name)) == (char *) 0 ||
			rtaddupdate(&rtmp) < 0)
				return(-1);
	}

	return(0);
}

/*
**	tet_addresult() - arbitrate between result codes
**
**	return result with highest priority
**
**	this function inplements the logic in tet_tpend() in the
**		(non-distributed) TET
*/

int tet_addresult(lastresult, thisresult)
register int lastresult, thisresult;
{
	if (lastresult < 0)
		return(thisresult);

	switch (thisresult) {
	case TET_PASS:
		/* lowest priority */
		return(lastresult);

	case TET_FAIL:
		/* highest priority */
		return(thisresult);

	case TET_UNRESOLVED:
	case TET_UNINITIATED:
		/* high priority */
		switch (lastresult) {
		case TET_FAIL:
			return(lastresult);
		default:
			return(thisresult);
		}

	case TET_NORESULT:
		/* output by tet_result() for invalid result codes,
		   and so must supersede everything that isn't some
		   sort of definite failure */
		switch (lastresult) {
		case TET_FAIL:
		case TET_UNRESOLVED:
		case TET_UNINITIATED:
			return(lastresult);
		default:
			return(thisresult);
		}

	case TET_UNSUPPORTED:
	case TET_NOTINUSE:
	case TET_UNTESTED:
		/* low priority */
		switch (lastresult) {
		case TET_PASS:
			return(thisresult);
		default:
			return(lastresult);
		}

	default:
		/* user-supplied codes: middle priority */
		switch (lastresult) {
		case TET_PASS:
		case TET_UNSUPPORTED:
		case TET_NOTINUSE:
		case TET_UNTESTED:
			return(thisresult);
		default:
			return(lastresult);
		}
	}
}

/*
**	tet_getstatusname() - look up status code in the status table;
**		return name if found, otherwise a default string
*/

char *tet_getstatusname(status)
int status;
{
	register struct statustab *stp;

	for (stp = tet_statustab; stp < tet_statustab + Ntet_statustab; stp++)
		if (stp->st_code == status)
			return stp->st_name;
	return invalid_status;
}

/*
**	tet_resulttostatus() - convert result code to exit status
**
**	return exit status corresponding to result code
*/

int tet_resulttostatus(result)
int result;
{
	switch (result) {
	case TET_PASS:
		return TET_EXIT_SUCCESS;
	case TET_FAIL:
	case TET_UNRESOLVED:
	case TET_UNINITIATED:
		return TET_EXIT_FAILURE;
	case TET_NOTINUSE:
	case TET_UNSUPPORTED:
	case TET_UNTESTED:
		return TET_EXIT_SKIP;
	default:
		/* Pass everything else (include TET_NORESULT) */
		return result;
	}
}

/*
**	tet_addstatus() - arbitrate between exit status codes
**
**	return status with highest priority
*/

int tet_addstatus(laststatus, thisstatus)
register int laststatus, thisstatus;
{
	if (laststatus < 0)
		return(thisstatus);

	switch (thisstatus) {
	case TET_EXIT_SUCCESS:
		/* lowest priority */
		return(laststatus);

	case TET_EXIT_FAILURE:
		/* highest priority */
		return(thisstatus);

	case TET_EXIT_SKIP:
		/* low priority */
		switch (laststatus) {
		case TET_EXIT_SUCCESS:
			return(thisstatus);
		default:
			return(laststatus);
		}

	default:
		/* user-supplied codes: high priority */
		switch (laststatus) {
		case TET_EXIT_FAILURE:
			return(laststatus);
		default:
			return(thisstatus);
		}
	}
}