summaryrefslogtreecommitdiff
path: root/dviware/dvipsconfig/addpsctrl
blob: ddc04cc054c1b19d739934788fef82d7460fe4ac (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/bin/bash
#
# addpsctrl
#
# Adds printer control commands to the given PostScript data.
# Currently requires bash - sorry but I have no time to support too many
# dumb shells (like Solaris /bin/sh). The korn shell (ksh) would be suitable,
# but the syntax is not the same as that of bash. bash is free - ksh loses out.
# Works with bash 1.x and 2.x.
#
# Copyright (C) by Volker Kuhlmann <v.kuhlmann@elec.canterbury.ac.nz>
# Copyright (C) by Volker Kuhlmann <VolkerKuhlmann@gmx.de>
# Released under the terms of the GNU General Public License (GPL) Version 2.
# See http://www.gnu.org/ for details.
#
# Volker Kuhlmann
#   5, 6, 12 Apr; 2 Aug 2000
#   26 Jan 2006
#

VERSION="VK 1.3, 26 Jan 2006"


####
#### Constants and initial variables
#
# Use kpsewhich:
ADDPSCTRL_DFLTOPT="${ADDPSCTRL_DFLTOPT:=--kpsewhich}"
# If you don't have kpsewhich, or would like to load the config files from a
# different directory, give the directory with the config files by setting
# the environment variable ADDPSCTRL_DFLTOPT:
#ADDPSCTRL_DFLTOPT="${ADDPSCTRL_DFLTOPT:=--nokpsewhich --dir YOURPATH}"


####
#### Usage
#
show_usage() {
    echo "
Usage: ${0##*/} [OPTION]... [FILE] [CTRL]...
Version $VERSION
"
}
usage() {
    show_usage
    echo "Call with -h for help.
"
    exitwith ErrUsage
}

####
#### Help
#
Help() { show_help; exitwith ErrHelp; }
show_help() {
    show_usage
    echo "\
Inserts printer control instructions (e.g. duplex) into a PostScript file.
OPTIONS:
  -h, --help          show help
  -k, --kpsewhich     use kpsewhich to locate config files if necessary
  -K, --nokpsewhich   never use kpsewhich
  -f, --file FILE     insert control commands into file FILE ('-' for stdin)
  -d, --dir DIR       control files are in directory DIR
  -c, --ctrl CTRL     locate + insert control file 'config.CTRL'
  -i, --insert INSRT  insert file INSRT before any other control files
                        (give full path and filename)
  -o, --output OUT    write to file OUT instead of stdout
  -V, --version       display version and exit

  -s, -1      same as -c simplex
  -2L         same as -c duplong
  -2S         same as -c dupshort
  -a4, -a3    same as -c a4 / -c a3
  -man        same as -c manualfeed

If -f is not used, the first non-option argument is taken as FILE.
Remaining arguments are names of control files to be inserted (after those 
given with -i and -c).
Control file searching:
  1)  In directory DIR (if given)
  2)  By running kpsewhich, unless disabled with -K
Default (can be changed by setting environment variable ADDPSCTRL_DFLTOPT):
  $ADDPSCTRL_DFLTOPT
"
}

####
#### Error/Exit codes
#
exitwith() {
    exec 1>&2  # write stdout on stderr instead
  case "$1" in
    ErrUsage|ErrHelp)
	# usage already displayed
	exit 1;;
    ErrNoread)
    	echo "Config/header file not readable: '$2'."
	exit 2;;
    ErrNoconf)
    	echo "Can not locate config/header file '$2'."
	exit 3;;
    ErrNodatafile)
    	echo "No file specified to insert control commands into."
	echo "Call with -h for help on command argument syntax."
	exit 9;;
    ErrNoinsfile)
    	echo "No control commands specified to insert."
	echo "Call with -h for help on command argument syntax."
	exit 9;;
    ErrMissingParameter)
	echo "A required parameter for option $2 is missing."
	echo "Call with -h for help."
	exit 9;;
    *)
	echo "Error: exitwith() was called with illegal error code '$1'."
	exit 19;;
  esac
}

