home *** CD-ROM | disk | FTP | other *** search
- # lstcommon - lst functions shared between several scripts
- #
- # Version: 0.7
- # Last changed: 12.05.94
- #
- # Copyright (c) Ralf Flaxa, Linux Support Team Erlangen, 1993, 1994
- #
- # Supported Constants:
- # --------------------
- CLEAR='c'
- REVON=''
- REVOFF=''
- BEEPTONE=''
- MSG=/tmp/.message
- ERRMSG=/tmp/.error
- PROGNAME=lstcommon
- FUNCTNAME=
- NULLDEVICE=/dev/null
- # TEXTCONSTANTS=/usr/lib/LST/libLSTstrings
- PAGER=more
- # USEDIALOG=true
-
- # String Constants:
- # -----------------
- # ein erster Versuch fⁿr Multi-Language Support ...
- # nur partiell verwirklicht
- # . $TEXTCONSTANTS
-
- #
- # Functions
- #
-
- debug()
- {
- if [ "$DEBUG" != "" ]; then
- echo "debug: $@"
- fi
- }
-
- # DoPanic ( panicmessage )
- #
- # Displays panic message.
- # uses environment variables PROGNAME and FUNCTNAME
- #
- DoPanic()
- {
- echo "$PROGNAME: panic: $FUNCTNAME: $@"
- }
-
- # DoLog ( [-d] [-n] [-f LOGFILE] text_to_log )
- #
- # Log a string for logging or postponed display
- #
- DoLog()
- {
- export FUNCTNAME=DoLog
- # defaults
- deleteflag=
- appendflag=
- logfile=$MSG
-
- # parse loop
- while [ 1 ]; do
- if [ $# -gt 0 ]; then
- # Test for options
- # We need to protect the "-" because
- # otherwise ash interprets them as tests :-(
- if [ "X$1" = 'X-d' ]; then
- deleteflag=true
- shift 1
- elif [ "X$1" = 'X-n' ]; then
- appendflag=true
- shift 1
- elif [ $# -gt 1 -a "X$1" = 'X-f' ]; then
- logfile=$2
- shift 2
- else
- # no more options, so the rest is message
- break
- fi
- else
- DoPanic "internal error: empty message passed to DoLog"
- return 1
- fi
- done
-
- # test if we must delete the old logfile
- if [ "$deleteflag" != "" ]; then
- rm -f "$logfile"
- fi
- if [ "$appendflag" != "" ]; then
- echo -n "$@" >> $logfile
- else
- echo "$@" >> $logfile
- fi
- }
-
- # Obsolete function ! Will be replaced by DoLog !
- #
- # LogMsg ( [-new] [-n] text_saved_for_postponed_display )
- #
- # Collect Text for postponed display
- #
- LogMsg()
- {
- export FUNCTNAME=LogMsg
- deleteflag=
- appendflag=
- if [ $# -gt 0 -a "$1" = '-new' ]; then
- deleteflag='-d'
- shift 1
- fi
- if [ $# -gt 0 -a "$1" = '-n' ]; then
- appendflag='-n'
- shift 1
- fi
- DoLog $deleteflag $appendflag -f $MSG "$@"
- }
-
- # Obsolete function ! Will be replaced by DoLog !
- #
- # LogErr ( [-new] [-n] error_message_saved_for_postponed_display )
- #
- # Collect Error Message for postponed display
- #
- LogErr()
- {
- export FUNCTNAME=LogErr
- deleteflag=
- appendflag=
- if [ $# -gt 0 -a "$1" = '-new' ]; then
- deleteflag='-d'
- shift 1
- fi
- if [ $# -gt 0 -a "$1" = '-n' ]; then
- appendflag='-n'
- shift 1
- fi
- DoLog $deleteflag $appendflag -f $ERRMSG "$@"
- }
-
- # FormatMessage ( ? options ? message_to_format )
- #
- # truncates message for use with dialog
- #
- FormatMessage()
- {
- export FUNCTNAME=FormatMessage
- cat $1 | cut -c 1-70 > $1.new
- mv $1.new $1
- zeilen=`wc $1 | cut -c 1-7 | sed 's/ //g' `
- if [ $zeilen -gt 15 ]; then
- zeilen=20
- else
- zeilen=`expr $zeilen + 5`
- fi
- export zeilen
- }
-
- # Seen ()
- #
- # display $MSG and wait for keypress
- #
- Seen()
- {
- export FUNCTNAME=Seen
- if [ ! -f $MSG ]; then
- touch $MSG
- fi
- if [ "$USEDIALOG" = "true" ]; then
- FormatMessage $MSG
- dialog --msgbox "`cat $MSG`" $zeilen 75
- else
- cat $MSG
- echo
- echo -n "$MKEYPRESS"
- read dummy
- fi
- rm -f $MSG
- }
-
- # ShowInfo ()
- #
- # display $MSG without paging or prompting
- #
- ShowInfo()
- {
- export FUNCTNAME=ShowInfo
- if [ ! -f $MSG ]; then
- touch $MSG
- fi
- if [ "$USEDIALOG" = "true" ]; then
- FormatMessage $MSG
- dialog --infobox "`cat $MSG`" $zeilen 75
- else
- cat $MSG
- fi
- rm -f $MSG
- }
-
- # Obsolete Function ! Will be replaced by ShowInfo
- #
- # Message ( )
- #
- Message()
- {
- export FUNCTNAME=Message
- ShowInfo $@
- }
-
- # PageMessage ()
- #
- # use $PAGER for paging in $MSG
- #
- PageMessage()
- {
- export FUNCTNAME=PageMessage
- if [ ! -f $MSG ]; then
- touch $MSG
- fi
- if [ "$USEDIALOG" = "true" ]; then
- FormatMessage $MSG
- dialog --textbox $MSG $zeilen 75
- if [ $? = 255 ]; then
- exit
- fi
- else
- cat $MSG | $PAGER
- echo
- echo -n "$MKEYPRESS"
- read dummy
- fi
- rm -f $MSG
- }
-
-
- # Error ( [error_message] )
- #
- # Beep and output an error message
- #
- Error()
- {
- export FUNCTNAME=Error
- if [ $# -gt 0 ]; then
- echo "$@" >> $ERRMSG
- fi
- if [ ! -f $ERRMSG ]; then
- touch $ERRMSG
- fi
- echo -n $BEEPTONE
- if [ "$USEDIALOG" = "true" ]; then
- FormatMessage $ERRMSG
- dialog --title "$MERROR" --msgbox "`cat $ERRMSG `" `expr $zeilen + 3 ` 75
- else
- echo "$REVON $MERROR !"
- cat $ERRMSG
- echo $REVOFF
- Seen
- fi
- rm -f $ERRMSG
- }
-
- # Warning ( [warning_message] )
- #
- # Beep and output a warning message
- #
- Warning()
- {
- export FUNCTNAME=Warning
- if [ $# -gt 0 ]; then
- echo "$@" >> $ERRMSG
- fi
- if [ ! -f $ERRMSG ]; then
- touch $ERRMSG
- fi
- echo -n $BEEPTONE
- if [ "$USEDIALOG" = "true" ]; then
- FormatMessage $ERRMSG
- dialog --title "Warnung" --infobox "`cat $ERRMSG `" $zeilen 75
- else
- echo "$REVON $MWARNING !"
- cat $ERRMSG
- echo $REVOFF
- fi
- rm -f $ERRMSG
- }
-
- # Askyesno( [-default DEFANSWER] )
- #
- # Just ask a yes/no question (j/n)
- #
- Askyesno()
- {
- export FUNCTNAME=Askyesno
- export ANSWER=""
- export DEFANSWER=
- if [ ! -f $MSG ]; then
- touch $MSG
- fi
- if [ $# -gt 1 -a "$1" = "-default" ]; then
- DEFANSWER=$2
- shift 2
- fi
- if [ "$USEDIALOG" = "true" ]; then
- if [ "$DEFANSWER" != "" ]; then
- echo "Vorschlag: $DEFANSWER" >> $MSG
- fi
- FormatMessage $MSG
- else
- echo -n "Eingabe ($YES/$NO)" >> $MSG
- if [ "$DEFANSWER" != "" ]; then
- echo -n " [$DEFANSWER]" >> $MSG
- fi
- echo -n ": " >> $MSG
- fi
- while [ 0 ]; do
- if [ "$USEDIALOG" = "true" ]; then
- dialog --yesno "`cat $MSG`" `expr $zeilen + 3 ` 75
- if [ $? = 0 ]; then
- ANSWER="$YES"
- else
- ANSWER="$NO"
- fi
- else
- echo $CLEAR
- cat $MSG
- read ANSWER
- fi
- if [ "$ANSWER" = "$YES" -o "$ANSWER" = "$NO" ]; then
- break
- elif [ "$DEFANSWER" != "" -a "$ANSWER" = "" ]; then
- ANSWER=$DEFANSWER
- break
- fi
- Warning "$MINPUT $MINVALID"
- done
- export ANSWER
- rm -f $MSG
- return 0
- }
-
- #
- # Askstring( [-emptyvalid] [ -default DEFANSWER] )
- #
- # Just ask for a non-null string
- #
- Askstring()
- {
- export FUNCTNAME=Askstring
- export EMPTYVALID=false
- export DEFANSWER=
- export STRING=
- if [ ! -f $MSG ]; then
- touch $MSG
- fi
- while [ 1 ]; do
- if [ $# = 0 ]; then
- break
- elif [ "$1" = "-emptyvalid" ]; then
- EMPTYVALID=true
- shift 1
- elif [ $# -gt 1 -a "$1" = "-default" ]; then
- DEFANSWER=$2
- shift 2
- else
- DoPanic "Askstring mit unbekannten Argumenten aufgerufen "
- fi
- done
- if [ "$USEDIALOG" = "true" ]; then
- if [ "$DEFANSWER" != "" ]; then
- echo "Vorschlag: $DEFANSWER" >> $MSG
- fi
- FormatMessage $MSG
- else
- echo -n "Eingabe" >> $MSG
- if [ "$DEFANSWER" != "" ]; then
- echo -n " [$DEFANSWER]" >> $MSG
- fi
- echo -n ": " >> $MSG
- fi
- while [ 0 ]; do
- if [ "$USEDIALOG" = "true" ]; then
- dialog --inputbox "`cat $MSG`" `expr $zeilen + 3 ` 75 2> /tmp/ANSWER
- STRING="`cat /tmp/ANSWER`"
- else
- echo $CLEAR
- cat $MSG
- read STRING
- fi
- if [ "$STRING" = "" ]; then
- if [ "$DEFANSWER" != "" ]; then
- STRING=$DEFANSWER
- break
- elif [ "$EMPTYVALID" = "true" ]; then
- STRING=""
- break
- else
- Error "Sie mⁿssen etwas eingeben !"
- fi
- else
- break
- fi
- done
- export STRING
- rm -f $MSG
- return 0
- }
-