glob.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. if(*p!=*q)
  115. return 0;
  116. if(twobyte(*p)) return p[1]==q[1];
  117. if(threebyte(*p)){
  118. if(p[1]!=q[1])
  119. return 0;
  120. if(p[1]=='\0')
  121. return 1; /* broken code at end of string! */
  122. return p[2]==q[2];
  123. }
  124. return 1;
  125. }
  126. /*
  127. * Return a pointer to the next utf code in the string,
  128. * not jumping past nuls in broken utf codes!
  129. */
  130. uchar*
  131. nextutf(uchar *p)
  132. {
  133. if(twobyte(*p)) return p[1]=='\0'?p+1:p+2;
  134. if(threebyte(*p)) return p[1]=='\0'?p+1:p[2]=='\0'?p+2:p+3;
  135. return p+1;
  136. }
  137. /*
  138. * Convert the utf code at *p to a unicode value
  139. */
  140. int
  141. unicode(uchar *p)
  142. {
  143. int u = *p;
  144. if(twobyte(u))
  145. return ((u&0x1f)<<6)|(p[1]&0x3f);
  146. if(threebyte(u))
  147. return (u<<12)|((p[1]&0x3f)<<6)|(p[2]&0x3f);
  148. return u;
  149. }
  150. /*
  151. * Does the string s match the pattern p
  152. * . and .. are only matched by patterns starting with .
  153. * * matches any sequence of characters
  154. * ? matches any single character
  155. * [...] matches the enclosed list of characters
  156. */
  157. int
  158. matchfn(void *as, void *ap)
  159. {
  160. uchar *s = as, *p = ap;
  161. if(s[0]=='.' && (s[1]=='\0' || s[1]=='.' && s[2]=='\0') && p[0]!='.')
  162. return 0;
  163. return match(s, p, '/');
  164. }
  165. int
  166. match(void *as, void *ap, int stop)
  167. {
  168. int compl, hit, lo, hi, t, c;
  169. uchar *s = as, *p = ap;
  170. for(; *p!=stop && *p!='\0'; s = nextutf(s), p = nextutf(p)){
  171. if(*p!=GLOB){
  172. if(!equtf(p, s)) return 0;
  173. }
  174. else switch(*++p){
  175. case GLOB:
  176. if(*s!=GLOB)
  177. return 0;
  178. break;
  179. case '*':
  180. for(;;){
  181. if(match(s, nextutf(p), stop)) return 1;
  182. if(!*s)
  183. break;
  184. s = nextutf(s);
  185. }
  186. return 0;
  187. case '?':
  188. if(*s=='\0')
  189. return 0;
  190. break;
  191. case '[':
  192. if(*s=='\0')
  193. return 0;
  194. c = unicode(s);
  195. p++;
  196. compl=*p=='~';
  197. if(compl)
  198. p++;
  199. hit = 0;
  200. while(*p!=']'){
  201. if(*p=='\0')
  202. return 0; /* syntax error */
  203. lo = unicode(p);
  204. p = nextutf(p);
  205. if(*p!='-')
  206. hi = lo;
  207. else{
  208. p++;
  209. if(*p=='\0')
  210. return 0; /* syntax error */
  211. hi = unicode(p);
  212. p = nextutf(p);
  213. if(hi<lo){ t = lo; lo = hi; hi = t; }
  214. }
  215. if(lo<=c && c<=hi)
  216. hit = 1;
  217. }
  218. if(compl)
  219. hit=!hit;
  220. if(!hit)
  221. return 0;
  222. break;
  223. }
  224. }
  225. return *s=='\0';
  226. }
  227. void
  228. globlist1(word *gl)
  229. {
  230. if(gl){
  231. globlist1(gl->next);
  232. glob(gl->word);
  233. }
  234. }
  235. void
  236. globlist(void)
  237. {
  238. word *a;
  239. globv = 0;
  240. globlist1(runq->argv->words);
  241. poplist();
  242. pushlist();
  243. if(globv){
  244. for(a = globv;a->next;a = a->next);
  245. a->next = runq->argv->words;
  246. runq->argv->words = globv;
  247. }
  248. }