[latex3-commits] [git/LaTeX3-latex3-latex3] luacmd: Move shell escape to pseudo primitives (3f12009d7)

Marcel Fabian Krüger tex at 2krueger.de
Sat Aug 22 18:57:03 CEST 2020


Repository : https://github.com/latex3/latex3
On branch  : luacmd
Link       : https://github.com/latex3/latex3/commit/3f12009d730900d9a0ad527fff39fa434fcffe43

>---------------------------------------------------------------

commit 3f12009d730900d9a0ad527fff39fa434fcffe43
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Sat Aug 22 18:36:49 2020 +0200

    Move shell escape to pseudo primitives
    
    Two years from know I would probably use token.scan_argument(false) to scan
    the non expanded strings, but that parameter doesn't work yet in
    released versions.


>---------------------------------------------------------------

3f12009d730900d9a0ad527fff39fa434fcffe43
 l3kernel/l3.ins       |  3 ++-
 l3kernel/l3luatex.dtx |  6 ++---
 l3kernel/l3sys.dtx    | 63 ++++++++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 60 insertions(+), 12 deletions(-)

diff --git a/l3kernel/l3.ins b/l3kernel/l3.ins
index 4498c5b21..874790b95 100644
--- a/l3kernel/l3.ins
+++ b/l3kernel/l3.ins
@@ -70,7 +70,7 @@ and all files in that bundle must be distributed together.
         \from{l3int.dtx}        {package}
         \from{l3flag.dtx}       {package}
         \from{l3prg.dtx}        {package}
-        \from{l3sys.dtx}        {package}
+        \from{l3sys.dtx}        {package,tex}
         \from{l3clist.dtx}      {package}
         \from{l3token.dtx}      {package}
         \from{l3prop.dtx}       {package}
@@ -168,6 +168,7 @@ and all files in that bundle must be distributed together.
 \generate{\file{expl3.lua}{
   \from{l3luatex.dtx}{package,lua}
   \from{l3names.dtx}{package,lua}
+  \from{l3sys.dtx}{package,lua}
 }}
 
 \endbatchfile
diff --git a/l3kernel/l3luatex.dtx b/l3kernel/l3luatex.dtx
index 1482c0d7f..3e1c58679 100644
--- a/l3kernel/l3luatex.dtx
+++ b/l3kernel/l3luatex.dtx
@@ -360,7 +360,6 @@ local lfs_attr   = lfs.attributes
 local open       = io.open
 local os_clock   = os.clock
 local os_date    = os.date
-local os_exec    = os.execute
 local setcatcode = tex.setcatcode
 local sprint     = tex.sprint
 local cprint     = tex.cprint
@@ -619,7 +618,8 @@ end)
 % \begin{macro}{l3kernel.shellescape}
 %   Replicating the \pdfTeX{} log interaction for shell escape.
 %    \begin{macrocode}
-function l3kernel.shellescape(cmd)
+local os_exec    = os.execute
+deprecated(l3kernel, "shellescape", function(cmd)
   local status,msg = os_exec(cmd)
   if status == nil then
     write_nl("log","runsystem(" .. cmd .. ")...(" .. msg .. ")\n")
@@ -628,7 +628,7 @@ function l3kernel.shellescape(cmd)
   else
     write_nl("log","runsystem(" .. cmd .. ")...failed " .. (msg or "") .. "\n")
   end
-end
+end)
 %    \end{macrocode}
 % \end{macro}
 %
diff --git a/l3kernel/l3sys.dtx b/l3kernel/l3sys.dtx
index a4de84f8b..d5dfd970d 100644
--- a/l3kernel/l3sys.dtx
+++ b/l3kernel/l3sys.dtx
@@ -317,6 +317,7 @@
 %
 %    \begin{macrocode}
 %<*package>
+%<*tex>
 %    \end{macrocode}
 %
 % \subsubsection{Detecting the engine}
@@ -637,34 +638,78 @@
 % \end{variable}
 %
 % \begin{macro}{\sys_shell_now:n}
+% \begin{macro}{\@@_shell_now:e}
 %   Execute commands through shell escape immediately.
+%
+%   For \LuaTeX{}, we use a pseudo-primitive to do the actual work.
+%    \begin{macrocode}
+%</tex>
+%<*lua>
+do
+  local os_exec = os.execute
+
+  local function shellescape(cmd)
+    local status,msg = os_exec(cmd)
+    if status == nil then
+      write_nl("log","runsystem(" .. cmd .. ")...(" .. msg .. ")\n")
+    elseif status == 0 then
+      write_nl("log","runsystem(" .. cmd .. ")...executed\n")
+    else
+      write_nl("log","runsystem(" .. cmd .. ")...failed " .. (msg or "") .. "\n")
+    end
+  end
+  luacmd("@@_shell_now:e", function()
+    shellescape(scan_string())
+  end, "global", "protected")
+%</lua>
+%    \end{macrocode}
+%
 %    \begin{macrocode}
+%<*tex>
 \sys_if_engine_luatex:TF
   {
     \cs_new_protected:Npn \sys_shell_now:n #1
-      {
-        \lua_now:e
-          { l3kernel.shellescape(" \lua_escape:e { \tl_to_str:n {#1} } ") }
-      }
+      { \@@_shell_now:e { \exp_not:n {#1} } }
   }
   {
     \cs_new_protected:Npn \sys_shell_now:n #1
       { \iow_now:Nn \c_@@_shell_stream_int {#1} }
   }
 \cs_generate_variant:Nn \sys_shell_now:n { x }
+%</tex>
 %    \end{macrocode}
 % \end{macro}
+% \end{macro}
 %
 % \begin{macro}{\sys_shell_shipout:n}
+% \begin{macro}{\@@_shell_shipout:e}
 %   Execute commands through shell escape at shipout.
+%
+%   For \LuaTeX, we use the same helper as above but delayed to using a late_lua whatsit.
+%    \begin{macrocode}
+%<*lua>
+  local whatsit_id = node.id'whatsit'
+  local latelua_sub = node.subtype'late_lua'
+  local node_new = node.direct.new
+  local setfield = node.direct.setwhatsitfield or node.direct.setfield
+  local node_write = node.direct.write
+
+  luacmd("@@_shell_shipout:e", function()
+    local cmd = scan_string()
+    local n = node_new(whatsit_id, latelua_sub)
+    setfield(n, 'data', function() shellescape(cmd) end)
+    node_write(n)
+  end, "global", "protected")
+end
+%</lua>
+%    \end{macrocode}
+%
 %    \begin{macrocode}
+%<*tex>
 \sys_if_engine_luatex:TF
   {
     \cs_new_protected:Npn \sys_shell_shipout:n #1
-      {
-        \lua_shipout_e:n
-          { l3kernel.shellescape(" \lua_escape:e { \tl_to_str:n {#1} } ") }
-      }
+    { \@@_shell_shipout:e { \exp_not:n {#1} } }
   }
   {
     \cs_new_protected:Npn \sys_shell_shipout:n #1
@@ -673,6 +718,7 @@
 \cs_generate_variant:Nn \sys_shell_shipout:n { x }
 %    \end{macrocode}
 % \end{macro}
+% \end{macro}
 %
 % \subsection{Dynamic (every job) code}
 %
@@ -959,6 +1005,7 @@
 % \end{variable}
 %
 %    \begin{macrocode}
+%</tex>
 %</package>
 %    \end{macrocode}
 %





More information about the latex3-commits mailing list.