Debian Codegraph @

Debconf/ConfModule.pm

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 Debconf::ConfModule - communicates with a ConfModule
6
7 =cut
8
9 package Debconf::ConfModule;
10 use warnings;
11 use strict;
12 use IPC::Open2;
13 use FileHandle;
14 use Debconf::Gettext;
15 use Debconf::Config;
16 use Debconf::Question;
17 use Debconf::Priority qw(priority_valid high_enough);
18 use Debconf::FrontEnd::Noninteractive;
19 use Debconf::Log ':all';
20 use Debconf::Encoding;
21 use base qw(Debconf::Base);
22
23 =head1 DESCRIPTION
24
25 This is a configuration module communication package for the Debian
26 configuration management system. It can launch a configuration module
27 script (hereafter called a "confmodule") and communicate with it. Each
28 instance of a ConfModule is connected to a separate, running confmodule.
29
30 There are a number of methods that are called in response to commands
31 from the client. Each has the same name as the command, with "command_"
32 prepended, and is fed in the parameters given after the command (split on
33 whitespace), and whatever it returns is passed back to the configuration
34 module. Each of them are described below.
35
36 =head1 FIELDS
37
38 =over 4
39
40 =item frontend
41
42 The frontend object that is used to interact with the user.
43
44 =item version
45
46 The protocol version spoken.
47
48 =item pid
49
50 The PID of the confmodule that is running and talking to this object, if
51 any.
52
53 =item write_handle
54
55 Writes to this handle are sent to the confmodule.
56
57 =item read_handle
58
59 Reads from this handle read from the confmodule.
60
61 =item caught_sigpipe
62
63 Set if we have caught a SIGPIPE signal. If it is set, the value of the
64 field should be returned, rather than the normal exit code.
65
66 =item client_capb
67
68 An array reference. If set, it will hold the capabilities the confmodule
69 reports.
70
71 =item seen
72
73 An array reference. If set, it will hold a list of all questions that have
74 ever been shown to the user in this confmodule run.
75
76 =item busy
77
78 An array reference. If set, it will hold a list of named of question that are
79 "busy" -- in the process of being shown, that cannot be unregistered right now.
80
81 =back
82
83 =head1 METHODS
84
85 =over 4
86
87 =cut
88
89 # Here I define all the numeric result codes that are used.
90 my %codes = (
91 success => 0,
92 escaped_data => 1,
93 badparams => 10,
94 syntaxerror => 20,
95 input_invisible => 30,
96 version_bad => 30,
97 go_back => 30,
98 progresscancel => 30,
99 internalerror => 100,
100 );
101
102 =item init
103
104 Called when a ConfModule is created.
105
106 =cut
107
108 sub init {
109 my $this=shift;
110
111 # Protcol version.
112 $this->version("2.0");
113
114 $this->owner('unknown') if ! defined $this->owner;
115
116 # If my frontend thought the client confmodule could backup
117 # (eg, because it was dealing earlier with a confmodule that could),
118 # tell it otherwise.
119 $this->frontend->capb_backup('');
120
121 $this->seen([]);
122 $this->busy([]);
123
124 # Let clients know a FrontEnd is actually running.
125 $ENV{DEBIAN_HAS_FRONTEND}=1;
126 }
127
128 =item startup
129
130 Pass this name name of a confmodule program, and it is started up. Any
131 further options are parameters to pass to the confmodule. You generally need
132 to do this before trying to use any of the rest of this object. The
133 alternative is to launch a confmodule manually, and connect the read_handle
134 and write_handle fields of this object to it.
135
136 =cut
137
138 sub startup {
139 my $this=shift;
140 my $confmodule=shift;
141
142 # There is an implicit clearing of any previously pending questions
143 # when a new confmodule is run.
144 $this->frontend->clear;
145 $this->busy([]);
146
147 my @args=$this->confmodule($confmodule);
148 push @args, @_ if @_;
149
150 debug developer => "starting ".join(' ',@args);
151 $this->pid(open2($this->read_handle(FileHandle->new),
152 $this->write_handle(FileHandle->new),
153 @args)) || die $!;
154
155 # Catch sigpipes so they don't kill us, and return 128 for them.
156 $this->caught_sigpipe('');
157 $SIG{PIPE}=sub { $this->caught_sigpipe(128) };
158 }
159
160 =item communicate
161
162 Read one command from the confmodule, process it, and respond
163 to it. Returns true unless there were no more commands to read.
164 This is typically called in a loop. It in turn calls various
165 command_* methods.
166
167 =cut
168
169 sub communicate {
170 my $this=shift;
171
172 my $r=$this->read_handle;
173 $_=<$r> || return $this->finish;
174 chomp;
175 my $ret=$this->process_command($_);
176 my $w=$this->write_handle;
177 print $w $ret."\n";
178 return '' unless length $ret;
179 return 1;
180 }
181
182 =item escape
183
184 Escape backslashes and newlines for output via the debconf protocol.
185
186 =cut
187
188 sub escape {
189 my $text=shift;
190 $text=~s/\\/\\\\/g;
191 $text=~s/\n/\\n/g;
192 return $text;
193 }
194
195 =item unescape_split
196
197 Unescape text received via the debconf protocol, and split by unescaped
198 whitespace.
199
200 =cut
201
202 sub unescape_split {
203 my $text=shift;
204 my @words;
205 my $word='';
206 for my $chunk (split /(\\.|\s+)/, $text) {
207 if ($chunk eq '\n') {
208 $word.="\n";
209 } elsif ($chunk=~/^\\(.)$/) {
210 $word.=$1;
211 } elsif ($chunk=~/^\s+$/) {
212 push @words, $word;
213 $word='';
214 } else {
215 $word.=$chunk;
216 }
217 }
218 push @words, $word if $word ne '';
219 return @words;
220 }
221
222 =item process_command
223
224 Pass in a raw command, and it will be processed and handled.
225
226 =cut
227
228 sub process_command {
229 my $this=shift;
230
231 debug developer => "<-- $_";
232 chomp;
233 my ($command, @params);
234 if (defined $this->client_capb and grep { $_ eq 'escape' } @{$this->client_capb}) {
235 ($command, @params)=unescape_split($_);
236 } else {
237 ($command, @params)=split(' ', $_);
238 }
239 if (! defined $command) {
240 my $ret = $codes{syntaxerror}.' '.
241 "Bad line \"$_\" received from confmodule.";
242 debug developer => "--> $ret";
243 return $ret;
244 }
245 $command=lc($command);
246 # This command could not be handled by a sub.
247 if (lc($command) eq "stop") {
248 return $this->finish;
249 }
250 # Make sure that the command is valid.
251 if (! $this->can("command_$command")) {
252 my $ret = $codes{syntaxerror}.' '.
253 "Unsupported command \"$command\" (full line was \"$_\") received from confmodule.";
254 debug developer => "--> $ret";
255 return $ret;
256 }
257 # Now call the subroutine for the command.
258 $command="command_$command";
259 my $ret=join(' ', $this->$command(@params));
260 debug developer => "--> $ret";
261 if ($ret=~/\n/) {
262 debug developer => 'Warning: return value is multiline, and would break the debconf protocol. Truncating to first line.';
263 $ret=~s/\n.*//s;
264 debug developer => "--> $ret";
265 }
266 return $ret;
267 }
268
269 =item finish
270
271 Waits for the child process (if any) to finish so its return code can be
272 examined. The return code is stored in the exitcode field of the object.
273 It also marks all questions that were shown as seen.
274
275 =cut
276
277 sub finish {
278 my $this=shift;
279
280 waitpid $this->pid, 0 if defined $this->pid;
281 $this->exitcode($this->caught_sigpipe || ($? >> 8));
282
283 # Stop catching sigpipe now. IGNORE and DEFAULT both cause obscure
284 # failures, BTW.
285 $SIG{PIPE} = sub {};
286
287 foreach (@{$this->seen}) {
288 # Try to get the question again, because it's possible it
289 # was shown, and then unregistered.
290 my $q=Debconf::Question->get($_->name);
291 $_->flag('seen', 'true') if $q;
292 }
293 $this->seen([]);
294
295 return '';
296 }
297
298 =item command_input
299
300 Creates an Element to stand for the question that is to be asked and adds it to
301 the list of elements in our associated FrontEnd.
302
303 =cut
304
305 sub command_input {
306 my $this=shift;
307 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
308 my $priority=shift;
309 my $question_name=shift;
310
311 my $question=Debconf::Question->get($question_name) ||
312 return $codes{badparams}, "\"$question_name\" doesn't exist";
313
314 if (! priority_valid($priority)) {
315 return $codes{syntaxerror}, "\"$priority\" is not a valid priority";
316 }
317
318 $question->priority($priority);
319
320 # Figure out if the question should be displayed to the user or
321 # not.
322 my $visible=1;
323
324 # Error questions are always shown even if they're asked at a low
325 # priority or have already been seen.
326 if ($question->type ne 'error') {
327 # Don't show items that are unimportant.
328 $visible='' unless high_enough($priority);
329
330 # Don't re-show already seen questions, unless reconfiguring.
331 $visible='' if ! Debconf::Config->reshow &&
332 $question->flag('seen') eq 'true';
333 }
334
335 # We may want to set the seen flag on noninteractive questions
336 # even though they aren't shown.
337 my $markseen=$visible;
338
339 # Noninteractive frontends never show anything.
340 if ($visible && ! $this->frontend->interactive) {
341 $visible='';
342 $markseen='' unless Debconf::Config->noninteractive_seen eq 'true';
343 }
344
345 my $element;
346 if ($visible) {
347 # Create an input Element of the type associated with
348 # the frontend.
349 $element=$this->frontend->makeelement($question);
350 # If that failed, quit now. This should never happen.
351 unless ($element) {
352 return $codes{internalerror},
353 "unable to make an input element";
354 }
355
356 # Ask the Element if it thinks it is visible. If not,
357 # fall back below to making a noninteractive element.
358 #
359 # This last check is useful, because for example, select
360 # Elements are not really visible if they have less than
361 # two choices.
362 $visible=$element->visible;
363 }
364
365 if (! $visible) {
366 # Create a noninteractive element. Supress debug messages
367 # because they generate FAQ's and are harmless.
368 $element=Debconf::FrontEnd::Noninteractive->makeelement($question, 1);
369
370 # If that failed, the question is just not visible.
371 return $codes{input_invisible}, "question skipped" unless $element;
372 }
373
374 $element->markseen($markseen);
375
376 push @{$this->busy}, $question_name;
377
378 $this->frontend->add($element);
379 if ($element->visible) {
380 return $codes{success}, "question will be asked";
381 }
382 else {
383 return $codes{input_invisible}, "question skipped";
384 }
385 }
386
387 =item command_clear
388
389 Clears out the list of elements in our accociated FrontEnd.
390
391 =cut
392
393 sub command_clear {
394 my $this=shift;
395 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 0;
396
397 $this->frontend->clear;
398 $this->busy([]);
399 return $codes{success};
400 }
401
402 =item command_version
403
404 Compares protocol versions with the confmodule. The version field of the
405 ConfModule is sent to the client.
406
407 =cut
408
409 sub command_version {
410 my $this=shift;
411 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ > 1;
412 my $version=shift;
413 if (defined $version) {
414 return $codes{version_bad}, "Version too low ($version)"
415 if int($version) < int($this->version);
416 return $codes{version_bad}, "Version too high ($version)"
417 if int($version) > int($this->version);
418 }
419 return $codes{success}, $this->version;
420 }
421
422 =item command_capb
423
424 Sets the client_capb field to the confmodules's capabilities, and also
425 sets the capb_backup field of the ConfModules associated FrontEnd if
426 the confmodule can backup. Sends the capb field of the associated
427 FrontEnd to the confmodule.
428
429 =cut
430
431 sub command_capb {
432 my $this=shift;
433 $this->client_capb([@_]);
434 # Set capb_backup on the frontend if the client can backup.
435 if (grep { $_ eq 'backup' } @_) {
436 $this->frontend->capb_backup(1);
437 } else {
438 $this->frontend->capb_backup('');
439 }
440 # Multiselect is added as a capability to fix a backwards
441 # compatability problem.
442 my @capb=('multiselect', 'escape');
443 push @capb, $this->frontend->capb;
444 return $codes{success}, @capb;
445 }
446
447 =item command_title
448
449 Stores the specified title in the associated FrontEnds title field.
450
451 =cut
452
453 sub command_title {
454 my $this=shift;
455 $this->frontend->title(join ' ', @_);
456 $this->frontend->requested_title($this->frontend->title);
457
458 return $codes{success};
459 }
460
461 =item command_settitle
462
463 Uses the short description of a question as the title, with automatic i18n.
464
465 =cut
466
467 sub command_settitle {
468 my $this=shift;
469
470 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
471 my $question_name=shift;
472
473 my $question=Debconf::Question->get($question_name) ||
474 return $codes{badparams}, "\"$question_name\" doesn't exist";
475
476 if ($this->frontend->can('settitle')) {
477 $this->frontend->settitle($question);
478 } else {
479 $this->frontend->title($question->description);
480 }
481 $this->frontend->requested_title($this->frontend->title);
482
483 return $codes{success};
484 }
485
486 =item beginblock, endblock
487
488 These are just stubs to be overridden by other modules.
489
490 =cut
491
492 sub command_beginblock {
493 return $codes{success};
494 }
495 sub command_endblock {
496 return $codes{success};
497 }
498
499 =item command_go
500
501 Tells the associated FrontEnd to display items to the user, by calling
502 its go method. That method should return false if the user asked to back
503 up, and true otherwise. If it returns true, then all of the questions that
504 were displayed are added to the seen array.
505
506 =cut
507
508 sub command_go {
509 my $this=shift;
510 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ > 0;
511
512 my $ret=$this->frontend->go;
513 # If no elements were shown, and we backed up last time, back up again
514 # even if the user didn't indicate they want to back up. This
515 # causes invisible elements to be skipped over in multi-stage backups.
516 if ($ret && (! $this->backed_up ||
517 grep { $_->visible } @{$this->frontend->elements})) {
518 foreach (@{$this->frontend->elements}) {
519 $_->question->value($_->value);
520 push @{$this->seen}, $_->question if $_->markseen && $_->question;
521 }
522 $this->frontend->clear;
523 $this->busy([]);
524 $this->backed_up('');
525 return $codes{success}, "ok"
526 }
527 else {
528 $this->frontend->clear;
529 $this->busy([]);
530 $this->backed_up(1);
531 return $codes{go_back}, "backup";
532 }
533 }
534
535 =item command_get
536
537 This must be passed a question name. It queries the question for the value
538 set in it and returns that to the confmodule
539
540 =cut
541
542 sub command_get {
543 my $this=shift;
544 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
545 my $question_name=shift;
546 my $question=Debconf::Question->get($question_name) ||
547 return $codes{badparams}, "$question_name doesn't exist";
548
549 my $value=$question->value;
550 if (defined $value) {
551 if (defined $this->client_capb and grep { $_ eq 'escape' } @{$this->client_capb}) {
552 return $codes{escaped_data}, escape($value);
553 } else {
554 return $codes{success}, $value;
555 }
556 }
557 else {
558 return $codes{success}, '';
559 }
560 }
561
562 =item command_set
563
564 This must be passed a question name and a value. It sets the question's value.
565
566 =cut
567
568 sub command_set {
569 my $this=shift;
570 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 1;
571 my $question_name=shift;
572 my $value=join(" ", @_);
573
574 my $question=Debconf::Question->get($question_name) ||
575 return $codes{badparams}, "$question_name doesn't exist";
576 $question->value($value);
577 return $codes{success}, "value set";
578 }
579
580 =item command_reset
581
582 Reset a question to its default value.
583
584 =cut
585
586 sub command_reset {
587 my $this=shift;
588 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
589 my $question_name=shift;
590
591 my $question=Debconf::Question->get($question_name) ||
592 return $codes{badparams}, "$question_name doesn't exist";
593 $question->value($question->default);
594 $question->flag('seen', 'false');
595 return $codes{success};
596 }
597
598 =item command_subst
599
600 This must be passed a question name, a key, and a value. It sets up variable
601 substitutions on the questions description so all instances of the key
602 (wrapped in "${}") are replaced with the value.
603
604 =cut
605
606 sub command_subst {
607 my $this = shift;
608 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 2;
609 my $question_name = shift;
610 my $variable = shift;
611 my $value = (join ' ', @_);
612
613 my $question=Debconf::Question->get($question_name) ||
614 return $codes{badparams}, "$question_name doesn't exist";
615 my $result=$question->variable($variable,$value);
616 return $codes{internalerror}, "Substitution failed" unless defined $result;
617 return $codes{success};
618 }
619
620 =item command_register
621
622 This should be passed a template name and a question name. Registers a
623 question to use the template.
624
625 =cut
626
627 sub command_register {
628 my $this=shift;
629 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
630 my $template=shift;
631 my $name=shift;
632
633 my $tempobj = Debconf::Question->get($template);
634 if (! $tempobj) {
635 return $codes{badparams}, "No such template, \"$template\"";
636 }
637 my $question=Debconf::Question->get($name) ||
638 Debconf::Question->new($name, $this->owner, $tempobj->type);
639 if (! $question) {
640 return $codes{internalerror}, "Internal error making question";
641 }
642 if (! defined $question->addowner($this->owner, $tempobj->type)) {
643 return $codes{internalerror}, "Internal error adding owner";
644 }
645 if (! $question->template($template)) {
646 return $codes{internalerror}, "Internal error setting template";
647 }
648
649 return $codes{success};
650 }
651
652 =item command_unregister
653
654 Pass this a question name, and it will give up ownership of the question,
655 which typically causes it to be removed.
656
657 =cut
658
659 sub command_unregister {
660 my $this=shift;
661 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
662 my $name=shift;
663
664 my $question=Debconf::Question->get($name) ||
665 return $codes{badparams}, "$name doesn't exist";
666 if (grep { $_ eq $name } @{$this->busy}) {
667 return $codes{badparams}, "$name is busy, cannot unregister right now";
668 }
669 $question->removeowner($this->owner);
670 return $codes{success};
671 }
672
673 =item command_purge
674
675 This will give up ownership of all questions a confmodule owns.
676
677 =cut
678
679 sub command_purge {
680 my $this=shift;
681 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ > 0;
682
683 my $iterator=Debconf::Question->iterator;
684 while (my $q=$iterator->iterate) {
685 $q->removeowner($this->owner);
686 }
687
688 return $codes{success};
689 }
690
691 =item command_metaget
692
693 Pass this a question name and a field name. It returns the value of the
694 specified field of the question.
695
696 =cut
697
698 sub command_metaget {
699 my $this=shift;
700 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
701 my $question_name=shift;
702 my $field=shift;
703
704 my $question=Debconf::Question->get($question_name) ||
705 return $codes{badparams}, "$question_name doesn't exist";
706 my $lcfield=lc $field;
707 my $fieldval=$question->$lcfield();
708 unless (defined $fieldval) {
709 return $codes{badparams}, "$field does not exist";
710 }
711 if (defined $this->client_capb and grep { $_ eq 'escape' } @{$this->client_capb}) {
712 return $codes{escaped_data}, escape($fieldval);
713 } else {
714 return $codes{success}, $fieldval;
715 }
716 }
717
718 =item command_fget
719
720 Pass this a question name and a flag name. It returns the value of the
721 specified flag on the question.
722
723 =cut
724
725 sub command_fget {
726 my $this=shift;
727 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
728 my $question_name=shift;
729 my $flag=shift;
730
731 my $question=Debconf::Question->get($question_name) ||
732 return $codes{badparams}, "$question_name doesn't exist";
733
734 return $codes{success}, $question->flag($flag);
735 }
736
737 =item command_fset
738
739 Pass this a question name, a flag name, and a value. It sets the value of
740 the specified flag in the specified question.
741
742 =cut
743
744 sub command_fset {
745 my $this=shift;
746 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 3;
747 my $question_name=shift;
748 my $flag=shift;
749 my $value=(join ' ', @_);
750
751 my $question=Debconf::Question->get($question_name) ||
752 return $codes{badparams}, "$question_name doesn't exist";
753
754 if ($flag eq 'seen') {
755 # If this question we're being asked to modify is one that was
756 # shown in the current session, it will be in our seen
757 # cache, and changing its value here will not persist
758 # after this session, because the seen property overwrites
759 # the values at the end of the session. Therefore, remove
760 # it from our seen cache.
761 $this->seen([grep {$_ ne $question} @{$this->seen}]);
762 }
763
764 return $codes{success}, $question->flag($flag, $value);
765 }
766
767 =item command_info
768
769 Pass this a question name. It displays the given template as an out-of-band
770 informative message. Unlike inputting a note, this doesn't require an
771 acknowledgement from the user, and depending on the frontend it may not even
772 be displayed at all. Frontends should display the info persistently until
773 some other info comes along.
774
775 With no arguments, this resets the info message to a default value.
776
777 =cut
778
779 sub command_info {
780 my $this=shift;
781
782 if (@_ == 0) {
783 $this->frontend->info(undef);
784 } elsif (@_ == 1) {
785 my $question_name=shift;
786
787 my $question=Debconf::Question->get($question_name) ||
788 return $codes{badparams}, "\"$question_name\" doesn't exist";
789
790 $this->frontend->info($question);
791 } else {
792 return $codes{syntaxerror}, "Incorrect number of arguments";
793 }
794
795 return $codes{success};
796 }
797
798 =item command_progress
799
800 Progress bar handling. Pass this a subcommand name followed by any arguments
801 required by the subcommand, as follows:
802
803 =over 4
804
805 =item START
806
807 Pass this a minimum value, a maximum value, and a question name. It creates
808 a progress bar with the specified range and the description of the specified
809 question as the title.
810
811 =item SET
812
813 Pass this a value. It sets the current position of the progress bar to the
814 specified value.
815
816 =item STEP
817
818 Pass this an increment. It increments the current position of the progress
819 bar by the specified amount.
820
821 =item INFO
822
823 Pass this a template name. It displays the specified template as an
824 informational message in the progress bar.
825
826 =item STOP
827
828 This subcommand takes no arguments. It destroys the progress bar.
829
830 =back
831
832 Note that the frontend's progress_set, progress_step, and progress_info
833 functions should return true, unless the progress bar was canceled.
834
835 =cut
836
837 sub command_progress {
838 my $this=shift;
839 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 1;
840 my $subcommand=shift;
841 $subcommand=lc($subcommand);
842
843 my $ret;
844
845 if ($subcommand eq 'start') {
846 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 3;
847 my $min=shift;
848 my $max=shift;
849 my $question_name=shift;
850
851 return $codes{syntaxerror}, "min ($min) > max ($max)" if $min > $max;
852
853 my $question=Debconf::Question->get($question_name) ||
854 return $codes{badparams}, "$question_name doesn't exist";
855
856 $this->frontend->progress_start($min, $max, $question);
857 $ret=1;
858 }
859 elsif ($subcommand eq 'set') {
860 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
861 my $value=shift;
862 $ret = $this->frontend->progress_set($value);
863 }
864 elsif ($subcommand eq 'step') {
865 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
866 my $inc=shift;
867 $ret = $this->frontend->progress_step($inc);
868 }
869 elsif ($subcommand eq 'info') {
870 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
871 my $question_name=shift;
872
873 my $question=Debconf::Question->get($question_name) ||
874 return $codes{badparams}, "$question_name doesn't exist";
875
876 $ret = $this->frontend->progress_info($question);
877 }
878 elsif ($subcommand eq 'stop') {
879 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 0;
880 $this->frontend->progress_stop();
881 $ret=1;
882 }
883 else {
884 return $codes{syntaxerror}, "Unknown subcommand";
885 }
886
887 if ($ret) {
888 return $codes{success}, "OK";
889 }
890 else {
891 return $codes{progresscancel}, "CANCELED";
892 }
893 }
894
895 =item command_data
896
897 Accept template data from the client, for use on the UI agent side of the
898 passthrough frontend.
899
900 TODO: Since process_command() collapses multiple spaces in commands into
901 single spaces, this doesn't quite handle bulleted lists correctly.
902
903 =cut
904
905 sub command_data {
906 my $this=shift;
907 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 3;
908 my $template=shift;
909 my $item=shift;
910 my $value=join(' ', @_);
911 $value=~s/\\([n"\\])/($1 eq 'n') ? "\n" : $1/eg;
912
913 my $tempobj=Debconf::Template->get($template);
914 if (! $tempobj) {
915 if ($item ne 'type') {
916 return $codes{badparams}, "Template data field '$item' received before type field";
917 }
918 $tempobj=Debconf::Template->new($template, $this->owner, $value);
919 if (! $tempobj) {
920 return $codes{internalerror}, "Internal error making template";
921 }
922 } else {
923 if ($item eq 'type') {
924 return $codes{badparams}, "Template type already set";
925 }
926 $tempobj->$item(Debconf::Encoding::convert("UTF-8", $value));
927 }
928
929 return $codes{success};
930 }
931
932 =item command_visible
933
934 Deprecated.
935
936 =cut
937
938 sub command_visible {
939 my $this=shift;
940 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 2;
941 my $priority=shift;
942 my $question_name=shift;
943
944 my $question=Debconf::Question->get($question_name) ||
945 return $codes{badparams}, "$question_name doesn't exist";
946 return $codes{success}, $this->frontend->visible($question, $priority) ? "true" : "false";
947 }
948
949 =item command_exist
950
951 Deprecated.
952
953 =cut
954
955 sub command_exist {
956 my $this=shift;
957 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ != 1;
958 my $question_name=shift;
959
960 return $codes{success},
961 Debconf::Question->get($question_name) ? "true" : "false";
962 }
963
964 =item command_x_loadtemplatefile
965
966 Extension to load a specified template file.
967
968 =cut
969
970 sub command_x_loadtemplatefile {
971 my $this=shift;
972
973 return $codes{syntaxerror}, "Incorrect number of arguments" if @_ < 1 || @_ > 2;
974
975 my $file=shift;
976 my $fh=FileHandle->new($file);
977 if (! $fh) {
978 return $codes{badparams}, "failed to open $file: $!";
979 }
980
981 my $owner=$this->owner;
982 if (@_) {
983 $owner=shift;
984 }
985
986 eval {
987 Debconf::Template->load($fh, $owner);
988 };
989 if ($@) {
990 $@=~s/\n/\\n/g;
991 return $codes{internalerror}, $@;
992 }
993 return $codes{success};
994 }
995
996 =item AUTOLOAD
997
998 Handles storing and loading fields.
999
1000 =cut
1001
1002 sub AUTOLOAD {
1003 (my $field = our $AUTOLOAD) =~ s/.*://;
1004
1005 no strict 'refs';
1006 *$AUTOLOAD = sub {
1007 my $this=shift;
1008
1009 return $this->{$field} unless @_;
1010 return $this->{$field}=shift;
1011 };
1012 goto &$AUTOLOAD;
1013 }
1014
1015 =item DESTROY
1016
1017 When the object is destroyed, the filehandles are closed and the confmodule
1018 script stopped. All questions that have been displayed during the lifetime
1019 of the confmodule are marked as seen.
1020
1021 =cut
1022
1023 sub DESTROY {
1024 my $this=shift;
1025
1026 $this->read_handle->close if $this->read_handle;
1027 $this->write_handle->close if $this->write_handle;
1028
1029 if (defined $this->pid && $this->pid > 1) {
1030 kill 'TERM', $this->pid;
1031 }
1032 }
1033
1034 =back
1035
1036 =head1 AUTHOR
1037
1038 Joey Hess <[email protected]>
1039
1040 =cut
1041
1042 1