<?xml version="1.0" encoding="UTF-8"?>
<!-- **************************************************************************
.... For copyright and licensing terms, see the file named COPYING.
.... **************************************************************************
.-->
<?xml-stylesheet href="docbook-xml.css" type="text/css"?>

<refentry id="userenv-fromenv">

<refmeta xmlns:xi="http://www.w3.org/2001/XInclude">
<refentrytitle>userenv-fromenv</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="manual">user commands</refmiscinfo>
<refmiscinfo class="source">nosh</refmiscinfo>
<xi:include href="version.xml" />
</refmeta>

<refnamediv>
<refname>userenv-fromenv</refname>
<refpurpose>set the "user" environment variables from a given UID</refpurpose>
</refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>userenv-fromenv</command>
<arg choice='opt'>--no-set-user</arg>
<arg choice='opt'>--no-set-shell</arg>
<arg choice='opt'>--default-locale</arg>
<arg choice='opt'>--default-path</arg>
<arg choice='opt'>--default-term</arg>
<arg choice='opt'>--default-timezone</arg>
<arg choice='opt'>--default-tools</arg>
<arg choice='opt'>--set-xdg</arg>
<arg choice='opt'>--set-dbus</arg>
<arg choice='opt'>--set-other</arg>
<arg choice='opt'>--toolkit-pager</arg>
<arg choice='req'><replaceable>next-prog</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>

<para>
<command>userenv-fromenv</command> is a chain-loading utility that sets environment variables and then chain loads to <replaceable>next-prog</replaceable> with the <citerefentry><refentrytitle>execvp</refentrytitle><manvolnum>3</manvolnum></citerefentry> function.
</para>

<para>
<replaceable>next-prog</replaceable> may contain its own command line options, which <command>userenv-fromenv</command> will ignore.
</para>

<para>
<command>userenv-fromenv</command> takes the value given in the <envar>UID</envar> environment variable as a user ID to look up in the system account database.
That variable can be set by commands such as <citerefentry><refentrytitle>getuidgid</refentrytitle><manvolnum>1</manvolnum></citerefentry>, <citerefentry><refentrytitle>envuidgid</refentrytitle><manvolnum>1</manvolnum></citerefentry>. or <citerefentry><refentrytitle>login-envuidgid</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
It first looks up the account record whose name matches the value given in the <envar>LOGNAME</envar> environment variable, if that variable is set.
If that account record's user ID field has the desired ID, it uses that record; otherwise, it looks up the account record by user ID.
</para>
<note>
This lookup by name falling back to lookup by ID allows for systems where multiple user names map to the same user ID in the system account database.
It ensures that the right account record, with the desired name, home directory, and shell fields, is found and used.
</note>

<para>
From the contents of the account database record, it sets these environment variables.
</para>

<refsection><title>User account database variables</title>

<para>
The following environment variables are set unless the <arg choice='plain'>--no-set-user</arg> command-line option is used:
</para>
<variablelist>

<varlistentry>
<term><envar>HOME</envar></term>
<listitem><para>Set to the pathname of the user's home directory, as given in the system account database, falling back if the field is blank to unset.</para></listitem>
</varlistentry><varlistentry>
<term><envar>USER</envar></term>
<term><envar>LOGNAME</envar></term>
<listitem><para>Set to the user's name, as given in the system account database, falling back if the field is blank to unset.</para></listitem>
</varlistentry>

</variablelist>

<para>
The following environment variables are set unless the <arg choice='plain'>--no-set-shell</arg> command-line option is used:
</para>
<variablelist>

<varlistentry>
<term><envar>SHELL</envar></term>
<listitem><para>
Set to the pathname of the user's shell program, as given in the system account database, falling back if the field is blank to the system's default "B" shell name that is hardwired into the C library.
(This fallback is in line with the behaviour of <citerefentry><refentrytitle>login</refentrytitle><manvolnum>1</manvolnum></citerefentry>.)
</para></listitem>
</varlistentry>

</variablelist>

</refsection><refsection><title>Path variables</title>

