/*	PROF.C		the runtime profiler

	Usage:		P [-M addr1 addr2] [-C n] [-P] progname [progargs...]
	Options:	-C n		Clock interval in 1/100 sec
					(Default n=10)
			-M addr1 addr2	Profile between address 1,2
					(Default: start,end address of program)
			-P		Printer output of profile

	Note:		PROF.C must link PROFR.REL of PROFR.M which is the 
			relocatable runtime portion of this program.

	23 Nov 1987	A.Herrmann
	18 Mar 1988	revisited
	21 Mar 1988	file opening and bin address initialization 
			now in assembly routine
	25 Mar 1988	now with clock interval setting and printer output
*/

#include <stdio.h>

#define	TRUE	1
#define	FALSE	0

#define	CL_INT	10		/* clock interval in 1/100 sec	*/

typedef	char	*ADDR;		/* memory address		*/


main(argc,argv)
int argc;
char *argv[];
{
	char	pname[20];		/* program file name		*/
	char	com_tail[80];		/* command tail of program call	*/
	int	clock=10;		/* clock interval in 1/100 sec	*/
	ADDR	pstart=NULL, pend=NULL;	/* profile start, end address	*/
	int	printer=FALSE;		/* TRUE iff output to printer	*/
	char	*s;

	extern void profr();		/* runtime part of profiler	*/


/* Get options */

	while (--argc>0 && (*++argv)[0] == '-')
		for (s=argv[0]+1; *s!='\0'; s++)
			switch (*s) {
			case 'C':	clock = atoi(*++argv);
					argc--;
					break;
			case 'M':	pstart = atoi(*++argv);
					pend = atoi(*++argv);
					argc -=2;
					break;
			case 'P':	printer=TRUE;
					break;
			default:	printf("Illegal option -%c\n", *s);
					argc=0;
					break;
			}
	if (argc<1) {
		printf("Usage: PROF [-C n] [-M addr1 addr2] [-P] progname [progargs...]\n");
		exit(1);
	}

/* Build program file name */

	strcpy(pname,*argv);
	if (s=index(pname,'.')) *s='\0';
	for (s=pname; *s!=NULL; *s=toupper(*s++));
	strcat(pname, ".COM");

/* Save program arguments */

	*com_tail='\0';
	while (--argc) {
		if (*com_tail) strcat(com_tail," ");
		strcat(com_tail,*++argv);
	}
	s=com_tail; 
	while (*s=toupper(*s)) s++;

/* Pass control to profiling routine which relocates itself,		*/
/* executes the user program, and subsequently outputs the profile	*/

	profr(pname,com_tail,clock,pstart,pend,printer);

}
