cfg_parser.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * $Id$
  3. * Standalone Configuration File Parser
  4. *
  5. * Copyright (C) 2008 iptelorg GmbH
  6. * Written by Jan Janak <[email protected]>
  7. *
  8. * This file is part of SER, a free SIP server.
  9. *
  10. * SER is free software; you can redistribute it and/or modify it under the
  11. * terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /*!
  25. * \file
  26. * \brief SIP-router core :: Standalone Configuration File Parser
  27. *
  28. * \ingroup core
  29. * Module: \ref core
  30. *
  31. * See \ref ConfigEngine
  32. */
  33. #ifndef _CFG_PARSER_H
  34. #define _CFG_PARSER_H
  35. #include "str.h"
  36. #include <stdio.h>
  37. #define MAX_TOKEN_LEN 512 /*!< Token names cannot be longer than this value */
  38. /*! \brief Configuration flags */
  39. typedef enum cfg_flags {
  40. /*! \brief Extended tokens can contain also delimiters, in addition to
  41. * alpha-numeric characters, this is used on the righ side of assignments
  42. * where no quotes are used.
  43. */
  44. CFG_EXTENDED_ALPHA = (1 << 0),
  45. /*! \brief The parser performs case-insensitive comparisons of token strings by
  46. * default. The parser will use case-sensitive comparison instead if this
  47. * flag is set.
  48. */
  49. CFG_CASE_SENSITIVE = (1 << 1),
  50. /*! \brief This is a flag that can be set in the last element of cfg_option
  51. * arrays (this is the one with 0 as token name). When this flag is set
  52. * then the value or parsing function of the element will be used for
  53. * options that do not match any other element in the array.
  54. */
  55. CFG_DEFAULT = (1 << 2),
  56. /*! \brief When this flag is set then the name of the options is a prefix and all
  57. * options that have the same prefix will be matched by this entry.
  58. */
  59. CFG_PREFIX = (1 << 3),
  60. /*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
  61. * pkg_malloc, if the destination varaiable contains a pointer to a buffer
  62. * already then it will be freed with pkg_free first.
  63. */
  64. CFG_STR_PKGMEM = (1 << 4),
  65. /*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
  66. * shm_malloc, if the destination variable contains a pointer to a buffer
  67. * already then it will be freed with shm_free first.
  68. */
  69. CFG_STR_SHMMEM = (1 << 5),
  70. /*! \brief The result of cfg_parse_str_val will be in a buffer allocated by
  71. * malloc, if the destination variable contains a pointer to a buffer
  72. * already then it will be freed with free first.
  73. */
  74. CFG_STR_MALLOC = (1 << 6),
  75. /*! \brief The result of cfg_parse_str_val will be copied into a pre-allocated
  76. * buffer with a fixed size, a pointer to str variable which contains the
  77. * buffer and its size is passed to the function in parameter 'param'.
  78. */
  79. CFG_STR_STATIC = (1 << 7),
  80. } cfg_flags_t;
  81. enum cfg_token_type {
  82. CFG_TOKEN_EOF = -1,
  83. CFG_TOKEN_ALPHA = -2,
  84. CFG_TOKEN_STRING = -3
  85. };
  86. /*! \brief Structure representing a lexical token */
  87. typedef struct cfg_token {
  88. char buf [MAX_TOKEN_LEN];
  89. int type; /*!< Token type */
  90. str val; /*!< Token value */
  91. struct { /*!< Position of first and last character of token in file */
  92. int line; /*!< The starting/ending line of the token */
  93. int col; /*!< The starting/ending column of the token */
  94. } start, end;
  95. } cfg_token_t;
  96. struct cfg_parser;
  97. typedef int (*cfg_func_f)(void* param, struct cfg_parser* st,
  98. unsigned int flags);
  99. /*! \brief Token mapping structure.
  100. *
  101. * This structure is used to map tokens to values or function calls. Arrays of
  102. * such structures are typically provided by the caller of the parser.
  103. */
  104. typedef struct cfg_option {
  105. char* name; /*!< Token name */
  106. unsigned int flags;
  107. void* param; /*!< Pointer to the destination variable */
  108. int val; /*!< Value */
  109. cfg_func_f f; /*!< Parser function to be called */
  110. } cfg_option_t;
  111. /*! \brief Parser state */
  112. typedef struct cfg_parser {
  113. FILE* f; /*!< Handle of the currently open file */
  114. char* file; /*!< Current file name */
  115. int line; /*!< Current line */
  116. int col; /*!< Column index */
  117. struct cfg_option* options; /*!< Array of supported options */
  118. struct {
  119. cfg_func_f parser; /*!< Section parser function */
  120. void* param; /*!< Parameter value for the parser function */
  121. } section;
  122. struct cfg_token* cur_opt; /*!< Current option */
  123. } cfg_parser_t;
  124. extern struct cfg_option cfg_bool_values[];
  125. struct cfg_parser* cfg_parser_init(str* basedir, str* filename);
  126. void cfg_section_parser(struct cfg_parser* st, cfg_func_f parser, void* param);
  127. void cfg_set_options(struct cfg_parser* st, struct cfg_option* options);
  128. int sr_cfg_parse(struct cfg_parser* st);
  129. void cfg_parser_close(struct cfg_parser* st);
  130. struct cfg_option* cfg_lookup_token(struct cfg_option* options, str* token);
  131. /*! ! \brief Interface to the lexical scanner */
  132. int cfg_get_token(struct cfg_token* token, struct cfg_parser* st, unsigned int flags);
  133. /* Commonly needed parser functions */
  134. int cfg_eat_equal(struct cfg_parser* st, unsigned int flags);
  135. int cfg_eat_eol(struct cfg_parser* st, unsigned int flags);
  136. /*! \brief Parse section identifier of form [section]. The function expects parameter
  137. * param to be of type (str*). The result string is allocated using pkg_malloc
  138. * and is zero terminated. To free the memory use pkg_free(((str*)param)->s)
  139. */
  140. int cfg_parse_section(void* param, struct cfg_parser* st, unsigned int flags);
  141. /*! \brief Parse string parameter value, either quoted or unquoted */
  142. int cfg_parse_str_opt(void* param, struct cfg_parser* st, unsigned int flags);
  143. int cfg_parse_str(void* param, struct cfg_parser* st, unsigned int flags);
  144. int cfg_parse_enum_opt(void* param, struct cfg_parser* st, unsigned int flags);
  145. int cfg_parse_enum(void* param, struct cfg_parser* st, unsigned int flags);
  146. /*! \brief Parser integer parameter value */
  147. int cfg_parse_int_opt(void* param, struct cfg_parser* st, unsigned int flags);
  148. int cfg_parse_int(void* param, struct cfg_parser* st, unsigned int flags);
  149. /*! \brief Parse boolean parameter value */
  150. int cfg_parse_bool_opt(void* param, struct cfg_parser* st, unsigned int flags);
  151. int cfg_parse_bool(void* param, struct cfg_parser* st, unsigned int flags);
  152. #endif /* _CFG_PARSER_H */