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