Made all gawk use the same structure syntax, a hanging {, for END, BEGIN, if, else, for

This will help avoid future difficulties in reading the code.

also converted all gawk one liners to normally expanded logic, one statement or case per line
This commit is contained in:
inxi-svn 2009-01-26 22:19:33 +00:00
parent b1503df5ae
commit e8a2c34658

166
inxi
View file

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
######################################################################## ########################################################################
#### Script Name: inxi #### Script Name: inxi
#### version: 0.9.6-b1-t1 #### version: 0.9.7-b1-t1
#### Date: 23 January 2009 #### Date: 26 January 2009
######################################################################## ########################################################################
#### SPECIAL THANKS #### SPECIAL THANKS
######################################################################## ########################################################################
@ -68,6 +68,9 @@
#### For all boolean tests, use 'true' / 'false'. Do NOT use 0 or 1 unless #### 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. #### 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: #### VARIABLE/FUNCTION NAMING:
#### All variables should explain what they are, except counters like i, j #### All variables should explain what they are, except counters like i, j
#### All variables MUST be initialized / declared explicitly #### All variables MUST be initialized / declared explicitly
@ -180,8 +183,8 @@ B_MODULES_DIR='false' #
B_PARTITIONS_DIR='false' # B_PARTITIONS_DIR='false' #
### Directory's used when present ### Directory's used when present
# DIR_CPUINFO='/proc/cpuinfo' DIR_CPUINFO='/proc/cpuinfo'
DIR_CPUINFO="$HOME/bin/scripts/inxi/svn/modules/data/xeon_2x_2" # DIR_CPUINFO="$HOME/bin/scripts/inxi/svn/modules/data/xeon_2x_2"
DIR_MEMINFO='/proc/meminfo' DIR_MEMINFO='/proc/meminfo'
DIR_ASOUND_DEVICE='/proc/asound/cards' DIR_ASOUND_DEVICE='/proc/asound/cards'
DIR_ASOUND_VERSION='/proc/asound/version' DIR_ASOUND_VERSION='/proc/asound/version'
@ -517,7 +520,10 @@ sanitize_characters()
# bash will interpret the |'s as usual and try to run a subshell! # bash will interpret the |'s as usual and try to run a subshell!
# Using weak quotes instead, or use '"..."' # Using weak quotes instead, or use '"..."'
echo "$2" | gawk " echo "$2" | gawk "
BEGIN { IGNORECASE=1 } { BEGIN {
IGNORECASE=1
}
{
gsub(/${!1}/,\"\") gsub(/${!1}/,\"\")
gsub(/ [ ]+/,\" \") ## ([ ]+) with (space) gsub(/ [ ]+/,\" \") ## ([ ]+) with (space)
gsub(/^ +| +$/,\"\") ## (pipe char) with (nothing) gsub(/^ +| +$/,\"\") ## (pipe char) with (nothing)
@ -744,7 +750,10 @@ remove_erroneous_chars()
## RS is input record separator ## RS is input record separator
## gsub is substitute; ## gsub is substitute;
gawk ' gawk '
BEGIN { RS="" } { BEGIN {
RS=""
}
{
gsub(/\n$/,"") ## (newline; end of string) with (nothing) gsub(/\n$/,"") ## (newline; end of string) with (nothing)
gsub(/\n/," "); ## (newline) with (space) gsub(/\n/," "); ## (newline) with (space)
gsub(/^ *| *$/, "") ## (pipe char) with (nothing) gsub(/^ *| *$/, "") ## (pipe char) with (nothing)
@ -1239,7 +1248,9 @@ get_audio_data()
device_count=$( echo "$lspci_data" | egrep -ic '(multimedia audio controller|audio device)' ) device_count=$( echo "$lspci_data" | egrep -ic '(multimedia audio controller|audio device)' )
if [[ $device_count -eq 1 ]] && [[ $B_ASOUND_CARDS == 'true' ]];then if [[ $device_count -eq 1 ]] && [[ $B_ASOUND_CARDS == 'true' ]];then
alsa_driver=$( gawk -F ']: ' ' alsa_driver=$( gawk -F ']: ' '
{ IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
# filtering out modems and usb devices like webcams, this might get a # filtering out modems and usb devices like webcams, this might get a
# usb audio card as well, this will take some trial and error # usb audio card as well, this will take some trial and error
$0 !~ /modem/ || $0 !~ /usb/ { $0 !~ /modem/ || $0 !~ /usb/ {
@ -1257,7 +1268,9 @@ get_audio_data()
# now we'll build the main audio data, card name, driver, and port. If no driver is found, # 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. # 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" ' A_AUDIO_DATA=( $( echo "$lspci_data" | gawk -F ': ' -v alsaDriver="$alsa_driver" '
BEGIN { IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
/multimedia audio controller|audio device/ { /multimedia audio controller|audio device/ {
audioCard=gensub(/^[0-9a-f:.]+ [^:]+: (.+)$/,"\\1","g",$0) audioCard=gensub(/^[0-9a-f:.]+ [^:]+: (.+)$/,"\\1","g",$0)
# The doublequotes are necessary because of the pipes in the variable. # The doublequotes are necessary because of the pipes in the variable.
@ -1327,7 +1340,9 @@ get_audio_data()
# in case of failure of first check do this instead # in case of failure of first check do this instead
if [[ ${#A_AUDIO_DATA[@]} -eq 0 ]] && [[ $B_ASOUND_CARDS == 'true' ]];then if [[ ${#A_AUDIO_DATA[@]} -eq 0 ]] && [[ $B_ASOUND_CARDS == 'true' ]];then
A_AUDIO_DATA=( $( gawk -F ']: ' ' A_AUDIO_DATA=( $( gawk -F ']: ' '
{ IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
$1 !~ /modem/ && $2 !~ /modem/ { $1 !~ /modem/ && $2 !~ /modem/ {
card=gensub( /^(.+)( - )(.+)$/, "\\3", 1, $2 ) card=gensub( /^(.+)( - )(.+)$/, "\\3", 1, $2 )
driver=gensub( /^(.+)( - )(.+)$/, "\\1", 1, $2 ) driver=gensub( /^(.+)( - )(.+)$/, "\\1", 1, $2 )
@ -1351,7 +1366,9 @@ get_audio_alsa_data()
# now we'll get the alsa data if the file exists # now we'll get the alsa data if the file exists
if [[ $B_ASOUND_VERSION == 'true' ]];then if [[ $B_ASOUND_VERSION == 'true' ]];then
alsa_data=$( gawk ' alsa_data=$( gawk '
{ IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
# some alsa strings have the build date in (...) # some alsa strings have the build date in (...)
# remove trailing . and remove possible second line if compiled by user # remove trailing . and remove possible second line if compiled by user
$0 !~ /compile/ { $0 !~ /compile/ {
@ -1378,7 +1395,6 @@ get_cpu_core_count()
# count the number of processors given # count the number of processors given
local cpu_core_count=${A_CPU_TYPE_PCNT_CCNT[2]} local cpu_core_count=${A_CPU_TYPE_PCNT_CCNT[2]}
local cpu_type='' local cpu_type=''
echo A_CPU_TYPE_PCNT_CCNT ${A_CPU_TYPE_PCNT_CCNT[2]}
if [[ ${A_CPU_TYPE_PCNT_CCNT[0]} == "HT" || ${A_CPU_TYPE_PCNT_CCNT[0]} == "SMP" ]]; then if [[ ${A_CPU_TYPE_PCNT_CCNT[0]} == "HT" || ${A_CPU_TYPE_PCNT_CCNT[0]} == "SMP" ]]; then
# note the use of the space, this avoids a double space if this is null in the output # note the use of the space, this avoids a double space if this is null in the output
@ -1412,11 +1428,15 @@ get_cpu_data()
if [[ $B_CPUINFO == 'true' ]];then if [[ $B_CPUINFO == 'true' ]];then
IFS=$'\n' IFS=$'\n'
A_CPU_DATA=( $( gawk -F': ' ' A_CPU_DATA=( $( gawk -F': ' '
{ IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
# TAKE STRONGER NOTE: \t+ does NOT always work, MUST be [ \t]+ # 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! # 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! # Therefore PATCH to use [ \t]+ when TESTING!
/^processor[ \t]+:/ { nr = $NF } /^processor[ \t]+:/ {
nr = $NF
}
/^model name|^cpu\t+:/ { /^model name|^cpu\t+:/ {
gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF ) gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF )
@ -1445,11 +1465,17 @@ get_cpu_data()
cpu[nr, "speed"] = $NF 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/ { /vendor_id/ {
gsub(/genuine|authentic/,"",$NF) gsub(/genuine|authentic/,"",$NF)
@ -1488,7 +1514,7 @@ get_cpu_ht_multicore_smp_data()
# if = 1 processor then single core/processor Uni-Processor (UP) # if = 1 processor then single core/processor Uni-Processor (UP)
if [[ $B_CPUINFO == 'true' ]]; then if [[ $B_CPUINFO == 'true' ]]; then
# A_CPU_TYPE_PCNT_CCNT=( $( A_CPU_TYPE_PCNT_CCNT=( $(
gawk ' gawk '
BEGIN { BEGIN {
FS=": " FS=": "
@ -1520,15 +1546,12 @@ get_cpu_ht_multicore_smp_data()
} }
END { END {
# look for the largest id number, and assign it # look for the largest id number, and assign it
for ( j = 0; j < num_of_processors; j++ ) for ( j = 0; j < num_of_processors; j++ ) {
{ if ( physical_id[j] > cpu_temp ) {
if ( physical_id[j] > cpu_temp )
{
cpu_temp = physical_id[j] cpu_temp = physical_id[j]
# print "physical_id: " physical_id[j] # print "physical_id: " physical_id[j]
} }
if ( core_id[j] > core_temp ) if ( core_id[j] > core_temp ) {
{
core_temp = core_id[j] core_temp = core_id[j]
# print "core_temp: " core_id[j] # print "core_temp: " core_id[j]
} }
@ -1540,35 +1563,27 @@ get_cpu_ht_multicore_smp_data()
# print "physical_cpu_count: " physical_cpu_count # print "physical_cpu_count: " physical_cpu_count
# looking at logical processor counts over 1, which means either HT, SMP or MCP # looking at logical processor counts over 1, which means either HT, SMP or MCP
if ( num_of_processors > 1 ) if ( num_of_processors > 1 ) {
{ if ( physical_cpu_count == 1 ) {
if ( physical_cpu_count == 1 ) if ( physical_cpu_count == core_count ) {
{
if ( physical_cpu_count == core_count )
{
type = "HT" # this is more than likely a P4 w/HT or an Atom 270 type = "HT" # this is more than likely a P4 w/HT or an Atom 270
} }
else else {
{ if ( core_count == num_of_cores && core_count == num_of_processors) {
if ( core_count == num_of_cores && core_count == num_of_processors)
{
type = "MCP" type = "MCP"
cores = core_count cores = core_count
} }
else else {
{
type = "HT" # this is i7 or Atom 330 type = "HT" # this is i7 or Atom 330
cores = core_count cores = core_count
} }
} }
} }
else else {
{
type = "SMP" type = "SMP"
processors = physical_cpu_count processors = physical_cpu_count
if ( num_of_cores > 1 ) if ( num_of_cores > 1 ) {
{
type = "SMPMC" # processors could be both MCP and SMP type = "SMPMC" # processors could be both MCP and SMP
cores = core_count cores = core_count
} }
@ -1577,7 +1592,7 @@ get_cpu_ht_multicore_smp_data()
print type " " processors " " cores print type " " processors " " cores
} }
' $DIR_CPUINFO ' $DIR_CPUINFO
# ) ) ) )
fi fi
# echo A_CPU_TYPE_PCNT_CCNT:1 ${A_CPU_TYPE_PCNT_CCNT[@]} # echo A_CPU_TYPE_PCNT_CCNT:1 ${A_CPU_TYPE_PCNT_CCNT[@]}
} }
@ -1640,7 +1655,10 @@ get_distro_data()
distro=$( get_distro_lsb_data ) distro=$( get_distro_lsb_data )
else else
distro=$( gawk ' distro=$( gawk '
BEGIN { RS="" } { BEGIN {
RS=""
}
{
gsub(/\\[a-z]/, "") gsub(/\\[a-z]/, "")
gsub(/,/, " ") gsub(/,/, " ")
gsub(/^ +| +$/, "") gsub(/^ +| +$/, "")
@ -1680,8 +1698,9 @@ get_distro_lsb_data()
if [[ $B_LSB_DIR == 'true' ]] && [[ $1 != 'app' ]];then if [[ $B_LSB_DIR == 'true' ]] && [[ $1 != 'app' ]];then
distro=$( gawk -F '=' ' distro=$( gawk -F '=' '
BEGIN {
{ IGNORECASE=1 } IGNORECASE=1
}
# note: adding the spacing directly to variable to make sure distro output is null if not found # note: adding the spacing directly to variable to make sure distro output is null if not found
/^DISTRIB_ID/ { /^DISTRIB_ID/ {
gsub(/^ +| +$/, "", $NF) gsub(/^ +| +$/, "", $NF)
@ -1740,7 +1759,9 @@ get_graphics_card_data()
IFS=$'\n' IFS=$'\n'
A_GFX_CARD_DATA=( $( echo "$lspci_data" | gawk -F': ' ' A_GFX_CARD_DATA=( $( echo "$lspci_data" | gawk -F': ' '
{ IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
/vga compatible controller/ { /vga compatible controller/ {
gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF) gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF)
gsub(/,/, " ", $NF) gsub(/,/, " ", $NF)
@ -1792,8 +1813,12 @@ get_graphics_glx_data()
} }
$2 && a[$2] $2 && a[$2]
} }
/opengl version/ && (f || $2 !~ /mesa/) { $2 && b[$2] } /opengl version/ && (f || $2 !~ /mesa/) {
/direct rendering/ { $2 && c[$2] } $2 && b[$2]
}
/direct rendering/ {
$2 && c[$2]
}
END { END {
printf("%s\n%s\n%s\n", join(a,", "), join(b,", "), join(c,", ")) printf("%s\n%s\n%s\n", join(a,", "), join(b,", "), join(c,", "))
@ -1817,14 +1842,19 @@ get_graphics_res_data()
/\*/ { 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 { END {
for (n in res) { for (n in res) {
if (res[n] ~ /^[[:digit:]]+x[[:digit:]]+/) if (res[n] ~ /^[[:digit:]]+x[[:digit:]]+/) {
line = line ? line ", " res[n] : res[n] line = line ? line ", " res[n] : res[n]
}
} }
if (line) if (line) {
print(line) print(line)
}
}' ) }' )
if [[ -z $screen_resolution ]];then if [[ -z $screen_resolution ]];then
screen_resolution=$( xdpyinfo | gawk '/dimensions/ { print $2 }' ) screen_resolution=$( xdpyinfo | gawk '
/dimensions/ {
print $2
}' )
fi fi
else else
screen_resolution=$( stty -F $( readlink /proc/$PPID/fd/0 ) size | gawk '{ print $2"x"$1 }' ) screen_resolution=$( stty -F $( readlink /proc/$PPID/fd/0 ) size | gawk '{ print $2"x"$1 }' )
@ -1839,7 +1869,10 @@ get_graphics_agp_data()
if [[ B_MODULES_DIR == 'true' ]];then if [[ B_MODULES_DIR == 'true' ]];then
## not used currently ## 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 fi
} }
@ -1998,7 +2031,9 @@ get_hard_drive_data_advanced()
IFS=$'\n' IFS=$'\n'
if [[ $B_SCSI_DIR == 'true' ]]; then if [[ $B_SCSI_DIR == 'true' ]]; then
a_temp_scsi=( $( gawk ' a_temp_scsi=( $( gawk '
BEGIN { IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
/host/ { /host/ {
getline a[$0] getline a[$0]
getline b[$0] getline b[$0]
@ -2087,8 +2122,12 @@ get_memory_data()
local memory='' local memory=''
memory=$( gawk ' memory=$( gawk '
/^MemTotal:/ { tot = $2 } /^MemTotal:/ {
/^(MemFree|Buffers|Cached):/ { notused+=$2 } tot = $2
}
/^(MemFree|Buffers|Cached):/ {
notused+=$2
}
END { END {
used = tot-notused used = tot-notused
printf("%.1f/%.1fMB\n", used/1024, tot/1024) printf("%.1f/%.1fMB\n", used/1024, tot/1024)
@ -2104,7 +2143,9 @@ get_module_version_number()
if [[ -n $( which modinfo ) ]];then if [[ -n $( which modinfo ) ]];then
module_version=$( modinfo $1 | gawk ' module_version=$( modinfo $1 | gawk '
BEGIN { IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
/^version/ { /^version/ {
gsub(/,/, " ", $2) gsub(/,/, " ", $2)
gsub(/^ +| +$/, "", $2) gsub(/^ +| +$/, "", $2)
@ -2122,7 +2163,9 @@ get_networking_data()
{ {
IFS=$'\n' IFS=$'\n'
A_NETWORK_DATA=( $( echo "$lspci_data" | gawk ' 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).*$/ { /^[0-9a-f:.]+ (ethernet|network) (controller|bridge)/ || /^[0-9a-f:.]+ [^:]+: .*(ethernet|network).*$/ {
nic=gensub(/^[0-9a-f:.]+ [^:]+: (.+)$/,"\\1","g",$0) nic=gensub(/^[0-9a-f:.]+ [^:]+: (.+)$/,"\\1","g",$0)
gsub(/realtek semiconductor/, "Realtek", nic) gsub(/realtek semiconductor/, "Realtek", nic)
@ -2207,7 +2250,9 @@ get_networking_local_ip_data()
if [[ $B_IFCONFIG == 'true' ]];then if [[ $B_IFCONFIG == 'true' ]];then
IFS=$'\n' IFS=$'\n'
A_INTERFACES_DATA=( $( $DIR_IFCONFIG | gawk ' A_INTERFACES_DATA=( $( $DIR_IFCONFIG | gawk '
BEGIN { IGNORECASE=1 } BEGIN {
IGNORECASE=1
}
$0 !~ /^lo/ { $0 !~ /^lo/ {
# not clear on why inet is coming through, but this gets rid of it # not clear on why inet is coming through, but this gets rid of it
# as first line item. # as first line item.
@ -2256,7 +2301,9 @@ get_partition_data()
## note: by subtracting from the last field number NF, we avoid a subtle issue with LVM df output, where if ## 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 ## 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 ' 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/ { /\/$|\/boot$|\/var$|\/home$|\/tmp$|\/usr$/ && ! /aufs/ {
print $NF "," $(NF - 4) "," $(NF - 3) "," $(NF - 1) ",main" print $NF "," $(NF - 4) "," $(NF - 3) "," $(NF - 1) ",main"
} }
@ -2308,7 +2355,10 @@ calculate_multicore_data()
fi fi
# handle weird error cases where it's not a number # handle weird error cases where it's not a number
if [[ -n $( egrep '^[0-9\.,]+$' <<< $string_number ) ]];then 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 elif [[ $string_number == '' ]];then
string_number='Not Available' string_number='Not Available'
else else