####
#### Parse command line parameters
#
checkargmm() {
    test "$1" != "--" -a "$2" != "--"
    #true  # uncomment this to allow "--" as argument to an option
}
checkarg2() {
    test $# -ge 2 && checkargmm "$2" && return
    exitwith ErrMissingParameter "$1"
}
checkarg3() {
    test $# -ge 3 && checkargmm "$2" "$3" && return
    exitwith ErrMissingParameter "$1"
}
parse_cmd_line() {
    local cmdline arg
    unset -v debug usekpsewhich configdir insfile datafile outfile ctrl
    set -- $ADDPSCTRL_OPT "$@"  # pre-init options
    while [ "$1" ]; do
	#test $debug && echo "Current arg: $1"
	arg="${1#-}"
	test "$arg" = "$1" && break  # does word start with "-"?
	test "$arg" = "-" && { shift; break; }
	# allow both "-long" and "--long" for long options:
	arg="${arg#-}"
	case "$arg" in
	    #debug)	debug=1;;
	    usage) 	usage;;
	    V|version) 	usage;;
	    h|help) 	Help;;

    	    k|kpsewhich)    usekpsewhich=1;;
    	    K|nokpsewhich)  usekpsewhich="";;
	    
	    d|dir)  	    checkarg2 "$@"; configdir="$2"; shift;;
    	    i|insert)	    checkarg2 "$@"; insfile="$2"; shift;;
    	    f|file)	    checkarg2 "$@"; datafile="$2"; shift;;
    	    o|output)	    checkarg2 "$@"; outfile="$2"; shift;;
	    
    	    c|ctrl) 	checkarg2 "$@"; ctrl="$2"; shift;;
	    s|1)    	ctrl=simplex;;
	    2L)     	ctrl=duplong;;
	    2S)     	ctrl=dupshort;;
	    a4|a3)  	ctrl="$arg";;
	    man)    	ctrl="manualfeed";;

	    "")     break;;  # allow "-" as file arg
	    *)	    exitwith ErrBadoption "$1";;
	esac
	shift
    done
    numfileargs=$#
}

locate_config_file() {
    insfile=""
    test -n "$configdir" && insfile="$configdir/config.$1"
    if [ -n "$usekpsewhich" -a \( -z "$insfile" -o ! -r "$insfile" \) ]; then
	insfile="`kpsewhich --format 'dvips config' config.$1`"
    fi
    test -z "$insfile" && exitwith ErrNoconf "config.$1"
    test -r "$insfile" || exitwith ErrNoread "$insfile"
}

insert_into_ps() {
    # 0: start
    # 1: !% found
    # 2: %% found
    # 3: insertion done
    in="$1"; shift
    #echo \
    AWK '\
	BEGIN {n = 0; ins="'"$*"'"} \
	n == 3 {print $0; next} \
	/^%%EndSetup/ || /^%%Page:/ || ((n == 1) && (! /^[% ]/) && (! /^$/)) { \
	    if ((n == 1) || (n == 2)) { \
		n = 3; \
		while ((getline line <ins) > 0) { print line } \
	    }} \
	/^%\!/ {if (n == 0) {n = 1}} \
	/^%%/ {if (n == 1) {n = 2}} \
	{print $0} \
	END {if (n < 3) { \
	    while ((getline line <ins) > 0) { print line } } } \
	' \
	"$in"
    # gawk 3.1.4 doesn't understand   while ("cat " ins | getline
    # and tries to run ins as a command. -VK26Jan06
}

####
#### Main

if [ $# -eq 0 ]; then usage; fi

ADDPSCTRL_OPT="${ADDPSCTRL_OPT:-$ADDPSCTRL_DFLTOPT}"

# find a useful awk (solaris awk is not)
p="`type -p gnuawk gawk nawk | head -1`" && eval 'AWK() { '"${p:-awk}"' "$@"; }'

parse_cmd_line "$@"
#echo $#, $numfileargs, `expr $# - $numfileargs`
shift `expr $# - $numfileargs`

#echo fileargs: "$@"
#echo "opts=$ADDPSCTRL_OPT;; use=$usekpsewhich"
#echo "ctrl=$ctrl, insfile=$insfile, dir=$configdir, out=$outfile"

if [ -z "$datafile" ]; then
    test -z "$1" && exitwith ErrNodatafile
    datafile="$1"; shift
fi
#echo "datafile=$datafile"

insfiles=""
if [ -n "$insfile" ]; then
    test -r "$insfile" || exitwith ErrNoread "$insfile"
    insfiles="$insfile"
fi
for c in "$ctrl" "$@"; do
    test -z "$c" && continue
    #echo locate_config_file "$c"
    locate_config_file "$c"
    #echo "insfile=$insfile"
    insfiles="$insfiles $insfile"
done
#echo "insfiles=$insfiles"
test -z "$insfiles" && exitwith ErrNoinsfile

if [ -z "$outfile" ]; then
    #echo insert_into_ps "$datafile" $insfiles
    insert_into_ps "$datafile" $insfiles
else
    insert_into_ps "$datafile" $insfiles >"$outfile"
fi