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! */
131 * Return a pointer to the next utf code in the string,
132 * not jumping past nuls in broken utf codes!
138 if(twobyte(*p)) return p[1]=='\0'?p+1:p+2;
139 if(threebyte(*p)) return p[1]=='\0'?p+1:p[2]=='\0'?p+2:p+3;
143 * Convert the utf code at *p to a unicode value
150 if(twobyte(u)) return ((u&0x1f)<<6)|(p[1]&0x3f);
151 if(threebyte(u)) return (u<<12)|((p[1]&0x3f)<<6)|(p[2]&0x3f);
155 * Does the string s match the pattern p
156 * . and .. are only matched by patterns starting with .
157 * * matches any sequence of characters
158 * ? matches any single character
159 * [...] matches the enclosed list of characters
163 matchfn(char *s, char *p)
165 if(s[0]=='.' && (s[1]=='\0' || s[1]=='.' && s[2]=='\0') && p[0]!='.')
167 return match(s, p, '/');
171 match(char *s, char *p, int stop)
173 int compl, hit, lo, hi, t, c;
174 for(;*p!=stop && *p!='\0';s = nextutf(s),p = nextutf(p)){
176 if(!equtf(p, s)) return 0;
185 if(match(s, nextutf(p), stop)) return 1;
206 return 0; /* syntax error */
214 return 0; /* syntax error */
217 if(hi<lo){ t = lo; lo = hi; hi = t; }
246 globlist1(runq->argv->words);
250 for(a = globv;a->next;a = a->next);
251 a->next = runq->argv->words;
252 runq->argv->words = globv;