summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ifplatform/ifplatform.dtx
blob: 0fe2dd70b53dae7e640bdabb343c4e1c73140055 (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
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
%\iffalse
%<*internal>
\iffalse
%</internal>
%<*internal>
\fi
\begingroup
%</internal>
%<*batchfile>
\input docstrip.tex
\keepsilent
\preamble
  ________________________________________________________
  Copyright (C) 2007-2017  Will Robertson & Johannes Große
  License information appended.
\endpreamble
\postamble
Copyright (C) 2007-2017 by Will Robertson & Johannes Große

Distributable under the LaTeX Project Public License,
version 1.3c or higher (your choice). The latest version of
this license is at: http://www.latex-project.org/lppl.txt

This work is "author-maintained" by Will Robertson.

This work consists of the file  ifplatform.dtx
          and the derived files ifplatform.pdf,
                                ifplatform.sty, and
                                ifplatform.ins.
\endpostamble
\askforoverwritefalse
\generate{\file{ifplatform.sty}{\from{ifplatform.dtx}{package}}}
%</batchfile>
%<batchfile>\endbatchfile
%<*internal>
\generate{\file{\jobname.ins}{\from{\jobname.dtx}{batchfile}}}
\nopreamble\nopostamble
\endgroup
\immediate\write18{makeindex -s gind.ist -o \jobname.ind  \jobname.idx}
\immediate\write18{makeindex -s gglo.ist -o \jobname.gls  \jobname.glo}
%</internal>
%<*driver>
\documentclass{ltxdoc}
\errorcontextlines=999
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
\usepackage{array,booktabs,color,enumitem,geometry,hologo,microtype}
\usepackage[sc,osf]{mathpazo}
\usepackage[colorlinks]{hyperref}
\geometry{b5paper}
\linespread{1.1}       % A bit more space between lines
\frenchspacing         % Remove ugly extra space after punctuation
\definecolor{niceblue}{rgb}{0.2,0.4,0.8}
\def\theCodelineNo{\textcolor{niceblue}{\sffamily\tiny\arabic{CodelineNo}}}
\newcommand*\pkg[1]{\textsf{#1}}
\hfuzz2pt
\usepackage{ifplatform}% for version info
\ifwindows
  \typeout{ifplatform debug: using Windows.}
\fi\iflinux
  \typeout{ifplatform debug: using Linux.}
\fi\ifmacosx
  \typeout{ifplatform debug: using Mac OS X.}
\fi\ifcygwin
  \typeout{ifplatform debug: using Cygwin.}
\fi
\begin{document}
\DocInput{ifplatform.dtx}
\end{document}
%</driver>
%\fi
%
% \GetFileInfo{\jobname.sty}
% \CheckSum{0}
% \makeatletter
%
% \title{The \pkg{\jobname} package}
% \author{Original code by Johannes Gro\ss{}e\\
%         Package by Will Robertson\\
%         \color[gray]{0.5}
%         \texttt{http://github.com/wspr/ifplatform}}
% \date{\fileversion\thanks{Thanks to Ken Brown, Joseph Wright, Zebb Prime, and others for testing this package.}\qquad \filedate}
%
% \maketitle
%
% \section{Main features and usage}
%
% This package provides the three following conditionals to test
% which operating system is being used to run \TeX:
% \begin{itemize}[nolistsep,label={}]
% \item \cs{ifwindows}
% \item \cs{iflinux}
% \item \cs{ifmacosx}
% \item \cs{ifcygwin}
% \end{itemize}
% If you only wish to detect \cs{ifwindows}, then it does not matter how you
% load this package. Note then that use of (Linux \emph{or} \macosxname\ \emph{or} Cygwin) can
% then be detected with \cs{ifwindows}\cs{else}.
%
% If you also wish to determine the difference between which Unix-variant
% you are using (i.e., also detect \cs{iflinux}, \cs{ifmacosx}, and \cs{ifcygwin}) then shell
% escape must be enabled.
% This is achieved by using the |-shell-escape| command line option when
% executing \LaTeX.
%
% If shell escape is not enabled, \cs{iflinux}, \cs{ifmacosx}, and \cs{ifcygwin} will all return \emph{false}. A warning will be printed in the console output to remind you in this case.
%
% \section{Auxiliary features}
%
% \cs{ifshellescape} is provided as a conditional to test whether
% shell escape is active or not. (Note: new versions of pdf\/\TeX\
% allow you to query shell escape with \verb|\ifnum\pdfshellescape>0|\,,
% and the \pkg{pdftexcmds} package provides the wrapper \cs{pdf@shellescape}
% which works with \hologo{XeTeX}, \hologo{pdfTeX}, and \hologo{LuaTeX}.)
%
% Also, the \cmd\platformname\ command is defined to expand to a
% macro that represents the operating system. Default definitions are
% (respectively):
% \begin{quote}
% \begin{tabular}{@{}l@{\quad$\to$\quad}l}
% \cmd\windowsname & `\windowsname' \\
% \cmd\notwindowsname & `\notwindowsname' (when shell escape is disabled) \\
% \cmd\linuxname & `\linuxname' \\
% \cmd\macosxname & `\macosxname' \\
% \cmd\cygwinname & `\cygwinname' \\
% \cmd\unknownplatform & \emph{whatever is returned by} |uname| \\
% \end{tabular}
% \end{quote}
% E.g., if \cs{ifwindows} is \emph{true} then \cs{platformname}
% expands to \cs{windowsname}, which expands to `\windowsname'.
% Redefine the macros above to customise
% the output of \cmd\platformname.
%
% \begin{center}\itshape
%  This documentation was compiled on \platformname.
% \end{center}
%
% \section{Other platforms}
%
% If greater granularity is required to differentiate between various \textsc{unix}-like operating systems, then \cmd\unknownplatform\ can be interrogated for the platform based on the output of |uname|. Table~\ref{table} lists possible outputs for a range of operating systems.
%
% For example, to test whether the AIX operating system is being used, you could use the following code:
% \begin{quote}
% \begin{verbatim}
%\def\myplatform{aix6}
%\ifx\myplatform\unknownplatform
%  ... AIX is being used ...
%\else
%  ... or not ...
%\fi
% \end{verbatim}
% \end{quote}
% The \pkg{ifthen} and \pkg{xifthen} packages might be of interest to those who prefer more \LaTeX-like methods of conditional testing.
%
% \begin{table}[hp]
% \centering
% \begin{tabular}{@{}l>{\ttfamily}c@{}}
% \toprule
% Platform & uname \textrm{string} \\
% \midrule
% FreeBSD & {FreeBSD} \\
% OpenBSD & {OpenBSD} \\
% Solaris & {SunOS} \\
% HPUX & {HP-UX} \\
% IRIX & {IRIX64} \\
% AIX & {aix6} \\
% Cray UNICOS & {sn5176} \\
% \bottomrule
% \end{tabular}
% \caption{List of operating systems and their \texttt{uname} strings. Adapted from % \url{http://en.wikipedia.org/wiki/Uname}.}
% \label{table}
% \end{table}
%
% \section{Limitations}
%
% Some technical information in case things go wrong.
% \begin{itemize}
% \item \pkg{ifplatform} checks for Windows by the presence or absence of the file `|nul:|'. If you have a file in your search path in *nix called `|nul:.tex|' (or without the |.tex|) then things may become confused.
%
% \item \pkg{ifplatform} checks for *nix by the presence or absence of the file `|/dev/null|'. If you have the file in Windows called |/dev/null.tex| (or without the extension) then things might similarly get mixed up.
%
% \item When both null files are detected (i.e., things aren't right with one of the two tests above), \pkg{ifplatform} uses another test to try and sort itself out. For interest, the test is: `|echo # > \jobname.w18|'. Under Windows you should end up with a text file containing an octothorpe. On *nix, the |#| will be seen as a comment char and the test will be ignored and the file will not be written.
%
% This `last resort' test will fail if shell escape is not enabled, or if the file |\jobname.w18|
% somehow already exists, or if the behaviour of |#| isn't as reliable as I think.
%
% \item Note that if you're running \TeX\ binaries from Cygwin on Windows, then your platform will \emph{not} be Windows. It will appear to be a *nix system, with platform name `Cygwin'.
%
% \item If you ever see the error
% \begin{quote}\ttfamily I can't tell if this is Windows or *nix; you
% appear to be neither.\end{quote}
% then I'd dearly like to know how it happened. It should
% never occur, as far as I know.
% (Update: in previous versions of this package, this message appeared when running under \hologo{LuaTeX}.)
% \end{itemize}
% Keep these points in mind and you'll never run into trouble.
% I hope you won't run into trouble in any case.
%
% \clearpage
% \StopEventually{\PrintIndex}
% \section{Implementation}
%\iffalse
%<*package>
%\fi
%    \begin{macrocode}
\ProvidesPackage{ifplatform}
  [2017/10/13 v0.4a  Testing for the operating system]
%    \end{macrocode}
% Packages required: (thanks Heiko)
%    \begin{macrocode}
\RequirePackage{shellesc,pdftexcmds,catchfile,ifluatex}
%    \end{macrocode}
% Conditionals we provide:
%    \begin{macrocode}
\newif\ifshellescape
\newif\ifwindows
\newif\ifmacosx
\newif\iflinux
\newif\ifcygwin
%    \end{macrocode}
% \begin{macro}{\windowsname}
% \begin{macro}{\notwindowsname}
% \begin{macro}{\linuxname}
% \begin{macro}{\macosxname}
% \begin{macro}{\cygwinname}
% \begin{macro}{\unknownplatform}
% Names of operating systems:
%    \begin{macrocode}
\newcommand\windowsname{Windows}
\newcommand\notwindowsname{*NIX}
\newcommand\linuxname{Linux}
\newcommand\macosxname{Mac\,OS\,X}
\newcommand\cygwinname{Cygwin}
\newcommand\unknownplatform{[Unknown]}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% For internal stuff later:
%    \begin{macrocode}
\edef\ip@file{\jobname.w18}
\newif\if@ip@nix@
%    \end{macrocode}
% \begin{macro}{\ifshellescape}
% Determine if shell escape is enabled:
%    \begin{macrocode}
\ifnum\pdf@shellescape=1\relax
  \shellescapetrue
\else
  \ifluatex\else
    \PackageWarningNoLine{ifplatform}{^^J \space\space\space
      shell escape is disabled,
      so I can only detect \@backslashchar ifwindows%
    }
  \fi
\fi
%    \end{macrocode}
% \end{macro}
% An error message for when things go wrong:
%    \begin{macrocode}
\def\ip@cantdecide{%
  \PackageWarningNoLine{ifplatform}{^^J \space\space\space
    I can't tell if this is Windows or *nix;
    you appear to be both%
  }%
}
%    \end{macrocode}
% Now the platform test. In Lua\TeX\ this is straightforward:
%    \begin{macrocode}
\ifluatex
  \csname\directlua{
      if os.type == "unix" then
        tex.sprint("@ip@nix@true")
      elseif os.type == "windows" then
        tex.sprint("windowstrue")
      end
    }\endcsname
\else
%    \end{macrocode}
% Otherwise we need to
% test for the null files of Windows and *nix.
% (This doesn't work at all in LuaTeX. Not sure why; haven't looked.)
% In a normal situation, this is all we need to do:
%    \begin{macrocode}
  \IfFileExists{nul:}{\@ip@nix@false}{\@ip@nix@true}
  \IfFileExists{/dev/null}{\windowsfalse}{\windowstrue}
%    \end{macrocode}
% \begin{table}
% \centering
% \begin{tabular}{@{}llll@{}}
% \toprule
% File & Exists & Windows? & *nix? \\
% \midrule
% \texttt{nul:} & true & Probably & Maybe \\
% &  false & Definitely not & Definitely \\
% \texttt{/dev/null} & true & Maybe & Probably \\
% & false & Definitely & Definitely not \\
% \bottomrule
% \end{tabular}
% \caption{Possibilities for testing null files and their prospects for determining the platform.}
% \label{tbl}
% \end{table}
% However, sometimes that's not good enough.
% If things go wrong above, we still don't know which platform. Can only proceed if shell escape is on; fallback heuristic:
% \begin{itemize}[nolistsep]
% \item If the tmp file exists
% \begin{itemize}[nolistsep]
% \item Tell them to delete it and abort.
% \item Otherwise:
% \end{itemize}
% \item Write to it with |echo| that only works on Windows
% \item Then see again if it exists
% \begin{itemize}[nolistsep]
% \item If the tmp file exists: Windows (and delete the file)
% \item Otherwise: *nix
% \end{itemize}
% \end{itemize}
% Here's the code for the above `last resort' test:
%    \begin{macrocode}
  \edef\ip@windows@echo@test{echo \string# > "\ip@file"}
  \def\ip@backupplan{%
    \IfFileExists{\ip@file}{%
      \PackageWarningNoLine{ifplatform}{^^J \space\space\space
        Please delete the file "\ip@file" and try again%
      }%
      \ip@cantdecide
    }{%
      \ShellEscape{\ip@windows@echo@test}%
      \IfFileExists{\ip@file}{%
        \ShellEscape{del "\ip@file"}%
        \windowstrue
      }{%
        \@ip@nix@true
      }%
    }%
  }
%    \end{macrocode}
% Now we use some odd logic to deduce what's happening in the edge cases when things go wrong: (see table~\ref{tbl})
%    \begin{macrocode}
  \ifwindows
    \if@ip@nix@
      \PackageWarningNoLine{ifplatform}{^^J \space\space\space
        I can't tell if this is Windows or *nix;
        you appear to be neither%
      }
    \fi
  \else
    \if@ip@nix@\else
      \ifshellescape
        \ip@backupplan
      \else
        \ip@cantdecide
      \fi
    \fi
  \fi
\fi
%    \end{macrocode}
% Needed below:
%    \begin{macrocode}
\def\ip@only@six#1#2#3#4#5#6#7\@nil{#1#2#3#4#5#6}
%    \end{macrocode}
%
% \begin{macro}{\iflinux}
% \begin{macro}{\ifmacosx}
% \begin{macro}{\ifcygwin}
% Now test for the others; directly test for Linux and Mac\,OS\,X; but what about Solaris or FreeBSD or \dots\ ?
% Define \cmd\unknownplatform\ as the output of |uname| rather than enumerate the possibilities.
%    \begin{macrocode}
\if@ip@nix@\ifshellescape
  \ifwindows\else
    \ShellEscape{uname -s > "\ip@file"}
    \CatchFileDef\@tempa{\ip@file}{}
    \ShellEscape{rm -- "\ip@file"}
%    \end{macrocode}
% Kill a trailing space:
%    \begin{macrocode}
    \edef\@tempa{\expandafter\zap@space\@tempa\@empty}
    \def\@tempb{Linux}
    \ifx\@tempa\@tempb
      \linuxtrue
    \else
      \def\@tempb{Darwin}
      \ifx\@tempa\@tempb
        \macosxtrue
      \else
        \def\@tempb{CYGWIN}
        \edef\@tempc{\expandafter\ip@only@six\@tempa------\@nil}
        \ifx\@tempb\@tempc
          \cygwintrue
        \else
          \edef\unknownplatform{\@tempa}
        \fi
      \fi
    \fi
  \fi
\fi\fi
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\platformname}
% Defined in terms of macros so the output is user-customisable.
%    \begin{macrocode}
\edef\platformname{%
  \ifwindows
    \noexpand\windowsname
  \else
    \ifshellescape
      \iflinux
        \noexpand\linuxname
      \else
        \ifmacosx
          \noexpand\macosxname
        \else
          \ifcygwin
            \noexpand\cygwinname
          \else
            \noexpand\unknownplatform
          \fi
        \fi
      \fi
    \else
      \noexpand\notwindowsname
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%\iffalse
%</package>
%\fi
%
% \Finale
%
% \typeout{------------------------------------------------------}
% \typeout{ To finish the installation please move the following}
% \typeout{ file into a directory searched by LaTeX:}
% \typeout{ \space- ifplatform.sty}
% \typeout{------------------------------------------------------}
%
\endinput