config.tex 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. \subsubsection{Structure of the configuration files}
  2. The config files are divided into sections and options/values.
  3. Every section has a type, but does not necessarily have a name.
  4. Every option has a name and a value and is assigned to the section
  5. it was written under.
  6. Syntax:
  7. \begin{Verbatim}
  8. config <type> ["<name>"] # Section
  9. option <name> "<value>" # Option
  10. \end{Verbatim}
  11. Every parameter needs to be a single string and is formatted exactly
  12. like a parameter for a shell function. The same rules for Quoting and
  13. special characters also apply, as it is parsed by the shell.
  14. \subsubsection{Parsing configuration files in custom scripts}
  15. To be able to load configuration files, you need to include the common
  16. functions with:
  17. \begin{Verbatim}
  18. . /etc/functions.sh
  19. \end{Verbatim}
  20. Then you can use \texttt{config\_load \textit{<name>}} to load config files. The function
  21. first checks for \textit{<name>} as absolute filename and falls back to loading
  22. it from \texttt{/etc/config} (which is the most common way of using it).
  23. If you want to use special callbacks for sections and/or options, you
  24. need to define the following shell functions before running \texttt{config\_load}
  25. (after including \texttt{/etc/functions.sh}):
  26. \begin{Verbatim}
  27. config_cb() {
  28. local type="$1"
  29. local name="$2"
  30. # commands to be run for every section
  31. }
  32. option_cb() {
  33. # commands to be run for every option
  34. }
  35. \end{Verbatim}
  36. You can also alter \texttt{option\_cb} from \texttt{config\_cb} based on the section type.
  37. This allows you to process every single config section based on its type
  38. individually.
  39. \texttt{config\_cb} is run every time a new section starts (before options are being
  40. processed). You can access the last section through the \texttt{CONFIG\_SECTION}
  41. variable. Also an extra call to \texttt{config\_cb} (without a new section) is generated
  42. after \texttt{config\_load} is done.
  43. That allows you to process sections both before and after all options were
  44. processed.
  45. You can access already processed options with the \texttt{config\_get} command
  46. Syntax:
  47. \begin{Verbatim}
  48. config_get <section> <option> # prints the value of the option
  49. config_get <variable> <section> <option> # stores the value inside the variable
  50. \end{Verbatim}
  51. In busybox ash the three-option \texttt{config\_get} is faster, because it does not
  52. result in an extra fork, so it is the preferred way.
  53. Additionally you can also modify or add options to sections by using the
  54. \texttt{config\_set} command.
  55. Syntax:
  56. \begin{Verbatim}
  57. config_set <section> <option> <value>
  58. \end{Verbatim}