From b882ebb991969f278aa2b20200270e9146838cad Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Wed, 6 Nov 2002 22:46:15 +0000 Subject: [PATCH] Complete rewrite Pass -w and use strict Work with almost all lm_sensors 2 releases Better data structures, allowing for linear complexity (O(N) compared with O(N^2) or worst previously) and thus the script is way faster Handle shared arrays Nicer output (my opinion at least) git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@1575 7894878c-1315-0410-8ee3-d5d059ff63e0 --- prog/doc/doc-features.pl | 526 ++++++++++++++++++++------------------- 1 file changed, 271 insertions(+), 255 deletions(-) diff --git a/prog/doc/doc-features.pl b/prog/doc/doc-features.pl index b56ede09..81cd1d2a 100755 --- a/prog/doc/doc-features.pl +++ b/prog/doc/doc-features.pl @@ -1,8 +1,9 @@ -#!/usr/bin/perl +#!/usr/bin/perl -w # # doc-features.pl - Generate module/library documentation # Copyright (c) 1999 Frodo Looijaard +# Copyright (c) 2002 Jean Delvare # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -21,297 +22,312 @@ use strict; -# Features is a hash of references (indexed by SENSORS_*_PREFIX names) to -# a hash with these elements: -# prefix: The prefix name -# arrayname: The name of the c-array containing its data -# features: A reference to an array of references to hashes, each -# containing the following fields: -# number: The preprocessor symbol for the number of this feature -# name: The name of this feature -# logical_mapping: The preprocessor symbol for the logical mapping -# feature -# compute_mapping: The preprocessor symbol for the compute mapping -# feature -# mode_read: 1 if the feature is readable, 0 if not -# mode_write: 1 if the feature is writable, 0 if not -# sysctl: Preprocessor name of the sysctl file -# offset: Offset (in longs) within the sysctl file -# magnitude: Magnitude of this feature +# %prefix contains the SENSORS_*_PREFIX defines as found in lib/chips.h. +# It is generated by scan_chips_h(). It shouldn't have duplicated values. +# %arrayname contains the sensors_chip_features array as found in +# lib/chips.c. It is generated by scan_chips_c_1(). It may have duplicated +# values. +# %prefix and %arrayname are expected to have the same set of keys, and +# this is verified in scan_all(). +# +# %sysctl contains the sysctl filenames as found in kernel/chips/*.c. It +# is generated by scan_kernel_chip, which is called on each file. It has +# (of course) duplicated values. +# +# %features contains the features for all chips as found in lib/chips.c. +# They are stored as arrays of references to hashes, each containing the +# following fields: +# number: The preprocessor symbol for the number of this feature +# name: The name of this feature +# logical_mapping: The preprocessor symbol for the logical mapping +# feature +# compute_mapping: The preprocessor symbol for the compute mapping +# feature +# mode_read: 1 if the feature is readable, 0 if not +# mode_write: 1 if the feature is writable, 0 if not +# sysctl: Preprocessor name of the sysctl file +# offset: Offset (in longs) within the sysctl file +# magnitude: Magnitude of this feature +# +# %mapping contains the feature names. It is generated while parsing +# lib/chips.c and is later used to display the mappings. -use vars qw(%features %sysctls %feature_name); +use vars qw(%prefix %arrayname %sysctl %features %mapping); -# Scan chips.h to get all prefix values -# Store the results in features{...}->prefix -# $_[0]: Name of chips.h file +# Scan chips.h to get all prefixes values. +# Store the results in %prefix. +# $_[0]: Path to chips.h sub scan_chips_h { - my ($chips_h) = @_; - my ($def,$val); - open INPUTFILE,$chips_h or die "Can't open $chips_h!"; - while () { - $features{$def}->{prefix} = $val - if ($def,$val) = /^\s*#\s*define\s+(SENSORS_\w+_PREFIX)\s+"(\S+)"/; - - } - close INPUTFILE; + my $chips_h = shift; + + open INPUTFILE, $chips_h + or die "Can't open $chips_h"; + + while () + { + if (m/^\s*#\s*define\s+(SENSORS_\w+_PREFIX)\s+"(\S+)"/) + { + die "Duplicate #define $1 in $chips_h" + if defined $prefix{$1}; + $prefix{$1} = $2; + } + } + + close INPUTFILE; } -# Scan chips.c to get the features names -# Store the results in features{...}->arrayname -# $_[0]: Name of the chips.c file +# Scan chips.c to get the feature array names. +# Store the results in %arrayname. +# $_[0]: Path to chips.c sub scan_chips_c_1 { - my ($chips_c) = @_; - my ($def,$val); - open INPUTFILE,$chips_c or die "Can't open $chips_c!"; - while ( !~ /^\s*sensors_chip_features/ ) {}; - while () { - last if /^\s*{\s*0/; - $features{$def}->{arrayname} = $val - if ($def,$val) = /^\s*{\s*(SENSORS_\w+_PREFIX)\s*,\s*(\w+)\s*}\s*,\s*$/; - } - close INPUTFILE; + my $chips_c = shift; + open INPUTFILE, $chips_c + or die "Can't open $chips_c"; + + while ( !~ m/^\s*sensors_chip_features\s/) {} + while () + { + last if m/^\s*\{\s*0/; + if (m/^\s*\{\s*(SENSORS_\w+_PREFIX)\s*,\s*(\w+)\s*\}\s*,/) + { + die "Duplicate entry $1 in sensors_chip_features" + if defined $arrayname{$1}; + $arrayname{$1} = $2; + } + } + + close INPUTFILE; } -# $_[0]: line to tokenize -# Returns the list of tokens +# Returns a list of tokens. # Tokens are either '{', '}', ',', ';' or anything between them. Spaces on # each side of a token are removed; spaces inside a token are not. +# Used by scan_kernel_chip() and scan_chips_c_2(). +# $_[0]: Line to tokenize sub tokenize { - my ($line) = @_; - my (@res,$next); - @res = (); - $line =~ s/^\s*//; - while (length $line) { - if ($line =~ s/^\s*$//) { - last; - } elsif ($line =~ s/^{\s*//) { - push @res, '{'; - } elsif ($line =~ s/^}\s*//) { - push @res, '}'; - } elsif ($line =~ s/^;\s*//) { - push @res, ';'; - } elsif ($line =~ s/^,\s*//) { - push @res, ','; - } else { - ($next) = $line =~ /^(\S+?)(?=[{},;\s])\s*/; - $line =~ s/^(\S+?)(?=[{},;\s])\s*//; - push @res, $next; - } - } - return @res; + my $line = shift; + + my @res = (); + $line =~ s/^\s*//; + + while (length $line) + { + last if $line =~ s/^\s*$//; + $line =~ s/^([{};,])\s*// + or $line =~ s/^([^{},;]+?)\s*([{},;\n])/$2/; + die "Parse error" + unless defined $1; + push @res, $1; + } + + return @res; } -# $_[0]: File to read from -# $_[1]: Feature to write to -sub scan_chip_c_entry +# Scan one chip file to get sysctl filenames. +# Store the results in %prefix. +# $_[0]: Kernel chip filename +sub scan_kernel_chip { - my ($file,$feature) = @_; - my ($line,@tokens,$next,$new); - for (;;) { - $line = <$file>; - @tokens = (@tokens,tokenize($line)); - last if $line =~ /;/; - } - die "Parse error: initial '{' expected for $feature->arrayname" - if shift @tokens ne '{'; - $feature->{features} = []; - while (shift @tokens eq '{') { - $new = {}; - $new->{number} = shift @tokens; - last if $new->{number} eq "0"; - die "Parse error: ',' expected for $feature->arrayname" - if shift @tokens ne ','; - $next = shift @tokens; - ($new->{name}) = $next =~ /^"(.*)"$/; - die "Parse error: ',' expected for $feature->arrayname" - if shift @tokens ne ','; - $new->{logical_mapping} = shift @tokens; - die "Parse error: ',' expected for $feature->arrayname" - if shift @tokens ne ','; - $new->{compute_mapping} = shift @tokens; - die "Parse error: ',' expected for $feature->arrayname" - if shift @tokens ne ','; - $next = shift @tokens; - $new->{mode_read} = ($next eq "SENSORS_MODE_R" or - $next eq "SENSORS_MODE_RW")?1:0; - $new->{mode_write} = ($next eq "SENSORS_MODE_W" or - $next eq "SENSORS_MODE_RW")?1:0; - die "Parse error: ',' expected for $feature->arrayname" - if shift @tokens ne ','; - $new->{sysctl} = shift @tokens; - die "Parse error: ',' expected for $feature->arrayname" - if shift @tokens ne ','; - $next = shift @tokens; - ($new->{offset}) = $next =~ /^VALUE\s*\((\d+)\s*\)/ - or die "Parse error: 'VALUE(..)' expected for $feature->arrayname"; - die "Parse error: ',' expected for $feature->arrayname" - if shift @tokens ne ','; - $new->{magnitude} = shift @tokens; - $next = shift @tokens; - $next = shift @tokens if $next eq ','; - push @{$feature->{features}},$new; - $feature_name{$new->{number}} = $new->{name}; - die "Parse error: '}' expected for $feature->arrayname" if $next ne '}'; - shift @tokens; - - } + my $chip = shift; + my $line; + + open INPUTFILE, $chip + or die "Can't open $chip"; + + while ($line = ) + { + next unless $line =~ m/^\s*static\s+ctl_table/; + my @tokens = tokenize($line); + + while ($line = ) + { + push @tokens, tokenize($line); + last if $line =~ m/;/; + } + + while (shift @tokens ne '{') { die unless @tokens } + for (;;) + { + while (shift @tokens ne '{') { die unless @tokens } + my $val = shift @tokens; + last if $val eq '0'; + die "Parse error: ',' expected after '$val' within $chip" + if shift @tokens ne ','; + my $name = shift @tokens; + die "Parse error: invalid name '$name' within $chip" + unless ($sysctl{$val}) = $name =~ m/^"(.+)"$/; + } + } + + close INPUTFILE; } -# Scan chips.c to get the features names -# Store the results in features{...}->feature -# $_[0]: Name of the chips.c file +# Scan chips.c to get the feature arrays. +# Store the results in %feature. +# $_[0]: Path to chips.c sub scan_chips_c_2 { - my ($chips_c) = @_; - my ($line,$name,$el,$feature); - open INPUTFILE,$chips_c or die "Can't open $chips_c!"; - while ($line = ) { - next if $line !~ /sensors_chip_feature /; - ($name) = $line =~ /sensors_chip_feature\s*(\w+)\s*\[/; - foreach $el (keys %features) { - $feature = $features{$el}; - last if $feature->{arrayname} eq $name - } - die "Can't find feature belonging to $name!" if (! defined $feature); - scan_chip_c_entry \*INPUTFILE,$feature; - } -} + my $chips_c = shift; + my $line; + + open INPUTFILE, $chips_c or + die "Can't open $chips_c"; + while () + { + next unless m/sensors_chip_feature\s+(\w+)\s*\[/; + my $name = $1; -# $_[0]: Name of the kernels module -sub scan_kernel_module -{ - my ($module_name) = @_; - my ($line,@tokens,$val,$name); - open INPUTFILE,$module_name or die "Can't open $module_name!"; - while ($line = ) { - next if $line !~ /^\s*static\s*ctl_table/; - @tokens = (); - for (;;) { - $line = ; - @tokens = (@tokens,tokenize($line)); - last if $line =~ /;/; - } - while (@tokens) { - for (;;) { - $val = shift @tokens; - last if $val ne '{'; - } - last if $val eq "0"; - die "Parse error: ',' expected within $module_name" - if shift @tokens ne ','; - $name = shift @tokens; - ($sysctls{$val}) = $name =~ /^"(\S+)"$/; - for (;;) { - last if shift @tokens eq '{'; - } - } - } - close INPUTFILE; + my @tokens = (); + while ($line = ) + { + push @tokens, tokenize($line); + last if $line =~ m/;/; + } + + $features{$name} = []; + die "Parse error: '{' expected for $name" + if shift @tokens ne '{'; + for(;;) + { + my $new = {}; + while (shift @tokens ne '{') { die unless @tokens } + $new->{number} = shift @tokens; + last if $new->{number} eq '0'; + die "Parse error: ',' expected for $name" + if shift @tokens ne ','; + ($new->{name}) = (shift @tokens) =~ m/^"(.*)"$/ + or die "Parse error: Bad name for $name"; + die "Parse error: ',' expected for $name" + if shift @tokens ne ','; + $new->{logical_mapping} = shift @tokens; + die "Parse error: ',' expected for $name" + if shift @tokens ne ','; + $new->{compute_mapping} = shift @tokens; + die "Parse error: ',' expected for $name" + if shift @tokens ne ','; + my $next = shift @tokens; + $new->{mode_read} = ($next =~ m/_RW?$/); + $new->{mode_write} = ($next =~ m/_R?W$/); + die "Parse error: ',' expected for $name" + if shift @tokens ne ','; + $new->{sysctl} = shift @tokens; + die "Parse error: ',' expected for $name" + if shift @tokens ne ','; + ($new->{offset}) = (shift @tokens) =~ m/^VALUE\s*\(\s*(\d+)\s*\)/ + or die "Parse error: Bad offset for $name"; + die "Parse error: ',' expected for $name" + if shift @tokens ne ','; + $new->{magnitude} = shift @tokens; + push @{$features{$name}}, $new; + $mapping{$new->{number}} = $new->{name}; + while (shift @tokens ne '}') { die unless @tokens } + die "Parse error: ',' expected after $name" + if shift @tokens ne ','; + } + } + + close INPUTFILE; } # $_[0]: Base directory sub scan_all { - my ($prefix) = @_; - my ($filename); - scan_chips_h $prefix . "/lib/chips.h"; - scan_chips_c_1 $prefix . "/lib/chips.c"; - scan_chips_c_2 $prefix . "/lib/chips.c"; - foreach $filename (glob ($prefix . "/kernel/chips/*.c")) { - scan_kernel_module "$filename" - } + my $basedir = shift; + + $basedir =~ s/\/*$//; + + # Generate %prefix + scan_chips_h("$basedir/lib/chips.h"); + # Generate %arrayname + scan_chips_c_1("$basedir/lib/chips.c"); + + # Check that %prefix and %arrayname have the same set of keys + foreach my $item (keys %prefix) + { + die "Missing array name for $item" + unless defined $arrayname{$item}; + } + foreach my $item (keys %arrayname) + { + die "Missing #define for $item" + unless defined $prefix{$item}; + } + + #Generate ?? + scan_chips_c_2("$basedir/lib/chips.c"); + # Generate %sysctl + foreach my $filename (glob "$basedir/kernel/chips/*.c") + { + scan_kernel_chip($filename); + } } -# $_[0]: index in master hash +# $_[0]: Prefix sub output_data { - my ($index) = @_; - my ($feature,$data,$read_write); - $feature = $features{$index}; - printf "Chip `%s'\n",$feature->{prefix}; - print " LABEL LABEL CLASS COMPUTE CLASS ACCESS ". - "MAGNITUDE\n"; - foreach $data (@{$feature->{features}}) { - $read_write = $data->{mode_read} + 2 * $data->{mode_write}; - if ($read_write == 0) { - $read_write = "--"; - } elsif ($read_write == 1) { - $read_write = "R "; - } elsif ($read_write == 2) { - $read_write = " W"; - } else { - $read_write = "RW"; - } - printf "%18s %18s %18s %2s %2s\n", - $data->{name}, $feature_name{$data->{logical_mapping}}, - $feature_name{$data->{compute_mapping}}, - $read_write,$data->{magnitude}; - } - print "\n"; - print " LABEL FEATURE SYMBOL ". - " SYSCTL FILE:NR\n"; - foreach $data (@{$feature->{features}}) { - printf "%18s %39s %18s:%1d\n",$data->{name}, $data->{number}, - $sysctls{$data->{sysctl}}, $data->{offset}, $data->{magnitude}; - } + my $index = shift; + + printf "Chip '\%s'\n", $prefix{$index}; + print "\n"; + + printf "\%-21s \%-21s \%-21s \%5s \%5s\n", 'LABEL', 'LABEL CLASS', + 'COMPUTE CLASS', 'MODE', 'MAGN'; + foreach my $data (@{$features{$arrayname{$index}}}) + { + print STDERR "Missing logical mapping for $data->{logical_mapping} (index = $index, name = $data->{name})\n" + unless defined $mapping{$data->{logical_mapping}}; + print STDERR "Missing compute mapping for $data->{compute_mapping} (index = $index, name = $data->{name})\n" + unless defined $mapping{$data->{compute_mapping}}; + + printf "\%-21s \%-21s \%-21s \%4s \%2d\n", $data->{name}, + $mapping{$data->{logical_mapping}} || '?', + $mapping{$data->{compute_mapping}} || '?', + ($data->{mode_read}?'R':'-').($data->{mode_write}?'W':'-'), + $data->{magnitude}; + } + print "\n"; + + printf "\%-21s \%-38s \%16s:\%1s\n", 'LABEL', 'FEATURE SYMBOL', + 'SYSCTL FILE', 'N'; + foreach my $data (@{$features{$arrayname{$index}}}) + { + printf "\%-21s \%-38s \%16s:\%1d\n",$data->{name}, $data->{number}, + $sysctl{$data->{sysctl}}, $data->{offset}; + } + print "\n", "\n"; } -sub initialize +if (@ARGV < 1 or $ARGV[0] =~ m/^-/) { - $feature_name{SENSORS_NO_MAPPING} = "NONE"; + print "Usage: $0 PATH [PREFIXES ...]\n", + " PATH is the path to the base directory of the lm_sensors tree.\n", + " PREFIXES are the chips that have to be output. If none are\n", + " specified, all chips are printed.\n"; + exit 0; } -sub help_message +%prefix = %arrayname = %sysctl = %features = (); +%mapping = (SENSORS_NO_MAPPING => '-'); + +scan_all(shift @ARGV); + +print "Chip Features\n", "-------------\n", "\n"; + +if (@ARGV) { - print STDERR "Syntax: doc-features PATH PREFIXES...\n"; - print STDERR "PATH is the path to the base of the lm_sensors tree.\n"; - print STDERR "PREFIXES are the chips that have to be output. If none ". - "are specified,\n". - "all chips are printed.\n"; + my %rprefix = reverse %prefix; + foreach my $prefix (@ARGV) + { + output_data($rprefix{$prefix}); + } } - -# @_: @ARGV -# Returns ($base_dir,@modules) -sub scan_arguments +else { - my ($base_dir,@chips); - if (@_ < 1 or @_[0] =~ /^-/) { - help_message; - exit 0; - } - $base_dir = @_[0]; - if (@_ == 1) { - @chips = (); - } else { - @chips = @_; - splice(@chips,0,1); - } - return ($base_dir,@chips) + foreach my $item (sort keys %prefix) + { + output_data($item); + } } - -sub main { - my ($el,$base_dir,@prefixes,$prefix); - initialize; - my ($base_dir,@prefixes) = scan_arguments @_; - scan_all $base_dir; - print "Chip Features\n"; - print "-------------\n\n"; - if (@prefixes) { - foreach $prefix (@prefixes) { - foreach $el (keys %features) { - output_data ($el), print "\n\n" - if ($features{$el}->{prefix} eq $prefix); - } - } - } else { - foreach $el (keys %features) { - output_data $el; - print "\n\n"; - } - } -} - -main @ARGV;