glob.c 4.3 KB

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