<?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="read-conf">

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

<refnamediv><refname>read-conf</refname><refpurpose>read a shell-style configuration file and chain</refpurpose></refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>read-conf</command>
<arg choice='opt'>--oknofile</arg>
<arg choice='req'><replaceable>filename</replaceable></arg>
<arg choice='req'><replaceable>next-prog</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Description</title>

<para>
<command>read-conf</command> reads the file <replaceable>filename</replaceable>, constructing a sequence of environment variable assignments, and then attempts to chain load <replaceable>next-prog</replaceable> with the <citerefentry><refentrytitle>execvp</refentrytitle><manvolnum>3</manvolnum></citerefentry> function.
Unless the <arg choice='opt'>--oknofile</arg> command line option is given, then a failure to open the file for reading is a fatal error.
With that option, the failure will be ignored and <replaceable>next-prog</replaceable> chain loaded.
</para>

<para>
<command>read-conf</command> implements a very limited subset of the <citerefentry><refentrytitle>sh</refentrytitle><manvolnum>1</manvolnum></citerefentry> lexical syntax for shell script files.
The configuration files that it reads are usually shell script files that consist solely of simple variable assignments.
Variable, command, brace, and parameter substitution are generally not used and are not supported by <command>read-conf</command>.
(This limited syntax is actually a requirement for the likes of <filename>/etc/rc.conf</filename> on OpenBSD, where it is mandated that system configuration files be parseable by things that are not shell script interpreters.
<filename>/etc/os-release</filename> and <filename>/etc/machine-info</filename> on Linux operating systems have a similar requirement not to perform substitutions.)
</para>

<para>
Each assignment, once read, is split at the first equals character into a variable name and its value.
<command>read-conf</command> sets the environment variable named to the value given.
</para>

<note>
Assigning an empty value to a variable is not the same as unsetting the variable.
</note>

<para>
If either the variable name or its value contain an equals character, counter-intuitive things will happen to the environment.
</para>

</refsection>

<refsection><title>File lexing</title>

<para>
Assignments are normally separated by one or more whitespace characters.
Pairs of quotation mark characters will prevent whitespace from being treated specially, and the quoted sequence of characters, which can include newlines, will form part of one assignment, concatenated with whatever quoted or unquoted sequences precede or follow it.
</para>

<para>
Inside a double-quotation mark delimited sequence of characters:
A slash followed by a newline is discarded entirely.
A slash followed by any other character is that character.
This allows one to provide literal slashes and quotation marks.
</para>

<para>
In a non-quotation mark delimited sequence of characters:
A slash followed by a newline is discarded entirely.
A slash followed by any other character is that character.
This allows one to provide literal slashes, hashes, and quotation marks.
A hash at the start of a sequence is a line comment, that is discarded along with everything that follows it up to and including the end of the line.
</para>

<para>
Inside a single-quotation mark delimited sequence of characters no characters are discarded or treated specially, except of course the single quotation mark that terminates the sequence.
</para>

<refsection><title>Example file</title>

<programlisting>LANG="C"	# Set the LANG variable.
hash=#		# Only the second # on this line begins a comment.
backslash1='\'
backslash2="\\"
backslash3=\\
SHELL=/"bin"/'sh'	# All one value.
greeting="Hello there!"
</programlisting>

</refsection>

</refsection>

<refsection><title>Examples</title>

<informalexample>
<para>
This handy shell function reads system information from some conventional information files, that are defined to contain simple variable assignments without shell expansions or substitutions.
It takes advantage of the fact that these are all built-in commands (c.f. <citerefentry><refentrytitle>exec</refentrytitle><manvolnum>1</manvolnum></citerefentry>) to completely clear the environment including <envar>PATH</envar> before reading the information files into it.
</para>
<literallayout>
read_os () {
        if test -r /etc/os-release
        then
                clearenv setenv "$1" "$2" read-conf /etc/os-release printenv "$1"
        else
                clearenv setenv "$1" "$2" read-conf /usr/lib/os-release printenv "$1"
        fi
}
</literallayout>
</informalexample>

</refsection>

<refsection><title>Bugs</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' having 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>

</refsection>

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

<itemizedlist>
<listitem><para>
<citerefentry><refentrytitle>appendpath</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>prependpath</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>setenv</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>unsetenv</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>envdir</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para></listitem>
<listitem><para>
<citerefentry><refentrytitle>printenv</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>
