config.tex 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. . /lib/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{/lib/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. Another way of iterating on config sections is using the \texttt{config\_foreach} command.
  46. Syntax:
  47. \begin{Verbatim}
  48. config_foreach <function name> [<sectiontype>] [<arguments...>]
  49. \end{Verbatim}
  50. This command will run the supplied function for every single config section in the currently
  51. loaded config. The section name will be passed to the function as argument 1.
  52. If the section type is added to the command line, the function will only be called for
  53. sections of the given type.
  54. You can access already processed options with the \texttt{config\_get} command
  55. Syntax:
  56. \begin{Verbatim}
  57. # print the value of the option
  58. config_get <section> <option>
  59. # store the value inside the variable
  60. config_get <variable> <section> <option>
  61. \end{Verbatim}
  62. In busybox ash the three-option \texttt{config\_get} is faster, because it does not
  63. result in an extra fork, so it is the preferred way.
  64. Additionally you can also modify or add options to sections by using the
  65. \texttt{config\_set} command.
  66. Syntax:
  67. \begin{Verbatim}
  68. config_set <section> <option> <value>
  69. \end{Verbatim}
  70. If a config section is unnamed, an automatically generated name will
  71. be assigned internally, e.g. \texttt{cfg1}, \texttt{cfg2}, ...
  72. While it is possible, using unnamed sections through these autogenerated names is
  73. strongly discouraged. Use callbacks or \texttt{config\_foreach} instead.