summaryrefslogtreecommitdiff
path: root/src/tet3/tcc/scen1.c
blob: 8abba13bb056665d84e0c538405fe10470351219 (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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
/*
 *	SCCS: @(#)scen1.c	1.8 (98/09/01)
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1996 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.
 * A copy of the end-user licence agreement is contained in the file
 * Licence which accompanies this distribution.
 * 
 * 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:   	@(#)scen1.c	1.8 98/09/01 TETware release 3.3
NAME:		scen1.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	August 1996

DESCRIPTION:
	scenario parser stage 1 - tokenise the scenario file

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., March 1997
	allow leading white space on lines read from a scenario include file

	Andrew Dingwall, UniSoft Ltd., March 1998
	Added support for number ranges in directive arguments.
	Increased the number of arguments that a directive may take.
	Permit a number range to appear as an argument to a remote or
	distributed directive, so as to avoid the need to handle long
	scenario lines and large numbers of arguments when many system
	IDs are specified.

	Andrew Dingwall, UniSoft Ltd., July 1998
	Added support for generic scenario file preprocessing,
	and support for the %include scenario keyword.
	Functions previously in getline_tcc.c moved to here, and enhanced to
	support multiple input files.

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

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

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include "dtmac.h"
#include "error.h"
#include "ltoa.h"
#include "llist.h"
#include "bstring.h"
#include "scentab.h"
#include "dirtab.h"
#include "dtetlib.h"
#include "tcc.h"

/* some limits which apply to directives */
/* max number of semicolon-delimited fields in group execution */
#define MAXFLDS	(LBUFLEN / 8)
#if MAXFLDS < 10
#  define MAXFLDS	10
#endif
/* max number of comma-delimited args in a single directive */
#define MAXARGS	(LBUFLEN / 4)
#if MAXARGS < 10
#  define MAXARGS	10
#endif

/* is this an attached test list or file name? */
#define isattached(s)	(*(s) && !isspace(*(s)))

/*
** structure of the line cache stack used by getline_tcc() and ungetline_tcc()
** this is a linked list
** the lc_next and lc_last elements must be 1st and 2nd so as to
** enable the stack to be manipulated by the llist functions
*/
struct lcache {
        struct lcache *lc_next;		/* ptr to next element in the list */
        struct lcache *lc_last;		/* ptr to prev element in the list */
        char *lc_line;          	/* the stored input line */
};

/*
** Structure of the input file stack.
** This is a linked list.
** The if_next and if_last elements must be 1st and 2nd so as to enable
** the stack to be manipulated by the llist functions.
**
** An input file stack element is allocated for each scenario file that
** is being read.
** Initially, an element is allocated for the tet_scen file, which is already
** open when proc1scfile() is called..
** If a %include keyword is encountered, a new element is allocated for
** the specified file and is pushed on to the stack.
** Thus the file to be %include'd becomes the current input file.
** The stack is unwound when EOF is encountered at each level.
*/
struct ifstack {
	struct ifstack *if_next;	/* ptr to next element in the list */
	struct ifstack *if_last;	/* ptr to prev element in the list */
	char *if_fname;			/* file name */
	FILE *if_fp;			/* stdio stream pointer */
	int if_lcount;			/* number of lines read from file */
	struct lcache *if_lcache;	/* line cache for this file */
};

/* the input file stack itself */
static struct ifstack *ifstack;

/*
** pointer to the currently active input file stack element;
** that is: the element in the stack where getline_tcc() and ungetline_tcc()
** fetch and store input lines
**
** normally, the currently active element is at the top of the stack;
** however, if there is more than one level in the stack and ungetline_tcc() is
** called to push back more lines than have been read from the file at any
** particular level, the next level down becomes the currently active level
** and lines are cached and retrieved from there
*/
static struct ifstack *ifstp;


/* static function declarations */
static int find1scen PROTOLIST((void));
static char *getline_tcc PROTOLIST((void));
static void includefile PROTOLIST((char *, char *, int));
static struct ifstack *ifsalloc PROTOLIST((void));
static void ifsfree PROTOLIST((struct ifstack *));
static struct ifstack *ifspop PROTOLIST((void));
static void ifspush PROTOLIST((struct ifstack *));
static int isnumrange PROTOLIST((char *, int *, int *));
static int iszorpnum PROTOLIST((char *));
static struct lcache *lcalloc PROTOLIST((void));
static void lcfree PROTOLIST((struct lcache *));
static struct lcache *lcpop PROTOLIST((void));
static struct lcache *lcpop2 PROTOLIST((struct ifstack *));
static void lcpush PROTOLIST((struct lcache *));
static int ppinclude PROTOLIST((char *));
static int preprocess PROTOLIST((char *));
static int proc1dgrp PROTOLIST((char *, char *, char *, int));
static void proc1scelem PROTOLIST((char *, int, int, int, char *));
static int proc1scen PROTOLIST((void));
static int proc1scline PROTOLIST((char *, char *, int));
static void ungetline_tcc PROTOLIST((char *));
#ifndef NOTRACE
static char *firstpart PROTOLIST((char *));
#endif


/*
**	proc1scfile() - tokenise a scenario input file
**
**	return 0 if successful or -1 on error
*/

int proc1scfile(fp, fname)
FILE *fp;
char *fname;
{
	struct ifstack *ifp;
	int rc;

	TRACE2(tet_Tscen, 1, "proc1scfile(): tokenising scenario file %s",
		fname);

	/* store the top level file details on the input file stack */
	ifp = ifsalloc();
	ifp->if_fname = fname;
	ifp->if_fp = fp;
	ifspush(ifp);

	/* tokenise each scenario in turn */
	do {
		if ((rc = find1scen()) > 0)
			while ((rc = proc1scen()) > 0)
				;
	} while (rc > 0);

	/*
	** close all files except the top level one
	**
	** normally, files are closed as the input file stack is unwound -
	** files only get closed here if one of the lower level functions
	** returned abruptly after encountering an error consition
	*/
	ifstp = ifstack;
	while (ifstack) {
		if (ifstack->if_next && ifstack->if_fp)
			(void) fclose(ifstack->if_fp);
		ifsfree(ifspop());
	}

	return(rc);
}

/*
**	find1scen() - search for the start of a scenario in the input file
**
**	return	 1 if found
**		 0 on EOF
**		-1 if processing of this file must be abandoned
*/

static int find1scen()
{
	char *line;
	int n, skipstart = 0;
	static char fmt[] = "found a scenario after skipping %d line%s";
	char msg[sizeof fmt + LNUMSZ];

	TRACE1(tet_Tscen, 4, "find1scen(): find start of scenario");

	/*
	** find the start of the next scenario -
	**	skip lines until it is found -
	**	when the start is found, push the line back so that
	**	the scenario processor can find it
	*/
	for (;;) {
		if ((line = getline_tcc()) == (char *) 0)
			return(ferror(ifstp->if_fp) ? -1 : 0);
		if (!isspace(*line)) {
			/* start of new scenario */
			if (skipstart) {
				n = ifstp->if_lcount - skipstart;
				(void) sprintf(msg, fmt, n, n == 1 ? "" : "s");
				scenermsg(msg, (char *) 0, ifstp->if_lcount,
					ifstp->if_fname);
			}
			ungetline_tcc(line);
			return(1);
		}
		/*
		** here if we are skipping lines -
		**	emit a warning first time through
		*/
		if (!skipstart) {
			skipstart = ifstp->if_lcount;
			scenerror("line(s) outside of a scenario ignored",
				(char *) 0, skipstart, ifstp->if_fname);
		}
	}
}

/*
**	proc1scen() - tokenise a scenario once the input stream is
**		correctly positioned
**
**	return	 1 if successful and the input stream is still
**			correctly positioned
**		 0 if successful and the input stream is not
**			correctly positioned (in practice this means EOF)
**		-1 if processing of this file must be abandoned
*/

static int proc1scen()
{
	char *line, *next;
	register char *p;
	register struct scentab *ep;

	/* read the scenario name - starts in column 1 */
	line = getline_tcc();
	ASSERT(line);
	ASSERT(!isspace(*line));

	/* see if there are scenario elements after the name */
	for (p = line; *p; p++)
		if (isspace(*p)) {
			*p++ = '\0';
			break;
		}
	next = p;

	TRACE3(tet_Tscen, 4, "proc1scen(): new scenario = <%s>, rest = <%s>",
		line, next);

	/* store the scenario header token */
	ep = scalloc();
	ep->sc_type = SC_SCENARIO;
	ep->sc_scenario = rstrstore(line);
	ep->sc_lineno = ifstp->if_lcount;
	ep->sc_fname = ifstp->if_fname;
	scpush(ep, &sclist);

	/* process any elements on the same line as the header */
	if (proc1scline(next, ifstp->if_fname, ifstp->if_lcount) < 0)
		return(-1);

	/* process the rest of the current scenario */
	while ((line = getline_tcc()) != (char *) 0) {
		if (!isspace(*line)) {
			/* a new scenario */
			ungetline_tcc(line);
			return(1);
		}
		/* process a line in the current scenario */
		if (proc1scline(line, ifstp->if_fname, ifstp->if_lcount) < 0)
			return(-1);
	}

	return(ferror(ifstp->if_fp) ? -1 : 0);
}

/*
**	proc1cmdline() - tokenise a scenario line specified with
**		the -l command-line option
**
**	return 0 if successful or -1 if processing must be abandoned
*/

int proc1cmdline(line)
char *line;
{
	static char cmdline[] = "<command-line>";
	static int lineno;
	struct scentab *ep;

	/* start a new (default) scenario first time through */
	if (!lineno++) {
		ep = scalloc();
		ep->sc_type = SC_SCENARIO;
		ep->sc_scenario = "all";
		ep->sc_lineno = lineno;
		ep->sc_fname = cmdline;
		scpush(ep, &sclist);
	}

	/* process the line and return */
	return(proc1scline(line, cmdline, lineno));
}

/*
**	proc1scline() - tokenise a line in the current scenario
**
**	return 0 if successful or -1 if processing of this file must be
**		abandoned
*/

static int proc1scline(line, fname, lineno)
char *line, *fname;
int lineno;
{
	register char *p;
	int type, flags;
	char *next;
	char delim;
	static char msg[2];

	/* skip leading spaces */
	while (*line && isspace(*line))
		line++;

	TRACE2(tet_Tscen, 4, "proc1scline(): line = <%s>", line);

	/* determine the element type and how it is delimited */
	flags = 0;
	switch (*line) {
	case '\0':	/* an empty line */
		return(0);
	case '"':	/* a scenario information line */
		type = SC_SCENINFO;
		delim = *line++;
		break;
	case '^':	/* a referenced scenario name */
		type = SC_SCEN_NAME;
		delim = ' ';
		line++;
		break;
	case ':':	/* a scenario directive */
		type = SC_DIRECTIVE;
		delim = *line++;
		break;
	default:	/* treat everything else as a testcase for now -
				we'll check for valid testcase names later */
		type = SC_TESTCASE;
		delim = ' ';
		break;
	}

	/* search for the required delimiter */
	for (p = line; *p; p++)
		if (*p == delim || (delim == ' ' && isspace(*p)))
			break;

	/* see if it was found */
	if (!*p && !isspace(delim)) {
		msg[0] = delim;
		scenerror("unmatched delimiter", msg, lineno, fname);
		return(0);
	}

	/* terminate this element, remember start of next element */
	if (*p)
		*p++ = '\0';
	next = p;

	TRACE3(tet_Tscen, 4, "proc1scline(): element = <%s>, rest = <%s>",
		line, next);

	/* process this element */
	switch (type) {
	case SC_TESTCASE:
	case SC_SCEN_NAME:
		if (!*(line + 1)) {
			scenerror("empty test case or scenario name",
				line, lineno, fname);
			break;
		}
		/* else fall through */
	case SC_SCENINFO:
		proc1scelem(line, type, flags, lineno, fname);
		break;
	case SC_DIRECTIVE:
		return(proc1dgrp(line, next, fname, lineno));
	default:
		/* this "can't happen" .*/
		fatal(0, "unexpected type", prsctype(type));
		/* NOTREACHED */
	}

	/* process the rest of the current line */
	return(proc1scline(next, fname, lineno));
}

/*
**	proc1scelem() - process a single (non-directive) scenario element
*/

static void proc1scelem(element, type, flags, lineno, fname)
char *element, *fname;
int type, flags, lineno;
{
	register struct scentab *ep;
	register char *p;
	char *iclist, *tcname;
	int lsceninfo;

	TRACE2(tet_Tscen, 4, "proc1scelem(): element = <%s>", element);

	/* allocate a scenario element and fill it in */
	ep = scalloc();
	ep->sc_type = type;
	ep->sc_flags = flags;
	ep->sc_lineno = lineno;
	ep->sc_fname = fname;

	switch (type) {
	case SC_SCENINFO:
		lsceninfo = 0;
		RBUFCHK(&ep->sc_sceninfo, &lsceninfo,
			(int) strlen(element) + 3);
		(void) sprintf(ep->sc_sceninfo, "\"%s\"", element);
		break;
	case SC_TESTCASE:
		tcname = element;
		iclist = (char *) 0;
		for (p = element; *p; p++)
			if (*p == '{') {
				*p++ = '\0';
				for (iclist = p; *p; p++)
					if (*p == '}') {
						*p = '\0';
						break;
					}
				break;
			}
		ep->sc_tcname = rstrstore(tcname);
		if (iclist && *iclist)
			ep->sc_sciclist = rstrstore(iclist);
		ep->sc_exiclist = ep->sc_sciclist;
		break;
	case SC_SCEN_NAME:
		ep->sc_scen_name = rstrstore(element);
		break;
	default:
		/* this "can't happen" */
		fatal(0, "unexpected type", prsctype(type));
		/* NOTREACHED */
	}

	/* then add the element to the list */
	scpush(ep, &sclist);
}

/*
**	proc1dgrp() - process a group of scenario directives;
**		i.e., something between a pair of ':' delimiters
**
**	return 0 if successful or -1 if processing of this file must be
**		abandoned
*/

static int proc1dgrp(dgroup, next, fname, lineno)
char *dgroup, *next, *fname;
int lineno;
{
	char buf[LBUFLEN];
	char *dirs[MAXFLDS];
	int ndirs;
	char **dirp;
	char *args[MAXARGS];
	int nargs;
	register char **ap;
	register struct scentab *ep;
	register struct dirtab *dp;
	char **vp;
	char *line;
	int istart, iend, ok, rc;
	register char *p1, *p2;
	struct scentab *scstack = (struct scentab *) 0;
#ifndef TET_LITE	/* -START-LITE-CUT- */
	int *ip, nsys, syslen;
#endif /* !TET_LITE */	/* -END-LITE-CUT- */

	TRACE3(tet_Tscen, 4,
		"proc1dgrp(): directive group = <%s>, rest = <%s>",
		dgroup, next);

	/* now the colons have gone - split a group-execution directive
		into semicolon-delimited fields */
	ndirs = split(dgroup, dirs, MAXFLDS, ';');

	/* trim leading spaces from each directive */
	for (dirp = dirs; dirp < &dirs[ndirs]; dirp++)
		while (**dirp && isspace(**dirp))
			++*dirp;

#ifndef NOTRACE
	TRACE2(tet_Tscen, 4, "group contains %s directive(s)", tet_i2a(ndirs));
	if (tet_Tscen >= 5)
		for (dirp = dirs; dirp < &dirs[ndirs]; dirp++)
			TRACE3(tet_Tscen, 5, "\tdirective %s = <%s>",
				tet_i2a((dirp - dirs) + 1), *dirp);
#endif

	/*
	**	check the syntax of the directive group
	*/

#ifndef NOTRACE
	if (isattached(next))
		TRACE2(tet_Tscen, 4,
			"directive group has attached items: <%s>",
			next);
	else
		TRACE1(tet_Tscen, 4, "directive group has no attached items");
#endif

	/* check the syntax of each directive in the group
		using the rules in the directives table */
	TRACE1(tet_Tscen, 4, "starting directive syntax check");
	ok = 1;
	for (dirp = dirs; dirp < &dirs[ndirs]; dirp++) {
		/* count the number of comma-separated arguments
			associated with this directive */
		(void) strcpy(buf, *dirp);
		args[0] = "";
		nargs = split(buf, args, MAXARGS, ',') - 1;

		/* here, args[0] is the directive,
			nargs is the number of arguments
			args[1] -> args[nargs] are the arguments */

		/* trim leading spaces from each argument */
		for (ap = &args[1]; ap <= &args[nargs]; ap++)
			while (**ap && isspace(**ap))
				++*ap;

#ifndef NOTRACE
		TRACE3(tet_Tscen, 6,
			"syntax checking <%s> with %s argument(s)",
			args[0], tet_i2a(nargs));
		if (tet_Tscen >= 7)
			for (ap = &args[1]; ap <= &args[nargs]; ap++)
				TRACE3(tet_Tscen, 7, "\targ %s = <%s>",
					tet_i2a(ap - args), *ap);
#endif

		/* look up the directive in the table */
		if ((dp = getdirbyname(args[0])) == (struct dirtab *) 0) {
			if (!*dirp)
				*dirp = "<empty>";
			scenerror("unknown/unsupported directive",
				*dirp, lineno, fname);
			ok = 0;
			continue;
		}

		/* check for optional and mandatory arguments */
		if (
			nargs == 0 &&
			(dp->dt_flags & (SDF_NEED_ARG | SDF_NEED_MARG))
		) {
			scenerror(args[0], "needs an argument",
				lineno, fname);
			ok = 0;
		}
		else if (
			nargs > 1 &&
			(dp->dt_flags & (SDF_NEED_ARG | SDF_OPT_ARG))
		) {
			scenerror(args[0], "has too many arguments",
				lineno, fname);
			ok = 0;
		} else if (
			nargs > 0 &&
			!(dp->dt_flags & (SDF_OPT_ARG | SDF_OPT_MARG |
				SDF_NEED_ARG | SDF_NEED_MARG))
		) {
			scenerror(args[0], "should not have arguments",
				lineno, fname);
			ok = 0;
		}

		/* check for non-numeric arguments */
		if (dp->dt_flags & SDF_NUMERIC_ARGS)
			for (ap = &args[1]; ap <= &args[nargs]; ap++)
				if (!iszorpnum(*ap)) {
					scenerror(args[0],
						"argument is not a +ve number",
						lineno, fname);
					ok = 0;
					break;
				}

		/* check for correctly formatted number range arguments */
		if (dp->dt_flags & SDF_NRANGE_ARGS)
			for (ap = &args[1]; ap <= &args[nargs]; ap++) {
				istart = iend = 0;
				if (
					!iszorpnum(*ap) &&
					!isnumrange(*ap, &istart, &iend)
				) {
					scenerror(args[0],
			"argument is not a +ve number or number range",
						lineno, fname);
					ok = 0;
					break;
				}
				else if (istart > iend) {
					scenerror(*ap,
			": start is greater than end in number range",
						lineno, fname);
					ok = 0;
				}
			}

		/* check for "variable" format arguments */
		if (dp->dt_flags & SDF_VARFMT_ARGS)
			for (ap = &args[1]; ap <= &args[nargs]; ap++) {
				p1 = tet_remvar(*ap, -1);
				p2 = tet_equindex(*ap);
				if (!p1 || !p2) {
					scenerror(*ap,
			"is not a properly formatted variable assignment",
						lineno, fname);
					ok = 0;
					break;
				}
				else if (!strncmp(p1, "TET_", 4)) {
					*p2 = '\0';
					scenerror(p1,
			"value may not be assigned in a scenario file",
						lineno, fname);
					ok = 0;
					break;
				}
			}

		/* check for optional and mandatory attached elements */
		if (isattached(next)) {
			if ((dp->dt_flags & (SDF_OPT_ATTACH | SDF_NEED_ATTACH)) == 0) {
				scenerror(args[0],
					"should not have an attached element",
					lineno, fname);
				ok = 0;
			}
		}
		else {
			if (dp->dt_flags & SDF_NEED_ATTACH) {
				scenerror(args[0],
					"needs an attached element",
					lineno, fname);
				ok = 0;
			}
		}
	}

	TRACE2(tet_Tscen, 6, "directive syntax check was %ssuccessful",
		ok ? "" : "un");

	if (!ok) {
		TRACE1(tet_Tscen, 4, "proc1dgrp(): return 0");
		return(0);
	}

	/* process each of the directives in turn */
	for (dirp = dirs; dirp < &dirs[ndirs]; dirp++) {

		TRACE2(tet_Tscen, 4, "process directive <%s>", *dirp);

		/* separate the directive from its arguments */
		nargs = split(*dirp, args, MAXARGS, ',') - 1;

		/* trim leading spaces from each argument */
		for (ap = &args[1]; ap <= &args[nargs]; ap++)
			while (**ap && isspace(**ap))
				++*ap;

		/* here, args[0] is the directive,
			nargs is the number of arguments
			args[1] -> args[nargs] are the arguments */

#ifndef NOTRACE
		TRACE3(tet_Tscen, 4, "directive = <%s> has %s argument(s)",
			args[0], tet_i2a(nargs));
		if (tet_Tscen >= 5)
			for (ap = &args[1]; ap <= &args[nargs]; ap++)
				TRACE3(tet_Tscen, 7, "\targ %s = <%s>",
					tet_i2a(ap - args), *ap);
#endif

		/* look the directive up in the table */
		dp = getdirbyname(args[0]);
		ASSERT(dp);

		/* an INCLUDE directive does not go in the list -
		**	in dtet mode it is just a tag on which
		**	to hang a filename;
		**	in etet mode it is a no-op
		*/
		if (dp->dt_directive == SD_INCLUDE)
			continue;

		/* allocate a scenario element and fill it in */
		ep = scalloc();
		ep->sc_type = SC_DIRECTIVE;
		ep->sc_directive = dp->dt_directive;
		ap = &args[1];
		switch (ep->sc_directive) {
		case SD_PARALLEL:
		case SD_REPEAT:
			ASSERT(nargs == 0 || nargs == 1);
			if (nargs == 0)
				ep->sc_count = 1;
			else
				ep->sc_count = atoi(*ap);
			break;
		case SD_TIMED_LOOP:
			ASSERT(nargs == 1);
			ep->sc_seconds = atol(*ap);
			break;
		case SD_VARIABLE:
			ASSERT(nargs >= 0);
			if ((ep->sc_vars = (char **) calloc((unsigned) TET_MAX(nargs, 1), sizeof *ep->sc_vars)) == (char **) 0)
				fatal(errno, "can't allocate memory for variable list", (char *) 0);
			TRACE2(tet_Tbuf, 6, "allocate variable list = %s",
				tet_i2x(ep->sc_vars));
			ep->sc_nsys = nargs;
			for (vp = ep->sc_vars; nargs > 0; nargs--)
				*vp++ = rstrstore(*ap++);
			errno = 0;
			break;
#ifndef TET_LITE	/* -START-LITE-CUT- */
		case SD_REMOTE:
		case SD_DISTRIBUTED:
			ASSERT(nargs > 0);
			ep->sc_nsys = 0;
			syslen = 0;
			for (; nargs > 0; nargs--, ap++) {
				nsys = 1;
				if (isnumrange(*ap, &istart, &iend))
					nsys += iend - istart;
				else
					istart = atoi(*ap);
				RBUFCHK((char **) &ep->sc_sys, &syslen, (int) (sizeof *ep->sc_sys * (ep->sc_nsys + nsys)));
				ip = ep->sc_sys + ep->sc_nsys;
				ep->sc_nsys += nsys;
				while (--nsys >= 0)
					*ip++ = istart++;
			}
			break;
#endif /* !TET_LITE */	/* -END-LITE-CUT- */
		case SD_RANDOM:
		case SD_END_PARALLEL:
		case SD_END_REPEAT:
		case SD_END_RANDOM:
		case SD_END_TIMED_LOOP:
		case SD_END_VARIABLE:
#ifndef TET_LITE	/* -START-LITE-CUT- */
		case SD_END_REMOTE:
		case SD_END_DISTRIBUTED:
#endif /* !TET_LITE */	/* -END-LITE-CUT- */
			ASSERT(nargs == 0);
			break;
		default:
			/* this "can't happen" */
			fatal(0, "unexpected directive",
				prscdir(dp->dt_directive));
			/* NOTREACHED */
		}
		ep->sc_fname = fname;
		ep->sc_lineno = lineno;

		/* store the directive */
		scpush(ep, &sclist);

		/*
		** if there is an attached element, we will need a matching
		** end* directive once the attached element is processed
		*/
		if (isattached(next) && (dp->dt_flags & SDF_ENDDIR)) {
			dp = getdirbyvalue(dp->dt_match);
			ASSERT(dp);
			ep = scalloc();
			ep->sc_type = SC_DIRECTIVE;
			ep->sc_directive = dp->dt_directive;
			ep->sc_flags = SCF_IMPLIED;
			ep->sc_fname = fname;
			ep->sc_lineno = lineno;
			scpush(ep, &scstack);
		}
	}

	/*
	** here we must decide what to do with the rest of the line -
	**
	** if the thing after the directive(s) is a space, there are no
	** implied end* directives and the rest of the line can be
	** processed separately
	*/

	if (!isattached(next)) {
		ASSERT(scstack == (struct scentab *) 0);
		TRACE1(tet_Tscen, 4,
			"no attached items - process rest of line");
		rc = proc1scline(next, fname, lineno);
		TRACE2(tet_Tscen, 4, "proc1dgrp(): RETURN %s", tet_i2a(rc));
		return(rc);
	}

	/*
	** but if the thing after the directive(s) is attached
	** we must determine the nature of the attached element
	** which could be one of the follwing:
	**
	**	/include-file-name
	**	@/test-case-name
	**	^scenario-name
	*/

	/* separate the attached element from the rest of the line */
	line = next;
	for (p1 = line; *p1; p1++)
		if (isspace(*p1)) {
			if (tet_compat == COMPAT_DTET)
				p2 = ":";
			else
				for (p2 = p1 + 1; *p2; p2++)
					if (!isspace(*p2))
						break;
			if (*p2 == ':') {
				*p1++ = '\0';
				break;
			}
		}
	next = p1;

	/*
	** now, line points to the (null-terminated) attached element
	** and next points to the rest of the line
	*/
	switch (*line) {
	case '@':
		line++;
		/* fall through */
	case '^':
		if (proc1scline(line, fname, lineno) < 0) {
			while ((ep = scpop(&scstack)) != (struct scentab *) 0)
				scfree(ep);
			TRACE1(tet_Tscen, 4, "proc1dgrp(): return -1");
			return(-1);
		}
		break;
	case '/':
		includefile(line, fname, lineno);
		break;
	default:
		scenerror("badly formed include file name:",
			line, lineno, fname);
		break;
	}

	/* add the implied end* directives */
	while ((ep = scpop(&scstack)) != (struct scentab *) 0)
		scpush(ep, &sclist);

	/* finally, process the rest of the line and return */
	TRACE1(tet_Tscen, 4, "process rest of line after attached items");
	rc = proc1scline(next, fname, lineno);
	TRACE2(tet_Tscen, 4, "proc1dgrp(): return %s", tet_i2a(rc));
	return(rc);
}

/*
**	includefile() - interpolate the contents of an include file
*/

static void includefile(nextfile, currfile, currline)
char *nextfile, *currfile;
int currline;
{
	char buf[TET_MAX(LBUFLEN, MAXPATH)];
	FILE *fp;
	char *fname;
	int lineno = 0;
	char *args[1];
	int nargs;
	register char *p, *bp;
	char delim;

	TRACE2(tet_Tscen, 4, "includefile(): file = <%s>", nextfile);

	/* strip off unwanted characters */
	for (p = nextfile; *p; p++)
		if (!isdirsep(*p))
			break;
	if (!*p) {
		scenerror("empty include file name:", nextfile,
			currline, currfile);
		return;
	}

	/*
	** generate the full path name of the file relative to the
	** test suite root directory
	*/
	fullpath(tet_tsroot, p, buf, sizeof buf, 0);
	fname = rstrstore(buf);

	/* open the file */
	if ((fp = fopen(fname, "r")) == (FILE *) 0) {
		error(errno, "can't open", fname);
		scenerrors++;
		TRACE2(tet_Tbuf, 6, "free include file name = %s",
			tet_i2x(fname));
		free(fname);
		return;
	}

	/* read each line in turn */
	while (fgets(buf, sizeof buf, fp) != (char *) 0) {
		lineno++;

		/* strip out comments and the trailing newline */
		for (p = buf; *p; p++)
			if (*p == '\n' || *p == '#') {
				*p = '\0';
				break;
			}
		/* strip trailing spaces */
		while (--p >= buf)
			if (isspace(*p))
				*p = '\0';
			else
				break;

		/* strip leading spaces */
		for (bp = buf; *bp; bp++)
			if (!isspace(*bp))
				break;

		TRACE2(tet_Tscen, 10, "line after stripping spaces = <%s>", bp);

		/* ignore a blank line */
		if (!*bp)
			continue;

		/* check that the line contains a valid element -
			only file names and scenario information lines
			are valid here
		*/
		switch (*bp) {
		case '/':
			delim = ' ';
			break;
		case '"':
			delim = '"';
			break;
		default:
			nargs = split(bp, args, 1, ' ');
			ASSERT(nargs >= 1);
			scenerror(args[0], "is not valid in included file",
				lineno, fname);
			continue;
		}

		/* ensure that there is only one element on the line */
		for (p = bp + 1; *p; p++)
			if (*p == delim || (delim == ' ' && isspace(*p))) {
				if (delim == '"')
					p++;
				if (split(p, args, 1, ' ') > 0) {
					scenerror("too many elements",
						(char *) 0, lineno, fname);
				}
				*p = '\0';
				break;
			}

		/* send the element for processing */
		if (proc1scline(bp, fname, lineno) < 0)
			break;
	}

	/* finally, close the file and return (but don't free fname!) */
	(void) fclose(fp);
	TRACE1(tet_Tscen, 4, "includefile(): return");
}

/*
**	iszorpnum() - see if a string is a non-negative number
**
**	return 1 if it is or 0 if it isn't
*/

static int iszorpnum(s)
register char *s;
{
	if (!*s)
		return(0);
	else
		while (*s && isdigit(*s))
			s++;

	return(*s ? 0 : 1);
}

/*
**      isnumrange() - see if a string is of the form m-n where m and n
**		are non-negative numbers
**
**	return 1 if it is or 0 if it isn't
**
**	if successful the values of m and n are returned indirectly
**		through *startp and *endp
*/

static int isnumrange(s, startp, endp)
char *s;
int *startp, *endp;
{
	register char *p;
	register int rc;

	for (p = s; *p; p++)
		if (*p == '-') {
			*p = '\0';
			if ((rc = iszorpnum(s) & iszorpnum(p + 1)) != 0) {
				if (startp)
					*startp = atoi(s);
				if (endp)
					*endp = atoi(p + 1);
			}
			*p = '-';
			return(rc);
		}

	return(0);
}

/*
**	ifsalloc(), ifsfree() - functions to allocate and free an
**		input file stack element
*/

static struct ifstack *ifsalloc()
{
	struct ifstack *ifp;

	errno = 0;
	if ((ifp = (struct ifstack *) malloc(sizeof *ifp)) == (struct ifstack *) 0)
		fatal(errno, "can't allocate input file stack element",
			(char *) 0);

	TRACE2(tet_Tbuf, 6, "allocate ifstack element = %s", tet_i2x(ifp));
	bzero((char *) ifp, sizeof *ifp);
	return(ifp);
}

static void ifsfree(ifp)
struct ifstack *ifp;
{
	struct lcache *lcp;

	TRACE2(tet_Tbuf, 6, "free input file stack element = %s", tet_i2x(ifp));

	if (ifp == (struct ifstack *) 0)
		return;

	while ((lcp = lcpop2(ifp)) != (struct lcache *) 0)
		lcfree(lcp);

	free((void *) ifp);
}

/*
**	ifspush(), ifspop() - input file stack manipulation functions
**
**	the operation of these functions depends on the LIFO behaviour
**	of linked lists which are manipulated by tet_listinsert() and
**	tet_listremove()
*/

static void ifspush(ifp)
struct ifstack *ifp;
{
	TRACE2(tet_Tscen, 10, "ifspush(): push active filename %s on stack",
		ifp->if_fname);

	ASSERT(ifstp == ifstack);

	tet_listinsert((struct llist **) &ifstack, (struct llist *) ifp);
	ifstp = ifstack;
}

static struct ifstack *ifspop()
{
	register struct ifstack *ifp;

	ASSERT(ifstp == ifstack);

	if ((ifp = ifstack) != (struct ifstack *) 0) {
		tet_listremove((struct llist **) &ifstack,
			(struct llist *) ifp);
		ifstp = ifstack;
		TRACE2(tet_Tscen, 10, "ifspop(): pop filename %s from stack",
			ifp->if_fname);
		if (ifstp)
			TRACE2(tet_Tscen, 10, "ifspop(): active file is now %s",
				ifstp->if_fname);
		else
			TRACE1(tet_Tscen, 10,
				"ifspop(): that was the last active file");
	}
	else
		TRACE1(tet_Tscen, 10, "ifspop(): stack is empty");

	return(ifp);
}

/*
**	getline_tcc() - get the next non-blank, non-comment line
**		from the currently active input file
**
**	return a pointer to the line, or (char *) 0 on EOF or error
*/

static char *getline_tcc()
{
	static char buf[LBUFLEN];
	struct lcache *lcp;
	register char *p;

	/*
	** pop a line off the stack for the current input file
	** if there is one
	*/
	if ((lcp = lcpop()) != (struct lcache *) 0) {
		(void) strcpy(buf, lcp->lc_line);
		lcfree(lcp);
		TRACE2(tet_Tscen, 10, "getline_tcc(): line = <%s>", firstpart(buf));
		return(buf);
	}

	ASSERT(ifstp == ifstack);
	for (;;) {
		ASSERT(ifstp);
		/*
		** read the next line from the currently active file;
		** on EOF or error:
		**	if this is not the top level:
		**		close the file;
		**		(don't free fname - it's pointed to by each
		**		scentab element generated from the file)
		**		pop the currently active level off the
		**		input file stack;
		**		continue reading from the file at the
		**		newly uncovered level
		**	otherwise:
		**		return the EOF indication
		*/
		if (
			feof(ifstp->if_fp) ||
			fgets(buf, sizeof buf, ifstp->if_fp) == (char *) 0
		) {
			if (ferror(ifstp->if_fp)) {
				error(errno, "read error on", ifstp->if_fname);
				scenerrors++;
			}
			else
				TRACE2(tet_Tscen, 10,
					"getline_tcc(): encountered EOF on %s",
					ifstp->if_fname);
			if (ifstp->if_next) {
				(void) fclose(ifstp->if_fp);
				ifsfree(ifspop());
				continue;
			}
			TRACE1(tet_Tscen, 10, "getline_tcc(): return EOF");
			return((char *) 0);
		}

		/* here we have a line - increment the line counter */
		ifstp->if_lcount++;

		/* strip comments (only in column 1) */
		if (buf[0] == '#')
			continue;

		/* strip the trailing newline */
		for (p = buf; *p; p++)
			if (*p == '\n') {
				*p = '\0';
				break;
			}

		/* strip trailing spaces */
		while (--p >= buf)
			if (isspace(*p))
				*p = '\0';
			else
				break;

		/* punt a % line to the "preprocessor" */
		if (buf[0] == '%') {
			if (preprocess(&buf[1]) < 0)
				return((char *) 0);
			else
				continue;
		}

		/* if there is anything left, return it */
		if (p >= buf) {
			TRACE4(tet_Tscen, 10, "getline_tcc(): fname = %s, lineno = %s, line = <%s>",
				ifstp->if_fname, tet_i2a(ifstp->if_lcount),
				firstpart(buf));
			return(buf);
		}
	}
}

/*
**	ungetline_tcc() - store a line for subsequent retrieval by getline_tcc()
*/

static void ungetline_tcc(line)
char *line;
{
	struct lcache *lcp;

	TRACE2(tet_Tscen, 10, "ungetline_tcc(): line = <%s>", firstpart(line));

	/* store the line and push it on to the stack */
	lcp = lcalloc();
	lcp->lc_line = rstrstore(line);
	lcpush(lcp);
}

/*
**	preprocess() - process a line starting with a %
**
**	return 0 to continue or -1 to abandon processing the current file
**
**	(at present, always returns 0)
*/

static int preprocess(line)
char *line;
{
	/* list of keywords and their associated functions */
	static struct ppfuncs {
		char *pp_keyword;
		int (*pp_func) PROTOLIST((char *));
	} ppfuncs[] = {
		{ "include", ppinclude }
	};
#define Nppfuncs	(sizeof ppfuncs / sizeof ppfuncs[0])

	struct ppfuncs *pp;
	char *p;

	TRACE2(tet_Tscen, 10, "preprocess(): line = <%s>", line);

	/* strip leading spaces */
	while (*line && isspace(*line))
		line++;

	if (!*line) {
		scenerror("need a keyword after %", (char *) 0,
			ifstp->if_lcount, ifstp->if_fname);
		return(0);
	}

	/* separate the keyword from the rest of the line */
	for (p = line; *p; p++)
		if (isspace(*p)) {
			*p++ = '\0';
			break;
		}

	/* strip leading spaces from the rest of the line */
	while (*p && isspace(*p))
		p++;

	/*
	** hand the rest of the line off to the appropriate preprocessor
	** function
	*/
	for (pp = ppfuncs; pp < &ppfuncs[Nppfuncs]; pp++)
		if (!strcmp(line, pp->pp_keyword))
			return((*pp->pp_func)(p));

	scenerror("unknown % keyword", line, ifstp->if_lcount,
		ifstp->if_fname);
	return(0);
}

/*
**	ppinclude() - process the rest of the line after a %include keyword
**
**	always returns 0
*/

static int ppinclude(line)
char *line;
{
	char fname[TET_MAX(LBUFLEN, MAXPATH)];
	struct ifstack *ifp;
	FILE *fp;
	char *p;

	/* ignore extra words on the line */
	for (p = line; *p; p++)
		if (isspace(*p)) {
			*p = '\0';
			break;
		}

	/*
	** if the file name is not a full path name, generate the full path
	** name of the file relative to the test suite root directory
	*/
	fullpath(tet_tsroot, line, fname, sizeof fname, 0);

	/* check for a %include loop */
	for (ifp = ifstp; ifp; ifp = ifp->if_next)
		if (!strcmp(fname, ifp->if_fname)) {
			scenerror("%include file loop", fname,
				ifstp->if_lcount, ifstp->if_fname);
			return(0);
		}

	/* open the file */
	if ((fp = fopen(fname, "r")) == (FILE *) 0) {
		error(errno, "can't open", fname);
		scenerrors++;
		return(0);
	}

	/* set up a new level on the input file stack and return */
	ifp = ifsalloc();
	ifp->if_fname = rstrstore(fname);
	ifp->if_fp = fp;
	ifspush(ifp);
	return(0);
}

/*
**	lcalloc(), lcfree() - functions to allocate and free a
**		line cache element
*/

static struct lcache *lcalloc()
{
	register struct lcache *lcp;

	errno = 0;
	if ((lcp = (struct lcache *) malloc(sizeof *lcp)) == (struct lcache *) 0)
		fatal(errno, "can't allocate line cache element", (char *) 0);

	TRACE2(tet_Tbuf, 6, "allocate lcache element = %s", tet_i2x(lcp));
	bzero((char *) lcp, sizeof *lcp);
	return(lcp);
}

static void lcfree(lcp)
struct lcache *lcp;
{
	TRACE2(tet_Tbuf, 6, "free lcache element = %s", tet_i2x(lcp));

	if (lcp) {
		if (lcp->lc_line) {
			TRACE2(tet_Tbuf, 6, "free lcache line = %s",
				tet_i2x(lcp->lc_line));
			free(lcp->lc_line);
		}
		free((char *) lcp);
	}
}

/*
**	lcpush(), lcpop() - line cache stack manipulation functions
**
**	the operation of these functions depends on the LIFO behaviour
**	of linked lists which are manipulated by tet_listinsert() and
**	tet_listremove()
*/

static void lcpush(lcp)
struct lcache *lcp;
{
	/*
	** if all the lines read from the current file are already in
	** the cache, this line must have been read from the next file
	** down in the input file stack;
	** so move the currently active level down
	**
	** if we reach the bottom of the stack we are trying to push back
	** more lines than have previously been read,
	** so bail out on a programming error
	*/
	while (ifstp->if_lcount <= 0) {
		ASSERT(ifstp->if_next);
		ifstp = ifstp->if_next;
		TRACE2(tet_Tscen, 10, "lcpush(): active file is now %s",
			ifstp->if_fname);
	}

	TRACE4(tet_Tscen, 10, "lcpush(): push line %s on to %s stack = <%s>",
		tet_i2a(ifstp->if_lcount), ifstp->if_fname,
		firstpart(lcp->lc_line));

	tet_listinsert((struct llist **) &ifstp->if_lcache,
		(struct llist *) lcp);
	ifstp->if_lcount--;
}

static struct lcache *lcpop()
{
	/*
	** if there are no lines in the cache at the currently active level
	** on the input file stack and there are higher levels,
	** move the currectly active level up
	*/
	while (ifstp->if_lcache == (struct lcache *) 0 && ifstp->if_last) {
		ifstp = ifstp->if_last;
		TRACE2(tet_Tscen, 10, "lcpop(): active file is now %s",
			ifstp->if_fname);
	}

	return(lcpop2(ifstp));
}

/*
**	lcpop2() - pop an lcache element off the line cache stack which
**		belongs to the input file stack element at *ifp
*/

static struct lcache *lcpop2(ifp)
struct ifstack *ifp;
{
	register struct lcache *lcp;

	if ((lcp = ifp->if_lcache) != (struct lcache *) 0) {
		tet_listremove((struct llist **) &ifp->if_lcache,
			(struct llist *) lcp);
		ifp->if_lcount++;
		TRACE4(tet_Tscen, 10,
			"lcpop(): pop line %s from %s stack = <%s>",
			tet_i2a(ifp->if_lcount), ifp->if_fname,
			firstpart(lcp->lc_line));
	}
	else
		TRACE2(tet_Tscen, 10, "lcpop(): %s stack is empty",
			ifp->if_fname);

	return(lcp);
}

/*
**	firstpart() - deliver the first part of a string
**
**	if the string is longer than PLEN, it has an elipsis appended
**	to it
*/

#ifndef NOTRACE

#define PLEN	30
static char *firstpart(s)
char *s;
{
	static char dots[] = " ...";
	static char buf[PLEN + sizeof dots];

	(void) sprintf(buf, "%.*s%s", PLEN, s,
		(int) strlen(s) > PLEN ? dots : "");

	return(buf);
}

#endif /* NOTRACE */