File: perl5i.c.PL

package info (click to toggle)
libperl5i-perl 2.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 996 kB
  • ctags: 343
  • sloc: perl: 6,259; makefile: 5
file content (115 lines) | stat: -rw-r--r-- 2,678 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Write out the perl5i wrapper C program making sure it uses
# the Perl its built with.

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', $0, $perl_path, $tempdir;
/* THIS FILE IS GENERATED BY %s
 * Any changes here will be wiped out.  Edit it there instead.
 */

#define DEBUG 0

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*
 * Meant to mimic the shell command
 *     exec perl -Mperl5i::latest "$@"
 *
 * This is a C program so it works in a #! line.
 */

int main (int argc, char* argv[]) {
    int i;
    int j;
    const char* perl_cmd = "%s";
    char* perl_args[argc+1];
    char* dash_m = (char *)malloc(sizeof(char) * (strlen(argv[0]) + 20));
    char* program = "";
    int saw_dash_e = 0;

    strcpy(dash_m, "-Mperl5i::cmd=");
    strcat(dash_m, argv[0]);

    perl_args[0] = (char *)perl_cmd;
    perl_args[1] = dash_m;

    for( i = 1, j = 2;  i < argc;  i++ ) {
        char *dash_e = strchr(argv[i], 'e');

        /* Its a one liner */
        if( dash_e
	    && dash_e > argv[i]
	    && *(dash_e-1) == '-'
            && (strchr(argv[i], '-') == argv[i])
            && i+1 < argc
            )
        {
            saw_dash_e = 1;

            /* Chop out the -e */
            dash_e[0] = '\0';

            /* If all that's left is a dash, ignore it */
            if( strcmp(argv[i], "-") != 0 ) {
                perl_args[j] = argv[i];
                j++;
            }

            /* Skip the next argument, its the program */
            i++;
            program = argv[i];
            continue;
        }

        perl_args[j] = argv[i];
        j++;
    }

    /* Turn one liners into real programs to work around
       a Devel::Declare bug */
    if( saw_dash_e ) {
        char tempfile[] = "%s/perl5i.XXXXXX";
        int fd = mkstemp(tempfile);
        if( fd == -1 ) {
            perror("Could not open temporary file 'tempfile'");
            exit(1);
        }
        FILE *fh = fdopen(fd, "w");
        fprintf( fh, "$0 = '-e';\n" );
        fprintf( fh, "#line 1 \"-e\"\n" );
        fprintf( fh, "%%s", program );
        fclose(fh);

        perl_args[j++] = tempfile;
    }

    /* Argument array must be terminated by a null */
    perl_args[j] = (char *)NULL;

#if DEBUG
    for( i = 0; i <= j; i++ ) {
        printf("perl_args[%%d]: %%s\n", i, perl_args[i]);
    }

    if( saw_dash_e )
        printf("program: %%s\n", program);
#endif

    return execv( perl_cmd, perl_args );
}
END