Debian Codegraph @

filter/braille/filters/brftopagedbrf.in

raw
1 #!/bin/bash
2
3 #
4 # Copyright (c) 2015-2018 Samuel Thibault <[email protected]>
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 # THE SOFTWARE.
25 #
26
27 # Make sure we have enough options
28 if [ $# != 5 -a $# != 6 ]; then
29 echo "ERROR: $0 jobid user name nb options [filename]" >&2
30 exit 1
31 fi
32
33 NB=$4
34 OPTIONS=$5
35 FILE=$6
36
37 . @CUPS_DATADIR@/braille/cups-braille.sh
38
39 shopt -s extglob
40
41 # Construct list of pages from PAGERANGES
42
43 PAGES=" " # Explicit list of pages
44 AFTER= # All pages including and after this
45 BEFORE= # All pages before and including this
46
47 [ -n "$PAGERANGES" ] || PAGERANGES="1-"
48
49 while [ -n "${PAGERANGES}" ]
50 do
51 PAGERANGE=${PAGERANGES/,*}
52 PAGERANGE=${PAGERANGE%%*( )}
53 PAGERANGE=${PAGERANGE##*( )}
54 if [ -n "${PAGERANGES/*,*}" ]
55 then
56 # last range
57 PAGERANGES=""
58 else
59 # Remove leading range
60 PAGERANGES="${PAGERANGES#*,}"
61 fi
62
63 if [ -n "${PAGERANGE/*-*}" ]
64 then
65 # single-page
66 PAGES="$PAGES$PAGERANGE "
67
68 elif [ -z "${PAGERANGE%%*-}" ]
69 then
70 # To the end
71 FIRST=${PAGERANGE%%-*}
72 if [ -z "$AFTER" ] || [ "$FIRST" -lt "$AFTER" ]
73 then
74 AFTER="$FIRST"
75 fi
76
77 elif [ -z "${PAGERANGE##-*}" ]
78 then
79 # From the beginning
80 LAST=${PAGERANGE##*-}
81 if [ -z "$BEFORE" ] || [ "$LAST" -gt "$BEFORE" ]
82 then
83 BEFORE="$LAST"
84 fi
85
86 else
87 # page range
88 FIRST=${PAGERANGE/-*}
89 LAST=${PAGERANGE/*-}
90 PAGES="$PAGES$(seq "$FIRST" "$LAST" | tr $'\012' ' ')"
91
92 fi
93 done
94
95 # Determine whether to print this page
96 doprint() {
97 PAGE="$1"
98 if [ -n "$BEFORE" ] && [ "$PAGE" -le "$BEFORE" ]
99 then
100 echo 1
101 return
102 elif [ -n "$AFTER" ] && [ "$PAGE" -ge "$AFTER" ]
103 then
104 echo 1
105 return
106 fi
107 case "$PAGES" in
108 *\ $PAGE\ *)
109 echo 1
110 return
111 ;;
112 esac
113 echo 0
114 }
115
116 if [ -z "$FILE" ]
117 then
118 cat
119 else
120 cat "$FILE"
121 fi | (
122 P=1
123 DOPRINT=$(doprint $P)
124 IFS=$'\n' read -r LINE
125 EOF=$?
126 while [ "$EOF" = 0 -o -n "$LINE" ]
127 do
128 while [ -n "$LINE" -a -z "${LINE/*$'\014'*}" ]
129 do
130 # Possibly print before FF
131 HEAD=${LINE%%$'\014'*}
132 [ $DOPRINT == 0 ] || printf %s "$HEAD"$'\014'
133
134 # Next page
135 P=$(($P + 1))
136 DOPRINT=$(doprint $P)
137
138 # Drop head of line
139 LINE=${LINE#*$'\014'}
140 done
141
142 # Remainder of line
143 if [ "$EOF" = 0 ]
144 then
145 NL="\n"
146 else
147 NL=""
148 fi
149 [ $DOPRINT == 0 ] || printf "%s$NL" "$LINE"
150
151 IFS=$'\n' read -r LINE
152 EOF=$?
153 done
154 )
155
156 echo "INFO: Ready" >&2
157 exit 0