Front page | perl.perl5.porters |
Postings from November 2003
[perl #24496] Exploiting PerlIO "feature" with trepidation
From:
Richard Kandarian
Date:
November 16, 2003 07:44
Subject:
[perl #24496] Exploiting PerlIO "feature" with trepidation
Message ID:
rt-24496-67482.17.3903026062506@rt.perl.org
# New Ticket Created by Richard Kandarian
# Please include the string: [perl #24496]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=24496 >
This is a bug report for perl from richard.kandarian@lanl.gov,
generated with the help of perlbug 1.34 running under perl v5.8.0.
-----------------------------------------------------------------
[Please enter your report here]
I'm working on Win32, but intending to be able to run on *nix also.
I'm embedding perl in a COTS (Commercial Off The Shelf) application via
an API. My embedded perl is loaded into the application as a DLL. I
found that My embedded perl had no access to stdout or stderr in the
COTS application.
On perusing the Perl source code I (happily) found that PerlIO is lazily
setup giving me an opportunity to open a file for stdout and stderr for
perl to discover and blissfully use. This is VERY handy.
My wish is that this be acknowledged as a USEFUL FEATURE and not changed
in future releases.
Please also notice my use of rsignal in the attached code snippet.
Code snippets follow: (Any comments and/or advice will be much
appreciated. Especially as it looks very promising that my application
will be released under the "Artistic License" soon.)
....
perl_construct( my_perl);
/*
I use plain ol' stdio when there is no perl interpreter, and I
save stdio stuff prior to including perl .h files as pperl_*
*/
pperl_fflush(NULL);
open_properl_stdio(); /* see below */
/*
Needed on Win32 to catch WM_TIMER that I seem intercept because of
something the host app does.
*/
rsignal(14, pp_sigcheck);
pperl_no_interp = 0; /* To tell my what kind of IO to use */
... /* opening debugging log files & such for use with PerlIO */
exitstatus = perl_parse(my_perl, xs_init, PPargc, PPargv, (char**)NULL);
...
/* after killing the interpreter */
PerlIO_close(PerlIO_stdout());
PerlIO_close(PerlIO_stderr());
...
/* Re-direct stdout and stderr to log files */
void open_properl_stdio(){
int outfd, errfd;
#if defined _WIN32
#define TEXTMODE "t"
HANDLE h;
if((h = CreateFile("properl.out", GENERIC_WRITE, FILE_SHARE_READ, NULL,
TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) ==
INVALID_HANDLE_VALUE){
if((h = CreateFile("properl.out", GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) ==
INVALID_HANDLE_VALUE){
pperl_fprintf(pperl_stderr, "Failed to open properl.out\n");
exit(1);
}
}
if((outfd = _open_osfhandle(h, 0)) < 0){
pperl_fprintf(pperl_stderr, "Failed to get fd for properl.out\n");
exit(1);
}
if((h = CreateFile("properl.err", GENERIC_WRITE, FILE_SHARE_READ, NULL,
TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) ==
INVALID_HANDLE_VALUE){
if((h = CreateFile("properl.err", GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) ==
INVALID_HANDLE_VALUE){
pperl_fprintf(pperl_stderr, "Failed to open properl.err\n");
exit(1);
}
}
if((errfd = _open_osfhandle(h, 0)) < 0){
pperl_fprintf(pperl_stderr, "Failed to get fd for properl.err\n");
exit(1);
}
#else
#define TEXTMODE ""
if((outfd = open("properl.out", O_CREAT, S_IREAD | S_IWRITE)) < 0){
pperl_fprintf(pperl_stderr, "Failed to open properl.out\n");
exit(1);
}
if((errfd = open("properl.err", O_CREAT, S_IREAD | S_IWRITE)) < 0){
pperl_fprintf(pperl_stderr, "Failed to open properl.err\n");
exit(1);
}
#endif
PerlIO_allocate(my_perl);
PerlIO_fdopen(0, "Ir" "t");
PerlIO_fdopen(outfd, "Iw" TEXTMODE);
PerlIO_fdopen(errfd, "Iw" TEXTMODE);
#if 1
/*
It might be nice to be able to use tail -f on these...
But there will be a performance penalty...
This is not "autoflush" - it will not flush lines with out an \n at
the end. I haven't figured out how to do that, but this is probably
good enough.
*/
PerlIO_setlinebuf(PerlIO_stdout());
PerlIO_setlinebuf(PerlIO_stderr());
#endif
}
Signal_t pp_sigcheck(int sig){
PerlIO_printf(PerlIO_stderr(), "Caught signal %d\n", sig);
}
[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
category=core
severity=wishlist
---
Site configuration information for perl v5.8.0:
Configured by 085598 at Mon Oct 27 17:00:47 2003.
Summary of my perl5 (revision 5 version 8 subversion 0) configuration:
Platform:
osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi
uname=''
config_args='undef'
hint=recommended, useposix=true, d_sigaction=undef
usethreads=undef use5005threads=undef useithreads=undef
usemultiplicity=define
useperlio=define d_sfio=undef uselargefiles=undef usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cl', ccflags ='-nologo -Gf -W3 -Od -MD -DNDEBUG -DWIN32 -D_CONSOLE
-DNO_STRICT -DHAVE_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DUSE_PERLIO
-DPERL_MSVCRT_READFIX',
optimize='-Od -MD -DNDEBUG',
cppflags='-DWIN32'
ccversion='', gccversion='', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=4
alignbytes=8, prototype=define
Linker and Libraries:
ld='link', ldflags ='-nologo -nodefaultlib
-release -libpath:"c:\perl\5.8.0\lib\MSWin32-x86-multi\CORE" -machine:x86'
libpth=\lib
libs=c:\src\openssl-0.9.6c\out32\libeay32.lib oldnames.lib
kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
shell32.lib ole32.lib oleaut32.lib netapi32.lib uuid.lib wsock32.lib
mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib msvcrt.lib
perllibs=c:\src\openssl-0.9.6c\out32\libeay32.lib oldnames.lib
kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
shell32.lib ole32.lib oleaut32.lib netapi32.lib uuid.lib wsock32.lib
mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib msvcrt.lib
libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl58.lib
gnulibc_version='undef'
Dynamic Linking:
dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib
-release -libpath:"c:\perl\5.8.0\lib\MSWin32-x86-multi\CORE" -machine:x86'
Locally applied patches:
---
@INC for perl v5.8.0:
c:/Pro_Perl/blib/lib
c:/Pro_Perl/blib/arch
c:/Perl/5.8.0/lib/MSWin32-x86-multi
c:/Perl/5.8.0/lib
c:/Perl/site/5.8.0/lib/MSWin32-x86-multi
c:/Perl/site/5.8.0/lib
c:/Perl/site/lib
.
---
Environment for perl v5.8.0:
HOME=C:\cygwin\home\085598
LANG (unset)
LANGUAGE (unset)
LD_LIBRARY_PATH (unset)
LOGDIR (unset)
PATH=C:\cygwin\usr\local\bin;C:\cygwin\bin;C:\cygwin\bin;c:\Program
Files\Microsoft Visual Studio .NET\Common7\IDE;c:\Program Files\Microsoft
Visual Studio .NET\VC7\BIN;c:\Program Files\Microsoft Visual Studio
.NET\Common7\Tools;c:\Program Files\Microsoft Visual Studio
.NET\Common7\Tools\bin\prerelease;c:\Program Files\Microsoft Visual Studio
.NET\Common7\Tools\bin;c:\Program Files\Microsoft Visual Studio
.NET\FrameworkSDK\bin;c:\WINDOWS\Microsoft.NET\Framework\v1.0.3705;c:\Perl\5.8.0\bin;c:\Perl\5.8.0\bin\MSWin32-x86-multi;c:\Program
Files\Reflection;c:\WINDOWS\system32;c:\WINDOWS;c:\WINDOWS\System32\Wbem;c:\PROGRA~1\F-Secure\Ssh;c:\Program
Files\Common Files\Adaptec
Shared\System;c:\pro_e\proiclient\bin;c:\pro_e\ver23\bin;c:\pro_e\ptchelp2001\bin;c:\pro_e\wildfire\bin;c:\pro_e\mechwildfire\bin;c:\pro_e\ptchelpWildfire\bin
PERL5LIB=c:/Pro_Perl/blib/lib;c:/Pro_Perl/blib/arch
PERL_BADLANG (unset)
SHELL (unset)
Richard Kandarian
http://www.lanl.gov/cgi-bin/fonelink.pl/085598
Any opinions stated in this message are not expressed on behalf of any
individual or entity other than me unless explicitly noted otherwise. My
node in the Web: http://www.kandarian.com
-
[perl #24496] Exploiting PerlIO "feature" with trepidation
by Richard Kandarian