Enhancement request: please add `locale` to list of allowed \write18 binaries

Max Chernoff mseven at telus.net
Thu Jul 28 07:31:02 CEST 2022

(I'm *not* a TeX Live maintainer, just a regular user)

> The `locale` tool reads no files, writes no files, takes no input, and
> outputs an extremely limited set of information derived *entirely*
> from environment variables.

I can confirm that the official documentation agrees with this. From the
man page:

   $ man 1p locale
          locale -- get locale-specific information
          locale [-a|-m]
          locale [-ck] name...
          The  locale  utility  shall conform to the Base Definitions
          volume of POSIX.1-2017, Section 12.2, Utility Syntax Guide-
          The following options shall be supported:
          -a        Write  information about all available public lo-
                    cales. The available locales shall include POSIX,
                    representing  the  POSIX  locale.  The  manner in
                    which the implementation  determines  what  other
                    locales are available is implementation-defined.
          -c        Write  the  names  of selected locale categories;
                    see the STDOUT section.  The -c option  increases
                    readability  when  more  than one category is se-
                    lected (for example, via more  than  one  keyword
                    name  or  via a category name).  It is valid both
                    with and without the -k option.
          -k        Write the names and values of selected  keywords.
                    The  implementation may omit values for some key-
                    words; see the OPERANDS section.
          -m        Write names of available charmaps; see  the  Base
                    Definitions  volume of POSIX.1-2017, Section 6.1,
                    Portable Character Set.
          The following operand shall be supported:
          name      The name of a locale category as defined  in  the
                    Base  Definitions volume of POSIX.1-2017, Chapter
                    7, Locale, the name of a keyword in a locale cat-
                    egory,  or  the reserved name charmap.  The named
                    category or keyword shall be selected for output.
                    If  a  single name represents both a locale cate-
                    gory name and a keyword name in the  current  lo-
                    cale,  the  results  are  unspecified. Otherwise,
                    both category and keyword names can be  specified
                    as  name  operands, in any sequence. It is imple-
                    mentation-defined whether any keyword values  are
                    written  for  the categories LC_CTYPE and LC_COL-
          Not used.
          The following environment variables shall affect the execu-
          tion of locale:
          LANG      Provide a default value for the internationaliza-
                    tion variables that are unset or null.  (See  the
                    Base  Definitions volume of POSIX.1-2017, Section
                    8.2,  Internationalization  Variables   for   the
                    precedence of internationalization variables used
                    to determine the values of locale categories.)
          LC_ALL    If set to a non-empty string value, override  the
                    values  of  all  the  other  internationalization
          LC_CTYPE  Determine the locale for  the  interpretation  of
                    sequences  of  bytes  of  text data as characters
                    (for example, single-byte as  opposed  to  multi-
                    byte characters in arguments and input files).
                    Determine  the  locale that should be used to af-
                    fect the format and contents of  diagnostic  mes-
                    sages written to standard error.
          NLSPATH   Determine  the  location  of message catalogs for
                    the processing of LC_MESSAGES.
          The application shall  ensure  that  the  LANG,  LC_*,  and
          NLSPATH  environment  variables  specify the current locale
          environment to be written out; they shall be used if the -a
          option is not specified.
          The LANG variable shall be written first using the format:
          The  LC_ALL variable shall be written last, using the first
          format shown above. If it is not set, it shall  be  written
          The standard error shall be used only for  diagnostic  mes-
          The following exit values shall be returned:
           0    All  the  requested  information was found and output
          >0    An error occurred.
          The following sections are informative.

So I agree that this command looks sufficiently safe to be in the
restricted \write18 list.

> This would be useful to guess a default language with `babel` or
> `polyglossia`.

A few general comments here:

1: "locale" is a POSIX command, so it should work with macOS, BSD, and
Linux. It will not work with Windows though. There are other commands
that you can use on Windows to get the current language; however, none
of them would be safe enough for restricted \write18. It wouldn't be too
hard to make a "safe" wrapper script for Windows, but this is of course
extra work.

2: I don't think that this is a good idea. One of the key features of
TeX (and LaTeX) is that identical input files should produce identical
output across all systems. Guessing a default language like this would
break this guarantee, which is probably ill-advised. I think that a
better approach would be to issue a warning if a default language isn't
set. This avoids any issues with system dependency or "hidden" state.

3: This is already (roughly) possible with LuaTeX right now:

   $ luatex --no-shell-escape '\relax'
   This is LuaTeX, Version 1.15.0 (TeX Live 2022) 
   *\directlua{texio.write_nl(os.getenv "LANG")}
   warning  (pdf backend): no pages of output.
   Transcript written on texput.log.

-- Max

More information about the tex-live mailing list.