#!/bin/ksh93 # Info: $0 -h typeset -r FPROG=${.sh.file} VERSION='1.1' typeset -r PROG=${FPROG##*/} SDIR=${FPROG%/*} SCRIPTDIR='/etc/libpaper.d' CFNAME='/etc/papersize' function showUsage { getopts -a ${PROG} "${ print ${USAGE} ; }" OPT --man } function validPaper { ${EXE} "$1" 2>/dev/null 1>&2 } # A linebreaker for lines with max. $1 characters. All remaining args are # append to the line[s] separated by a single space. function lineWrap { integer MAXWIDTH=$1 typeset PREFIX=' ' OUT= CLINE="${PREFIX}" W shift (( MAXWIDTH-- )) for W in "$@" ; do if (( (${#CLINE} + ${#W}) > MAXWIDTH )); then OUT+="${CLINE}\n" CLINE="${PREFIX}" fi CLINE+=" $W" done [[ ${CLINE} != ${PREFIX} ]] && OUT+="${CLINE}\n" print -n "${OUT}" } # assure that the conf file is created with right access perms (MP 13/05/2000) function setPaper { typeset PAPERRIGHT="${ ${EXE} "$1" 2>/dev/null ; }" X print "${PAPERRIGHT}" >"${PAPERCONF}" || return 2 chmod 644 "${PAPERCONF}" || return 3 # Added code for calling back applications when papersize changes. # See bug #345466 eppesuig@debian.org, 2006-01-05 if [[ -d ${PAPERDIR} ]]; then for X in ~(N)${PAPERDIR}/* ; do [[ -x $X && -f $X ]] && $( $X ) done fi [[ ${SCRIPTDIR} == ${PAPERDIR} || ! -d ${SCRIPTDIR} ]] && return 0 for X in ~(N)${SCRIPTDIR}/* ; do [[ -x $X && -f $X ]] && $( $X ) done } function doMain { typeset -x EXE="${SDIR%/*}/bin/paperconf" LESS if [[ ! -x ${EXE} ]] ; then EXE="${ whence paperconf ; }" if [[ -z ${EXE} ]]; then print -u2 'paperconf executable not found - exiting.' return 1 fi fi if [[ -n ${PAPER} ]]; then if ! validPaper "${PAPER}" ; then print -u2 "${PROG}: '${PAPER}' is not a known paper name" return 3 fi setPaper "${PAPER}" return 0 fi KNOWNPAPERS="${ ${EXE} -a ; }" PAPER="${ ${EXE} 2>/dev/null ; }" CURRENT="${PAPER}" DFTPAPER="${ ${EXE} -d ; }" if (( FORCE )) || [[ ! -e ${PAPERCONF} ]] || ! validPaper "${PAPER}" ; then PAPERS="${ lineWrap 72 ${KNOWNPAPERS} ; }" if [[ -x /usr/bin/pager ]]; then PAGER=/usr/bin/pager elif [[ -x /bin/more ]]; then PAGER=/bin/more else PAGER=cat fi LESS+=' -X -E' print ' The default (also known as system) paper can be chosen from many known papers that are currently recognized by programs using the libpaper library (with libpaper, paper names are case insensitive; if you use programs that use the system paper size but do not rely on the libpaper library, this may not be true and some of the papers listed below may not be known by these programs): '"${PAPERS}"' ' | ${PAGER} fi while true ; do [[ -z ${PAPER} ]] && PAPER="${DFTPAPER}" read ANS?"Default paper name? [${PAPER}] " [[ -z ${ANS} ]] && break validPaper ${ANS} && PAPER=${ANS} && break print -u2 ' Unknow paper "'"${ANS}"'", please choose one from the available papers list. ' done [[ ${CURRENT} == ${PAPER} ]] && grep -q "[# ]" "${PAPERCONF}" && return 0 setPaper "${PAPER}" } X="${SDIR%/*}" [[ $X == '/usr' ]] && X=/ typeset -r DEF_PAPERDIR="${X}${SCRIPTDIR}" DEF_PAPERCONF="${X}${CFNAME}" [[ -z ${PAPERDIR} ]] && PAPERDIR="${DEF_PAPERDIR}" if [[ -z ${PAPERCONF} ]]; then [[ -e ${CFNAME} ]] && PAPERCONF=${CFNAME} || PAPERCONF=${DEF_PAPERCONF} fi PAPER= PAPERSIZE= export PAPERSIZE PAPERCONF USAGE="[-?${VERSION} ]"' [-copyright?1996, Yves Arrouye ] [-copyright?2001, Adrian Bunk ] [-copyright?2015, Jens Elkner (Solaris/ksh93 port)] [-license?CDDL 1.0] [+NAME?'"${PROG}"' - configure the system default paper size] [+DESCRIPTION?\bpaperconfig\b sets the system (or default) paper to be used by tools using the \bpapersize\b file. It can either ask interactively for the paper to use or be called non-interactively by scripts.] [+?When the paper size has been changed, paperconfig runs all executable files (if any) in the \b'"${DEF_PAPERDIR}"'/\b directory, and \b'"${SCRIPTDIR}"'/\b unless they are the same.] [h:help?Print this help and exit.] [F:functions?Print a list of all functions available.] [T:trace]:[functionList?A comma separated list of functions of this script to trace (convinience for troubleshooting).] [+?] [v:version?Print version information and exit.] [p:paper]:[name?Set the paper \aname\a as default and exit.] [f:force?Force paper configuration.] [+ENVIRONMENT?]{ [+PAPERCONF?The full path to a file containing the paper size to use. If unset, \b'"${CFNAME}"'\b gets used if it exists, otherwise \b'"${DEF_PAPERCONF}"'\b.] } ' X="${ print ${USAGE} ; }" integer FORCE=0 while getopts "${X}" OPT ; do case ${OPT} in h) showUsage ; exit 0 ;; T) if [[ ${OPTARG} == 'ALL' ]]; then typeset -ft ${ typeset +f ; } else typeset -ft ${OPTARG//,/ } fi ;; F) typeset +f && exit 0 ;; v) print "${PROG} version ${VERSION}" ; exit 0 ;; p) PAPER="${OPTARG}" ;; f) FORCE=1 ;; esac done X=$((OPTIND-1)) shift $X && OPTIND=1 unset X doMain "$@" exit 0