]> git.lyx.org Git - lyx.git/blob - src/Literate.C
d77447f1cd31fec54710cd934bb8eaf7d2a9e8d9
[lyx.git] / src / Literate.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor         
5  *           Copyright (C) 1995 Matthias Ettrich
6  *           Copyright (C) 1995-1998 The LyX Team.
7  *
8  *           This file is Copyright (C) 1996-1998
9  *           Lars Gullik Bjønnes
10  *
11  *======================================================
12  */
13
14 #include <config.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #ifdef __GNUG__
20 #pragma implementation
21 #endif
22
23 #include "filetools.h"
24 #include "LaTeX.h"
25 #include "Literate.h"
26 #include "lyxlex.h"
27 #include "FileInfo.h"
28 #include "error.h"
29 #include "lyxlib.h"
30 #include "syscall.h"
31 #include "syscontr.h"
32 #include "pathstack.h"
33 #include "bufferlist.h"
34 #include "minibuffer.h"
35 #include "gettext.h"
36
37 //      $Id: Literate.C,v 1.1 1999/09/27 18:44:36 larsbj Exp $  
38
39 #if !defined(lint) && !defined(WITH_WARNINGS)
40 static char vcid[] = "$Id: Literate.C,v 1.1 1999/09/27 18:44:36 larsbj Exp $";
41 #endif /* lint */
42
43 extern BufferList bufferlist;
44
45 Literate::Literate(LString const & latex, LString const & f, LString const & p, 
46                    LString const & l, 
47                    LString const & literate, LString const & literate_f, 
48                    LString const & build, LString const & build_f)
49                    : LaTeX (latex, f, p),
50                      litfile(l),
51                      literate_cmd(literate), literate_filter(literate_f), 
52                      build_cmd(build), build_filter(build_f)
53 {
54 }
55
56
57 int Literate::weave(TeXErrors &terr, MiniBuffer *minib)
58 {
59         int scanres = Literate::NO_ERRORS;
60         LString tmp1, tmp2;
61         int ret1, ret2;
62         Systemcalls one, two;
63
64         // The class LaTeX does not know the temp path.
65         bufferlist.updateIncludedTeXfiles(GetCWD());
66         
67         lyxerr.debug(LString(_("Weaving document")),
68                      Error::LATEX);
69         minib->Set(LString(_("Weaving document")));
70         minib->Store();
71
72         // Run the literate program to convert \literate_extension file to .tex file
73         //
74         tmp1 = literate_cmd + " < " + litfile + " > " + file + " 2> " + litfile + ".out";
75         tmp2 = literate_filter + " < " + litfile + ".out" + " > " + litfile + ".log";
76         ret1 = one.Startscript(Systemcalls::System, tmp1);
77         ret2 = two.Startscript(Systemcalls::System, tmp2);
78         lyxerr.debug(LString(_("LITERATE")) + " {" + tmp1 + "} {" + tmp2 + "}");
79         scanres = scanLiterateLogFile(terr);
80         if (scanres & Literate::ERRORS) return scanres; // return on literate error
81
82         return run(terr, minib);
83 }
84
85
86 int Literate::build(TeXErrors &terr, MiniBuffer *minib)
87         // We know that this function will only be run if the lyx buffer
88         // has been changed. 
89 {
90         int scanres = Literate::NO_ERRORS;
91         num_errors = 0; // just to make sure.
92         // DepTable head; // empty head // unused
93         // bool rerun = false; // rerun requested // unused
94         LString tmp1, tmp2;
95         int ret1, ret2;
96         Systemcalls one, two;
97         
98         // The class LaTeX does not know the temp path.
99         bufferlist.updateIncludedTeXfiles(GetCWD());
100         
101         lyxerr.debug(LString(_("Building program")), 
102                      Error::LATEX);
103         minib->Set(LString(_("Building program")));
104         minib->Store();
105
106         // Run the build program
107         //
108         tmp1 = build_cmd + ' ' + litfile + " > " + litfile + ".out 2>&1";
109         tmp2 = build_filter + " < " + litfile + ".out" + " > " + litfile + ".log";
110         ret1 = one.Startscript(Systemcalls::System, tmp1);
111         ret2 = two.Startscript(Systemcalls::System, tmp2);
112         scanres = scanBuildLogFile(terr);
113         lyxerr.debug("Done.", Error::LATEX);
114
115         return scanres;
116 }
117
118
119 int Literate::scanLiterateLogFile(TeXErrors &terr)
120 {
121         LString token;
122         int retval = NO_ERRORS;
123         
124         LyXLex lex(NULL, 0);
125  
126         LString tmp = litfile + ".log";
127         
128         if (!lex.setFile(tmp)) {
129                 // unable to open file
130                 // return at once
131                 retval |= NO_LOGFILE;
132                 return retval;
133         }
134         
135         while (lex.IsOK()) {
136                 if (lex.EatLine())
137                         token = lex.GetString();
138                 else // blank line in the file being read
139                         continue;
140  
141                 lyxerr.debug(token, Error::LATEX);
142                 
143                 if (token.prefixIs("Build Warning:")) {
144                         // Here shall we handle different
145                         // types of warnings
146                         retval |= LATEX_WARNING;
147                         lyxerr.debug("Build Warning.", Error::LATEX);
148                 } else if (token.prefixIs("! Build Error:")) {
149                         // Here shall we handle different
150                         // types of errors
151                         retval |= LATEX_ERROR;
152                         lyxerr.debug("Build Error.", Error::LATEX);
153                         // this is not correct yet
154                         terr.scanError(lex);
155                         num_errors++;
156                 }
157         }       
158         return retval;
159 }
160
161
162 int Literate::scanBuildLogFile(TeXErrors &terr)
163 {
164         LString token;
165         int retval = NO_ERRORS;
166         
167         LyXLex lex(NULL, 0);
168  
169         LString tmp = litfile + ".log";
170         
171         if (!lex.setFile(tmp)) {
172                 // unable to open file
173                 // return at once
174                 retval |= NO_LOGFILE;
175                 return retval;
176         }
177         
178         while (lex.IsOK()) {
179                 if (lex.EatLine())
180                         token = lex.GetString();
181                 else // blank line in the file being read
182                         continue;
183  
184                 lyxerr.debug(token, Error::LATEX);
185                 
186                 if (token.prefixIs("Build Warning:")) {
187                         // Here shall we handle different
188                         // types of warnings
189                         retval |= LATEX_WARNING;
190                         lyxerr.debug("Build Warning.", Error::LATEX);
191                 } else if (token.prefixIs("! Build Error:")) {
192                         // Here shall we handle different
193                         // types of errors
194                         retval |= LATEX_ERROR;
195                         lyxerr.debug("Build Error.", Error::LATEX);
196                         // this is not correct yet
197                         terr.scanError(lex);
198                         num_errors++;
199                 }
200         }       
201         return retval;
202 }
203
204