Debian Codegraph @

texmf-dist/source/latex/tools/l3sys-query.dtx

raw
1 % \iffalse
2 %% Source File: l3sys-query.dtx
3 %% Copyright (C) 2024-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{l3sys-query.dtx}
13 %</dtx>
14 %<package>\ProvidesPackage{l3sys-query}
15 %<driver> \ProvidesFile{l3sys-query.drv}
16 % \fi
17 % \ProvidesFile{l3sys-query.dtx}
18 [2024-03-28 v1.0a LaTeX2e interface for l3sys file queries]
19 %
20 % \iffalse
21 %<*driver>
22 \documentclass{l3doc}%{ltxdoc}
23 \begin{document}
24 \DocInput{l3sys-query.dtx}
25 \end{document}
26 %</driver>
27 % \fi
28 %
29 % \GetFileInfo{l3sys-query.dtx}
30 %
31 % \title{\LaTeX\ document interface to the \pkg{l3sys-query} script:
32 % System queries for LaTeX using Lua\thanks{This file
33 % has version number \fileversion, last
34 % revised \filedate.}}
35 % \author{\LaTeX\ project}
36 % \date{\filedate}
37 %
38 %
39 % \maketitle
40 %
41 % \tableofcontents
42 %
43 % \section{Introduction}
44 %
45 % \TeX{} engines provide only very limited access to information about the system
46 % they are used on: using primitives, one can for example get the size of a single
47 % file, but not a list of files in a given location. For most documents, this is
48 % not an issue as they are self-contained. However, for cases where
49 % \enquote{dynamic} construction of parts of a document is needed based on file
50 % lists or other system-dependent data, methods to obtain this from (restricted)
51 % shell escape are desirable.
52 %
53 % Security considerations mean that directly querying the system shell is
54 % problematic for general use. Instead, \emph{restricted} shell escape may be used
55 % to get many details, provided a suitable tool is available to provide the
56 % information in a platform-neutral and security-conscious way. The Java program
57 % \texttt{texosquery}, written by Nicola Talbot, has been available for a number
58 % of years to provide this facility. As well as file system insight,
59 % \texttt{texosquery} also provides for example locale data and other system
60 % information. However, the requirement for Java means that the script is not
61 % automatically usable when a \TeX{} system is installed.
62 %
63 % The \LaTeX{} team have therefore provided a Lua-based script,
64 % \texttt{l3sys-query}, which conforms to the security requirements of \TeX{} Live
65 % using Lua to obtain the system information. This means that it can be used
66 % \enquote{out of the box} across platforms. The facilities provided by
67 % \texttt{l3sys-query} are more limited than \texttt{texosquery}, partly as some
68 % information is available in modern \TeX{} systems using primitives, and partly
69 % as the aim of \texttt{l3sys-query} is to provide information where there are
70 % defined use cases. Requests for additional data interfaces are welcome.
71 %
72 % This package provides a document level \textit{Key-Value} interface
73 % to The \texttt{l3sys-query} functionality, although as a convenience
74 % it also summarizes the documentation for the L3 programming
75 % interface layer (prvided by the \pkg{l3sys} module of
76 % \pkg{l3kernel}, and of the command line Lua script
77 % \texttt{l3sys-query} which is distributed separately and provides
78 % the underlying functionality.
79 %
80 %
81 %
82 % \section{The document level key-value interface\label{sec:kv}}
83 %
84 % This provides an interface to \texttt{l3sys-query} based on
85 % \pkg{l3keys} that allows the options to be specified and checked
86 % individually. Internally the supplied key values are used to build
87 % up the arguments to the commands described above in the L3
88 % programming layer commands described in section~\ref{sec:expl3}
89 %
90 % Results containing path separators \emph{always}
91 % use~|/|, irrespective of the platform in use.
92 %
93 %
94 % \begin{function}
95 % {\QueryWorkingDirectory}
96 % \begin{syntax}
97 % \cs{QueryWorkingDirectory} \Arg{result cmd}
98 % \end{syntax}
99 % Defines supplied command \meta{result cmd} to hold
100 % the absolute path to the current working directory of the
101 % \TeX\ system.
102 % This is the directory (folder) from which \TeX\ was started, so usually
103 % the location of the main document file.
104 % \end{function}
105 %
106 % \begin{function}
107 % {\QueryFiles, \QueryFilesTF}
108 % \begin{syntax}
109 % \cs{QueryFiles} \oarg{options} \Arg{spec} \Arg{function}
110 % \cs{QueryFilesTF} \oarg{options} \Arg{spec} \Arg{function} \Arg{pre code} \Arg{empty list code}
111 % \end{syntax}
112 % This generates a file list based on the \meta{spec} and \meta{options}.
113 % The command then applies \meta{function} to each item in the sequence of filenames.
114 % The \meta{function} should be a macro body which will be passed the file path as |#1|.
115 %
116 % The TF version executes the T (\meta{pre code}) argument before iterating over the list
117 % and the F (\meta{empty list code}) argument if the list is empty.
118 % \end{function}
119 %
120 % Note that this interface in mapping directly over the sequence of
121 % filenames does not allow some uses which are provided by the
122 % programming interface described in the following section, which allows
123 % the sequence to be manipulated before being used.
124 %
125 % The defined keys map very closely to the options of the
126 % \texttt{l3sys-query} command which is described in
127 % section~\ref{sec:lua}.
128 %
129 % \begin{itemize}
130 % \item
131 % The keys \texttt{recursive}, \texttt{ignore-case}, \texttt{reverse}, \texttt{pattern}
132 % take no values and map directly to the command line options of the same name.
133 %
134 % \item
135 % The key \texttt{sort} accepts the values \texttt{date} and \texttt{name}.
136 %
137 % \item
138 % The key \texttt{type} accepts \texttt{d} or \texttt{f}.
139 %
140 % \item
141 % The key \texttt{exclude} accepts a glob (or Lua pattern) matching
142 % files to be excluded. The package arranges that the quoting of the
143 % argument is automatically added if unrestricted shell escape is
144 % enabled.
145 %
146 % \end{itemize}
147 %
148 % Within the main \meta{spec} argument, and the value of the \texttt{exclude} key,
149 % the following characters may need special handling.
150 % |~| may be used (which \texttt{kpathsea} uses to denote the users home directory).
151 % |\%|, or at the top level |%|, may be used to produce a literal |%|
152 % which may be especially useful if the \texttt{pattern} key is used
153 % as \% is the escape character in Lua patterns.
154 %
155 % \subsection{Examples\label{sec:examples2e}}
156 %
157 % \begin{itemize}
158 % \item
159 % Include every |png| file in the current directory.
160 % \begin{verbatim}
161 % \QueryFiles{*.png}{\includegraphics{#1}\par}
162 % \end{verbatim}
163 %
164 % \item
165 % Input every \TeX\ file with a filename matching \texttt{chapter[0-9].tex}.
166 % \begin{verbatim}
167 % \QueryFiles[pattern]{chapter%d.*%.tex}{\input{#1}}
168 % \end{verbatim}
169 % \end{itemize}
170 %
171 % \section{The L3 programming layer interface\label{sec:expl3}}
172 %
173 % \begin{function}
174 % {\sys_get_query:nN, \sys_get_query:nnN, \sys_get_query:nnnN}
175 % \begin{syntax}
176 % \cs{sys_get_query:nN} \Arg{cmd} \Arg{tl var}
177 % \cs{sys_get_query:nnN} \Arg{cmd} \Arg{spec} \Arg{tl var}
178 % \cs{sys_get_query:nnnN} \Arg{cmd} \Arg{options} \Arg{spec} \Arg{tl var}
179 % \end{syntax}
180 % Sets the \meta{tl var} to the information returned by the
181 % \texttt{l3sys-query} \meta{cmd}, potentially supplying the \meta{options}
182 % and \meta{spec} to the query call. The valid \meta{cmd} names are at present
183 % \begin{itemize}
184 % \item \texttt{pwd} Returns the absolute path to the current working directory
185 % \item \texttt{ls} Returns a directory listing, using the \meta{spec} to
186 % select files and applying the \meta{options} if given
187 % \end{itemize}
188 % The \meta{spec} should be a file glob and will automatically be passed to
189 % the script without shell expansion.
190 % \end{function}
191 %
192 % \begin{function}
193 % {\sys_split_query:nN, \sys_split_query:nnN, \sys_split_query:nnnN}
194 % \begin{syntax}
195 % \cs{sys_split_query:nN} \Arg{cmd} \Arg{seq}
196 % \cs{sys_split_query:nnN} \Arg{cmd} \Arg{spec} \Arg{seq}
197 % \cs{sys_split_query:nnnN} \Arg{cmd} \Arg{options} \Arg{spec} \Arg{seq}
198 % \end{syntax}
199 % Works as described for \cs{sys_get_query:nnnN}, but sets the \meta{seq}
200 % to contain one entry for each line returned by \texttt{l3sys-query}.
201 % \end{function}
202 %
203 % \section{The \texttt{l3sys-query} Lua script\label{sec:lua}}
204 % \subsection{The command line interface}
205 %
206 % The command line interface to
207 % \begin{center}
208 % \ttfamily
209 % l3sys-query \meta{cmd} [\meta{option(s)}] [\meta{args}]
210 % \end{center}
211 % where \texttt{\meta{cmd}} can be one of the following:
212 % \begin{itemize}[noitemsep]\ttfamily
213 % \item ls
214 % \item ls \meta{args}
215 % \item pwd
216 % \end{itemize}
217 % The \meta{cmd} are described below. The result of the \meta{cmd} will be printed
218 % to the terminal in an interactive run; in normal usage, this will be piped to
219 % the calling \TeX{} process. Results containing path separators \emph{always}
220 % use~|/|, irrespective of the platform in use.
221 %
222 % As well as these targets, the script recognizes the options
223 % \begin{itemize}
224 % \item |--exclude| Specification for directory entries to exclude
225 % \item |--ignore-case| Ignores case when sorting directory listings
226 % \item |--pattern| (|-p|) Treat the \meta{args} as Lua patterns rather
227 % than converting from wildcards
228 % \item |--recursive| (|-r|) Enables recursive searching during directory
229 % listings
230 % \item |--reverse| Causes sorting to go from highest to lowest rather
231 % than lowest to highest
232 % \item |--sort| Sets the method used to sort entries returned by |ls|
233 % \item |--type| Selects the type of entry returned by |ls|
234 % \end{itemize}
235 % The action of these options on the appropriate \meta{cmd(s)} is detailed below.
236 %
237 % \subsubsection{\texttt{ls [\meta{args}]}}
238 %
239 % Lists the contents of one or more directories, in a manner somewhat reminiscent
240 % of the Unix command |ls| or the Windows command |dir|. The exact nature of the
241 % output will depend on the \meta{args}, if given, along with the prevailing
242 % options. Note that the options names are inspired by ideas from the Unix
243 % commands |ls| and |find| as well as the Windows command |dir|: they therefore do
244 % not map directly to those of any one of the command line tools that they
245 % somewhat mirror.
246 %
247 % When no \meta{args} are given, all entries in the current directory will be
248 % listed, one per line in the output. This will include both files and
249 % subdirectories. Each entry will include a path relative to the current
250 % directory: for files \emph{in} the current directory, this will be |./|. The
251 % order of results will be determined by the underlying operating system process:
252 % unless requested \emph{via} an option, no sorting takes place.
253 %
254 % As standard, the \meta{args} are treated as a file/path name potentially
255 % including |?| and |*| as wildcards, for example |*.png| or |file?.txt|.
256 % \begin{verbatim}
257 % l3sys-query ls '*.png'
258 % \end{verbatim}
259 % Some care is needed in preventing expansion of such wildcards by the shell or
260 % \texttt{texlua} process: these are detailed in Section~\ref{sec:wildcard}. In
261 % this section, |'| is used to indicate a character being used to suppress
262 % expansion: this is for example normal on macOS and Linux.
263 %
264 % Removal of entries from the listing can be achieved using the |--exclude|
265 % option, which should be given with a \meta{xarg}, for example
266 % \begin{verbatim}
267 % l3sys-query ls --exclude '*.bak' 'graphics/*'
268 % \end{verbatim}
269 % Directory entries starting |.| are traditionally hidden on Linux and macOS
270 % systems: these \enquote{dot} entries are excluded from the output of
271 % \texttt{l3sys-query}. The entries |.| and |..| for the current and parent
272 % directory are also excluded from the results returned by \texttt{l3sys-query} as
273 % they can always be assumed.
274 %
275 % For more complex matching, the \meta{args} can be treated as a Lua pattern using
276 % the |--pattern| (|-p|) option; this also applies to the \meta{xarg} argument to
277 % the |--exclude| option. For example, the equivalent to wildcard |*.png| could be
278 % obtained using
279 % \begin{verbatim}
280 % l3sys-query ls --pattern '^.*%.png$'
281 % \end{verbatim}
282 %
283 % The results returned by |ls| can be sorted using the |--sort| option. This can
284 % be set to |none| (use the order from the file system: the default), |name| (sort
285 % by file name) or |date| (sort by date last modified). The sorting order can be
286 % reversed using |--reverse|. Sorting normally takes account of case: this can be
287 % suppressed with the |--ignore-case| option.
288 %
289 % The listing can be filtered based on the type of entry using the |--type|
290 % option. This takes a string argument, one of |d| (directory) or |f| (file).
291 %
292 % As standard, only the path specified as part of the \meta{args} is queried.
293 % However, if the |--recursive| (|-r|) option is set, the query is applied within
294 % all subdirectories. Subdirectories starting with~|.| (macOS and Linux hidden)
295 % are excluded from recursion.
296 %
297 % For security reasons, only paths within the current working directory can be
298 % queried, thus for example |graphics/*.png| will list all |png| files in the
299 % |graphics| subdirectory, but |../graphics/*.png| will yield no output.
300 %
301 % \subsubsection{\texttt{pwd}}
302 %
303 % Returns the current working directory from which \texttt{l3sys-query} is run.
304 % From within a \TeX{} run, this will (usually) be the directory containing the
305 % main file, assuming a command such as
306 % \begin{verbatim}
307 % pdflatex main.tex
308 % \end{verbatim}
309 % The \texttt{pwd} command is unaffected by any options.
310 %
311 % \subsection{Spaces in arguments}
312 %
313 % Since \texttt{l3sys-query} is intended primarily for use with restricted shell
314 % escape calls from \TeX{} processes, handling of spaces is unusual. It is not
315 % possible to quote spaces in such a call, so for example whilst
316 % \begin{verbatim}
317 % l3sys-query ls "foo *"
318 % \end{verbatim}
319 % does work from the command prompt to find all files with names starting
320 % \verb*|foo |, it would not work \emph{via} restricted shell escape. To
321 % circumvent this, \texttt{l3sys-query} will collect all command line arguments
322 % after any \meta{options}, and combine these as a space-separated \meta{args},
323 % for example allowing
324 % \begin{verbatim}
325 % l3sys-query ls foo '*'
326 % \end{verbatim}
327 % to achieve the same result as the first example. The result is that the
328 % \meta{args} will only every be interpreted by \texttt{l3sys-query} as a single
329 % argument. It also means that spaces cannot be used at the start or end of the
330 % argument, nor can multiple spaces appear between non-space arguments.
331 %
332 % \subsection{Wildcard expansion handling\label{sec:wildcard}}
333 %
334 % The handling of wildcards needs some further comment for those using
335 % \texttt{l3sys-query} from the command line: the \pkg{expl3} interface described
336 % in Section~\ref{sec:expl3} handles this aspect automatically for the user.
337 %
338 % On macOS and Linux, the shell normally expands globs, which include the
339 % wildcards |*| and |?|, before passing arguments to the appropriate command.
340 % This can be suppressed by surrounding the argument with |'| characters, hence
341 % the formulation
342 % \begin{verbatim}
343 % l3sys-query ls '*.png'
344 % \end{verbatim}
345 % earlier.
346 %
347 % On Windows, the shell does no expansion, and thus arguments are passed as-is to
348 % the relevant command. As such, |'| has no special meaning here. However, to
349 % allow quoting of wildcards from the shell in a platform-neutral manner,
350 % \texttt{l3sys-query} will strip exactly one set of |'| characters around each
351 % argument before further processing.
352 %
353 % It is not possible to use |"| quotes at all in the argument passed to
354 % \texttt{l3sys-query} from \TeX{}, as the \TeX{} system removes all |"| in
355 % |\input| while handling space quoting.
356 %
357 % Restricted shell escape prevents shell expansion of wildcards entirely. On
358 % non-Windows systems, it does this by ensuring that each argument is |'| quoted
359 % to ensure further expansion. Thus a \TeX{} call such as
360 % \begin{verbatim}
361 % \input|"l3sys-query ls '*.png'"
362 % \end{verbatim}
363 % will work if |--shell-escape| is used as the argument is passed directly to the
364 % shell, but in restricted shell escape will give an error such as:
365 % \begin{verbatim}
366 % ! I can't find file `"|l3sys-query ls '*.png'"'.
367 % \end{verbatim}
368 % The \LaTeX{} interfaces described above adjust the quoting used depending on the
369 % |shell-escape| status.
370 %
371 %
372 % \MaybeStop{}
373 %
374 % \section{The \LaTeXe\ package implementation}
375 %
376 % \begin{macrocode}
377 %<*package>
378 % \end{macrocode}
379 %
380 % \begin{macrocode}
381 \ExplSyntaxOn
382 %<@@=queryfiles>
383 % \end{macrocode}
384 %
385 % The package should eventually work in restricted shell escape but will do
386 % nothing useful if the process was started with |--no-shell-escape|.
387 % \begin{macrocode}
388 \sys_if_shell:F{
389 \PackageWarningNoLine{l3sys-query}
390 {Shell ~Escape ~is ~disabled.\MessageBreak All ~queries ~will ~return ~empty ~results}
391 }
392 % \end{macrocode}
393 %
394 % \subsection{\cs{QueryWorkingDirectory}}
395 % |\QueryWorkingDirectory| is a direct call to the |pwd| command provided by \texttt{l3sys-query}.
396 % \begin{macrocode}
397 \NewDocumentCommand\QueryWorkingDirectory {m} {
398 \sys_get_query:nN {pwd} #1
399 }
400 % \end{macrocode}
401 %
402 % \subsection{\cs{QueryFiles} and \cs{QueryFilesTF}}
403 %
404 % The declarations of these commands are done in two steps to allow catcode chanes
405 % before the arguments are read. This allows the use of |%| and |^^| in patterns
406 % at least if the command is not nested in another command argument. (|\%| may be used
407 % to generate |%| in all cases).
408 %
409 % Variables for saving the current definition of |\%| and |~|.
410 % \begin{macrocode}
411 \tl_new:N\l_query_percent_tl
412 \tl_new:N\l_query_tilde_tl
413 % \end{macrocode}
414 %
415 % Allow |%| and |^^| at the top level.
416 % \begin{macrocode}
417 \NewDocumentCommand\QueryFiles {} {
418 \group_begin:
419 \char_set_catcode_other:N \%
420 \char_set_catcode_other:N \^
421 \QueryFiles_inner
422 }
423 % \end{macrocode}
424 %
425 % \begin{macrocode}
426 \char_set_catcode_active:N \~
427 % \end{macrocode}
428 %
429 % \begin{macrocode}
430 \NewDocumentCommand\QueryFiles_inner {O{}m}{
431 \group_end:
432 \tl_set:Nn\l_tmpa_tl{}
433 \cs_set_eq:NN \l_query_percent_tl \%
434 \cs_set_eq:NN \% \c_percent_str
435 \cs_set_eq:NN \l_query_tilde_tl ~
436 \cs_set_eq:NN ~ \c_tilde_str
437 \keys_set:nn{QueryFiles}{#1}
438 \exp_args:NnV\sys_split_query:nnnN {ls} \l_tmpa_tl {#2} \l_tmpa_seq
439 \cs_set_eq:NN \% \l_query_percent_tl
440 \cs_set_eq:NN ~ \l_query_tilde_tl
441 \seq_map_inline:Nn\l_tmpa_seq
442 }
443 % \end{macrocode}
444 %
445 % This duplicates rather than shares code so as to read the function
446 % and TF arguments with normal catcode regime.
447 % (This could probably be optimised.)
448 % \begin{macrocode}
449 \NewDocumentCommand\QueryFilesTF {} {
450 \group_begin:
451 \char_set_catcode_other:N \%
452 \QueryFilesTF_inner
453 }
454 % \end{macrocode}
455 %
456 % \begin{macrocode}
457 \NewDocumentCommand\QueryFilesTF_inner {O{}m}{
458 \group_end:
459 \tl_set:Nn\l_tmpa_tl{}
460 \cs_set_eq:NN \l_query_percent_tl \%
461 \cs_set_eq:NN \% \c_percent_str
462 \cs_set_eq:NN \l_query_tilde_tl ~
463 \cs_set_eq:NN ~ \c_tilde_str
464 \keys_set:nn{QueryFiles}{#1}
465 \exp_args:NnV\sys_split_query:nnnN {ls} \l_tmpa_tl {#2} \l_tmpa_seq
466 \cs_set_eq:NN \% \l_query_percent_tl
467 \cs_set_eq:NN ~ \l_query_tilde_tl
468 \seq_if_empty:NTF \l_tmpa_seq \use_iii:nnn \__queryfiles_aux:nnn
469 }
470 % \end{macrocode}
471 %
472 % \begin{macrocode}
473 \char_set_catcode_space:N \~
474 % \end{macrocode}
475 %
476 % \begin{macrocode}
477 \cs_new:Npn \__queryfiles_aux:nnn #1#2#3 {
478 #2
479 \seq_map_inline:Nn\l_tmpa_seq {#1}
480 }
481 % \end{macrocode}
482 %
483 % Defining the keys. Most take no value and simply add a \texttt{l3sys-query} \texttt{-{}-}
484 % option to the command linebeing constructed.
485 % \begin{macrocode}
486 \keys_define:nn {QueryFiles} {
487 % \end{macrocode}
488 %
489 % \begin{macrocode}
490 recursive .code:n =\tl_put_right:Nn \l_tmpa_tl {--recursive ~ } ,
491 recursive .value_forbidden:n = true ,
492 % \end{macrocode}
493 %
494 % \begin{macrocode}
495 ignore-case .code:n =\tl_put_right:Nn \l_tmpa_tl {--ignore-case ~ } ,
496 ignore-case .value_forbidden:n = true ,
497 % \end{macrocode}
498 %
499 % \begin{macrocode}
500 reverse .code:n =\tl_put_right:Nn \l_tmpa_tl {--reverse ~ } ,
501 reverse .value_forbidden:n = true ,
502 % \end{macrocode}
503 %
504 % \begin{macrocode}
505 exclude .code:n =\tl_put_right:Ne \l_tmpa_tl {
506 --exclude ~
507 \sys_if_shell_restricted:F'
508 \exp_not:n{#1}
509 \sys_if_shell_restricted:F'
510 ~ } ,
511 exclude .value_required:n = true ,
512 % \end{macrocode}
513 %
514 % The |type| key checks the supplied value is valid |d| for directories
515 % or |f| for files The default behaviour lists both.
516 % \begin{macrocode}
517 type .choices:nn = {d,f}
518 {\tl_put_right:Nn \l_tmpa_tl {--type ~ #1 ~ }} ,
519 % \end{macrocode}
520 %
521 % The |sort| key checks the supplied value is valid |date| or |name|.
522 % \begin{macrocode}
523 sort .choices:nn = {date,name}
524 {\tl_put_right:Nn \l_tmpa_tl {--sort ~ #1 ~ }} ,
525 % \end{macrocode}
526 %
527 % \begin{macrocode}
528 pattern .code:n =\tl_put_right:Nn \l_tmpa_tl {--pattern ~ } ,
529 pattern .value_forbidden:n = true ,
530 % \end{macrocode}
531 %
532 % \begin{macrocode}
533 }
534 % \end{macrocode}
535 %
536 % \begin{macrocode}
537 \ExplSyntaxOff
538 % \end{macrocode}
539 %
540 % \begin{macrocode}
541 %</package>
542 % \end{macrocode}
543 %
544 % \Finale
545 %