7 * delete all the GLOB marks from s, in place
17 int globcmp(const void *s, const void *t)
19 return strcmp(*(char**)s, *(char**)t);
21 void globsort(word *left, word *right)
26 for(a=left;a!=right;a=a->next) n++;
27 list=(char **)emalloc(n*sizeof(char *));
28 for(a=left,n=0;a!=right;a=a->next,n++) list[n]=a->word;
29 qsort((char *)list, n, sizeof(char *), globcmp);
30 for(a=left,n=0;a!=right;a=a->next,n++) a->word=list[n];
34 * Push names prefixed by globname and suffixed by a match of p onto the astack.
35 * namep points to the end of the prefix in globname.
37 void globdir(char *p, char *namep)
41 /* scan the pattern looking for a component with a metacharacter in it */
43 globv=newword(globname, globv);
57 /* If we ran out of pattern, append the name if accessible */
60 if(access(globname, 0)==0)
61 globv=newword(globname, globv);
64 /* read the directory and recur for any entry that matches */
66 if((f=Opendir(globname[0]?globname:"."))<0) return;
67 while(*newp!='/' && *newp!='\0') newp++;
68 while(Readdir(f, namep)){
69 if(matchfn(namep, p)){
77 * Push all file names matched by p on the current thread's stack.
78 * If there are no matches, the list consists of p.
83 int globlen=Globsize(p);
86 globv=newword(p, globv);
89 globname=emalloc(globlen);
95 globv=newword(p, globv);
98 globsort(globv, svglobv);
101 * Do p and q point at equal utf codes
103 int equtf(char *p, char *q){
105 if(twobyte(*p)) return p[1]==q[1];
107 if(p[1]!=q[1]) return 0;
108 if(p[1]=='\0') return 1; /* broken code at end of string! */
114 * Return a pointer to the next utf code in the string,
115 * not jumping past nuls in broken utf codes!
117 char *nextutf(char *p){
118 if(twobyte(*p)) return p[1]=='\0'?p+1:p+2;
119 if(threebyte(*p)) return p[1]=='\0'?p+1:p[2]=='\0'?p+2:p+3;
123 * Convert the utf code at *p to a unicode value
125 int unicode(char *p){
127 if(twobyte(u)) return ((u&0x1f)<<6)|(p[1]&0x3f);
128 if(threebyte(u)) return (u<<12)|((p[1]&0x3f)<<6)|(p[2]&0x3f);
132 * Does the string s match the pattern p
133 * . and .. are only matched by patterns starting with .
134 * * matches any sequence of characters
135 * ? matches any single character
136 * [...] matches the enclosed list of characters
138 int matchfn(char *s, char *p)
140 if(s[0]=='.' && (s[1]=='\0' || s[1]=='.' && s[2]=='\0') && p[0]!='.')
142 return match(s, p, '/');
144 int match(char *s, char *p, int stop)
146 int compl, hit, lo, hi, t, c;
147 for(;*p!=stop && *p!='\0';s=nextutf(s),p=nextutf(p)){
149 if(!equtf(p, s)) return 0;
153 if(*s!=GLOB) return 0;
157 if(match(s, nextutf(p), stop)) return 1;
163 if(*s=='\0') return 0;
166 if(*s=='\0') return 0;
173 if(*p=='\0') return 0; /* syntax error */
179 if(*p=='\0') return 0; /* syntax error */
182 if(hi<lo){ t=lo; lo=hi; hi=t; }
184 if(lo<=c && c<=hi) hit=1;
193 void globlist1(word *gl)
203 globlist1(runq->argv->words);
207 for(a=globv;a->next;a=a->next);
208 a->next=runq->argv->words;
209 runq->argv->words=globv;