glob.c 4.4 KB

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