I know about \ifeof18 and \pdfshellescape to detect, if \write18 is enabled, but both only tell me, if it's enabled at all. I need to use a program which isn't in the list of commands restricted \write18is allowed to use and thus want to check, if it's restricted. How do you do this properly?
- 5,702
2 Answers
You can interrogate the engine with the suitable expl3 functions. This works cross-engines.
\documentclass{article}
\ExplSyntaxOn
\iow_term:e
{
*************^^J *~
\int_case:nn { \c_sys_shell_escape_int }
{
{0}{No~shell~escape}
{1}{Unrestricted~shell~escape}
{2}{Restricted~shell~escape}
}
^^J*************
}
\sys_if_shell_restricted:T
{
\sys_get_shell:nnN
{ kpsewhich~--var-value=shell_escape_commands }
{ \cctab_select:N \c_document_cctab }
\l_tmpa_tl
\iow_term:e
{
*************^^J
~Restricted~shell-escape~is~available;~the~allowed~programs~are^^J
~\l_tmpa_tl ^^J
*************
}
}
\ExplSyntaxOff
\stop
Output with pdflatex
This is pdfTeX, Version 3.141592653-2.6-1.40.25 (TeX Live 2023) (preloaded format=pdflatex)
[...]
***************
* Restricted shell escape
***************
(|kpsewhich --var-value=shell_escape_commands)
***************
* Restricted shell-escape is available; the allowed programs are
* bibtex,bibtex8,extractbb,gregorio,kpsewhich,makeindex,memoize-extract.pl,memo
ize-extract.py,repstopdf,r-mpost,texosquery-jre8,
***************
Output with lualatex -no-shell-escape
This is LuaHBTeX, Version 1.17.0 (TeX Live 2023)
[...]
***************
* No shell escape
***************
Output with uplatex -shell-escape
This is e-upTeX, Version 3.141592653-p4.1.0-u1.29-230214-2.6 (utf8.uptex) (TeX Live 2023) (preloaded format=uplatex)
[...]
***************
* Unrestricted shell escape
***************
Original answer 2012
The best method for testing the shell-escape is use the pdftexcmds package:
\usepackage{pdftexcmds}
\makeatletter
\ifcase\pdf@shellescape
\message{No shell escape}\or
\message{Unrestricted shell escape}\or
\message{Restricted shell escape}\fi
\makeatother
Of course you can put any code you want. Note that on older TeX distributions this can give wrong results when LuaTeX (version number less than 0.68) is used.
The package uses the integer \pdfshellescape if pdftex is used, \shellescape with xetex and emulates the register with a Lua function with LuaTeX.
The integer's value is
0, if the shell escape is disabled (a call like
pdflatex -no-shell-escape)1, if the shell escape is unrestricted (a call like
pdflatex -shell-escape)2, if the shell escape is restricted (a simple call
pdflatex)
(the above holds under normal setting of the variable shell_escape in texmf.cnf to the value p).
With pdflatex you can list the allowed programs in restricted shell escape with
\ifnum\pdf@shellescape=\tw@
\begingroup
\everyeof{\noexpand}
\typeout{%
***************^^J%
* Restricted shell-escape is available; the allowed programs are^^J%
* \@@input|"kpsewhich --var-value shell_escape_commands" ^^J%
***************}
\endgroup
but unfortunately this can't be done with xelatex and lualatex (for the latter some Lua function might help). The output is
***************
* Restricted shell-escape is available; the allowed programs are
* bibtex,bibtex8,kpsewhich,makeindex,mpost,repstopdf,
***************
- 1,121,712
-
This works great, I just read the documentation of pdftexcmds. There seems to be no way to get the list of commands that are allowed though. Is this also possible? – Max Dec 30 '12 at 15:46
I'm doing this:
\ifnum\ShellEscapeStatus=1%
% -shell-escape is ON
\else%
\message{Oops, it's OFF :( ^^J}%
\fi%
- 12,021
\pdfshellescapeequals 2. In XeTeX it's\shellescapeand in LuaTeX it's missing. With thepdftexcmdspackage you can test the status of the shell independently from the engine. – egreg Dec 30 '12 at 15:13