From: intrigeri Date: Tue, 29 Nov 2005 10:09:04 +0000 (+0000) Subject: r3557@krups: intrigeri | 2005-11-15 14:41:32 +0100 X-Git-Tag: backupninja-0.9.2~57 X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fupstream%2Fbackupninja.git;a=commitdiff_plain;h=13149b96008227bb11582fa0e0d59b727fd8d1bd r3557@krups: intrigeri | 2005-11-15 14:41:32 +0100 Start to move common functions (maketemp) to lib/tools, and better use autotools for lib/{easydialog,tools}. --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 1a8ed4c..2b0e93a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,6 +1,14 @@ +EXTRALIBS = easydialog tools +dist_pkglib_DATA = $(EXTRALIBS) +EXTRA_DIST = easydialog.in tools.in -EXTRALIBRARIES = easydialog.sh +edit = sed \ + -e "s,@BASH\@,$(BASH),g" -EXTRA_DIST = Makefile.am $(EXTRALIBRARIES) +easydialog: easydialog.in + rm -f easydialog + $(edit) easydialog.in > easydialog -dist_pkglib_DATA = $(EXTRALIBRARIES) +tools: tools.in + rm -f tools + $(edit) tools.in > tools diff --git a/lib/Makefile.in b/lib/Makefile.in index d462ea8..67a1df5 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -111,9 +111,12 @@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ -EXTRALIBRARIES = easydialog.sh -EXTRA_DIST = Makefile.am $(EXTRALIBRARIES) -dist_pkglib_DATA = $(EXTRALIBRARIES) +EXTRALIBS = easydialog tools +dist_pkglib_DATA = $(EXTRALIBS) +EXTRA_DIST = easydialog.in tools.in +edit = sed \ + -e "s,@BASH\@,$(BASH),g" + all: all-am .SUFFIXES: @@ -285,6 +288,14 @@ uninstall-am: uninstall-dist_pkglibDATA uninstall-info-am pdf-am ps ps-am uninstall uninstall-am \ uninstall-dist_pkglibDATA uninstall-info-am + +easydialog: easydialog.in + rm -f easydialog + $(edit) easydialog.in > easydialog + +tools: tools.in + rm -f tools + $(edit) tools.in > tools # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. .NOEXPORT: diff --git a/lib/easydialog.in b/lib/easydialog.in new file mode 100644 index 0000000..59b0efa --- /dev/null +++ b/lib/easydialog.in @@ -0,0 +1,255 @@ +#!@BASH@ + +# copyright 2002 lmoore@tump.com under the terms of the GNU LGPL. +# additions 2005 collective@riseup.net + +# whiptail has trouble being called in the foo=$(whiptail ...) fashion for +# some reason. this is very annoying. this means that we need to use +# temporary files to store the answers from the input and list based boxes +# and then read the answers into a REPLY variable. that just really +# stinks, oh well, that's what you get when you have a weak link +# implementation... +# +# inputBox and passwordBox could be refactored to use a common function + +test -z "$WIDTH" && WIDTH=0 +test -z "$HEIGHT" && HEIGHT=0 +BACKTITLE="" +DIALOG=dialog +HELP= + +setApplicationTitle() { + BACKTITLE=$* +} + +setHelp() { + HELP="$@" +} + +setDimension() { + WIDTH=$1 + HEIGHT=$2 +} + +booleanBox() { + $DIALOG --backtitle "$BACKTITLE" --title "$1" \ + `[ "$3" == no ] && echo '--defaultno'` --yesno "$2" $HEIGHT $WIDTH +} + +msgBox() { + $DIALOG --backtitle "$BACKTITLE" --title "$1" \ + --msgbox "$2" $HEIGHT $WIDTH +} + +gaugeBox() { + $DIALOG --backtitle "$BACKTITLE" --title "$1" \ + --gauge "$2" $HEIGHT $WIDTH 0 +} + +inputBox() { + local temp=$(mktemp -t) || exit 1 + trap "rm -f $temp" 0 + REPLY= + $DIALOG --backtitle "$BACKTITLE" --title "$1" \ + --inputbox "$2" $HEIGHT $WIDTH "$3" 2> $temp + local status=$? + [ $status = 0 ] && REPLY=$(cat $temp) + rm -f $temp + return $status +} + +# Xdialog and {dialog,whiptail} use different mechanism to "qoute" the +# values from a checklist. {dialog,whiptail} uses standard double quoting +# while Xdialog uses a "/" as the separator. the slash is arguably better, +# but the double quoting is more standard. anyway, this function can be +# overridden to allow a derived implementation to change it's quoting +# mechanism to the standard double-quoting one. it receives two +# arguements, the file that has the data and the box type. +_listReplyHook() { + cat $1 +} + +# this is the base implementation of all the list based boxes, it works +# out nicely that way. the real function just passes it's arguments to +# this function with an extra argument specifying the actual box that +# needs to be rendered. +_genericListBox() { + local box=$1 + shift 1 + local title=$1 + local text=$2 + shift 2 + local temp=$(mktemp -t) || exit 1 + trap "rm -f $temp" 0 + REPLY= + $DIALOG $HELP $_DEFAULT --backtitle "$BACKTITLE" --title "$title" \ + $box "$text" $HEIGHT $WIDTH 10 \ + "$@" 2> $temp + local status=$? + [ $status = 0 ] && REPLY=$(_listReplyHook $temp $box) + rm -f $temp + _DEFAULT= + return $status +} + +setDefault() { + _DEFAULT="--default-item $1" +} + +menuBox() { + _genericListBox --menu "$@" +} + +## a menu box with additional help info displayed +## at the bottom of the window when an item is selected +menuBoxHelp() { + HELP="--item-help" + _genericListBox --menu "$@" + status=$? + HELP= + return $status +} + +## a menu box with an addition button 'help' +menuBoxHelpFile() { + HELP="--help-button" + _genericListBox --menu "$@" + status=$? + HELP= + return $status +} + +checkBox() { + _genericListBox --checklist "$@" +} + +radioBox() { + _genericListBox --radiolist "$@" +} + +textBox() { + $DIALOG --backtitle "$BACKTITLE" --title "$1" --textbox "$2" $HEIGHT $WIDTH +} + +passwordBox() { + local temp=$(mktemp -t) || exit 1 + trap "rm -f $temp" 0 + REPLY= + $DIALOG --backtitle "$BACKTITLE" --title "$1" \ + --passwordbox "$2" $HEIGHT $WIDTH 2> $temp + local status=$? + [ $status = 0 ] && REPLY=$(cat $temp) + rm -f $temp + return $status +} + + +######################################################### +## begin-item-display style lists +## +## these lists are built by calling fuctions multiple times. +## this can make it easier to build your list in a loop +## + +listBegin() { + _menu_title=$1 + _menu_msg=$2 + _menu_items=0 + _menu_text= + _menu_labels= + _menu_status= +} + +listItem() { + _menu_labels[$_menu_items]=$1 + _menu_text[$_menu_items]=$2 + _menu_status[$_menu_items]=$3 # available only for checklist + let "_menu_items += 1" +} + + +## +## takes one of: +## menu, checklist, radiolist +## +listDisplay() { + boxtype=$1 + local temp=$(mktemp -t) || exit 1 + trap "rm -f $temp" 0 + + local label + local text + local status + ( + echo -ne " $HELP $_DEFAULT " + echo -ne " --backtitle '$BACKTITLE' " + echo -ne " --title '$_menu_title' " + echo -ne " --$boxtype '$_menu_msg' " + echo -ne " $HEIGHT $WIDTH 10 " + for ((i=0; i < $_menu_items ; i++)); do + label=${_menu_labels[$i]} + text=${_menu_text[$i]} + status=${_menu_status[$i]} + echo -ne " $label '$text' $status " + done + ) | xargs $DIALOG 2> $temp + + local status=$? + REPLY="" + [ $status = 0 ] && REPLY=`cat $temp` + rm -f $temp + _DEFAULT= + return $status +} + +#################################################### +## FORM + +_form_gap=2 +formBegin() { + _form_title=$1 + _form_items=0 + _form_labels= + _form_text= +} + +formItem() { + _form_labels[$_form_items]=$1 + _form_text[$_form_items]=$2 + let "_form_items += 1" +} + +formDisplay() { + local temp=$(mktemp -t) || exit 1 + + max_length=0 + for ((i=0; i < ${#_form_labels[@]} ; i++)); do + label=${_form_labels[$i]} + length=`expr length $label` + if [ $length -gt $max_length ]; then + max_length=$length + fi + done + let "max_length += 2" + + local xpos=1 + ( + echo -n -e "--form '$_form_title' 0 0 20" + for ((i=0; i < $_form_items ; i++)); do + label=${_form_labels[$i]} + text=${_form_text[$i]} + echo -n -e " $label $xpos 1 '$text' $xpos $max_length 30 30" + let "xpos += _form_gap" + done + ) | xargs $DIALOG 2> $temp + local status=$? + + REPLY= + if [ $status = 0 ]; then + IFS=$'' + REPLY=`cat $temp` + IFS=$' \t\n' + fi + rm -f $temp + return $status +} diff --git a/lib/easydialog.sh b/lib/easydialog.sh deleted file mode 100644 index 0113d9c..0000000 --- a/lib/easydialog.sh +++ /dev/null @@ -1,255 +0,0 @@ -#!/bin/bash - -# copyright 2002 lmoore@tump.com under the terms of the GNU LGPL. -# additions 2005 collective@riseup.net - -# whiptail has trouble being called in the foo=$(whiptail ...) fashion for -# some reason. this is very annoying. this means that we need to use -# temporary files to store the answers from the input and list based boxes -# and then read the answers into a REPLY variable. that just really -# stinks, oh well, that's what you get when you have a weak link -# implementation... -# -# inputBox and passwordBox could be refactored to use a common function - -test -z "$WIDTH" && WIDTH=0 -test -z "$HEIGHT" && HEIGHT=0 -BACKTITLE="" -DIALOG=dialog -HELP= - -setApplicationTitle() { - BACKTITLE=$* -} - -setHelp() { - HELP="$@" -} - -setDimension() { - WIDTH=$1 - HEIGHT=$2 -} - -booleanBox() { - $DIALOG --backtitle "$BACKTITLE" --title "$1" \ - `[ "$3" == no ] && echo '--defaultno'` --yesno "$2" $HEIGHT $WIDTH -} - -msgBox() { - $DIALOG --backtitle "$BACKTITLE" --title "$1" \ - --msgbox "$2" $HEIGHT $WIDTH -} - -gaugeBox() { - $DIALOG --backtitle "$BACKTITLE" --title "$1" \ - --gauge "$2" $HEIGHT $WIDTH 0 -} - -inputBox() { - local temp=$(mktemp -t) || exit 1 - trap "rm -f $temp" 0 - REPLY= - $DIALOG --backtitle "$BACKTITLE" --title "$1" \ - --inputbox "$2" $HEIGHT $WIDTH "$3" 2> $temp - local status=$? - [ $status = 0 ] && REPLY=$(cat $temp) - rm -f $temp - return $status -} - -# Xdialog and {dialog,whiptail} use different mechanism to "qoute" the -# values from a checklist. {dialog,whiptail} uses standard double quoting -# while Xdialog uses a "/" as the separator. the slash is arguably better, -# but the double quoting is more standard. anyway, this function can be -# overridden to allow a derived implementation to change it's quoting -# mechanism to the standard double-quoting one. it receives two -# arguements, the file that has the data and the box type. -_listReplyHook() { - cat $1 -} - -# this is the base implementation of all the list based boxes, it works -# out nicely that way. the real function just passes it's arguments to -# this function with an extra argument specifying the actual box that -# needs to be rendered. -_genericListBox() { - local box=$1 - shift 1 - local title=$1 - local text=$2 - shift 2 - local temp=$(mktemp -t) || exit 1 - trap "rm -f $temp" 0 - REPLY= - $DIALOG $HELP $_DEFAULT --backtitle "$BACKTITLE" --title "$title" \ - $box "$text" $HEIGHT $WIDTH 10 \ - "$@" 2> $temp - local status=$? - [ $status = 0 ] && REPLY=$(_listReplyHook $temp $box) - rm -f $temp - _DEFAULT= - return $status -} - -setDefault() { - _DEFAULT="--default-item $1" -} - -menuBox() { - _genericListBox --menu "$@" -} - -## a menu box with additional help info displayed -## at the bottom of the window when an item is selected -menuBoxHelp() { - HELP="--item-help" - _genericListBox --menu "$@" - status=$? - HELP= - return $status -} - -## a menu box with an addition button 'help' -menuBoxHelpFile() { - HELP="--help-button" - _genericListBox --menu "$@" - status=$? - HELP= - return $status -} - -checkBox() { - _genericListBox --checklist "$@" -} - -radioBox() { - _genericListBox --radiolist "$@" -} - -textBox() { - $DIALOG --backtitle "$BACKTITLE" --title "$1" --textbox "$2" $HEIGHT $WIDTH -} - -passwordBox() { - local temp=$(mktemp -t) || exit 1 - trap "rm -f $temp" 0 - REPLY= - $DIALOG --backtitle "$BACKTITLE" --title "$1" \ - --passwordbox "$2" $HEIGHT $WIDTH 2> $temp - local status=$? - [ $status = 0 ] && REPLY=$(cat $temp) - rm -f $temp - return $status -} - - -######################################################### -## begin-item-display style lists -## -## these lists are built by calling fuctions multiple times. -## this can make it easier to build your list in a loop -## - -listBegin() { - _menu_title=$1 - _menu_msg=$2 - _menu_items=0 - _menu_text= - _menu_labels= - _menu_status= -} - -listItem() { - _menu_labels[$_menu_items]=$1 - _menu_text[$_menu_items]=$2 - _menu_status[$_menu_items]=$3 # available only for checklist - let "_menu_items += 1" -} - - -## -## takes one of: -## menu, checklist, radiolist -## -listDisplay() { - boxtype=$1 - local temp=$(mktemp -t) || exit 1 - trap "rm -f $temp" 0 - - local label - local text - local status - ( - echo -ne " $HELP $_DEFAULT " - echo -ne " --backtitle '$BACKTITLE' " - echo -ne " --title '$_menu_title' " - echo -ne " --$boxtype '$_menu_msg' " - echo -ne " $HEIGHT $WIDTH 10 " - for ((i=0; i < $_menu_items ; i++)); do - label=${_menu_labels[$i]} - text=${_menu_text[$i]} - status=${_menu_status[$i]} - echo -ne " $label '$text' $status " - done - ) | xargs $DIALOG 2> $temp - - local status=$? - REPLY="" - [ $status = 0 ] && REPLY=`cat $temp` - rm -f $temp - _DEFAULT= - return $status -} - -#################################################### -## FORM - -_form_gap=2 -formBegin() { - _form_title=$1 - _form_items=0 - _form_labels= - _form_text= -} - -formItem() { - _form_labels[$_form_items]=$1 - _form_text[$_form_items]=$2 - let "_form_items += 1" -} - -formDisplay() { - local temp=$(mktemp -t) || exit 1 - - max_length=0 - for ((i=0; i < ${#_form_labels[@]} ; i++)); do - label=${_form_labels[$i]} - length=`expr length $label` - if [ $length -gt $max_length ]; then - max_length=$length - fi - done - let "max_length += 2" - - local xpos=1 - ( - echo -n -e "--form '$_form_title' 0 0 20" - for ((i=0; i < $_form_items ; i++)); do - label=${_form_labels[$i]} - text=${_form_text[$i]} - echo -n -e " $label $xpos 1 '$text' $xpos $max_length 30 30" - let "xpos += _form_gap" - done - ) | xargs $DIALOG 2> $temp - local status=$? - - REPLY= - if [ $status = 0 ]; then - IFS=$'' - REPLY=`cat $temp` - IFS=$' \t\n' - fi - rm -f $temp - return $status -} diff --git a/lib/tools.in b/lib/tools.in new file mode 100644 index 0000000..89f2ff7 --- /dev/null +++ b/lib/tools.in @@ -0,0 +1,19 @@ +#!@BASH@ + + +# +# create a temporary file in a secure way. +# +function maketemp() { + if [ -x /bin/mktemp ] + then + local tempfile=`mktemp /tmp/$1.XXXXXXXX` + else + DATE=`date` + sectmp=`echo $DATE | /usr/bin/md5sum | cut -d- -f1` + local tempfile=/tmp/$1.$sectmp + fi + echo $tempfile +} + + diff --git a/src/ninjahelper.in b/src/ninjahelper.in index 7497b69..f0ad45a 100755 --- a/src/ninjahelper.in +++ b/src/ninjahelper.in @@ -215,7 +215,7 @@ if [ ! -d "$configdirectory" ]; then exit 1 fi -. $libdir/easydialog.sh +. $libdir/easydialog if [ "$UID" != "0" ]; then msgBox "warning" "ninjahelper must be run by root!"