Refactored, a few cosmetic changes.

This commit is contained in:
inxi-svn 2009-01-06 22:13:48 +00:00
parent 8e40abc247
commit de13eba739

573
inxi
View file

@ -1,8 +1,8 @@
#!/bin/bash
########################################################################
#### Script Name: inxi
#### version: 0.9.2
#### Date: January 5, 2009
#### version: 0.9.3
#### Date: January 6, 2009
########################################################################
#### SPECIAL THANKS
########################################################################
@ -302,13 +302,10 @@ fi
########################################################################
main()
{
# first two functions must be set first for colors etc. Remember, no debugger
# stuff works on these first two functions unless you set the debugging flag
# first init function must be set first for colors etc. Remember, no debugger
# stuff works on this function unless you set the debugging flag
# manually. Debugging flag -@ [number] will not work until get_parameters runs.
set_calculated_variables
# initialize some booleans
init
initialize_script_data
## this needs to run before the KONVI stuff is set below
get_start_client
@ -316,7 +313,6 @@ get_start_client
# Check for dependencies before running anything else except above functions
check_script_depends
check_script_suggested_apps
discover_ht_multicore_smp_cpu_data
# note: this only works if it's run from inside konversation as a script builtin or something
# only do this if inxi has been started as a konversation script, otherwise bypass this
@ -369,14 +365,46 @@ exit 0
}
#### -------------------------------------------------------------------
#### basic tests: init some boleans
#### basic tests: set script data, booleans, PATH
#### -------------------------------------------------------------------
# Set PATH data so we can access all programs as user. Set BAN lists.
# initialize some boleans, these directories are used throughout the script
# some apps are used for extended functions
# any directory used, should be checked here first
init()
# some apps are used for extended functions any directory used, should be
# checked here first.
initialize_script_data()
{
local path='' sys_path='' added_path='' b_path_found=''
# Extra path variable to make execute failures less likely, merged below
local extra_paths="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
# Fallback paths put into $extra_paths; This might, among others, help on gentoo.
# Now, create a difference of $PATH and $extra_paths and add that to $PATH:
IFS=":"
for path in $extra_paths
do
b_path_found='false'
for sys_path in $PATH
do
if [[ $path == $sys_path ]];then
b_path_found='true'
fi
done
if [[ $b_path_found == 'false' ]];then
added_path="$added_path:$path"
fi
done
IFS="$ORIGINAL_IFS"
PATH="${PATH}${added_path}"
##echo "PATH='$PATH'"
##/bin/sh -c 'echo "PATH in subshell=\"$PATH\""'
# Do this after sourcing of config overrides so user can customize banwords
BAN_LIST_NORMAL=$( make_ban_lists "${A_NORMAL_BANS[@]}" ) # Contrary to my previous belief, "${ARR[@]}" passes a quoted list, not one string
BAN_LIST_CPU=$( make_ban_lists "${A_CPU_BANS[@]}" )
##echo "BAN_LIST_NORMAL='$BAN_LIST_NORMAL'"
# now set the script BOOLEANS for files required to run features
if [[ -d "/proc/" ]];then
B_PROC='true'
else
@ -445,8 +473,6 @@ check_script_suggested_apps()
else
script_debugger "Suggestion: update to Bash v3.1 for optimal inxi output"
fi
}
# Determine if any of the absolutely necessary tools are absent
@ -479,12 +505,87 @@ check_script_depends()
done
}
## note: this is now running inside each gawk sequence directly to avoid exiting gawk
## looping in bash through arrays, then re-entering gawk to clean up, then writing back to array
## in bash. For now I'll leave this here because there's still some interesting stuff to get re methods
# Enforce boilerplate and buzzword filters
# args: $1 - BAN_LIST_NORMAL/BAN_LIST_CPU; $2 - string to sanitize
sanitize_characters()
{
# Cannot use strong quotes to unquote a string with pipes in it!
# 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 } {
gsub(/${!1}/,\"\")
gsub(/ [ ]+/,\" \") ## ([ ]+) with (space)
gsub(/^ +| +$/,\"\") ## (pipe char) with (nothing)
print ## prints (returns) cleaned input
}"
}
# Filter boilerplate & buzzwords.
# args: $1 - quoted: "$@" array of ban terms
make_ban_lists()
{
local ban_list=''
# Iterate over $@
## note: this is a weird, non-intuitive method, needs some documentation or rewriting
## if you declare ban_string it stops working, have to read up on this
for ban_string
do
# echo "term=\"$ban_string\"" # >&2
if [[ ${ban_string:0:1} = $'\2' ]];then
ban_list="${ban_list}${ban_list+|}${ban_string:1:${#ban_string}-1}"
else
# Automatically escapes [ ] ( ) . and +
ban_list="${ban_list}${ban_list+|}$( echo "$ban_string" | gawk '{
gsub(/([\[\]+().])/,"\\\\&")
print
}' )"
fi
done
echo "$ban_list"
}
# make_ban_lists "${A_CPU_BANS[@]}";exit
# Set the colorscheme
# args: $1 = <scheme number>|<"none">
set_color_scheme()
{
local i='' script_colors='' color_codes=''
if [[ $1 -ge ${#A_COLOR_SCHEMES[@]} ]];then
set -- 1
fi
# Set a global variable to allow checking for chosen scheme later
SCHEME="$1"
if [[ $B_RUNNING_IN_SHELL == 'true' ]];then
color_codes=( $ANSI_COLORS )
else
color_codes=( $IRC_COLORS )
fi
for (( i=0; i < ${#A_COLORS_AVAILABLE[@]}; i++ ))
do
eval "${A_COLORS_AVAILABLE[i]}=\"${color_codes[i]}\""
done
IFS=","
script_colors=( ${A_COLOR_SCHEMES[$1]} )
IFS="$ORIGINAL_IFS"
# then assign the colors globally
C1="${!script_colors[0]}"
C2="${!script_colors[1]}"
CN="${!script_colors[2]}"
# ((COLOR_SCHEME++)) ## note: why is this? ##
}
########################################################################
#### UTILITY FUNCTIONS
########################################################################
#### -------------------------------------------------------------------
#### error handler and debugger
#### error handler, debugger, script updater
#### -------------------------------------------------------------------
# Error handling
@ -563,6 +664,38 @@ script_debugger()
fi
}
# args: $1 - download url, not including file name; $2 - string to print out
# note that $1 must end in / to properly construct the url path
script_self_updater()
{
local wget_error=0
print_screen_output "Starting $SCRIPT_NAME self updater."
print_screen_output "Currently running $SCRIPT_NAME version number: $SCRIPT_VERSION_NUMBER"
print_screen_output "Updating $SCRIPT_NAME in $SCRIPT_PATH using $2 as download source..."
# first test if path is good, need to make sure it's good because we're -O overwriting file
wget -q --spider $1$SCRIPT_NAME || wget_error=$?
# then do the actual download
if [[ $wget_error -eq 0 ]];then
wget -q -O $SCRIPT_PATH/$SCRIPT_NAME $1$SCRIPT_NAME || wget_error=$?
if [[ $wget_error -eq 0 ]];then
SCRIPT_VERSION_NUMBER=$( grep -im 1 'version:' $SCRIPT_PATH/$SCRIPT_NAME | gawk '{print $3}' )
print_screen_output "Successfully updated to $2 version: $SCRIPT_VERSION_NUMBER"
print_screen_output "To run the new version, just start $SCRIPT_NAME again."
exit 0
fi
fi
# now run the error handlers on any wget failure
if [[ $wget_error -gt 0 ]];then
if [[ $2 == 'svn server' ]];then
error_handler 8 "$wget_error"
elif [[ $2 == 'alt server' ]];then
error_handler 10 "$1"
else
error_handler 12 "$1"
fi
fi
}
#### -------------------------------------------------------------------
#### print / output cleaners
#### -------------------------------------------------------------------
@ -621,108 +754,9 @@ remove_erroneous_chars()
}' "$1" ## prints (returns) cleaned input
}
## note: this is now running inside each gawk sequence directly to avoid exiting gawk
## looping in bash through arrays, then re-entering gawk to clean up, then writing back to array
## in bash. For now I'll leave this here because there's still some interesting stuff to get re methods
# Enforce boilerplate and buzzword filters
# args: $1 - BAN_LIST_NORMAL/BAN_LIST_CPU; $2 - string to sanitize
sanitize_characters()
{
# Cannot use strong quotes to unquote a string with pipes in it!
# 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 } {
gsub(/${!1}/,\"\")
gsub(/ [ ]+/,\" \") ## ([ ]+) with (space)
gsub(/^ +| +$/,\"\") ## (pipe char) with (nothing)
print ## prints (returns) cleaned input
}"
}
# Filter boilerplate & buzzwords.
# args: $1 - quoted: "$@" array of ban terms
make_ban_lists()
{
local ban_list=''
# Iterate over $@
## note: this is a weird, non-intuitive method, needs some documentation or rewriting
## if you declare ban_string it stops working, have to read up on this
for ban_string
do
# echo "term=\"$ban_string\"" # >&2
if [[ ${ban_string:0:1} = $'\2' ]];then
ban_list="${ban_list}${ban_list+|}${ban_string:1:${#ban_string}-1}"
else
# Automatically escapes [ ] ( ) . and +
ban_list="${ban_list}${ban_list+|}$( echo "$ban_string" | gawk '{
gsub(/([\[\]+().])/,"\\\\&")
print
}' )"
fi
done
echo "$ban_list"
}
# make_ban_lists "${A_CPU_BANS[@]}";exit
# Set the colorscheme
# args: $1 = <scheme number>|<"none">
set_color_scheme()
{
local i='' script_colors='' color_codes=''
if [[ $1 -ge ${#A_COLOR_SCHEMES[@]} ]];then
set -- 1
fi
# Set a global variable to allow checking for chosen scheme later
SCHEME="$1"
if [[ $B_RUNNING_IN_SHELL == 'true' ]];then
color_codes=( $ANSI_COLORS )
else
color_codes=( $IRC_COLORS )
fi
for (( i=0; i < ${#A_COLORS_AVAILABLE[@]}; i++ ))
do
eval "${A_COLORS_AVAILABLE[i]}=\"${color_codes[i]}\""
done
IFS=","
script_colors=( ${A_COLOR_SCHEMES[$1]} )
IFS="$ORIGINAL_IFS"
# then assign the colors globally
C1="${!script_colors[0]}"
C2="${!script_colors[1]}"
CN="${!script_colors[2]}"
# ((COLOR_SCHEME++)) ## note: why is this? ##
}
# Parse the null separated commandline under /proc/<pid passed in $1>/cmdline
# args: $1 - $PPID
get_cmdline()
{
local i=0 ppid=$1
if [[ ! -e /proc/$ppid/cmdline ]];then
echo 0
return
fi
##print_screen_output "Marker"
##print_screen_output "\$ppid='$ppid' -=- $(< /proc/$ppid/cmdline)"
unset A_CMDL
## note: need to figure this one out, and ideally clean it up and make it readable
while read -d $'\0' L && [ "$i" -lt 32 ]
do
A_CMDL[i++]="$L" ## note: make sure this is valid - What does L mean? ##
done < /proc/$ppid/cmdline
##print_screen_output "\$i='$i'"
if [[ $i -eq 0 ]];then
A_CMDL[0]=$(< /proc/$ppid/cmdline)
if [[ -n ${A_CMDL[0]} ]];then
i=1
fi
fi
CMDL_MAX=$i
}
#### -------------------------------------------------------------------
#### parameter handling, print usage functions.
#### -------------------------------------------------------------------
# Get the parameters. Note: standard options should be lower case, advanced or testing, upper
# args: $1 - full script startup args: $@
@ -962,38 +996,6 @@ print_version_info()
print_screen_output "(at your option) any later version."
}
# args: $1 - download url, not including file name; $2 - string to print out
# note that $1 must end in / to properly construct the url path
script_self_updater()
{
local wget_error=0
print_screen_output "Starting $SCRIPT_NAME self updater."
print_screen_output "Currently running $SCRIPT_NAME version number: $SCRIPT_VERSION_NUMBER"
print_screen_output "Updating $SCRIPT_NAME in $SCRIPT_PATH using $2 as download source..."
# first test if path is good, need to make sure it's good because we're -O overwriting file
wget -q --spider $1$SCRIPT_NAME || wget_error=$?
# then do the actual download
if [[ $wget_error -eq 0 ]];then
wget -q -O $SCRIPT_PATH/$SCRIPT_NAME $1$SCRIPT_NAME || wget_error=$?
if [[ $wget_error -eq 0 ]];then
SCRIPT_VERSION_NUMBER=$( grep -im 1 'version:' $SCRIPT_PATH/$SCRIPT_NAME | gawk '{print $3}' )
print_screen_output "Successfully updated to $2 version: $SCRIPT_VERSION_NUMBER"
print_screen_output "To run the new version, just start $SCRIPT_NAME again."
exit 0
fi
fi
# now run the error handlers on any wget failure
if [[ $wget_error -gt 0 ]];then
if [[ $2 == 'svn server' ]];then
error_handler 8 "$wget_error"
elif [[ $2 == 'alt server' ]];then
error_handler 10 "$1"
else
error_handler 12 "$1"
fi
fi
}
########################################################################
#### MAIN FUNCTIONS
########################################################################
@ -1195,39 +1197,32 @@ get_start_client()
fi
}
## this is a mishmash and will be mostly moved to other places over time, for now
## it's just a holder for some misc stuff that has to happen
set_calculated_variables()
# Parse the null separated commandline under /proc/<pid passed in $1>/cmdline
# args: $1 - $PPID
get_cmdline()
{
local path='' sys_path='' added_path='' b_path_found=''
# Extra path variable to make execute failures less likely, merged below
local extra_paths="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
local i=0 ppid=$1
# Fallback paths put into $extra_paths; This might, among others, help on gentoo.
# Now, create a difference of $PATH and $extra_paths and add that to $PATH:
IFS=":"
for path in $extra_paths
do
b_path_found='false'
for sys_path in $PATH
do
if [[ $path == $sys_path ]];then
b_path_found='true'
if [[ ! -e /proc/$ppid/cmdline ]];then
echo 0
return
fi
done
if [[ $b_path_found == 'false' ]];then
added_path="$added_path:$path"
##print_screen_output "Marker"
##print_screen_output "\$ppid='$ppid' -=- $(< /proc/$ppid/cmdline)"
unset A_CMDL
## note: need to figure this one out, and ideally clean it up and make it readable
while read -d $'\0' L && [ "$i" -lt 32 ]
do
A_CMDL[i++]="$L" ## note: make sure this is valid - What does L mean? ##
done < /proc/$ppid/cmdline
##print_screen_output "\$i='$i'"
if [[ $i -eq 0 ]];then
A_CMDL[0]=$(< /proc/$ppid/cmdline)
if [[ -n ${A_CMDL[0]} ]];then
i=1
fi
done
IFS="$ORIGINAL_IFS"
PATH="${PATH}${added_path}"
##echo "PATH='$PATH'"
##/bin/sh -c 'echo "PATH in subshell=\"$PATH\""'
# Do this after sourcing of config overrides so user can customize banwords
BAN_LIST_NORMAL=$( make_ban_lists "${A_NORMAL_BANS[@]}" ) # Contrary to my previous belief, "${ARR[@]}" passes a quoted list, not one string
BAN_LIST_CPU=$( make_ban_lists "${A_CPU_BANS[@]}" )
##echo "BAN_LIST_NORMAL='$BAN_LIST_NORMAL'"
fi
CMDL_MAX=$i
}
#### -------------------------------------------------------------------
@ -1371,10 +1366,118 @@ get_audio_alsa_data()
echo "$alsa_data"
}
## this is for counting processors and finding HT types
discover_ht_multicore_smp_cpu_data()
{
## create A_CPU_CORE_DATA, currently with two values: integer core count; core string text
## return value cpu core count string, this helps resolve the multi redundant lines of old style output
get_cpu_core_count()
{
if [[ $B_CPUINFO == 'true' ]]; then
# load the A_CPU_TYPE_PCNT_CCNT core data array
get_cpu_ht_multicore_smp_data
## Because of the upcoming release of cpus with core counts over 6, a count of cores is given after Deca (10)
# count the number of processors given
local cpu_core_count=${A_CPU_TYPE_PCNT_CCNT[2]}
local cpu_type=''
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
cpu_type=" ${A_CPU_TYPE_PCNT_CCNT[0]}"
fi
# match the numberic value to an alpha value
case $cpu_core_count in
1) cpu_alpha_count='Single';;
2) cpu_alpha_count='Dual';;
3) cpu_alpha_count='Triple';;
4) cpu_alpha_count='Quad';;
5) cpu_alpha_count='Penta';;
6) cpu_alpha_count='Hexa';;
7) cpu_alpha_count='Hepta';;
8) cpu_alpha_count='Octa';;
9) cpu_alpha_count='Ennea';;
10) cpu_alpha_count='Deca';;
*) cpu_alpha_count='Multi';;
esac
# create array, core count integer; core count string
A_CPU_CORE_DATA=( "$cpu_core_count" "$cpu_alpha_count Core$cpu_type" )
fi
}
## main cpu data collector
get_cpu_data()
{
local i='' j='' cpu_array_nu='' a_cpu_working='' multi_cpu='' bits=''
if [[ $B_CPUINFO == 'true' ]];then
IFS=$'\n'
A_CPU_DATA=($(gawk -F': ' '
{ IGNORECASE=1 }
# 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 }
/^model name|^cpu\t+:/ {
gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF )
gsub(/'"$BAN_LIST_CPU"'/, "", $NF )
gsub(/,/, " ", $NF)
gsub(/^ +| +$/, "", $NF)
gsub(/ [ \t]+/, " ", $NF)
cpu[nr, "model"] = $NF
}
/^cpu MHz|^clock\t+:/ {
if (!min) {
min = $NF
}
else {
if ($NF < min) {
min = $NF
}
}
if ($NF > max) {
max = $NF
}
gsub(/MHZ/,"",$NF) ## clears out for cell cpu
gsub(/.00[0]+$/,".00",$NF) ## clears out excessive zeros
cpu[nr, "speed"] = $NF
}
/^cache size/ { cpu[nr, "cache"] = $NF }
/^flags/ { cpu[nr, "flags"] = $NF }
/^bogomips/ { cpu[nr, "bogomips"] = $NF }
/vendor_id/ {
gsub(/genuine|authentic/,"",$NF)
cpu[nr, "vendor"] = tolower( $NF )
}
END {
#if (!nr) { print ",,,"; exit } # <- should this be necessary or should bash handle that
for ( i = 0; i <= nr; i++ ) {
print cpu[i, "model"] "," cpu[i, "speed"] "," cpu[i, "cache"] "," cpu[i, "flags"] "," cpu[i, "bogomips"] "," cpu[nr, "vendor"]
}
if (!min) {
print "not found"
exit
}
if (min != max) {
printf("Min:%s%s Max:%s%s\n", min, "Mhz", max, "Mhz")
}
else {
printf("%s %s\n", max, "Mhz")
}
}' $DIR_CPUINFO))
fi
IFS="$ORIGINAL_IFS"
}
## this is for counting processors and finding HT types
get_cpu_ht_multicore_smp_data()
{
# in /proc/cpuinfo
# if > 1 processor && processor id == core id then Hyperthreaded (HT)
# if > 1 processor && different processor ids then Multiple Processors (SMP)
@ -1454,112 +1557,6 @@ discover_ht_multicore_smp_cpu_data()
fi
}
## create A_CPU_CORE_DATA, currently with two values: integer core count; core string text
## return value cpu core count string, this helps resolve the multi redundant lines of old style output
get_cpu_core_count()
{
if [[ $B_CPUINFO == 'true' ]]; then
## Because of the upcoming release of cpus with core counts over 6, a count of cores is given after Deca (10)
# count the number of processors given
local cpu_core_count=${A_CPU_TYPE_PCNT_CCNT[2]}
local cpu_type=''
if [[ ${A_CPU_TYPE_PCNT_CCNT[0]} == "HT" || ${A_CPU_TYPE_PCNT_CCNT[0]} == "SMP" ]]; then
cpu_type=${A_CPU_TYPE_PCNT_CCNT[0]}
fi
# match the numberic value to an alpha value
case $cpu_core_count in
1) cpu_alpha_count='Single';;
2) cpu_alpha_count='Dual';;
3) cpu_alpha_count='Triple';;
4) cpu_alpha_count='Quad';;
5) cpu_alpha_count='Penta';;
6) cpu_alpha_count='Hexa';;
7) cpu_alpha_count='Hepta';;
8) cpu_alpha_count='Octa';;
9) cpu_alpha_count='Ennea';;
10) cpu_alpha_count='Deca';;
*) cpu_alpha_count='Multi';;
esac
# create array, core count integer; core count string
A_CPU_CORE_DATA=( "$cpu_core_count" "$cpu_alpha_count Core $cpu_type" )
fi
}
## main cpu data collector
get_cpu_data()
{
local i='' j='' cpu_array_nu='' a_cpu_working='' multi_cpu='' bits=''
if [[ $B_CPUINFO == 'true' ]];then
IFS=$'\n'
A_CPU_DATA=($(gawk -F': ' '
{ IGNORECASE=1 }
# 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 }
/^model name|^cpu\t+:/ {
gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF )
gsub(/'"$BAN_LIST_CPU"'/, "", $NF )
gsub(/,/, " ", $NF)
gsub(/^ +| +$/, "", $NF)
gsub(/ [ \t]+/, " ", $NF)
cpu[nr, "model"] = $NF
}
/^cpu MHz|^clock\t+:/ {
if (!min) {
min = $NF
}
else {
if ($NF < min) {
min = $NF
}
}
if ($NF > max) {
max = $NF
}
gsub(/MHZ/,"",$NF) ## clears out for cell cpu
gsub(/.00[0]+$/,".00",$NF) ## clears out excessive zeros
cpu[nr, "speed"] = $NF
}
/^cache size/ { cpu[nr, "cache"] = $NF }
/^flags/ { cpu[nr, "flags"] = $NF }
/^bogomips/ { cpu[nr, "bogomips"] = $NF }
/vendor_id/ {
gsub(/genuine|authentic/,"",$NF)
cpu[nr, "vendor"] = tolower( $NF )
}
END {
#if (!nr) { print ",,,"; exit } # <- should this be necessary or should bash handle that
for ( i = 0; i <= nr; i++ ) {
print cpu[i, "model"] "," cpu[i, "speed"] "," cpu[i, "cache"] "," cpu[i, "flags"] "," cpu[i, "bogomips"] "," cpu[nr, "vendor"]
}
if (!min) {
print "not found"
exit
}
if (min != max) {
printf("Min:%s%s Max:%s%s\n", min, "Mhz", max, "Mhz")
}
else {
printf("%s %s\n", max, "Mhz")
}
}' $DIR_CPUINFO))
fi
IFS="$ORIGINAL_IFS"
}
# for more on distro id, please reference this python thread: http://bugs.python.org/issue1322
## return distro name/id if found
get_distro_data()