libc/locale: Fix type breakage in __collate_range_cmp().
[freebsd-src.git] / contrib / dialog / dialog.pl
blob8fbb68fd8eb7570b99e2f7567094aa522a8f6353
1 # Functions that handle calling dialog(1) -*-perl-*-
2 # $Id: dialog.pl,v 1.4 2001/10/13 00:40:22 tom Exp $
4 # Return values are 1 for success and 0 for failure (or cancel)
5 # Resultant text (if any) is in dialog_result
7 # Unfortunately, the gauge requires use of /bin/sh to get going.
8 # I didn't bother to make the others shell-free, although it
9 # would be simple to do.
11 # Note that dialog generally returns 0 for success, so I invert the
12 # sense of the return code for more readable boolean expressions.
14 $scr_lines = 24;
16 require "flush.pl";
18 sub rhs_clear {
19 return system("dialog --clear");
22 sub rhs_textbox {
23 local ( $title, $file, $width, $height ) = @_;
25 system("dialog --title \"$title\" --textbox $file $height $width");
27 return 1;
30 sub rhs_msgbox {
31 local ( $title, $message, $width ) = @_;
32 local ( $tmp, $height, $message_len );
34 $message = &rhs_wordwrap($message, $width);
35 $message_len = split(/^/, $message);
36 $tmp = $message;
37 if (chop($tmp) eq "\n") {
38 $message_len++;
40 $height = 4 + $message_len;
42 $tmp = system("dialog --title \"$title\" --msgbox \"$message\" $height $width");
43 if ($tmp) {
44 return 0;
45 } else {
46 return 1;
50 sub rhs_infobox {
51 local ( $title, $message, $width ) = @_;
52 local ( $tmp, $height, $message_len );
54 $message = &rhs_wordwrap($message, $width);
55 $message_len = split(/^/, $message);
56 $tmp = $message;
57 if (chop($tmp) eq "\n") {
58 $message_len++;
60 $height = 2 + $message_len;
62 return system("dialog --title \"$title\" --infobox \"$message\" $height $width");
65 sub rhs_yesno {
66 local ( $title, $message, $width ) = @_;
67 local ( $tmp, $height, $message_len );
69 $message = &rhs_wordwrap($message, $width);
70 $message_len = split(/^/, $message);
71 $tmp = $message;
72 if (chop($tmp) eq "\n") {
73 $message_len++;
75 $height = 4 + $message_len;
77 $tmp = system("dialog --title \"$title\" --yesno \"$message\" $height $width");
78 # Dumb: dialog returns 0 for "yes" and 1 for "no"
79 if (! $tmp) {
80 return 1;
81 } else {
82 return 0;
86 sub rhs_gauge {
87 local ( $title, $message, $width, $percent ) = @_;
88 local ( $tmp, $height, $message_len );
90 $gauge_width = $width;
92 $message = &rhs_wordwrap($message, $width);
93 $message_len = split(/^/, $message);
94 $tmp = $message;
95 if (chop($tmp) eq "\n") {
96 $message_len++;
98 $height = 5 + $message_len;
100 open(GAUGE, "|dialog --title \"$title\" --gauge \"$message\" $height $width $percent");
103 sub rhs_update_gauge {
104 local ( $percent ) = @_;
106 &printflush(GAUGE, "$percent\n");
109 sub rhs_update_gauge_and_message {
110 local ( $message, $percent ) = @_;
112 $message = &rhs_wordwrap($message, $gauge_width);
113 $message =~ s/\n/\\n/g;
114 &printflush(GAUGE, "XXX\n$percent\n$message\nXXX\n");
117 sub rhs_stop_gauge {
118 close GAUGE;
121 sub rhs_inputbox {
122 local ( $title, $message, $width, $instr ) = @_;
123 local ( $tmp, $height, $message_len );
125 $message = &rhs_wordwrap($message, $width);
126 $message_len = split(/^/, $message);
127 $tmp = $message;
128 if (chop($tmp) eq "\n") {
129 $message_len++;
131 $height = 7 + $message_len;
133 return &return_output(0, "dialog --title \"$title\" --inputbox \"$message\" $height $width \"$instr\"");
136 sub rhs_menu {
137 local ( $title, $message, $width, $numitems ) = @_;
138 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
140 shift; shift; shift; shift;
142 @list = ();
143 for ($i = 0; $i < $numitems; $i++) {
144 $ent = shift;
145 $list[@list] = "\"$ent\"";
146 $ent = shift;
147 $list[@list] = "\"$ent\"";
150 $message = &rhs_wordwrap($message, $width);
152 $message_len = split(/^/, $message);
153 $tmp = $message;
154 if (chop($tmp) eq "\n") {
155 $message_len++;
158 $height = $message_len + 6 + $numitems;
159 if ($height <= $scr_lines) {
160 $menuheight = $numitems;
161 } else {
162 $height = $scr_lines;
163 $menuheight = $scr_lines - $message_len - 6;
166 return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
169 sub rhs_menul {
170 local ( $title, $message, $width, $numitems ) = @_;
171 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
173 shift; shift; shift; shift;
175 @list = ();
176 for ($i = 0; $i < $numitems; $i++) {
177 $ent = shift;
178 $list[@list] = "\"$ent\"";
179 $list[@list] = "\"\"";
182 $message = &rhs_wordwrap($message, $width);
184 $message_len = split(/^/, $message);
185 $tmp = $message;
186 if (chop($tmp) eq "\n") {
187 $message_len++;
190 $height = $message_len + 6 + $numitems;
191 if ($height <= $scr_lines) {
192 $menuheight = $numitems;
193 } else {
194 $height = $scr_lines;
195 $menuheight = $scr_lines - $message_len - 6;
198 return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
201 sub rhs_menua {
202 local ( $title, $message, $width, %items ) = @_;
203 local ( $tmp, $ent, $height, $menuheight, @list, $message_len );
205 @list = ();
206 foreach $ent (sort keys (%items)) {
207 $list[@list] = "\"$ent\"";
208 $list[@list] = "\"$items{$ent}\"";
211 $message = &rhs_wordwrap($message, $width);
213 $message_len = split(/^/, $message);
214 $tmp = $message;
215 if (chop($tmp) eq "\n") {
216 $message_len++;
219 $numitems = keys(%items);
220 $height = $message_len + 6 + $numitems;
221 if ($height <= $scr_lines) {
222 $menuheight = $numitems;
223 } else {
224 $height = $scr_lines;
225 $menuheight = $scr_lines - $message_len - 6;
228 return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list");
231 sub rhs_checklist {
232 local ( $title, $message, $width, $numitems ) = @_;
233 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
235 shift; shift; shift; shift;
237 @list = ();
238 for ($i = 0; $i < $numitems; $i++) {
239 $ent = shift;
240 $list[@list] = "\"$ent\"";
241 $ent = shift;
242 $list[@list] = "\"$ent\"";
243 $ent = shift;
244 if ($ent) {
245 $list[@list] = "ON";
246 } else {
247 $list[@list] = "OFF";
251 $message = &rhs_wordwrap($message, $width);
253 $message_len = split(/^/, $message);
254 $tmp = $message;
255 if (chop($tmp) eq "\n") {
256 $message_len++;
259 $height = $message_len + 6 + $numitems;
260 if ($height <= $scr_lines) {
261 $menuheight = $numitems;
262 } else {
263 $height = $scr_lines;
264 $menuheight = $scr_lines - $message_len - 6;
267 return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
270 sub rhs_checklistl {
271 local ( $title, $message, $width, $numitems ) = @_;
272 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
274 shift; shift; shift; shift;
276 @list = ();
277 for ($i = 0; $i < $numitems; $i++) {
278 $ent = shift;
279 $list[@list] = "\"$ent\"";
280 $list[@list] = "\"\"";
281 $list[@list] = "OFF";
284 $message = &rhs_wordwrap($message, $width);
286 $message_len = split(/^/, $message);
287 $tmp = $message;
288 if (chop($tmp) eq "\n") {
289 $message_len++;
292 $height = $message_len + 6 + $numitems;
293 if ($height <= $scr_lines) {
294 $menuheight = $numitems;
295 } else {
296 $height = $scr_lines;
297 $menuheight = $scr_lines - $message_len - 6;
299 return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
302 sub rhs_checklista {
303 local ( $title, $message, $width, %items ) = @_;
304 local ( $tmp, $ent, $height, $menuheight, @list, $message_len );
306 shift; shift; shift; shift;
308 @list = ();
309 foreach $ent (sort keys (%items)) {
310 $list[@list] = "\"$ent\"";
311 $list[@list] = "\"$items{$ent}\"";
312 $list[@list] = "OFF";
315 $message = &rhs_wordwrap($message, $width);
317 $message_len = split(/^/, $message);
318 $tmp = $message;
319 if (chop($tmp) eq "\n") {
320 $message_len++;
323 $numitems = keys(%items);
324 $height = $message_len + 6 + $numitems;
325 if ($height <= $scr_lines) {
326 $menuheight = $numitems;
327 } else {
328 $height = $scr_lines;
329 $menuheight = $scr_lines - $message_len - 6;
332 return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list");
335 sub rhs_radiolist {
336 local ( $title, $message, $width, $numitems ) = @_;
337 local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len );
339 shift; shift; shift; shift;
341 @list = ();
342 for ($i = 0; $i < $numitems; $i++) {
343 $ent = shift;
344 $list[@list] = "\"$ent\"";
345 $ent = shift;
346 $list[@list] = "\"$ent\"";
347 $ent = shift;
348 if ($ent) {
349 $list[@list] = "ON";
350 } else {
351 $list[@list] = "OFF";
355 $message = &rhs_wordwrap($message, $width);
357 $message_len = split(/^/, $message);
358 $tmp = $message;
359 if (chop($tmp) eq "\n") {
360 $message_len++;
363 $height = $message_len + 6 + $numitems;
364 if ($height <= $scr_lines) {
365 $menuheight = $numitems;
366 } else {
367 $height = $scr_lines;
368 $menuheight = $scr_lines - $message_len - 6;
371 return &return_output(0 , "dialog --title \"$title\" --radiolist \"$message\" $height $width $menuheight @list");
374 sub return_output {
375 local ( $listp, $command ) = @_;
376 local ( $res ) = 1;
378 pipe(PARENT_READER, CHILD_WRITER);
379 # We have to fork (as opposed to using "system") so that the parent
380 # process can read from the pipe to avoid deadlock.
381 my ($pid) = fork;
382 if ($pid == 0) { # child
383 close(PARENT_READER);
384 open(STDERR, ">&CHILD_WRITER");
385 exec($command);
386 die("no exec");
388 if ($pid > 0) { # parent
389 close( CHILD_WRITER );
390 if ($listp) {
391 @dialog_result = ();
392 while (<PARENT_READER>) {
393 chop;
394 $dialog_result[@dialog_result] = $_;
397 else { $dialog_result = <PARENT_READER>; }
398 close(PARENT_READER);
399 waitpid($pid,0);
400 $res = $?;
403 # Again, dialog returns results backwards
404 if (! $res) {
405 return 1;
406 } else {
407 return 0;
411 sub rhs_wordwrap {
412 local ( $intext, $width ) = @_;
413 local ( $outtext, $i, $j, @lines, $wrap, @words, $pos, $pad );
415 $outtext = "";
416 $pad = 3; # leave 3 spaces around each line
417 $pos = $pad; # current insert position
418 $wrap = 0; # 1 if we have been auto wraping
419 $insert_nl = 0; # 1 if we just did an absolute
420 # and we should preface any new text
421 # with a new line
422 @lines = split(/\n/, $intext);
423 for ($i = 0; $i <= $#lines; $i++) {
424 if ($lines[$i] =~ /^>/) {
425 $outtext .= "\n" if ($insert_nl);
426 $outtext .= "\n" if ($wrap);
427 $lines[$i] =~ /^>(.*)$/;
428 $outtext .= $1;
429 $insert_nl = 1;
430 $wrap = 0;
431 $pos = $pad;
432 } else {
433 $wrap = 1;
434 @words = split(/\s+/,$lines[$i]);
435 for ($j = 0; $j <= $#words; $j++) {
436 if ($insert_nl) {
437 $outtext .= "\n";
438 $insert_nl = 0;
440 if ((length($words[$j]) + $pos) > $width - $pad) {
441 $outtext .= "\n";
442 $pos = $pad;
444 $outtext .= $words[$j] . " ";
445 $pos += length($words[$j]) + 1;
450 return $outtext;
453 ############