readconf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* readconf/readconf.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/readconf.h>
  19. #include <readconf/port.h>
  20. #include <readconf/do_op_set.h>
  21. /* returns zero if success, -1 if eof */
  22. static int parse_line(void);
  23. /* fill a line splits by READCONF_LINE_SPLIT. returns 0 if end of file. */
  24. static int read_line(void);
  25. char *line_buffer;
  26. int line_no = 0;
  27. struct readconf_op ops[] = {
  28. {
  29. "set", 3, do_op_set
  30. },
  31. {
  32. "show", 4, show_names
  33. },
  34. {
  35. "", 0, NULL
  36. }
  37. };
  38. int readconf_init(void) {
  39. line_buffer = readconf_malloc(READCONF_MAXLINESIZE);
  40. if (!line_buffer) {
  41. readconf_error("libreadconf: failed to allocate line buffer. \n");
  42. return 1;
  43. }
  44. while (!parse_line());
  45. free(line_buffer);
  46. return 0;
  47. }
  48. static int parse_line(void) {
  49. struct readconf_op *o;
  50. if (!read_line())
  51. return 1;
  52. for (o = ops; o->do_op; o++)
  53. if (!strncmp(line_buffer, o->op, o->oplen))
  54. return o->do_op();
  55. readconf_errorf("no valid op found");
  56. return 1;
  57. }
  58. static int read_line(void) {
  59. int i;
  60. char c;
  61. memset(line_buffer, 0x00, READCONF_MAXLINESIZE);
  62. for (i = 0; i < READCONF_MAXLINESIZE; i++) {
  63. c = 0;
  64. line_buffer[i] = c = readconf_getbyte();
  65. if (c == READCONF_LINE_SPLIT) {
  66. line_buffer[i] = 0;
  67. line_no++;
  68. if (!i)
  69. return read_line();
  70. return i;
  71. }
  72. else if (c == READCONF_COMMENT) {
  73. while (c != READCONF_LINE_SPLIT)
  74. if (!(c = readconf_getbyte()))
  75. /* when comment at end of file */
  76. return 0;
  77. line_no++;
  78. return read_line();
  79. }
  80. else if (c == -1)
  81. return 0;
  82. }
  83. return 0;
  84. }
  85. static const char *whitespace = READCONF_WHITESPACE;
  86. char *skip_space(char *buffer) {
  87. int i;
  88. for (i = 0; i < READCONF_MAXLINESIZE; i++) {
  89. int j;
  90. for (j = 0; whitespace[j]; j++)
  91. if (buffer[i] == whitespace[j])
  92. break;
  93. if (!whitespace[j])
  94. return buffer + i;
  95. }
  96. return NULL;
  97. }
  98. int len_of(char *start) {
  99. register int i;
  100. for (i = 0; i < READCONF_MAXLINESIZE; i++) {
  101. int j;
  102. if (!start[i])
  103. return i;
  104. for (j = 0; whitespace[j]; j++)
  105. if (start[i] == whitespace[j])
  106. break;
  107. if (whitespace[j])
  108. return i;
  109. }
  110. return 0;
  111. }