Linux Audio

Check our new training course

Loading...
v5.14.15
  1#!/usr/bin/perl
  2# SPDX-License-Identifier: GPL-2.0
  3
  4use strict;
  5use Pod::Usage;
  6use Getopt::Long;
  7use File::Find;
  8use Fcntl ':mode';
  9use Cwd 'abs_path';
 10
 11my $help;
 12my $man;
 13my $debug;
 14my $arch;
 15my $feat;
 
 16
 17my $basename = abs_path($0);
 18$basename =~ s,/[^/]+$,/,;
 19
 20my $prefix=$basename . "../Documentation/features";
 21
 22# Used only at for full features output. The script will auto-adjust
 23# such values for the minimal possible values
 24my $status_size = 1;
 25my $description_size = 1;
 26
 27GetOptions(
 28	"debug|d+" => \$debug,
 29	"dir=s" => \$prefix,
 30	'help|?' => \$help,
 31	'arch=s' => \$arch,
 32	'feat=s' => \$feat,
 33	'feature=s' => \$feat,
 
 34	man => \$man
 35) or pod2usage(2);
 36
 37pod2usage(1) if $help;
 38pod2usage(-exitstatus => 0, -verbose => 2) if $man;
 39
 40pod2usage(1) if (scalar @ARGV < 1 || @ARGV > 2);
 41
 42my ($cmd, $arg) = @ARGV;
 43
 44pod2usage(2) if ($cmd ne "current" && $cmd ne "rest" && $cmd ne "validate"
 45		&& $cmd ne "ls" && $cmd ne "list");
 46
 47require Data::Dumper if ($debug);
 48
 49my %data;
 50my %archs;
 51
 52#
 53# Displays an error message, printing file name and line
 54#
 55sub parse_error($$$$) {
 56	my ($file, $ln, $msg, $data) = @_;
 57
 58	$data =~ s/\s+$/\n/;
 59
 60	print STDERR "Warning: file $file#$ln:\n\t$msg";
 61
 62	if ($data ne "") {
 63		print STDERR ". Line\n\t\t$data";
 64	} else {
 65	    print STDERR "\n";
 66	}
 67}
 68
 69#
 70# Parse a features file, storing its contents at %data
 71#
 72
 73my $h_name = "Feature";
 74my $h_kconfig = "Kconfig";
 75my $h_description = "Description";
 76my $h_subsys = "Subsystem";
 77my $h_status = "Status";
 78my $h_arch = "Architecture";
 79
 80my $max_size_name = length($h_name);
 81my $max_size_kconfig = length($h_kconfig);
 82my $max_size_description = length($h_description);
 83my $max_size_subsys = length($h_subsys);
 84my $max_size_status = length($h_status);
 85
 86my $max_size_arch = 0;
 87my $max_size_arch_with_header;
 88my $max_description_word = 0;
 89
 90sub parse_feat {
 91	my $file = $File::Find::name;
 92
 93	my $mode = (stat($file))[2];
 94	return if ($mode & S_IFDIR);
 95	return if ($file =~ m,($prefix)/arch-support.txt,);
 96	return if (!($file =~ m,arch-support.txt$,));
 97
 
 
 
 
 98	my $subsys = "";
 99	$subsys = $2 if ( m,.*($prefix)/([^/]+).*,);
100
101	if (length($subsys) > $max_size_subsys) {
102		$max_size_subsys = length($subsys);
103	}
104
105	my $name;
106	my $kconfig;
107	my $description;
108	my $comments = "";
109	my $last_status;
110	my $ln;
111	my %arch_table;
112
113	print STDERR "Opening $file\n" if ($debug > 1);
114	open IN, $file;
115
116	while(<IN>) {
117		$ln++;
118
119		if (m/^\#\s+Feature\s+name:\s*(.*\S)/) {
120			$name = $1;
121			if (length($name) > $max_size_name) {
122				$max_size_name = length($name);
123			}
124			next;
125		}
126		if (m/^\#\s+Kconfig:\s*(.*\S)/) {
127			$kconfig = $1;
128			if (length($kconfig) > $max_size_kconfig) {
129				$max_size_kconfig = length($kconfig);
130			}
131			next;
132		}
133		if (m/^\#\s+description:\s*(.*\S)/) {
134			$description = $1;
135			if (length($description) > $max_size_description) {
136				$max_size_description = length($description);
137			}
138
139			foreach my $word (split /\s+/, $description) {
140				if (length($word) > $max_description_word) {
141					$max_description_word = length($word);
142				}
143			}
144
145			next;
146		}
147		next if (m/^\\s*$/);
148		next if (m/^\s*\-+\s*$/);
149		next if (m/^\s*\|\s*arch\s*\|\s*status\s*\|\s*$/);
150
151		if (m/^\#\s*(.*)/) {
152			$comments .= "$1\n";
153			next;
154		}
155		if (m/^\s*\|\s*(\S+):\s*\|\s*(\S+)\s*\|\s*$/) {
156			my $a = $1;
157			my $status = $2;
158
159			if (length($status) > $max_size_status) {
160				$max_size_status = length($status);
161			}
162			if (length($a) > $max_size_arch) {
163				$max_size_arch = length($a);
164			}
165
166			$status = "---" if ($status =~ m/^\.\.$/);
167
168			$archs{$a} = 1;
169			$arch_table{$a} = $status;
170			next;
171		}
172
173		#Everything else is an error
174		parse_error($file, $ln, "line is invalid", $_);
175	}
176	close IN;
177
178	if (!$name) {
179		parse_error($file, $ln, "Feature name not found", "");
180		return;
181	}
182
183	parse_error($file, $ln, "Subsystem not found", "") if (!$subsys);
184	parse_error($file, $ln, "Kconfig not found", "") if (!$kconfig);
185	parse_error($file, $ln, "Description not found", "") if (!$description);
186
187	if (!%arch_table) {
188		parse_error($file, $ln, "Architecture table not found", "");
189		return;
190	}
191
192	$data{$name}->{where} = $file;
193	$data{$name}->{subsys} = $subsys;
194	$data{$name}->{kconfig} = $kconfig;
195	$data{$name}->{description} = $description;
196	$data{$name}->{comments} = $comments;
197	$data{$name}->{table} = \%arch_table;
198
199	$max_size_arch_with_header = $max_size_arch + length($h_arch);
200}
201
202#
203# Output feature(s) for a given architecture
204#
205sub output_arch_table {
206	my $title = "Feature status on $arch architecture";
207
208	print "=" x length($title) . "\n";
209	print "$title\n";
210	print "=" x length($title) . "\n\n";
211
212	print "=" x $max_size_subsys;
213	print "  ";
214	print "=" x $max_size_name;
215	print "  ";
216	print "=" x $max_size_kconfig;
217	print "  ";
218	print "=" x $max_size_status;
219	print "  ";
220	print "=" x $max_size_description;
221	print "\n";
222	printf "%-${max_size_subsys}s  ", $h_subsys;
223	printf "%-${max_size_name}s  ", $h_name;
224	printf "%-${max_size_kconfig}s  ", $h_kconfig;
225	printf "%-${max_size_status}s  ", $h_status;
226	printf "%-${max_size_description}s\n", $h_description;
227	print "=" x $max_size_subsys;
228	print "  ";
229	print "=" x $max_size_name;
230	print "  ";
231	print "=" x $max_size_kconfig;
232	print "  ";
233	print "=" x $max_size_status;
234	print "  ";
235	print "=" x $max_size_description;
236	print "\n";
237
238	foreach my $name (sort {
239				($data{$a}->{subsys} cmp $data{$b}->{subsys}) ||
240				("\L$a" cmp "\L$b")
241			       } keys %data) {
242		next if ($feat && $name ne $feat);
243
244		my %arch_table = %{$data{$name}->{table}};
245		printf "%-${max_size_subsys}s  ", $data{$name}->{subsys};
246		printf "%-${max_size_name}s  ", $name;
247		printf "%-${max_size_kconfig}s  ", $data{$name}->{kconfig};
248		printf "%-${max_size_status}s  ", $arch_table{$arch};
249		printf "%-s\n", $data{$name}->{description};
250	}
251
252	print "=" x $max_size_subsys;
253	print "  ";
254	print "=" x $max_size_name;
255	print "  ";
256	print "=" x $max_size_kconfig;
257	print "  ";
258	print "=" x $max_size_status;
259	print "  ";
260	print "=" x $max_size_description;
261	print "\n";
262}
263
264#
265# list feature(s) for a given architecture
266#
267sub list_arch_features {
268	print "#\n# Kernel feature support matrix of the '$arch' architecture:\n#\n";
269
270	foreach my $name (sort {
271				($data{$a}->{subsys} cmp $data{$b}->{subsys}) ||
272				("\L$a" cmp "\L$b")
273			       } keys %data) {
274		next if ($feat && $name ne $feat);
275
276		my %arch_table = %{$data{$name}->{table}};
277
278		my $status = $arch_table{$arch};
279		$status = " " x ((4 - length($status)) / 2) . $status;
280
281		printf " %${max_size_subsys}s/ ", $data{$name}->{subsys};
282		printf "%-${max_size_name}s: ", $name;
283		printf "%-5s|   ", $status;
284		printf "%${max_size_kconfig}s # ", $data{$name}->{kconfig};
285		printf " %s\n", $data{$name}->{description};
286	}
287}
288
289#
290# Output a feature on all architectures
291#
292sub output_feature {
293	my $title = "Feature $feat";
294
295	print "=" x length($title) . "\n";
296	print "$title\n";
297	print "=" x length($title) . "\n\n";
298
299	print ":Subsystem: $data{$feat}->{subsys} \n" if ($data{$feat}->{subsys});
300	print ":Kconfig: $data{$feat}->{kconfig} \n" if ($data{$feat}->{kconfig});
301
302	my $desc = $data{$feat}->{description};
303	$desc =~ s/^([a-z])/\U$1/;
304	$desc =~ s/\.?\s*//;
305	print "\n$desc.\n\n";
306
307	my $com = $data{$feat}->{comments};
308	$com =~ s/^\s+//;
309	$com =~ s/\s+$//;
310	if ($com) {
311		print "Comments\n";
312		print "--------\n\n";
313		print "$com\n\n";
314	}
315
316	print "=" x $max_size_arch_with_header;
317	print "  ";
318	print "=" x $max_size_status;
319	print "\n";
320
321	printf "%-${max_size_arch}s  ", $h_arch;
322	printf "%-${max_size_status}s", $h_status . "\n";
323
324	print "=" x $max_size_arch_with_header;
325	print "  ";
326	print "=" x $max_size_status;
327	print "\n";
328
329	my %arch_table = %{$data{$feat}->{table}};
330	foreach my $arch (sort keys %arch_table) {
331		printf "%-${max_size_arch}s  ", $arch;
332		printf "%-${max_size_status}s\n", $arch_table{$arch};
333	}
334
335	print "=" x $max_size_arch_with_header;
336	print "  ";
337	print "=" x $max_size_status;
338	print "\n";
339}
340
341#
342# Output all features for all architectures
343#
344
345sub matrix_lines($$$) {
346	my $desc_size = shift;
347	my $status_size = shift;
348	my $header = shift;
349	my $fill;
350	my $ln_marker;
351
352	if ($header) {
353		$ln_marker = "=";
354	} else {
355		$ln_marker = "-";
356	}
357
358	$fill = $ln_marker;
359
360	print "+";
361	print $fill x $max_size_name;
362	print "+";
363	print $fill x $desc_size;
364	print "+";
365	print $ln_marker x $status_size;
366	print "+\n";
367}
368
369sub output_matrix {
370	my $title = "Feature status on all architectures";
371	my $notcompat = "Not compatible";
372
373	print "=" x length($title) . "\n";
374	print "$title\n";
375	print "=" x length($title) . "\n\n";
376
377	my $desc_title = "$h_kconfig / $h_description";
378
379	my $desc_size = $max_size_kconfig + 4;
380	if (!$description_size) {
381		$desc_size = $max_size_description if ($max_size_description > $desc_size);
382	} else {
383		$desc_size = $description_size if ($description_size > $desc_size);
384	}
385	$desc_size = $max_description_word if ($max_description_word > $desc_size);
386
387	$desc_size = length($desc_title) if (length($desc_title) > $desc_size);
388
389	$max_size_status = length($notcompat) if (length($notcompat) > $max_size_status);
390
391	# Ensure that the status will fit
392	my $min_status_size = $max_size_status + $max_size_arch + 6;
393	$status_size = $min_status_size if ($status_size < $min_status_size);
394
395
396	my $cur_subsys = "";
397	foreach my $name (sort {
398				($data{$a}->{subsys} cmp $data{$b}->{subsys}) or
399				("\L$a" cmp "\L$b")
400			       } keys %data) {
401
402		if ($cur_subsys ne $data{$name}->{subsys}) {
403			if ($cur_subsys ne "") {
404				printf "\n";
405			}
406
407			$cur_subsys = $data{$name}->{subsys};
408
409			my $title = "Subsystem: $cur_subsys";
410			print "$title\n";
411			print "=" x length($title) . "\n\n";
412
413
414			matrix_lines($desc_size, $status_size, 0);
415
416			printf "|%-${max_size_name}s", $h_name;
417			printf "|%-${desc_size}s", $desc_title;
418
419			printf "|%-${status_size}s|\n", "Status per architecture";
420			matrix_lines($desc_size, $status_size, 1);
421		}
422
423		my %arch_table = %{$data{$name}->{table}};
424		my $cur_status = "";
425
426		my (@lines, @descs);
427		my $line = "";
428		foreach my $arch (sort {
429					($arch_table{$b} cmp $arch_table{$a}) or
430					("\L$a" cmp "\L$b")
431				       } keys %arch_table) {
432
433			my $status = $arch_table{$arch};
434
435			if ($status eq "---") {
436				$status = $notcompat;
437			}
438
439			if ($status ne $cur_status) {
440				if ($line ne "") {
441					push @lines, $line;
442					$line = "";
443				}
444				$line = "- **" . $status . "**: " . $arch;
445			} elsif (length($line) + length ($arch) + 2 < $status_size) {
446				$line .= ", " . $arch;
447			} else {
448				push @lines, $line;
449				$line = "  " . $arch;
450			}
451			$cur_status = $status;
452		}
453		push @lines, $line if ($line ne "");
454
455		my $description = $data{$name}->{description};
456		while (length($description) > $desc_size) {
457			my $d = substr $description, 0, $desc_size;
458
459			# Ensure that it will end on a space
460			# if it can't, it means that the size is too small
461			# Instead of aborting it, let's print what we have
462			if (!($d =~ s/^(.*)\s+.*/$1/)) {
463				$d = substr $d, 0, -1;
464				push @descs, "$d\\";
465				$description =~ s/^\Q$d\E//;
466			} else {
467				push @descs, $d;
468				$description =~ s/^\Q$d\E\s+//;
469			}
470		}
471		push @descs, $description;
472
473		# Ensure that the full description will be printed
474		push @lines, "" while (scalar(@lines) < 2 + scalar(@descs));
475
476		my $ln = 0;
477		for my $line(@lines) {
478			if (!$ln) {
479				printf "|%-${max_size_name}s", $name;
480				printf "|%-${desc_size}s", "``" . $data{$name}->{kconfig} . "``";
481			} elsif ($ln >= 2 && scalar(@descs)) {
482				printf "|%-${max_size_name}s", "";
483				printf "|%-${desc_size}s", shift @descs;
484			} else {
485				printf "|%-${max_size_name}s", "";
486				printf "|%-${desc_size}s", "";
487			}
488
489			printf "|%-${status_size}s|\n", $line;
490
491			$ln++;
492		}
493		matrix_lines($desc_size, $status_size, 0);
494	}
495}
496
497
498#
499# Parses all feature files located at $prefix dir
500#
501find({wanted =>\&parse_feat, no_chdir => 1}, $prefix);
502
503print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
504
505#
506# Handles the command
507#
508if ($cmd eq "current") {
509	$arch = qx(uname -m | sed 's/x86_64/x86/' | sed 's/i386/x86/');
510	$arch =~s/\s+$//;
511}
512
513if ($cmd eq "ls" or $cmd eq "list") {
514	if (!$arch) {
515		$arch = qx(uname -m | sed 's/x86_64/x86/' | sed 's/i386/x86/');
516		$arch =~s/\s+$//;
517	}
518
519	list_arch_features;
520
521	exit;
522}
523
524if ($cmd ne "validate") {
525	if ($arch) {
526		output_arch_table;
527	} elsif ($feat) {
528		output_feature;
529	} else {
530		output_matrix;
531	}
532}
533
534__END__
535
536=head1 NAME
537
538get_feat.pl - parse the Linux Feature files and produce a ReST book.
539
540=head1 SYNOPSIS
541
542B<get_feat.pl> [--debug] [--man] [--help] [--dir=<dir>] [--arch=<arch>]
543	       [--feature=<feature>|--feat=<feature>] <COMAND> [<ARGUMENT>]
544
545Where <COMMAND> can be:
546
547=over 8
548
549B<current>               - output table in ReST compatible ASCII format
550			   with features for this machine's architecture
551
552B<rest>                  - output table(s)  in ReST compatible ASCII format
553			   with features in ReST markup language. The output
554			   is affected by --arch or --feat/--feature flags.
555
556B<validate>              - validate the contents of the files under
557			   Documentation/features.
558
559B<ls> or B<list>         - list features for this machine's architecture,
560			   using an easier to parse format.
561			   The output is affected by --arch flag.
562
563=back
564
565=head1 OPTIONS
566
567=over 8
568
569=item B<--arch>
570
571Output features for an specific architecture, optionally filtering for
572a single specific feature.
573
574=item B<--feat> or B<--feature>
575
576Output features for a single specific feature.
577
578=item B<--dir>
579
580Changes the location of the Feature files. By default, it uses
581the Documentation/features directory.
 
 
 
 
 
