vlp-10 Using coding style in loggraph.
[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",
124                                                                 poms1.data());
125                 exit(1);
126         }
127
128         if (!srcfile.open(IO_ReadOnly)) {
129                 fprintf(stdout, "Cannot open file\n");
130                 exit(1);
131         }
132
133         QTextStream comps(&compfile);
134         QTextStream src(&srcfile);
135
136         while (!src.eof()) {
137                 poms = src.readLine();
138                 i = poms.find("#include");
139                 if (i != -1) {
140                         /* get include file */
141                         i=poms.find('"');
142                         if (i != -1) {
143                                 j=poms.find('"',i+1);
144                         }
145                         if ((i != -1) && (j != -1)) {
146                                 IncFile *p;
147                                 poms1 = poms.mid(i + 1, j - i - 1);
148                                 p = new IncFile((char*)poms1.ascii(),
149                                                                 currentline);
150                                 poms2.sprintf("%s%s", homedir, poms1.data());
151                                 QFile pomf(poms2.data());
152                                 if (!pomf.open(IO_ReadOnly)) {
153                                         fprintf(stdout, "Cannot open include "
154                                                 "file: %s\n", poms2.data());
155                                         exit(1);
156                                 }
157                                 QTextStream pomstream(&pomf);
158                                 while (!pomstream.eof()) {
159                                         poms1 = pomstream.readLine();
160                                         comps << poms1.data();
161                                         comps << "\n";
162                                         p->len++;
163                                         currentline++;
164                                 }
165                                 pomf.close();
166                                 inc.append(p);
167                         }
168                 } else {
169                         comps << poms.data();
170                         comps << "\n";
171                 }
172                 currentline++;
173         }/* eof */
174
175         srcfile.close();
176         compfile.close();
177
178         poms.sprintf("%s/cmp01.lcd", mydir);
179         unlink(poms.data());
180
181         sprintf(ss, "%s/loglan %s/cmp01 > %s/.cmp00", mydir, mydir, mydir);
182         if (system(ss) == -1) {
183                 fprintf(stdout, "Cannot execute compiler\n");
184                 exit(1);
185         }
186
187         poms1.sprintf("%s.lcd", fname);
188
189         if (QFile::exists(poms.data())) {
190                 rename(poms.data(), poms1.data());
191                 unlink(poms.data());
192                 fprintf(stdout, "Compile ok\n");
193                 sprintf(ss,"%s/gen %s", mydir, fname);
194                 if (system(ss)==-1) {
195                         fprintf(stdout, "Cannot execute generator\n");
196                         exit(1);
197                 }
198                 poms.sprintf("%s.ccd", fname);
199                 if (QFile::exists(poms.data())) {
200                         unlink(poms1.data());
201                 }
202         } else {
203                 fprintf(stdout, "Errors\n");
204                 poms.sprintf("%s/.cmp00", mydir);
205                 QFile err(poms.data());
206
207                 if (!err.open(IO_ReadOnly)) {
208                         fprintf(stdout,"Cannot open error file\n");
209                         exit(1);
210                 }
211                 QTextStream errstream(&err);
212                 IncFile *fl;
213
214                 poms = errstream.readLine();
215                 i = poms.find("LOGLAN-82");
216                 while ((!errstream.eof()) && (i == -1)) {
217                         poms = errstream.readLine();
218                         i = poms.find("LOGLAN-82");
219                 }
220
221                 while (!errstream.eof()) {
222                         poms = errstream.readLine();
223                         i = poms.find("ERROR");
224                         if (i != -1) {
225                                 i = i-2;
226                                 j = poms.findRev(' ', i);
227                                 poms1 = poms.mid(j + 1, i - j);
228                                 line = poms1.toInt();
229                                 fl = findTrueLine(line, &tline);
230                                 poms2 = poms.right(poms.length() - i - 1);
231                                 if (fl != NULL)
232                                         fprintf(stdout, "%s: ", fl->filename);
233                                 fprintf(stdout, "%d  %s\n",
234                                                         tline, poms2.data());
235                         }
236                 }
237                 err.close();
238         } /* errors */
239
240         poms.sprintf("%s/cmp01.log",mydir);
241         unlink(poms.data());
242         poms.sprintf("%s/.cmp00",mydir);
243         unlink(poms.data());
244
245         return 0;
246 }