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