Debian Codegraph @

Debconf/FrontEnd/Dialog.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::FrontEnd::Dialog - dialog FrontEnd
6
7 =cut
8
9 package Debconf::FrontEnd::Dialog;
10 use warnings;
11 use strict;
12 use Debconf::Gettext;
13 use Debconf::Priority;
14 use Debconf::TmpFile;
15 use Debconf::Log qw(:all);
16 use Debconf::Encoding qw(wrap $columns width);
17 use Debconf::Path;
18 use IPC::Open3;
19 use POSIX;
20 use Fcntl;
21 use base qw(Debconf::FrontEnd::ScreenSize);
22
23 =head1 DESCRIPTION
24
25 This FrontEnd is for a user interface based on dialog or whiptail.
26 It will use whichever is available, but prefers to use whiptail if available.
27 It handles all the messy communication with these programs.
28
29 =head1 METHODS
30
31 =over 4
32
33 =item init
34
35 Checks to see if whiptail, or dialog are available, in that
36 order. To make it use dialog, set DEBCONF_FORCE_DIALOG in the environment.
37
38 =cut
39
40 sub init {
41 my $this=shift;
42
43 $this->SUPER::init(@_);
44
45 # These environment variable screws up at least whiptail with the
46 # way we call it. Posix does not allow safe arg passing like
47 # whiptail needs.
48 delete $ENV{POSIXLY_CORRECT} if exists $ENV{POSIXLY_CORRECT};
49 delete $ENV{POSIX_ME_HARDER} if exists $ENV{POSIX_ME_HARDER};
50
51 # Detect all the ways people have managed to screw up their
52 # terminals (so far...)
53 if (! exists $ENV{TERM} || ! defined $ENV{TERM} || $ENV{TERM} eq '') {
54 die gettext("TERM is not set, so the dialog frontend is not usable.")."\n";
55 }
56 elsif ($ENV{TERM} =~ /emacs/i) {
57 die gettext("Dialog frontend is incompatible with emacs shell buffers")."\n";
58 }
59 elsif ($ENV{TERM} eq 'dumb' || $ENV{TERM} eq 'unknown') {
60 die gettext("Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.")."\n";
61 }
62
63 $this->interactive(1);
64 $this->capb('backup');
65
66 # Autodetect if whiptail or dialog is available and set magic numbers.
67 if (Debconf::Path::find("whiptail") &&
68 (! defined $ENV{DEBCONF_FORCE_DIALOG} || ! Debconf::Path::find("dialog")) &&
69 (! defined $ENV{DEBCONF_FORCE_XDIALOG} || ! Debconf::Path::find("Xdialog")) &&
70 system('whiptail --version >/dev/null 2>&1') == 0) {
71 $this->program('whiptail');
72 $this->dashsep('--');
73 $this->borderwidth(5);
74 $this->borderheight(6);
75 $this->spacer(1);
76 $this->titlespacer(10);
77 $this->columnspacer(3);
78 $this->selectspacer(13);
79 $this->hasoutputfd(1);
80 }
81 elsif (Debconf::Path::find("dialog") &&
82 (! defined $ENV{DEBCONF_FORCE_XDIALOG} || ! Debconf::Path::find("Xdialog")) &&
83 system('dialog --version >/dev/null 2>&1') == 0) {
84 $this->program('dialog');
85 $this->dashsep(''); # dialog does not need (or support)
86 # double-dash separation
87 $this->borderwidth(7);
88 $this->borderheight(6);
89 $this->spacer(0);
90 $this->titlespacer(4);
91 $this->columnspacer(2);
92 $this->selectspacer(0);
93 $this->hasoutputfd(1);
94 }
95 elsif (Debconf::Path::find("Xdialog") && defined $ENV{DISPLAY}) {
96 $this->program("Xdialog");
97 $this->borderwidth(7);
98 $this->borderheight(20);
99 $this->spacer(0);
100 $this->titlespacer(10);
101 $this->selectspacer(0);
102 $this->columnspacer(2);
103 # Depends on its geometry. Anything is possible, but
104 # this is reasonable.
105 $this->screenheight(200);
106 }
107 else {
108 die gettext("No usable dialog-like program is installed, so the dialog based frontend cannot be used.");
109 }
110
111 # Whiptail and dialog can't deal with very small screens. Detect
112 # this and fail, forcing use of some other frontend.
113 # The numbers were arrived at by experimentation.
114 if ($this->screenheight < 13 || $this->screenwidth < 31) {
115 die gettext("Dialog frontend requires a screen at least 13 lines tall and 31 columns wide.")."\n";
116 }
117 }
118
119 =item sizetext
120
121 Dialog and whiptail have an annoying field of requiring you specify
122 their dimensions explicitly. This function handles doing that. Just pass in
123 the text that will be displayed in the dialog, and it will spit out new text,
124 formatted nicely, then the height for the dialog, and then the width for the
125 dialog.
126
127 =cut
128
129 sub sizetext {
130 my $this=shift;
131 my $text=shift;
132
133 # Try to guess how many lines the text will take up in the dialog.
134 # This is difficult because long lines are wrapped. So what I'll do
135 # is pre-wrap the text and then just look at the number of lines it
136 # takes up.
137 $columns = $this->screenwidth - $this->borderwidth - $this->columnspacer;
138 $text=wrap('', '', $text);
139 my @lines=split(/\n/, $text);
140
141 # Now figure out what's the longest line. Look at the title size
142 # too. Note use of width function to count columns, not just
143 # characters.
144 my $window_columns=width($this->title) + $this->titlespacer;
145 map {
146 my $w=width($_);
147 $window_columns = $w if $w > $window_columns;
148 } @lines;
149
150 return $text, $#lines + 1 + $this->borderheight,
151 $window_columns + $this->borderwidth;
152 }
153
154 =item ellipsize
155
156 Items in select and multiselect questions must be trimmed to fit when using
157 whiptail, or else the window may overflow the screen. This function handles
158 that: pass it a line of text and it will truncate it if necessary, adding a
159 trailing ellipsis.
160
161 =cut
162
163 sub ellipsize {
164 my $this=shift;
165 my $text=shift;
166
167 # We only need to do this for whiptail; dialog truncates items by
168 # itself.
169 return $text if $this->program ne 'whiptail';
170
171 $columns = $this->screenwidth - $this->borderwidth - $this->columnspacer - $this->selectspacer;
172 if (width($text) > $columns) {
173 # Ideally we might use Unicode::Truncate, but that isn't
174 # currently in Debian and extra dependencies are awkward
175 # here. Truncating in a way that respects the character
176 # encoding and doesn't leave us with partial multi-byte
177 # characters seems to be really hard in Perl. As a cheap
178 # hack, wrap the text (which at least honours character
179 # encoding if Text::WrapI18N and Text::CharWidth are
180 # installed) and take the first line.
181 $columns -= 3;
182 $text = (split(/\n/, wrap('', '', $text), 2))[0] . '...';
183 }
184 return $text;
185 }
186
187 =item hide_escape
188
189 Used to hide escaped characters in input text from processing by dialog.
190
191 =cut
192
193 sub hide_escape {
194 my $line = $_;
195
196 # dialog will display "\n" as a literal newline; use zero-width
197 # utf-8 characters to avoid this.
198 $line =~ s/\\n/\\\xe2\x81\xa0n/g;
199 return $line;
200 }
201
202 =item showtext
203
204 Pass this some text and it will display the text to the user in
205 a dialog. If the text is too long to fit in one dialog, it will use a
206 scrollable dialog.
207
208 =cut
209
210 sub showtext {
211 my $this=shift;
212 my $question=shift;
213 my $intext=shift;
214
215 my $lines = $this->screenheight;
216 my ($text, $height, $width)=$this->sizetext($intext);
217
218 my @lines = split(/\n/, $text);
219 my $num;
220 my @args=('--msgbox', join("\n", @lines));
221 if ($lines - 4 - $this->borderheight <= $#lines) {
222 $num=$lines - 4 - $this->borderheight;
223 if ($this->program eq 'whiptail') {
224 # Whiptail can scroll text easily.
225 push @args, '--scrolltext';
226 }
227 else {
228 # Dialog has to use a temp file.
229 my $fh=Debconf::TmpFile::open();
230 print $fh join("\n", map { &hide_escape } @lines);
231 close $fh;
232 @args=("--textbox", Debconf::TmpFile::filename());
233 }
234 }
235 else {
236 $num=$#lines + 1;
237 }
238 $this->showdialog($question, @args, $num + $this->borderheight, $width);
239 if ($args[0] eq '--textbox') {
240 Debconf::TmpFile::cleanup();
241 }
242 }
243
244 =item makeprompt
245
246 This is a helper function used by some dialog Elements. Pass it the Question
247 that is going to be displayed. It will use this to generate a prompt, using
248 both the short and long descriptions of the Question.
249
250 You can optionally pass in a second parameter: a number. This can be used to
251 tune how many lines are free on the screen.
252
253 If the prompt is too large to fit on the screen, it will instead be displayed
254 immediatly, and the prompt will be changed to just the short description.
255
256 The return value is identical to the return value of sizetext() run on the
257 generated prompt.
258
259 =cut
260
261 sub makeprompt {
262 my $this=shift;
263 my $question=shift;
264 my $freelines=$this->screenheight - $this->borderheight + 1;
265 $freelines += shift if @_;
266
267 my ($text, $lines, $columns)=$this->sizetext(
268 $question->extended_description."\n\n".
269 $question->description
270 );
271
272 if ($lines > $freelines) {
273 $this->showtext($question, $question->extended_description);
274 ($text, $lines, $columns)=$this->sizetext($question->description);
275 }
276
277 return ($text, $lines, $columns);
278 }
279
280 sub startdialog {
281 my $this=shift;
282 my $question=shift;
283 my $wantinputfd=shift;
284
285 debug debug => "preparing to run dialog. Params are:" ,
286 join(",", $this->program, @_);
287
288 # Save stdout, stdin, the open3 below messes with them.
289 our ($saveout, $savein);
290 open($saveout, ">&", \*STDOUT) || die $!;
291 $this->dialog_saveout($saveout);
292 if ($wantinputfd) {
293 $this->dialog_savein(undef);
294 } else {
295 open($savein, "<&", \*STDIN) || die $!;
296 $this->dialog_savein($savein);
297 }
298
299 # If warnings are enabled by $^W, they are actually printed to
300 # stdout by IPC::Open3 and get stored in $stdout below!
301 # So they must be disabled.
302 $this->dialog_savew($^W);
303 $^W=0;
304
305 unless ($this->capb_backup || grep { $_ eq '--defaultno' } @_) {
306 if ($this->program ne 'Xdialog') {
307 unshift @_, '--nocancel';
308 }
309 else {
310 unshift @_, '--no-cancel';
311 }
312 }
313
314 if ($this->program eq 'Xdialog' && $_[0] eq '--passwordbox') {
315 $_[0]='--password --inputbox'
316 }
317
318 # Set up a pipe to the output fd, before calling open3.
319 use vars qw{*OUTPUT_RDR *OUTPUT_WTR};
320 if ($this->hasoutputfd) {
321 pipe(OUTPUT_RDR, OUTPUT_WTR) || die "pipe: $!";
322 my $flags=fcntl(\*OUTPUT_WTR, F_GETFD, 0);
323 fcntl(\*OUTPUT_WTR, F_SETFD, $flags & ~FD_CLOEXEC);
324 $this->dialog_output_rdr(\*OUTPUT_RDR);
325 unshift @_, "--output-fd", fileno(\*OUTPUT_WTR);
326 }
327
328 my $backtitle='';
329 if (defined $this->info) {
330 $backtitle = $this->info->description;
331 } else {
332 $backtitle = gettext("Package configuration");
333 }
334
335 use vars qw{*INPUT_RDR *INPUT_WTR};
336 if ($wantinputfd) {
337 pipe(INPUT_RDR, INPUT_WTR) || die "pipe: $!";
338 autoflush INPUT_WTR 1;
339 my $flags=fcntl(\*INPUT_RDR, F_GETFD, 0);
340 fcntl(\*INPUT_RDR, F_SETFD, $flags & ~FD_CLOEXEC);
341 $this->dialog_input_wtr(\*INPUT_WTR);
342 } else {
343 $this->dialog_input_wtr(undef);
344 }
345
346 use vars qw{*ERRFH};
347 my $pid = open3($wantinputfd ? '<&INPUT_RDR' : '<&STDIN', '>&STDOUT',
348 \*ERRFH, $this->program,
349 '--backtitle', $backtitle,
350 '--title', $this->title, @_);
351 $this->dialog_errfh(\*ERRFH);
352 $this->dialog_pid($pid);
353 close OUTPUT_WTR if $this->hasoutputfd;
354 }
355
356 sub waitdialog {
357 my $this=shift;
358
359 my $input_wtr=$this->dialog_input_wtr;
360 if ($input_wtr) {
361 close $input_wtr;
362 }
363 my $output_rdr=$this->dialog_output_rdr;
364 my $errfh=$this->dialog_errfh;
365 my $output='';
366 if ($this->hasoutputfd) {
367 while (<$output_rdr>) {
368 $output.=$_;
369 }
370 my $error=0;
371 while (<$errfh>) {
372 print STDERR $_;
373 $error++;
374 }
375 if ($error) {
376 die sprintf("debconf: %s output the above errors, giving up!", $this->program)."\n";
377 }
378 }
379 else {
380 while (<$errfh>) { # ugh
381 $output.=$_;
382 }
383 }
384 chomp $output;
385
386 # Have to put the wait here to make sure $? is set properly.
387 waitpid($this->dialog_pid, 0);
388 $^W=$this->dialog_savew;
389
390 # Restore stdin, stdout. Must be this way round because open3 closed
391 # stdin, and if we dup onto stdout first Perl tries to use the free
392 # fd 0 as a temporary fd and then warns about reopening STDIN as
393 # STDOUT.
394 if (defined $this->dialog_savein) {
395 open(STDIN, '<&', $this->dialog_savein) || die $!;
396 }
397 open(STDOUT, '>&', $this->dialog_saveout) || die $!;
398
399 # Now check dialog's return code to see if escape (255 (really -1)) or
400 # Cancel (1) were hit. If so, make a note that we should back up.
401 #
402 # To complicate things, a return code of 1 also means that yes was
403 # selected from a yes/no dialog, so we must parse the parameters
404 # to see if such a dialog was displayed.
405 my $ret=$? >> 8;
406 if ($ret == 255 || ($ret == 1 && join(' ', @_) !~ m/--yesno\s/)) {
407 $this->backup(1);
408 return;
409 }
410
411 if (wantarray) {
412 return $ret, $output;
413 }
414 else {
415 return $output;
416 }
417 }
418
419 =item showdialog
420
421 Displays a dialog. After the first parameters which should point to the question
422 being displayed, all remaining parameters are passed to whiptail/dialog.
423
424 If called in a scalar context, returns whatever dialog outputs to stderr.
425 If called in a list context, returns the return code of dialog, then the
426 stderr output.
427
428 Note that the return code of dialog is examined, and if the user hit escape
429 or cancel, this frontend will assume they wanted to back up. In that case,
430 showdialog will return undef.
431
432 =cut
433
434 sub showdialog {
435 my $this=shift;
436 my $question=shift;
437
438 @_=map { &hide_escape } @_;
439
440 # It's possible to ask questions in the middle of a progress bar.
441 # However, whiptail doesn't like having two instances of itself
442 # trying to talk to the same terminal, so we need to shut the
443 # progress bar down temporarily.
444 if (defined $this->progress_bar) {
445 $this->progress_bar->stop;
446 }
447
448 $this->startdialog($question, 0, @_);
449 my (@ret, $ret);
450 if (wantarray) {
451 @ret=$this->waitdialog(@_);
452 } else {
453 $ret=$this->waitdialog(@_);
454 }
455
456 # Restart the progress bar if necessary.
457 if (defined $this->progress_bar) {
458 $this->progress_bar->start;
459 }
460
461 if (wantarray) {
462 return @ret;
463 } else {
464 return $ret;
465 }
466 }
467
468 =back
469
470 =head1 AUTHOR
471
472 Joey Hess <[email protected]>
473
474 =cut
475
476 1