]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalSupport.C
Prevent crash when clicking on either the external or the include insets.
[lyx.git] / src / insets / ExternalSupport.C
1 /**
2  * \file ExternalSupport.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ExternalSupport.h"
15 #include "ExternalTemplate.h"
16 #include "ExternalTransforms.h"
17 #include "insetexternal.h"
18
19 #include "buffer.h"
20 #include "converter.h"
21 #include "debug.h"
22
23 #include "support/filetools.h"
24 #include "support/forkedcall.h"
25 #include "support/lstrings.h"
26 #include "support/lyxalgo.h"
27 #include "support/lyxlib.h"
28 #include "support/path.h"
29 #include "support/path_defines.h"
30
31 #include "support/std_ostream.h"
32
33 namespace support = lyx::support;
34
35 using std::endl;
36
37 using std::ostream;
38 using std::string;
39 using std::vector;
40
41
42 namespace lyx {
43 namespace external {
44
45 Template const * getTemplatePtr(InsetExternalParams const & params)
46 {
47         TemplateManager const & etm = TemplateManager::get();
48         return etm.getTemplateByName(params.templatename());
49 }
50
51
52 void editExternal(InsetExternalParams const & params, Buffer const & buffer)
53 {
54         Template const * const et_ptr = getTemplatePtr(params);
55         if (!et_ptr)
56                 return;
57         Template const & et = *et_ptr;
58
59         if (et.editCommand.empty())
60                 return;
61
62         string const command = doSubstitution(params, buffer, et.editCommand);
63
64         support::Path p(buffer.filePath());
65         support::Forkedcall call;
66         if (lyxerr.debugging(Debug::EXTERNAL)) {
67                 lyxerr << "Executing '" << command << "' in '"
68                        << buffer.filePath() << '\'' << endl;
69         }
70         call.startscript(support::Forkedcall::DontWait, command);
71 }
72
73
74 namespace {
75
76 /** Substitute meta-variables in the string \p s.
77     \p filename has to be the filename as read from the .lyx file (this
78     can be an absolute path or a path relative to the parent document).
79     Otherwise, the $$AbsOrRelPath* variables would not work.
80     If we are using a temporary directory, \p filename is the mangled name.
81 */
82 string const doSubstitution(InsetExternalParams const & params,
83                             Buffer const & buffer, string const & s,
84                             string const & filename)
85 {
86         string result;
87         string const basename = support::ChangeExtension(
88                         support::OnlyFilename(filename), string());
89         string const absname = support::MakeAbsPath(filename, buffer.filePath());
90         string const filepath = support::OnlyPath(filename);
91         string const abspath = support::OnlyPath(absname);
92         Buffer const * m_buffer = buffer.getMasterBuffer();
93         string relToMasterPath = support::OnlyPath(
94                         support::MakeRelPath(absname, m_buffer->filePath()));
95         if (relToMasterPath == "./")
96                 relToMasterPath.clear();
97         string relToParentPath = support::OnlyPath(
98                         support::MakeRelPath(absname, buffer.filePath()));
99         if (relToParentPath == "./")
100                 relToParentPath.clear();
101
102         result = support::subst(s, "$$FName", filename);
103         result = support::subst(result, "$$Basename", basename);
104         result = support::subst(result, "$$Extension",
105                         '.' + support::GetExtension(filename));
106         result = support::subst(result, "$$FPath", filepath);
107         result = support::subst(result, "$$AbsPath", abspath);
108         result = support::subst(result, "$$RelPathMaster", relToMasterPath);
109         result = support::subst(result, "$$RelPathParent", relToParentPath);
110         if (support::AbsolutePath(filename)) {
111                 result = support::subst(result, "$$AbsOrRelPathMaster",
112                                         abspath);
113                 result = support::subst(result, "$$AbsOrRelPathParent",
114                                         abspath);
115         } else {
116                 result = support::subst(result, "$$AbsOrRelPathMaster",
117                                         relToMasterPath);
118                 result = support::subst(result, "$$AbsOrRelPathParent",
119                                         relToParentPath);
120         }
121         result = support::subst(result, "$$Tempname", params.tempname());
122         result = support::subst(result, "$$Sysdir", support::system_lyxdir());
123
124         // Handle the $$Contents(filename) syntax
125         if (support::contains(result, "$$Contents(\"")) {
126
127                 string::size_type const pos = result.find("$$Contents(\"");
128                 string::size_type const end = result.find("\")", pos);
129                 string const file = result.substr(pos + 12, end - (pos + 12));
130                 string contents;
131
132                 string const filepath = support::IsFileReadable(file) ?
133                         buffer.filePath() : m_buffer->temppath();
134                 support::Path p(filepath);
135
136                 if (support::IsFileReadable(file))
137                         contents = support::GetFileContents(file);
138
139                 result = support::subst(result,
140                                         ("$$Contents(\"" + file + "\")").c_str(),
141                                         contents);
142         }
143
144         return result;
145 }
146
147 /** update the file represented by the template.
148     If \param external_in_tmpdir == true, then the generated file is
149     place in the buffer's temporary directory.
150 */
151 void updateExternal(InsetExternalParams const & params,
152                     string const & format,
153                     Buffer const & buffer,
154                     bool external_in_tmpdir)
155 {
156         Template const * const et_ptr = getTemplatePtr(params);
157         if (!et_ptr)
158                 return; // FAILURE
159         Template const & et = *et_ptr;
160
161         if (!et.automaticProduction)
162                 return; // NOT_NEEDED
163
164         Template::Formats::const_iterator cit = et.formats.find(format);
165         if (cit == et.formats.end())
166                 return; // FAILURE
167
168         Template::Format const & outputFormat = cit->second;
169         if (outputFormat.updateResult.empty())
170                 return; // NOT_NEEDED
171
172         string from_format = et.inputFormat;
173         if (from_format.empty())
174                 return; // NOT_NEEDED
175
176         string abs_from_file = params.filename.absFilename();
177
178         if (from_format == "*") {
179                 if (abs_from_file.empty())
180                         return; // NOT_NEEDED
181
182                 // Try and ascertain the file format from its contents.
183                 from_format = support::getExtFromContents(abs_from_file);
184                 if (from_format.empty())
185                         return; // FAILURE
186
187         }
188
189         string const to_format = outputFormat.updateFormat;
190         if (to_format.empty())
191                 return; // NOT_NEEDED
192
193         if (!converters.isReachable(from_format, to_format)) {
194                 lyxerr[Debug::EXTERNAL]
195                         << "external::updateExternal. "
196                         << "Unable to convert from "
197                         << from_format << " to " << to_format << endl;
198                 return; // FAILURE
199         }
200
201         string from_file = params.filename.outputFilename(buffer.filePath());
202
203         // The master buffer. This is useful when there are multiple levels
204         // of include files
205         Buffer const * m_buffer = buffer.getMasterBuffer();
206
207         if (external_in_tmpdir && !abs_from_file.empty()) {
208                 // We are running stuff through LaTeX
209                 string const temp_file =
210                         support::MakeAbsPath(params.filename.mangledFilename(),
211                                              m_buffer->temppath());
212                 unsigned long const from_checksum = support::sum(abs_from_file);
213                 unsigned long const temp_checksum = support::sum(temp_file);
214
215                 if (from_checksum != temp_checksum) {
216                         if (!support::copy(abs_from_file, temp_file)) {
217                                 lyxerr[Debug::EXTERNAL]
218                                         << "external::updateExternal. "
219                                         << "Unable to copy "
220                                         << abs_from_file << " to " << temp_file << endl;
221                                 return; // FAILURE
222                         }
223                 }
224
225                 from_file = temp_file;
226                 abs_from_file = temp_file;
227         }
228
229         string const to_file = doSubstitution(params, buffer,
230                                               outputFormat.updateResult,
231                                               from_file);
232
233         string const abs_to_file =
234                 support::MakeAbsPath(to_file, external_in_tmpdir
235                         ? m_buffer->temppath()
236                         : buffer.filePath());
237
238         // Do we need to perform the conversion?
239         // Yes if to_file does not exist or if from_file is newer than to_file
240         if (support::compare_timestamps(abs_from_file, abs_to_file) < 0)
241                 return; // SUCCESS
242
243         string const to_file_base =
244                 support::ChangeExtension(to_file, string());
245         /* bool const success = */
246                 converters.convert(&buffer, abs_from_file, to_file_base,
247                                    from_format, to_format);
248         // return success
249 }
250
251
252 string const substituteCommands(InsetExternalParams const & params,
253                                 string const & input, string const & format);
254
255 string const substituteOptions(InsetExternalParams const & params,
256                                string const & input, string const & format);
257
258 } // namespace anon
259
260
261 int writeExternal(InsetExternalParams const & params,
262                   string const & format,
263                   Buffer const & buffer, ostream & os,
264                   bool external_in_tmpdir)
265 {
266         Template const * const et_ptr = getTemplatePtr(params);
267         if (!et_ptr)
268                 return 0;
269         Template const & et = *et_ptr;
270
271         Template::Formats::const_iterator cit = et.formats.find(format);
272         if (cit == et.formats.end()) {
273                 lyxerr[Debug::EXTERNAL]
274                         << "External template format '" << format
275                         << "' not specified in template "
276                         << params.templatename() << endl;
277                 return 0;
278         }
279
280         updateExternal(params, format, buffer, external_in_tmpdir);
281
282         string from_file = params.filename.outputFilename(buffer.filePath());
283         if (external_in_tmpdir && !from_file.empty()) {
284                 // We are running stuff through LaTeX
285                 from_file =
286                         support::MakeAbsPath(params.filename.mangledFilename(),
287                                              buffer.getMasterBuffer()->temppath());
288         }
289
290         string str = doSubstitution(params, buffer, cit->second.product,
291                                     from_file);
292         str = substituteCommands(params, str, format);
293         str = substituteOptions(params, str, format);
294         os << str;
295         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
296 }
297
298
299 /// Substitute meta-variables in this string
300 string const doSubstitution(InsetExternalParams const & params,
301                             Buffer const & buffer, string const & s)
302 {
303         string const buffer_path = buffer.filePath();
304         string const filename = params.filename.outputFilename(buffer_path);
305         return doSubstitution(params, buffer, s, filename);
306 }
307
308 namespace {
309
310 // Empty template, specialised below.
311 template <typename TransformType>
312 string const substituteIt(string const &,
313                           TransformID,
314                           string const &,
315                           Template::Format const &,
316                           InsetExternalParams const &);
317
318
319 template <>
320 string const substituteIt<TransformCommand>(string const & input,
321                                             TransformID id,
322                                             string const & /* formatname */,
323                                             Template::Format const & format,
324                                             InsetExternalParams const & params)
325 {
326         typedef std::map<TransformID, TransformStore> Transformers;
327         Transformers::const_iterator it = format.command_transformers.find(id);
328         if (it == format.command_transformers.end())
329                 return input;
330
331         TransformStore const & store = it->second;
332
333         TransformCommand::ptr_type ptr;
334         if (id == Rotate)
335                 ptr = store.getCommandTransformer(params.rotationdata);
336         else if (id == Resize)
337                 ptr = store.getCommandTransformer(params.resizedata);
338
339         if (!ptr.get())
340                 return input;
341
342         string result =
343                 support::subst(input, ptr->front_placeholder(), ptr->front());
344         return support::subst(result, ptr->back_placeholder(),  ptr->back());
345 }
346
347
348 template <>
349 string const substituteIt<TransformOption>(string const & input,
350                                            TransformID id,
351                                            string const & fname,
352                                            Template::Format const & format,
353                                            InsetExternalParams const & params)
354 {
355         typedef std::map<TransformID, TransformStore> Transformers;
356         Transformers::const_iterator it = format.option_transformers.find(id);
357         if (it == format.option_transformers.end())
358                 return input;
359
360         TransformStore const & store = it->second;
361
362         TransformOption::ptr_type ptr;
363         switch (id) {
364         case Clip:
365                 ptr = store.getOptionTransformer(params.clipdata);
366                 break;
367         case Extra:
368                 ptr = store.getOptionTransformer(params.extradata.get(fname));
369                 break;
370         case Rotate:
371                 ptr = store.getOptionTransformer(params.rotationdata);
372                 break;
373         case Resize:
374                 ptr = store.getOptionTransformer(params.resizedata);
375                 break;
376         }
377
378         if (!ptr.get())
379                 return input;
380
381         return support::subst(input, ptr->placeholder(), ptr->option());
382 }
383
384
385 template <typename TransformerType>
386 string const transformIt(InsetExternalParams const & params,
387                          string const & s, string const & formatname)
388 {
389         Template const * const et = getTemplatePtr(params);
390         if (!et || et->transformIds.empty())
391                 return s;
392
393         Template::Formats::const_iterator fit = et->formats.find(formatname);
394         if (fit == et->formats.end())
395                 return s;
396
397         string result = s;
398         Template::Format const & format =  fit->second;
399
400         typedef vector<TransformID> TransformsIDs;
401         TransformsIDs::const_iterator it  = et->transformIds.begin();
402         TransformsIDs::const_iterator end = et->transformIds.end();
403         for (; it != end; ++it) {
404                 result = substituteIt<TransformerType>(result, *it, formatname,
405                                                        format, params);
406         }
407         return result;
408 }
409
410
411 string const substituteCommands(InsetExternalParams const & params,
412                                 string const & input, string const & format)
413 {
414         return transformIt<TransformCommand>(params, input, format);
415 }
416
417
418 string const substituteOption(InsetExternalParams const & params,
419                               string const & input, string const & format)
420 {
421         string opt = transformIt<TransformOption>(params, input, format);
422
423         if (format == "LaTeX" || format == "PDFLaTeX")
424                 return sanitizeLatexOption(opt);
425         if (format == "DocBook")
426                 return sanitizeDocBookOption(opt);
427         if (format == "LinuxDoc")
428                 return sanitizeLinuxDocOption(opt);
429         return opt;
430 }
431
432
433 string const substituteOptions(InsetExternalParams const & params,
434                                string const & input, string const & format)
435 {
436         string output = input;
437
438         Template const * const et = getTemplatePtr(params);
439         if (!et || et->transformIds.empty())
440                 return output;
441
442         Template::Formats::const_iterator fit = et->formats.find(format);
443         if (fit == et->formats.end() || fit->second.options.empty())
444                 return output;
445
446         typedef vector<Template::Option> Options;
447         Options const & options = fit->second.options;
448         Options::const_iterator it  = options.begin();
449         Options::const_iterator end = options.end();
450         for (; it != end; ++it) {
451                 string const opt = substituteOption(params, it->option, format);
452                 string const placeholder = "$$" + it->name;
453                 output = support::subst(output, placeholder, opt);
454         }
455
456         return output;
457  }
458
459 } // namespace anon
460
461 } // namespace external
462 } // namespace lyx