varsub.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "mk.h"
  2. static Word *subsub(Word*, char*, char*);
  3. static Word *expandvar(char**);
  4. static Bufblock *varname(char**);
  5. static Word *extractpat(char*, char**, char*, char*);
  6. static int submatch(char*, Word*, Word*, int*, char**);
  7. static Word *varmatch(char *);
  8. Word *
  9. varsub(char **s)
  10. {
  11. Bufblock *b;
  12. Word *w;
  13. if(**s == '{') /* either ${name} or ${name: A%B==C%D}*/
  14. return expandvar(s);
  15. b = varname(s);
  16. if(b == 0)
  17. return 0;
  18. w = varmatch(b->start);
  19. freebuf(b);
  20. return w;
  21. }
  22. /*
  23. * extract a variable name
  24. */
  25. static Bufblock*
  26. varname(char **s)
  27. {
  28. Bufblock *b;
  29. char *cp;
  30. Rune r;
  31. int n;
  32. b = newbuf();
  33. cp = *s;
  34. for(;;){
  35. n = chartorune(&r, cp);
  36. if (!WORDCHR(r))
  37. break;
  38. rinsert(b, r);
  39. cp += n;
  40. }
  41. if (b->current == b->start){
  42. SYNERR(-1);
  43. fprint(2, "missing variable name <%s>\n", *s);
  44. freebuf(b);
  45. return 0;
  46. }
  47. *s = cp;
  48. insert(b, 0);
  49. return b;
  50. }
  51. static Word*
  52. varmatch(char *name)
  53. {
  54. Word *w;
  55. Symtab *sym;
  56. sym = symlook(name, S_VAR, 0);
  57. if(sym){
  58. /* check for at least one non-NULL value */
  59. for (w = sym->u.ptr; w; w = w->next)
  60. if(w->s && *w->s)
  61. return wdup(w);
  62. }
  63. return 0;
  64. }
  65. static Word*
  66. expandvar(char **s)
  67. {
  68. Word *w;
  69. Bufblock *buf;
  70. Symtab *sym;
  71. char *cp, *begin, *end;
  72. begin = *s;
  73. (*s)++; /* skip the '{' */
  74. buf = varname(s);
  75. if (buf == 0)
  76. return 0;
  77. cp = *s;
  78. if (*cp == '}') { /* ${name} variant*/
  79. (*s)++; /* skip the '}' */
  80. w = varmatch(buf->start);
  81. freebuf(buf);
  82. return w;
  83. }
  84. if (*cp != ':') {
  85. SYNERR(-1);
  86. fprint(2, "bad variable name <%s>\n", buf->start);
  87. freebuf(buf);
  88. return 0;
  89. }
  90. cp++;
  91. end = charin(cp , "}");
  92. if(end == 0){
  93. SYNERR(-1);
  94. fprint(2, "missing '}': %s\n", begin);
  95. Exit();
  96. }
  97. *end = 0;
  98. *s = end+1;
  99. sym = symlook(buf->start, S_VAR, 0);
  100. if(sym == 0 || sym->u.value == 0)
  101. w = newword(buf->start);
  102. else
  103. w = subsub(sym->u.ptr, cp, end);
  104. freebuf(buf);
  105. return w;
  106. }
  107. static Word*
  108. extractpat(char *s, char **r, char *term, char *end)
  109. {
  110. int save;
  111. char *cp;
  112. Word *w;
  113. cp = charin(s, term);
  114. if(cp){
  115. *r = cp;
  116. if(cp == s)
  117. return 0;
  118. save = *cp;
  119. *cp = 0;
  120. w = stow(s);
  121. *cp = save;
  122. } else {
  123. *r = end;
  124. w = stow(s);
  125. }
  126. return w;
  127. }
  128. static Word*
  129. subsub(Word *v, char *s, char *end)
  130. {
  131. int nmid;
  132. Word *head, *tail, *w, *h;
  133. Word *a, *b, *c, *d;
  134. Bufblock *buf;
  135. char *cp, *enda;
  136. a = extractpat(s, &cp, "=%&", end);
  137. b = c = d = 0;
  138. if(PERCENT(*cp))
  139. b = extractpat(cp+1, &cp, "=", end);
  140. if(*cp == '=')
  141. c = extractpat(cp+1, &cp, "&%", end);
  142. if(PERCENT(*cp))
  143. d = stow(cp+1);
  144. else if(*cp)
  145. d = stow(cp);
  146. head = tail = 0;
  147. buf = newbuf();
  148. for(; v; v = v->next){
  149. h = w = 0;
  150. if(submatch(v->s, a, b, &nmid, &enda)){
  151. /* enda points to end of A match in source;
  152. * nmid = number of chars between end of A and start of B
  153. */
  154. if(c){
  155. h = w = wdup(c);
  156. while(w->next)
  157. w = w->next;
  158. }
  159. if(PERCENT(*cp) && nmid > 0){
  160. if(w){
  161. bufcpy(buf, w->s, strlen(w->s));
  162. bufcpy(buf, enda, nmid);
  163. insert(buf, 0);
  164. free(w->s);
  165. w->s = strdup(buf->start);
  166. } else {
  167. bufcpy(buf, enda, nmid);
  168. insert(buf, 0);
  169. h = w = newword(buf->start);
  170. }
  171. buf->current = buf->start;
  172. }
  173. if(d && *d->s){
  174. if(w){
  175. bufcpy(buf, w->s, strlen(w->s));
  176. bufcpy(buf, d->s, strlen(d->s));
  177. insert(buf, 0);
  178. free(w->s);
  179. w->s = strdup(buf->start);
  180. w->next = wdup(d->next);
  181. while(w->next)
  182. w = w->next;
  183. buf->current = buf->start;
  184. } else
  185. h = w = wdup(d);
  186. }
  187. }
  188. if(w == 0)
  189. h = w = newword(v->s);
  190. if(head == 0)
  191. head = h;
  192. else
  193. tail->next = h;
  194. tail = w;
  195. }
  196. freebuf(buf);
  197. delword(a);
  198. delword(b);
  199. delword(c);
  200. delword(d);
  201. return head;
  202. }
  203. static int
  204. submatch(char *s, Word *a, Word *b, int *nmid, char **enda)
  205. {
  206. Word *w;
  207. int n;
  208. char *end;
  209. n = 0;
  210. for(w = a; w; w = w->next){
  211. n = strlen(w->s);
  212. if(strncmp(s, w->s, n) == 0)
  213. break;
  214. }
  215. if(a && w == 0) /* a == NULL matches everything*/
  216. return 0;
  217. *enda = s+n; /* pointer to end a A part match */
  218. *nmid = strlen(s)-n; /* size of remainder of source */
  219. end = *enda+*nmid;
  220. for(w = b; w; w = w->next){
  221. n = strlen(w->s);
  222. if(strcmp(w->s, end-n) == 0){
  223. *nmid -= n;
  224. break;
  225. }
  226. }
  227. if(b && w == 0) /* b == NULL matches everything */
  228. return 0;
  229. return 1;
  230. }