1 #define PERL_NO_GET_CONTEXT
9 #define MY_CXT_KEY "File::Glob::_guts" XS_VERSION
17 Perl_ophook_t x_GLOB_OLD_OPHOOK;
22 #define GLOB_ERROR (MY_CXT.x_GLOB_ERROR)
24 #include "const-c.inc"
30 errfunc(const char *foo, int bar) {
32 return !(bar == EACCES || bar == ENOENT || bar == ENOTDIR);
37 doglob(pTHX_ const char *pattern, int flags)
48 memset(&pglob, 0, sizeof(glob_t));
49 retval = bsd_glob(pattern, flags, errfunc, &pglob);
52 /* return any matches found */
53 EXTEND(sp, pglob.gl_pathc);
54 for (i = 0; i < pglob.gl_pathc; i++) {
55 /* printf("# bsd_glob: %s\n", pglob.gl_pathv[i]); */
56 tmp = newSVpvn_flags(pglob.gl_pathv[i], strlen(pglob.gl_pathv[i]),
69 iterate(pTHX_ bool(*globber)(pTHX_ AV *entries, const char *pat, STRLEN len, bool is_utf8))
74 const char * const cxixpv = (char *)&PL_op;
75 STRLEN const cxixlen = sizeof(OP *);
77 U32 const gimme = GIMME_V;
79 bool on_stack = FALSE;
81 if (!MY_CXT.x_GLOB_ENTRIES) MY_CXT.x_GLOB_ENTRIES = newHV();
82 entries = (AV *)*(hv_fetch(MY_CXT.x_GLOB_ENTRIES, cxixpv, cxixlen, 1));
84 /* if we're just beginning, do it all first */
85 if (SvTYPE(entries) != SVt_PVAV) {
90 /* glob without args defaults to $_ */
94 && (patsv = DEFSV, SvGETMAGIC(patsv), !SvOK(patsv))
101 pat = SvPV_nomg(patsv,len);
102 is_utf8 = !!SvUTF8(patsv);
103 /* the lower-level code expects a null-terminated string */
104 if (!SvPOK(patsv) || pat != SvPVX(patsv) || pat[len] != '\0') {
105 SV *newpatsv = newSVpvn_flags(pat, len, SVs_TEMP);
106 pat = SvPV_nomg(newpatsv,len);
110 if (!IS_SAFE_SYSCALL(pat, len, "pattern", "glob")) {
111 if (gimme != G_ARRAY)
118 on_stack = globber(aTHX_ entries, pat, len, is_utf8);
122 /* chuck it all out, quick or slow */
123 if (gimme == G_ARRAY) {
125 EXTEND(SP, AvFILLp(entries)+1);
126 Copy(AvARRAY(entries), SP+1, AvFILLp(entries)+1, SV *);
127 SP += AvFILLp(entries)+1;
129 /* No G_DISCARD here! It will free the stack items. */
130 (void)hv_delete(MY_CXT.x_GLOB_ENTRIES, cxixpv, cxixlen, 0);
133 if (AvFILLp(entries) + 1) {
134 mPUSHs(av_shift(entries));
137 /* return undef for EOL */
138 (void)hv_delete(MY_CXT.x_GLOB_ENTRIES, cxixpv, cxixlen, G_DISCARD);
145 /* returns true if the items are on the stack already, but only in
148 csh_glob(pTHX_ AV *entries, const char *pat, STRLEN len, bool is_utf8)
153 const char *s = NULL;
154 const char *piece = NULL;
156 SV *flags_sv = get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD);
157 int const flags = (int)SvIV(flags_sv);
158 U32 const gimme = GIMME_V;
162 assert(SvTYPE(entries) != SVt_PVAV);
163 sv_upgrade((SV *)entries, SVt_PVAV);
165 /* extract patterns */
167 while (++s < patend) {
173 const char quote = *s;
176 if (is_utf8) SvUTF8_on(word);
178 if (piece) sv_catpvn(word, piece, s-piece);
183 /* If the backslash is here to escape a quote,
185 if (s < patend && *s == quote)
186 sv_catpvn(word, piece, s-piece-1), piece = s;
188 else if (*s == quote) {
189 sv_catpvn(word, piece, s-piece);
194 if (!found) { /* unmatched quote */
195 /* Give up on tokenisation and treat the whole string
196 as a single token, but with whitespace stripped. */
198 while (isSPACE(*pat)) pat++;
199 while (isSPACE(*(patend-1))) patend--;
200 /* bsd_glob expects a trailing null, but we cannot mod-
202 if (patend < pat + len) {
203 if (word) sv_setpvn(word, pat, patend-pat);
205 word = newSVpvn_flags(
206 pat, patend-pat, SVf_UTF8*is_utf8
211 if (word) SvREFCNT_dec(word), word=NULL;
220 if (!piece) piece = s;
222 /* If the backslash is here to escape a quote,
224 if (s < patend && (*s == '"' || *s == '\'')) {
226 word = newSVpvn(piece,s-piece-1);
227 if (is_utf8) SvUTF8_on(word);
229 else sv_catpvn(word, piece, s-piece-1);
237 word = newSVpvn(piece,s-piece);
238 if (is_utf8) SvUTF8_on(word);
240 else sv_catpvn(word, piece, s-piece);
243 if (!patav) patav = (AV *)sv_2mortal((SV *)newAV());
244 av_push(patav, word);
248 else if (!piece) piece = s;
255 I32 items = AvFILLp(patav) + 1;
256 SV **svp = AvARRAY(patav);
260 doglob(aTHX_ SvPVXx(*svp++), flags);
266 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
271 /* piece is set at this point if there is no trailing whitespace.
272 It is the beginning of the last token or quote-delimited
273 piece thereof. word is set at this point if the last token has
274 multiple quoted pieces. */
277 if (piece) sv_catpvn(word, piece, s-piece);
282 doglob(aTHX_ piece, flags);
283 if (word) SvREFCNT_dec(word);
288 /* short-circuit here for a fairly common case */
289 if (!patav && gimme == G_ARRAY) { PUTBACK; return TRUE; }
291 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
303 iterate(aTHX_ csh_glob);
306 /* wrapper around doglob that can be passed to the iterator */
308 doglob_iter_wrapper(pTHX_ AV *entries, const char *pattern, STRLEN len, bool is_utf8)
311 SV * flags_sv = get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD);
312 int const flags = (int)SvIV(flags_sv);
314 PERL_UNUSED_VAR(len); /* we use \0 termination instead */
315 /* XXX we currently just use the underlying bytes of the passed SV.
316 * Some day someone needs to make glob utf8 aware */
317 PERL_UNUSED_VAR(is_utf8);
321 doglob(aTHX_ pattern, flags);
326 if (GIMME_V == G_ARRAY) { PUTBACK; return TRUE; }
327 sv_upgrade((SV *)entries, SVt_PVAV);
329 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
336 glob_ophook(pTHX_ OP *o)
338 if (PL_dirty) return;
341 if (MY_CXT.x_GLOB_ENTRIES
342 && (o->op_type == OP_GLOB || o->op_type == OP_ENTERSUB))
343 (void)hv_delete(MY_CXT.x_GLOB_ENTRIES, (char *)&o, sizeof(OP *),
345 if (MY_CXT.x_GLOB_OLD_OPHOOK) MY_CXT.x_GLOB_OLD_OPHOOK(aTHX_ o);
349 MODULE = File::Glob PACKAGE = File::Glob
361 bsd_glob(pattern_sv,...)
369 pattern = SvPV(pattern_sv, len);
370 if (!IS_SAFE_SYSCALL(pattern, len, "pattern", "bsd_glob"))
372 /* allow for optional flags argument */
374 flags = (int) SvIV(ST(1));
375 /* remove unsupported flags */
376 flags &= ~(GLOB_APPEND | GLOB_DOOFFS | GLOB_ALTDIRFUNC | GLOB_MAGCHAR);
378 SV * flags_sv = get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD);
379 flags = (int)SvIV(flags_sv);
383 doglob(aTHX_ pattern, flags);
391 /* For backward-compatibility with the original Perl function, we sim-
392 * ply take the first argument, regardless of how many there are.
396 XPUSHs(&PL_sv_undef);
403 bsd_glob_override(...)
407 XPUSHs(&PL_sv_undef);
410 iterate(aTHX_ doglob_iter_wrapper);
418 HV *glob_entries_clone = NULL;
420 PERL_UNUSED_ARG(items);
423 if ( MY_CXT.x_GLOB_ENTRIES ) {
425 param.stashes = NULL;
427 param.proto_perl = MY_CXT.interp;
429 glob_entries_clone = MUTABLE_HV(sv_dup_inc((SV*)MY_CXT.x_GLOB_ENTRIES, ¶m));
434 MY_CXT.x_GLOB_ENTRIES = glob_entries_clone;
435 MY_CXT.interp = aTHX;
442 #ifndef PERL_EXTERNAL_GLOB
443 /* Don't do this at home! The globhook interface is highly volatile. */
444 PL_globhook = csh_glob_iter;
453 MY_CXT.x_GLOB_ENTRIES = NULL;
454 MY_CXT.x_GLOB_OLD_OPHOOK = PL_opfreehook;
456 MY_CXT.interp = aTHX;
458 PL_opfreehook = glob_ophook;
462 INCLUDE: const-xs.inc