
/*
 * compile.
 */

#include <stdio.h>

#define	GOK	0		/* Who knows */
#define	OBJ	1		/* Object */
#define	S	2		/* ASM input */
#define C	3		/* CCX input */
#define MAC	4		/* MACRO input */

int	cflag;
int	mflag;
int	pflag;
int	vflag;
int	quit;
int	nlib;
char	*lib[NLIB];

char	*lname[] = {
	"**Gok**",
	"**Obj**",
	"Asm",
	"C",
	"Macro"
};

main(argc, argv)
char *argv[];
{
	register char *p;
	register c, i;
	int k, nf;

	nf = argc-1;
	for (i=1; i<argc; ++i) {
		p = argv[i];
		if (*p == '-') {
			argv[i] = NULL;
			--nf;
			++p;
			while (c = *p++)
				switch (c) {

				case 'c':
				case 'C':
					++cflag;
					break;

				case 'm':
				case 'M':
					++mflag;
					break;

				case 'p':
				case 'P':
					++pflag;
					break;

				case 'v':
				case 'V':
					++vflag;
					break;

				default:
					usage();
				}
		}
	}
	if (nf == 0)
		usage();
	for (i=1; i<argc; ++i) {
		if ((p = argv[i]) == NULL)
			continue;
		setkind(p);
		if (kind != OBJ && kind != GOK) {
			if (vflag || nf>1)
				printf("%s: %s:\n", lname[kind], p);
			compile();
			if (kind == C)
				addlib("lb:[1,1]clib.olb");
		}
	}
	if (quit || cflag)
		exit(0);
	tkb(argc, argv);
}

compile()
{
	register s;

	switch (kind) {

	case MAC:
		s = sysf("mac %s.obj=%s.mac", fn, fn);
		break;

	case C:
		s = sysf("ccx %s%s", pflag?"-p":"", fn);
		if (s != IS_SUC)
			break;
		s = sysf("asm -d %s.s", fn);
		break;

	case S:
		s = sysf("asm -g %s.s", fn);
		break;

	default:
		fprintf(stderr, "Bad kind -- cannot happen!\n");
		exit(1);
	}
	if (s != IS_SUC) {
		printf("Bad status (%d)\n", s);
		++quit;
	}
}

addlib(s)
char *s;
{
	register i;

	for (i=0; i<nlib; ++i)
		if (streq(s, lib[i]))
			return;
	if (nlib >= NLIB) {
		fprintf(stderr, "Too many libraries!\n");
		exit(1);
	}
	lib[nlib++] = s;
}

tkb(argc, argv)
char *argv[];
{
	register FILE *fp;

	if ((fp = fopen("cmp.tmp", "wv")) == NULL) {
		fprintf(stderr, "Cannot create tkb command file!\n");
		exit(1);
	}
	f = t = 0;
	fprintf(fp, "/\n");
	fprintf(fp, "task=...%s\n", tnb);
	fprintf(fp, "//\n");
	fclose(fp);
	s = sysf("tkb @cal.tmp");
	sysf("pip cal.tmp;*/de");
	return (s);
}

setkind(s)
char *s;
{
	k = GOK;
	p = s;
	while ((c = *p++) && c != '.' && c != ';')
		;
	if (c == '.') {
		determine the type();
	}
	while (p>s && (c=*--p)!=':' && c!=']')
		;
	q = fn;
	....
}

usage()
{
	fprintf(stderr, "Usage: compile [-cmp] file [file ...]\n");
	exit(1);
}