582
583=item B<--debug>
584
585Put the script in verbose mode, useful for debugging. Can be called multiple
586times, to increase verbosity.
587
588=item B<--help>
589
590Prints a brief help message and exits.
591
592=item B<--man>
593
594Prints the manual page and exits.
595
596=back
597
598=head1 DESCRIPTION
599
600Parse the Linux feature files from Documentation/features (by default),
601optionally producing results at ReST format.
602
603It supports output data per architecture, per feature or a
604feature x arch matrix.
605
606When used with B<rest> command, it will use either one of the tree formats:
607
608If neither B<--arch> or B<--feature> arguments are used, it will output a
609matrix with features per architecture.
610
611If B<--arch> argument is used, it will output the features availability for
612a given architecture.
613
614If B<--feat> argument is used, it will output the content of the feature
615file using ReStructured Text markup.
616
617=head1 BUGS
618
619Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
620
621=head1 COPYRIGHT
622
623Copyright (c) 2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
624
625License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
626
627This is free software: you are free to change and redistribute it.
628There is NO WARRANTY, to the extent permitted by law.
629
630=cut
v6.13.7
  1#!/usr/bin/env perl
  2# SPDX-License-Identifier: GPL-2.0
  3
  4use strict;
  5use Pod::Usage;
  6use Getopt::Long;
  7use File::Find;
  8use Fcntl ':mode';
  9use Cwd 'abs_path';
 10
 11my $help;
 12my $man;
 13my $debug;
 14my $arch;
 15my $feat;
 16my $enable_fname;
 17
 18my $basename = abs_path($0);
 19$basename =~ s,/[^/]+$,/,;
 20
 21my $prefix=$basename . "../Documentation/features";
 22
 23# Used only at for full features output. The script will auto-adjust
 24# such values for the minimal possible values
 25my $status_size = 1;
 26my $description_size = 1;
 27
 28GetOptions(
 29	"debug|d+" => \$debug,
 30	"dir=s" => \$prefix,
 31	'help|?' => \$help,
 32	'arch=s' => \$arch,
 33	'feat=s' => \$feat,
 34	'feature=s' => \$feat,
 35	"enable-fname" => \$enable_fname,
 36	man => \$man
 37) or pod2usage(2);
 38
 39pod2usage(1) if $help;
 40pod2usage(-exitstatus => 0, -verbose => 2) if $man;
 41
 42pod2usage(1) if (scalar @ARGV < 1 || @ARGV > 2);
 43
 44my ($cmd, $arg) = @ARGV;
 45
 46pod2usage(2) if ($cmd ne "current" && $cmd ne "rest" && $cmd ne "validate"
 47		&& $cmd ne "ls" && $cmd ne "list");
 48
 49require Data::Dumper if ($debug);
 50
 51my %data;
 52my %archs;
 53
 54#
 55# Displays an error message, printing file name and line
 56#
 57sub parse_error($$$$) {
 58	my ($file, $ln, $msg, $data) = @_;
 59
 60	$data =~ s/\s+$/\n/;
 61
 62	print STDERR "Warning: file $file#$ln:\n\t$msg";
 63
 64	if ($data ne "") {
 65		print STDERR ". Line\n\t\t$data";
 66	} else {
 67	    print STDERR "\n";
 68	}
 69}
 70
 71#
 72# Parse a features file, storing its contents at %data
 73#
 74
 75my $h_name = "Feature";
 76my $h_kconfig = "Kconfig";
 77my $h_description = "Description";
 78my $h_subsys = "Subsystem";
 79my $h_status = "Status";
 80my $h_arch = "Architecture";
 81
 82my $max_size_name = length($h_name);
 83my $max_size_kconfig = length($h_kconfig);
 84my $max_size_description = length($h_description);
 85my $max_size_subsys = length($h_subsys);
 86my $max_size_status = length($h_status);
 87
 88my $max_size_arch = 0;
 89my $max_size_arch_with_header;
 90my $max_description_word = 0;
 91
 92sub parse_feat {
 93	my $file = $File::Find::name;
 94
 95	my $mode = (stat($file))[2];
 96	return if ($mode & S_IFDIR);
 97	return if ($file =~ m,($prefix)/arch-support.txt,);
 98	return if (!($file =~ m,arch-support.txt$,));
 99
100	if ($enable_fname) {
101		printf ".. FILE %s\n", abs_path($file);
102	}
103
104	my $subsys = "";
105	$subsys = $2 if ( m,.*($prefix)/([^/]+).*,);
106
107	if (length($subsys) > $max_size_subsys) {
108		$max_size_subsys = length($subsys);
109	}
110
111	my $name;
112	my $kconfig;
113	my $description;
114	my $comments = "";
115	my $last_status;
116	my $ln;
117	my %arch_table;
118
119	print STDERR "Opening $file\n" if ($debug > 1);
120	open IN, $file;
121
122	while(<IN>) {
123		$ln++;
124
125		if (m/^\#\s+Feature\s+name:\s*(.*\S)/) {
126			$name = $1;
127			if (length($name) > $max_size_name) {
128				$max_size_name = length($name);
129			}
130			next;
131		}
132		if (m/^\#\s+Kconfig:\s*(.*\S)/) {
133			$kconfig = $1;
134			if (length($kconfig) > $max_size_kconfig) {
135				$max_size_kconfig = length($kconfig);
136			}
137			next;
138		}
139		if (m/^\#\s+description:\s*(.*\S)/) {
140			$description = $1;
141			if (length($description) > $max_size_description) {
142				$max_size_description = length($description);
143			}
144
145			foreach my $word (split /\s+/, $description) {
146				if (length($word) > $max_description_word) {
147					$max_description_word = length($word);
148				}
149			}
150
151			next;
152		}
153		next if (m/^\\s*$/);
154		next if (m/^\s*\-+\s*$/);
155		next if (m/^\s*\|\s*arch\s*\|\s*status\s*\|\s*$/);
156
157		if (m/^\#\s*(.*)/) {
158			$comments .= "$1\n";
159			next;
160		}
161		if (m/^\s*\|\s*(\S+):\s*\|\s*(\S+)\s*\|\s*$/) {
162			my $a = $1;
163			my $status = $2;
164
165			if (length($status) > $max_size_status) {
166				$max_size_status = length($status);
167			}
168			if (length($a) > $max_size_arch) {
169				$max_size_arch = length($a);
170			}
171
172			$status = "---" if ($status =~ m/^\.\.$/);
173
174			$archs{$a} = 1;
175			$arch_table{$a} = $status;
176			next;
177		}
178
179		#Everything else is an error
180		parse_error($file, $ln, "line is invalid", $_);
181	}
182	close IN;
183
184	if (!$name) {
185		parse_error($file, $ln, "Feature name not found", "");
186		return;
187	}
188
189	parse_error($file, $ln, "Subsystem not found", "") if (!$subsys);
190	parse_error($file, $ln, "Kconfig not found", "") if (!$kconfig);
191	parse_error($file, $ln, "Description not found", "") if (!$description);
192
193	if (!%arch_table) {
194		parse_error($file, $ln, "Architecture table not found", "");
195		return;
196	}
197
198	$data{$name}->{where} = $file;
199	$data{$name}->{subsys} = $subsys;
200	$data{$name}->{kconfig} = $kconfig;
201	$data{$name}->{description} = $description;
202	$data{$name}->{comments} = $comments;
203	$data{$name}->{table} = \%arch_table;
204
205	$max_size_arch_with_header = $max_size_arch + length($h_arch);
206}
207
208#
209# Output feature(s) for a given architecture
210#
211sub output_arch_table {
212	my $title = "Feature status on $arch architecture";
213
214	print "=" x length($title) . "\n";
215	print "$title\n";
216	print "=" x length($title) . "\n\n";
217
218	print "=" x $max_size_subsys;
219	print "  ";
220	print "=" x $max_size_name;
221	print "  ";
222	print "=" x $max_size_kconfig;
223	print "  ";
224	print "=" x $max_size_status;
225	print "  ";
226	print "=" x $max_size_description;
227	print "\n";
228	printf "%-${max_size_subsys}s  ", $h_subsys;
229	printf "%-${max_size_name}s  ", $h_name;
230	printf "%-${max_size_kconfig}s  ", $h_kconfig;
231	printf "%-${max_size_status}s  ", $h_status;
232	printf "%-${max_size_description}s\n", $h_description;
233	print "=" x $max_size_subsys;
234	print "  ";
235	print "=" x $max_size_name;
236	print "  ";
237	print "=" x $max_size_kconfig;
238	print "  ";
239	print "=" x $max_size_status;
240	print "  ";
241	print "=" x $max_size_description;
242	print "\n";
243
244	foreach my $name (sort {
245				($data{$a}->{subsys} cmp $data{$b}->{subsys}) ||
246				("\L$a" cmp "\L$b")
247			       } keys %data) {
248		next if ($feat && $name ne $feat);
249
250		my %arch_table = %{$data{$name}->{table}};
251		printf "%-${max_size_subsys}s  ", $data{$name}->{subsys};
252		printf "%-${max_size_name}s  ", $name;
253		printf "%-${max_size_kconfig}s  ", $data{$name}->{kconfig};
254		printf "%-${max_size_status}s  ", $arch_table{$arch};
255		printf "%-s\n", $data{$name}->{description};
256	}
257
258	print "=" x $max_size_subsys;
259	print "  ";
260	print "=" x $max_size_name;
261	print "  ";
262	print "=" x $max_size_kconfig;
263	print "  ";
264	print "=" x $max_size_status;
265	print "  ";
266	print "=" x $max_size_description;
267	print "\n";
268}
269
270#
271# list feature(s) for a given architecture
272#
273sub list_arch_features {
274	print "#\n# Kernel feature support matrix of the '$arch' architecture:\n#\n";
275
276	foreach my $name (sort {
277				($data{$a}->{subsys} cmp $data{$b}->{subsys}) ||
278				("\L$a" cmp "\L$b")
279			       } keys %data) {
280		next if ($feat && $name ne $feat);
281
282		my %arch_table = %{$data{$name}->{table}};
283
284		my $status = $arch_table{$arch};
285		$status = " " x ((4 - length($status)) / 2) . $status;
286
287		printf " %${max_size_subsys}s/ ", $data{$name}->{subsys};
288		printf "%-${max_size_name}s: ", $name;
289		printf "%-5s|   ", $status;
290		printf "%${max_size_kconfig}s # ", $data{$name}->{kconfig};
291		printf " %s\n", $data{$name}->{description};
292	}
293}
294
295#
296# Output a feature on all architectures
297#
298sub output_feature {
299	my $title = "Feature $feat";
300
301	print "=" x length($title) . "\n";
302	print "$title\n";
303	print "=" x length($title) . "\n\n";
304
305	print ":Subsystem: $data{$feat}->{subsys} \n" if ($data{$feat}->{subsys});
306	print ":Kconfig: $data{$feat}->{kconfig} \n" if ($data{$feat}->{kconfig});
307
308	my $desc = $data{$feat}->{description};
309	$desc =~ s/^([a-z])/\U$1/;
310	$desc =~ s/\.?\s*//;
311	print "\n$desc.\n\n";
312
313	my $com = $data{$feat}->{comments};
314	$com =~ s/^\s+//;
315	$com =~ s/\s+$//;
316	if ($com) {
317		print "Comments\n";
318		print "--------\n\n";
319		print "$com\n\n";
320	}
321
322	print "=" x $max_size_arch_with_header;
323	print "  ";
324	print "=" x $max_size_status;
325	print "\n";
326
327	printf "%-${max_size_arch}s  ", $h_arch;
328	printf "%-${max_size_status}s", $h_status . "\n";
329
330	print "=" x $max_size_arch_with_header;
331	print "  ";
332	print "=" x $max_size_status;
333	print "\n";
334
335	my %arch_table = %{$data{$feat}->{table}};
336	foreach my $arch (sort keys %arch_table) {
337		printf "%-${max_size_arch}s  ", $arch;
338		printf "%-${max_size_status}s\n", $arch_table{$arch};
339	}
340
341	print "=" x $max_size_arch_with_header;
342	print "  ";
343	print "=" x $max_size_status;
344	print "\n";
345}
346
347#
348# Output all features for all architectures
349#
350
351sub matrix_lines($$$) {
352	my $desc_size = shift;
353	my $status_size = shift;
354	my $header = shift;
355	my $fill;
356	my $ln_marker;
357
358	if ($header) {
359		$ln_marker = "=";
360	} else {
361		$ln_marker = "-";
362	}
363
364	$fill = $ln_marker;
365
366	print "+";
367	print $fill x $max_size_name;
368	print "+";
369	print $fill x $desc_size;
370	print "+";
371	print $ln_marker x $status_size;
372	print "+\n";
373}
374
375sub output_matrix {
376	my $title = "Feature status on all architectures";
377	my $notcompat = "Not compatible";
378
379	print "=" x length($title) . "\n";
380	print "$title\n";
381	print "=" x length($title) . "\n\n";
382
383	my $desc_title = "$h_kconfig / $h_description";
384
385	my $desc_size = $max_size_kconfig + 4;
386	if (!$description_size) {
387		$desc_size = $max_size_description if ($max_size_description > $desc_size);
388	} else {
389		$desc_size = $description_size if ($description_size > $desc_size);
390	}
391	$desc_size = $max_description_word if ($max_description_word > $desc_size);
392
393	$desc_size = length($desc_title) if (length($desc_title) > $desc_size);
394
395	$max_size_status = length($notcompat) if (length($notcompat) > $max_size_status);
396
397	# Ensure that the status will fit
398	my $min_status_size = $max_size_status + $max_size_arch + 6;
399	$status_size = $min_status_size if ($status_size < $min_status_size);
400
401
402	my $cur_subsys = "";
403	foreach my $name (sort {
404				($data{$a}->{subsys} cmp $data{$b}->{subsys}) or
405				("\L$a" cmp "\L$b")
406			       } keys %data) {
407
408		if ($cur_subsys ne $data{$name}->{subsys}) {
409			if ($cur_subsys ne "") {
410				printf "\n";
411			}
412
413			$cur_subsys = $data{$name}->{subsys};
414
415			my $title = "Subsystem: $cur_subsys";
416			print "$title\n";
417			print "=" x length($title) . "\n\n";
418
419
420			matrix_lines($desc_size, $status_size, 0);
421
422			printf "|%-${max_size_name}s", $h_name;
423			printf "|%-${desc_size}s", $desc_title;
424
425			printf "|%-${status_size}s|\n", "Status per architecture";
426			matrix_lines($desc_size, $status_size, 1);
427		}
428
429		my %arch_table = %{$data{$name}->{table}};
430		my $cur_status = "";
431
432		my (@lines, @descs);
433		my $line = "";
434		foreach my $arch (sort {
435					($arch_table{$b} cmp $arch_table{$a}) or
436					("\L$a" cmp "\L$b")
437				       } keys %arch_table) {
438
439			my $status = $arch_table{$arch};
440
441			if ($status eq "---") {
442				$status = $notcompat;
443			}
444
445			if ($status ne $cur_status) {
446				if ($line ne "") {
447					push @lines, $line;
448					$line = "";
449				}
450				$line = "- **" . $status . "**: " . $arch;
451			} elsif (length($line) + length ($arch) + 2 < $status_size) {
452				$line .= ", " . $arch;
453			} else {
454				push @lines, $line;
455				$line = "  " . $arch;
456			}
457			$cur_status = $status;
458		}
459		push @lines, $line if ($line ne "");
460
461		my $description = $data{$name}->{description};
462		while (length($description) > $desc_size) {
463			my $d = substr $description, 0, $desc_size;
464
465			# Ensure that it will end on a space
466			# if it can't, it means that the size is too small
467			# Instead of aborting it, let's print what we have
468			if (!($d =~ s/^(.*)\s+.*/$1/)) {
469				$d = substr $d, 0, -1;
470				push @descs, "$d\\";
471				$description =~ s/^\Q$d\E//;
472			} else {
473				push @descs, $d;
474				$description =~ s/^\Q$d\E\s+//;
475			}
476		}
477		push @descs, $description;
478
479		# Ensure that the full description will be printed
480		push @lines, "" while (scalar(@lines) < 2 + scalar(@descs));
481
482		my $ln = 0;
483		for my $line(@lines) {
484			if (!$ln) {
485				printf "|%-${max_size_name}s", $name;
486				printf "|%-${desc_size}s", "``" . $data{$name}->{kconfig} . "``";
487			} elsif ($ln >= 2 && scalar(@descs)) {
488				printf "|%-${max_size_name}s", "";
489				printf "|%-${desc_size}s", shift @descs;
490			} else {
491				printf "|%-${max_size_name}s", "";
492				printf "|%-${desc_size}s", "";
493			}
494
495			printf "|%-${status_size}s|\n", $line;
496
497			$ln++;
498		}
499		matrix_lines($desc_size, $status_size, 0);
500	}
501}
502
503
504#
505# Parses all feature files located at $prefix dir
506#
507find({wanted =>\&parse_feat, no_chdir => 1}, $prefix);
508
509print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
510
511#
512# Handles the command
513#
514if ($cmd eq "current") {
515	$arch = qx(uname -m | sed 's/x86_64/x86/' | sed 's/i386/x86/');
516	$arch =~s/\s+$//;
517}
518
519if ($cmd eq "ls" or $cmd eq "list") {
520	if (!$arch) {
521		$arch = qx(uname -m | sed 's/x86_64/x86/' | sed 's/i386/x86/');
522		$arch =~s/\s+$//;
523	}
524
525	list_arch_features;
526
527	exit;
528}
529
530if ($cmd ne "validate") {
531	if ($arch) {
532		output_arch_table;
533	} elsif ($feat) {
534		output_feature;
535	} else {
536		output_matrix;
537	}
538}
539
540__END__
541
542=head1 NAME
543
544get_feat.pl - parse the Linux Feature files and produce a ReST book.
545
546=head1 SYNOPSIS
547
548B<get_feat.pl> [--debug] [--man] [--help] [--dir=<dir>] [--arch=<arch>]
549	       [--feature=<feature>|--feat=<feature>] <COMAND> [<ARGUMENT>]
550
551Where <COMMAND> can be:
552
553=over 8
554
555B<current>               - output table in ReST compatible ASCII format
556			   with features for this machine's architecture
557
558B<rest>                  - output table(s)  in ReST compatible ASCII format
559			   with features in ReST markup language. The output
560			   is affected by --arch or --feat/--feature flags.
561
562B<validate>              - validate the contents of the files under
563			   Documentation/features.
564
565B<ls> or B<list>         - list features for this machine's architecture,
566			   using an easier to parse format.
567			   The output is affected by --arch flag.
568
569=back
570
571=head1 OPTIONS
572
573=over 8
574
575=item B<--arch>
576
577Output features for an specific architecture, optionally filtering for
578a single specific feature.
579
580=item B<--feat> or B<--feature>
581
582Output features for a single specific feature.
583
584=item B<--dir>
585
586Changes the location of the Feature files. By default, it uses
587the Documentation/features directory.
588
589=item B<--enable-fname>
590
591Prints the file name of the feature files. This can be used in order to
592track dependencies during documentation build.
593
594=item B<--debug>
595
596Put the script in verbose mode, useful for debugging. Can be called multiple
597times, to increase verbosity.
598
599=item B<--help>
600
601Prints a brief help message and exits.
602
603=item B<--man>
604
605Prints the manual page and exits.
606
607=back
608
609=head1 DESCRIPTION
610
611Parse the Linux feature files from Documentation/features (by default),
612optionally producing results at ReST format.
613
614It supports output data per architecture, per feature or a
615feature x arch matrix.
616
617When used with B<rest> command, it will use either one of the tree formats:
618
619If neither B<--arch> or B<--feature> arguments are used, it will output a
620matrix with features per architecture.
621
622If B<--arch> argument is used, it will output the features availability for
623a given architecture.
624
625If B<--feat> argument is used, it will output the content of the feature
626file using ReStructured Text markup.
627
628=head1 BUGS
629
630Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
631
632=head1 COPYRIGHT
633
634Copyright (c) 2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
635
636License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
637
638This is free software: you are free to change and redistribute it.
639There is NO WARRANTY, to the extent permitted by law.
640
641=cut