1 # -*- Mode: perl; indent-tabs-mode: nil -*-
3 # The contents of this file are subject to the Mozilla Public
4 # License Version 1.1 (the "License"); you may not use this file
5 # except in compliance with the License. You may obtain a copy of
6 # the License at http://www.mozilla.org/MPL/
8 # Software distributed under the License is distributed on an "AS
9 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 # implied. See the License for the specific language governing
11 # rights and limitations under the License.
13 # The Original Code is the Bugzilla Bug Tracking System.
15 # The Initial Developer of the Original Code is Netscape Communications
16 # Corporation. Portions created by Netscape are
17 # Copyright (C) 1998 Netscape Communications Corporation. All
20 # Contributor(s): Harrison Page <harrison@netscape.com>
21 # Terry Weissman <terry@mozilla.org>
22 # Ian Hickson <py8ieh=mozbot@bath.ac.uk>
24 package Configuration;
29 my ($file, $config) = @_;
31 open FILE, "<$file" or return 0;
35 if (/^ *([^#;][^=\n\r]*)(?:=(.*))?$/os) {
36 my $value = $$config{$1};
37 if (defined($value)) {
38 $value = $$value while ref($value) eq 'REF';
39 if (ref($value) eq 'SCALAR') {
41 } elsif (ref($value) eq 'ARRAY') {
48 } elsif (ref($value) eq 'HASH') {
53 $2 =~ /^(.)(.*?)\1=>(.*)$/so;
57 } # else unknown variable, ignore
59 } # else ignore (probably comment)
66 my ($file, $config) = @_;
69 # Try to keep file structure if possible
71 if (open FILE, "<$file") {
78 # but make sure we put in all the data (dups are dealt with)
79 foreach (sort keys %$config) {
83 # Open file to which we are saving
84 open FILE, ">$file.~$$~" or confess("Could not save configuration: $!");
86 # ok, save file back again
87 # make sure we only write parameters once by
88 # keeping a log of those done
92 if (/^ *([^#;][^=\n\r]*)(?:=(.*))?$/os) {
95 if (defined($$config{$variable})) {
96 unless ($seen{$variable}) {
97 $value = $$config{$variable};
98 $value = $$value while ref($value) eq 'REF';
99 if (ref($value) eq 'SCALAR') {
100 if (defined($$value)) {
101 print FILE $variable.'='.$$value."\n" or confess("Could not save configuration: $!");
103 } elsif (ref($value) eq 'HASH') {
104 my @keys = keys %$value;
106 foreach my $item (@keys) {
107 my $data = $$value{$item};
108 $item = '' unless defined $item;
109 $data = '' unless defined $data;
111 foreach ('"','\'','|',':','#','*','<','>','/','[',']','{','}',
112 '(',')','\\','=','-','@','!','\$','%','&',' ','\`','~') {
113 if ($item !~ /\Q$_\E=>/os) {
118 if (defined($delimiter)) {
119 print FILE "$variable=$delimiter$item$delimiter=>$data\n"
120 or confess("Could not save configuration: $!");
122 # else, silent data loss... XXX
125 print FILE "$variable\n" or confess("Could not save configuration: $!");
127 } elsif (ref($value) eq 'ARRAY') {
129 foreach my $item (@$value) {
130 if (defined($item)) {
131 print FILE "$variable=$item\n" or confess("Could not save configuration: $!");
133 print FILE "$variable=\n" or confess("Could not save configuration: $!");
137 print FILE "$variable\n" or confess("Could not save configuration: $!");
140 confess("Unsupported data type '".ref($value)."' writing $variable (".$$config{$variable}.')');
142 $seen{$variable} = 1;
143 } # else seen it already
145 if (defined($value)) {
146 print FILE "$variable=$value\n" or confess("Could not save configuration: $!");
148 print FILE "$variable\n" or confess("Could not save configuration: $!");
153 print FILE $_."\n" or confess("Could not save configuration: $!");
156 # actually do make a change to the real file
157 close FILE or confess("Could not save configuration: $!");
159 # -- #mozwebtools was here --
160 # * Hixie is sad as his bot crashes.
161 # * Hixie adds in a check to make sure that the file he tries
162 # to delete actually exists first.
163 # <timeless> delete??
165 unlink $file or confess("Could not delete $file: $!") if (-e $file);
166 rename("$file.~$$~", $file) or confess("Could not rename to $file: $!");
173 if (ref($$_[1]) eq 'SCALAR') {
174 unless (defined(${$$_[1]})) {
179 ${$$_[1]} = '' unless defined ${$$_[1]};
183 confess("Terminal is not interactive, so could not ask '$$_[0]'. Gave up");
186 } elsif (ref($$_[1]) eq 'ARRAY') {
187 unless (defined(@{$$_[1]})) {
189 print $$_[0]. " (enter a blank line to finish)\n";
193 $input = '' unless defined $input;
195 push @{$$_[1]}, $input if $input;
199 confess("Terminal is not interactive, so could not ask '$$_[0]'. Gave up");
203 confess("Unsupported data type expected for question '$$_[0]'");