parse.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2010 Philippe Pepiot <phil@philpep.org>
  3. * Copyright (c) 2011 Martin Duquesnoy <xorg62@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef PARSE_H
  18. #define PARSE_H
  19. #include "wmfs.h"
  20. #define INCLUDE_CMD "@include"
  21. #define PARSE_MAX_LIST 32
  22. struct conf_opt
  23. {
  24. char *name;
  25. char *val[PARSE_MAX_LIST];
  26. size_t nval;
  27. bool used;
  28. int line;
  29. char *filename;
  30. SLIST_ENTRY(conf_opt) entry;
  31. };
  32. struct conf_sec
  33. {
  34. char *name;
  35. SLIST_HEAD(, conf_opt) optlist;
  36. TAILQ_HEAD(cshead, conf_sec) sub;
  37. size_t nopt;
  38. size_t nsub;
  39. TAILQ_ENTRY(conf_sec) entry;
  40. };
  41. struct opt_type
  42. {
  43. long int num;
  44. float fnum;
  45. bool boolean;
  46. char *str;
  47. };
  48. /*
  49. * Create config from file
  50. * return -1 on failure
  51. */
  52. int get_conf(const char *);
  53. /*
  54. * Print unused option name from section s (and subsections).
  55. * If s == NULL print unused option name for all config struct.
  56. */
  57. void print_unused(struct conf_sec *s);
  58. /*
  59. * Free the config struct.
  60. * WARNING: This make all string
  61. * returned by fetch_(opt|section)(_first) unusable.
  62. */
  63. int free_conf(void);
  64. /*
  65. * Get all subsection matching the given name on the given
  66. * section.
  67. * If section == NULL, return subsections from root section.
  68. * Return a NULL terminated array.
  69. * Subsections are returned in order as they are in config file
  70. * WARNING : This MUST be free() after use.
  71. */
  72. struct conf_sec **fetch_section(struct conf_sec *, char *);
  73. /*
  74. * Get first subsection matching the given name
  75. * on the given section. (first found on the file)
  76. */
  77. struct conf_sec *fetch_section_first(struct conf_sec *, char *);
  78. /*
  79. * Count member of a conf_sec **
  80. */
  81. size_t fetch_section_count(struct conf_sec **);
  82. /*
  83. * Return all options matching the given name on the given subsection.
  84. * If none match or section == NULL return opt_type build with the
  85. * given default param.
  86. * WARNING: This MUST be free() after use.
  87. * WARNING: The string member is directly taken from the config struct.
  88. * WARNING: Returned in reverse order as they are in config file.
  89. * (I think the last option MUST overwrite all others)
  90. */
  91. struct opt_type fetch_opt_first(struct conf_sec *, char *, char *);
  92. /*
  93. * Get first (last in config file) option matching the given name
  94. * on the given section.
  95. * WARNING: The string member is directly taken from the config struct.
  96. */
  97. struct opt_type *fetch_opt(struct conf_sec *, char *, char *);
  98. /*
  99. * Count member of a opt_type *
  100. */
  101. size_t fetch_opt_count(struct opt_type *);
  102. #endif /* PARSE_H */