| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::FrontEnd::Editor - Edit a config file to answer questions |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::FrontEnd::Editor; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Encoding q(wrap); |
| 13 |
use Debconf::TmpFile; |
| 14 |
use Debconf::Gettext; |
| 15 |
use base qw(Debconf::FrontEnd::ScreenSize); |
| 16 |
|
| 17 |
my $fh; |
| 18 |
|
| 19 |
=head1 DESCRIPTION |
| 20 |
|
| 21 |
This FrontEnd isn't really a frontend. It just generates a series of config |
| 22 |
files and runs the users editor on them, then parses the result. |
| 23 |
|
| 24 |
=head1 METHODS |
| 25 |
|
| 26 |
=over 4 |
| 27 |
|
| 28 |
=cut |
| 29 |
|
| 30 |
sub init { |
| 31 |
my $this=shift; |
| 32 |
|
| 33 |
$this->SUPER::init(@_); |
| 34 |
$this->interactive(1); |
| 35 |
} |
| 36 |
|
| 37 |
=item comment |
| 38 |
|
| 39 |
Displays a comment, word-wrapped. |
| 40 |
|
| 41 |
=cut |
| 42 |
|
| 43 |
sub comment { |
| 44 |
my $this=shift; |
| 45 |
my $comment=shift; |
| 46 |
|
| 47 |
# $Text::Wrap::break=q/\s+/; |
| 48 |
print $fh wrap('# ','# ',$comment); |
| 49 |
$this->filecontents(1); |
| 50 |
} |
| 51 |
|
| 52 |
=item separator |
| 53 |
|
| 54 |
Displays a divider bar; a line of hashes. |
| 55 |
|
| 56 |
=cut |
| 57 |
|
| 58 |
sub divider { |
| 59 |
my $this=shift; |
| 60 |
|
| 61 |
print $fh ("\n".('#' x ($this->screenwidth - 1))."\n"); |
| 62 |
} |
| 63 |
|
| 64 |
=item item |
| 65 |
|
| 66 |
Displays an item. First parameter is the item's name, second is its value. |
| 67 |
|
| 68 |
=cut |
| 69 |
|
| 70 |
sub item { |
| 71 |
my $this=shift; |
| 72 |
my $name=shift; |
| 73 |
my $value=shift; |
| 74 |
|
| 75 |
print $fh "$name=\"$value\"\n\n"; |
| 76 |
$this->filecontents(1); |
| 77 |
} |
| 78 |
|
| 79 |
=item go |
| 80 |
|
| 81 |
Items write out data into a temporary file, which is then edited with the |
| 82 |
user's editor. Then the file is parsed back in. |
| 83 |
|
| 84 |
=cut |
| 85 |
|
| 86 |
sub go { |
| 87 |
my $this=shift; |
| 88 |
my @elements=@{$this->elements}; |
| 89 |
return 1 unless @elements; |
| 90 |
|
| 91 |
# End the filename in .sh because it is basically a shell |
| 92 |
# format file, and this makes some editors do good things. |
| 93 |
$fh = Debconf::TmpFile::open('.sh'); |
| 94 |
|
| 95 |
$this->comment(gettext("You are using the editor-based debconf frontend to configure your system. See the end of this document for detailed instructions.")); |
| 96 |
$this->divider; |
| 97 |
print $fh ("\n"); |
| 98 |
|
| 99 |
$this->filecontents(''); |
| 100 |
foreach my $element (@elements) { |
| 101 |
$element->show; |
| 102 |
} |
| 103 |
|
| 104 |
# Only proceed if something interesting was actually written to the |
| 105 |
# file. |
| 106 |
if (! $this->filecontents) { |
| 107 |
Debconf::TmpFile::cleanup(); |
| 108 |
return 1; |
| 109 |
} |
| 110 |
|
| 111 |
$this->divider; |
| 112 |
$this->comment(gettext("The editor-based debconf frontend presents you with one or more text files to edit. This is one such text file. If you are familiar with standard unix configuration files, this file will look familiar to you -- it contains comments interspersed with configuration items. Edit the file, changing any items as necessary, and then save it and exit. At that point, debconf will read the edited file, and use the values you entered to configure the system.")); |
| 113 |
print $fh ("\n"); |
| 114 |
close $fh; |
| 115 |
|
| 116 |
# Launch editor. |
| 117 |
my $editor=$ENV{EDITOR} || $ENV{VISUAL} || '/usr/bin/editor'; |
| 118 |
# $editor may possibly contain spaces and options |
| 119 |
system "$editor ".Debconf::TmpFile->filename; |
| 120 |
|
| 121 |
# Now parse the temporary file, looking for lines that look like |
| 122 |
# items. Figure out which Element corresponds to the item, and |
| 123 |
# pass the text into it to be processed. |
| 124 |
# FIXME: this isn't really very robust. Syntax errors are ignored. |
| 125 |
my %eltname=map { $_->question->name => $_ } @elements; |
| 126 |
open (my $in, "<", Debconf::TmpFile::filename()); |
| 127 |
while (<$in>) { |
| 128 |
next if /^\s*#/; |
| 129 |
|
| 130 |
if (/(.*?)="(.*)"/ && $eltname{$1}) { |
| 131 |
# Elements can override the value method to |
| 132 |
# process the input. |
| 133 |
$eltname{$1}->value($2); |
| 134 |
} |
| 135 |
} |
| 136 |
close $in; |
| 137 |
|
| 138 |
Debconf::TmpFile::cleanup(); |
| 139 |
|
| 140 |
return 1; |
| 141 |
} |
| 142 |
|
| 143 |
=item screenwidth |
| 144 |
|
| 145 |
This method from my base class is overridden, so after the screen width |
| 146 |
changes, $Debconf::Encoding::columns is updated to match. |
| 147 |
|
| 148 |
=cut |
| 149 |
|
| 150 |
sub screenwidth { |
| 151 |
my $this=shift; |
| 152 |
|
| 153 |
$Debconf::Encoding::columns=$this->SUPER::screenwidth(@_); |
| 154 |
} |
| 155 |
|
| 156 |
=back |
| 157 |
|
| 158 |
=head1 AUTHOR |
| 159 |
|
| 160 |
Joey Hess <[email protected]> |
| 161 |
|
| 162 |
=cut |
| 163 |
|
| 164 |
1 |