glob.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "rc.h"
  10. #include "exec.h"
  11. #include "fns.h"
  12. char *globname;
  13. struct word *globv;
  14. /*
  15. * delete all the GLOB marks from s, in place
  16. */
  17. void
  18. deglob(void *as)
  19. {
  20. char *s = as;
  21. char *t = s;
  22. do{
  23. if(*t==GLOB)
  24. t++;
  25. *s++=*t;
  26. }while(*t++);
  27. }
  28. int
  29. globcmp(const void *s, const void *t)
  30. {
  31. return strcmp(*(char**)s, *(char**)t);
  32. }
  33. void
  34. globsort(word *left, word *right)
  35. {
  36. char **list;
  37. word *a;
  38. int n = 0;
  39. for(a = left;a!=right;a = a->next) n++;
  40. list = (char **)emalloc(n*sizeof(char *));
  41. for(a = left,n = 0;a!=right;a = a->next,n++) list[n] = a->word;
  42. qsort((void *)list, n, sizeof(void *), globcmp);
  43. for(a = left,n = 0;a!=right;a = a->next,n++) a->word = list[n];
  44. efree((char *)list);
  45. }
  46. /*
  47. * Push names prefixed by globname and suffixed by a match of p onto the astack.
  48. * namep points to the end of the prefix in globname.
  49. */
  50. void
  51. globdir(uint8_t *p, uint8_t *namep)
  52. {
  53. uint8_t *t, *newp;
  54. int f;
  55. /* scan the pattern looking for a component with a metacharacter in it */
  56. if(*p=='\0'){
  57. globv = newword(globname, globv);
  58. return;
  59. }
  60. t = namep;
  61. newp = p;
  62. while(*newp){
  63. if(*newp==GLOB)
  64. break;
  65. *t=*newp++;
  66. if(*t++=='/'){
  67. namep = t;
  68. p = newp;
  69. }
  70. }
  71. /* If we ran out of pattern, append the name if accessible */
  72. if(*newp=='\0'){
  73. *t='\0';
  74. if(access(globname, 0)==0)
  75. globv = newword(globname, globv);
  76. return;
  77. }
  78. /* read the directory and recur for any entry that matches */
  79. *namep='\0';
  80. if((f = Opendir(globname[0]?globname:"."))<0) return;
  81. while(*newp!='/' && *newp!='\0') newp++;
  82. while(Readdir(f, namep, *newp=='/')){
  83. if(matchfn(namep, p)){
  84. for(t = namep;*t;t++);
  85. globdir(newp, t);
  86. }
  87. }
  88. Closedir(f);
  89. }
  90. /*
  91. * Push all file names matched by p on the current thread's stack.
  92. * If there are no matches, the list consists of p.
  93. */
  94. void
  95. glob(void *ap)
  96. {
  97. uint8_t *p = ap;
  98. word *svglobv = globv;
  99. int globlen = Globsize(ap);
  100. if(!globlen){
  101. deglob(p);
  102. globv = newword((char *)p, globv);
  103. return;
  104. }
  105. globname = emalloc(globlen);
  106. globname[0]='\0';
  107. globdir(p, (uint8_t *)globname);
  108. efree(globname);
  109. if(svglobv==globv){
  110. deglob(p);
  111. globv = newword((char *)p, globv);
  112. }
  113. else
  114. globsort(globv, svglobv);
  115. }
  116. /*
  117. * Do p and q point at equal utf codes
  118. */
  119. int
  120. equtf(uint8_t *p, uint8_t *q)
  121. {
  122. Rune pr, qr;
  123. if(*p!=*q)
  124. return 0;
  125. chartorune(&pr, (char*)p);
  126. chartorune(&qr, (char*)q);
  127. return pr == qr;
  128. }
  129. /*
  130. * Return a pointer to the next utf code in the string,
  131. * not jumping past nuls in broken utf codes!
  132. */
  133. uint8_t*
  134. nextutf(uint8_t *p)
  135. {
  136. Rune dummy;
  137. return p + chartorune(&dummy, (char*)p);
  138. }
  139. /*
  140. * Convert the utf code at *p to a unicode value
  141. */
  142. int
  143. unicode(uint8_t *p)
  144. {
  145. Rune r;
  146. chartorune(&r, (char*)p);
  147. return r;
  148. }
  149. /*
  150. * Does the string s match the pattern p
  151. * . and .. are only matched by patterns starting with .
  152. * * matches any sequence of characters
  153. * ? matches any single character
  154. * [...] matches the enclosed list of characters
  155. */
  156. int
  157. matchfn(void *as, void *ap)
  158. {
  159. uint8_t *s = as, *p = ap;
  160. if(s[0]=='.' && (s[1]=='\0' || s[1]=='.' && s[2]=='\0') && p[0]!='.')
  161. return 0;
  162. return match(s, p, '/');
  163. }
  164. int
  165. match(void *as, void *ap, int stop)
  166. {
  167. int compl, hit, lo, hi, t, c;
  168. uint8_t *s = as, *p = ap;
  169. for(; *p!=stop && *p!='\0'; s = nextutf(s), p = nextutf(p)){
  170. if(*p!=GLOB){
  171. if(!equtf(p, s)) return 0;
  172. }
  173. else switch(*++p){
  174. case GLOB:
  175. if(*s!=GLOB)
  176. return 0;
  177. break;
  178. case '*':
  179. for(;;){
  180. if(match(s, nextutf(p), stop)) return 1;
  181. if(!*s)
  182. break;
  183. s = nextutf(s);
  184. }
  185. return 0;
  186. case '?':
  187. if(*s=='\0')
  188. return 0;
  189. break;
  190. case '[':
  191. if(*s=='\0')
  192. return 0;
  193. c = unicode(s);
  194. p++;
  195. compl=*p=='~';
  196. if(compl)
  197. p++;
  198. hit = 0;
  199. while(*p!=']'){
  200. if(*p=='\0')
  201. return 0; /* syntax error */
  202. lo = unicode(p);
  203. p = nextutf(p);
  204. if(*p!='-')
  205. hi = lo;
  206. else{
  207. p++;
  208. if(*p=='\0')
  209. return 0; /* syntax error */
  210. hi = unicode(p);
  211. p = nextutf(p);
  212. if(hi<lo){ t = lo; lo = hi; hi = t; }
  213. }
  214. if(lo<=c && c<=hi)
  215. hit = 1;
  216. }
  217. if(compl)
  218. hit=!hit;
  219. if(!hit)
  220. return 0;
  221. break;
  222. }
  223. }
  224. return *s=='\0';
  225. }
  226. void
  227. globlist1(word *gl)
  228. {
  229. if(gl){
  230. globlist1(gl->next);
  231. glob(gl->word);
  232. }
  233. }
  234. void
  235. globlist(void)
  236. {
  237. word *a;
  238. globv = 0;
  239. globlist1(runq->argv->words);
  240. poplist();
  241. pushlist();
  242. if(globv){
  243. for(a = globv;a->next;a = a->next);
  244. a->next = runq->argv->words;
  245. runq->argv->words = globv;
  246. }
  247. }