7 my $default = '/etc/default/openvswitch-switch';
9 my (%config) = load_config($default);
11 foreach my $arg (@ARGV) {
12 my ($key, $value) = $arg =~ /^([^=]+)=(.*)/
13 or die "bad argument '$arg'\n";
15 $config{$key} = $value;
20 save_config($default, %config);
22 print "$_=$config{$_}\n" foreach sort(keys(%config));
27 # Get the list of the variables that the shell sets automatically.
28 my (%auto_vars) = read_vars("set -a && env");
30 # Get the variables from $default.
31 my (%config) = read_vars("set -a && . '$default' && env");
34 delete @config{keys %auto_vars};
42 if (!open(VARS, '-|', $cmd)) {
43 print STDERR "$cmd: failed to execute: $!\n";
48 my ($var, $value) = /^([^=]+)=(.*)$/ or next;
49 $config{$var} = $value;
59 } elsif (m&^[-a-zA-Z0-9:./%^_+,]*$&) {
68 my ($var, $value) = @_;
69 return $var . '=' . shell_escape($value);
73 my ($file, %config) = @_;
75 if (open(FILE, '<', $file)) {
81 # Replace all existing variable assignments.
82 for (my ($i) = 0; $i <= $#lines; $i++) {
83 local $_ = $lines[$i];
84 my ($var, $value) = /^\s*([^=#]+)=(.*)$/ or next;
85 if (exists($config{$var})) {
86 $lines[$i] = shell_assign($var, $config{$var});
89 $lines[$i] = "#$lines[$i]";
93 # Find a place to put any remaining variable assignments.
95 for my $var (keys(%config)) {
96 my $assign = shell_assign($var, $config{$var});
98 # Replace the last commented-out variable assignment to $var, if any.
99 for (my ($i) = $#lines; $i >= 0; $i--) {
100 local $_ = $lines[$i];
101 if (/^\s*#\s*$var=/) {
102 $lines[$i] = $assign;
107 # Find a place to add the var: after the final commented line
108 # just after a line that contains "$var:".
109 for (my ($i) = 0; $i <= $#lines; $i++) {
110 if ($lines[$i] =~ /^\s*#\s*$var:/) {
111 for (my ($j) = $i + 1; $j <= $#lines; $j++) {
112 if ($lines[$j] !~ /^\s*#/) {
113 splice(@lines, $j, 0, $assign);
121 push(@lines, $assign);
124 open(NEWFILE, '>', "$file.tmp") or die "$file.tmp: create: $!\n";
125 print NEWFILE join('', map("$_\n", @lines));
127 rename("$file.tmp", $file) or die "$file.tmp: rename to $file: $!\n";