line_splitter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "../libnetdata.h"
  3. #ifndef NETDATA_LINE_SPLITTER_H
  4. #define NETDATA_LINE_SPLITTER_H
  5. #define PLUGINSD_MAX_WORDS 30
  6. struct line_splitter {
  7. size_t count; // counts number of lines
  8. char *words[PLUGINSD_MAX_WORDS]; // an array of pointers for the words in this line
  9. size_t num_words; // the number of pointers used in this line
  10. };
  11. bool line_splitter_reconstruct_line(BUFFER *wb, void *ptr);
  12. static inline void line_splitter_reset(struct line_splitter *line) {
  13. line->num_words = 0;
  14. }
  15. int pluginsd_isspace(char c);
  16. int config_isspace(char c);
  17. int group_by_label_isspace(char c);
  18. extern bool isspace_map_pluginsd[256];
  19. extern bool isspace_map_config[256];
  20. extern bool isspace_map_group_by_label[256];
  21. static inline size_t quoted_strings_splitter(char *str, char **words, size_t max_words, bool *isspace_map) {
  22. char *s = str, quote = 0;
  23. size_t i = 0;
  24. // skip all white space
  25. while (unlikely(isspace_map[(uint8_t)*s]))
  26. s++;
  27. if(unlikely(!*s)) {
  28. words[i] = NULL;
  29. return 0;
  30. }
  31. // check for quote
  32. if (unlikely(*s == '\'' || *s == '"')) {
  33. quote = *s; // remember the quote
  34. s++; // skip the quote
  35. }
  36. // store the first word
  37. words[i++] = s;
  38. // while we have something
  39. while (likely(*s)) {
  40. // if it is an escape
  41. if (unlikely(*s == '\\' && s[1])) {
  42. s += 2;
  43. continue;
  44. }
  45. // if it is a quote
  46. else if (unlikely(*s == quote)) {
  47. quote = 0;
  48. *s = ' ';
  49. continue;
  50. }
  51. // if it is a space
  52. else if (unlikely(quote == 0 && isspace_map[(uint8_t)*s])) {
  53. // terminate the word
  54. *s++ = '\0';
  55. // skip all white space
  56. while (likely(isspace_map[(uint8_t)*s]))
  57. s++;
  58. // check for a quote
  59. if (unlikely(*s == '\'' || *s == '"')) {
  60. quote = *s; // remember the quote
  61. s++; // skip the quote
  62. }
  63. // if we reached the end, stop
  64. if (unlikely(!*s))
  65. break;
  66. // store the next word
  67. if (likely(i < max_words))
  68. words[i++] = s;
  69. else
  70. break;
  71. }
  72. // anything else
  73. else
  74. s++;
  75. }
  76. if (likely(i < max_words))
  77. words[i] = NULL;
  78. return i;
  79. }
  80. #define quoted_strings_splitter_query_group_by_label(str, words, max_words) \
  81. quoted_strings_splitter(str, words, max_words, isspace_map_group_by_label)
  82. #define quoted_strings_splitter_config(str, words, max_words) \
  83. quoted_strings_splitter(str, words, max_words, isspace_map_config)
  84. #define quoted_strings_splitter_pluginsd(str, words, max_words) \
  85. quoted_strings_splitter(str, words, max_words, isspace_map_pluginsd)
  86. static inline char *get_word(char **words, size_t num_words, size_t index) {
  87. if (unlikely(index >= num_words))
  88. return NULL;
  89. return words[index];
  90. }
  91. #endif //NETDATA_LINE_SPLITTER_H