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