mhutch7714 has asked for the wisdom of the Perl Monks concerning the following question:
Am trying to add "features" to an existing C/Unix program to use a seperate Perl program for regex substitutions.
Saw the problem as a single bi-directional pipe, where
1. C program writes a string to the Perl program.
2. Perl progam transforms the string and write it back.
Mocked it up in C using socketpair(0, fork/exec, but how do I hook up C's descriptors with Perl's? (All of the examples I've found perform C<->C, or Perl<->Perl.)
Am I just making this harder than need be? Suggestions welcome.
Thanks in advance.
mhutch
Perl & C code follow: ----------------------------
HelloWorld.pl #!/usr/bin/perl -w # HelloWorld.pl pipe2 - bidirectional communication using socketpair # "the best ones always go both ways" use Socket ; use IO::Handle; # thousands of lines just for autoflush :-( # We say AF_UNIX because although *_LOCAL is the # POSIX 1003.1g form of the constant, many machines # still don't have it. socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!"; CHILD->autoflush(1); PARENT->autoflush(1); if ($pid = fork) { # parent #exit ; close PARENT; do { print CHILD "Parent Pid $$ is sending this\n"; print "Parent Pid $$ just read this: `$line`\n"; } while( chomp($line = <CHILD>) ) ; close CHILD; waitpid($pid,0); } else { # child die "cannot fork: $!" unless defined $pid; close CHILD; while( chomp($line = <PARENT>)) { print "Child Pid $$ just read this: `$line`\n"; print PARENT "Child Pid $$ is sending this\n"; } close PARENT; exit; } ---------------------------------------------------------- spair.c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> char * strupr( char * string ) ; /* Change string to uppercas +e */ int main(void) { int sv[2]; /* the pair of socket descriptors */ char buf[1024]; /* for data exchange between processes */ int i ; /* counter */ char *ch = "a" ; char * mydata[] = { "alfa", "bravo", "charlie", "delta", "echo" } ; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) { perror("socketpair"); exit(1); } if (!fork()) { /* child */ /*execlp ("subsn.pl", "subsn.pl", 0);*/ /* Fire the substitution modul +e */ execlp ("HelloWorld.pl", "HelloWorld.pl", 0); /* Fire the substitution + module */ for ( i = 0 ; i < 5 ; i ++ ) { read(sv[1], &buf, sizeof( buf ) ); printf("child: read [%s]\n", buf); /*buf = toupper(buf);*/ /* make it uppercase */ strupr( buf ) ; /* make it uppercase */ write(sv[1], &buf, sizeof(buf)); printf("child: sent [%s]\n", buf); } } else { /* parent */ for ( i = 0 ; i < 5 ; i ++ ) { strcpy( buf, mydata[ i ] ) ; /* copy data to buffer */ write(sv[0], buf, sizeof( buf)); printf("parent: sent [%s]\n", buf ); read(sv[0], &buf, sizeof(buf)); printf("parent: read [%s]\n\n", buf); } wait(NULL); /* wait for child to die */ } return 0; } /* ** strupr : Change string to uppercase */ char * strupr( char * string ) { char * original = string ; while ( * string ) { * string = _toupper( *string ) ; string++ ; } return ( original ) ; }
Edited by planetscape - added readmore tags
|
|---|