slitaz-tools view tinyutils/tazdialog @ rev 250

tazdialog: avoid return out of functions
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jul 18 21:40:51 2008 +0000 (2008-07-18)
parents 981e48565c3a
children
line source
1 #!/bin/sh
2 # tazdialog - SliTaz GNU/Linux dialog wrapper to gtkdialog.
3 #
4 # (C) 2008 SliTaz - GNU General Public License v3.
5 #
6 # Author : Pascal Bellard <pascal.bellard@slitaz.org>
7 #
9 toutf8()
10 {
11 echo -e $1 | sed -e 's/\\Z.//g' | tr 'àâäçéèêëîïûü' 'aaaceeeeiiuu'
12 }
14 fake_gauge()
15 {
16 [ -n "$percent" ] && echo $percent
17 while read line; do
18 case "$line" in
19 [0-9]*) echo $line;;
20 *);;
21 esac
22 done
23 echo 100
24 }
26 fake_dialog()
27 {
28 if [ -n "$gauge" ]; then
29 fake_gauge | gtkdialog --center --program=BOX
30 else
31 gtkdialog --center --program=BOX
32 fi 2> /dev/null | while read line; do
33 case "$line" in
34 *OUTPUT*) eval $line
35 echo "$OUTPUT" 1>&2 ;;
36 *CHECK*true*) line=${line%=*}; line=${line#CHECK}
37 [ $line -eq 0 ] || shift $((3 * $line))
38 echo -n "\"$1\" " 1>&2;;
39 *LIST=*) eval $line; set -- $LIST
40 echo "$1" 1>&2 ;;
41 *EXIT*abort*) echo 255;;
42 *EXIT*) eval $line
43 echo $EXIT ;;
44 esac
45 done
46 }
48 if [ -z "$XAUTHORITY" ]; then
49 [ -n "$TEXTDIALOG" ] || TEXTDIALOG="dialog"
50 export DIALOG=$TEXTDIALOG
51 exec $TEXTDIALOG "$@"
52 else
53 title=""
54 backtitle=""
55 lines="10"
56 cols="10"
57 [ -n "$ICON" ] || ICON="applications-other"
58 buttonok=""
59 buttoncancel=""
60 buttonextra=""
61 yeslabel="ok"
62 nolabel="cancel"
63 extralabel="rename"
64 gauge=""
65 inputbox=""
66 default=""
67 buttonhelp=""
68 checklist=""
69 menu=""
70 textbox=""
71 radiolist=""
72 fselect=""
73 while [ -n "$1" ]; do
74 case "$1" in
75 --title) title="$2"; shift 2;;
76 --backtitle) backtitle="$2"; shift 2;;
77 --clear|--colors) shift;;
78 --yes-label) yeslabel="$2"; shift 2;;
79 --no-label) nolabel="$2"; shift 2;;
80 --extra-label) extralabel="$2"; shift 2;;
81 --extra-button) buttonextra="1"; shift;;
82 --help-button) buttonhelp="1"; shift;;
83 --msgbox) msgbox="$2"; lines="$3"; cols="$4"
84 buttonok="1"; break;;
85 --yesno) msgbox="$2"; lines="$3"; cols="$4"
86 buttonok="1"; buttoncancel="1"; break;;
87 --gauge) msgbox="$2"; lines="$3"; cols="$4"
88 percent="$5"; gauge="1"; break;;
89 --inputbox) msgbox="$2"; lines="$3"; cols="$4"; default="$5"
90 buttonok="1"; buttoncancel="1"; inputbox="1"; break;;
91 --checklist) msgbox="$2"; lines="$3"; cols="$4"; inlines="$5"
92 buttonok="1"; checklist="1"; shift 5; break;;
93 --menu) msgbox="$2"; lines="$3"; cols="$4"; inlines="$5"
94 buttonok="1"; buttoncancel="1"
95 menu="1"; shift 5; break;;
96 --textbox) textbox="$2"; lines="$3"; cols="$4"
97 buttonok="1"; break;;
98 --radiolist) textbox="$2"; lines="$3"; cols="$4"; inlines="$5"
99 buttonok="1"; buttoncancel="1"
100 radiolist="1"; shift 5; break;;
101 --fselect) fselect="$2"; lines="$3"; cols="$4"
102 buttonok="1"; buttoncancel="1"; break;;
103 *) echo "Unknown arg $1"; exit 255;;
104 esac
105 done
106 fi
107 msgbox="$(toutf8 "$msgbox")"
108 textbox="$(toutf8 "$textbox")"
109 title="$(toutf8 "$title")"
110 backtitle="$(toutf8 "$backtitle")"
111 yeslabel="$(toutf8 "$yeslabel")"
112 nolabel="$(toutf8 "$nolabel")"
113 extralabel="$(toutf8 "$extralabel")"
114 default="$(toutf8 "$default")"
115 BOX="<window"
116 [ -n "$backtitle" ] && BOX="$BOX title=\"$backtitle\""
117 [ -n "$ICON" ] && BOX="$BOX icon-name=\"$ICON\""
118 BOX="$BOX>
119 <vbox>
120 "
121 [ -n "$title" ] && BOX="$BOX <text use-markup=\"true\" width-chars=\"44\">
122 <label>\"<b>$title</b>\" </label>
123 </text>
124 "
125 [ -n "$msgbox" ] && BOX="$BOX <text wrap=\"true\" use-markup=\"true\">
126 <label> \"$msgbox\" </label>
127 </text>
128 "
129 [ -n "$gauge" ] && BOX="$BOX <progressbar>
130 <input>while read data; do echo \$data; done</input>
131 <action type=\"exit\">0</action>
132 </progressbar>
133 "
134 [ -n "$default" ] && default="<default>$default</default>
135 "
136 [ -n "$inputbox" ] && BOX="$BOX <entry>
137 $default<variable>OUTPUT</variable>
138 </entry>
139 "
140 #[ -n "$textbox" ] && BOX="$BOX <text><input file width-chars=\"$cols\" \
141 #height=\"$lines\" wrap=\"true\">$textbox</input></text>"
142 [ -n "$textbox" ] && BOX="$BOX <text>
143 <input file wrap=\"true\">$textbox</input>
144 </text>
145 "
146 [ -n "$menu" ] && BOX="$BOX <list>
147 $(while [ -n "$1" ]; do
148 echo " <item>$1 $2</item>
149 "
150 shift 2
151 done) <variable>LIST</variable>
152 </list>
153 "
154 [ -n "$radiolist" ] && BOX="$BOX$(
155 i=0; while [ -n "$1" ]; do
156 [ "$3" = "on" ] && c=" active=\"true\"" || c=""
157 echo " <radiobutton$c>
158 <label>$1 $2</label>
159 <variable>CHECK$i</variable>
160 </radiobutton>
161 "
162 i=$(($i+1))
163 shift 3
164 done)"
165 [ -n "$checklist" ] && BOX="$BOX $(
166 i=0; while [ -n "$1" ]; do
167 [ "$3" = "on" ] && c=" active=\"true\"" || c=""
168 echo " <checkbox$c>
169 <label>$1 $2</label>
170 <variable>CHECK$i</variable>
171 </checkbox>
172 "
173 i=$(($i+1))
174 shift 3
175 done)"
176 [ -n "$fselect" ] && BOX="$BOX <hbox>
177 <entry>
178 <variable>OUTPUT</variable>
179 </entry>
180 <button>
181 <input file stock=\"gtk-open\"></input>
182 <variable>FILE_BROWSE</variable>
183 <action type=\"fileselect\">OUTPUT</action>
184 </button>
185 </hbox>
186 "
187 [ "$yeslabel" = "ok" ] && yeslabel="" || yeslabel="
188 <label>$yeslabel</label>"
189 [ "$nolabel" = "cancel" ] && nolabel="" || nolabel="
190 <label>$nolabel</label>"
191 [ -n "$buttonok$buttonextra$buttoncancel" ] && BOX="$BOX <hbox>"
192 [ -n "$buttonok" ] && BOX="$BOX <button ok>$yeslabel
193 <action type=\"exit\">0</action>
194 </button>
195 "
196 [ -n "$buttonextra" ] && BOX="$BOX <button>
197 <label>$extralabel</label>
198 <input file icon=\"forward\"> </input>
199 <action type=\"exit\">3</action>
200 </button>
201 "
202 [ -n "$buttoncancel" ] && BOX="$BOX <button cancel>$nolabel
203 <action type=\"exit\">1</action>
204 </button>
205 "
206 [ -n "$buttonhelp" ] && BOX="$BOX <button help>
207 <action type=\"exit\">2</action>
208 </button>
209 "
210 [ -n "$buttonok$buttonextra$buttoncancel" ] && BOX="$BOX </hbox>
211 "
212 BOX="$BOX </vbox>
213 </window>
214 "
215 export BOX
216 exit $(fake_dialog)