<para>
The following environment variables are set from the <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> database if the <arg choice='plain'>--default-path</arg> command-line option is used and they are not already set:
</para>
<variablelist>

<varlistentry>
<term><envar>PATH</envar></term>
<listitem><para>Set to the value of the <code>path</code> capability, defaulting if there is no capability to the system's default path that is hardwired into the C library.</para></listitem>
</varlistentry><varlistentry>
<term><envar>MANPATH</envar></term>
<listitem><para>Set to the value of the <code>manpath</code> capability, defaulting to unset if there is no capability.</para></listitem>
</varlistentry><varlistentry>
<term><envar>TERMPATH</envar></term>
<listitem><para>Set to the value of the <code>termpath</code> capability, defaulting if there is no capability to <filename>/usr/local/etc/termcap</filename>:<filename>/etc/termcap</filename>:<filename>/usr/pkg/etc/termcap</filename>:<filename>/usr/share/misc/termcap</filename>.</para></listitem>
</varlistentry><varlistentry>
<term><envar>TERMINFO_DIRS</envar></term>
<listitem><para>Set to the value of the <code>terminfopath</code> capability, defaulting if there is no capability to <filename>/usr/local/etc/terminfo</filename>:<filename>/etc/terminfo</filename>:<filename>/usr/pkg/etc/terminfo</filename>:<filename>/usr/share/misc/terminfo</filename>.</para></listitem>
</varlistentry>

</variablelist>

</refsection><refsection><title>Tools variables</title>

<para>
The following environment variables are set from the <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> database if the <arg choice='plain'>--default-tools</arg> command-line option is used and they are not already set:
</para>
<variablelist>

<varlistentry>
<term><envar>EDITOR</envar></term>
<listitem><para>
Set to the value of the <code>editor</code> capability, defaulting if there is no capability to <command>ex</command>.
(POSIX gives two choices of universally available line editor.
This is the one that is not <citerefentry><refentrytitle>ed</refentrytitle><manvolnum>1</manvolnum></citerefentry>.)
</para></listitem>
</varlistentry><varlistentry>
<term><envar>VISUAL</envar></term>
<listitem><para>
Set to the value of the <code>visual</code> capability, defaulting if there is no capability to <command>vi</command>.
(POSIX gives one choice of universally available full-screen editor.)
</para></listitem>
</varlistentry><varlistentry>
<term><envar>PAGER</envar></term>
<listitem><para>
Set to the value of the <code>pager</code> capability, defaulting if there is no capability to <command>more</command>.
(The Single Unix Specification dropped <command>pg</command> in 2017.
<command>less</command> is not universal, and where it is available it is usually also available as <command>more</command> anyway.
<command>most</command> is likewise not universal.)
If the <arg choice='plain'>--toolkit-pager</arg> command-line option is used that default changes from the standard <citerefentry><refentrytitle>more</refentrytitle><manvolnum>1</manvolnum></citerefentry> to the <citerefentry><refentrytitle>console-tty37-viewer</refentrytitle><manvolnum>1</manvolnum></citerefentry> that comes in the same toolkit as <command>userenv-fromenv</command>.
</para></listitem>
</varlistentry>

</variablelist>

</refsection><refsection><title>Terminal variables</title>

<para>
The following environment variables are set from the <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> database if the <arg choice='plain'>--default-term</arg> command-line option is used and they are not already set:
</para>
<variablelist>

<varlistentry>
<term>
<citerefentry><refentrytitle><envar>TERM</envar></refentrytitle><manvolnum>7</manvolnum></citerefentry>
</term>
<listitem><para>
Set to the value of the <code>term</code> capability, defaulting if there is no capability to <code>ansi</code>.
The <envar>TERM</envar> variable will have been set for a login session by the TTY login system, and this default will thus not be required.
</para></listitem>
</varlistentry>

</variablelist>

</refsection><refsection><title>Locale variables</title>

<para>
The following environment variables are set from the <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> database if the <arg choice='plain'>--default-locale</arg> command-line option is used and they are not already set:
</para>
<variablelist>

