Add tests, fixes for tests, reinstate and type-convert stuff marked "bitrot"
[ccom.git] / ccom.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include "c0.h"
5 #include "c1.h"
6
7 /* new merged main program for C compiler pass [01], makes own tempfiles */
8 int temp_fi;
9 FILE *temp_fp[2];
10 char buf0[BUFSIZ];
11 char buf1[BUFSIZ];
12 char buf2[BUFSIZ];
13
14 int main(argc, argv) int argc; char **argv; {
15         register unsigned i;
16         register struct kwtab *ip;
17         char temp_name[16];
18         int fd;
19
20         while (argc >= 2) {
21                 if (strcmp(argv[1], "-P") == 0)
22                         proflg = 1;
23                 else if (strcmp(argv[1], "-W") == 0 || strcmp(argv[1], "-w") == 0)
24                         Wflag = 1;
25                 else
26                         break;
27                 --argc;
28                 ++argv;
29         }
30
31         if (argc >= 2 && strcmp(argv[1], "-") != 0 && freopen(argv[1], "r", stdin) == 0) {
32                 error0("Can't find %s", argv[1]);
33                 exit(1);
34         }
35         for (i = 0; i < 2; ++i) {
36                 strcpy(temp_name, "/tmp/ccomXXXXXX");
37                 fd = mkstemp(temp_name);
38                 if (fd == -1 || (temp_fp[i] = fdopen(fd, "w+")) == 0 /*|| unlink(temp_name)*/) {
39                         error0("Can't create temp: %s", temp_name);
40                         exit(1);
41                 }
42         }
43         setbuf(stdin, buf0);
44         setbuf(temp_fp[0], buf1);
45         setbuf(temp_fp[1], buf2);
46
47         STAUTO = -8;
48
49         /*
50          * The hash table locations of the keywords
51          * are marked; if an identifier hashes to one of
52          * these locations, it is looked up in in the keyword
53          * table first.
54          */
55         for (ip=kwtab; ip->kwname; ip++) {
56                 i = hash(ip->kwname);
57                 kwhash[i/LNBPW] |= 1 << (i%LNBPW);
58         }
59         coremax = locbase = /*sbrk(0);*/ funcbase = curbase = sbrk(0);
60         temp_fi = 0;
61         while(!eof)
62  {
63   fprintf(stderr, "%ld %ld\n", ftell(temp_fp[0]), ftell(temp_fp[1]));
64                 extdef();
65  }
66         outcode("B", EOFC);
67         temp_fi = 1;
68         outcode("B", EOFC);
69         blkend();
70         if (nerror)
71                 exit(1);
72         fclose(stdin);
73
74         if (argc >= 3 && strcmp(argv[2], "-") != 0 && freopen(argv[2], "w", stdin) == 0) {
75                 error0("Can't create %s", argv[2]);
76                 exit(1);
77         }
78         setbuf(stdout, buf0);
79
80         /*funcbase = curbase = coremax = sbrk(0);*/
81         temp_fi = 0;
82         rewind(temp_fp[0]);
83         getree();
84         /*
85          * If any floating-point instructions
86          * were used, generate a reference that
87          * pulls in the floating-point part of printf.
88          */
89         if (nfloat)
90                 printf(".globl  fltused\n");
91         /*
92          * tack on the string file.
93          */
94         printf(".globl\n.data\n");
95         temp_fi = 1;
96         rewind(temp_fp[1]);
97         getree();
98         if (totspace >= (_UNSIGNED_INT)56000)
99                 werror1("possibly too much data");
100         exit(nerror!=0);
101 }