(Change version number)

Fixed syntax of gawk, made it all consistent, one method for everything, hanging { now in gawk for all structures and flow controls

Fixed small double space thing for kernel output also.

Removed all one liners from gawk, like BEGIN { IGNORECASE = 1 }, and now use NO one liner flow controls at all. This is the new 
convention, neither trash nor I were comfortable enough with gawk to really check the actual programming syntax, but now it's kind of 
obvious that the same rules apply: all one liner flow controls hide logic, and give bugs somewhere to live. Same as any other language. 
What a surprise.
This commit is contained in:
inxi-svn 2009-01-26 22:44:52 +00:00
parent 959caae1f7
commit 5a35c5fb12

219
inxi
View file

@ -1,8 +1,8 @@
#!/bin/bash
########################################################################
#### Script Name: inxi
#### version: 0.9.6
#### Date: 24 January 2009
#### version: 0.9.7
#### Date: 26 January 2009
########################################################################
#### SPECIAL THANKS
########################################################################
@ -68,6 +68,9 @@
#### For all boolean tests, use 'true' / 'false'. Do NOT use 0 or 1 unless
#### it's a function return. Avoid complicated tests in the if condition itself.
####
#### For gawk: use always if ( num_of_cores > 1 ) { hanging { starter for all blocks
#### This lets us use one method for all gawk structures, including BEGIN/END, if, for, etc
####
#### VARIABLE/FUNCTION NAMING:
#### All variables should explain what they are, except counters like i, j
#### All variables MUST be initialized / declared explicitly
@ -516,7 +519,10 @@ sanitize_characters()
# bash will interpret the |'s as usual and try to run a subshell!
# Using weak quotes instead, or use '"..."'
echo "$2" | gawk "
BEGIN { IGNORECASE=1 } {
BEGIN {
IGNORECASE=1
}
{
gsub(/${!1}/,\"\")
gsub(/ [ ]+/,\" \") ## ([ ]+) with (space)
gsub(/^ +| +$/,\"\") ## (pipe char) with (nothing)
@ -743,7 +749,10 @@ remove_erroneous_chars()
## RS is input record separator
## gsub is substitute;
gawk '
BEGIN { RS="" } {
BEGIN {
RS=""
}
{
gsub(/\n$/,"") ## (newline; end of string) with (nothing)
gsub(/\n/," "); ## (newline) with (space)
gsub(/^ *| *$/, "") ## (pipe char) with (nothing)
@ -1238,7 +1247,9 @@ get_audio_data()
device_count=$( echo "$lspci_data" | egrep -ic '(multimedia audio controller|audio device)' )
if [[ $device_count -eq 1 ]] && [[ $B_ASOUND_CARDS == 'true' ]];then
alsa_driver=$( gawk -F ']: ' '
{ IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
# filtering out modems and usb devices like webcams, this might get a
# usb audio card as well, this will take some trial and error
$0 !~ /modem/ || $0 !~ /usb/ {
@ -1256,7 +1267,9 @@ get_audio_data()
# now we'll build the main audio data, card name, driver, and port. If no driver is found,
# and if the first method above is not null, and one card is found, it will use that instead.
A_AUDIO_DATA=( $( echo "$lspci_data" | gawk -F ': ' -v alsaDriver="$alsa_driver" '
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/multimedia audio controller|audio device/ {
audioCard=gensub(/^[0-9a-f:.]+ [^:]+: (.+)$/,"\\1","g",$0)
# The doublequotes are necessary because of the pipes in the variable.
@ -1326,7 +1339,9 @@ get_audio_data()
# in case of failure of first check do this instead
if [[ ${#A_AUDIO_DATA[@]} -eq 0 ]] && [[ $B_ASOUND_CARDS == 'true' ]];then
A_AUDIO_DATA=( $( gawk -F ']: ' '
{ IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
$1 !~ /modem/ && $2 !~ /modem/ {
card=gensub( /^(.+)( - )(.+)$/, "\\3", 1, $2 )
driver=gensub( /^(.+)( - )(.+)$/, "\\1", 1, $2 )
@ -1350,7 +1365,9 @@ get_audio_alsa_data()
# now we'll get the alsa data if the file exists
if [[ $B_ASOUND_VERSION == 'true' ]];then
alsa_data=$( gawk '
{ IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
# some alsa strings have the build date in (...)
# remove trailing . and remove possible second line if compiled by user
$0 !~ /compile/ {
@ -1409,11 +1426,16 @@ get_cpu_data()
if [[ $B_CPUINFO == 'true' ]];then
IFS=$'\n'
A_CPU_DATA=($(gawk -F': ' '
{ IGNORECASE=1 }
A_CPU_DATA=( $( gawk -F': ' '
BEGIN {
IGNORECASE=1
}
# TAKE STRONGER NOTE: \t+ does NOT always work, MUST be [ \t]+
# TAKE NOTE: \t+ will work for $DIR_CPUINFO, but SOME ARBITRARY FILE used for TESTING might contain SPACES!
# Therefore PATCH to use [ \t]+ when TESTING!
/^processor\t+:/ { nr = $NF }
/^processor[ \t]+:/ {
nr = $NF
}
/^model name|^cpu\t+:/ {
gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF )
@ -1442,11 +1464,17 @@ get_cpu_data()
cpu[nr, "speed"] = $NF
}
/^cache size/ { cpu[nr, "cache"] = $NF }
/^cache size/ {
cpu[nr, "cache"] = $NF
}
/^flags/ { cpu[nr, "flags"] = $NF }
/^flags/ {
cpu[nr, "flags"] = $NF
}
/^bogomips/ { cpu[nr, "bogomips"] = $NF }
/^bogomips/ {
cpu[nr, "bogomips"] = $NF
}
/vendor_id/ {
gsub(/genuine|authentic/,"",$NF)
@ -1468,10 +1496,11 @@ get_cpu_data()
else {
printf("%s %s\n", max, "Mhz")
}
}' $DIR_CPUINFO ))
}' $DIR_CPUINFO ) )
fi
IFS="$ORIGINAL_IFS"
# echo getMainCpu: ${[@]}
}
## this is for counting processors and finding HT types
@ -1485,27 +1514,40 @@ get_cpu_ht_multicore_smp_data()
if [[ $B_CPUINFO == 'true' ]]; then
A_CPU_TYPE_PCNT_CCNT=( $( gawk '
BEGIN { FS=": "; i = 0 } {IGNORECASE = 1}
/^processor/ { num_of_processors = $NF + 1 } # counts logical processors, both HT and physical
/^cpu cores/ { num_of_cores = $NF } # counts physical cores
/^physical/ { physical_id[i] = $NF } # array of physical cpus ids
/^core id/ { core_id[i] = $NF; i++ } # array of core ids
{
BEGIN {
FS=": "
IGNORECASE = 1
i = 0
processors = 1
cores = 1 # single cores are obviously a Uni-processor
type = "UP"
cpu_temp = 0
core_temp = 0
}
# counts logical processors, both HT and physical
/^processor/ {
num_of_processors = $NF + 1
}
# counts physical cores
/^cpu cores/ {
num_of_cores = $NF
}
# array of physical cpus ids
/^physical/ {
physical_id[i] = $NF
}
# array of core ids
/^core id/ {
core_id[i] = $NF
i++
}
END {
# look for the largest id number, and assign it
for ( j = 0; j <= num_of_processors; j++)
{
if ( physical[j] > cpu_temp )
{
cpu_temp = physical[j]
for ( j = 0; j < num_of_processors; j++ ) {
if ( physical_id[j] > cpu_temp ) {
cpu_temp = physical_id[j]
}
if ( core_id[j] > core_temp )
{
if ( core_id[j] > core_temp ) {
core_temp = core_id[j]
}
}
@ -1514,44 +1556,37 @@ get_cpu_ht_multicore_smp_data()
core_count = core_temp + 1
# looking at logical processor counts over 1, which means either HT, SMP or MCP
if ( num_of_processors > 1 )
{
if ( physical_cpu_count == 1 )
{
if ( physical_cpu_count == core_count )
{
if ( num_of_processors > 1 ) {
if ( physical_cpu_count == 1 ) {
if ( physical_cpu_count == core_count ) {
type = "HT" # this is more than likely a P4 w/HT or an Atom 270
}
else
{
if ( core_count == num_of_cores && core_count == num_of_processors)
{
else {
if ( core_count == num_of_cores && core_count == num_of_processors) {
type = "MCP"
cores = core_count
}
else
{
else {
type = "HT" # this is i7 or Atom 330
cores = core_count
}
}
}
else
{
else {
type = "SMP"
processors = physical_cpu_count
if ( num_of_cores > 1 )
{
if ( num_of_cores > 1 ) {
type = "SMPMC" # processors could be both MCP and SMP
cores = core_count
}
}
}
print type " " processors " " cores
}
END { print type " " processors " " cores }
' $DIR_CPUINFO ) )
fi
# echo A_CPU_TYPE_PCNT_CCNT:1 ${A_CPU_TYPE_PCNT_CCNT[@]}
}
# for more on distro id, please reference this python thread: http://bugs.python.org/issue1322
@ -1612,7 +1647,10 @@ get_distro_data()
distro=$( get_distro_lsb_data )
else
distro=$( gawk '
BEGIN { RS="" } {
BEGIN {
RS=""
}
{
gsub(/\\[a-z]/, "")
gsub(/,/, " ")
gsub(/^ +| +$/, "")
@ -1652,8 +1690,9 @@ get_distro_lsb_data()
if [[ $B_LSB_DIR == 'true' ]] && [[ $1 != 'app' ]];then
distro=$( gawk -F '=' '
{ IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
# note: adding the spacing directly to variable to make sure distro output is null if not found
/^DISTRIB_ID/ {
gsub(/^ +| +$/, "", $NF)
@ -1712,7 +1751,9 @@ get_graphics_card_data()
IFS=$'\n'
A_GFX_CARD_DATA=( $( echo "$lspci_data" | gawk -F': ' '
{ IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/vga compatible controller/ {
gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF)
gsub(/,/, " ", $NF)
@ -1740,6 +1781,7 @@ get_graphics_glx_data()
if [[ $B_X_RUNNING == 'true' ]];then
IFS=$'\n'
A_GLX_DATA=( $( glxinfo | gawk -F ': ' '
# note: function declarations go before BEGIN? It appears so, confirm.
function join(arr, sep) {
s=""
i=flag=0
@ -1752,7 +1794,9 @@ get_graphics_glx_data()
return s
}
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/opengl renderer/ {
if ($2 ~ /mesa/) {
# Allow r300 et al, but not the rest
@ -1764,8 +1808,12 @@ get_graphics_glx_data()
}
$2 && a[$2]
}
/opengl version/ && (f || $2 !~ /mesa/) { $2 && b[$2] }
/direct rendering/ { $2 && c[$2] }
/opengl version/ && (f || $2 !~ /mesa/) {
$2 && b[$2]
}
/direct rendering/ {
$2 && c[$2]
}
END {
printf("%s\n%s\n%s\n", join(a,", "), join(b,", "), join(c,", "))
@ -1786,17 +1834,24 @@ get_graphics_res_data()
# Added the two ?'s , because the resolution is now reported without spaces around the 'x', as in
# 1400x1050 instead of 1400 x 1050. Change as of X.org version 1.3.0
screen_resolution=$( xrandr | gawk '
/\*/ { res[++m] = gensub(/^.* ([0-9]+) ?x ?([0-9]+)[_ ].* ([0-9\.]+)\*.*$/,"\\1x\\2@\\3hz","g",$0) }
/\*/ {
res[++m] = gensub(/^.* ([0-9]+) ?x ?([0-9]+)[_ ].* ([0-9\.]+)\*.*$/,"\\1x\\2@\\3hz","g",$0)
}
END {
for (n in res) {
if (res[n] ~ /^[[:digit:]]+x[[:digit:]]+/)
if (res[n] ~ /^[[:digit:]]+x[[:digit:]]+/) {
line = line ? line ", " res[n] : res[n]
}
}
if (line)
if (line) {
print(line)
}
}' )
if [[ -z $screen_resolution ]];then
screen_resolution=$( xdpyinfo | gawk '/dimensions/ { print $2 }' )
screen_resolution=$( xdpyinfo | gawk '
/dimensions/ {
print $2
}' )
fi
else
screen_resolution=$( stty -F $( readlink /proc/$PPID/fd/0 ) size | gawk '{ print $2"x"$1 }' )
@ -1811,7 +1866,10 @@ get_graphics_agp_data()
if [[ B_MODULES_DIR == 'true' ]];then
## not used currently
agp_module=$( gawk '/agp/ && !/agpgart/ && $3 > 0 { print(gensub(/(.*)_agp.*/,"\\1","g",$1)) }' $DIR_MODULES )
agp_module=$( gawk '
/agp/ && !/agpgart/ && $3 > 0 {
print(gensub(/(.*)_agp.*/,"\\1","g",$1))
}' $DIR_MODULES )
fi
}
@ -1823,7 +1881,9 @@ get_graphics_x_data()
if [[ $B_X_RUNNING == 'true' ]];then
# X vendor and version detection.
x_vendor=$( xdpyinfo | gawk -F': +' '
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/vendor string/ {
gsub(/the|inc|foundation|project|corporation/, "", $2)
gsub(/,/, " ", $2)
@ -1838,7 +1898,9 @@ get_graphics_x_data()
x_version=$( xdpyinfo | gawk '/version:/ { print $NF }' )
if [[ -z $x_version ]];then
x_version=$(xdpyinfo | gawk -F': +' '
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/vendor release number/ {
gsub(/0+$/, "", $2)
gsub(/0+/, ".", $2)
@ -1970,7 +2032,9 @@ get_hard_drive_data_advanced()
IFS=$'\n'
if [[ $B_SCSI_DIR == 'true' ]]; then
a_temp_scsi=( $( gawk '
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/host/ {
getline a[$0]
getline b[$0]
@ -2059,8 +2123,12 @@ get_memory_data()
local memory=''
memory=$( gawk '
/^MemTotal:/ { tot = $2 }
/^(MemFree|Buffers|Cached):/ { notused+=$2 }
/^MemTotal:/ {
tot = $2
}
/^(MemFree|Buffers|Cached):/ {
notused+=$2
}
END {
used = tot-notused
printf("%.1f/%.1fMB\n", used/1024, tot/1024)
@ -2076,7 +2144,9 @@ get_module_version_number()
if [[ -n $( which modinfo ) ]];then
module_version=$( modinfo $1 | gawk '
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/^version/ {
gsub(/,/, " ", $2)
gsub(/^ +| +$/, "", $2)
@ -2094,7 +2164,9 @@ get_networking_data()
{
IFS=$'\n'
A_NETWORK_DATA=( $( echo "$lspci_data" | gawk '
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/^[0-9a-f:.]+ (ethernet|network) (controller|bridge)/ || /^[0-9a-f:.]+ [^:]+: .*(ethernet|network).*$/ {
nic=gensub(/^[0-9a-f:.]+ [^:]+: (.+)$/,"\\1","g",$0)
gsub(/realtek semiconductor/, "Realtek", nic)
@ -2179,7 +2251,9 @@ get_networking_local_ip_data()
if [[ $B_IFCONFIG == 'true' ]];then
IFS=$'\n'
A_INTERFACES_DATA=( $( $DIR_IFCONFIG | gawk '
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
$0 !~ /^lo/ {
# not clear on why inet is coming through, but this gets rid of it
# as first line item.
@ -2228,7 +2302,9 @@ get_partition_data()
## note: by subtracting from the last field number NF, we avoid a subtle issue with LVM df output, where if
## the first field is too long, it will occupy its own line, this way we are getting only the needed data
A_PARTITION_DATA=( $( df -h -T --exclude-type=aufs --exclude-type=tmpfs --exclude-type=iso9660 | gawk '
BEGIN { IGNORECASE=1 }
BEGIN {
IGNORECASE=1
}
/\/$|\/boot$|\/var$|\/home$|\/tmp$|\/usr$/ && ! /aufs/ {
print $NF "," $(NF - 4) "," $(NF - 3) "," $(NF - 1) ",main"
}
@ -2280,7 +2356,10 @@ calculate_multicore_data()
fi
# handle weird error cases where it's not a number
if [[ -n $( egrep '^[0-9\.,]+$' <<< $string_number ) ]];then
string_number=$( echo $string_number $2 | gawk '{total = $1*$2; print total}' )
string_number=$( echo $string_number $2 | gawk '{
total = $1*$2
print total
}' )
elif [[ $string_number == '' ]];then
string_number='Not Available'
else
@ -2995,9 +3074,9 @@ print_system_data()
if [[ $B_SHOW_HOST == 'true' ]];then
system_data=$( create_print_line "System:" "${C1}Host${C2} $host_name ${C1}Kernel${C2}" )
else
system_data=$( create_print_line "System:" "${C1}Kernel${C2} ${CN}" )
system_data=$( create_print_line "System:" "${C1}Kernel${C2}" )
fi
system_data="$system_data ${C2} $current_kernel $bits ${C1}Distro${C2} $distro"
system_data="$system_data $current_kernel $bits ${C1}Distro${C2} $distro"
print_screen_output "$system_data"
}