From 331764431e1704861a5cbe51ee3bc7fa4eaec5b7 Mon Sep 17 00:00:00 2001 From: Benjamin Landers Date: Wed, 4 Apr 2018 20:39:37 -0700 Subject: [PATCH] Makes post process scripts able to pass args along with filename (#4363) Any whitespace is the boundrary between the args/filename. Whitespace can be escaped by putting a exxclamation point in front of it. And exclamation points can be escaped by putting an exclamation point in front of the exclamation point to be escaped. I thought about adding another box for arguments, but I think that would make it more confusing to use. The only worry I have with this method is peoples existing scripts with whitespace in the name. --- lib/Slic3r/Print.pm | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 586bd8a8d..23799e8be 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -71,6 +71,34 @@ sub process { } } +sub escaped_split { + my ($line) = @_; + + # Free up three characters for temporary replacement + $line =~ s/%/%%/g; + $line =~ s/#/##/g; + $line =~ s/\?/\?\?/g; + + # replace escaped !'s + $line =~ s/\!\!/%#\?/g; + + # split on non-escaped whitespace + my @split = split /(?<=[^\!])\s+/, $line, -1; + + for my $part (@split) { + # replace escaped whitespace with the whitespace + $part =~ s/\!(\s+)/$1/g; + + # resub temp symbols + $part =~ s/%#\?/\!/g; + $part =~ s/%%/%/g; + $part =~ s/##/#/g; + $part =~ s/\?\?/\?/g; + } + + return @split; +} + sub export_gcode { my $self = shift; my %params = @_; @@ -121,11 +149,14 @@ sub export_gcode { $self->config->setenv; for my $script (@{$self->config->post_process}) { Slic3r::debugf " '%s' '%s'\n", $script, $output_file; + my @parsed_script = escaped_split $script; + my $executable = shift @parsed_script ; + push @parsed_script, $output_file; # -x doesn't return true on Windows except for .exe files - if (($^O eq 'MSWin32') ? !(-e $script) : !(-x $script)) { - die "The configured post-processing script is not executable: check permissions. ($script)\n"; + if (($^O eq 'MSWin32') ? !(-e $executable) : !(-x $executable)) { + die "The configured post-processing script is not executable: check permissions or escape whitespace/exclamation points. ($executable) \n"; } - system($script, $output_file); + system($executable, @parsed_script); } } }