scc

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

main.c (1413B)


      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 static struct items uflags;
     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]... [-o output] [input]\n", stderr);
     40 	exit(1);
     41 }
     42 
     43 int
     44 main(int argc, char *argv[])
     45 {
     46 	int i;
     47 
     48 	ilex();
     49 	icpp();
     50 	icode();
     51 	ibuilts();
     52 
     53 	ARGBEGIN {
     54 	case 'D':
     55 		defmacro(EARGF(usage()));
     56 		break;
     57 	case 'M':
     58 		onlyheader = 1;
     59 		break;
     60 	case 'E':
     61 		onlycpp = 1;
     62 		break;
     63 	case 'I':
     64 		incdir(EARGF(usage()));
     65 		break;
     66 	case 'U':
     67 		newitem(&uflags, EARGF(usage()));
     68 		break;
     69 	case 'd':
     70 		DBGON();
     71 		break;
     72 	case 'w':
     73 		warnings = 1;
     74 		break;
     75 	default:
     76 		usage();
     77 	} ARGEND
     78 
     79 	if (argc > 1)
     80 		usage();
     81 
     82 	for (i = 0; i < uflags.n; ++i)
     83 		undefmacro(uflags.s[i]);
     84 
     85 	infile = (*argv) ? *argv : "<stdin>";
     86 	addinput(*argv, NULL, NULL);
     87 
     88 	/*
     89 	 * we cannot initialize arch until we have an
     90 	 * output stream, because we maybe want to emit new types
     91 	 */
     92 	iarch();
     93 	if (onlycpp || onlyheader) {
     94 		outcpp();
     95 	} else {
     96 		for (next(); yytoken != EOFTOK; decl())
     97 			;
     98 	}
     99 
    100 	return failure;
    101 }