Debian Codegraph @

texmf-dist/source/latex/tools/shellesc.dtx

raw
1 % \iffalse
2 %% Source File: shellesc.dtx
3 %% Copyright (C) 2015-2025
4 %%
5 %% The LaTeX Project and any individual authors listed elsewhere
6 %% in this file.
7 %%
8 %% This file may be distributed under the terms of the LPPL.
9 %% See README for details.
10 %
11 %<*dtx>
12 \ProvidesFile{shellesc.dtx}
13 %</dtx>
14 %<package>\ifx\ProvidesPackage\undefined
15 %<package>\def\next#1#2[#3]{\wlog{#2 #3}}
16 %<package>\ifx\PackageInfo\undefined\def\PackageInfo#1#2{\wlog{#1: #2}}\fi
17 %<package>\ifx\PackageWarning\undefined\def\PackageWarning#1#2{\wlog{#1: #2}}\fi
18 %<package>\expandafter\next\fi
19 %<package>\ProvidesPackage{shellesc}
20 %<driver> \ProvidesFile{shellesc.drv}
21 % \fi
22 % \ProvidesFile{shellesc.dtx}
23 [2023/07/08 v1.0d unified shell escape interface for LaTeX]
24 %
25 % \iffalse
26 %<*driver>
27 \documentclass{ltxdoc}
28 \begin{document}
29 \DocInput{shellesc.dtx}
30 \end{document}
31 %</driver>
32 % \fi
33 %
34 % \GetFileInfo{shellesc.dtx}
35 %
36 % \title{The \textsf{shellesc} Package\thanks{This file
37 % has version number \fileversion, last
38 % revised \filedate.}}
39 % \author{\LaTeX\ project}
40 % \date{\filedate}
41 %
42 %
43 % \maketitle
44 %
45 % \vspace*{-\baselineskip}
46 % \enlargethispage{\baselineskip}
47 %
48 % \changes{v0.1b}{2016/02/02}{Doc typo fixes (JB)}
49 % \changes{v0.2a}{2016/06/07}{Improve use with plain TeX}
50 % \section{Introduction}
51 %
52 %
53 % For many years web2c-based \TeX\ implementations have used the syntax
54 % of the \verb|\write| command to access system commands by using a
55 % special stream 18 (streams above 15 can not be allocated to files in
56 % classical \TeX\ so stream 18 would otherwise just print to the
57 % terminal).
58 %
59 % This is a useful extension that did not break the strict rules on
60 % extensions in classical \TeX. This package provides a simple
61 % macro level interface hiding the \verb|write18| implementation
62 % so a command to remove a file on a Unix-like system could be
63 % specified using \verb|\ShellEscape{rm file.txt}| (or \verb|del| in
64 % Windows). Note that by default system access is not allowed and
65 % \LaTeX\ will typically need to be called with the \verb|--shell-escape|
66 % command line option.
67 %
68 % The package may be used with standard \texttt{latex} or
69 % \texttt{pdflatex} or \texttt{xetex}, however it is mostly motivated by
70 % \texttt{lualatex} as from Lua\TeX~0.87 onwards Lua\TeX\ does \emph{not}
71 % support the \verb|\write18| syntax to access system commands: it has
72 % 256 write streams and stream 18 can be associated to a file and
73 % (without this package) has no special significance. This packge
74 % defines the same \verb|\ShellEscape| syntax in Lua\LaTeX, but the
75 % implementation is via Lua and the \verb|os.execute| function.
76 %
77 % \verb|\ShellEscape| in fact corresponds to \verb|\immediate\write18|
78 % (or \verb|\directlua|). Very rarely you may need to delay a system
79 % command until the current page is output (when page numbers are
80 % known), for this you could classically use \verb|\write18| (or
81 % (\verb|\latelua|). This package provides \verb|\DelayedShellEscape|
82 % as a common syntax for this use.
83 %
84 % The shell escape status may be queried by checking the integer (chardef)
85 % command \verb|\ShellEscapeStatus|, 0 (disabled) 1 (enabled) 2 (restricted).
86 %
87 % To aid porting existing documents to Lua\TeX~0.87 this package does
88 % overload the \verb|\write| command so that
89 % \verb|\write18{rm file.txt}|
90 % will work with Lua\TeX. Note that the redefinition of \verb|\write|
91 % can not detect whether \verb|\immediate| has been used,
92 % \verb|\immediate| will work as normal when writing to file streams
93 % or the terminal but the special case of stream 18 which is defined to
94 % use \verb|os.execute| always uses \verb|\directlua| (so corresponds
95 % to \verb|\immediate\write18|). In the rare situations that you need
96 % non-immediate \verb|\write18| in a document being ported to current
97 % Lua\TeX, you will need to change to use the
98 % \verb|\DelayedShellEscape| command.
99 %
100 % \section{Implementation}
101 %
102 % \begin{macrocode}
103 %<*package>
104 % \end{macrocode}
105 %
106 % \changes{v1.0b}{2019/10/17}{Catcode protection}
107 % \begin{macrocode}
108 \chardef\shellesc@quotecat\catcode`\"
109 \chardef\shellesc@underscorecat\catcode`\_
110 \@makeother\"
111 \@makeother\_
112 % \end{macrocode}
113 %
114 % \subsection{Status Check}
115 %
116 %
117 % \subsection{The shellesc package interface}
118 %
119 % \begin{macro}{\ShellEscapeStatus}
120 % \changes{v1.0a}{2019/10/13}{Command Introduced}
121 % Integer value with meanings 0 (shell escape disabled), 1 (shell escape allowed), 2 (restricted shell escape).
122 %
123 % \begin{macrocode}
124 \chardef\ShellEscapeStatus
125 \ifx\pdfshellescape\@undefined
126 \ifx\shellescape\@undefined
127 \ifx\directlua\@undefined
128 \z@
129 \else
130 \directlua{%
131 tex.sprint((status.shell_escape or os.execute()) .. " ")}
132 \fi
133 \else
134 \shellescape
135 \fi
136 \else
137 \pdfshellescape
138 \fi
139 % \end{macrocode}
140 % \end{macro}
141 %
142 % \changes{v0.2a}{2016/06/07}{spelling in messages}
143 % \begin{macrocode}
144 \ifcase\ShellEscapeStatus
145 \PackageWarning{shellesc}{Shell escape disabled}
146 \or
147 \PackageInfo {shellesc}{Unrestricted shell escape enabled}
148 \else
149 \PackageInfo {shellesc}{Restricted shell escape enabled}
150 \fi
151 % \end{macrocode}
152 %
153 %
154 % \begin{macro}{\ShellEscape}
155 % \changes{v1.0a}{2019/10/13}{Lua logging for gh/195}
156 % Execute the supplied tokens as a system dependent command, assuming
157 % such execution is allowed.
158 % \begin{macrocode}
159 \ifx\lastsavedimageresourcepages\@undefined
160 \protected\def\ShellEscape{\immediate\write18 }
161 % \end{macrocode}
162 %
163 % \begin{macrocode}
164 \else
165 \protected\def\ShellEscape{\directlua\ShellEscape@Lua}
166 \fi
167 % \end{macrocode}
168 % \end{macro}
169 %
170 % \begin{macro}{\DelayedShellEscape}
171 % \changes{v0.1c}{2016/04/29}{Define \cs{DelayedShellEscape} not \cs{ShellEscape}(UF)}
172 % \changes{v1.0a}{2019/10/13}{Lua logging for gh/195}
173 % Execute the supplied tokens as a system dependent command, when this
174 % node is shipped out with the completed page, assuming
175 % such execution is allowed.
176 % \begin{macrocode}
177 \ifx\lastsavedimageresourcepages\@undefined
178 \protected\def\DelayedShellEscape{\relax\write18 }
179 % \end{macrocode}
180 %
181 % \begin{macrocode}
182 \else
183 \protected\def\DelayedShellEscape{\latelua\ShellEscape@Lua}
184 \fi
185 % \end{macrocode}
186 % \end{macro}
187 %
188 %
189 %
190 % \begin{macro}{\ShellEscape@Lua}
191 % \changes{v1.0a}{2019/10/13}{loging for gh/195}
192 % \changes{v1.0d}{2023/04/15}{Add dots in messages gh/1008}
193 % Shared Lua code for \verb|\DelayedShellEscape| and \verb|\ShellEscape|.
194 % \begin{macrocode}
195 \ifx\directlua\@undefined\else
196 \protected\def\ShellEscape@Lua#1{{%
197 local status, msg = os.execute("\luaescapestring{#1}")%
198 if status == nil then
199 texio.write_nl("log",%
200 "runsystem(" .. "\luaescapestring{#1}"%
201 .. ")...(" .. msg .. ").\string\n")
202 elseif status == 0 then
203 texio.write_nl("log",%
204 "runsystem(" .. "\luaescapestring{#1}"%
205 .. ")...executed.\string\n")
206 else
207 texio.write_nl("log",%
208 "runsystem(" .. "\luaescapestring{#1}"%
209 .. ")...failed. " .. (msg or "") .. "\string\n")
210 end
211 }}
212 \fi
213 % \end{macrocode}
214 % \end{macro}
215 %
216 % \subsection{The write18 package interface}
217 %
218 % In web2c-based engines other than Lua\TeX, |\write18| may be used
219 % directly. The same was true in older Lua\TeX, but from version 0.85
220 % onwards that is not available.
221 %
222 % The above |shellesc| package interface is recommended for new code,
223 % however for ease of porting existing documents and packages to newer
224 % Lua\TeX\ releases, a |\write18| interface is provided here via a
225 % call to Lua's |os.execute|.
226 %
227 % Note that as currently written this always does an \emph{immediate}
228 % call to the system.
229 %
230 % |\immediate| is supported but ignored, |\immediate\write18| and
231 % |\write18| both execute immediately. To use a delayed execution at
232 % the next shipout, use the |\DelayedShellEscape| command defined
233 % above.
234 %
235 % Note that it would be easy to make |\wriete18| defined here use
236 % delayed execution, just use |\DelayedShellEscape| instead of
237 % |ShellEscape| in the definition below. However detecting
238 % |\immediate| is tricky so the choice here is to always use the
239 % immediate form, which is overwhelmingly more commonly used with
240 % |\write18|.
241 %
242 % Stop at this point if not a recent Lua\TeX.
243 % \begin{macrocode}
244 \ifx\lastsavedimageresourcepages\@undefined
245 \catcode`\"\shellesc@quotecat
246 \catcode`\_\shellesc@underscorecat
247 \expandafter\endinput
248 \fi
249 % \end{macrocode}
250 %
251 % \begin{macrocode}
252 \directlua{%
253 % \end{macrocode}
254 %
255 % \begin{macrocode}
256 shellesc = shellesc or {}
257 % \end{macrocode}
258 %
259 % Lua function to use the token scanner to grab the following \TeX\
260 % number, and then test if stream 18 is being used, and then insert an
261 % appropriate \TeX\ command to handle the following brace group in
262 % each case.
263 % \begin{macrocode}
264 local function write_or_execute()
265 local s = token.scan_int()
266 if (s==18) then
267 tex.sprint(\the\numexpr\catcodetable@atletter\relax,
268 "\string\\ShellEscape ")
269 else
270 tex.sprint(\the\numexpr\catcodetable@atletter\relax,
271 "\string\\shellesc@write " .. s)
272 end
273 end
274 % \end{macrocode}
275 %
276 % \begin{macrocode}
277 shellesc.write_or_execute=write_or_execute
278 % \end{macrocode}
279 %
280 % \begin{macrocode}
281 }
282 % \end{macrocode}
283 %
284 % \begin{macrocode}
285 \let\shellesc@write\write
286 % \end{macrocode}
287 %
288 % \begin{macrocode}
289 \protected\def\write{\directlua{shellesc.write_or_execute()}}
290 % \end{macrocode}
291 %
292 % \begin{macrocode}
293 \catcode`\"\shellesc@quotecat
294 \catcode`\_\shellesc@underscorecat
295 % \end{macrocode}
296 %
297 % \begin{macrocode}
298 %</package>
299 % \end{macrocode}
300 %
301 % \Finale
302 %