cc1.c (1383B)
1 #include <stdbool.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include "util.h" 5 #include "arg.h" 6 #include "cc.h" 7 8 static void 9 usage(void) 10 { 11 fprintf(stderr, "usage: %s [input]\n", argv0); 12 exit(2); 13 } 14 15 int 16 main(int argc, char *argv[]) 17 { 18 bool pponly = false; 19 char *output = NULL, *target = NULL; 20 21 argv0 = progname(argv[0], "cproc-qbe"); 22 ARGBEGIN 23 { 24 case 'E': 25 pponly = true; 26 break; 27 case 't': 28 target = EARGF(usage()); 29 break; 30 case 'o': 31 output = EARGF(usage()); 32 break; 33 default: 34 usage(); 35 } 36 ARGEND 37 38 targinit(target); 39 40 if (output && !freopen(output, "w", stdout)) 41 fatal("open %s:", output); 42 43 if (argc) { 44 while (argc--) 45 scanfrom(argv[argc], NULL); 46 scanopen(); 47 } else { 48 scanfrom("<stdin>", stdin); 49 } 50 51 tokeninit(); 52 ppinit(); 53 if (pponly) { 54 ppflags |= PPNEWLINE; 55 while (tok.kind != TEOF) { 56 tokenprint(&tok, stdout); 57 next(); 58 } 59 } else { 60 scopeinit(); 61 while (tok.kind != TEOF) { 62 if (!decl(&filescope, NULL)) { 63 if (tok.kind == TSEMICOLON) 64 error(&tok.loc, "unexpected ';' at top-level"); 65 error( 66 &tok.loc, 67 "expected declaration or " 68 "function definition" 69 ); 70 } 71 } 72 emittentativedefns(); 73 } 74 75 fflush(stdout); 76 if (ferror(stdout)) 77 fatal("write failed"); 78 return 0; 79 }