scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

main.c (1457B)


      1 static char sccsid[] = "@(#) ./cc1/main.c";
      2 #include <setjmp.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <errno.h>
      7 
      8 #include <scc/arg.h>
      9 #include <scc/scc.h>
     10 #include "cc1.h"
     11 
     12 char *argv0, *infile;
     13 
     14 int warnings;
     15 jmp_buf recover;
     16 
     17 static struct items uflags;
     18 int onlycpp, onlyheader;
     19 
     20 
     21 extern int failure;
     22 
     23 static void
     24 defmacro(char *macro)
     25 {
     26 	char *p = strchr(macro, '=');
     27 
     28 	if (p)
     29 		*p++ = '\0';
     30 	else
     31 		p = "1";
     32 
     33 	defdefine(macro, p, "command-line");
     34 }
     35 
     36 static void
     37 usage(void)
     38 {
     39 	fputs("usage: cc1 [-Ewd] [-D def[=val]]... [-U def]... "
     40 	      "[-I dir]... [-o output] [input]\n", stderr);
     41 	exit(1);
     42 }
     43 
     44 int
     45 main(int argc, char *argv[])
     46 {
     47 	int i;
     48 
     49 	ilex();
     50 	icpp();
     51 	icode();
     52 	ibuilts();
     53 
     54 	ARGBEGIN {
     55 	case 'D':
     56 		defmacro(EARGF(usage()));
     57 		break;
     58 	case 'M':
     59 		onlyheader = 1;
     60 		break;
     61 	case 'E':
     62 		onlycpp = 1;
     63 		break;
     64 	case 'I':
     65 		incdir(EARGF(usage()));
     66 		break;
     67 	case 'U':
     68 		newitem(&uflags, EARGF(usage()));
     69 		break;
     70 	case 'd':
     71 		DBGON();
     72 		break;
     73 	case 'w':
     74 		warnings = 1;
     75 		break;
     76 	default:
     77 		usage();
     78 	} ARGEND
     79 
     80 	if (argc > 1)
     81 		usage();
     82 
     83 	for (i = 0; i < uflags.n; ++i)
     84 		undefmacro(uflags.s[i]);
     85 
     86 	infile = (*argv) ? *argv : "<stdin>";
     87 	addinput(*argv, NULL, NULL);
     88 
     89 	/*
     90 	 * we cannot initialize arch until we have an
     91 	 * output stream, because we maybe want to emit new types
     92 	 */
     93 	iarch();
     94 	if (onlycpp || onlyheader) {
     95 		outcpp();
     96 	} else {
     97 		for (next(); yytoken != EOFTOK; decl())
     98 			;
     99 	}
    100 
    101 	return failure;
    102 }