Skip to content

Commit

Permalink
Merge pull request #296 from evalEmpire/rewrite_cmd_wrapper
Browse files Browse the repository at this point in the history
Rewrite cmd wrapper
  • Loading branch information
schwern authored Aug 8, 2016
2 parents e5085bc + 4933f8a commit d172a0f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 119 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ nytprof*
MYMETA.*
*.o
bin/perl5i
bin/perl5i.c
bin/perl5i.h
bin/perl5i.bat
.prove
*.swp
*.dSYM/
2 changes: 1 addition & 1 deletion Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ my $builder = MyBuild->new(
},

PL_files => {
'bin/perl5i.c.PL' => 'bin/perl5i.c',
'bin/perl5i.h.PL' => 'bin/perl5i.h',
'bin/perl5i.bat.PL' => 'bin/perl5i.bat',
},

Expand Down
3 changes: 2 additions & 1 deletion MANIFEST
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.perlcriticrc
.perltidyrc
bin/perl5i.bat.PL
bin/perl5i.c.PL
bin/perl5i.c
bin/perl5i.h.PL
bin/perl5i.plx
Build.PL
Changes
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.SKIP
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

# Don't ship perl5i
^bin/perl5i$
^bin/perl5i.c$
^bin/perl5i.h$
^bin/perl5i.bat$

# Ignore C object files
Expand Down
44 changes: 44 additions & 0 deletions bin/perl5i.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Meant to mimic the shell command
* exec perl -Mperl5i::latest "$@"
*
* This is a C program so it works in a #! line with minimal overhead.
*/

#define DEBUG 0

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "perl5i.h"

char *safe_cat(char *a, char*b) {
char *new = malloc(sizeof(char) * (strlen(a) + strlen(b) + 1));

strcpy(new, a);
strcat(new, b);

return new;
}

int main (int argc, char* argv[]) {
int i;

char **exec_args = malloc(sizeof(char*) * (argc + 2));
int num_exec_args = argc + 1;

/* Insert -Mperl5i::cmd=... into a copy of argv */
exec_args[0] = argv[0];
exec_args[1] = safe_cat("-Mperl5i::cmd=", argv[0]);
for( i = 1; i < argc; i++ ) {
exec_args[i+1] = argv[i];
}

exec_args[num_exec_args] = NULL;

execv(Perl_Path, exec_args );

fprintf(stderr, "Executing %s failed: %s\n", Perl_Path, strerror(errno));
}
115 changes: 0 additions & 115 deletions bin/perl5i.c.PL

This file was deleted.

23 changes: 23 additions & 0 deletions bin/perl5i.h.PL
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Write out a header config file for the perl5i C wrapper.

use strict;
use warnings;
use File::Spec;

my $file = shift;

# Its going inside double quotes.
my $perl_path = $^X;
$perl_path =~ s{ ([\\"]) }{\\$1}gx;

my $tempdir = File::Spec->tmpdir || "/tmp";

open my $fh, ">", $file or die $!;
printf $fh <<"END";
/* THIS FILE IS GENERATED BY $0
* Any changes here will be wiped out. Edit it there instead.
*/
const char Perl_Path[] = "$perl_path";
const char Temp_Template[] = "$tempdir/perl5i.XXXXXXXX";
END
4 changes: 4 additions & 0 deletions t/command_line_wrapper.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ like `$perl5icmd -h`, qr/disable all warnings/, 'perl5i -h works as expected';

like capture { system @perl5icmd, "-e", '$^X->say' }, qr/perl5i/, '$^X is perl5i';

is capture { system @perl5icmd, "-e", 'say @ARGV', 'foo', 'bar' }, "foobar\n", "-e with args";

is capture { system @perl5icmd, "-e", 'func foo() { say 42 } foo()' }, "42\n", "-e with Devel::Declare";

is capture { system @perl5icmd, '-wle', q[print 'Hello'] }, "Hello\n", "compound -e";

is capture { system @perl5icmd, "-Minteger", "-e", q[say 'Hello'] }, "Hello\n",
Expand Down

0 comments on commit d172a0f

Please sign in to comment.