do_op_set.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* readconf/do_op_set.c
  2. *
  3. * This file is part of readconf.
  4. *
  5. * readconf is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * readconf is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with readconf. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <readconf.h>
  19. #include <port.h>
  20. #include <do_op_set.h>
  21. static int get_set_type(char *line);
  22. static char *get_set_name(char *line);
  23. static int get_conf_value(char *line, __uint64_t *value);
  24. static char *get_conf_field(char *line, int *len);
  25. struct klist_node name_list = {
  26. .value = NODETYPE_INVALID,
  27. .payload = NULL,
  28. .next = &name_list,
  29. .prev = &name_list
  30. };
  31. /* this calls from readconf.c:parse_line, line_buffer is the current
  32. * line. save name that we found to list, and stop if syntax error. */
  33. int do_op_set(void) {
  34. int type;
  35. int namelen;
  36. char *name, *name0end;
  37. if ((type = get_set_type(line_buffer)) == SET_TYPE_INVALID) {
  38. readconf_errorf("invalid set-op");
  39. return 1;
  40. }
  41. name = get_set_name(line_buffer);
  42. if (!name)
  43. return 1;
  44. namelen = len_of(name);
  45. name0end = readconf_malloc(namelen);
  46. strncpy(name0end, name, namelen);
  47. if (readconf_find_name_any(name0end, NULL)) {
  48. readconf_errorf("name already defined");
  49. readconf_free(name0end);
  50. return 1;
  51. }
  52. readconf_free(name0end);
  53. if (type == SET_TYPE_UINT) {
  54. struct readconf_uintconf *conf = readconf_malloc(sizeof(struct readconf_uintconf));
  55. struct klist_node *node = readconf_malloc(sizeof(struct klist_node));
  56. readconf_klist_init(node);
  57. /* never assumes it fail */
  58. node->value = NODETYPE_UINT;
  59. node->payload = conf;
  60. conf->conf_key = readconf_malloc(namelen);
  61. strncpy(conf->conf_key, name, namelen);
  62. conf->conf_key_len = namelen;
  63. if (!get_conf_value(line_buffer, &conf->conf_value)) {
  64. readconf_free(conf);
  65. readconf_free(node);
  66. readconf_free(name0end);
  67. return 1;
  68. }
  69. readconf_klist_add(&name_list, node);
  70. return 0;
  71. }
  72. else {
  73. struct readconf_strconf *conf = readconf_malloc(sizeof(struct readconf_strconf));
  74. struct klist_node *node = readconf_malloc(sizeof(struct klist_node));
  75. readconf_klist_init(node);
  76. node->value = NODETYPE_STR;
  77. node->payload = conf;
  78. conf->conf_key = readconf_malloc(namelen);
  79. strncpy(conf->conf_key, name, namelen);
  80. conf->conf_key_len = namelen;
  81. if (!(conf->conf_value = get_conf_field(line_buffer, &conf->conf_value_len))) {
  82. readconf_free(conf);
  83. readconf_free(node);
  84. readconf_free(name0end);
  85. return 1;
  86. }
  87. readconf_klist_add(&name_list, node);
  88. return 0;
  89. }
  90. return 0;
  91. }
  92. static int get_set_type(char *line) {
  93. switch (line[3]) {
  94. case 'i':
  95. return SET_TYPE_UINT;
  96. case 's':
  97. return SET_TYPE_STR;
  98. default:
  99. return SET_TYPE_INVALID;
  100. }
  101. /* :-) */
  102. return 144514;
  103. }
  104. static char *get_set_name(char *line) {
  105. char *start = line + 4;
  106. start = skip_space(start);
  107. if (!start) {
  108. readconf_errorf("no white space character");
  109. return NULL;
  110. }
  111. return start;
  112. }
  113. #define isdigit(c) ((c) >= '0' && (c) <= '9')
  114. #define atoi(c) ((c) - '0')
  115. static int get_conf_value(char *buffer, __uint64_t *p) {
  116. __uint64_t value = 0;
  117. int start;
  118. int i, j, base = 1;
  119. for (start = 0; buffer[start] != READCONF_EQUAL
  120. && start < READCONF_MAXLINESIZE; start++);
  121. if (start == READCONF_MAXLINESIZE) {
  122. readconf_errorf("no equal character");
  123. return 0;
  124. }
  125. for (; !isdigit(buffer[start])
  126. && start < READCONF_MAXLINESIZE; start++);
  127. if (start == READCONF_MAXLINESIZE) {
  128. readconf_errorf("a right value required");
  129. return 0;
  130. }
  131. for (i = 0; isdigit(buffer[start])
  132. && start < READCONF_MAXLINESIZE; i++, start++);
  133. start--;
  134. for (j = 0; j < i; j++, start--, base *= 10)
  135. value += atoi(buffer[start]) * base;
  136. *p = value;
  137. return 1;
  138. }
  139. static char *get_conf_field(char *buffer, int *len) {
  140. int i;
  141. char *bufinline;
  142. char *retb;
  143. for (i = 0; buffer[i] != READCONF_EQUAL
  144. && i < READCONF_MAXLINESIZE; i++);
  145. if (i == READCONF_MAXLINESIZE) {
  146. readconf_errorf("no equal character");
  147. return NULL;
  148. }
  149. bufinline = skip_space(buffer + i + 1);
  150. if (!bufinline) {
  151. readconf_errorf("a string required");
  152. return NULL;
  153. }
  154. *len = len_of(bufinline);
  155. retb = readconf_malloc(*len);
  156. strncpy(retb, bufinline, *len);
  157. return retb;
  158. }
  159. int show_names(void) {
  160. struct klist_node *node = name_list.next;
  161. int n;
  162. readconf_puts("names defined: \n");
  163. if (node == &name_list) {
  164. readconf_puts("\t(no name defined. )\n");
  165. return 0;
  166. }
  167. for (n = 0; node != &name_list; node = node->next, n++) {
  168. if (node->value == NODETYPE_STR) {
  169. struct readconf_strconf *conf = node->payload;
  170. readconf_printf("\tstring %s (%d): %s (%d)\n",
  171. conf->conf_key, conf->conf_key_len, conf->conf_value, conf->conf_value_len);
  172. }
  173. else if (node->value == NODETYPE_UINT) {
  174. struct readconf_uintconf *conf = node->payload;
  175. readconf_printf("\tint %s (%d): %ld\n",
  176. conf->conf_key, conf->conf_key_len, conf->conf_value);
  177. }
  178. }
  179. readconf_printf("total %d names defined. \n", n);
  180. /* success */
  181. return 0;
  182. }
  183. int clear_name_list(void) {
  184. struct klist_node *n, *next;
  185. for (n = name_list.next; n != &name_list; n = next) {
  186. next = n->next;
  187. if (n->payload)
  188. free(n->payload);
  189. free(n);
  190. }
  191. readconf_klist_init(&name_list);
  192. return 0;
  193. }
  194. void *readconf_find_name_any(char *name, int *type_out) {
  195. struct klist_node *node = name_list.next;
  196. if (node == &name_list)
  197. return NULL;
  198. for (; node != &name_list; node = node->next) {
  199. /* cast it to any because the positions are same */
  200. struct readconf_uintconf *conf = node->payload;
  201. if (!strcmp(name, conf->conf_key)) {
  202. if (type_out)
  203. *type_out = node->value;
  204. return conf;
  205. }
  206. }
  207. return NULL;
  208. }
  209. struct readconf_strconf *readconf_find_strname(char *name) {
  210. int type;
  211. struct readconf_strconf *conf = readconf_find_name_any(name, &type);
  212. if (!conf)
  213. return NULL;
  214. if (type != NODETYPE_STR)
  215. return NULL;
  216. return conf;
  217. }
  218. struct readconf_uintconf *readconf_find_uintname(char *name) {
  219. int type;
  220. struct readconf_uintconf *conf = readconf_find_name_any(name, &type);
  221. if (!conf)
  222. return NULL;
  223. if (type != NODETYPE_UINT)
  224. return NULL;
  225. return conf;
  226. }