pupdialog 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/bin/sh
  2. #(c) Copyright Barry Kauler Sept. 2011 puppylinux.com
  3. #2011 GPL licence v3 (/usr/share/doc/legal)
  4. #a simple replacement for 'dialog', for when X is running. can use this instead of Xdialog.
  5. #110924 when no window decoration, also do not appear in taskbar. menu dlg to fit in 480 pixel screen vert. fix pupkill.
  6. #111011 add --checklist dialog, --helpwindow parameter.
  7. #111021 increase limit, number commandline items.
  8. #120131 rodin.s: adding gettext to help
  9. #120203 think need OUTPUT_CHARSET=UTF-8
  10. #130221 problem, window close-box and timeout both exit with 255. change timeout to 254 (differs from dialog).
  11. export TEXTDOMAIN=pupdialog
  12. export OUTPUT_CHARSET=UTF-8
  13. #examples passed params:
  14. # --timeout 90 --extra-button --ok-label "SAVE TO A FILE" --extra-label "SAVE TO ${xPDEV}" --cancel-label "DO NOT SAVE" --yesno "etetetete" 0 0
  15. #accept these, not available in dialog: --background --foreground --countdown "text"
  16. [ ! $1 ] && HELPFLG='yes'
  17. [ "$1" == "--help" ] && HELPFLG='yes'
  18. if [ "$HELPFLG" == "yes" ];then
  19. echo "pupdialog is a simple replacement for 'dialog' when X is running.
  20. pupdialog only implements these dialogs: --msgbox --yesno --menu --inputbox --checklist
  21. These parameters are supported:
  22. --extra-button --ok-label --extra-label --cancel-label --colors --yes-label --no-label --title --backtitle --stdout --default-item
  23. ex: pupdialog --timeout 90 --extra-button --ok-label \"SAVE TO A FILE\" --extra-label \"SAVE TO PARTITION\" --cancel-label \"DO NOT SAVE\" --yesno \"example text\" 0 0
  24. ex: pupdialog --backtitle \"backtitle here\" --default-item tag2 --menu \"this is blurb for menu dlg\" 0 0 0 tag1 item1 tag2 item2 tag3 item3
  25. The height and width parameters are ignored.
  26. If no --title <string>, then window is not decorated.
  27. Ansi text markup is supported with --colors ex: \Zbbold\ZB \Z1red\Zn
  28. -- but basic parsing, always pair them as shown in example (nesting allowed).
  29. The following are extras in pupdialog not in dialog:
  30. --background <value> #background color of window
  31. --foreground <value> #color of text
  32. --countdown <string> #countdown message, time substituted for TIME (requires --timeout)
  33. --helpwindow <executable> #run another application, typically help window. does not exit.
  34. ex: pupdialog --background yellow --foreground black --timeout 90 --countdown \"Shutdown in TIME seconds\" ...
  35. Note: use 'pupkill $!' to kill a pupdialog window."
  36. exit
  37. fi
  38. GTKDIALOG="`which gtkdialog4`"
  39. [ ! "$GTKDIALOG" ] && GTKDIALOG="`which gtkdialog`"
  40. [ ! "$GTKDIALOG" ] && exit 255
  41. [ ! -e /tmp/pupdialog_exec ] && ln -s $GTKDIALOG /tmp/pupdialog_exec
  42. TIMEOUT=0 #means never timeout.
  43. FONTSTYLE='Mono 14'
  44. TABCHAR="`echo -n -e '\t'`"
  45. #evaluate the passed parameters...
  46. unset EXTRABUTTON OKLABEL EXTRALABEL BACKGROUND CANCELLABEL COLORS FOREGROUND YESLABEL MSGBOX NOLABEL YESNO TITLE NOCOLLAPSE COUNTDOWN MENU STDOUT DEFAULTITEM INPUTBOX HEIGHT WIDTH MENUHEIGHT CHECKLIST HELPWINDOW
  47. AVAR='START'; AVAL=''; ENDCNT=0; TOGGLE=0; MENUITEMS=''; INITVAL=''; INPUTXML=''; MENUCNT=0; SUBCNT=0
  48. for AFIELD in "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" "${7}" "${8}" "${9}" "${10}" "${11}" "${12}" "${13}" "${14}" "${15}" "${16}" "${17}" "${18}" "${19}" "${20}" "${21}" "${22}" "${23}" "${24}" "${25}" "${26}" "${27}" "${28}" "${29}" "${30}" "${31}" "${32}" "${33}" "${34}" "${35}" "${36}" "${37}" "${38}" "${39}" "${40}" "${41}" "${42}" "${43}" "${44}" "${45}" "${46}" "${47}" "${48}" "${49}" "${50}" "${51}" "${52}" "${53}" "${54}" "${55}" "${56}" "${57}" "${58}" "${59}" "${60}" "${61}" "${62}" "${63}" "${64}" "${65}" "${66}" "${67}" "${68}" "${69}" "${70}" "${71}" "${72}" "${73}" "${74}" "${75}" "${76}" "${77}" "${78}" "${79}" "${80}" "${81}" "${82}" "${83}" "${84}" "${85}" "${86}" "${87}" "${88}" "${89}" "${90}" "${91}" "${92}" "${93}" "${94}" "${95}" "${96}" "${97}" "${98}" "${99}" "${100}" "${101}" "${102}" TOOLONG
  49. do
  50. if [ "$AFIELD" == "TOOLONG" ];then
  51. xmessage -center -bg red "ERROR: There are too many menu items for pupdialog to handle. Aborting."
  52. exit 255
  53. fi
  54. [ "$AFIELD" == "" ] && break #end.
  55. case $AVAR in
  56. MSGBOX|YESNO|MENU|INPUTBOX|CHECKLIST)
  57. DLGTYPE="$AVAR"
  58. case $ENDCNT in
  59. 0)
  60. AVAL="`echo "$AFIELD" | tr '"' "'" | tr '\t' ' ' | tr -s ' ' | sed -e 's% $%%'`" #cleanup text.
  61. eval "${AVAR}=\"${AVAL}\"" #assigns to variables MSGBOX, YESNO, MENU or INPUTBOX.
  62. BODYSTR="$AVAL"
  63. ;;
  64. 1) HEIGHT=$AFIELD ;;
  65. 2) WIDTH=$AFIELD ;;
  66. 3)
  67. case $AVAR in
  68. MENU|CHECKLIST) MENUHEIGHT=$AFIELD ;;
  69. INPUTBOX) INITVAL="$AFIELD" ;;
  70. esac
  71. ;;
  72. *)
  73. case $AVAR in
  74. MENU)
  75. if [ $TOGGLE -eq 0 ];then #menu items are format "tag description" for each item, where description displays.
  76. MENUCNT=$(($MENUCNT + 1))
  77. TAG="$AFIELD"
  78. MENUITEMS="${MENUITEMS} <radiobutton draw_indicator=\"false\" xalign=\"0\"><variable>${AFIELD}</variable>"
  79. [ "$DEFAULTITEM" == "$AFIELD" ] && MENUITEMS="${MENUITEMS}<default>true</default>"
  80. TOGGLE=1
  81. else
  82. MENUITEMS="${MENUITEMS}<label>${TAG}${TABCHAR}${AFIELD}</label></radiobutton>"
  83. TOGGLE=0
  84. fi
  85. ;;
  86. CHECKLIST) #111011
  87. SUBCNT=$(($SUBCNT + 1))
  88. case $SUBCNT in
  89. 1)
  90. MENUCNT=$(($MENUCNT + 1))
  91. TAG="$AFIELD"
  92. MENUITEMS="${MENUITEMS} <checkbox draw_indicator=\"false\" xalign=\"0\"><variable>${AFIELD}</variable>"
  93. ;;
  94. 2)
  95. MENUITEMS="${MENUITEMS}<label>${TAG}${TABCHAR}${AFIELD}</label>"
  96. ;;
  97. 3)
  98. [ "$AFIELD" = "yes" ] && AFIELD='true'
  99. [ "$AFIELD" = "no" ] && AFIELD='false'
  100. [ "$AFIELD" = "on" ] && AFIELD='true'
  101. [ "$AFIELD" = "off" ] && AFIELD='false'
  102. MENUITEMS="${MENUITEMS}<default>${AFIELD}</default></checkbox>"
  103. SUBCNT=0
  104. ;;
  105. esac
  106. ;;
  107. esac
  108. ;;
  109. esac
  110. ENDCNT=$(($ENDCNT + 1))
  111. continue
  112. ;;
  113. esac
  114. case $AFIELD in
  115. --[a-z]*)
  116. [ "$AVAL" == "" ] && AVAL="true" #so as not empty.
  117. eval "${AVAR}='${AVAL}'" #evaluate previous param.
  118. AVAR="`echo -n "$AFIELD"| tr -d '-' | tr '[a-z]' '[A-Z]'`"
  119. AVAL=''
  120. ;;
  121. *)
  122. AVAL="`echo "$AFIELD" | tr '"' "'" | tr '\t' ' ' | tr -s ' ' | sed -e 's% $%%'`" #cleanup text.
  123. ;;
  124. esac
  125. done
  126. #if [ "$YESNO" -o "$MSGBOX" ];then
  127. BODYSTR="`echo "$BODYSTR" | sed -e 's%<%\&lt;%g' -e 's%>%\&gt;%g' -e 's%\\\n%\\n%g'`"
  128. if [ "$COLORS" ];then
  129. #Interpret embedded "\Z" sequences in the dialog text by the following character, which tells dialog to set colors or video attributes: 0 through 7 are the ANSI used in curses: black, red, green, yellow, blue, magenta, cyan and white respectively. Bold is set by 'b', reset by 'B'. Reverse is set by 'r', reset by 'R'. Underline is set by 'u', reset by 'U'. The settings are cumulative, e.g., "\Zb\Z1" makes the following text bold (perhaps bright) red. Restore normal settings with "\Zn".
  130. #\Z0 - \Z7 = black, red, green, yellow, blue, magenta, cyan and white
  131. BODYSTR="`echo "$BODYSTR" | sed -e 's%\\\Zb%<b>%g' -e 's%\\\ZB%</b>%g' -e 's%\\\Z0%<span color='"'black'"'>%g' -e 's%\\\Z1%<span color='"'red'"'>%g' -e 's%\\\Z2%<span color='"'green'"'>%g' -e 's%\\\Z3%<span color='"'yellow'"'>%g' -e 's%\\\Z4%<span color='"'blue'"'>%g' -e 's%\\\Z5%<span color='"'magenta'"'>%g' -e 's%\\\Z6%<span color='"'cyan'"'>%g' -e 's%\\\Z7%<span color='"'white'"'>%g' -e 's%\\\Zn%</span>%'`"
  132. fi
  133. BODYTEXT="<text use-markup=\"true\"><label>\"${BODYSTR}\"</label></text>"
  134. YESBUTTON="<button><label>OK</label><action>EXIT:okbutton</action></button>"
  135. [ "$OKLABEL" ] && YESBUTTON="<button><label>${OKLABEL}</label><action>EXIT:okbutton</action></button>"
  136. #hmmm, different from dialog, default is no cancel button, unless label specified...
  137. [ "$CANCELLABEL" ] && NOBUTTON="<button><label>${CANCELLABEL}</label><action>EXIT:cancelbutton</action></button>"
  138. case $DLGTYPE in
  139. YESNO)
  140. YESBUTTON="<button><label>Yes</label><action>EXIT:yesbutton</action></button>"
  141. [ "$OKLABEL" ] && YESBUTTON="<button><label>${OKLABEL}</label><action>EXIT:yesbutton</action></button>"
  142. [ "$YESLABEL" ] && YESBUTTON="<button><label>${YESLABEL}</label><action>EXIT:yesbutton</action></button>"
  143. if [ "$EXTRABUTTON" ];then
  144. EXTRABUTTON="<button><label>Extra</label><action>EXIT:extrabutton</action></button>"
  145. [ "$EXTRALABEL" ] && EXTRABUTTON="<button><label>${EXTRALABEL}</label><action>EXIT:extrabutton</action></button>"
  146. fi
  147. NOBUTTON="<button><label>No</label><action>EXIT:nobutton</action></button>"
  148. [ "$NOLABEL" ] && NOBUTTON="<button><label>${NOLABEL}</label><action>EXIT:nobutton</action></button>"
  149. [ "$CANCELLABEL" ] && NOBUTTON="<button><label>${CANCELLABEL}</label><action>EXIT:nobutton</action></button>"
  150. ;;
  151. MENU|CHECKLIST)
  152. if [ "$MENUITEMS" ];then
  153. if [ $MENUCNT -gt 5 ];then
  154. MENUXML="<vbox scrollable=\"true\" height=\"230\" width=\"454\">${MENUITEMS}</vbox>"
  155. else
  156. MENUXML="<vbox>${MENUITEMS}</vbox>"
  157. fi
  158. fi
  159. ;;
  160. INPUTBOX)
  161. [ "$INITVAL" ] && DEFAULTXML="<default>${INITVAL}</default>"
  162. INPUTXML="<entry>${DEFAULTXML}<variable>INPUTVAR</variable></entry>"
  163. ;;
  164. esac
  165. if [ "$TITLE" ];then
  166. TITLEPARAMS="title=\"${TITLE}\""
  167. else
  168. TITLEPARAMS="decorated=\"false\" skip_taskbar_hint=\"true\""
  169. fi
  170. [ "$BACKTITLE" ] && BTTEXT="<text use-markup=\"true\"><label>\"<big><b>${BACKTITLE}</b></big>\"</label></text>"
  171. if [ "$HELPWINDOW" ];then
  172. HELPBUTTONXML="<button><label>$(gettext 'Help')</label><action>${HELPWINDOW} & </action></button>"
  173. fi
  174. export PUP${$}DLG="<window ${TITLEPARAMS}>
  175. <vbox>
  176. ${BTTEXT}
  177. ${BODYTEXT}
  178. ${MENUXML}
  179. ${INPUTXML}
  180. <hbox>
  181. ${YESBUTTON}${EXTRABUTTON}${NOBUTTON}${HELPBUTTONXML}
  182. </hbox>
  183. </vbox>
  184. </window>"
  185. #fi
  186. #dumpdlg="<window ${TITLEPARAMS}>
  187. # <vbox>
  188. # ${BTTEXT}
  189. # ${BODYTEXT}
  190. # ${MENUXML}
  191. # ${INPUTXML}
  192. # <hbox>
  193. # ${YESBUTTON}${EXTRABUTTON}${NOBUTTON}
  194. # </hbox>
  195. # </vbox>
  196. # </window>"
  197. #echo "${dumpdlg}" > /tmp/pupdialog_debug_dumpdlg #for debugging.
  198. #setup gtk styles...
  199. #foreground/background colours, not supported by dialog...
  200. [ "$BACKGROUND" ] && insertBG=" bg[NORMAL] = \"${BACKGROUND}\""
  201. [ "$FOREGROUND" ] && insertFG=" fg[NORMAL] = \"${FOREGROUND}\""
  202. #[ "FONTSTYLE" ] && insertFS="style \"specialfont\"
  203. #{
  204. # font_name=\"${FONTSTYLE}\"
  205. #}
  206. #class \"GtkWidget\" style \"specialfont\""
  207. gtkrc="style \"windowstuff\"
  208. {
  209. ${insertBG}
  210. ${insertFG}
  211. #font_name=\"${FONTSTYLE}\"
  212. }
  213. class \"*\" style \"windowstuff\"
  214. ${insertFS}
  215. "
  216. echo "$gtkrc" > /tmp/pupdialog_gtkrc${$}
  217. export GTK2_RC_FILES=/tmp/pupdialog_gtkrc${$}:/root/.gtkrc-2.0
  218. nTIMEOUT=$TIMEOUT #convert string to numeric.
  219. if [ $nTIMEOUT -a $nTIMEOUT -ne 0 ];then #assume 0 means never timeout.
  220. #note, jwm will use module16.xpm as the window icon...
  221. /tmp/pupdialog_exec --center --class="module16" --program=PUP${$}DLG > /tmp/pupdialog_retvals${$} &
  222. dlgPID=$!
  223. pidPATTERN="^${dlgPID} "
  224. while [ $nTIMEOUT -ne 0 ];do
  225. sleep 1
  226. ALLPS="`ps`"
  227. [ "`echo "$ALLPS" | sed -e 's%^ *%%' | grep "$pidPATTERN"`" == "" ] && break #already killed.
  228. nTIMEOUT=$(($nTIMEOUT - 1))
  229. if [ "$COUNTDOWN" ];then
  230. BKCOL="yellow"
  231. [ $nTIMEOUT -lt 31 ] && BKCOL="orange"
  232. [ $nTIMEOUT -lt 11 ] && BKCOL="red"
  233. SPLASHSTR="`echo -n "$COUNTDOWN" | sed -e "s%TIME%${nTIMEOUT}%"`"
  234. /usr/lib/gtkdialog/box_splash -timeout 2 -bg ${BKCOL} -placement top -close never -fontsize large -text " ${SPLASHSTR} " >/dev/null &
  235. fi
  236. done
  237. if [ $nTIMEOUT -eq 0 ];then #has timed out.
  238. kill $dlgPID
  239. rm -f /tmp/pupdialog_gtkrc${$}
  240. rm -f /tmp/pupdialog_retvals${$}
  241. exit 254 #255 #as per dialog. 130221 change to 254.
  242. fi
  243. else
  244. #note, jwm will use module16.xpm as the window icon...
  245. /tmp/pupdialog_exec --center --class="module16" --program=PUP${$}DLG > /tmp/pupdialog_retvals${$}
  246. fi
  247. RETVALS="`cat /tmp/pupdialog_retvals${$}`"
  248. rm -f /tmp/pupdialog_gtkrc${$}
  249. rm -f /tmp/pupdialog_retvals${$}
  250. #111011 problem if tags are just numbers, ex 2=true
  251. xRETVALS="`echo "$RETVALS" | sed -e 's%^%TAGPREFIXFIX_%'`"
  252. eval "$xRETVALS"
  253. ECHODEST='/dev/stderr'
  254. [ "$STDOUT" ] && ECHODEST='/dev/stdout'
  255. if [ "$MENU" ];then
  256. echo "$RETVALS" | grep '"true"' | head -n 1 | cut -f 1 -d '=' > ${ECHODEST} #echo tag
  257. fi
  258. if [ "$INPUTBOX" ];then
  259. echo "$TAGPREFIXFIX_INPUTVAR" > ${ECHODEST} #echo contents of entry box.
  260. fi
  261. if [ "$CHECKLIST" ];then
  262. echo "$RETVALS" | grep '"true"' | cut -f 1 -d '=' > ${ECHODEST} #echo 'true' tags
  263. fi
  264. case $TAGPREFIXFIX_EXIT in
  265. yesbutton|okbutton) exit 0 ;;
  266. extrabutton) exit 3 ;;
  267. cancelbutton|nobutton) exit 1 ;;
  268. helpbutton) exit 2 ;;
  269. *) exit 255 ;;
  270. esac
  271. ###END###