view mpsplit.pl @ 997:f7cd4528926b draft

Warning message for BOM fixed
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 17 Oct 2022 17:11:04 +0859
parents 1ba114ebcf2c
children
line wrap: on
line source

#!/usr/bin/env perl
$sep = "--" . $ARGV[0];
$dir = ($ARGV[1] || "tmp");
if ($dir =~ /^([^<>\;\&]*)$/) {
  $dir = $1;
}

#print "sep=".$sep, "dir=$dir\n";
#binmode STDIN;
$/ = undef;
$stream = (<STDIN>);
$n=0;
@slices = split($sep, $stream);
shift(@slices);
pop(@slices);
foreach $item (@slices) {
  $item =~ s/^\n//;
  ($header = $item) =~ s/\r\n\r\n.*//s;
  ($body = $item) =~ s/.*?\r\n\r\n//s;
  $body =~ s/\r\n$//;
  #print "h=[$header]\n";
  #print "b=[$body]\n";
  unless ($header =~ /\bname=([\"']?)(.*?)\1/) {
    next;
  }
  $name = $2;
  #print "name=$name\n";
  if ($header =~ /filename=(['\"]?)(.*?)\1/ && $2 gt "") {
    $fn=$2;
    if ($fn =~ /^([^\/]*)$/) {
      $fn = $1;
    }
    $fn =~ s/[ 	<>&\#'\"\&|*?]/_/g;
    $fn =~ s,.*[/\\],,;
    open(OUT, ">$dir/$fn");
      print OUT $body;
    close(OUT);
    printf("%s:filename=%s\n", $name, unpack("H*", $fn));
  } else {
    printf("%s=%s\n", $name, unpack("H*", $body));
  }
}

yatex.org