<varlistentry>
<term><envar>LANG</envar></term>
<listitem><para>Set to the value of the <code>lang</code> capability, defaulting to <code>POSIX</code> if there is no capability.</para></listitem>
</varlistentry><varlistentry>
<term><envar>MM_CHARSET</envar></term>
<listitem><para>Set to the value of the <code>charset</code> capability, defaulting to <code>UTF-8</code> if there is no capability.</para></listitem>
</varlistentry>

</variablelist>

</refsection><refsection><title>Timezone variables</title>

<para>
The following environment variables are set from the <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> database if the <arg choice='plain'>--default-timezone</arg> command-line option is used and they are not already set:
</para>
<variablelist>

<varlistentry>
<term><envar>TZ</envar></term>
<listitem><para>Set to the value of the <code>timezone</code> capability, defaulting to <code>UTC</code> if there is no capability.</para></listitem>
</varlistentry>

</variablelist>

</refsection><refsection><title>XDG variables</title>

<para>
The following environment variables are set if the <arg choice='plain'>--set-xdg</arg> command-line option is used:
</para>
<variablelist>

<varlistentry>
<term><envar>XDG_RUNTIME_DIR</envar></term>
<listitem><para>Set to <filename>/run/user/<replaceable>username</replaceable>/</filename>.</para></listitem>
</varlistentry>
<varlistentry>
<term><envar>XDG_CONFIG_DIRS</envar></term>
<listitem><para>Set to <filename>/usr/local/etc/xdg:/etc/xdg:/usr/pkg/etc/xdg</filename>.</para></listitem>
The addition of <filename>/usr/local/etc</filename> comes from the BSDs.
<filename>/usr/pkg/etc</filename> is the conventional <filename><replaceable>$LOCALBASE</replaceable>/etc</filename> from NetBSD pkgsrc.
</varlistentry>
<varlistentry>
<term><envar>XDG_CONFIG_HOME</envar></term>
<listitem><para>Unset, so that applications use the default of <filename><replaceable>$HOME</replaceable>/.config</filename>.</para></listitem>
</varlistentry>
<varlistentry>
<term><envar>XDG_DATA_DIRS</envar></term>
<listitem><para>
Set to <filename>/usr/local/share</filename>:<filename>/usr/pkg/share</filename>:<filename>/usr/share</filename>:<filename>/share</filename>.
The addition of <filename>/share</filename> comes from TrueOS Desktop.
<filename>/usr/pkg/share</filename> is the conventional <filename><replaceable>$LOCALBASE</replaceable>/share</filename> from NetBSD pkgsrc.
</para><note>
TrueOS Desktop ordered the directories the other way around, giving operating system data files priority over "site local" ones.
This ordering, on the other hand, is the ordering that is used in the XDG Desktop Specification.
</note></listitem>
</varlistentry>
<varlistentry>
<term><envar>XDG_DATA_HOME</envar></term>
<listitem><para>Unset, so that applications use the default of <filename><replaceable>$HOME</replaceable>/.local/share</filename>.</para></listitem>
</varlistentry>
<varlistentry>
<term><envar>XDG_STATE_HOME</envar></term>
<listitem><para>Unset, so that applications use the default of <filename><replaceable>$HOME</replaceable>/.local/state</filename>.</para></listitem>
</varlistentry>
<varlistentry>
<term><envar>XDG_CACHE_HOME</envar></term>
<listitem><para>Unset, so that applications use the default of <filename><replaceable>$HOME</replaceable>/.cache</filename>.</para></listitem>
</varlistentry>
<varlistentry>
<term><envar>PATH</envar></term>
<listitem><para>The value <filename><replaceable>$HOME</replaceable>/.local/bin</filename> is prepended, after any <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> processing has been done.</para></listitem>
</varlistentry>
<varlistentry>
<term><envar>MANPATH</envar></term>
<listitem><para>The value <filename><replaceable>$HOME</replaceable>/.local/share/man</filename> is prepended, after any <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> processing has been done.</para></listitem>
</varlistentry>
<varlistentry>
<term><envar>TERMPATH</envar></term>
<listitem><para>The value <filename><replaceable>$HOME</replaceable>/.config/termcap</filename> is prepended, after any <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> processing has been done.</para></listitem>
</varlistentry>

