7 * delete all the GLOB marks from s, in place
22 globcmp(const void *s, const void *t)
24 return strcmp(*(char**)s, *(char**)t);
28 globsort(word *left, word *right)
33 for(a = left;a!=right;a = a->next) n++;
34 list = (char **)emalloc(n*sizeof(char *));
35 for(a = left,n = 0;a!=right;a = a->next,n++) list[n] = a->word;
36 qsort((void *)list, n, sizeof(void *), globcmp);
37 for(a = left,n = 0;a!=right;a = a->next,n++) a->word = list[n];
41 * Push names prefixed by globname and suffixed by a match of p onto the astack.
42 * namep points to the end of the prefix in globname.
46 globdir(char *p, char *namep)
50 /* scan the pattern looking for a component with a metacharacter in it */
52 globv = newword(globname, globv);
66 /* If we ran out of pattern, append the name if accessible */
69 if(access(globname, 0)==0)
70 globv = newword(globname, globv);
73 /* read the directory and recur for any entry that matches */
75 if((f = Opendir(globname[0]?globname:"."))<0) return;
76 while(*newp!='/' && *newp!='\0') newp++;
77 while(Readdir(f, namep, *newp=='/')){
78 if(matchfn(namep, p)){
79 for(t = namep;*t;t++);
86 * Push all file names matched by p on the current thread's stack.
87 * If there are no matches, the list consists of p.
93 word *svglobv = globv;
94 int globlen = Globsize(p);
97 globv = newword(p, globv);
100 globname = emalloc(globlen);
102 globdir(p, globname);
106 globv = newword(p, globv);
109 globsort(globv, svglobv);
112 * Do p and q point at equal utf codes
116 equtf(char *p, char *q)
120 if(twobyte(*p)) return p[1]==q[1];
125 return 1; /* broken code at end of string! */
142 * Return a pointer to the next utf code in the string,
143 * not jumping past nuls in broken utf codes!
149 if(twobyte(*p)) return p[1]=='\0'?p+1:p+2;
150 if(threebyte(*p)) return p[1]=='\0'?p+1:p[2]=='\0'?p+2:p+3;
151 if(fourbyte(*p)) return p[1]=='\0'?p+1:p[2]=='\0'?p+2:p[3]=='\0'?p+3:p+4;
155 * Convert the utf code at *p to a unicode value
162 if(twobyte(u)) return ((u&0x1f)<<6)|(p[1]&0x3f);
163 if(threebyte(u)) return (u<<12)|((p[1]&0x3f)<<6)|(p[2]&0x3f);
164 if(fourbyte(u)) return (u<<18)|((p[1]&0x3f)<<12)|((p[2]&0x3f)<<6)|(p[3]&0x3f);
168 * Does the string s match the pattern p
169 * . and .. are only matched by patterns starting with .
170 * * matches any sequence of characters
171 * ? matches any single character
172 * [...] matches the enclosed list of characters
176 matchfn(char *s, char *p)
178 if(s[0]=='.' && (s[1]=='\0' || s[1]=='.' && s[2]=='\0') && p[0]!='.')
180 return match(s, p, '/');
184 match(char *s, char *p, int stop)
186 int compl, hit, lo, hi, t, c;
187 for(;*p!=stop && *p!='\0';s = nextutf(s),p = nextutf(p)){
189 if(!equtf(p, s)) return 0;
198 if(match(s, nextutf(p), stop)) return 1;
219 return 0; /* syntax error */
227 return 0; /* syntax error */
230 if(hi<lo){ t = lo; lo = hi; hi = t; }
259 globlist1(runq->argv->words);
263 for(a = globv;a->next;a = a->next);
264 a->next = runq->argv->words;
265 runq->argv->words = globv;