Cool! the ARGV method you suggested works just fine. I would not be able to figure that out from perlembed. Thanks.

And you were right about buffering. Ending the message with a line feed fixed that. So now I get the sighandler message BUT still the Perl code terminates even if Perl's sighandler has no die() and is meant to just ignore it/print message.

Update: just a warning that an infinite loop ensues if argc==0

Update2: of course argc is always > 0.

Update3: it turns out one can send parameters to perl -e ... using perl -e 'print join(",",@ARGV)."\n"' -- -a -b -c 12, ikegami adds -- before any user-specified arguments to perl.

Update4: I have changed perlargv[i+3] to perlargv[i+4] so that -- is not overwritten.

Update5: kschwab found out why ctrl-c kills the program (because of the long sleep, sleep(1000)),

Update6: based on all the suggestions by ikegami and kschwab, this works for me:

/* harness for embedding Perl into C modified by Bliako from https://perldoc.perl.org/perlembed.html Compile: $(perl -MConfig -e 'print $Config{cc}') perl_embed_argv_signal.c $ +(perl -MExtUtils::Embed -e ccopts -e ldopts) -o perl_embed_argv_signa +l 30/01/2019 */ #include <stdio.h> #include <signal.h> #include <EXTERN.h> /* from the Perl distribution */ #include <perl.h> /* from the Perl distribution */ static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ void cleanup(void); //let perl handle it void signal_handler(int signum){ printf("got signal %d\n", signum); } int main(int argc, char **argv, char **env){ signal(SIGINT, signal_handler); PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); printf("%s : perl_alloc()\n", argv[0]); perl_construct(my_perl); printf("%s : perl_construct()\n", argv[0]); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; char** perlargv = malloc( (argc+3) * sizeof(char*) ); perlargv[0] = "perl"; perlargv[1] = "-e"; perlargv[2] = "1"; perlargv[3] = "--"; int i = argc-1; while (i--) perlargv[i+4] = argv[i+1]; perl_parse(my_perl, NULL, argc+3, perlargv, NULL); const char perlcode[] = "#$SIG{INT} = sub { print \"Caught your ctrl-c and exiting!\\n\"; exit +(0); };\n" "$SIG{INT} = sub { print \"Caught your ctrl-c but ignoring it!\\n\" }; +\n" "print \"$0: ARGV:\"; print ' '.$_ foreach(@ARGV); print \"\\n\";\n" "print \"my pid $$\\n\";\n" "print \"now sleeping, and you ctrl-c me\\n\";\n" "for(1..100){sleep(1);print\"$_\\n\";}\n" ; printf("%s : executing :\n%s\n", argv[0], perlcode); SV *ret = eval_pv(perlcode, FALSE); if( SvTRUE(ERRSV) ){ fprintf(stderr, "%s : eval_sv() has failed wi +th:\n%s\nfor the code:\n%s\n", argv[0], SvPVx_nolen(ERRSV), perlcode) +; cleanup(); exit(1); } printf("%s : done.\n", argv[0]); exit(EXIT_SUCCESS); } void cleanup(void){ perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }

In reply to Re^2: Pass signals and argv from C to embedded Perl by bliako
in thread Pass signals and argv from C to embedded Perl by bliako

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.