7 * delete all the GLOB marks from s, in place
23 globcmp(const void *s, const void *t)
25 return strcmp(*(char**)s, *(char**)t);
29 globsort(word *left, word *right)
34 for(a = left;a!=right;a = a->next) n++;
35 list = (char **)emalloc(n*sizeof(char *));
36 for(a = left,n = 0;a!=right;a = a->next,n++) list[n] = a->word;
37 qsort((void *)list, n, sizeof(void *), globcmp);
38 for(a = left,n = 0;a!=right;a = a->next,n++) a->word = list[n];
42 * Push names prefixed by globname and suffixed by a match of p onto the astack.
43 * namep points to the end of the prefix in globname.
47 globdir(uchar *p, uchar *namep)
51 /* scan the pattern looking for a component with a metacharacter in it */
53 globv = newword(globname, globv);
67 /* If we ran out of pattern, append the name if accessible */
70 if(access(globname, 0)==0)
71 globv = newword(globname, globv);
74 /* read the directory and recur for any entry that matches */
76 if((f = Opendir(globname[0]?globname:"."))<0) return;
77 while(*newp!='/' && *newp!='\0') newp++;
78 while(Readdir(f, namep, *newp=='/')){
79 if(matchfn(namep, p)){
80 for(t = namep;*t;t++);
87 * Push all file names matched by p on the current thread's stack.
88 * If there are no matches, the list consists of p.
95 word *svglobv = globv;
96 int globlen = Globsize(ap);
100 globv = newword((char *)p, globv);
103 globname = emalloc(globlen);
105 globdir(p, (uchar *)globname);
109 globv = newword((char *)p, globv);
112 globsort(globv, svglobv);
115 * Do p and q point at equal utf codes
119 equtf(uchar *p, uchar *q)
123 if(twobyte(*p)) return p[1]==q[1];
128 return 1; /* broken code at end of string! */
134 * Return a pointer to the next utf code in the string,
135 * not jumping past nuls in broken utf codes!
141 if(twobyte(*p)) return p[1]=='\0'?p+1:p+2;
142 if(threebyte(*p)) return p[1]=='\0'?p+1:p[2]=='\0'?p+2:p+3;
146 * Convert the utf code at *p to a unicode value
155 return ((u&0x1f)<<6)|(p[1]&0x3f);
157 return (u<<12)|((p[1]&0x3f)<<6)|(p[2]&0x3f);
161 * Does the string s match the pattern p
162 * . and .. are only matched by patterns starting with .
163 * * matches any sequence of characters
164 * ? matches any single character
165 * [...] matches the enclosed list of characters
169 matchfn(void *as, void *ap)
171 uchar *s = as, *p = ap;
173 if(s[0]=='.' && (s[1]=='\0' || s[1]=='.' && s[2]=='\0') && p[0]!='.')
175 return match(s, p, '/');
179 match(void *as, void *ap, int stop)
181 int compl, hit, lo, hi, t, c;
182 uchar *s = as, *p = ap;
184 for(; *p!=stop && *p!='\0'; s = nextutf(s), p = nextutf(p)){
186 if(!equtf(p, s)) return 0;
195 if(match(s, nextutf(p), stop)) return 1;
216 return 0; /* syntax error */
224 return 0; /* syntax error */
227 if(hi<lo){ t = lo; lo = hi; hi = t; }
256 globlist1(runq->argv->words);
260 for(a = globv;a->next;a = a->next);
261 a->next = runq->argv->words;
262 runq->argv->words = globv;