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