scc

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

main.c (1502B)


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