How can I get the whole command line?

I am writing a perl script that mimics gcc. This is my script should handle some stdout from gcc. The processing part has been completed, but I cannot get the simple part to work: how can I forward all the command line parameters, as well as the next process (gcc in my case). Command lines sent to gcc tend to be very long and potentially contain many escape sequences, and I don’t want to play this game with escaping now, and I know that it’s difficult to do this correctly in windows in difficult cases.

Basically, gcc.pl is some crazies\ t\\ "command line\""and that gcc.pl should forward the same command line to real gcc.exe (I use windows).

I do it like this: open("gcc.exe $cmdline 2>&1 |")so that stderr from gcc is served to stdout, and my perl script handles stdout. The problem is that I cannot find anywhere how to build this $cmdline.

+3
source share
4 answers

Run the exec and system function in Perl.

If you provide any of them with an array of arguments (rather than a single line), it calls the execve () function of Unix or a close relative directly, preventing the shell from interpreting anything exactly as you need.

+1
source

I would use AnyEvent :: Subprocess :

use AnyEvent::Subprocess;

my $process_line = sub { say "got line: $_[0]" };

my $gcc = AnyEvent::Subprocess->new(
    code      => ['gcc.exe', @ARGV],
    delegates => [ 'CompletionCondvar', 'StandardHandles', {
         MonitorHandle => {
              handle   => 'stdout',
              callback => $process_line,
         }}, {
         MonitorHandle => {
              handle   => 'stderr',
              callback => $process_line,
         }},
    ],
);

my $running = $gcc->run;
my $done = $running->recv;
$done->is_success or die "OH NOES";

say "it worked";

MonitorHandle , , stdout stderr. "code" - , .

+2

"Safe Pipe Opens" perlipc , , , , , .

, 2>&1 , , , .

#! /usr/bin/perl

use warnings;
use strict;

my $pid = open my $fromgcc, "-|";
die "$0: fork: $!" unless defined $pid;

if ($pid) {
  while (<$fromgcc>) {
    print "got: $_";
  }
}
else {
  # 2>&1
  open STDERR, ">&STDOUT" or warn "$0: dup STDERR: $!";

  no warnings "exec";  # so we can write our own message
  exec "gcc", @ARGV       or die  "$0: exec: $!";
}

Windows open FH, "-|", Cygwin :

$ ./gcc.pl foo.c
got: gcc: foo.c: No such file or directory
got: gcc: no input files
+2

, , , perl: , , . Perl , , MS stdlib ( win32).

- , signle, perl . , , , - perl. , 1) , 2) perl .

:

script.pl """test |test"

win32 :

ARGV=['"test', '|test']

""

ARGV=['"test |test']

activestate perl, perl: . , perl, msys, , , , mingw cygwin?..

perl , cmd- , . . MATTER WHAT cygwin . , ( )

perl gcc.pl -c  "-IC:\ffmpeg\lib_avutil\" rest of args

Perl , : -c '-IC:\ffmpeg\lib_avutil " " cmd arg: '-IC:\ffmpeg\lib_avutil \', , perl - , cmd . boost:: regex ++ , , , ne != .. Windows , perl - .

0

Source: https://habr.com/ru/post/1751858/


All Articles