1 /**
2  * @file   texlive.c
3  * @brief
4  *
5  * Copyright (C) 2009 Gummi Developers
6  * All Rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use,
12  * copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following
15  * conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #include "texlive.h"
31 
32 #include <glib.h>
33 
34 #include "configfile.h"
35 #include "constants.h"
36 #include "latex.h"
37 #include "utils.h"
38 #include "external.h"
39 
40 gboolean pdf_detected = FALSE;
41 gboolean xel_detected = FALSE;
42 gboolean lua_detected = FALSE;
43 
44 /* All the functions for "pure" building with texlive only tools */
45 
texlive_init(void)46 int texlive_init (void) {
47     int texversion = 0;
48 
49     if (external_exists (C_LATEX)) {
50         texversion = external_version2 (EX_TEXLIVE);
51         slog (L_INFO, "Texlive %d was found installed\n", texversion);
52     }
53 
54     if (external_exists (C_PDFLATEX)) {
55         slog (L_INFO, "Typesetter detected: %s\n",
56               external_version (C_PDFLATEX));
57         pdf_detected = TRUE;
58     }
59 
60     if (external_exists (C_XELATEX)) {
61         slog (L_INFO, "Typesetter detected: %s\n",
62               external_version (C_XELATEX));
63         xel_detected = TRUE;
64     }
65 
66     if (external_exists (C_LUALATEX)) {
67         slog (L_INFO, "Typesetter detected: %s\n",
68               external_version (C_LUALATEX));
69         lua_detected = TRUE;
70     }
71     return texversion;
72 }
73 
texlive_active(void)74 gboolean texlive_active (void) {
75     if (pdflatex_active() || xelatex_active() || lualatex_active()) {
76         return TRUE;
77     }
78     return FALSE;
79 }
80 
pdflatex_active(void)81 gboolean pdflatex_active (void) {
82     if (config_value_as_str_equals ("Compile", "typesetter", "pdflatex")) {
83         return TRUE;
84     }
85     return FALSE;
86 }
87 
xelatex_active(void)88 gboolean xelatex_active (void) {
89     if (config_value_as_str_equals ("Compile", "typesetter", "xelatex")) {
90         return TRUE;
91     }
92     return FALSE;
93 }
94 
lualatex_active(void)95 gboolean lualatex_active (void) {
96     if (config_value_as_str_equals ("Compile", "typesetter", "lualatex")) {
97         return TRUE;
98     }
99     return FALSE;
100 }
101 
pdflatex_detected(void)102 gboolean pdflatex_detected (void) {
103     return pdf_detected;
104 }
105 
xelatex_detected(void)106 gboolean xelatex_detected (void) {
107     return xel_detected;
108 }
109 
lualatex_detected(void)110 gboolean lualatex_detected (void) {
111     return lua_detected;
112 }
113 
texlive_get_command(const gchar * method,gchar * workfile,gchar * basename)114 gchar* texlive_get_command (const gchar* method, gchar* workfile, gchar* basename) {
115 
116     const gchar* outdir = g_strdup_printf("-output-directory=\"%s\"", C_TMPDIR);
117 
118 
119     gchar *typesetter = NULL;
120     gchar *texcmd = NULL;
121 
122     if (pdflatex_active()) typesetter = C_PDFLATEX;
123     else if (lualatex_active()) typesetter = C_LUALATEX;
124     else typesetter = C_XELATEX;
125 
126     gchar *flags = texlive_get_flags("texpdf");
127 
128     gchar *dviname = g_strdup_printf("%s.dvi", g_path_get_basename (basename));
129     gchar *psname = g_strdup_printf("%s.ps", g_path_get_basename (basename));
130 
131     #ifdef WIN32
132     gchar *script = g_build_filename (GUMMI_LIBS, "latex_dvi.cmd", NULL);
133     #else
134     gchar *script = g_build_filename (GUMMI_LIBS, "latex_dvi.sh", NULL);
135     #endif
136 
137     if (STR_EQU (method, "texpdf")) {
138 
139         texcmd = g_strdup_printf("%s %s %s \"%s\"", typesetter,
140                                                 flags,
141                                                 outdir,
142                                                 workfile);
143     } else if (STR_EQU (method, "texdvipdf")) {
144         texcmd = g_strdup_printf("%s pdf "
145                 "\"%s\" \"%s\" \"%s\" \"%s\" \"%s\"", script,
146                 flags, outdir, workfile, C_TMPDIR, dviname);
147     } else {
148         texcmd = g_strdup_printf("%s ps "
149                 "\"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"", script,
150                 flags, outdir, workfile, C_TMPDIR, dviname, psname);
151     }
152 
153     g_free(script);
154     g_free(dviname);
155     g_free(psname);
156 
157     return texcmd;
158 }
159 
texlive_get_flags(const gchar * method)160 gchar* texlive_get_flags (const gchar* method) {
161     gchar* flags = g_strdup_printf("-interaction=nonstopmode "
162                                       "-file-line-error "
163                                       "-halt-on-error");
164 
165     if (!latex_use_shellescaping()) {
166         gchar* tmp = g_strconcat(flags, " -no-shell-escape", NULL);
167         g_free(flags);
168         flags = tmp;
169     }
170     else {
171         gchar* tmp = g_strconcat(flags, " -shell-escape", NULL);
172         g_free(flags);
173         flags = tmp;
174     }
175 
176     if (config_get_boolean ("Compile", "synctex")) {
177         gchar* tmp = g_strconcat(flags, " -synctex=1", NULL);
178         g_free(flags);
179         flags = tmp;
180     }
181 
182     return flags;
183 }
184