summaryrefslogtreecommitdiff
path: root/obsolete/support/prv/prvps
blob: 230d8aed9b523d376f9f3b74db43bcaa2cae6dd1 (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
#!/usr/bin/perl -w
use Env;
use Config;
use Term::ReadLine;

=head1 NAME

prvps - preview postscript document and eventually print it

=head1 SYNOPSIS

system call:

    prvps [<filename>]

=head1 DESCRIPTION

B<prvps> previews PostScript documents and eventually
prints them, letting you choose the pages and the output
format. Without a F<filename>, standard input is used. 
If a file with F<filename> does not exist, F<filename.eps> is tried or,
if that also does not exist, F<filename.ps>.
B<prvps> displays your Postscript document
using B<gv>, and then interrogates you about the pages you
want to print. To that end the following prompt appears:

Pages ([range[,range...]|a] [b|t] [x<n>])? 

You can react to this as in the following examples:

    5       # to print page 5
    5-      # to print pages 5 through the end
    5-7     # to print pages 5, 6 and 7
    -7      # to print the first 7 pages
    5-7,19- # to print pages 5, 6, 7 and 19 through the end
    a       # to print the whole document
    a x3    # to print 3 copies of the document
    x3      # the same
    5 x3    # to print 3 copies of page 5
    t       # print the whole document twosided
    t 2-    # print twosided starting at page 2
    b       # to print the whole document as an a5 size booklet
    b -12   # to print the first 12 pages as an a5 size booklet

If you use b to print a booklet, or t to print twosided, then you will
have to arrange your printer such that, with the printed sides up, the
first page printed will be at the bottom of the stack, and the last
page printed will be on top. Normally you will then have your output
come out the back of your printer.

B<prvps> will first print one side of the
paper and prompt you for a return when the printer is ready and you
have turned the stack and put it back in the paper feed.

Finally, if you used the b option to print a booklet, you will have to 
reshuffle your pages manually as follows:
- take the bottom page from the stack and put it aside without
  changing its position
- take the top page from the stack and put it upside down on the
  bottom page
- repeat these two actions until the stack is empty
- if you find out how to prevent this manual action, please tell me how
  you did it!! (wybo@servalys.hobby.nl)

=cut

$term=new Term::ReadLine 'test';

$TMP='/tmp' unless $TMP;
$in    = "$TMP/$$.in";    # temp file for saving standard input
$sel   = "$TMP/$$.sel";   # temp file for selected pages
$front = "$TMP/$$.front"; # temp file for front pages
$back  = "$TMP/$$.back";  # temp file for back pages
$print = $Config{'lpr'} or $print='lpr';
$booklet=$selection=$lpropt=$doit="";
$prompt='Pages ([range[,range...]|a] [b|t] [x<n>])? ';
$print_prompt="printer ready? then turn pack and type return ";

$file=shift or $file="";	# empty -> stdin
if ($file) { 	# if a filename was given
  if (! -f $file) { # and it does not exist
    if (-f "$file.ps") { $file.=".ps" } 
    elsif (-f "$file.eps") { $file.='.eps' }
    else { die "no file $file.eps or $file.ps found\n" }
  }
} else {
  $file=$in;
  open(OUT,">$in") or die("Could not open $file");
  while(<>) { print OUT }; close OUT;
}

# look in the first non-empty line to check if it is right: 
open(IN,$file); do { chomp($_=<IN>) } until $_;  close IN;                 

/^%!PS/ or die("Input file is not a PostScript file\n");

system "gv -scale 2 -spartan $file";


$front_spec='"4:-3L@.707(210mm,0)+0L@.707(210mm,148.5mm)"';
$back_spec='"4:1L@.707(210mm,0)+-2L@.707(210mm,148.5mm)"';
while (1) {
    if ($selection) {   # if anything was set
	print STDERR "\n";
        $selection =~ /[1-9]/ or $selection.=" -e -o ";
        system("psselect -q $selection $file $sel");
        if ($booklet) {
            system("pstops -q $front_spec $sel >$front");
            system("$print $lpropt $front");
            $term->readline($print_prompt);
            system("pstops -q $back_spec $sel >$back");
            system("$print $lpropt $back");
        } elsif ($twosided) {
            system("pstops 2:0 $sel >$front");
            system("$print $lpropt $front");
            $term->readline($print_prompt);
            system("pstops 2:1 $sel >$back");
            system("$print $lpropt $back");
        } else {
            system("$print $lpropt $sel");
        }
    }
    $booklet=$twosided=$selection=$lpropt=$doit="";

    $com=$term->readline($prompt);

    for (split(/\s+/,$com)) {
        CASE: {
            /^q$/                       # die on n of q
                and goto READY;		# don't just exit: may be called with do
            /^x[0-9]+$/                 # x3 -> lpr -#3
                and do { s/x//;
			 print STDERR " $_ copies";
		         $lpropt = '-#'.$_;
                         $selection.=" -q";
                },
                last CASE;
            /^b$/                       # print a5 booklet
                and do { $selection.=" -q ";
		         print STDERR " booklet";
		         $booklet=1;
		       },
                last CASE;
            /^t$/                       # print twosided
                and do { $selection.=" -q";
		         $twosided=1;
		         print STDERR " twosided";
		       },
                last CASE;
            /^a$/                       # print all
                and do { $selection="-e -o";
		         print STDERR " all pages";
                },
                last CASE;
            /^(([0-9]+-?[0-9]*|[0-9]*-?[0-9]+),?)+$/    # n-m or n- or -m
                and do { ($selection=$_) =~ s/,$//;
		         print STDERR " pages: $selection";
                },
                last CASE;

             warn 
             "Illegal specification(s)\n".
             "Examples of page specifications:\n".
             "5       to print page 5\n".
             "5-      to print pages 5 through the end\n".
             "5-7     to print pages 5, 6 and 7\n".
             "-7      to print the first 7 pages\n".
             "5-7,19- to print pages 5, 6, 7 and 19 through the end\n".
             "a       to print the whole document\n".
             "a x3    to print 3 copies of the document\n".
             "x3      the same\n".
             "5 x3    to print 3 copies of page 5\n".
             "t       print the whole document twosided\n".
             "t 2-    print twosided starting at page 2\n".
             "b       to print the whole document as an a5 size booklet\n".
	     "b -12   to print the first 12 pages as an a5 size booklet\n";
                  
         }
    }
}
READY:
for (glob "/tmp/$$*") { unlink $_ }