summaryrefslogtreecommitdiff
blob: 58e4d9c195a4c54fe19488c5f9d3aca13b1aa098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!XX_PERL@
use strict;
use warnings;

# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $

use lib qw{XX_perldir@};
use Portage;

# 0 = normal, 1 = gdb, 2 = valgrind
use constant { EXEC => 0 };

my $version = 'XX_PACKAGE_VERSION@';

my $interface = 'ufed-curses';
my $gdb       = "gdb -ex run ufed-curses";
my $memcheck  = "/usr/bin/valgrind -v --trace-children=yes --tool=memcheck"
              . " --track-origins=yes --leak-check=full --show-reachable=no"
              . " --read-var-info=yes"
              . " XX_libexecdir@/ufed-curses 2>/tmp/ufed_memcheck.log";

sub finalise;
sub flags_dialog;
sub save_flags;


flags_dialog;


# Take a list and return it ordered the following way:
# Put "-*" first, followed by enabling flags and put
# disabling flags to the end.
# Parameters: list of flags
sub finalise {
	my @arg = @_;
	my @result = sort {
		($a ne '-*') <=> ($b ne '-*')
		||
		($a =~ /^-/) <=> ($b =~ /^-/)
		||
		$a cmp $b
	} @arg;
	return @result;
}

# Launch the curses inteface. Communication is done using
# pipes. Waiting for pipe read/write to finish is done
# automatically.
# No parameters accepted.
sub flags_dialog {
	use POSIX ();
	POSIX::dup2 1, 3;
	POSIX::dup2 1, 4;
	my ($iread, $iwrite) = POSIX::pipe;
	my ($oread, $owrite) = POSIX::pipe;
	my $child = fork;
	die "fork() failed\n" if not defined $child;
	if($child == 0) {
		POSIX::close $iwrite;
		POSIX::close $oread;
		POSIX::dup2 $iread, 3;
		POSIX::close $iread;
		POSIX::dup2 $owrite, 4;
		POSIX::close $owrite;
		if (0 == EXEC) {
			exec { "XX_libexecdir@/$interface" } $interface or
			do { print STDERR "Couldn't launch $interface\n"; exit 3 }
		} elsif (1 == EXEC) {
			exec $gdb or
			do { print STDERR "Couldn't launch $interface\n"; exit 3 }
		} elsif (2 == EXEC) {
			exec $memcheck or
			do { print STDERR "Couldn't launch $interface\n"; exit 3 }
		} else {
			print STDERR "Value " . EXEC . " unknown for EXEC\n";
			exit 4;
		}
	}
	POSIX::close $iread;
	POSIX::close $owrite;
	my $outTxt = "";

	# Write out flags 
	for my $flag (sort { uc $a cmp uc $b } keys %$Portage::use_flags) {
		my $conf = $Portage::use_flags->{$flag}; ## Shortcut

		$outTxt .= sprintf ("%s [%s%s] %d\n", $flag,
					defined($conf->{global}{conf}) ?
						$conf->{global}{conf} > 0 ? '+' :
						$conf->{global}{conf} < 0 ? '-' : ' ' : ' ',
					defined($conf->{global}{"default"}) ?
						$conf->{global}{"default"} > 0 ? '+' :
						$conf->{global}{"default"} < 0 ? '-' : ' ' : ' ',
					$conf->{count});

		# Print global description first (if available)
		if (defined($conf->{global}) && length($conf->{global}{descr})) {
			$outTxt .= sprintf("\t%s\t%s\t ( ) [+%s%s%s   ]\n",
						$conf->{global}{descr},
						$conf->{global}{descr_alt},
						$conf->{global}{installed} ? '+' : ' ',
						$conf->{global}{forced} ? '+' : ' ',
						$conf->{global}{masked} ? '+' : ' ');
		}

		# Finally print the local description lines
		for my $pkg (sort keys %{$conf->{"local"}}) {
			$outTxt .= sprintf("\t%s\t%s\t (%s) [ %s%s%s%s%s%s]\n",
						$conf->{"local"}{$pkg}{descr},
						$conf->{"local"}{$pkg}{descr_alt},
						$pkg,
						$conf->{"local"}{$pkg}{installed} > 0 ? '+' :
						$conf->{"local"}{$pkg}{installed} < 0 ? '-' : ' ',
						$conf->{"local"}{$pkg}{forced}    > 0 ? '+' :
						$conf->{"local"}{$pkg}{forced}    < 0 ? '-' : ' ',
						$conf->{"local"}{$pkg}{masked}    > 0 ? '+' :
						$conf->{"local"}{$pkg}{masked}    < 0 ? '-' : ' ',
						$conf->{"local"}{$pkg}{"default"} > 0 ? '+' :
						$conf->{"local"}{$pkg}{"default"} < 0 ? '-' : ' ',
						$conf->{"local"}{$pkg}{"package"} > 0 ? '+' :
						$conf->{"local"}{$pkg}{"package"} < 0 ? '-' : ' ',
						$conf->{"local"}{$pkg}{pkguse}    > 0 ? '+' :
						$conf->{"local"}{$pkg}{pkguse}    < 0 ? '-' : ' ');
		}
	}

	# Some overlays (like sunrise) use UTF-8 characters in their
	# use descriptions. They cause problems unless the whole
	# interface is changed to use wchar. Substitute with ISO:
	$outTxt =~ tr/\x{2014}\x{201c}\x{201d}/\x2d\x22\x22/ ;

	# Now let the interface know of the result
	if (open my $fh, '>&=', $iwrite) {
		binmode( $fh, ":encoding(ISO-8859-1)" );

		# Fixed config:
		# byte 1: Read only 0/1
		# Rest: The flags configuration
		print $fh "$Portage::ro_mode$outTxt";
		close $fh;
	} else {
		die "Couldn't let interface know of flags\n";
	}
	POSIX::close $iwrite;
	wait;
	if(POSIX::WIFEXITED($?)) {
		my $rc = POSIX::WEXITSTATUS($?);
		if( (0 == $rc) && (0 == $Portage::ro_mode) ) {
			open my $fh, '<&=', $oread or die "Couldn't read output.\n";
			my @flags = grep { $_ ne '--*' } do { local $/; split /\n/, <$fh> };
			close $fh;
			save_flags finalise @flags;
		} elsif( 1 == $rc ) {
			print "Cancelled, not saving changes.\n";
		}
		exit $rc;
	} elsif(POSIX::WIFSIGNALED($?)) {
		kill (POSIX::WTERMSIG($?), $$);
	} else {
		exit 127;
	}
	return;
}


