
#include "V.h"
#include "mem.h"
#include "sys_varargs.h"
/*
* Here's a simple test routine, which will accept a  -d<l><file>  arg,
* and  put  out sample debug messages for each level from 0 to 9.  You
* might test it by compiling with -DTEST and -DDEBUG=5 or a few  other
* values, and see if the DEBUG level properly suppresses the output.
*/
extern char * ttyname();
global int   i1, i2;
global Ulong u1, u2;
global float f1, f2;
int
main(ac,av)
	int   ac;
	char**av;
{	int   a;

	char *p;







	errno = 0;

	Vfshow = 3;










	V1 "This message should not appear." D;
	V2 "This message should also not appear." D;
	ac = Vinit(ac,av);


	V2 "There are %d args, and USE_varargs=%d.",ac,USE_varargs D;








	for (a=1; a<ac; a++) {
		V2 "Arg%3d=%s",a,Dsps(av[a],-1) D;
		if (av[a][0] == '-') {
			V2 "Option \"%s\"",av[a] D;
			Switch(av[a][1]) {
			  case 'D':
			  case 'd':
				Vopt(av[a]+2);
				V2 "Debug level %d file %s",Vlvl,Dsps(Vfile,-1) D;
				break;
		      default:
				V1 "Option \"%s\" unknown, ignored.",av[a] D;
				break;
			}
		} else {
			V1 "Arg%3d=\"%s\" unknown, ignored.",a,av[a] D;
			break;
		}
	}
	i1 = 17;
	i2 = -17;
	u1 = 0xF;
	u2 = 0xFFFFFFFF;
	f1 = 1.1;
	f2 = -17.42;
/*
* The P*() messages should appear without any initial junk added, only
* a newline appended.  They are all indented via a single tab, so they
* should stand out.
*/
	P0 "==============================================" D;
	P0 "First, 'P' messages 0-%d:",Vlvl D;
	P0 "\tMessage P0" D;
	p1();
/*
* Let's see if the Vopt(0L) call works:
*/
	P0 "Next, use Vopt(0L) to reopen the Vout file:" D;
	Vopt(0L);
/*
* The D*() messages should have the last 2 (or 3 depending on jc_Vd)
* functions*  from the stack at the left, plus a tab, the message, and
  a newline.
*/
	P0 "Next, 'D' messages 0-%d:",Vlvl D;
	V0 "Message V0" D;
	d1();
/*
* Here's a special goodie to test the  'M'  debug  flag.   The  memory
* routines have messages showing the calls at level 6M and more detail
* at higher levels of Vflg['M'].
*/
	P0 "Last, 'M' messages 0-%d:",Vflg['M'] D;
	p = (CP)GetChunk(100,"GetChunk");
	p = (CP)SizChunk(p,100,200,"SizChunk");
	IRelChunk(p,"IRelChunk");

	P4 "Here is the open-file table:" D;
	if (Vlvl>=4) Vdmpft();

	V1 "%s	All done.",Vtime() D;
	exit(errno);
}
/*
* Here are some "debug" output routines.  The result should  be  a  series  of
* output  lines  labeled  "Message  V0"  thru  "Message V9", but ending at the
* current debug level. These routines should also produce one or more function
* names at the start of each line of output; the number of functions displayed
* is determined in jc_Vd().
*/
d1()
{
	Fenter("d1");
	if (Vlvl > 4) ChkFlvl;
	V1 "Message V1 i1=%d	[should be 17]"  ,i1 D;
	V2 "Message V2 i2=%d	[should be -17]" ,i2 D;
	d3();
	Fexit;
	return(1);
}
d3()
{
	Fenter("d3");
	if (Vlvl > 4) ChkFlvl;
	V3 "Message V3 u1=%u	[should be 15 in %s]"        ,u1,Fctname D;
	V4 "Message V4 u2=%lu	[should be 4294967295]",u2 D;
#if VBS_SLOW
	H5(&Vfct,sizeof(V_f),"V_f");
#endif /*VBS_SLOW*/
	d5();
	Fexit;
	return(3);
}
d5()
{
	Fenter("d5");
	if (Vlvl > 4) ChkFlvl;
	V5 "Message V5 f1=%.3f	[should be 1.100]"     ,f1 D;
	V6 "Message V6 f2=%.3f	[should be -17.420]"   ,f2 D;




	if (Vlvl>4)
		stktst();
	d7();
	Fexit;
	return(5);
}
d7()
{
	Fenter("d7");
	V7 "Message V7 i1=%d i2=%d u1=%u u2=%u f1=%f f2=%f",i1,i2,u1,u2,f1,f2 D;

	Fexit;
	return(7);
}
d9()
{
	Fenter("d9");

	Fexit;
	return(9);
}
/*
* Here are some similar "print" routines, which should produce pretty much the
* same output as the above d*() routines, but without any junk at the start of
* the output lines.
*/
p1()
{
	Fenter("p1");
	P1 "\tMessage P1 i1=%d	[should be 17]"        ,i1 D;
	P2 "\tMessage P2 i2=%d	[should be -17]"       ,i2 D;

	p3();

	Fexit;
	return(1);
}
p3()
{
	Fenter("p3");





	P3 "\tMessage P3 u1=%lu"        ,u1 D;

	P3 "\tMessage P3 Fctname is \"%s\"",Fctname D;
	P3 "\tMessage P3 u1=%lu	[should be 15 in %s]"    ,u1,Fctname D;
	P4 "\tMessage P4 u2=%u	[should be 4294967295]",u2 D;
	p5();
	Fexit;

	return(3);
}
p5()
{
	Fenter("p5");
	P5 "\tMessage P5 f1=%.3f	[should be 1.100]"     ,f1 D;
	P6 "\tMessage P6 f2=%.3f	[should be -17.420]"   ,f2 D;
	p7();
	Fexit;
	return(5);
}
p7()
{
	Fenter("p7");
	P7 "\tMessage P7 i1=%d i2=%d u1=%u u2=%u f1=%f f2=%f",i1,i2,u1,u2,f1,f2 D;

	Fexit;
	return(7);
}
p9()
{
	Fenter("p9");

	Fexit;
	return(9);
}
/*
* Here is a routine for exercising the stack-checking macro.  We call badstk()
* to push its struct on the stack and not pop it; the V_Flvl macro should then
* give us an error message.  We then pop the stack, and all should be OK.
*/
stktst()
{
	Fenter("stktst");
#if VBS_SLOW
	V2 "Checking debug stack control ..." D;
	ChkFlvlM("Should be good");
	V2 "Call badstk() ..." D;
	badstk();
	ChkFlvlM("Should be bad");
	Vfcts = &Vfct;		/* Here is how to restore the Vfct stack */
	Vflvl = flevel;
	ChkFlvlM("Should be good but isn't");	/* No message if OK */
	V2 "-- Only two warning messages should preceed this line." D;
#endif /*VBS_SLOW*/
	Fexit;
	return(0);
}
badstk()
{
	Fenter("badstk");
	if (Vlvl > 4) ChkFlvl;
	ChkFlvl;
	V2 "--- A warning should follow about a bad stack:" D;
/*	*** No Fexit here!!! */
	return(0);
}
