list.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* $Id: list.c 986 2004-11-17 00:11:10Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * A generic method to parse a list of integers which are
  33. * delimited by commas and dashes to indicate individual
  34. * numbers and ranges
  35. * Provides both a way to process the list and determine
  36. * if an integer exists in the list.
  37. */
  38. #include "config.h"
  39. #include "defines.h"
  40. #include "common.h"
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <sys/types.h>
  44. #include <regex.h>
  45. #include <errno.h>
  46. list_t *
  47. new_list()
  48. {
  49. list_t *newlist;
  50. newlist = (list_t *)safe_malloc(sizeof(list_t));
  51. return (newlist);
  52. }
  53. /*
  54. * Processes a string (ourstr) containing the list in human readable
  55. * format and places the data in **list and finally returns 1 for
  56. * success, 0 for fail.
  57. */
  58. int
  59. parse_list(list_t ** listdata, char *ourstr)
  60. {
  61. list_t *listcur, *list_ptr;
  62. char *this = NULL;
  63. char *first, *second;
  64. int i, rcode;
  65. regex_t preg;
  66. char ebuf[EBUF_SIZE];
  67. char regex[] = "^[0-9]+(-[0-9]+)?$";
  68. char *token = NULL;
  69. /* compile the regex first */
  70. if ((rcode = regcomp(&preg, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
  71. regerror(rcode, &preg, ebuf, sizeof(ebuf));
  72. errx(1, "Unable to compile regex (%s): %s", regex, ebuf);
  73. }
  74. /* first iteration */
  75. this = strtok_r(ourstr, ",", &token);
  76. first = this;
  77. second = NULL;
  78. /* regex test */
  79. if (regexec(&preg, this, 0, NULL, 0) != 0) {
  80. warnx("Unable to parse: %s", this);
  81. return 0;
  82. }
  83. *listdata = new_list();
  84. list_ptr = *listdata;
  85. listcur = list_ptr;
  86. for (i = 0; i < strlen(this); i++) {
  87. if (this[i] == '-') {
  88. this[i] = '\0';
  89. second = &this[i + 1];
  90. }
  91. }
  92. list_ptr->min = strtoull(first, NULL, 0);
  93. if (second != NULL) {
  94. list_ptr->max = strtoull(second, NULL, 0);
  95. }
  96. else {
  97. list_ptr->max = list_ptr->min;
  98. }
  99. while (1) {
  100. this = strtok_r(NULL, ",", &token);
  101. if (this == NULL)
  102. break;
  103. first = this;
  104. second = NULL;
  105. /* regex test */
  106. if (regexec(&preg, this, 0, NULL, 0) != 0) {
  107. warnx("Unable to parse: %s", this);
  108. return 0;
  109. }
  110. listcur->next = new_list();
  111. listcur = listcur->next;
  112. for (i = 0; i < strlen(this); i++) {
  113. if (this[i] == '-') {
  114. this[i] = '\0';
  115. second = &this[i + 1];
  116. }
  117. }
  118. listcur->min = strtoull(first, NULL, 0);
  119. if (second != NULL) {
  120. listcur->max = strtoull(second, NULL, 0);
  121. }
  122. else {
  123. listcur->max = listcur->min;
  124. }
  125. }
  126. return 1;
  127. }
  128. /*
  129. * Checks to see if the given integer exists in the LIST.
  130. * Returns 1 for true, 0 for false
  131. */
  132. int
  133. check_list(list_t * list, COUNTER value)
  134. {
  135. list_t *current;
  136. current = list;
  137. do {
  138. if ((current->min != 0) && (current->max != 0)) {
  139. if ((value >= current->min) && (value <= current->max))
  140. return 1;
  141. }
  142. else if (current->min == 0) {
  143. if (value <= current->max)
  144. return 1;
  145. }
  146. else if (current->max == 0) {
  147. if (value >= current->min)
  148. return 1;
  149. }
  150. if (current->next != NULL) {
  151. current = current->next;
  152. }
  153. else {
  154. current = NULL;
  155. }
  156. } while (current != NULL);
  157. return 0;
  158. }
  159. /*
  160. * Free's all the memory associated with the given LIST
  161. */
  162. void
  163. free_list(list_t * list)
  164. {
  165. /* recursively go down the list */
  166. if (list->next != NULL)
  167. free_list(list->next);
  168. free(list);
  169. }
  170. /*
  171. Local Variables:
  172. mode:c
  173. indent-tabs-mode:nil
  174. c-basic-offset:4
  175. End:
  176. */