Hello,

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

( keep:1 edit:19 reap:0 )


In reply to How to hook up C's socketpair() with Perl's? by mhutch7714

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.