summaryrefslogtreecommitdiff
path: root/print/if-psprint/files/if-psprint.pl
blob: 399055d1709ed03ebb302518515484aba0f89e0f (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!@PREFIX@/bin/perl -w
#
# $NetBSD: if-psprint.pl,v 1.2 2000/10/17 11:12:37 abs Exp $
# 
#	Copyright (c) 2000 David Brownlee <abs@netbsd.org>. All rights
#	reserved. Provided as-is without express or implied warranties.
#
#	Redistribution and use in source and binary forms, with or without
#	modification, are permitted provided the above copyright and this
#	notice is retained.
#

=head1 Overview

if-psprint will ssend text, postscript, or native printer language to an
arbitrary printer.

Designed as a quick fix for the random printers that get hooked up to
the NetBSD server and random Windows boxes around the office. Uses enscript
to convert text to postscript, and ghostcript to convert to native printer
language as required.

=head1 Details

=over 4

=item *

Overloads 'af' entry to contain printer type, and optional location.
in the form 'type[.model][/smb/smb_dest]'. Use type 'ps' for no gs filter.

=item *

Reads first 1k and calls 'file' to determine filetype.

=item *

Builds a spool command based on filetype:

=over 4

=item *

If text and not postscript, use enscript text->postscript

=item *

If enscripted or postscript, use gs postscript->printer_format

=item *

Otherwise assumed to be native printer language (its your rope)

=back

=item *

Open pipe to spool command, send first 1k, then rest of data

=item *

requires ghostscript, enscript, and samba if printing to smb hosts

=back

=head1 Sample printcap entries

(Remember to create spool dir [sd])

=over 4

=item *

HP deskjet named 'leaves' connected to smb host 'tea'.
(using ghostscript 'hpdj' driver model 'unspec')

  leaves:\	
    :if=@PREFIX@/libexec/if-psprint:lf=/var/log/lpd-errs:\
    :sh:mx=0:lp=/dev/null:sd=/var/spool/lpd/leaves:\
    :af=hpdj.unspec/smb/tea/leaves:

=item *

Canon bubblejet connected to /dev/lpa0 (using gs 'bjc800' driver)

  bubbly:\	
    :if=@PREFIX@/libexec/if-psprint:lf=/var/log/lpd-errs:\
    :sh:mx=0:lp=/dev/lpa0:sd=/var/spool/lpd/bubbly:\
    :af=bjc800:

=back

=cut

$ENV{'PATH'}="@PREFIX@/bin:/usr/bin:/bin";

use strict;
use Getopt::Std;
use IPC::Open3;

my( $user,
    $dest,
    $spoolhost,
    $device,
    $model,
    %opt,
    );

# Parse options (ignore most)
#

&getopt('w:l:i:n:h:', \%opt);
$user = $opt{'n'};
$user ||= $ENV{'USER'};
$spoolhost = $opt{'h'};
if (!$spoolhost)
    { chomp($spoolhost = `hostname`); }

if (@ARGV != 1 || $ARGV[0] !~ m#(\w+)(\.(\w+)|)(/smb/.*/.*|)#)
    { &usage_and_exit; }
$device = $1;
$model = $3;
$dest = $4;
if ($dest)
    { $dest =~ s#/smb/#smb:/#; }

# Determine filetype
#

my($data, $filetype);
if (!read(STDIN, $data, 1024)) # initial filetype check data
    { &fail("No data to print"); }
$filetype = &filetype($data);

# Generate spool command
#

my($spool);
$spool = '';
if ($filetype =~ /^PostScript/ || $filetype =~ /text/)
    {
    if ($filetype !~ /^PostScript/)
	{ $spool .= '|'.filter_enscript(); }
    if ($model || $device ne 'ps')
	{ $spool .= '|'.filter_gs($device, $model); }
    }

if ($dest)
    { $spool .= "|smbspool smb://$dest 1 $user $spoolhost 1 -"; }

if ($spool eq '')
    { $spool = '>&STDOUT'; }

# Spool output
#
if (!open(OUTPUT, $spool))
    { &fail("Unable to run '$spool': $!"); }
print OUTPUT $data; # print initial filetype check data
while (read(STDIN, $data, 16*1024))
    { print OUTPUT $data; }
close(STDIN);
close(OUTPUT);
exit;

sub fail
    {
    print STDERR "if-psprint: @_\n";
    exit 1;
    }

sub filetype
    {
    my($data) = @_;
    my($pid, $filetype);

    unless ($pid = open3('WTRFH', 'RDRFH', 'ERRFH', 'file -b -'))
	{ &fail("Unable to run 'file': $!"); }
    print WTRFH $data;
    close(WTRFH);
    close(ERRFH);
    0 && close(ERRFH); # Pacify perl's -w
    chop($filetype = <RDRFH>);
    close(RDRFH);
    wait;
    $filetype;
    }

sub filter_enscript
    {
    my($filter);

    $filter = "enscript -q -B -p -";
    }

sub filter_gs
    {
    my($device, $model) = @_;
    my($filter);
    $filter = "gs -q -dBATCH -sDEVICE=$device";
    if (defined $model)
	{ $filter .= " -sModel=$model"; }
    $filter .= " -SOutputFile=- -";
    }

sub usage_and_exit
    {
    print "Usage: if-psprint [opts] gs_device[.gs_model]/smbdestination
[opts]
	-w width	
	-l lines	
	-i indent	
	-n user	
	-h host
";
    exit 1;
    }