This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Glob.xs: Remove comment
[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) {
96 Copy(AvARRAY(entries), SP+1, AvFILLp(entries)+1, SV *);
97 SP += AvFILLp(entries)+1;
98 }
88b04955
FC
99 /* No G_DISCARD here! It will free the stack items. */
100 hv_delete(MY_CXT.x_GLOB_ENTRIES, cxixpv, cxixlen, 0);
101 }
102 else {
103 if (AvFILLp(entries) + 1) {
88b04955
FC
104 mPUSHs(av_shift(entries));
105 }
106 else {
107 /* return undef for EOL */
88b04955
FC
108 hv_delete(MY_CXT.x_GLOB_ENTRIES, cxixpv, cxixlen, G_DISCARD);
109 PUSHs(&PL_sv_undef);
110 }
111 }
112 PUTBACK;
113}
114
115/* returns true if the items are on the stack already */
116static bool
1d6dcc39 117csh_glob(pTHX_ AV *entries, SV *patsv)
88b04955
FC
118{
119 dSP;
1bb8785a
FC
120 const char *pat;
121 AV *patav = NULL;
122 const char *patend;
88b04955 123 const char *s = NULL;
1bb8785a
FC
124 const char *piece = NULL;
125 SV *word = NULL;
126 int const flags =
127 (int)SvIV(get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD));
128 bool is_utf8;
88b04955
FC
129 STRLEN len;
130 U32 const gimme = GIMME_V;
1bb8785a
FC
131
132 /* glob without args defaults to $_ */
133 SvGETMAGIC(patsv);
134 if (
135 !SvOK(patsv)
136 && (patsv = DEFSV, SvGETMAGIC(patsv), !SvOK(patsv))
137 )
138 pat = "", len = 0, is_utf8 = 0;
139 else pat = SvPV_nomg(patsv,len), is_utf8 = !!SvUTF8(patsv);
140 patend = pat + len;
141
142 /* extract patterns */
1bb8785a
FC
143 s = pat-1;
144 while (++s < patend) {
145 switch (*s) {
146 case '\'':
147 case '"' :
148 {
149 bool found = FALSE;
e1621fc9 150 const char quote = *s;
1bb8785a
FC
151 if (!word) {
152 word = newSVpvs("");
153 if (is_utf8) SvUTF8_on(word);
154 }
155 if (piece) sv_catpvn(word, piece, s-piece);
156 piece = s+1;
f89f9e93 157 while (++s < patend)
e1621fc9
FC
158 if (*s == '\\') {
159 s++;
160 /* If the backslash is here to escape a quote,
161 obliterate it. */
162 if (s < patend && *s == quote)
163 sv_catpvn(word, piece, s-piece-1), piece = s;
164 }
165 else if (*s == quote) {
1bb8785a
FC
166 sv_catpvn(word, piece, s-piece);
167 piece = NULL;
168 found = TRUE;
169 break;
170 }
171 if (!found) { /* unmatched quote */
172 /* Give up on tokenisation and treat the whole string
173 as a single token, but with whitespace stripped. */
174 piece = pat;
175 while (isSPACE(*pat)) pat++;
176 while (isSPACE(*(patend-1))) patend--;
177 /* bsd_glob expects a trailing null, but we cannot mod-
178 ify the original */
179 if (patend < SvEND(patsv)) {
180 if (word) sv_setpvn(word, pat, patend-pat);
181 else
182 word = newSVpvn_flags(
183 pat, patend-pat, SVf_UTF8*is_utf8
184 );
185 piece = NULL;
186 }
187 else {
188 if (word) SvREFCNT_dec(word), word=NULL;
189 piece = pat;
190 s = patend;
191 }
192 goto end_of_parsing;
193 }
194 break;
195 }
e1621fc9
FC
196 case '\\':
197 if (!piece) piece = s;
198 s++;
199 /* If the backslash is here to escape a quote,
200 obliterate it. */
201 if (s < patend && (*s == '"' || *s == '\'')) {
202 if (!word) {
203 word = newSVpvn(piece,s-piece-1);
204 if (is_utf8) SvUTF8_on(word);
205 }
206 else sv_catpvn(word, piece, s-piece-1);
207 piece = s;
208 }
209 break;
1bb8785a
FC
210 default:
211 if (isSPACE(*s)) {
212 if (piece) {
213 if (!word) {
214 word = newSVpvn(piece,s-piece);
215 if (is_utf8) SvUTF8_on(word);
216 }
217 else sv_catpvn(word, piece, s-piece);
218 }
219 if (!word) break;
220 if (!patav) patav = (AV *)sv_2mortal((SV *)newAV());
221 av_push(patav, word);
222 word = NULL;
223 piece = NULL;
224 }
225 else if (!piece) piece = s;
226 break;
227 }
228 }
229 end_of_parsing:
230
1d6dcc39 231 assert(SvTYPE(entries) != SVt_PVAV);
1bb8785a
FC
232 sv_upgrade((SV *)entries, SVt_PVAV);
233
234 if (patav) {
235 I32 items = AvFILLp(patav) + 1;
236 SV **svp = AvARRAY(patav);
237 while (items--) {
238 PUSHMARK(SP);
239 PUTBACK;
240 doglob(aTHX_ SvPVXx(*svp++), flags);
241 SPAGAIN;
242 {
243 dMARK;
244 dORIGMARK;
245 while (++MARK <= SP)
246 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
247 SP = ORIGMARK;
248 }
249 }
250 }
251 /* piece is set at this point if there is no trailing whitespace.
252 It is the beginning of the last token or quote-delimited
253 piece thereof. word is set at this point if the last token has
254 multiple quoted pieces. */
255 if (piece || word) {
256 if (word) {
257 if (piece) sv_catpvn(word, piece, s-piece);
258 piece = SvPVX(word);
259 }
260 PUSHMARK(SP);
261 PUTBACK;
262 doglob(aTHX_ piece, flags);
263 if (word) SvREFCNT_dec(word);
264 SPAGAIN;
265 {
266 dMARK;
267 dORIGMARK;
1bb8785a 268 /* short-circuit here for a fairly common case */
88b04955 269 if (!patav && gimme == G_ARRAY) { PUTBACK; return TRUE; }
c0d74586
FC
270 while (++MARK <= SP)
271 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
1bb8785a
FC
272
273 SP = ORIGMARK;
274 }
275 }
88b04955
FC
276 PUTBACK;
277 return FALSE;
278}
1bb8785a 279
88b04955
FC
280static void
281csh_glob_iter(pTHX)
282{
283 iterate(aTHX_ csh_glob);
1bb8785a
FC
284}
285
f4cbf990
FC
286/* wrapper around doglob that can be passed to the iterator */
287static bool
1d6dcc39 288doglob_iter_wrapper(pTHX_ AV *entries, SV *patsv)
f4cbf990
FC
289{
290 dSP;
291 const char *pattern;
292 int const flags =
293 (int)SvIV(get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD));
f4cbf990
FC
294
295 SvGETMAGIC(patsv);
296 if (
297 !SvOK(patsv)
298 && (patsv = DEFSV, SvGETMAGIC(patsv), !SvOK(patsv))
299 )
300 pattern = "";
301 else pattern = SvPV_nomg_nolen(patsv);
302
303 PUSHMARK(SP);
304 PUTBACK;
305 doglob(aTHX_ pattern, flags);
306 SPAGAIN;
307 {
308 dMARK;
309 dORIGMARK;
310 if (GIMME_V == G_ARRAY) { PUTBACK; return TRUE; }
f4cbf990
FC
311 sv_upgrade((SV *)entries, SVt_PVAV);
312 while (++MARK <= SP)
313 av_push(entries, SvREFCNT_inc_simple_NN(*MARK));
314 SP = ORIGMARK;
315 }
316 return FALSE;
317}
318
72b16652
GS
319MODULE = File::Glob PACKAGE = File::Glob
320
b84cd0b1
NC
321int
322GLOB_ERROR()
323 PREINIT:
324 dMY_CXT;
325 CODE:
326 RETVAL = GLOB_ERROR;
327 OUTPUT:
328 RETVAL
329
72b16652 330void
960ddb65 331bsd_glob(pattern,...)
72b16652 332 char *pattern
72b16652
GS
333PREINIT:
334 glob_t pglob;
335 int i;
336 int retval;
337 int flags = 0;
338 SV *tmp;
339PPCODE:
340 {
89ca4ac7
JH
341 dMY_CXT;
342
72b16652
GS
343 /* allow for optional flags argument */
344 if (items > 1) {
345 flags = (int) SvIV(ST(1));
3c97495f
CB
346 /* remove unsupported flags */
347 flags &= ~(GLOB_APPEND | GLOB_DOOFFS | GLOB_ALTDIRFUNC | GLOB_MAGCHAR);
960ddb65 348 } else {
28cd8e1d 349 flags = (int) SvIV(get_sv("File::Glob::DEFAULT_FLAGS", GV_ADD));
72b16652 350 }
1bb8785a
FC
351
352 PUTBACK;
353 doglob(aTHX_ pattern, flags);
354 SPAGAIN;
355 }
72b16652 356
1bb8785a
FC
357PROTOTYPES: DISABLE
358void
359csh_glob(...)
360PPCODE:
361 /* For backward-compatibility with the original Perl function, we sim-
362 * ply take the first two arguments, regardless of how many there are.
363 */
364 if (items >= 2) SP += 2;
365 else {
366 SP += items;
367 XPUSHs(&PL_sv_undef);
368 if (!items) XPUSHs(&PL_sv_undef);
72b16652 369 }
1bb8785a 370 PUTBACK;
88b04955 371 csh_glob_iter(aTHX);
1bb8785a 372 SPAGAIN;
72b16652 373
f4cbf990
FC
374void
375bsd_glob_override(...)
376PPCODE:
377 if (items >= 2) SP += 2;
378 else {
379 SP += items;
380 XPUSHs(&PL_sv_undef);
381 if (!items) XPUSHs(&PL_sv_undef);
382 }
383 PUTBACK;
384 iterate(aTHX_ doglob_iter_wrapper);
385 SPAGAIN;
386
28cd8e1d
NC
387BOOT:
388{
d67594ff
FC
389#ifndef PERL_EXTERNAL_GLOB
390 /* Don’t do this at home! The globhook interface is highly volatile. */
88b04955 391 PL_globhook = csh_glob_iter;
d67594ff 392#endif
bcd258b8 393}
28cd8e1d 394
bcd258b8
NC
395BOOT:
396{
28cd8e1d 397 MY_CXT_INIT;
1bb8785a
FC
398 {
399 dMY_CXT;
6a606603 400 MY_CXT.x_GLOB_ENTRIES = NULL;
1bb8785a 401 }
28cd8e1d
NC
402}
403
1cb0fb50 404INCLUDE: const-xs.inc