1 # Common functions for editing the Frox config file
3 BEGIN { push(@INC, ".."); };
6 @ui_tds = ( undef, "nowrap" );
9 # Returns an array reference containing the contents of the Frox config file
12 if (!scalar(@get_config_cache)) {
14 open(CONF, $config{'frox_conf'});
18 if (/^\s*(\S+)\s*(.*)/) {
19 push(@get_config_cache,
22 'words' => [ split(/\s+/, $2) ],
24 'index' => scalar(@get_config_cache) });
30 return \@get_config_cache;
37 foreach $c (@{$_[1]}) {
38 if (lc($c->{'name'}) eq lc($_[0])) {
42 return wantarray ? @rv : $rv[0];
45 # find_value(name, &config)
48 local @rv = &find($_[0], $_[1]);
50 return map { $_->{'value'} } @rv;
53 return $rv[0]->{'value'};
57 # save_directive(&config, name, &values)
58 # Update the config file with some new values for an option
61 local ($conf, $name, $values) = @_;
62 local $lref = &read_file_lines($config{'frox_conf'});
63 local @old = &find($name, $conf);
64 local @new = @$values;
67 for($i=0; $i<@old || $i<@new; $i++) {
68 if ($i<@old && $i<@new) {
69 # Updating some directive
70 $lref->[$old[$i]->{'line'}] = $name." ".$new[$i];
71 $old[$i]->{'value'} = $new[$i];
75 # Removing some directive (need to renumber)
76 splice(@$lref, $old[$i]->{'line'}, 1);
77 splice(@$conf, $old[$i]->{'index'}, 1);
78 &renumber($conf, 'line', $old[$i]->{'line'}, -1);
79 &renumber($conf, 'index', $old[$i]->{'index'}, -1);
82 # Adding some directive (perhaps after same)
83 local $nd = { 'name' => $name,
84 'value' => $new[$i] };
86 for($j=0; $j<@$lref; $j++) {
87 if ($lref->[$j] =~ /^\s*#+\s*(\S+)/ && $1 eq $name) {
93 $nd->{'line'} = $changed->{'line'}+1;
94 $nd->{'index'} = $changed->{'index'}+1;
95 &renumber($conf, 'line', $changed->{'line'}, 1);
96 &renumber($conf, 'index', $changed->{'index'}, 1);
97 splice(@$lref, $changed->{'line'}+1, 0,
99 splice(@$conf, $changed->{'index'}+1, 0, $nd);
102 elsif (defined($cmtline)) {
103 # Adding after comment of same directive
104 local ($aftercmt) = grep { $_->{'line'} > $cmtline } @$conf;
105 $nd->{'line'} = $cmtline;
106 $nd->{'index'} = $aftercmt ? $aftercmt->{'index'} : 0;
107 &renumber($conf, 'line', $nd->{'line'}-1, 1);
108 &renumber($conf, 'index', $nd->{'index'}-1, 1);
109 splice(@$lref, $cmtline+1, 0,
111 splice(@$conf, $nd->{'index'}, 0, $nd);
116 $nd->{'line'} = scalar(@$lref);
117 $nd->{'index'} = scalar(@$conf);
118 push(@$lref, $name." ".$new[$i]);
125 # renumber(&conf, field, pos, offset)
128 local ($conf, $field, $pos, $offset) = @_;
130 foreach $c (@$conf) {
131 if ($c->{$field} > $pos) {
132 $c->{$field} += $offset;
137 # config_textbox(&config, name, size, cols, [prefix])
138 # Returns HTML for a table row for some text field
141 local ($conf, $name, $size, $cols, $prefix) = @_;
143 local $val = join(" ", &find_value($name, $conf));
144 $val =~ s/\s*'(.*)'\s*$/$1/;
145 return &ui_table_row($text{'edit_'.$name},
146 &ui_textbox($name, $val, $size)." ".$prefix,
150 # save_textbox(config, name, [function], [split])
153 local ($conf, $name, $func, $split) = @_;
154 local @vals = $split ? split(/\s+/, $in{$name}) : ( $in{$name} );
157 &error_check($name, $func, $v);
158 $v = "'$v'" if ($v =~ /\s/);
160 &save_directive($conf, $name, \@vals);
163 # config_opt_textbox(&config, name, size, cols, [default])
164 # Returns HTML for a table row for some optional text field
165 sub config_opt_textbox
167 local ($conf, $name, $size, $cols, $default) = @_;
169 local $val = join(" ", &find_value($name, $conf));
170 $val =~ s/\s*'(.*)'\s*$/$1/;
171 return &ui_table_row($text{'edit_'.$name},
172 &ui_radio($name."_def", $val ? 0 : 1,
173 [ [ 1, $default || $text{'default'} ],
175 &ui_textbox($name, $val, $size), $cols, \@ui_tds);
178 # save_opt_textbox(config, name, [function], [split])
181 local ($conf, $name, $func, $split) = @_;
182 if ($in{$name."_def"}) {
183 &save_directive($conf, $name, [ ]);
190 # config_yesno(&config, name, [yes], [no], [default])
191 # Returns HTML for a table row for yes/no checkboxes
194 local ($conf, $name, $yes, $no, $default) = @_;
195 local $val = &find_value($name, $conf);
197 return &ui_table_row($text{'edit_'.$name},
198 &ui_radio($name, $val =~ /yes|on|true/i ? 1 : 0,
199 [ [ 1, $yes || $text{'yes'} ],
200 [ 0, $no || $text{'no'} ] ]), 1, \@ui_tds).
201 &ui_hidden($name."_default", $default);
204 # save_yesno(&config, name, default)
207 local ($conf, $name, $default) = @_;
208 local $val = &find_value($name, $conf);
209 $val ||= $in{$name."_default"};
210 local $curr = $val =~ /yes|on|true/i ? 1 : 0;
211 if ($curr ne $in{$name}) {
212 &save_directive($conf, $name, [ $in{$name} ? "yes" : "no" ]);
216 # config_exists(&config, name, [yes], [no])
217 # Returns HTML for a table row for yes/no checkboxes, based on the existance
218 # of some config item
221 local ($conf, $name, $yes, $no) = @_;
222 local $exists = &find($name, $conf);
223 return &ui_table_row($text{'edit_'.$name},
224 &ui_radio($name, $exists ? 1 : 0,
225 [ [ 1, $yes || $text{'yes'} ],
226 [ 0, $no || $text{'no'} ] ]), 1, \@ui_tds);
229 # save_exists(&config, name)
232 local ($conf, $name) = @_;
233 &save_directive($conf, $name, $in{$name} ? [ "" ] : [ ]);
236 # config_user(&config, name)
237 # Returns HTML for a table row for a username field
240 local ($conf, $name) = @_;
241 local $val = &find_value($name, $conf);
242 return &ui_table_row($text{'edit_'.$name},
243 &ui_user_textbox($name, $val, $size), 1, \@ui_tds);
246 # save_user(&config, name)
247 # Saves a username field
250 local ($conf, $name) = @_;
251 defined(getpwnam($in{$name})) ||
252 &error($text{'edit_'.$name}." : ".$text{'edit_euser'});
253 &save_directive($conf, $name, [ $in{$name} ]);
256 # config_group(&config, name)
257 # Returns HTML for a table row for a groupname field
260 local ($conf, $name) = @_;
261 local $val = &find_value($name, $conf);
262 return &ui_table_row($text{'edit_'.$name},
263 &ui_group_textbox($name, $val, $size), 1, \@ui_tds);
266 # save_group(&config, name)
267 # Saves a group name field
270 local ($conf, $name) = @_;
271 defined(getgrnam($in{$name})) ||
272 &error($text{'edit_'.$name}." : ".$text{'edit_egroup'});
273 &save_directive($conf, $name, [ $in{$name} ]);
276 # config_opt_range(&config, name, cols, default)
277 # Returns HTML for a row containing a two-part range input field
280 local ($conf, $name, $cols, $default) = @_;
282 local $val = &find_value($name, $conf);
283 local ($v1, $v2) = split(/\-/, $val);
284 return &ui_table_row($text{'edit_'.$name},
285 &ui_radio($name."_def", $val ? 0 : 1,
286 [ [ 1, $default || $text{'default'} ],
288 $text{'edit_from'}." ".
289 &ui_textbox($name."_from", $v1, 6)." ".
290 $text{'edit_to'}." ".
291 &ui_textbox($name."_to", $v2, 6), $cols, \@ui_tds);
294 # save_opt_range(config, name)
297 local ($conf, $name, $func, $split) = @_;
298 if ($in{$name."_def"}) {
299 &save_directive($conf, $name, [ ]);
302 local $v1 = $in{$name."_from"};
303 local $v2 = $in{$name."_to"};
304 $v1 =~ /^\d+$/ && $v1 > 0 && $v2 < 65535 ||
305 &error($text{'edit_'.$name}." : ".$text{'edit_efrom'});
306 $v2 =~ /^\d+$/ && $v2 > 0 && $v2 < 65536 ||
307 &error($text{'edit_'.$name}." : ".$text{'edit_eto'});
308 $v1 < $v2 || &error($text{'edit_'.$name}." : ".$text{'edit_erange'});
309 &save_directive($conf, $name, [ $v1."-".$v2 ]);
313 # error_check(name, func, value)
316 local ($name, $func, $val) = @_;
318 local $err = &$func($val);
319 &error($text{'edit_'.$name}." : ".$err) if ($err);
323 # Returns the PID if Frox is running, 0 if not
326 local $conf = &get_config();
327 local $pidfile = &find_value("PidFile", $conf);
329 open(PID, $pidfile) || return 0;
332 return $pid && kill(0, $pid) ? $pid : 0;
335 local ($pid) = &find_byname("frox");
341 # Apply the current frox configuration, or return an error message
344 if ($config{'apply_cmd'}) {
345 $out = &backquote_logged("($config{'apply_cmd'}) 2>&1 </dev/null");
347 return "<pre>$out</pre>";
351 $pid = &is_frox_running();
352 $pid || return $text{'stop_egone'};
353 &kill_logged('HUP', $pid) || return $text{'stop_egone'};