1217309Snwhitehorn# Functions that handle calling dialog(1) -*-perl-*- 2217309Snwhitehorn# $Id: dialog.pl,v 1.4 2001/10/13 00:40:22 tom Exp $ 3217309Snwhitehorn 4217309Snwhitehorn# Return values are 1 for success and 0 for failure (or cancel) 5217309Snwhitehorn# Resultant text (if any) is in dialog_result 6217309Snwhitehorn 7217309Snwhitehorn# Unfortunately, the gauge requires use of /bin/sh to get going. 8217309Snwhitehorn# I didn't bother to make the others shell-free, although it 9217309Snwhitehorn# would be simple to do. 10217309Snwhitehorn 11217309Snwhitehorn# Note that dialog generally returns 0 for success, so I invert the 12217309Snwhitehorn# sense of the return code for more readable boolean expressions. 13217309Snwhitehorn 14217309Snwhitehorn$scr_lines = 24; 15217309Snwhitehorn 16217309Snwhitehornrequire "flush.pl"; 17217309Snwhitehorn 18217309Snwhitehornsub rhs_clear { 19217309Snwhitehorn return system("dialog --clear"); 20217309Snwhitehorn} 21217309Snwhitehorn 22217309Snwhitehornsub rhs_textbox { 23217309Snwhitehorn local ( $title, $file, $width, $height ) = @_; 24217309Snwhitehorn 25217309Snwhitehorn system("dialog --title \"$title\" --textbox $file $height $width"); 26217309Snwhitehorn 27217309Snwhitehorn return 1; 28217309Snwhitehorn} 29217309Snwhitehorn 30217309Snwhitehornsub rhs_msgbox { 31217309Snwhitehorn local ( $title, $message, $width ) = @_; 32217309Snwhitehorn local ( $tmp, $height, $message_len ); 33217309Snwhitehorn 34217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 35217309Snwhitehorn $message_len = split(/^/, $message); 36217309Snwhitehorn $tmp = $message; 37217309Snwhitehorn if (chop($tmp) eq "\n") { 38217309Snwhitehorn $message_len++; 39217309Snwhitehorn } 40217309Snwhitehorn $height = 4 + $message_len; 41217309Snwhitehorn 42217309Snwhitehorn $tmp = system("dialog --title \"$title\" --msgbox \"$message\" $height $width"); 43217309Snwhitehorn if ($tmp) { 44217309Snwhitehorn return 0; 45217309Snwhitehorn } else { 46217309Snwhitehorn return 1; 47217309Snwhitehorn } 48217309Snwhitehorn} 49217309Snwhitehorn 50217309Snwhitehornsub rhs_infobox { 51217309Snwhitehorn local ( $title, $message, $width ) = @_; 52217309Snwhitehorn local ( $tmp, $height, $message_len ); 53217309Snwhitehorn 54217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 55217309Snwhitehorn $message_len = split(/^/, $message); 56217309Snwhitehorn $tmp = $message; 57217309Snwhitehorn if (chop($tmp) eq "\n") { 58217309Snwhitehorn $message_len++; 59217309Snwhitehorn } 60217309Snwhitehorn $height = 2 + $message_len; 61217309Snwhitehorn 62217309Snwhitehorn return system("dialog --title \"$title\" --infobox \"$message\" $height $width"); 63217309Snwhitehorn} 64217309Snwhitehorn 65217309Snwhitehornsub rhs_yesno { 66217309Snwhitehorn local ( $title, $message, $width ) = @_; 67217309Snwhitehorn local ( $tmp, $height, $message_len ); 68217309Snwhitehorn 69217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 70217309Snwhitehorn $message_len = split(/^/, $message); 71217309Snwhitehorn $tmp = $message; 72217309Snwhitehorn if (chop($tmp) eq "\n") { 73217309Snwhitehorn $message_len++; 74217309Snwhitehorn } 75217309Snwhitehorn $height = 4 + $message_len; 76217309Snwhitehorn 77217309Snwhitehorn $tmp = system("dialog --title \"$title\" --yesno \"$message\" $height $width"); 78217309Snwhitehorn # Dumb: dialog returns 0 for "yes" and 1 for "no" 79217309Snwhitehorn if (! $tmp) { 80217309Snwhitehorn return 1; 81217309Snwhitehorn } else { 82217309Snwhitehorn return 0; 83217309Snwhitehorn } 84217309Snwhitehorn} 85217309Snwhitehorn 86217309Snwhitehornsub rhs_gauge { 87217309Snwhitehorn local ( $title, $message, $width, $percent ) = @_; 88217309Snwhitehorn local ( $tmp, $height, $message_len ); 89217309Snwhitehorn 90217309Snwhitehorn $gauge_width = $width; 91217309Snwhitehorn 92217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 93217309Snwhitehorn $message_len = split(/^/, $message); 94217309Snwhitehorn $tmp = $message; 95217309Snwhitehorn if (chop($tmp) eq "\n") { 96217309Snwhitehorn $message_len++; 97217309Snwhitehorn } 98217309Snwhitehorn $height = 5 + $message_len; 99217309Snwhitehorn 100217309Snwhitehorn open(GAUGE, "|dialog --title \"$title\" --gauge \"$message\" $height $width $percent"); 101217309Snwhitehorn} 102217309Snwhitehorn 103217309Snwhitehornsub rhs_update_gauge { 104217309Snwhitehorn local ( $percent ) = @_; 105217309Snwhitehorn 106217309Snwhitehorn &printflush(GAUGE, "$percent\n"); 107217309Snwhitehorn} 108217309Snwhitehorn 109217309Snwhitehornsub rhs_update_gauge_and_message { 110217309Snwhitehorn local ( $message, $percent ) = @_; 111217309Snwhitehorn 112217309Snwhitehorn $message = &rhs_wordwrap($message, $gauge_width); 113217309Snwhitehorn $message =~ s/\n/\\n/g; 114217309Snwhitehorn &printflush(GAUGE, "XXX\n$percent\n$message\nXXX\n"); 115217309Snwhitehorn} 116217309Snwhitehorn 117217309Snwhitehornsub rhs_stop_gauge { 118217309Snwhitehorn close GAUGE; 119217309Snwhitehorn} 120217309Snwhitehorn 121217309Snwhitehornsub rhs_inputbox { 122217309Snwhitehorn local ( $title, $message, $width, $instr ) = @_; 123217309Snwhitehorn local ( $tmp, $height, $message_len ); 124217309Snwhitehorn 125217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 126217309Snwhitehorn $message_len = split(/^/, $message); 127217309Snwhitehorn $tmp = $message; 128217309Snwhitehorn if (chop($tmp) eq "\n") { 129217309Snwhitehorn $message_len++; 130217309Snwhitehorn } 131217309Snwhitehorn $height = 7 + $message_len; 132217309Snwhitehorn 133217309Snwhitehorn return &return_output(0, "dialog --title \"$title\" --inputbox \"$message\" $height $width \"$instr\""); 134217309Snwhitehorn} 135217309Snwhitehorn 136217309Snwhitehornsub rhs_menu { 137217309Snwhitehorn local ( $title, $message, $width, $numitems ) = @_; 138217309Snwhitehorn local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len ); 139217309Snwhitehorn 140217309Snwhitehorn shift; shift; shift; shift; 141217309Snwhitehorn 142217309Snwhitehorn @list = (); 143217309Snwhitehorn for ($i = 0; $i < $numitems; $i++) { 144217309Snwhitehorn $ent = shift; 145217309Snwhitehorn $list[@list] = "\"$ent\""; 146217309Snwhitehorn $ent = shift; 147217309Snwhitehorn $list[@list] = "\"$ent\""; 148217309Snwhitehorn } 149217309Snwhitehorn 150217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 151217309Snwhitehorn 152217309Snwhitehorn $message_len = split(/^/, $message); 153217309Snwhitehorn $tmp = $message; 154217309Snwhitehorn if (chop($tmp) eq "\n") { 155217309Snwhitehorn $message_len++; 156217309Snwhitehorn } 157217309Snwhitehorn 158217309Snwhitehorn $height = $message_len + 6 + $numitems; 159217309Snwhitehorn if ($height <= $scr_lines) { 160217309Snwhitehorn $menuheight = $numitems; 161217309Snwhitehorn } else { 162217309Snwhitehorn $height = $scr_lines; 163217309Snwhitehorn $menuheight = $scr_lines - $message_len - 6; 164217309Snwhitehorn } 165217309Snwhitehorn 166217309Snwhitehorn return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list"); 167217309Snwhitehorn} 168217309Snwhitehorn 169217309Snwhitehornsub rhs_menul { 170217309Snwhitehorn local ( $title, $message, $width, $numitems ) = @_; 171217309Snwhitehorn local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len ); 172217309Snwhitehorn 173217309Snwhitehorn shift; shift; shift; shift; 174217309Snwhitehorn 175217309Snwhitehorn @list = (); 176217309Snwhitehorn for ($i = 0; $i < $numitems; $i++) { 177217309Snwhitehorn $ent = shift; 178217309Snwhitehorn $list[@list] = "\"$ent\""; 179217309Snwhitehorn $list[@list] = "\"\""; 180217309Snwhitehorn } 181217309Snwhitehorn 182217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 183217309Snwhitehorn 184217309Snwhitehorn $message_len = split(/^/, $message); 185217309Snwhitehorn $tmp = $message; 186217309Snwhitehorn if (chop($tmp) eq "\n") { 187217309Snwhitehorn $message_len++; 188217309Snwhitehorn } 189217309Snwhitehorn 190217309Snwhitehorn $height = $message_len + 6 + $numitems; 191217309Snwhitehorn if ($height <= $scr_lines) { 192217309Snwhitehorn $menuheight = $numitems; 193217309Snwhitehorn } else { 194217309Snwhitehorn $height = $scr_lines; 195217309Snwhitehorn $menuheight = $scr_lines - $message_len - 6; 196217309Snwhitehorn } 197217309Snwhitehorn 198217309Snwhitehorn return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list"); 199217309Snwhitehorn} 200217309Snwhitehorn 201217309Snwhitehornsub rhs_menua { 202217309Snwhitehorn local ( $title, $message, $width, %items ) = @_; 203217309Snwhitehorn local ( $tmp, $ent, $height, $menuheight, @list, $message_len ); 204217309Snwhitehorn 205217309Snwhitehorn @list = (); 206217309Snwhitehorn foreach $ent (sort keys (%items)) { 207217309Snwhitehorn $list[@list] = "\"$ent\""; 208217309Snwhitehorn $list[@list] = "\"$items{$ent}\""; 209217309Snwhitehorn } 210217309Snwhitehorn 211217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 212217309Snwhitehorn 213217309Snwhitehorn $message_len = split(/^/, $message); 214217309Snwhitehorn $tmp = $message; 215217309Snwhitehorn if (chop($tmp) eq "\n") { 216217309Snwhitehorn $message_len++; 217217309Snwhitehorn } 218217309Snwhitehorn 219217309Snwhitehorn $numitems = keys(%items); 220217309Snwhitehorn $height = $message_len + 6 + $numitems; 221217309Snwhitehorn if ($height <= $scr_lines) { 222217309Snwhitehorn $menuheight = $numitems; 223217309Snwhitehorn } else { 224217309Snwhitehorn $height = $scr_lines; 225217309Snwhitehorn $menuheight = $scr_lines - $message_len - 6; 226217309Snwhitehorn } 227217309Snwhitehorn 228217309Snwhitehorn return &return_output(0, "dialog --title \"$title\" --menu \"$message\" $height $width $menuheight @list"); 229217309Snwhitehorn} 230217309Snwhitehorn 231217309Snwhitehornsub rhs_checklist { 232217309Snwhitehorn local ( $title, $message, $width, $numitems ) = @_; 233217309Snwhitehorn local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len ); 234217309Snwhitehorn 235217309Snwhitehorn shift; shift; shift; shift; 236217309Snwhitehorn 237217309Snwhitehorn @list = (); 238217309Snwhitehorn for ($i = 0; $i < $numitems; $i++) { 239217309Snwhitehorn $ent = shift; 240217309Snwhitehorn $list[@list] = "\"$ent\""; 241217309Snwhitehorn $ent = shift; 242217309Snwhitehorn $list[@list] = "\"$ent\""; 243217309Snwhitehorn $ent = shift; 244217309Snwhitehorn if ($ent) { 245217309Snwhitehorn $list[@list] = "ON"; 246217309Snwhitehorn } else { 247217309Snwhitehorn $list[@list] = "OFF"; 248217309Snwhitehorn } 249217309Snwhitehorn } 250217309Snwhitehorn 251217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 252217309Snwhitehorn 253217309Snwhitehorn $message_len = split(/^/, $message); 254217309Snwhitehorn $tmp = $message; 255217309Snwhitehorn if (chop($tmp) eq "\n") { 256217309Snwhitehorn $message_len++; 257217309Snwhitehorn } 258217309Snwhitehorn 259217309Snwhitehorn $height = $message_len + 6 + $numitems; 260217309Snwhitehorn if ($height <= $scr_lines) { 261217309Snwhitehorn $menuheight = $numitems; 262217309Snwhitehorn } else { 263217309Snwhitehorn $height = $scr_lines; 264217309Snwhitehorn $menuheight = $scr_lines - $message_len - 6; 265217309Snwhitehorn } 266217309Snwhitehorn 267217309Snwhitehorn return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list"); 268217309Snwhitehorn} 269217309Snwhitehorn 270217309Snwhitehornsub rhs_checklistl { 271217309Snwhitehorn local ( $title, $message, $width, $numitems ) = @_; 272217309Snwhitehorn local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len ); 273217309Snwhitehorn 274217309Snwhitehorn shift; shift; shift; shift; 275217309Snwhitehorn 276217309Snwhitehorn @list = (); 277217309Snwhitehorn for ($i = 0; $i < $numitems; $i++) { 278217309Snwhitehorn $ent = shift; 279217309Snwhitehorn $list[@list] = "\"$ent\""; 280217309Snwhitehorn $list[@list] = "\"\""; 281217309Snwhitehorn $list[@list] = "OFF"; 282217309Snwhitehorn } 283217309Snwhitehorn 284217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 285217309Snwhitehorn 286217309Snwhitehorn $message_len = split(/^/, $message); 287217309Snwhitehorn $tmp = $message; 288217309Snwhitehorn if (chop($tmp) eq "\n") { 289217309Snwhitehorn $message_len++; 290217309Snwhitehorn } 291217309Snwhitehorn 292217309Snwhitehorn $height = $message_len + 6 + $numitems; 293217309Snwhitehorn if ($height <= $scr_lines) { 294217309Snwhitehorn $menuheight = $numitems; 295217309Snwhitehorn } else { 296217309Snwhitehorn $height = $scr_lines; 297217309Snwhitehorn $menuheight = $scr_lines - $message_len - 6; 298217309Snwhitehorn } 299217309Snwhitehorn return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list"); 300217309Snwhitehorn} 301217309Snwhitehorn 302217309Snwhitehornsub rhs_checklista { 303217309Snwhitehorn local ( $title, $message, $width, %items ) = @_; 304217309Snwhitehorn local ( $tmp, $ent, $height, $menuheight, @list, $message_len ); 305217309Snwhitehorn 306217309Snwhitehorn shift; shift; shift; shift; 307217309Snwhitehorn 308217309Snwhitehorn @list = (); 309217309Snwhitehorn foreach $ent (sort keys (%items)) { 310217309Snwhitehorn $list[@list] = "\"$ent\""; 311217309Snwhitehorn $list[@list] = "\"$items{$ent}\""; 312217309Snwhitehorn $list[@list] = "OFF"; 313217309Snwhitehorn } 314217309Snwhitehorn 315217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 316217309Snwhitehorn 317217309Snwhitehorn $message_len = split(/^/, $message); 318217309Snwhitehorn $tmp = $message; 319217309Snwhitehorn if (chop($tmp) eq "\n") { 320217309Snwhitehorn $message_len++; 321217309Snwhitehorn } 322217309Snwhitehorn 323217309Snwhitehorn $numitems = keys(%items); 324217309Snwhitehorn $height = $message_len + 6 + $numitems; 325217309Snwhitehorn if ($height <= $scr_lines) { 326217309Snwhitehorn $menuheight = $numitems; 327217309Snwhitehorn } else { 328217309Snwhitehorn $height = $scr_lines; 329217309Snwhitehorn $menuheight = $scr_lines - $message_len - 6; 330217309Snwhitehorn } 331217309Snwhitehorn 332217309Snwhitehorn return &return_output("list", "dialog --title \"$title\" --separate-output --checklist \"$message\" $height $width $menuheight @list"); 333217309Snwhitehorn} 334217309Snwhitehorn 335217309Snwhitehornsub rhs_radiolist { 336217309Snwhitehorn local ( $title, $message, $width, $numitems ) = @_; 337217309Snwhitehorn local ( $i, $tmp, $ent, $height, $menuheight, @list, $message_len ); 338217309Snwhitehorn 339217309Snwhitehorn shift; shift; shift; shift; 340217309Snwhitehorn 341217309Snwhitehorn @list = (); 342217309Snwhitehorn for ($i = 0; $i < $numitems; $i++) { 343217309Snwhitehorn $ent = shift; 344217309Snwhitehorn $list[@list] = "\"$ent\""; 345217309Snwhitehorn $ent = shift; 346217309Snwhitehorn $list[@list] = "\"$ent\""; 347217309Snwhitehorn $ent = shift; 348217309Snwhitehorn if ($ent) { 349217309Snwhitehorn $list[@list] = "ON"; 350217309Snwhitehorn } else { 351217309Snwhitehorn $list[@list] = "OFF"; 352217309Snwhitehorn } 353217309Snwhitehorn } 354217309Snwhitehorn 355217309Snwhitehorn $message = &rhs_wordwrap($message, $width); 356217309Snwhitehorn 357217309Snwhitehorn $message_len = split(/^/, $message); 358217309Snwhitehorn $tmp = $message; 359217309Snwhitehorn if (chop($tmp) eq "\n") { 360217309Snwhitehorn $message_len++; 361217309Snwhitehorn } 362217309Snwhitehorn 363217309Snwhitehorn $height = $message_len + 6 + $numitems; 364217309Snwhitehorn if ($height <= $scr_lines) { 365217309Snwhitehorn $menuheight = $numitems; 366217309Snwhitehorn } else { 367217309Snwhitehorn $height = $scr_lines; 368217309Snwhitehorn $menuheight = $scr_lines - $message_len - 6; 369217309Snwhitehorn } 370217309Snwhitehorn 371217309Snwhitehorn return &return_output(0 , "dialog --title \"$title\" --radiolist \"$message\" $height $width $menuheight @list"); 372217309Snwhitehorn} 373217309Snwhitehorn 374217309Snwhitehornsub return_output { 375217309Snwhitehorn local ( $listp, $command ) = @_; 376217309Snwhitehorn local ( $res ) = 1; 377217309Snwhitehorn 378217309Snwhitehorn pipe(PARENT_READER, CHILD_WRITER); 379217309Snwhitehorn # We have to fork (as opposed to using "system") so that the parent 380217309Snwhitehorn # process can read from the pipe to avoid deadlock. 381217309Snwhitehorn my ($pid) = fork; 382217309Snwhitehorn if ($pid == 0) { # child 383217309Snwhitehorn close(PARENT_READER); 384217309Snwhitehorn open(STDERR, ">&CHILD_WRITER"); 385217309Snwhitehorn exec($command); 386217309Snwhitehorn die("no exec"); 387217309Snwhitehorn } 388217309Snwhitehorn if ($pid > 0) { # parent 389217309Snwhitehorn close( CHILD_WRITER ); 390217309Snwhitehorn if ($listp) { 391217309Snwhitehorn @dialog_result = (); 392217309Snwhitehorn while (<PARENT_READER>) { 393217309Snwhitehorn chop; 394217309Snwhitehorn $dialog_result[@dialog_result] = $_; 395217309Snwhitehorn } 396217309Snwhitehorn } 397217309Snwhitehorn else { $dialog_result = <PARENT_READER>; } 398217309Snwhitehorn close(PARENT_READER); 399217309Snwhitehorn waitpid($pid,0); 400217309Snwhitehorn $res = $?; 401217309Snwhitehorn } 402217309Snwhitehorn 403217309Snwhitehorn # Again, dialog returns results backwards 404217309Snwhitehorn if (! $res) { 405217309Snwhitehorn return 1; 406217309Snwhitehorn } else { 407217309Snwhitehorn return 0; 408217309Snwhitehorn } 409217309Snwhitehorn} 410217309Snwhitehorn 411217309Snwhitehornsub rhs_wordwrap { 412217309Snwhitehorn local ( $intext, $width ) = @_; 413217309Snwhitehorn local ( $outtext, $i, $j, @lines, $wrap, @words, $pos, $pad ); 414217309Snwhitehorn 415217309Snwhitehorn $outtext = ""; 416217309Snwhitehorn $pad = 3; # leave 3 spaces around each line 417217309Snwhitehorn $pos = $pad; # current insert position 418217309Snwhitehorn $wrap = 0; # 1 if we have been auto wraping 419217309Snwhitehorn $insert_nl = 0; # 1 if we just did an absolute 420217309Snwhitehorn # and we should preface any new text 421217309Snwhitehorn # with a new line 422217309Snwhitehorn @lines = split(/\n/, $intext); 423217309Snwhitehorn for ($i = 0; $i <= $#lines; $i++) { 424217309Snwhitehorn if ($lines[$i] =~ /^>/) { 425217309Snwhitehorn $outtext .= "\n" if ($insert_nl); 426217309Snwhitehorn $outtext .= "\n" if ($wrap); 427217309Snwhitehorn $lines[$i] =~ /^>(.*)$/; 428217309Snwhitehorn $outtext .= $1; 429217309Snwhitehorn $insert_nl = 1; 430217309Snwhitehorn $wrap = 0; 431217309Snwhitehorn $pos = $pad; 432217309Snwhitehorn } else { 433217309Snwhitehorn $wrap = 1; 434217309Snwhitehorn @words = split(/\s+/,$lines[$i]); 435217309Snwhitehorn for ($j = 0; $j <= $#words; $j++) { 436217309Snwhitehorn if ($insert_nl) { 437217309Snwhitehorn $outtext .= "\n"; 438217309Snwhitehorn $insert_nl = 0; 439217309Snwhitehorn } 440217309Snwhitehorn if ((length($words[$j]) + $pos) > $width - $pad) { 441217309Snwhitehorn $outtext .= "\n"; 442217309Snwhitehorn $pos = $pad; 443217309Snwhitehorn } 444217309Snwhitehorn $outtext .= $words[$j] . " "; 445217309Snwhitehorn $pos += length($words[$j]) + 1; 446217309Snwhitehorn } 447217309Snwhitehorn } 448217309Snwhitehorn } 449217309Snwhitehorn 450217309Snwhitehorn return $outtext; 451217309Snwhitehorn} 452217309Snwhitehorn 453217309Snwhitehorn############ 454217309Snwhitehorn1; 455