]> git.lyx.org Git - lyx.git/blob - src/Literate.C
da0ad20dbb7bcadefd38a793f1eeebed1cc9b376
[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 "error.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(string(_("Weaving document")),
59                      Error::LATEX);
60         minib->Set(string(_("Weaving document")));
61         minib->Store();
62
63         // Run the literate program to convert \literate_extension file to .tex file
64         //
65         tmp1 = literate_cmd + " < " + litfile + " > " + file + " 2> " + litfile + ".out";
66         tmp2 = literate_filter + " < " + litfile + ".out" + " > " + litfile + ".log";
67         ret1 = one.Startscript(Systemcalls::System, tmp1);
68         ret2 = two.Startscript(Systemcalls::System, tmp2);
69         lyxerr.debug(string(_("LITERATE")) + " {" + tmp1 + "} {" + tmp2 + "}");
70         scanres = scanLiterateLogFile(terr);
71         if (scanres & Literate::ERRORS) return scanres; // return on literate error
72
73         return run(terr, minib);
74 }
75
76
77 int Literate::build(TeXErrors &terr, MiniBuffer *minib)
78         // We know that this function will only be run if the lyx buffer
79         // has been changed. 
80 {
81         int scanres = Literate::NO_ERRORS;
82         num_errors = 0; // just to make sure.
83         // DepTable head; // empty head // unused
84         // bool rerun = false; // rerun requested // unused
85         string tmp1, tmp2;
86         int ret1, ret2;
87         Systemcalls one, two;
88         
89         // The class LaTeX does not know the temp path.
90         bufferlist.updateIncludedTeXfiles(GetCWD());
91         
92         lyxerr.debug(string(_("Building program")), 
93                      Error::LATEX);
94         minib->Set(string(_("Building program")));
95         minib->Store();
96
97         // Run the build program
98         //
99         tmp1 = build_cmd + ' ' + litfile + " > " + litfile + ".out 2>&1";
100         tmp2 = build_filter + " < " + litfile + ".out" + " > " + litfile + ".log";
101         ret1 = one.Startscript(Systemcalls::System, tmp1);
102         ret2 = two.Startscript(Systemcalls::System, tmp2);
103         scanres = scanBuildLogFile(terr);
104         lyxerr.debug("Done.", Error::LATEX);
105
106         return scanres;
107 }
108
109
110 int Literate::scanLiterateLogFile(TeXErrors &terr)
111 {
112         string token;
113         int retval = NO_ERRORS;
114         
115         LyXLex lex(0, 0);
116  
117         string tmp = litfile + ".log";
118         
119         if (!lex.setFile(tmp)) {
120                 // unable to open file
121                 // return at once
122                 retval |= NO_LOGFILE;
123                 return retval;
124         }
125         
126         while (lex.IsOK()) {
127                 if (lex.EatLine())
128                         token = lex.GetString();
129                 else // blank line in the file being read
130                         continue;
131  
132                 lyxerr.debug(token, Error::LATEX);
133                 
134                 if (prefixIs(token, "Build Warning:")) {
135                         // Here shall we handle different
136                         // types of warnings
137                         retval |= LATEX_WARNING;
138                         lyxerr.debug("Build Warning.", Error::LATEX);
139                 } else if (prefixIs(token, "! Build Error:")) {
140                         // Here shall we handle different
141                         // types of errors
142                         retval |= LATEX_ERROR;
143                         lyxerr.debug("Build Error.", Error::LATEX);
144                         // this is not correct yet
145                         terr.scanError(lex);
146                         num_errors++;
147                 }
148         }       
149         return retval;
150 }
151
152
153 int Literate::scanBuildLogFile(TeXErrors &terr)
154 {
155         string token;
156         int retval = NO_ERRORS;
157         
158         LyXLex lex(0, 0);
159  
160         string tmp = litfile + ".log";
161         
162         if (!lex.setFile(tmp)) {
163                 // unable to open file
164                 // return at once
165                 retval |= NO_LOGFILE;
166                 return retval;
167         }
168         
169         while (lex.IsOK()) {
170                 if (lex.EatLine())
171                         token = lex.GetString();
172                 else // blank line in the file being read
173                         continue;
174  
175                 lyxerr.debug(token, Error::LATEX);
176                 
177                 if (prefixIs(token, "Build Warning:")) {
178                         // Here shall we handle different
179                         // types of warnings
180                         retval |= LATEX_WARNING;
181                         lyxerr.debug("Build Warning.", Error::LATEX);
182                 } else if (prefixIs(token, "! Build Error:")) {
183                         // Here shall we handle different
184                         // types of errors
185                         retval |= LATEX_ERROR;
186                         lyxerr.debug("Build Error.", Error::LATEX);
187                         // this is not correct yet
188                         terr.scanError(lex);
189                         num_errors++;
190                 }
191         }       
192         return retval;
193 }
194
195