readconf.c 2.7 KB

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