This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
DosGlob: Don’t use the magic 2nd arg to glob
[perl5.git] / ext / File-Glob / Glob.xs
CommitLineData
8e9c4baf
NC
1#define PERL_NO_GET_CONTEXT
2
72b16652
GS
3#include "EXTERN.h"
4#include "perl.h"
5#include "XSUB.h"
6
7#include "bsd_glob.h"
8
df3728a2 9#define MY_CXT_KEY "File::Glob::_guts" XS_VERSION
89ca4ac7
JH
10
11typedef struct {
12 int x_GLOB_ERROR;
1bb8785a 13 HV * x_GLOB_ENTRIES;
89ca4ac7
JH
14} my_cxt_t;
15
16START_MY_CXT
17
18#define GLOB_ERROR (MY_CXT.x_GLOB_ERROR)
72b16652 19
1cb0fb50 20#include "const-c.inc"
72b16652
GS
21
22#ifdef WIN32
23#define errfunc NULL
24#else
f681a178 25static int
72b16652 26errfunc(const char *foo, int bar) {
c33e8be1 27 PERL_UNUSED_ARG(foo);
5f18268b 28 return !(bar == EACCES || bar == ENOENT || bar == ENOTDIR);
72b16652
GS
29}
30#endif
31
1bb8785a
FC
32static void
33doglob(pTHX_ const char *pattern, int flags)
34{
35 dSP;
36 glob_t pglob;
37 int i;
38 int retval;
39 SV *tmp;
40 {
41 dMY_CXT;
42
43 /* call glob */
44 memset(&pglob, 0, sizeof(glob_t));
45 retval = bsd_glob(pattern, flags, errfunc, &pglob);
46 GLOB_ERROR = retval;
47
48 /* return any matches found */
49 EXTEND(sp, pglob.gl_pathc);
50 for (i = 0; i < pglob.gl_pathc; i++) {
51 /* printf("# bsd_glob: %s\n", pglob.gl_pathv[i]); */
52 tmp = newSVpvn_flags(pglob.gl_pathv[i], strlen(pglob.gl_pathv[i]),
53 SVs_TEMP);
54 TAINT;
55 SvTAINT(tmp);
56 PUSHs(tmp);
57 }
58 PUTBACK;
59
60 bsd_globfree(&pglob);
61 }
62}
63
1bb8785a 64static void
1d6dcc39 65iterate(pTHX_ bool(*globber)(pTHX_ AV *entries, SV *patsv))
1bb8785a
FC
66{
67 dSP;
68 dMY_CXT;
69
3ab888cf 70 SV * const cxixsv = POPs;
1bb8785a
FC
71 const char *cxixpv;
72 STRLEN cxixlen;
88b04955 73 AV *entries;
3ab888cf 74 U32 const gimme = GIMME_V;
1bb8785a 75 SV *patsv = POPs;
88b04955 76 bool on_stack = FALSE;
1bb8785a
FC
77
78 /* assume global context if not provided one */
79 SvGETMAGIC(cxixsv);
80 if (SvOK(cxixsv)) cxixpv = SvPV_nomg(cxixsv, cxixlen);
81 else cxixpv = "_G_", cxixlen = 3;
82
1bb8785a 83 if (!MY_CXT.x_GLOB_ENTRIES) MY_CXT.x_GLOB_ENTRIES = newHV();
1d6dcc39 84 entries = (AV *)*(hv_fetch(MY_CXT.x_GLOB_ENTRIES, cxixpv, cxixlen, 1));
1bb8785a
FC
85
86 /* if we're just beginning, do it all first */
1d6dcc39 87 if (SvTYPE(entries) != SVt_PVAV) {
88b04955 88 PUTBACK;
1d6dcc39 89 on_stack = globber(aTHX_ entries, patsv);
88b04955
FC
90 SPAGAIN;
91 }
92
93 /* chuck it all out, quick or slow */
88b04955
FC
94 if (gimme == G_ARRAY) {
95 if (!on_stack) {
a6636b43 96 EXTEND(SP, AvFILLp(entries)+1);
88b04955
FC
97 Copy(AvARRAY(entries), SP+1, AvFILLp(entries)+1, SV *);
98 SP += AvFILLp(entries)+1;
99 }
88b04955
FC
100 /* No G_DISCARD here! It will free the stack items. */
101 hv_delete(MY_CXT.x_GLOB_ENTRIES, cxixpv, cxixlen, 0);
102 }
103 else {
104 if (AvFILLp(entries) + 1) {
88b04955
FC
105 mPUSHs(av_shift(entries));
106 }
107 else {
108 /* return undef for EOL */
88b04955
FC
109 hv_delete(MY_CXT.x_GLOB_ENTRIES, cxixpv, cxixlen, G_DISCARD);
110 PUSHs(&PL_sv_undef);
111 }
112 }
113 PUTBACK;
114}
115
7a26772a
FC
116/* returns true if the items are on the stack already, but only in
117 list context */
88b04955 118static bool
1d6dcc39 119csh_glob(pTHX_ AV *entries, SV *patsv)
88b04955
FC
120{
121 dSP;
1bb8785a
FC
122 const char *pat;
123 AV *patav = NULL;
124 const char *patend;
88b04955 125 const char *s = NULL;
1bb8785a
FC
126 const char *piece = NULL;
127 SV *word = NULL;
128 int const flags =
129 (int)SvIV(get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD));
130 bool is_utf8;
88b04955
FC
131 STRLEN len;
132 U32 const gimme = GIMME_V;
1bb8785a
FC
133
134 /* glob without args defaults to $_ */
135 SvGETMAGIC(patsv);
136 if (
137 !SvOK(patsv)
138 && (patsv = DEFSV, SvGETMAGIC(patsv), !SvOK(patsv))
139 )
140 pat = "", len = 0, is_utf8 = 0;
141 else pat = SvPV_nomg(patsv,len), is_utf8 = !!SvUTF8(patsv);
142 patend = pat + len;
143
144 /* extract patterns */
1bb8785a
FC
145 s = pat-1;
146 while (++s < patend) {
147 switch (*s) {
148 case '\'':
149 case '"' :
150 {
151 bool found = FALSE;
e1621fc9 152 const char quote = *s;
1bb8785a
FC
153 if (!word) {
154 word = newSVpvs("");
155 if (is_utf8) SvUTF8_on(word);
156 }
157 if (piece) sv_catpvn(word, piece, s-piece);
158 piece = s+1;
f89f9e93 159 while (++s < patend)
e1621fc9
FC
160 if (*s == '\\') {
161 s++;
162 /* If the backslash is here to escape a quote,
163 obliterate it. */
164 if (s < patend && *s == quote)
165 sv_catpvn(word, piece, s-piece-1), piece = s;
166 }
167 else if (*s == quote) {
1bb8785a
FC
168 sv_catpvn(word, piece, s-piece);
169 piece = NULL;
170 found = TRUE;
171 break;
172 }
173 if (!found) { /* unmatched quote */
174 /* Give up on tokenisation and treat the whole string
175 as a single token, but with whitespace stripped. */
176 piece = pat;
177 while (isSPACE(*pat)) pat++;
178 while (isSPACE(*(patend-1))) patend--;
179 /* bsd_glob expects a trailing null, but we cannot mod-
180 ify the original */
181 if (patend < SvEND(patsv)) {
182 if (word) sv_setpvn(word, pat, patend-pat);
183 else
184 word = newSVpvn_flags(
185 pat, patend-pat, SVf_UTF8*is_utf8
186 );
187 piece = NULL;
188 }
189 else {
190 if (word) SvREFCNT_dec(word), word=NULL;
191 piece = pat;
192 s = patend;
193 }
194 goto end_of_parsing;
195 }
196 break;
197 }
e1621fc9
FC
198 case '\\':
199 if (!piece) piece = s;
200 s++;
201 /* If the backslash is here to escape a quote,
202 obliterate it. */
203 if (s < patend && (*s == '"' || *s == '\'')) {
204 if (!word) {
205 word = newSVpvn(piece,s-piece-1);
206 if (is_utf8) SvUTF8_on(word);
207 }
208 else sv_catpvn(word, piece, s-piece-1);
209 piece = s;
210 }
211 break;
1bb8785a
FC
212 default:
213 if (isSPACE(*s)) {
214 if (piece) {
215 if (!word) {
216 word = newSVpvn(piece,s-piece);
217 if (is_utf8) SvUTF8_on(word);
218 }
219 else sv_catpvn(word, piece, s-piece);
220 }
221 if (!word) break;
222 if (!patav) patav = (AV *)sv_2mortal((SV *)newAV());
223 av_push(patav, word);
224 word = NULL;
225 piece = NULL;
226 }
227 else if (!piece) piece = s;
228 break;
229 }
230 }
231 end_of_parsing:
232
1d6dcc39 233 assert(SvTYPE(entries) != SVt_PVAV);
1bb8785a
FC
234 sv_upgrade((SV *)entries, SVt_PVAV);
235
236 if (patav) {
237 I32 items = AvFILLp(patav) + 1;
238 SV **svp = AvARRAY(patav);
239 while (items--) {
240 PUSHMARK(SP);
241 PUTBACK;
242 doglob(aTHX_ SvPVXx(*svp++), flags);
243 SPAGAIN;
244 {
245 dMARK;
246 dORIGMARK;
247 while (++MARK <= SP)
248 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
249 SP = ORIGMARK;
250 }
251 }
252 }
253 /* piece is set at this point if there is no trailing whitespace.
254 It is the beginning of the last token or quote-delimited
255 piece thereof. word is set at this point if the last token has
256 multiple quoted pieces. */
257 if (piece || word) {
258 if (word) {
259 if (piece) sv_catpvn(word, piece, s-piece);
260 piece = SvPVX(word);
261 }
262 PUSHMARK(SP);
263 PUTBACK;
264 doglob(aTHX_ piece, flags);
265 if (word) SvREFCNT_dec(word);
266 SPAGAIN;
267 {
268 dMARK;
269 dORIGMARK;
1bb8785a 270 /* short-circuit here for a fairly common case */
88b04955 271 if (!patav && gimme == G_ARRAY) { PUTBACK; return TRUE; }
c0d74586
FC
272 while (++MARK <= SP)
273 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
1bb8785a
FC
274
275 SP = ORIGMARK;
276 }
277 }
88b04955
FC
278 PUTBACK;
279 return FALSE;
280}
1bb8785a 281
88b04955
FC
282static void
283csh_glob_iter(pTHX)
284{
285 iterate(aTHX_ csh_glob);
1bb8785a
FC
286}
287
f4cbf990
FC
288/* wrapper around doglob that can be passed to the iterator */
289static bool
1d6dcc39 290doglob_iter_wrapper(pTHX_ AV *entries, SV *patsv)
f4cbf990
FC
291{
292 dSP;
293 const char *pattern;
294 int const flags =
295 (int)SvIV(get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD));
f4cbf990
FC
296
297 SvGETMAGIC(patsv);
298 if (
299 !SvOK(patsv)
300 && (patsv = DEFSV, SvGETMAGIC(patsv), !SvOK(patsv))
301 )
302 pattern = "";
303 else pattern = SvPV_nomg_nolen(patsv);
304
305 PUSHMARK(SP);
306 PUTBACK;
307 doglob(aTHX_ pattern, flags);
308 SPAGAIN;
309 {
310 dMARK;
311 dORIGMARK;
312 if (GIMME_V == G_ARRAY) { PUTBACK; return TRUE; }
f4cbf990
FC
313 sv_upgrade((SV *)entries, SVt_PVAV);
314 while (++MARK <= SP)
315 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
316 SP = ORIGMARK;
317 }
318 return FALSE;
319}
320
72b16652
GS
321MODULE = File::Glob PACKAGE = File::Glob
322
b84cd0b1
NC
323int
324GLOB_ERROR()
325 PREINIT:
326 dMY_CXT;
327 CODE:
328 RETVAL = GLOB_ERROR;
329 OUTPUT:
330 RETVAL
331
72b16652 332void
960ddb65 333bsd_glob(pattern,...)
72b16652 334 char *pattern
72b16652 335PREINIT:
72b16652 336 int flags = 0;
72b16652
GS
337PPCODE:
338 {
339 /* allow for optional flags argument */
340 if (items > 1) {
341 flags = (int) SvIV(ST(1));
3c97495f
CB
342 /* remove unsupported flags */
343 flags &= ~(GLOB_APPEND | GLOB_DOOFFS | GLOB_ALTDIRFUNC | GLOB_MAGCHAR);
960ddb65 344 } else {
28cd8e1d 345 flags = (int) SvIV(get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD));
72b16652 346 }
1bb8785a
FC
347
348 PUTBACK;
349 doglob(aTHX_ pattern, flags);
350 SPAGAIN;
351 }
72b16652 352
1bb8785a
FC
353PROTOTYPES: DISABLE
354void
355csh_glob(...)
356PPCODE:
357 /* For backward-compatibility with the original Perl function, we sim-
358 * ply take the first two arguments, regardless of how many there are.
359 */
360 if (items >= 2) SP += 2;
361 else {
362 SP += items;
363 XPUSHs(&PL_sv_undef);
364 if (!items) XPUSHs(&PL_sv_undef);
72b16652 365 }
1bb8785a 366 PUTBACK;
88b04955 367 csh_glob_iter(aTHX);
1bb8785a 368 SPAGAIN;
72b16652 369
f4cbf990
FC
370void
371bsd_glob_override(...)
372PPCODE:
373 if (items >= 2) SP += 2;
374 else {
375 SP += items;
376 XPUSHs(&PL_sv_undef);
377 if (!items) XPUSHs(&PL_sv_undef);
378 }
379 PUTBACK;
380 iterate(aTHX_ doglob_iter_wrapper);
381 SPAGAIN;
382
28cd8e1d
NC
383BOOT:
384{
d67594ff 385#ifndef PERL_EXTERNAL_GLOB
e1fa07e3 386 /* Don't do this at home! The globhook interface is highly volatile. */
88b04955 387 PL_globhook = csh_glob_iter;
d67594ff 388#endif
bcd258b8 389}
28cd8e1d 390
bcd258b8
NC
391BOOT:
392{
28cd8e1d 393 MY_CXT_INIT;
1bb8785a
FC
394 {
395 dMY_CXT;
6a606603 396 MY_CXT.x_GLOB_ENTRIES = NULL;
1bb8785a 397 }
28cd8e1d
NC
398}
399
1cb0fb50 400INCLUDE: const-xs.inc