Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
5.2 KiB

  1. /* $OpenBSD: fparseln.c,v 1.4 2002/06/09 22:18:43 fgsch Exp $ */
  2. /* $NetBSD: fparseln.c,v 1.7 1999/07/02 15:49:12 simonb Exp $ */
  3. /*
  4. * Copyright (c) 1997 Christos Zoulas. 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. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Christos Zoulas.
  17. * 4. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #if defined(LIBC_SCCS) && !defined(lint)
  32. static const char rcsid[] = "$OpenBSD: fparseln.c,v 1.4 2002/06/09 22:18:43 fgsch Exp $";
  33. #endif /* LIBC_SCCS and not lint */
  34. #include <sys/cdefs.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include "util.h"
  39. static int isescaped(const char *, const char *, int);
  40. /* isescaped():
  41. * Return true if the character in *p that belongs to a string
  42. * that starts in *sp, is escaped by the escape character esc.
  43. */
  44. static int
  45. isescaped(sp, p, esc)
  46. const char *sp, *p;
  47. int esc;
  48. {
  49. const char *cp;
  50. size_t ne;
  51. /* No escape character */
  52. if (esc == '\0')
  53. return 1;
  54. /* Count the number of escape characters that precede ours */
  55. for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
  56. continue;
  57. /* Return true if odd number of escape characters */
  58. return (ne & 1) != 0;
  59. }
  60. /* fparseln():
  61. * Read a line from a file parsing continuations ending in \
  62. * and eliminating trailing newlines, or comments starting with
  63. * the comment char.
  64. */
  65. char *
  66. fparseln(fp, size, lineno, str, flags)
  67. FILE *fp;
  68. size_t *size;
  69. size_t *lineno;
  70. const char str[3];
  71. int flags;
  72. {
  73. static const char dstr[3] = { '\\', '\\', '#' };
  74. size_t s, len;
  75. char *buf;
  76. char *ptr, *cp;
  77. int cnt;
  78. char esc, con, nl, com;
  79. len = 0;
  80. buf = NULL;
  81. cnt = 1;
  82. if (str == NULL)
  83. str = dstr;
  84. esc = str[0];
  85. con = str[1];
  86. com = str[2];
  87. /*
  88. * XXX: it would be cool to be able to specify the newline character,
  89. * but unfortunately, fgetln does not let us
  90. */
  91. nl = '\n';
  92. while (cnt) {
  93. cnt = 0;
  94. if (lineno)
  95. (*lineno)++;
  96. if ((ptr = fgetln(fp, &s)) == NULL)
  97. break;
  98. if (s && com) { /* Check and eliminate comments */
  99. for (cp = ptr; cp < ptr + s; cp++)
  100. if (*cp == com && !isescaped(ptr, cp, esc)) {
  101. s = cp - ptr;
  102. cnt = s == 0 && buf == NULL;
  103. break;
  104. }
  105. }
  106. if (s && nl) { /* Check and eliminate newlines */
  107. cp = &ptr[s - 1];
  108. if (*cp == nl)
  109. s--; /* forget newline */
  110. }
  111. if (s && con) { /* Check and eliminate continuations */
  112. cp = &ptr[s - 1];
  113. if (*cp == con && !isescaped(ptr, cp, esc)) {
  114. s--; /* forget escape */
  115. cnt = 1;
  116. }
  117. }
  118. if (s == 0 && buf != NULL)
  119. continue;
  120. if ((cp = realloc(buf, len + s + 1)) == NULL) {
  121. free(buf);
  122. return NULL;
  123. }
  124. buf = cp;
  125. (void) memcpy(buf + len, ptr, s);
  126. len += s;
  127. buf[len] = '\0';
  128. }
  129. if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
  130. strchr(buf, esc) != NULL) {
  131. ptr = cp = buf;
  132. while (cp[0] != '\0') {
  133. int skipesc;
  134. while (cp[0] != '\0' && cp[0] != esc)
  135. *ptr++ = *cp++;
  136. if (cp[0] == '\0' || cp[1] == '\0')
  137. break;
  138. skipesc = 0;
  139. if (cp[1] == com)
  140. skipesc += (flags & FPARSELN_UNESCCOMM);
  141. if (cp[1] == con)
  142. skipesc += (flags & FPARSELN_UNESCCONT);
  143. if (cp[1] == esc)
  144. skipesc += (flags & FPARSELN_UNESCESC);
  145. if (cp[1] != com && cp[1] != con && cp[1] != esc)
  146. skipesc = (flags & FPARSELN_UNESCREST);
  147. if (skipesc)
  148. cp++;
  149. else
  150. *ptr++ = *cp++;
  151. *ptr++ = *cp++;
  152. }
  153. *ptr = '\0';
  154. len = strlen(buf);
  155. }
  156. if (size)
  157. *size = len;
  158. return buf;
  159. }
  160. #ifdef TEST
  161. int main(int, char **);
  162. int
  163. main(argc, argv)
  164. int argc;
  165. char **argv;
  166. {
  167. char *ptr;
  168. size_t size, line;
  169. line = 0;
  170. while ((ptr = fparseln(stdin, &size, &line, NULL,
  171. FPARSELN_UNESCALL)) != NULL)
  172. printf("line %d (%d) |%s|\n", line, size, ptr);
  173. return 0;
  174. }
  175. /*
  176. # This is a test
  177. line 1
  178. line 2 \
  179. line 3 # Comment
  180. line 4 \# Not comment \\\\
  181. # And a comment \
  182. line 5 \\\
  183. line 6
  184. */
  185. #endif /* TEST */