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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
|
%\iffalse
%<*package>
%% \CharacterTable
%% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%% Digits \0\1\2\3\4\5\6\7\8\9
%% Exclamation \! Double quote \" Hash (number) \#
%% Dollar \$ Percent \% Ampersand \&
%% Acute accent \' Left paren \( Right paren \)
%% Asterisk \* Plus \+ Comma \,
%% Minus \- Point \. Solidus \/
%% Colon \: Semicolon \; Less than \<
%% Equals \= Greater than \> Question mark \?
%% Commercial at \@ Left bracket \[ Backslash \\
%% Right bracket \] Circumflex \^ Underscore \_
%% Grave accent \` Left brace \{ Vertical bar \|
%% Right brace \} Tilde \~}
%</package>
%\fi
% \iffalse
% Doc-Source file
% Copyright (C) 2016 Nicola Talbot, all rights reserved.
% \fi
% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{tgtermes}
\usepackage{metalogo}
\usepackage[colorlinks,hyperindex=false]{hyperref}
\CheckSum{269}
\RecordChanges
\PageIndex
\CodelineNumbered
\IndexPrologue{}
\renewcommand*{\usage}[1]{\hyperpage{#1}}
\renewcommand*{\main}[1]{\hyperpage{#1}}
\newcommand*{\shortargfmt}[1]{\texttt{-#1}}
\newcommand*{\longargfmt}[1]{\texttt{-{}-#1}}
\newcommand*{\shortarg}[1]{\shortargfmt{#1}%
\index{texosquery options=\texttt{texosquery} options>#1=\protect\shortargfmt{-#1}|hyperpage}}
\newcommand*{\longarg}[1]{\longargfmt{#1}%
\index{texosquery options=\texttt{texosquery} options>#1=\protect\longargfmt{#1}|hyperpage}}
\newenvironment{important}
{\begin{quote}\textbf{Important Note:}}
{\end{quote}}
\begin{document}
\DocInput{texosquery.dtx}
\end{document}
%</driver>
%\fi
%
%\DeleteShortVerb{|}
%
%\title{texosquery: query OS information from \TeX}
%\author{Nicola L. C. Talbot\\
%\href{http://www.dickimaw-books.com/}{\nolinkurl{dickimaw-books.com}}
%\and Paulo Cereda}
%\date{2016-07-14 (v1.1)}
%\maketitle
%
%\begin{abstract}
%\texttt{texosquery.jar} is a cross-platform Java application
%to query certain OS information designed for use in \TeX's
%shell escape mechanism.
%The accompanying \TeX\ code provides a simple interface to the
%\texttt{texosquery} Java application, which may be skipped
%if you want to explicitly use \cs{input} (but take
%care of any special characters appearing in the result).
%\end{abstract}
%
%\begin{important}
%You will need \TeX's shell escape enabled,
%and you will also need the Java Runtime Environment (JRE)
%installed to use \texttt{texosquery}.
%\end{important}
%
%If you want to rebuild the application, instructions for
%compiling the source code (including the code for this document)
%are in the accompanying \texttt{README.md} file.
%
%\tableofcontents
%
%\section{texosquery.jar: the Java application}
%The \texttt{texosquery} Java command line application looks up
%certain system information that may be of use in \TeX\
%documents. All this information can easily be obtained using
%native commands, but the Java application allows an
%OS-independent approach with results that can easily be
%captured by \TeX's shell-escape without having to strip
%formatting information.
%
%\begin{important}
%\texttt{texosquery} provides read-only actions, and I don't
%intend adding any actions that modify system settings or files.
%\end{important}
%
%Since the application is designed to work with \TeX, each
%function will display the result on a single line without
%formatting. (For multiple results, each line is grouped
%from v1.1.) A blank line (or empty group) will be displayed
%if the information isn't available. A forward slash is always
%used as a directory divider, regardless of the operating
%system, so the result can be used, for example, in \cs{input} or
%\cs{includegraphics}.
%
%If an input file name is required (for example, with the
%\longargfmt{pdfdate} argument described below) then the file may be in
%the current working directory, relative to the current directory
%(with forward slash \texttt{/} as the directory divider), an absolute path
%(again with forward slash) or on \TeX's path (in
%which case, \texttt{kpsewhich} is used to locate it).
%
%Command line invocation:
%\begin{flushleft}\ttfamily
%texosquery \meta{action} ...
%\end{flushleft}
%Available actions (at least one required):
%\begin{description}
%\item[\shortarg{L} or \longarg{locale}] Display the locale
%information in the form
%\begin{flushleft}\ttfamily
%\meta{lang}-\meta{region}.\meta{codeset}@\meta{modifier}
%\end{flushleft}
%where \meta{lang} is the ISO code for the language (e.g.\
%\texttt{en}),
%\meta{region} is the ISO code for the region (e.g.\ \texttt{GB}),
%\meta{codeset} is the default code set (e.g.\ \texttt{UTF-8})
% and \meta{modifier} is the modifier. Elements may be omitted
% if unavailable. For example, \texttt{en-GB.UTF-8} has the
% \meta{modifier} omitted, and \texttt{en} has all but the language
% omitted.
%\item[\shortarg{l} or \longarg{locale-lcs}] As the above but the
%codeset (if present) is converted to lower case and any hyphens are
%stripped. For example, if \longargfmt{locale} returns
%\texttt{en-GB.UTF-8}, then \longargfmt{locale-lcs} would return
%\texttt{en-GB.utf8}.
%\item[\shortarg{c} or \longarg{cwd}] Displays the current working
%directory.
%\item[\shortarg{m} or \longarg{userhome}] Displays the user's home
%directory.
%\item[\shortarg{t} or \longarg{tmpdir}] Displays the temporary
%directory.
%\item[\shortarg{o} or \longarg{osname}] Displays the operating
%system name.
%\item[\shortarg{r} or \longarg{osversion}] Displays the operating
%system version.
%\item[\shortarg{a} or \longarg{osarch}] Displays the operating
%system architecture.
%\item[\shortarg{n} or \longarg{pdfnow}] Displays the current
% date and time in PDF format. For example
%\begin{verbatim}
%D:20160704131006+01'00'
%\end{verbatim}
%Note that some, but not all, \TeX\ formats provide
%\cs{pdfcreationdate}, which is more efficient than using the shell
%escape, but this can be used
%as a fallback method for those that don't (for example, \XeTeX).
%\item[\shortarg{d} \meta{file} or \longarg{pdfdate} \meta{file}]
% Displays the last modified time stamp of the given file in PDF format or a blank line
% if the file doesn't exist or the file permissions prohibit this
% action.
%Again some, but not all, \TeX\ formats provide
%\cs{pdffilemoddate}\marg{file}, which is more efficient than using
%the shell escape.
%\item[\shortarg{s} \meta{file} or \longarg{filesize} \meta{file}]
% Displays the size in bytes of the given file or a blank line
% if the file doesn't exist or the file permissions prohibit this
% action.
%Some, but not all, \TeX\ formats provide
%\cs{pdffilesize}\marg{file}, which is more efficient than using
%the shell escape.
%\item[\shortarg{i} \meta{sep} \meta{dir} or \longarg{list}
%\meta{sep} \meta{dir}]
%List all files in the given directory with the output on a single
%line using \meta{sep} as the separator between entries. Note that
%the list doesn't include the full path, just the file names.
%\item[\shortarg{f} \meta{sep} \meta{regex} \meta{dir} or
%\longarg{filterlist} \meta{sep} \meta{regex} \meta{dir}]
%Like the above but only lists those files whose name matches the
%regular expression given in \meta{regex}. Note that this tests for a
%complete match on the file name (not including path). For example,
%if \meta{regexp} is \texttt{foo.*}, it will only match files whose
%name starts with \texttt{foo} (for example, \texttt{foobar} will
%match but \texttt{barfoo} won't).
%\item[\shortarg{u} \meta{file} or \longarg{uri} \meta{file}]
%Displays the URI of the given file or a blank line if the file
%doesn't exist or the file permissions prohibit this action.
%Note that this may include in a percent character in the result. The \TeX\
%command \cs{TeXOSQuery} protects against this by changing the
%category code, but if you explicitly call \texttt{texosquery}
%using the shell escape, you'll need to be careful of this.
%\item[\shortarg{p} \meta{file} or \longarg{path} \meta{file}]
%Displays the canonical path of the given file or a blank line if
%the file doesn't exist or the file permissions prohibit this
%action.
%\item[\shortarg{e} \meta{file} or \longarg{dirname} \meta{file}]
%(New to v1.1.)
%Displays the canonical path of the given file's parent (that is, the
%directory containing \meta{file}) or a blank line if
%the file doesn't exist or the file permissions prohibit this
%action. Note that this is different to the Unix-like
%\texttt{dirname} command, which will return a relative path if
%\meta{file} isn't an absolute path.
%\item[\shortarg{h} or \longarg{help}] Displays help message and
%exits.
%\item[\shortarg{v} or \longarg{version}] Displays version
%information and exits.
%\end{description}
%
%If multiple options are given, they will be processed in the
%order specified in the command line invocation. Each result will
%be displayed on a separate line. As from v1.1, if there are
%multiple actions, each result will be grouped. This makes it easier
%to process the results in \TeX. For example:
%\begin{verbatim}
%texosquery -l
%\end{verbatim}
%This just produces (for me):
%\begin{verbatim}
%en-GB.utf8
%\end{verbatim}
%whereas
%\begin{verbatim}
%texosquery -l -n
%\end{verbatim}
%produces:
%\begin{verbatim}
%{en-GB.utf8}
%{D:20160714112732+01'00'}
%\end{verbatim}
%Note that unavailable information will produce an empty group.
%For example (assuming \texttt{nofile} doesn't exist):
%\begin{verbatim}
%texosquery -l -d nofile -n
%\end{verbatim}
%produces:
%\begin{verbatim}
%{en-GB.utf8}
%{}
%{D:20160714112732+01'00'}
%\end{verbatim}
%whereas
%\begin{verbatim}
%texosquery -d nofile
%\end{verbatim}
%just displays an empty line.
%
%\section{texosquery.tex: generic \TeX\ code}
%You can run \texttt{texosquery} directly from \TeX's shell escape.
%For example:
%\begin{verbatim}
%\input|"texosquery --locale"
%\end{verbatim}
%However, \texttt{texosquery.tex} provides generic \TeX\ code to do
%this for you and store the result in a control sequence.
%
%Plain \TeX\ users can input this file through the usual \cs{input}
%method:
%\begin{verbatim}
%\input texosquery
%\end{verbatim}
%\LaTeX\ users may also simply input this file:
%\begin{verbatim}
%\input{texosquery}
%\end{verbatim}
%but may prefer the standard package approach:
%\begin{verbatim}
%\usepackage{texosquery}
%\end{verbatim}
%
%\begin{important}
%The commands described below are all fragile.
%\end{important}
%
%The basic command to run \texttt{texosquery} and capture its output
%in a control sequence is:
%\DescribeMacro\TeXOSQuery
%\begin{flushleft}\ttfamily
%\cs{TeXOSQuery}\marg{cs}\marg{args}
%\end{flushleft}
%where \meta{cs} is the control sequence in which to store the
%result and \meta{args} are the command line arguments to pass to
%\texttt{texosquery}. This first locally changes the category code
%of some problematic characters that may appear in the result.
%The only special characters that aren't changed are the backslash
%\verb|\|, curly braces \verb|{| and \verb|}|, and hash \verb|#|.
%(\texttt{texosquery.jar} will replace \verb|#| with \verb|\#| in places
%where it might possibly occur in the result, but in general it's
%best to avoid these characters in file names.)
%
%There are some short cut commands for convenience, described below.
%If any of these commands cause an error message in the form:
%\begin{verbatim}
%I can't find file `|texosquery'.
%\end{verbatim}
%then check that you have the shell escape on. If the error
%persists with the shell escape enabled
%(and not restricted), check that \texttt{texosquery} is on
%your system's path. To do this, open a command prompt or terminal and
%type \texttt{texosquery}. If it isn't installed correctly, there will be a
%message like:
%\begin{verbatim}
%`texosquery' is not recognised
%\end{verbatim}
%or
%\begin{verbatim}
%texosquery: command not found
%\end{verbatim}
%If this happens, check the installation. (Instructions are
%in the accompanying \texttt{README.md} file.)
%
%Dry run mode is determined by the conditional
%\DescribeMacro\ifTeXOSQueryDryRun
%\begin{verbatim}
%\ifTeXOSQueryDryRun
%\end{verbatim}
%If true, the shell escape won't be used and the requested command
%invocation will be printed in the transcript file prefixed with
%\begin{verbatim}
%TeXOSQuery:
%\end{verbatim}
%(the control sequence \meta{cs} will be set to empty).
%This conditional will automatically be switched on unless
%\cs{shellescape} or \cs{pdfshellescape} is 1. (If
%\texttt{texosquery.jar} is later allowed on the restricted list,
%newer versions may change this default.)
%
%If multiple queries are required, it's more efficient to
%perform them all in one go. For example:
%\begin{verbatim}
%\TeXOSQuery{\result}{-l -n -o}
%
%\def\parseresult#1#2#3{%
% Locale: #1. Now: #2. OS: #3.%
%}
%
%\ifx\result\empty
% Query failed.
%\else
% \expandafter\parseresult\result
%\fi
%\end{verbatim}
%(Make sure you have at least v1.1 for this to work correctly.)
%
%The locale (\shortarg{l} or \longarg{locale-lcs}) information can be
%obtained using:
%\DescribeMacro\TeXOSQueryLocale
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryLocale}\marg{cs}
%\end{flushleft}
%Note that this uses the lower case codeset form, which has a better
%chance of matching the encoding names used by the \texttt{inputenc}
%package. If you want the unprocessed codeset name, you can do:
%\begin{flushleft}\ttfamily
%\cs{TeXOSQuery}\marg{cs}\{\shortarg{L}\}
%\end{flushleft}
%
%The current working directory (\shortarg{c} or \longarg{cwd}) can
%be obtained using:
%\DescribeMacro\TeXOSQueryCwd
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryCwd}\marg{cs}
%\end{flushleft}
%
%The home directory (\shortarg{m} or \longarg{userhome}) can
%be obtained using:
%\DescribeMacro\TeXOSQueryHome
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryHome}\marg{cs}
%\end{flushleft}
%
%The temporary directory (\shortarg{t} or \longarg{tmpdir}) can
%be obtained using:
%\DescribeMacro\TeXOSQueryTmpDir
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryTmpDir}\marg{cs}
%\end{flushleft}
%
%The OS name (\shortarg{o} or \longarg{osname}) can
%be obtained using:
%\DescribeMacro\TeXOSQueryName
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryName}\marg{cs}
%\end{flushleft}
%
%The OS version (\shortarg{r} or \longarg{osversion}) can
%be obtained using:
%\DescribeMacro\TeXOSQueryVersion
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryVersion}\marg{cs}
%\end{flushleft}
%
%The OS architecture (\shortarg{a} or \longarg{osarch}) can
%be obtained using:
%\DescribeMacro\TeXOSQueryArch
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryArch}\marg{cs}
%\end{flushleft}
%
%The current date-time stamp in PDF format (\shortarg{n} or
%\longarg{pdfnow}) can be obtained using:
%\DescribeMacro\TeXOSQueryNow
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryNow}\marg{cs}
%\end{flushleft}
%This is provided for the benefit of users who don't have
%\cs{pdfcreationdate} defined by their \TeX\ format (for example,
%\XeTeX). As from v1.1, this ensures that the initial \texttt{D} has
%category code 12 (which won't happen if \cs{TeXOSQuery} is used
%explicitly).
%
%\begin{important}
%The remaining commands all require extra arguments after the
%relevant switch. These are automatically enclosed in single-quotes
%to protect any spaces. If the argument actually contains any single-quote
%characters, make sure you use \verb|\string\'| to prevent
%interference. \emph{However, in general it's a very bad idea to use
%quotes as part of a file name (rather than using them as
%delimiters).}
%Since the file name may need to be obtained from \cs{jobname},
%which sometimes includes double-quotes, the first double-quote pair
%found is stripped in file name arguments. Any other double-quotes
%will need to be protected in the same manner as single-quotes
%(but, again, this shouldn't be an issue if you use a safe
%file naming scheme). Any paths should use a
%forward slash for the directory divider.
%\end{important}
%
%The modification date-time stamp in PDF format for a file
%(\shortarg{d} or \longarg{pdfdate}) can be obtained using:
%\DescribeMacro\TeXOSQueryFileDate
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryFileDate}\marg{cs}\marg{filename}
%\end{flushleft}
%where \meta{filename} is the name of the file.
%This is provided for the benefit of users who don't have
%\cs{pdffilemoddate} defined by their \TeX\ format. As from v1.1,
%this ensures that the initial \texttt{D} has category code 12 (which
%won't happen if \cs{TeXOSQuery} is used explicitly).
%
%The size in bytes of a file
%(\shortarg{s} or \longarg{filesize}) can be obtained using:
%\DescribeMacro\TeXOSQueryFileSize
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryFileSize}\marg{cs}\marg{filename}
%\end{flushleft}
%where \meta{filename} is the name of the file.
%This is provided for the benefit of users who don't have
%\cs{pdffilesize} defined by their \TeX\ format.
%
%The list of files in a given directory
%(\shortarg{i} or \longarg{list}) can be obtained using:
%\DescribeMacro\TeXOSQueryFileList
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryFileList}\marg{cs}\marg{sep}\marg{dir}
%\end{flushleft}
%where \meta{sep} is the separator and \meta{dir} is the directory name.
%For example:
%\begin{verbatim}
%\TeXOSQueryFileList{\result}{,}{.}
%\end{verbatim}
%will store a comma-separated list of all the files contained in the
%current directory in the control sequence \cs{result}.
%
%A filtered list of files in a given directory
%(\shortarg{f} or \longarg{filterlist}) can be obtained using:
%\DescribeMacro\TeXOSQueryFilterFileList
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryFilterFileList}\marg{cs}\marg{sep}\marg{regex}\marg{dir}
%\end{flushleft}
%where \meta{regex} is a regular expression. \emph{Take care of any backslashes
%in the regular expression!}
%For example, to list only those files that have an extension:
%\begin{verbatim}
%\TeXOSQueryFilterFileList{\result}{,}{.+\string\..*}{.}
%\end{verbatim}
%Note the use of \verb|\string\.| to ensure that \cs{.} isn't
%interpreted as a command. Another example, list only \texttt{.png}
%and \texttt{.jpg} files in the directory called \texttt{images}:
%\begin{verbatim}
%\TeXOSQueryFilterFileList{\result}{,}{.+\string\.(jpg|png)}{images}
%\end{verbatim}
%
%The URI of a file
%(\shortarg{u} or \longarg{uri}) can be obtained using:
%\DescribeMacro\TeXOSQueryFileURI
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryFileURI}\marg{cs}\marg{filename}
%\end{flushleft}
%where \meta{filename} is the name of the file.
%(Any percent symbols \verb|%| contained in the URI will have
%their category code set to 12.)
%
%The canonical path of a file
%(\shortarg{p} or \longarg{path}) can be obtained using:
%\DescribeMacro\TeXOSQueryFilePath
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryFilePath}\marg{cs}\marg{filename}
%\end{flushleft}
%where \meta{filename} is the name of the file.
%
%The canonical path of a file's parent
%(\shortarg{e} or \longarg{dirname}) can be obtained using:
%\DescribeMacro\TeXOSQueryDirName
%\begin{flushleft}\ttfamily
%\cs{TeXOSQueryDirName}\marg{cs}\marg{filename}
%\end{flushleft}
%where \meta{filename} is the name of the file.
%
%\StopEventually{\phantomsection
%\addcontentsline{toc}{section}{Change History}%
%\PrintChanges
%\addcontentsline{toc}{section}{\indexname}%
%\PrintIndex
%}
%
%\section{The Code}
%\changes{1.0}{2016-07-08}{Initial release}
%\subsection{Generic \TeX\ Code}
%\iffalse
% \begin{macrocode}
%<*texosquery.tex>
% \end{macrocode}
%\fi
%Change category code of \texttt{@} if necessary.
% \begin{macrocode}
\ifnum\catcode`\@=11\relax
\def\@texosquery@restore@at{}%
\else
\expandafter\edef\csname @texosquery@restore@at\endcsname{%
\noexpand\catcode`\noexpand\@=\number\catcode`\@\relax
}%
\catcode`\@=11\relax
\fi
% \end{macrocode}
% Check if already loaded.
% \begin{macrocode}
\ifx\TeXOSQuery\undefined
\else
\@texosquery@restore@at
\expandafter\endinput
\fi
% \end{macrocode}
%\begin{macro}{\TeXOSInvokerName}
%The name of the \texttt{texosquery} application.
% \begin{macrocode}
\def\TeXOSInvokerName{texosquery}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryInvoker}
%If we're using \LaTeX, we'll need to use \cs{@@input} rather
%than \cs{input}.
% \begin{macrocode}
\ifx\@@input\undefined
\def\TeXOSQueryInvoker#1{\input|"\TeXOSInvokerName\space#1" }
\else
\def\TeXOSQueryInvoker#1{\@@input|"\TeXOSInvokerName\space#1" }
\fi
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\ifTeXOSQueryDryRun}
%Provide a dry-run mode.
%\changes{1.1}{2016-07-14}{dry run mode only false by default if with
%unrestricted mode}
% \begin{macrocode}
\newif\ifTeXOSQueryDryRun
\TeXOSQueryDryRuntrue
% \end{macrocode}
%\end{macro}
%
%If shell escape is unrestricted, automatically switch off dry-run mode.
% \begin{macrocode}
\ifx\shellescape\undefined
\ifx\pdfshellescape\undefined
\else
\ifnum\pdfshellescape=1\relax
\TeXOSQueryDryRunfalse
\fi
\fi
\else
\ifnum\shellescape=1\relax
\TeXOSQueryDryRunfalse
\fi
\fi
% \end{macrocode}
%
%\begin{macro}{\TeXOSQuery}
%Use \texttt{texosquery} with the option given in the second
%argument and store the result in control sequence given in the
%first argument.
% \begin{macrocode}
\def\TeXOSQuery#1#2{%
\ifTeXOSQueryDryRun
\begingroup
\newlinechar=`\^^J
\message{^^JTeXOSQuery: \TeXOSInvokerName\space#2^^J}%
\endgroup
\def#1{}%
\else
\begingroup
\endlinechar=-1\relax
% \end{macrocode}
%Just in case the result contains any awkward characters that have
%some special meaning to \TeX. (Can't really protect against hash,
%backslash or curly braces, but they're unlikely to occur unless
%the user has a very wacky and inappropriate file naming scheme.)
% \begin{macrocode}
\catcode`\-=12\relax
\catcode`\_=12\relax
\catcode`\^=12\relax
\catcode`\~=12\relax
\catcode`\$=12\relax
\catcode`\&=12\relax
\catcode`\"=12\relax
\catcode`\'=12\relax
\catcode`\.=12\relax
\catcode`\/=12\relax
\catcode`\:=12\relax
\catcode`\;=12\relax
\catcode`\%=12\relax
\everyeof{\noexpand}\relax
\edef\x{\endgroup\def\noexpand#1{\TeXOSQueryInvoker{#2}}}\x
\fi
}
% \end{macrocode}
%\end{macro}
%
%Now for some convenient shortcuts so the user doesn't have to
%remember the command line options. \cs{string} is used in
%case the hyphen character has been made active.
%\begin{macro}{\TeXOSQueryLocale}
%Query the locale and store the result in the control sequence
%provided in the argument.
% \begin{macrocode}
\def\TeXOSQueryLocale#1{\TeXOSQuery{#1}{\string-l}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryCwd}
%Query the current working directory.
% \begin{macrocode}
\def\TeXOSQueryCwd#1{\TeXOSQuery{#1}{\string-c}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryHome}
%Query the user's home directory.
% \begin{macrocode}
\def\TeXOSQueryHome#1{\TeXOSQuery{#1}{\string-m}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryTmpDir}
%Query the temporary directory.
% \begin{macrocode}
\def\TeXOSQueryTmpDir#1{\TeXOSQuery{#1}{\string-t}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryVersion}
%Query the operating system version.
% \begin{macrocode}
\def\TeXOSQueryVersion#1{\TeXOSQuery{#1}{\string-r}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryArch}
%Query the operating system architecture.
% \begin{macrocode}
\def\TeXOSQueryArch#1{\TeXOSQuery{#1}{\string-a}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryName}
%Query the operating system name.
% \begin{macrocode}
\def\TeXOSQueryName#1{\TeXOSQuery{#1}{\string-o}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryNow}
%\changes{1.1}{2016-07-14}{changed catcode of D to 12}
%Query the current time stamp.
% \begin{macrocode}
\def\TeXOSQueryNow#1{%
% \end{macrocode}
% The \texttt{D} needs category code 12. (Don't need to worry about
% \texttt{Z} as \texttt{texosquery.jar} uses \texttt{+00'00'} for
% UTC+0.) This change can't be done with the other catcode changes
% in \cs{TeXOSQuery}, as this is only appropriate for the PDF dates.
% Save and restore the catcode rather than fiddle around with
% scoping.
% \begin{macrocode}
\edef\@texosquery@restore@D{%
\noexpand\catcode`\noexpand\D=\the\catcode`\D\relax}%
\catcode`\D=12\relax
\TeXOSQuery{#1}{\string-n}%
\@texosquery@restore@D
}
% \end{macrocode}
%\end{macro}
%
%If the file name is supplied using \cs{jobname} it may have
%double-quotes which will interfere with things.
%\begin{macro}{\texosquerystripquotes}
% \begin{macrocode}
\def\texosquerystripquotes#1{%
\@texosquery@stripquotes#1\@mid@texosquery@stripquotes
"\relax"\relax\@end@texosquery@stripquotes
}
\def\@texosquery@stripquotes#1"#2"{%
\@@texosquery@stripquotes#1#2%
}
\def\@@texosquery@stripquotes#1\@mid@texosquery@stripquotes#2\@end@texosquery@stripquotes{%
#1%
}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryFileDate}
%\changes{1.1}{2016-07-14}{changed catcode of D to 12}
%Query the time stamp of the file given in the second argument.
% \begin{macrocode}
\def\TeXOSQueryFileDate#1#2{%
\edef\@texosquery@restore@D{%
\noexpand\catcode`\noexpand\D=\the\catcode`\D\relax}%
\catcode`\D=12\relax
\TeXOSQuery{#1}{\string-d \string'\texosquerystripquotes{#2}\string'}%
\@texosquery@restore@D
}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryFileSize}
%Query the size of the file given in the second argument.
% \begin{macrocode}
\def\TeXOSQueryFileSize#1#2{\TeXOSQuery{#1}{\string-s
\string'\texosquerystripquotes{#2}\string'}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryFileList}
%List all files in the directory given in the third argument,
%separated by the second argument.
% \begin{macrocode}
\def\TeXOSQueryFileList#1#2#3{\TeXOSQuery{#1}{%
\string-i \string'#2\string'
\string'\texosquerystripquotes{#3}\string'}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryFilterFileList}
%Filtered list files in the directory given in the fourth argument,
%separated by the second argument. The third argument is the regular
%expression used to filter the list. \emph{Take care of backslashes
%in the regular expression!}
% \begin{macrocode}
\def\TeXOSQueryFilterFileList#1#2#3#4{\TeXOSQuery{#1}{%
\string-f \string'#2\string' \string'#3\string'
\string'\texosquerystripquotes{#4}\string'}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryFileURI}
%Get the URI of the file given in the second argument.
% \begin{macrocode}
\def\TeXOSQueryFileURI#1#2{\TeXOSQuery{#1}{\string-u
\string'\texosquerystripquotes{#2}\string'}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryFilePath}
%Get the canonical path of the file given in the second argument.
% \begin{macrocode}
\def\TeXOSQueryFilePath#1#2{\TeXOSQuery{#1}{\string-p
\string'\texosquerystripquotes{#2}\string'}}
% \end{macrocode}
%\end{macro}
%
%\begin{macro}{\TeXOSQueryDirName}
%\changes{1.1}{2016-07-14}{new}
%Get the canonical path of the directory containing the file given
%in the second argument.
% \begin{macrocode}
\def\TeXOSQueryDirName#1#2{\TeXOSQuery{#1}{\string-e
\string'\texosquerystripquotes{#2}\string'}}
% \end{macrocode}
%\end{macro}
%
%All done.
%Restore the category code of \texttt{@}:
% \begin{macrocode}
\@texosquery@restore@at
% \end{macrocode}
%\iffalse
% \begin{macrocode}
%</texosquery.tex>
% \end{macrocode}
%\fi
%
%\subsection{\LaTeX\ Code}
%This is just a simple wrapper for \texttt{texosquery.tex}
%so that it can be loaded using \LaTeX's standard \cs{usepackage}
%method.
%\iffalse
% \begin{macrocode}
%<*texosquery.sty>
% \end{macrocode}
%\fi
%Identify package:
% \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{texosquery}[2016/07/14 v1.1 (NLCT)]
% \end{macrocode}
%Load \texttt{texosquery.tex}:
% \begin{macrocode}
\input{texosquery}
% \end{macrocode}
%That's it!
%\iffalse
% \begin{macrocode}
%</texosquery.sty>
% \end{macrocode}
%\fi
%
%\Finale
\endinput
|