home *** CD-ROM | disk | FTP | other *** search
/ Chip 1994 February / CHIP0294.ISO / digital / test / linux / bootdisk / rootdisk.18 / usr / lib / LST / libLSTdialog < prev    next >
Encoding:
Text File  |  1994-05-27  |  7.2 KB  |  408 lines

  1. # lstcommon - lst functions shared between several scripts
  2. #
  3. # Version: 0.7
  4. # Last changed: 12.05.94
  5. #
  6. # Copyright (c) Ralf Flaxa, Linux Support Team Erlangen,  1993, 1994
  7. #
  8. # Supported Constants:
  9. # --------------------
  10. CLEAR='c'
  11. REVON=''
  12. REVOFF=''
  13. BEEPTONE=''
  14. MSG=/tmp/.message
  15. ERRMSG=/tmp/.error
  16. PROGNAME=lstcommon
  17. FUNCTNAME=
  18. NULLDEVICE=/dev/null
  19. # TEXTCONSTANTS=/usr/lib/LST/libLSTstrings
  20. PAGER=more
  21. # USEDIALOG=true
  22.  
  23. # String Constants:
  24. # -----------------
  25. # ein erster Versuch fⁿr Multi-Language Support ...
  26. # nur partiell verwirklicht
  27. # . $TEXTCONSTANTS
  28.  
  29. #
  30. # Functions
  31. #
  32.  
  33. debug()
  34. {
  35.     if [ "$DEBUG" != "" ]; then
  36.         echo "debug: $@" 
  37.     fi
  38. }
  39.  
  40. # DoPanic ( panicmessage )
  41. #
  42. #    Displays panic message. 
  43. #    uses environment variables PROGNAME and FUNCTNAME
  44. #
  45. DoPanic()
  46. {
  47.     echo "$PROGNAME: panic: $FUNCTNAME: $@"
  48. }
  49.  
  50. # DoLog ( [-d] [-n] [-f LOGFILE] text_to_log )
  51. #
  52. #       Log a string for logging or postponed display
  53. #
  54. DoLog()
  55. {
  56.     export FUNCTNAME=DoLog
  57.     # defaults
  58.     deleteflag=
  59.     appendflag=
  60.     logfile=$MSG
  61.  
  62.     # parse loop
  63.     while [ 1 ]; do
  64.         if [ $# -gt 0 ]; then
  65.             # Test for options
  66.             # We need to protect the "-" because
  67.             # otherwise ash interprets them as tests :-(
  68.             if [ "X$1" = 'X-d' ]; then
  69.                 deleteflag=true
  70.                 shift 1
  71.             elif [ "X$1" = 'X-n' ]; then
  72.                 appendflag=true
  73.                 shift 1
  74.             elif [ $# -gt 1 -a "X$1" = 'X-f' ]; then
  75.                 logfile=$2
  76.                 shift 2
  77.             else
  78.                 # no more options, so the rest is message
  79.                 break
  80.             fi
  81.         else
  82.             DoPanic "internal error: empty message passed to DoLog" 
  83.             return 1
  84.         fi
  85.     done
  86.  
  87.     # test if we must delete the old logfile
  88.     if [ "$deleteflag" != "" ]; then
  89.         rm -f "$logfile"
  90.     fi
  91.     if [ "$appendflag" != "" ]; then
  92.         echo -n "$@" >> $logfile
  93.     else
  94.         echo "$@" >> $logfile
  95.     fi
  96. }
  97.  
  98. # Obsolete function ! Will be replaced by DoLog !     
  99. #
  100. # LogMsg ( [-new] [-n] text_saved_for_postponed_display )
  101. #
  102. #    Collect Text for postponed display
  103. #
  104. LogMsg()
  105. {
  106.     export FUNCTNAME=LogMsg
  107.     deleteflag=
  108.     appendflag=
  109.     if [ $# -gt 0 -a "$1" = '-new' ]; then
  110.         deleteflag='-d'
  111.         shift 1
  112.     fi
  113.     if [ $# -gt 0 -a "$1" = '-n' ]; then
  114.         appendflag='-n'
  115.         shift 1
  116.     fi
  117.     DoLog $deleteflag $appendflag -f $MSG "$@"
  118. }
  119.  
  120. # Obsolete function ! Will be replaced by DoLog !     
  121. #
  122. # LogErr ( [-new] [-n] error_message_saved_for_postponed_display )
  123. #
  124. #    Collect Error Message for postponed display
  125. #
  126. LogErr()
  127. {
  128.     export FUNCTNAME=LogErr
  129.     deleteflag=
  130.     appendflag=
  131.     if [ $# -gt 0 -a "$1" = '-new' ]; then
  132.         deleteflag='-d'
  133.         shift 1
  134.     fi
  135.     if [ $# -gt 0 -a "$1" = '-n' ]; then
  136.         appendflag='-n'
  137.         shift 1
  138.     fi
  139.     DoLog $deleteflag $appendflag -f $ERRMSG "$@"
  140. }
  141.  
  142. # FormatMessage ( ? options ? message_to_format )
  143. #
  144. #    truncates message for use with dialog
  145. #
  146. FormatMessage()
  147. {
  148.     export FUNCTNAME=FormatMessage
  149.     cat $1 | cut -c 1-70 > $1.new
  150.     mv $1.new $1
  151.     zeilen=`wc $1 | cut -c 1-7 | sed 's/ //g' `
  152.     if [ $zeilen -gt 15 ]; then
  153.         zeilen=20
  154.     else
  155.         zeilen=`expr $zeilen + 5`
  156.     fi
  157.     export zeilen
  158. }
  159.  
  160. # Seen ()
  161. #
  162. #    display $MSG and wait for keypress 
  163. #
  164. Seen()
  165. {
  166.     export FUNCTNAME=Seen
  167.     if [ ! -f $MSG ]; then
  168.         touch $MSG
  169.     fi
  170.     if [ "$USEDIALOG" = "true" ]; then
  171.         FormatMessage $MSG
  172.         dialog --msgbox "`cat $MSG`" $zeilen 75
  173.     else
  174.         cat $MSG
  175.         echo
  176.         echo -n "$MKEYPRESS"
  177.         read dummy
  178.     fi
  179.     rm -f $MSG
  180. }
  181.  
  182. # ShowInfo ()
  183. #
  184. #    display $MSG without paging or prompting
  185. #
  186. ShowInfo()
  187. {
  188.     export FUNCTNAME=ShowInfo
  189.     if [ ! -f $MSG ]; then
  190.         touch $MSG
  191.     fi
  192.     if [ "$USEDIALOG" = "true" ]; then
  193.         FormatMessage $MSG
  194.         dialog --infobox "`cat $MSG`" $zeilen 75
  195.     else
  196.         cat $MSG
  197.     fi
  198.     rm -f $MSG
  199. }
  200.  
  201. # Obsolete Function ! Will be replaced by ShowInfo
  202. #
  203. #    Message ( )
  204. #
  205. Message()
  206. {
  207.     export FUNCTNAME=Message
  208.     ShowInfo $@
  209. }
  210.  
  211. # PageMessage ()
  212. #
  213. #    use $PAGER for paging in $MSG
  214. #
  215. PageMessage()
  216. {
  217.     export FUNCTNAME=PageMessage
  218.     if [ ! -f $MSG ]; then
  219.         touch $MSG
  220.     fi
  221.     if [ "$USEDIALOG" = "true" ]; then
  222.         FormatMessage $MSG
  223.         dialog --textbox $MSG $zeilen 75
  224.         if [ $? = 255 ]; then
  225.             exit
  226.         fi
  227.     else
  228.         cat $MSG | $PAGER
  229.         echo
  230.         echo -n "$MKEYPRESS"
  231.         read dummy
  232.     fi
  233.     rm -f $MSG
  234. }
  235.  
  236.  
  237. # Error ( [error_message] )
  238. #
  239. #       Beep and output an error message
  240. #
  241. Error()
  242. {
  243.     export FUNCTNAME=Error
  244.     if [ $# -gt 0 ]; then
  245.         echo "$@" >> $ERRMSG
  246.     fi
  247.     if [ ! -f $ERRMSG ]; then
  248.         touch $ERRMSG
  249.     fi
  250.     echo -n $BEEPTONE
  251.     if [ "$USEDIALOG" = "true" ]; then
  252.         FormatMessage $ERRMSG
  253.         dialog --title "$MERROR" --msgbox "`cat $ERRMSG `" `expr $zeilen + 3 ` 75
  254.     else
  255.             echo "$REVON $MERROR !"
  256.             cat $ERRMSG
  257.             echo $REVOFF
  258.             Seen
  259.     fi
  260.     rm -f $ERRMSG
  261. }
  262.  
  263. # Warning ( [warning_message] )
  264. #
  265. #       Beep and output a warning message
  266. #
  267. Warning()
  268. {
  269.     export FUNCTNAME=Warning
  270.     if [ $# -gt 0 ]; then
  271.         echo "$@" >> $ERRMSG
  272.     fi
  273.     if [ ! -f $ERRMSG ]; then
  274.         touch $ERRMSG
  275.     fi
  276.     echo -n $BEEPTONE
  277.     if [ "$USEDIALOG" = "true" ]; then
  278.         FormatMessage $ERRMSG
  279.         dialog --title "Warnung" --infobox "`cat $ERRMSG `" $zeilen 75
  280.     else
  281.             echo "$REVON $MWARNING !"
  282.             cat $ERRMSG
  283.             echo $REVOFF
  284.     fi
  285.     rm -f $ERRMSG
  286. }
  287.  
  288. #  Askyesno( [-default DEFANSWER] )
  289. #       Just ask a yes/no question (j/n)
  290. #
  291. Askyesno()
  292. {
  293.     export FUNCTNAME=Askyesno
  294.         export ANSWER=""
  295.     export DEFANSWER=
  296.     if [ ! -f $MSG ]; then
  297.         touch $MSG
  298.     fi
  299.     if [ $# -gt 1 -a "$1" = "-default" ]; then
  300.         DEFANSWER=$2
  301.         shift 2
  302.     fi
  303.     if [ "$USEDIALOG" = "true" ]; then
  304.         if [ "$DEFANSWER" != "" ]; then
  305.             echo "Vorschlag: $DEFANSWER" >> $MSG
  306.         fi
  307.         FormatMessage $MSG
  308.     else
  309.         echo -n "Eingabe ($YES/$NO)" >> $MSG
  310.         if [ "$DEFANSWER" != "" ]; then
  311.             echo -n " [$DEFANSWER]" >> $MSG
  312.         fi
  313.         echo -n ": " >> $MSG
  314.     fi
  315.         while [ 0 ]; do
  316.         if [ "$USEDIALOG" = "true" ]; then
  317.             dialog --yesno "`cat $MSG`" `expr $zeilen + 3 ` 75 
  318.             if [ $? = 0 ]; then
  319.                 ANSWER="$YES"
  320.             else
  321.                 ANSWER="$NO"
  322.             fi
  323.         else
  324.             echo $CLEAR
  325.             cat $MSG
  326.                     read ANSWER
  327.         fi
  328.                 if [ "$ANSWER" = "$YES" -o "$ANSWER" = "$NO" ]; then
  329.                         break
  330.                 elif [ "$DEFANSWER" != "" -a "$ANSWER" = "" ]; then
  331.             ANSWER=$DEFANSWER
  332.             break
  333.                 fi
  334.         Warning "$MINPUT $MINVALID"
  335.         done
  336.     export ANSWER
  337.     rm -f $MSG
  338.         return 0
  339. }
  340.  
  341. #
  342. #   Askstring( [-emptyvalid] [ -default DEFANSWER] )
  343. #
  344. #       Just ask for a non-null string
  345. #
  346. Askstring()
  347. {
  348.     export FUNCTNAME=Askstring
  349.     export EMPTYVALID=false
  350.     export DEFANSWER=
  351.         export STRING=
  352.     if [ ! -f $MSG ]; then
  353.         touch $MSG
  354.     fi
  355.     while [ 1 ]; do
  356.         if [ $# = 0 ]; then
  357.             break
  358.         elif [ "$1" = "-emptyvalid" ]; then
  359.             EMPTYVALID=true
  360.             shift 1
  361.         elif [ $# -gt 1 -a "$1" = "-default" ]; then
  362.             DEFANSWER=$2
  363.             shift 2
  364.         else
  365.             DoPanic "Askstring mit unbekannten Argumenten aufgerufen "
  366.         fi
  367.     done
  368.     if [ "$USEDIALOG" = "true" ]; then
  369.         if [ "$DEFANSWER" != "" ]; then
  370.             echo "Vorschlag: $DEFANSWER" >> $MSG
  371.         fi
  372.         FormatMessage $MSG
  373.     else
  374.         echo -n "Eingabe" >> $MSG
  375.         if [ "$DEFANSWER" != "" ]; then
  376.             echo -n " [$DEFANSWER]" >> $MSG
  377.         fi
  378.         echo -n ": " >> $MSG
  379.     fi
  380.         while [ 0 ]; do
  381.         if [ "$USEDIALOG" = "true" ]; then    
  382.             dialog --inputbox "`cat $MSG`" `expr $zeilen + 3 ` 75 2> /tmp/ANSWER
  383.             STRING="`cat /tmp/ANSWER`"
  384.         else
  385.             echo $CLEAR
  386.             cat $MSG
  387.             read STRING
  388.         fi
  389.         if [ "$STRING" = "" ]; then
  390.             if [ "$DEFANSWER" != "" ]; then
  391.                 STRING=$DEFANSWER
  392.                 break
  393.             elif [ "$EMPTYVALID" = "true" ]; then
  394.                 STRING=""
  395.                 break
  396.             else
  397.                 Error "Sie mⁿssen etwas eingeben !"
  398.             fi 
  399.                 else
  400.             break
  401.         fi
  402.         done
  403.     export STRING
  404.     rm -f $MSG
  405.         return 0
  406. }
  407.