rc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "mk.h"
  10. char *termchars = "'= \t"; /*used in parse.c to isolate assignment attribute*/
  11. char *shflags = "-I"; /* rc flag to force non-interactive mode */
  12. int IWS = '\1'; /* inter-word separator in env - not used in plan 9 */
  13. /*
  14. * This file contains functions that depend on rc's syntax. Most
  15. * of the routines extract strings observing rc's escape conventions
  16. */
  17. /*
  18. * skip a token in single quotes.
  19. */
  20. static char *
  21. squote(char *cp)
  22. {
  23. Rune r;
  24. int n;
  25. while(*cp){
  26. n = chartorune(&r, cp);
  27. if(r == '\'') {
  28. n += chartorune(&r, cp+n);
  29. if(r != '\'')
  30. return(cp);
  31. }
  32. cp += n;
  33. }
  34. SYNERR(-1); /* should never occur */
  35. fprint(2, "missing closing '\n");
  36. return 0;
  37. }
  38. /*
  39. * search a string for characters in a pattern set
  40. * characters in quotes and variable generators are escaped
  41. */
  42. char *
  43. charin(char *cp, char *pat)
  44. {
  45. Rune r;
  46. int n, vargen;
  47. vargen = 0;
  48. while(*cp){
  49. n = chartorune(&r, cp);
  50. switch(r){
  51. case '\'': /* skip quoted string */
  52. cp = squote(cp+1); /* n must = 1 */
  53. if(!cp)
  54. return 0;
  55. break;
  56. case '$':
  57. if(*(cp+1) == '{')
  58. vargen = 1;
  59. break;
  60. case '}':
  61. if(vargen)
  62. vargen = 0;
  63. else if(utfrune(pat, r))
  64. return cp;
  65. break;
  66. default:
  67. if(vargen == 0 && utfrune(pat, r))
  68. return cp;
  69. break;
  70. }
  71. cp += n;
  72. }
  73. if(vargen){
  74. SYNERR(-1);
  75. fprint(2, "missing closing } in pattern generator\n");
  76. }
  77. return 0;
  78. }
  79. /*
  80. * extract an escaped token. Possible escape chars are single-quote,
  81. * double-quote,and backslash. Only the first is valid for rc. the
  82. * others are just inserted into the receiving buffer.
  83. */
  84. char*
  85. expandquote(char *s, Rune r, Bufblock *b)
  86. {
  87. if (r != '\'') {
  88. rinsert(b, r);
  89. return s;
  90. }
  91. while(*s){
  92. s += chartorune(&r, s);
  93. if(r == '\'') {
  94. if(*s == '\'')
  95. s++;
  96. else
  97. return s;
  98. }
  99. rinsert(b, r);
  100. }
  101. return 0;
  102. }
  103. /*
  104. * Input an escaped token. Possible escape chars are single-quote,
  105. * double-quote and backslash. Only the first is a valid escape for
  106. * rc; the others are just inserted into the receiving buffer.
  107. */
  108. int
  109. escapetoken(Biobuf *bp, Bufblock *buf, int preserve, int esc)
  110. {
  111. int c, line;
  112. if(esc != '\'')
  113. return 1;
  114. line = mkinline;
  115. while((c = nextrune(bp, 0)) > 0){
  116. if(c == '\''){
  117. if(preserve)
  118. rinsert(buf, c);
  119. c = Bgetrune(bp);
  120. if (c < 0)
  121. break;
  122. if(c != '\''){
  123. Bungetrune(bp);
  124. return 1;
  125. }
  126. }
  127. rinsert(buf, c);
  128. }
  129. SYNERR(line); fprint(2, "missing closing %c\n", esc);
  130. return 0;
  131. }
  132. /*
  133. * copy a single-quoted string; s points to char after opening quote
  134. */
  135. static char *
  136. copysingle(char *s, Bufblock *buf)
  137. {
  138. Rune r;
  139. while(*s){
  140. s += chartorune(&r, s);
  141. rinsert(buf, r);
  142. if(r == '\'')
  143. break;
  144. }
  145. return s;
  146. }
  147. /*
  148. * check for quoted strings. backquotes are handled here; single quotes above.
  149. * s points to char after opening quote, q.
  150. */
  151. char *
  152. copyq(char *s, Rune q, Bufblock *buf)
  153. {
  154. if(q == '\'') /* copy quoted string */
  155. return copysingle(s, buf);
  156. if(q != '`') /* not quoted */
  157. return s;
  158. while(*s){ /* copy backquoted string */
  159. s += chartorune(&q, s);
  160. rinsert(buf, q);
  161. if(q == '}')
  162. break;
  163. if(q == '\'')
  164. s = copysingle(s, buf); /* copy quoted string */
  165. }
  166. return s;
  167. }