summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/l3experimental/l3sys-shell/l3sys-shell.dtx
blob: 606b372e71e405d5492becdd232755467fcac4ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
% \iffalse meta-comment
%
%% File: l3sys-shell.dtx Copyright(C) 2018 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    http://www.latex-project.org/lppl.txt
%
% This file is part of the "l3experimental bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver|package>
\RequirePackage{expl3}
%</driver|package>
%<*driver>
\documentclass[full]{l3doc}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \pkg{l3sys-shell} package\\ System shell functions^^A
% }
%
% \author{^^A
%  The \LaTeX3 Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
%
% \date{Released 2018-10-31}
%
% \maketitle
%
% \begin{documentation}
%
% \begin{function}[added = 2018-07-28]{\sys_shell_cp:nn}
%   \begin{syntax}
%     \cs{sys_shell_cp:nn} \Arg{source} \Arg{dest}
%   \end{syntax}
%   Copies the files specified in the \meta{source} (which may include
%   wildcards) to the \meta{dest}. The file paths should be specified using
%   |/| as a path separator. Copying is \emph{not} recursive: only files at
%   the path level given are copied.  If unrestricted shell escape is not
%   enabled, no action is attempted.
% \end{function}
%
% \begin{function}[added = 2018-07-27]{\sys_shell_mkdir:n}
%   \begin{syntax}
%     \cs{sys_shell_mkdir:n} \Arg{directory}
%   \end{syntax}
%   Creates the \meta{directory}, which should should  be specified using |/| as
%   a path separator.  If unrestricted shell escape is not enabled, no action is
%   attempted.
% \end{function}
%
% \begin{function}[added = 2018-07-28]{\sys_shell_mv:nn}
%   \begin{syntax}
%     \cs{sys_shell_mv:nn} \Arg{old} \Arg{new}
%   \end{syntax}
%   Moves the files from the \meta{old} to \meta{new} names/locations: the
%   \meta{old} names may include wildcards. In both arguments, |/| should be
%   used as the path separator.  If unrestricted shell escape is not enabled, no
%   action is attempted.
% \end{function}
%
% \begin{function}[added = 2018-07-27]{\sys_shell_rm:n}
%   \begin{syntax}
%     \cs{sys_shell_rm:n} \Arg{files}
%   \end{syntax}
%   Removes the \meta{files} (which may include wildcards). The file path should
%   be specified using |/| as a path separator.  If unrestricted shell escape is
%   not enabled, no action is attempted.
% \end{function}
%
% \begin{function}[added = 2018-07-27]{\sys_shell_rmdir:n}
%   \begin{syntax}
%     \cs{sys_shell_rmdir:n} \Arg{directory}
%   \end{syntax}
%   Removes the \meta{directory}, which should should  be specified using |/| as
%   a path separator.  If unrestricted shell escape is not enabled, no action is
%   attempted.
% \end{function}
%
% \begin{function}[added = 2018-07-28]{\sys_shell_get_pwd:N}
%   \begin{syntax}
%     \cs{sys_shell_get_pwd:N} \meta{str var}
%   \end{syntax}
%   Sets the \meta{str var} to the present working directory (the output of
%   |pwd| on Unix or |cd| on Windows). Note that on Windows this will contain
%   string backslash chars (|\|).  If unrestricted shell escape is not
%   enabled, no action is taken.
% \end{function}
%
% \begin{function}[added = 2018-07-28]{\sys_shell_split_ls:nN}
%   \begin{syntax}
%     \cs{sys_shell_split_ls:N} \Arg{glob} \meta{seq}
%   \end{syntax}
%   Sets the \meta{seq var} to contain one entry per directory listing
%   (equivalent to |ls -1| or |dir /b|) as specified by the \meta{glob}: the
%   entries are strings. If unrestricted shell is not enabled, no action is
%   taken.
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3sys-shell} implementation}
%
%    \begin{macrocode}
%<*initex|package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=sys>
%    \end{macrocode}
%
%    \begin{macrocode}
%<*package>
\ProvidesExplPackage{l3sys-shell}{2018-10-31}{}
  {L3 Experimental system shell functions}
