scc

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

main.c (1514B)


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