2006-10-14 00:41:34 +02:00
|
|
|
\subsubsection{Structure of the configuration files}
|
|
|
|
|
|
|
|
The config files are divided into sections and options/values.
|
|
|
|
|
|
|
|
Every section has a type, but does not necessarily have a name.
|
|
|
|
Every option has a name and a value and is assigned to the section
|
|
|
|
it was written under.
|
|
|
|
|
|
|
|
Syntax:
|
|
|
|
|
|
|
|
\begin{Verbatim}
|
2006-11-07 00:37:55 +01:00
|
|
|
config <type> ["<name>"] # Section
|
|
|
|
option <name> "<value>" # Option
|
2006-10-14 00:41:34 +02:00
|
|
|
\end{Verbatim}
|
|
|
|
|
|
|
|
Every parameter needs to be a single string and is formatted exactly
|
2006-11-07 00:37:55 +01:00
|
|
|
like a parameter for a shell function. The same rules for Quoting and
|
2006-10-14 00:41:34 +02:00
|
|
|
special characters also apply, as it is parsed by the shell.
|
|
|
|
|
|
|
|
\subsubsection{Parsing configuration files in custom scripts}
|
|
|
|
|
2006-11-07 00:37:55 +01:00
|
|
|
To be able to load configuration files, you need to include the common
|
2006-10-14 00:41:34 +02:00
|
|
|
functions with:
|
|
|
|
|
|
|
|
\begin{Verbatim}
|
|
|
|
. /etc/functions.sh
|
|
|
|
\end{Verbatim}
|
|
|
|
|
2006-11-07 00:37:55 +01:00
|
|
|
Then you can use \texttt{config\_load \textit{<name>}} to load config files. The function
|
|
|
|
first checks for \textit{<name>} as absolute filename and falls back to loading
|
2006-10-14 00:41:34 +02:00
|
|
|
it from \texttt{/etc/config} (which is the most common way of using it).
|
|
|
|
|
|
|
|
If you want to use special callbacks for sections and/or options, you
|
|
|
|
need to define the following shell functions before running \texttt{config\_load}
|
|
|
|
(after including \texttt{/etc/functions.sh}):
|
|
|
|
|
|
|
|
\begin{Verbatim}
|
|
|
|
config_cb() {
|
2006-11-07 00:37:55 +01:00
|
|
|
local type="$1"
|
|
|
|
local name="$2"
|
|
|
|
# commands to be run for every section
|
2006-10-14 00:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
option_cb() {
|
2006-11-07 00:37:55 +01:00
|
|
|
# commands to be run for every option
|
2006-10-14 00:41:34 +02:00
|
|
|
}
|
|
|
|
\end{Verbatim}
|
|
|
|
|
|
|
|
You can also alter \texttt{option\_cb} from \texttt{config\_cb} based on the section type.
|
|
|
|
This allows you to process every single config section based on its type
|
|
|
|
individually.
|
|
|
|
|
|
|
|
\texttt{config\_cb} is run every time a new section starts (before options are being
|
|
|
|
processed). You can access the last section through the \texttt{CONFIG\_SECTION}
|
|
|
|
variable. Also an extra call to \texttt{config\_cb} (without a new section) is generated
|
|
|
|
after \texttt{config\_load} is done.
|
|
|
|
That allows you to process sections both before and after all options were
|
|
|
|
processed.
|
|
|
|
|
2007-08-04 14:23:46 +02:00
|
|
|
Another way of iterating on config sections is using the \texttt{config\_foreach} command.
|
|
|
|
|
|
|
|
Syntax:
|
|
|
|
\begin{Verbatim}
|
|
|
|
config_foreach <function name> [<sectiontype>] [<arguments...>]
|
|
|
|
\end{Verbatim}
|
|
|
|
|
|
|
|
This command will run the supplied function for every single config section in the currently
|
|
|
|
loaded config. The section name will be passed to the function as argument 1.
|
|
|
|
If the section type is added to the command line, the function will only be called for
|
|
|
|
sections of the given type.
|
|
|
|
|
|
|
|
|
2006-10-14 00:41:34 +02:00
|
|
|
You can access already processed options with the \texttt{config\_get} command
|
|
|
|
Syntax:
|
|
|
|
|
|
|
|
\begin{Verbatim}
|
2007-08-04 13:58:31 +02:00
|
|
|
# print the value of the option
|
|
|
|
config_get <section> <option>
|
|
|
|
|
|
|
|
# store the value inside the variable
|
|
|
|
config_get <variable> <section> <option>
|
2006-10-14 00:41:34 +02:00
|
|
|
\end{Verbatim}
|
|
|
|
|
|
|
|
In busybox ash the three-option \texttt{config\_get} is faster, because it does not
|
|
|
|
result in an extra fork, so it is the preferred way.
|
|
|
|
|
2006-11-07 00:37:55 +01:00
|
|
|
Additionally you can also modify or add options to sections by using the
|
2006-10-14 00:41:34 +02:00
|
|
|
\texttt{config\_set} command.
|
|
|
|
|
|
|
|
Syntax:
|
|
|
|
|
|
|
|
\begin{Verbatim}
|
|
|
|
config_set <section> <option> <value>
|
|
|
|
\end{Verbatim}
|
|
|
|
|
2007-08-04 14:23:46 +02:00
|
|
|
If a config section is unnamed, an automatically generated name will
|
|
|
|
be assigned internally, e.g. \texttt{cfg1}, \texttt{cfg2}, ...
|
|
|
|
|
|
|
|
While it is possible, using unnamed sections through these autogenerated names is
|
|
|
|
strongly discouraged. Use callbacks or \texttt{config\_foreach} instead.
|
|
|
|
|