</variablelist>

</refsection><refsection><title>Desktop Bus variables</title>

<para>
The following environment variables are set if the <arg choice='plain'>--set-dbus</arg> command-line option is used:
</para>
<variablelist>

<varlistentry>
<term><envar>DBUS_SESSION_BUS_ADDRESS</envar></term>
<listitem><para>
Set to <filename>unix:path=/run/user/<replaceable>username</replaceable>/bus</filename>.
(In practice, <filename>unix:runtime=yes</filename> is not actually supported yet by most operating systems.)
</para></listitem>
</varlistentry>

</variablelist>

</refsection><refsection><title>Other variables</title>

<para>
If the <arg choice='plain'>--set-other</arg> command-line option is used, all environment variables are set from the <code>setenv</code> capability in the <citerefentry><refentrytitle>login.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry> database.
Both the system and user databases are processed.
In line with the behaviour of <citerefentry><refentrytitle>login</refentrytitle><manvolnum>1</manvolnum></citerefentry>, any conflicting environment variables set here will be overridden by the variables taken from other capabilities and from the user account database, with the exceptions of the tools, XDG, and Desktop Bus variables, which will conversely be overriden by any settings here.
This ordering is because the tools, XDG, and Desktop Bus variables are an extension, setting variables for which people will have been using <code>setenv</code>.
</para>

</refsection>

</refsection><refsection><title>bugs and compatibility</title>

<para>
As of version 1.34 this program no longer employs the GNU C library nor the BSD C library for environment variable handling.
The previously documented workaround for those libraries' have memory leaks in their <citerefentry><refentrytitle>setenv</refentrytitle><manvolnum>3</manvolnum></citerefentry> and <citerefentry><refentrytitle>clearenv</refentrytitle><manvolnum>3</manvolnum></citerefentry> functions, is therefore no longer necessary.
</para>

<para>
The FreeBSD <citerefentry><refentrytitle>login_getpath</refentrytitle><manvolnum>3</manvolnum></citerefentry> library function has a bug that makes it impossible to specify paths containing space characters.
This command implements the OpenBSD mechanism of allowing space characters, in the <code>path</code> and <code>manpath</code> capabilities, to be escaped with a backslash.
</para>

<para>
The FreeBSD <citerefentry><refentrytitle>login_getcaplist</refentrytitle><manvolnum>3</manvolnum></citerefentry> library function has several bugs, one of which makes it impossible to specify paths containing comma characters.
This command implements the OpenBSD mechanism of allowing comma characters, in the <code>setenv</code> capability, to be escaped with a backslash.
</para>

<para>
In both cases, the backslash must be <emphasis>itself</emphasis> escaped again in the actual capability file text, as this is <emphasis>two</emphasis> levels of escaping.
First the comma/space is escaped to make an escape sequence in the unencoded capability string, then the capability string is encoded with a second level of escaping in order to store it as text in the capability file.
</para>

<para>
Unlike the OpenBSD behaviour, this command does not restrict the expansion of <code>~</code> and <code>$</code> to certain places in the aforementioned environment list and path strings.
To include literal tildes and dollar characters <emphasis>anywhere</emphasis> in environment lists and paths, those characters too <emphasis>must always</emphasis> be (double) escaped with a backslash.
</para>

</refsection><refsection><title>See also</title>

<itemizedlist>
<listitem><para>
<citerefentry><refentrytitle>userenv</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>machineenv</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>setenv</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>envuidgid</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>clearenv</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
</itemizedlist>

</refsection><refsection><title>Author</title>
<para><author><personname><firstname>Jonathan</firstname> <surname>de Boyne Pollard</surname></personname></author></para>
</refsection>

</refentry>
