| 1 |
|
| /* |
| 2 |
|
| PerlIO::fse - File System Encoding |
| 3 |
|
| |
| 4 |
|
| */ |
| 5 |
|
| |
| 6 |
|
| #define PERL_NO_GET_CONTEXT |
| 7 |
|
| #include <EXTERN.h> |
| 8 |
|
| #include <perl.h> |
| 9 |
|
| #include <XSUB.h> |
| 10 |
|
| |
| 11 |
|
| #include <perliol.h> |
| 12 |
|
| |
| 13 |
|
| #include "ppport.h" |
| 14 |
|
| |
| 15 |
|
| #define LayerFetch(layer, n) ((layer)->array[n].funcs) |
| 16 |
|
| #define LayerFetchSafe(layer, n) ( ((n) >= 0 && (n) < (layer)->cur) \ |
| 17 |
|
| ? (layer)->array[n].funcs : PERLIO_FUNCS_CAST(&PerlIO_unix) ) |
| 18 |
|
| |
| 19 |
|
| |
| 20 |
|
| #define DEFAULT_FSE "UTF-8" |
| 21 |
|
| |
| 22 |
|
| #ifdef __CYGWIN__ |
| 23 |
|
| #include <windows.h> |
| 24 |
|
| #endif |
| 25 |
|
| |
| 26 |
|
| static PerlIO* |
| 27 |
0 |
| PerlIOUtil_openn(pTHX_ PerlIO_funcs* const force_tab, PerlIO_list_t* const layers, IV const n, |
| 28 |
|
| const char* const mode, int const fd, int const imode, int const perm, |
| 29 |
|
| PerlIO* f, int const narg, SV** const args){ |
| 30 |
0 |
| PerlIO_funcs* tab = NULL; |
| 31 |
0 |
| IV i = n; |
| 32 |
|
| |
| 33 |
0 |
▶ | while(--i >= 0){ /* find a layer with Open() */ |
Branch: 1 branch
| Branch | True | False | Total |
| while(--i >= 0){ /* find a layer with Open() */ |
0 |
0 |
0 |
|
| 34 |
0 |
| tab = LayerFetch(layers, i); |
| 35 |
0 |
▶ | if(tab && tab->Open){ |
Branch: 2 branches
| Branch | True | False | Total |
| if(tab && tab->Open){ |
0 |
0 |
0 |
| if(tab && tab->Open){ |
0 |
0 |
0 |
|
| 36 |
0 |
| break; |
| 37 |
|
| } |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
0 |
▶ | if(force_tab) tab = force_tab; |
Branch: 1 branch
| Branch | True | False | Total |
| if(force_tab) tab = force_tab; |
0 |
0 |
0 |
|
| 41 |
|
| |
| 42 |
0 |
▶ | if(tab && tab->Open){ |
Branch: 2 branches
| Branch | True | False | Total |
| if(tab && tab->Open){ |
0 |
0 |
0 |
| if(tab && tab->Open){ |
0 |
0 |
0 |
|
| 43 |
0 |
| f = tab->Open(aTHX_ tab, layers, i, mode, |
| 44 |
|
| fd, imode, perm, f, narg, args); |
| 45 |
|
| |
| 46 |
|
| /* apply 'upper' layers |
| 47 |
|
| e.g. [ :unix :perlio :utf8 :creat ] |
| 48 |
|
| ~~~~~ |
| 49 |
|
| */ |
| 50 |
|
| |
| 51 |
0 |
▶ | if(f && ++i < n){ |
Branch: 2 branches
| Branch | True | False | Total |
| if(f && ++i < n){ |
0 |
0 |
0 |
| if(f && ++i < n){ |
0 |
0 |
0 |
|
| 52 |
0 |
▶ | if(PerlIO_apply_layera(aTHX_ f, mode, layers, i, n) != 0){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(PerlIO_apply_layera(aTHX_ f, mode, layers, i, n) != 0){ |
0 |
0 |
0 |
|
| 53 |
0 |
| PerlIO_close(f); |
| 54 |
0 |
| f = NULL; |
| 55 |
|
| } |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
|
| } |
| 59 |
|
| else{ |
| 60 |
0 |
| SETERRNO(EINVAL, LIB_INVARG); |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
0 |
| return f; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| static IV |
| 67 |
0 |
| PerlIOUtil_useless_pushed(pTHX_ PerlIO* fp, const char* mode, SV* arg, |
| 68 |
|
| PerlIO_funcs* tab){ |
| 69 |
|
| PERL_UNUSED_ARG(fp); |
| 70 |
|
| PERL_UNUSED_ARG(mode); |
| 71 |
|
| PERL_UNUSED_ARG(arg); |
| 72 |
|
| |
| 73 |
0 |
▶ | if(ckWARN(WARN_LAYER)){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(ckWARN(WARN_LAYER)){ |
0 |
0 |
0 |
|
| 74 |
0 |
| Perl_warner(aTHX_ packWARN(WARN_LAYER), "Too late for %s layer", tab->name); |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
0 |
| return -1; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| |
| 81 |
|
| static SV* |
| 82 |
1 |
| PerlIOFSE_get_fse(pTHX){ |
| 83 |
1 |
| SV* const fse = get_sv("PerlIO::fse::fse", GV_ADDMULTI); |
| 84 |
|
| |
| 85 |
1 |
▶ | if (!SvOK(fse)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(fse)) { |
1 |
0 |
1 |
|
| 86 |
|
| #if defined(WIN32) || defined(__CYGWIN__) |
| 87 |
|
| unsigned long const codepage = GetACP(); |
| 88 |
|
| if(codepage != 0){ |
| 89 |
|
| Perl_sv_setpvf(aTHX_ fse, "cp%lu", codepage); |
| 90 |
|
| } |
| 91 |
|
| #endif |
| 92 |
|
| |
| 93 |
1 |
▶ | if(!PL_tainting){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!PL_tainting){ |
1 |
0 |
1 |
|
| 94 |
1 |
| const char* const env_fse = PerlEnv_getenv("PERLIO_FSE"); |
| 95 |
1 |
▶ | if(env_fse && *env_fse){ |
Branch: 2 branches
| Branch | True | False | Total |
| if(env_fse && *env_fse){ |
0 |
1 |
1 |
| if(env_fse && *env_fse){ |
0 |
0 |
0 |
|
| 96 |
0 |
| sv_setpv(fse, env_fse); |
| 97 |
|
| } |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
1 |
▶ | if(!SvOK(fse)){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(fse)){ |
1 |
0 |
1 |
|
| 101 |
1 |
| sv_setpvs(fse, DEFAULT_FSE); |
| 102 |
|
| } |
| 103 |
1 |
| PerlIO_debug("PerlIOFSE_initialize: encoding=%" SVf , fse); |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
1 |
| return fse; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
|
| static SV* |
| 110 |
0 |
| PerlIOFSE_encode(pTHX_ SV* const enc, SV* const str){ |
| 111 |
0 |
| dSP; |
| 112 |
|
| |
| 113 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 114 |
0 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
0 |
0 |
|
| 115 |
0 |
| PUSHs(enc); |
| 116 |
0 |
| PUSHs(str); |
| 117 |
0 |
| PUTBACK; |
| 118 |
|
| |
| 119 |
0 |
| call_pv("Encode::encode", G_SCALAR); |
| 120 |
|
| |
| 121 |
0 |
| SPAGAIN; |
| 122 |
|
| |
| 123 |
0 |
| return POPs; /* bytes */ |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
|
| static PerlIO* |
| 127 |
0 |
| PerlIOFSE_open(pTHX_ PerlIO_funcs* self, PerlIO_list_t* layers, IV n, |
| 128 |
|
| const char* mode, int fd, int imode, int perm, |
| 129 |
|
| PerlIO* f, int narg, SV** args){ |
| 130 |
|
| PERL_UNUSED_ARG(self); |
| 131 |
|
| |
| 132 |
0 |
▶ | if(SvUTF8(args[0])){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvUTF8(args[0])){ |
0 |
0 |
0 |
|
| 133 |
0 |
| SV* const arg = PerlIOArg; |
| 134 |
|
| SV* fse; |
| 135 |
|
| SV* save; |
| 136 |
|
| |
| 137 |
0 |
▶ | if(arg && SvOK(arg)){ |
Branch: 2 branches
| Branch | True | False | Total |
| if(arg && SvOK(arg)){ |
0 |
0 |
0 |
| if(arg && SvOK(arg)){ |
0 |
0 |
0 |
|
| 138 |
0 |
| fse = arg; |
| 139 |
|
| } |
| 140 |
|
| else{ |
| 141 |
0 |
| fse = PerlIOFSE_get_fse(aTHX); |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
0 |
▶ | if(!SvOK(fse)){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(fse)){ |
0 |
0 |
0 |
|
| 145 |
0 |
| Perl_croak(aTHX_ "fse: encoding not set"); |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
0 |
| ENTER; |
| 149 |
0 |
| SAVETMPS; |
| 150 |
|
| |
| 151 |
0 |
| save = args[0]; |
| 152 |
0 |
| args[0] = PerlIOFSE_encode(aTHX_ fse, args[0]); |
| 153 |
|
| |
| 154 |
0 |
| f = PerlIOUtil_openn(aTHX_ NULL, layers, n, |
| 155 |
|
| mode, fd, imode, perm, f, narg, args); |
| 156 |
|
| |
| 157 |
0 |
| args[0] = save; |
| 158 |
|
| |
| 159 |
0 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
0 |
0 |
0 |
|
| 160 |
0 |
| LEAVE; |
| 161 |
|
| |
| 162 |
0 |
| return f; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
0 |
| return PerlIOUtil_openn(aTHX_ NULL, layers, n, |
| 166 |
|
| mode, fd, imode, perm, f, narg, args); |
| 167 |
|
| |
| 168 |
|
| } |
| 169 |
|
| |
| 170 |
|
| PERLIO_FUNCS_DECL(PerlIO_fse) = { |
| 171 |
|
| sizeof(PerlIO_funcs), |
| 172 |
|
| "fse", |
| 173 |
|
| 0, /* size */ |
| 174 |
|
| PERLIO_K_DUMMY, /* kind */ |
| 175 |
|
| PerlIOUtil_useless_pushed, |
| 176 |
|
| NULL, /* popped */ |
| 177 |
|
| PerlIOFSE_open, |
| 178 |
|
| NULL, /* binmode */ |
| 179 |
|
| NULL, /* arg */ |
| 180 |
|
| NULL, /* fileno */ |
| 181 |
|
| NULL, /* dup */ |
| 182 |
|
| NULL, /* read */ |
| 183 |
|
| NULL, /* unread */ |
| 184 |
|
| NULL, /* write */ |
| 185 |
|
| NULL, /* seek */ |
| 186 |
|
| NULL, /* tell */ |
| 187 |
|
| NULL, /* close */ |
| 188 |
|
| NULL, /* flush */ |
| 189 |
|
| NULL, /* fill */ |
| 190 |
|
| NULL, /* eof */ |
| 191 |
|
| NULL, /* error */ |
| 192 |
|
| NULL, /* clearerr */ |
| 193 |
|
| NULL, /* setlinebuf */ |
| 194 |
|
| NULL, /* get_base */ |
| 195 |
|
| NULL, /* bufsiz */ |
| 196 |
|
| NULL, /* get_ptr */ |
| 197 |
|
| NULL, /* get_cnt */ |
| 198 |
|
| NULL /* set_ptrcnt */ |
| 199 |
|
| }; |
| 200 |
|
| |
| 201 |
|
| MODULE = PerlIO::fse PACKAGE = PerlIO::fse |
| 202 |
|
| |
| 203 |
|
| PROTOTYPES: DISABLE |
| 204 |
|
| |
| 205 |
|
| BOOT: |
| 206 |
2 |
| PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_fse)); |
| 207 |
|
| |
| 208 |
|
| SV* |
| 209 |
|
| get_fse(klass) |
| 210 |
|
| CODE: |
| 211 |
1 |
| RETVAL = PerlIOFSE_get_fse(aTHX); |
| 212 |
1 |
| SvREFCNT_inc_simple_void_NN(RETVAL); |
| 213 |
|
| OUTPUT: |
| 214 |
|
| RETVAL |
| 215 |
|
| |
| 216 |
|
| SV* |
| 217 |
|
| set_fse(klass, SV* encoding) |
| 218 |
|
| CODE: |
| 219 |
0 |
| RETVAL = PerlIOFSE_get_fse(aTHX); |
| 220 |
0 |
| SvREFCNT_inc_simple_void_NN(RETVAL); |
| 221 |
0 |
| sv_setsv(RETVAL, encoding); |
| 222 |
|
| OUTPUT: |
| 223 |
|
| RETVAL |