%</package>
%    \end{macrocode}
%
% \begin{macro}[EXP]{\@@_path_to_win:n}
% \begin{macro}[EXP]{\@@_path_to_win:w}
% \begin{macro}[EXP]{\@@_path_to_win:N}
%   A simple expandable search-and-replace for providing Windows-style paths.
%    \begin{macrocode}
\cs_new:Npn \@@_path_to_win:n #1
  {
    \exp_after:wN \@@_path_to_win:w \tl_to_str:n {#1} ~ \q_stop
  }
\cs_new:Npn \@@_path_to_win:w #1 ~ #2 \q_stop
  {
    \@@_path_to_win:N #1 \q_nil
    \tl_if_empty:nF {#2}
      {
        \c_space_tl
        \@@_path_to_win:w #2 \q_stop
      }
  }
\cs_new:Npn \@@_path_to_win:N #1
  {
    \quark_if_nil:NF #1
      {
        \token_if_eq_meaning:NNTF #1 /
          { \c_backslash_str }
          {#1}
         \@@_path_to_win:N
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\sys_shell_cp:nn}
%   Simple Unix-like file copying: at some stage we may need a directory-only
%   version as Windows and Unix have different requirements here.
%    \begin{macrocode}
\cs_new_protected:Npx \sys_shell_cp:nn #1#2
  {
    \sys_if_shell_unrestricted:T
      {
        \sys_shell_now:x
          {
            \sys_if_platform_unix:T
              { 
                cp~-f~ \exp_not:N \tl_to_str:n {#1} ~
                  \exp_not:N \tl_to_str:n {#2}
              }
            \sys_if_platform_windows:T
              {
                copy~/y~ \exp_not:N \@@_path_to_win:n {#1} ~
                  \exp_not:N \@@_path_to_win:n {#2}
              }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sys_shell_mkdir:n}
%   Windows (with the extensions) will automatically make directory trees but
%   issues a warning if the directory already exists: avoid by including a
%   test.
%    \begin{macrocode}
\cs_new_protected:Npx \sys_shell_mkdir:n #1
  {
    \sys_if_shell_unrestricted:T
      {
        \sys_shell_now:x
          {
            \sys_if_platform_unix:T
              { mkdir~-p~ \exp_not:N \tl_to_str:n {#1} }
            \sys_if_platform_windows:T
              {
                if~not~exist~
                  \exp_not:N \@@_path_to_win:n { #1 / nul } ~
                  mkdir~ \exp_not:N \@@_path_to_win:n {#1}
              }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sys_shell_mv:nn}
%   On Windows we do not have a single |mv| operation, so copy-and-delete
%   instead.
%    \begin{macrocode}
\cs_new_protected:Npx \sys_shell_mv:nn #1#2
  {
    \sys_if_shell_unrestricted:T
      {
        \sys_shell_now:x
          {
            \sys_if_platform_unix:T
              {
                mv~ \exp_not:N \tl_to_str:n {#1} ~
                  \exp_not:N \tl_to_str:n {#2}
              }
            \sys_if_platform_windows:T
              {
                copy~/y~ \exp_not:N \@@_path_to_win:n {#1} ~
                  \exp_not:N \@@_path_to_win:n {#2}
                  \token_to_str:N & \token_to_str:N &
                  del~/f~/q~\exp_not:N \@@_path_to_win:n {#1}
              }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sys_shell_rm:n}
%   Deletion: obviously a big health warning here!
%    \begin{macrocode}
\cs_new_protected:Npx \sys_shell_rm:n #1
  {
    \sys_if_shell_unrestricted:T
      {
        \sys_shell_now:x
          {
            \sys_if_platform_unix:T
              { rm~-f~ \exp_not:N \tl_to_str:n {#1}  }
            \sys_if_platform_windows:T
              { del~/f~/q~ \exp_not:N \@@_path_to_win:n {#1} }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sys_shell_rmdir:n}
%   When removing a directory, we create it first as that avoids errors in
%   the Windows case.
%    \begin{macrocode}
\cs_new_protected:Npx \sys_shell_rmdir:n #1
  {
    \sys_if_shell_unrestricted:T
      {
        \sys_shell_mkdir:n {#1}
        \sys_shell_now:x
          {
            \sys_if_platform_unix:T
              { rm~-rf~ \exp_not:N \tl_to_str:n {#1} }
            \sys_if_platform_windows:T
              { rmdir~/s~/q~ \exp_not:N \@@_path_to_win:n {#1} }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{variable}{\l_@@_tmp_tl}
%   Scratch space.
%    \begin{macrocode}
\tl_new:N \l_@@_tmp_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\sys_shell_get_pwd:N}
%   Getting the path is easy: the main work is avoiding loosing any
%   information. (This information can be obtained using the recorder file:
%   that does not require shell escape. However, it's hard to see how it might
%   be useful without the other file functions.)
%    \begin{macrocode}
\cs_new_protected:Npx \sys_shell_get_pwd:N #1
  {
    \sys_if_shell_unrestricted:T
      {
        \exp_not:N \tl_set_from_shell:Nnn \exp_not:N \l_@@_tmp_tl
          {
            \char_set_catcode_other:N \exp_not:N \\
            \char_set_catcode_other:N \exp_not:N \#
            \char_set_catcode_other:N \exp_not:N \~
            \char_set_catcode_other:N \exp_not:N \%
            \char_set_catcode_space:N \exp_not:N \ %
          }
          {
            \sys_if_platform_unix:T { pwd }
            \sys_if_platform_windows:T { cd }
          }
        \str_set:NV #1 \exp_not:N \l_@@_tmp_tl
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\sys_shell_split_ls:nN}
%   Getting a one-per-line listing is easy enough. We need to set
%   \cs{ExplSyntaxOff} as that deals with the end-of-line character. After that,
%   just a case of tidying up. The listing always ends in |^^M| so there is an
%   extra entry to trim.
%    \begin{macrocode}
\cs_new_protected:Npx \sys_shell_split_ls:nN #1#2
  {
    \sys_if_shell_unrestricted:T
      {
        \exp_not:N \tl_set_from_shell:Nnn \exp_not:N \l_@@_tmp_tl
          {
            \ExplSyntaxOff
            \char_set_catcode_other:N \exp_not:N \\
            \char_set_catcode_other:N \exp_not:N \#
            \char_set_catcode_other:N \exp_not:N \~
            \char_set_catcode_other:N \exp_not:N \%
            \char_set_catcode_other:n { 13 }
          }
          {
            \sys_if_platform_unix:T { ls~-1~ #1 }
            \sys_if_platform_windows:T { dir~/b~ #1 }
          }
        \str_set:NV \exp_not:N \l_@@_tmp_tl \exp_not:N \l_@@_tmp_tl
        \seq_set_split:NnV #2 { \char_generate:nn { `\^^M } { 12 } }
          \exp_not:N \l_@@_tmp_tl
        \seq_pop_right:NN #2 \exp_not:N \l_@@_tmp_tl
      }
  }
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex