7a00924f8a3c59c289b477bead18ee5cbc1d07db
[vlp.git] / src / preproc / prep.cpp
1 #include <qlist.h>
2 #include <qstring.h>
3 #include <stdio.h>
4 #include <qfile.h>
5 #include <qtstream.h>
6 #include <string.h>
7 #include <qdir.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10
11 class IncFile
12 {
13 public:
14         char filename[255];
15         int start, len;
16         IncFile(char *s, int st)
17         {
18                 strcpy(filename,s);
19                 start=st;
20                 len=0;
21         };
22 };
23
24 QList<IncFile> inc;
25
26 IncFile *findTrueLine(int ln, int *trueline)
27 {
28         IncFile *pom, *pom1;
29         QList<IncFile> pl;
30         int c1 = 0, c2 = 0;
31
32         if (inc.isEmpty()) {
33                 *trueline = ln;
34                 return NULL;
35         } 
36         pl.clear();
37         pom = inc.first();
38         if (pom->start > ln) {
39                 *trueline = ln;
40                 return NULL;
41         }
42
43         while ((pom != NULL) && (ln > pom->start)) {
44                 if (ln < pom->start + pom->len) {
45                         *trueline = ln-pom->start;
46                         return pom;
47                 }
48                 pl.append(pom);
49                 pom=inc.next();
50         }
51
52         if (pom != NULL) {
53                 pom1 = pl.first();
54                 c1=0;
55                 while (pom1 != NULL) {
56                         c1 += pom1->len;
57                         pom1 = pl.next();
58                 }
59                 *trueline = ln - c1;
60                 return NULL;
61         } else {
62                 pom1 = inc.first();
63                 c1 = 0;
64                 while (pom1 != NULL) {
65                         c1 += pom1->len;
66                         pom1=inc.next();
67                 }
68                 *trueline = ln - c1;
69                 return NULL;
70         }
71         return NULL;
72 }
73
74 int main(int argc,char **argv)
75 {
76         char homedir[255];
77         char mydir[255];
78         char ss[255];
79         char fname[255];
80         QString poms, poms1, poms2;
81
82         int currentline = 1;
83         int i, j, line, tline;
84  
85         inc.clear();
86         poms.sprintf("%s", argv[1]);
87         i = poms.findRev('/', poms.length() - 1, FALSE);
88
89         //******* get home directory
90         if (i != -1) {
91                 poms1 = poms.left(i + 1);
92                 strcpy(homedir, poms1.data());
93         }
94         else
95                 strcpy(homedir, "");
96
97         poms.sprintf("%s", argv[0]);
98         i = poms.findRev('/', poms.length() - 1, FALSE);
99         if (i != -1) {
100                 poms1=poms.left(i);
101         }
102         else
103                 poms1.sprintf(".");
104
105         strcpy(mydir,poms1.data());
106
107         poms.sprintf("%s", argv[1]);
108         i = poms.findRev(".log", poms.length()-1, FALSE);
109         poms1 = poms.left(i);
110         strcpy(fname, poms1.data());
111
112         // ********************
113
114         poms.sprintf("%s/.cmp00", mydir);
115         poms1.sprintf("%s/cmp01.log", mydir);
116         unlink(poms.data());
117         unlink(poms1.data());
118
119         QFile compfile(poms1.data());
120         QFile srcfile(argv[1]);
121
122         if (!compfile.open(IO_WriteOnly)) {
123                 fprintf(stdout,"Cannot open temp file to write %s\n", poms1.data());
124                 exit(1);
125         }
126
127         if (!srcfile.open(IO_ReadOnly)) {
128                 fprintf(stdout, "Cannot open file\n");
129                 exit(1);
130         }
131
132         QTextStream comps(&compfile);
133         QTextStream src(&srcfile);
134
135         while (!src.eof()) {
136                 poms = src.readLine();
137                 i = poms.find("#include");
138                 if (i != -1) {
139                         /* get include file */
140                         i=poms.find('"');
141                         if (i != -1) {
142                                 j=poms.find('"',i+1);
143                         }
144                         if ((i != -1) && (j != -1)) {
145                                 IncFile *p;
146                                 poms1 = poms.mid(i + 1, j - i - 1);
147                                 p = new IncFile((char*)poms1.ascii(), currentline);
148                                 poms2.sprintf("%s%s", homedir, poms1.data());
149                                 QFile pomf(poms2.data());
150                                 if (!pomf.open(IO_ReadOnly)) {
151                                         fprintf(stdout, "Cannot open include file: %s\n", poms2.data());
152                                         exit(1);
153                                 }
154                                 QTextStream pomstream(&pomf);
155                                 while (!pomstream.eof()) {
156                                         poms1 = pomstream.readLine();
157                                         comps << poms1.data();
158                                         comps << "\n";
159                                         p->len++;
160                                         currentline++;
161                                 }
162                                 pomf.close();
163                                 inc.append(p);
164                         }
165                 } else {
166                         comps << poms.data();
167                         comps << "\n";
168                 }
169                 currentline++;
170         }/* eof */
171
172         srcfile.close();
173         compfile.close();
174
175         poms.sprintf("%s/cmp01.lcd", mydir);
176         unlink(poms.data());
177
178         sprintf(ss, "%s/loglan %s/cmp01 > %s/.cmp00", mydir, mydir, mydir);
179         if (system(ss) == -1) {
180                 fprintf(stdout, "Cannot execute compiler\n");
181                 exit(1);
182         }
183
184         poms1.sprintf("%s.lcd", fname);
185
186         if (QFile::exists(poms.data())) {
187                 rename(poms.data(), poms1.data());
188                 unlink(poms.data());
189                 fprintf(stdout, "Compile ok\n");
190                 sprintf(ss,"%s/gen %s", mydir, fname);
191                 if (system(ss)==-1) {
192                         fprintf(stdout, "Cannot execute generator\n");
193                         exit(1);
194                 }
195                 poms.sprintf("%s.ccd", fname);
196                 if (QFile::exists(poms.data())) {
197                         unlink(poms1.data());
198                 }
199         } else {
200                 fprintf(stdout, "Errors\n");
201                 poms.sprintf("%s/.cmp00", mydir);
202                 QFile err(poms.data());
203
204                 if (!err.open(IO_ReadOnly)) {
205                         fprintf(stdout,"Cannot open error file\n");
206                         exit(1);
207                 }
208                 QTextStream errstream(&err);
209                 IncFile *fl;
210
211                 poms = errstream.readLine();
212                 i = poms.find("LOGLAN-82");
213                 while ((!errstream.eof()) && (i == -1)) {
214                         poms = errstream.readLine();
215                         i = poms.find("LOGLAN-82");
216                 } // ***
217
218                 while (!errstream.eof()) {
219                         poms = errstream.readLine();
220                         i = poms.find("ERROR");
221                         if (i != -1) {
222                                 i = i-2;
223                                 j = poms.findRev(' ', i);
224                                 poms1 = poms.mid(j+1, i-j);
225                                 line = poms1.toInt();
226                                 fl = findTrueLine(line, &tline);
227                                 poms2 = poms.right(poms.length() - i - 1);
228                                 if (fl != NULL)
229                                         fprintf(stdout, "%s: ", fl->filename);
230                                 fprintf(stdout, "%d  %s\n", tline, poms2.data());
231                         }
232                 }
233                 err.close();
234         } // errors
235
236         poms.sprintf("%s/cmp01.log",mydir);
237         unlink(poms.data());
238         poms.sprintf("%s/.cmp00",mydir);
239         unlink(poms.data());
240
241         return 0;
242 }