s_rdinstack.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "String.h"
  13. struct Sinstack{
  14. int depth;
  15. Biobuf *fp[32]; /* hard limit to avoid infinite recursion */
  16. };
  17. /* initialize */
  18. extern Sinstack *
  19. s_allocinstack(char *file)
  20. {
  21. Sinstack *sp;
  22. Biobuf *fp;
  23. fp = Bopen(file, OREAD);
  24. if(fp == nil)
  25. return nil;
  26. sp = malloc(sizeof *sp);
  27. sp->depth = 0;
  28. sp->fp[0] = fp;
  29. return sp;
  30. }
  31. extern void
  32. s_freeinstack(Sinstack *sp)
  33. {
  34. while(sp->depth >= 0)
  35. Bterm(sp->fp[sp->depth--]);
  36. free(sp);
  37. }
  38. /* Append an input line to a String.
  39. *
  40. * Empty lines and leading whitespace are removed.
  41. */
  42. static char *
  43. rdline(Biobuf *fp, String *to)
  44. {
  45. int c;
  46. int len = 0;
  47. c = Bgetc(fp);
  48. /* eat leading white */
  49. while(c==' ' || c=='\t' || c=='\n' || c=='\r')
  50. c = Bgetc(fp);
  51. if(c < 0)
  52. return 0;
  53. for(;;){
  54. switch(c) {
  55. case -1:
  56. goto out;
  57. case '\\':
  58. c = Bgetc(fp);
  59. if (c != '\n') {
  60. s_putc(to, '\\');
  61. s_putc(to, c);
  62. len += 2;
  63. }
  64. break;
  65. case '\r':
  66. break;
  67. case '\n':
  68. if(len != 0)
  69. goto out;
  70. break;
  71. default:
  72. s_putc(to, c);
  73. len++;
  74. break;
  75. }
  76. c = Bgetc(fp);
  77. }
  78. out:
  79. s_terminate(to);
  80. return to->ptr - len;
  81. }
  82. /* Append an input line to a String.
  83. *
  84. * Returns a pointer to the character string (or 0).
  85. * Leading whitespace and newlines are removed.
  86. * Lines starting with #include cause us to descend into the new file.
  87. * Empty lines and other lines starting with '#' are ignored.
  88. */
  89. extern char *
  90. s_rdinstack(Sinstack *sp, String *to)
  91. {
  92. char *p;
  93. Biobuf *fp, *nfp;
  94. s_terminate(to);
  95. fp = sp->fp[sp->depth];
  96. for(;;){
  97. p = rdline(fp, to);
  98. if(p == nil){
  99. if(sp->depth == 0)
  100. break;
  101. Bterm(fp);
  102. sp->depth--;
  103. return s_rdinstack(sp, to);
  104. }
  105. if(strncmp(p, "#include", 8) == 0 && (p[8] == ' ' || p[8] == '\t')){
  106. to->ptr = p;
  107. p += 8;
  108. /* sanity (and looping) */
  109. if(sp->depth >= nelem(sp->fp))
  110. sysfatal("s_recgetline: includes too deep");
  111. /* skip white */
  112. while(*p == ' ' || *p == '\t')
  113. p++;
  114. nfp = Bopen(p, OREAD);
  115. if(nfp == nil)
  116. continue;
  117. sp->depth++;
  118. sp->fp[sp->depth] = nfp;
  119. return s_rdinstack(sp, to);
  120. }
  121. /* got milk? */
  122. if(*p != '#')
  123. break;
  124. /* take care of comments */
  125. to->ptr = p;
  126. s_terminate(to);
  127. }
  128. return p;
  129. }