# Write given list of flags back to make.conf if
# the file has not been changed since reading it.
# Parameters: list of flags
sub save_flags {
	my (@flags) = @_;
	my $BLANK = qr{(?:[ \n\t]+|#.*)+};              # whitespace and comments
	my $UBLNK = qr{(?:                              # as above, but scan for #USE=
		[ \n\t]+ |
		\#[ \t]*USE[ \t]*=.*(\n?) | # place capture after USE=... line
		\#.*)+}x;
	my $IDENT = qr{([^ \\\n\t'"{}=#]+)};            # identifiers
	my $ASSIG = qr{=};                              # assignment operator
	my $UQVAL = qr{(?:[^ \\\n\t'"#]+|\\.)+}s;       # unquoted value
	my $SQVAL = qr{'[^']*'};                        # singlequoted value
	my $DQVAL = qr{"(?:[^\\"]|\\.)*"}s;             # doublequoted value
	my $BNUQV = qr{(?:[^ \\\n\t'"#]+|\\\n()|\\.)+}s;# unquoted value (scan for \\\n)
	my $BNDQV = qr{"(?:[^\\"]|\\\n()|\\.)*"}s;      # doublequoted value (scan for \\\n)

	my $contents;

	my $makeconf_name = $Portage::used_make_conf;
	{
		open my $makeconf, '<', $makeconf_name or die "Couldn't open $makeconf_name\n";
		open my $makeconfold, '>', $makeconf_name . '~' or die "Couldn't open ${makeconf_name}~\n";
		local $/;
		$_ = <$makeconf>;
		print $makeconfold $_;
		close $makeconfold;
		close $makeconf;
	}

	my $sourcing = 0;
	eval {
		# USE comment start/end (start/end of newline character at the end, specifically)
		# default to end of make.conf, to handle make.confs without #USE=
		my($ucs, $uce) = (length, length);
		my $flags = '';
		pos = 0;
		for(;;) {
			if(/\G$UBLNK/gc) {
				($ucs, $uce) = ($-[1], $+[1]) if defined $1;
			}
			last if pos == length;
			my $flagatstartofline = do {
				my $linestart = 1+rindex $_, "\n", pos()-1;
				my $line = substr($_, $linestart, pos()-$linestart);
				$line !~ /[^ \t]/;
			};
			/\G$IDENT/gc or die;
			my $name = $1;
			/\G$BLANK/gc;
			if($name ne 'source') {
			/\G$ASSIG/gc or die;
			/\G$BLANK/gc;
			} else {
				$sourcing = 1;
			}
			die if pos == length;
			if($name ne 'USE') {
				/\G(?:$UQVAL|$SQVAL|$DQVAL)+/gc or die;
			} else {
				my $start = pos;
				/\G(?:$BNUQV|$SQVAL|$BNDQV)+/gc or die;
				my $end = pos;
				# save whether user uses backslash-newline
				my $bsnl = defined $1 || defined $2;
				# start of the line is one past the last newline; also handles first line
				my $linestart = 1+rindex $_, "\n", $start-1;
				# everything on the current line before the USE flags, plus one for the "
				my $line = substr($_, $linestart, $start-$linestart).' ';
				# only indent if USE starts a line
				my $blank = $flagatstartofline ? $line : "";
				$blank =~ s/[^ \t]/ /g;
				# word wrap
				if(@flags != 0) {
					my $length = 0;
					while($line =~ /(.)/g) {
						if($1 ne "\t") {
							$length++;
						} else {
							# no best tab size discussions, please. terminals use ts=8.
							$length&=~8;
							$length+=8;
						}
					}
					my $blanklength = $blank ne '' ? $length : 0;
					# new line, using backslash-newline if the user did that
					my $nl = ($bsnl ? " \\\n" : "\n").$blank;
					my $linelength = $bsnl ? 76 : 78;
					my $flag = $flags[0];
					if($blanklength != 0 || length $flag <= $linelength) {
						$flags   = $flag;
						$length += length $flag;
					} else {
						$flags   = $nl.$flag;
						$length  = length $flag;
					}
					for my $flag (@flags[1..$#flags]) {
						if($length + 1 + length $flag <= $linelength) {
							$flags  .= " $flag";
							$length += 1+length $flag;
						} else {
							$flags  .= $nl.$flag;
							$length  = $blanklength + length $flag;
						}
					}
				}
				# replace the current USE flags with the modified ones
				substr($_, $start, $end-$start) = "\"$flags\"";
				# and have the next search start after our new flags
				pos = $start + 2 + length $flags;
				# and end this
				undef $flags;
				last;
			}
		}
		if(defined $flags) { # if we didn't replace the flags, tack them after the last #USE= or at the end
			$flags = '';
			if(@flags != 0) {
				$flags = $flags[0];
				my $length = 5 + length $flags[0];
				for my $flag(@flags[1..$#flags]) {
					if($length + 1 + length $flag <= 78) {
						$flags  .= " $flag";
						$length += 1+length $flag;
					} else {
						$flags  .= "\n     $flag";
						$length  = 5+length $flag;
					}
				}
			}
			substr($_, $ucs, $uce-$ucs) = "\nUSE=\"$flags\"\n";
		} else { # if we replaced the flags, delete any further overrides
			for(;;) {
				my $start = pos;
				/\G$BLANK/gc;
				last if pos == length;
				/\G$IDENT/gc or die;
				my $name = $1;
				/\G$BLANK/gc;
				if($name ne 'source') {
				/\G$ASSIG/gc or die;
				/\G$BLANK/gc;
				} else {
					$sourcing = 1;
				}
				/\G(?:$UQVAL|$SQVAL|$DQVAL)+/gc or die;
				my $end = pos;
				if($name eq 'USE') {
					substr($_, $start, $end-$start) = '';
					pos = $start;
				}
			}
		}
	};
	die "Parse error when writing make.conf - did you modify it while ufed was running?\n" if $@;

	print STDERR <<EOF if $sourcing;
Warning: source command found in $makeconf_name. Flags may
be saved incorrectly if the sourced file modifies them.
EOF
	{
		open my $makeconf, '>', $makeconf_name or die "Couldn't open $makeconf_name\n";
		print $makeconf $_;
		close $makeconf;
		$makeconf_name =~ /\/make\.conf$/
			or print "USE flags written to $makeconf_name\n";
	}
	
	return;
}