/* 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_signal 30/01/2019 */ #include #include #include /* from the Perl distribution */ #include /* 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 with:\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(); }