2015年10月2日金曜日

FreeBSDでメールやテキストメモをPostScriptプリンタに印刷する

FreeBSDでメールやテキストメモをPostScriptプリンタに印刷するツールとして、古くからの友人である歌代さんのa2ps.plを使っていました。
ftp://ftp.sra.co.jp/pub/lang/perl/scripts/utashiro-scripts/
http://utashiro.hatenablog.com/
ただ、作られたのが古いため、新しいバージョンのperlでは改造なしに動かすことが難しくなってきました。

いろいろと調べてみたところ、e2psが代替になりそうです。
ubuntu u2psの代わりにe2psを使う
http://arakanoj.com/ubuntu-u2ps%E3%81%AE%E4%BB%A3%E3%82%8F%E3%82%8A%E3%81%ABe2ps%E3%82%92%E4%BD%BF%E3%81%86-3169.html
a2psの代わりにe2ps
http://hgw09.exblog.jp/21925627
さっそく、/usr/port/japanese/e2psでmake installとして、使ってみました。
$ pkg info ja-e2ps
ja-e2ps-4.34
Name           : ja-e2ps
Version        : 4.34
Installed on   : Fri Oct  2 07:32:29 JST 2015
Origin         : japanese/e2ps
Architecture   : freebsd:9:x86:64
Prefix         : /usr/local
Categories     : print japanese
Licenses       :
Maintainer     : ports@FreeBSD.org
WWW            : UNKNOWN
Comment        : Text file to postscript converter (with Japanese support)
Options        :
        DOCS           : on
Annotations    :
Flat size      : 77.7KiB
Description    :
E2ps formats each named file for printing in a postscript printer; if
no file is given, e2ps reads from the standard input. The format used
is nice and compact: normally two pages on each physical page, headers
with useful information (page number, printing date, file name or
supplied header), etc.
e2psというぐらいなので、日本語のコードとしてはEUCしか対応してません。そこで、シェルスクリプトを使い、オプションはe2psのままで入力をnkfコマンドで変換するようなコマンドを作りました。
#!/bin/sh
E2PS=/usr/local/bin/e2ps
LPT=${PRINTER-lp}

while [ $# -ge 1 ]
do
    case "$1" in
        '-l'|'-l2'|'-l4'|'-lv4'|'-p'|'-p2'|'-p4'|'-pv4'|'-a7'|'-b4'|\
            '-le'|'-ohp'|'-box'|'-nh'|'-e'|'-j')
            ARGS="$ARGS $1"
            shift
            ;;
        '-af'|'-abf'|'-abi'|'-siz'|'-afw'|'-afh'|'-kfw'|'-kfh'|\
            '-ls'|'-line'|'-tm'|'-bm'|'-lm'|'-rm'|'-date'|'-page' )
            ARGS="$ARGS $1"
            ARGS="$ARGS $2"
            shift 2
            ;;
        '-head' )
            HEADER="$2"
            shift 2
            ;;
        '-P' )
            LPT="$2"
            shift 2
            ;;
        '-h'|'--help'|'-v'|'--version'|'-fl'|'-lib' )
            $E2PS "$1"
            break
            ;;
        -*)
            echo "Unknown option: $1"
            break
            ;;
        *)
            FILES="$FILES $1"
            shift
            ;;
    esac
done

if [ $FILES ]; then
    for FILE in $FILES
    do
        if [ "$HEADER" ]; then
            nkf -e $FILE | $E2PS $ARGS -P $LPT -head "$HEADER" > /dev/null 2>&1
        else
            nkf -e $FILE | $E2PS $ARGS -P $LPT -head "$FILE" > /dev/null 2>&1
        fi
    done
else
        if [ "$HEADER" ]; then
            nkf -e | $E2PS $ARGS -P $LPT -head "$HEADER" > /dev/null 2>&1
        else
            nkf -e | $E2PS $ARGS -P $LPT -head "$FILE" > /dev/null 2>&1
        fi
fi