shithub: scc

ref: cfe807ad6019b35141c53981f42e2289c66e9bb1
dir: /cc1/main.c/

View raw version
/* See LICENSE file for copyright and license details. */
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "../inc/arg.h"
#include "../inc/cc.h"
#include "arch.h"
#include "cc1.h"

char *argv0;

int warnings;
jmp_buf recover;

static char *output;
int onlycpp;

extern int failure;

static void
clean(void)
{
	if (failure && output)
		remove(output);
}

static void
usage(void)
{
	die("usage: %s [-E] [-D macro[=value]] ... [-I dir] [-w] [-d]"
	    "[-o output] [input]", argv0);
}

int
main(int argc, char *argv[])
{
	char *base;
	static char *uvec[NR_USWITCHES], **umacro = uvec;

	atexit(clean);
	icpp();

	ARGBEGIN {
	case 'w':
		warnings = 1;
		break;
	case 'E':
		onlycpp = 1;
		break;
	case 'D':
		defmacro(EARGF(usage()));
		break;
	case 'U':
		if (umacro == &uvec[NR_USWITCHES])
			die("too many -U switches");
		*umacro++ = EARGF(usage());
		break;
	case 'd':
		DBGON();
		break;
	case 'I':
		incdir(EARGF(usage()));
		break;
	case 'o':
		output = EARGF(usage());
		break;
	default:
		usage();
	} ARGEND

	for (umacro = uvec; *umacro; umacro++)
		undefmacro(*umacro);

	if (argc > 1)
		usage();

	/* if run as cpp, only run the preprocessor */
	if ((base = strrchr(argv0, '/')))
		++base;
	else
		base = argv0;
	if (!strcmp(base, "cpp"))
		onlycpp = 1;

	if (output && !freopen(output, "w", stdout))
		die("error opening output: %s", strerror(errno));

	ilex(*argv);
	if (onlycpp) {
		outcpp();
	} else {
		next();

		while (yytoken != EOFTOK)
			decl();
	}

	return failure;
}