/usr/share/doc/printer-driver-cups-pdf/examples/pstitleiconv-0.2/pstitleiconv is in printer-driver-cups-pdf 2.6.1-21.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #!/usr/bin/perl -w
# Copyright 2006 Nickolay Kondrashov
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
use strict;
use Text::Iconv;
my $CONFIG_PATH = "/etc/cups/pstitleiconv.conf";
### CONFIGURATION ##############################
# set default configuration parameters
my $FROMCODE='ascii';
my $TOCODE='utf-8';
# read config file contents
my $CONFIG_CONTENTS;
{
local $/;
open( CONFIG, $CONFIG_PATH ) or
die "ERROR: Failed to open config file $CONFIG_PATH: $!\n";
$CONFIG_CONTENTS = <CONFIG>;
close( CONFIG ) or
die "ERROR: Failed to close config file $CONFIG_PATH: $!\n";
}
# eval config file contents
eval $CONFIG_CONTENTS;
if( $@ ) {
die "ERROR: Failed to evaluate config file contents: $@\n"
}
### SUBS #######################################
sub try_decode_ps_hex_string
{
my( $string ) = @_;
if( $string =~ m/^<[0-9a-fA-F \t]*>$/ ) {
$string =~ tr/ \t<>//d; # remove whitespace and brackets
$string = pack( 'H*', $string );
}
return $string;
}
sub encode_ps_hex_string
{
my( $string ) = @_;
return '<'. uc( unpack( 'H*', $string ) ). '>';
}
sub convert_title
{
my( $converter, $original_title ) = @_;
warn "DEBUG: decoding title: $original_title\n";
my $decoded_title = try_decode_ps_hex_string( $original_title );
warn "DEBUG: converting title: $decoded_title\n";
my $converted_title = $converter->convert( $decoded_title );
unless( defined $converted_title ) {
warn "WARNING: failed to convert title, leaving as is.\n";
return $original_title;
}
warn "DEBUG: encoding title: $converted_title\n";
my $encoded_title = encode_ps_hex_string( $converted_title );
warn "DEBUG: encoded title: $encoded_title\n";
return $encoded_title;
}
### MAIN #######################################
my( $printer, $jobid, $username, $title, $copies, $options, $filename ) = @_;
my $fh;
if( defined $filename ) {
open( $fh, $filename ) or
die "ERROR: Failed to open $filename: $!\n";
} else {
$fh = *STDIN;
}
my $converter = new Text::Iconv( $FROMCODE, $TOCODE );
my $rec_depth = 0;
my $got_title = 0;
while( <$fh> ) {
if( m/^\%!/ ) {
if( $rec_depth == 0 ) {
warn "DEBUG: found beginning of postscript code: $_\n";
} else {
warn "DEBUG: found beginning of embedded (e)ps code: $_\n";
}
$rec_depth++;
} elsif( m/^\%\%EOF/ ) {
$rec_depth--;
if( $rec_depth == 0 ) {
warn "DEBUG: found end of postscript code: $_\n";
} elsif( $rec_depth > 0 ) {
warn "DEBUG: found end of embedded (e)ps code: $_\n";
} else {
warn "WARNING: found unexpected end of ps code: $_\n";
}
} elsif(
$rec_depth == 1 &&
! $got_title &&
s/^\%\%Title: ([^\r\n]*)(\r?\n?)$/
"\%\%Title: ".
convert_title( $converter, $1 ).
$2
/e
) {
warn "DEBUG: title converted\n";
$got_title = 1;
}
print;
}
close $fh;
|