more refactoring

This commit is contained in:
inxi-svn 2008-10-28 02:24:11 +00:00
parent ef7ac17e39
commit 04674b0e72

92
inxi
View file

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
######################################################################## ########################################################################
#### Script Name: inxi #### Script Name: inxi
#### version: 0.1.4 #### version: 0.1.5
#### Date: October 27 2008 #### Date: October 27 2008
######################################################################## ########################################################################
#### inxi is a fork of infobash, the original bash sys info script by locsmif #### inxi is a fork of infobash, the original bash sys info script by locsmif
@ -70,13 +70,13 @@ IRCCL=" $IRCCL \x0312 \x0302 \x0313 \x0306 \x0311 \x0310 \x0300
COLORS=(DGREY BLACK RED DRED GREEN DGREEN YELLOW DYELLOW BLUE DBLUE MAGENTA DMAGENTA CYAN DCYAN WHITE GREY NORMAL) COLORS=(DGREY BLACK RED DRED GREEN DGREEN YELLOW DYELLOW BLUE DBLUE MAGENTA DMAGENTA CYAN DCYAN WHITE GREY NORMAL)
# See above for notes on EMPTY # See above for notes on EMPTY
SCHEMES=(EMPTY,EMPTY,EMPTY NORMAL,NORMAL,NORMAL BLUE,NORMAL,NORMAL GREEN,YELLOW,NORMAL DYELLOW,NORMAL,NORMAL CYAN,BLUE,NORMAL RED,NORMAL,NORMAL GREEN,NORMAL,NORMAL YELLOW,NORMAL,NORMAL GREEN,DGREEN,NORMAL BLUE,RED,NORMAL BLUE,NORMAL,RED YELLOW,WHITE,GREEN BLUE,NORMAL,GREEN DCYAN,NORMAL,DMAGENTA) SCHEMES=(EMPTY,EMPTY,EMPTY NORMAL,NORMAL,NORMAL BLUE,NORMAL,NORMAL GREEN,YELLOW,NORMAL DYELLOW,NORMAL,NORMAL CYAN,BLUE,NORMAL RED,NORMAL,NORMAL GREEN,NORMAL,NORMAL YELLOW,NORMAL,NORMAL GREEN,DGREEN,NORMAL BLUE,RED,NORMAL BLUE,NORMAL,RED YELLOW,WHITE,GREEN BLUE,NORMAL,GREEN DCYAN,NORMAL,DMAGENTA)
DEFSCHEME=2 # Defaults to 2, make this 1 for normal, 0 for no colorcodes at all. Set to any other valid scheme you like. Same as runtime parameter. DEFAULT_SCHEME=2 # Defaults to 2, make this 1 for normal, 0 for no colorcodes at all. Set to any other valid scheme you like. Same as runtime parameter.
SHOWHOST=1 # Set this to 0 to avoid printing the hostname SHOW_HOST=1 # Set this to 0 to avoid printing the hostname
SHOWIRC=2 # SHOWIRC=1 to avoid showing the irc client version number, or SHOWIRC=0 to disable client information completely. SHOW_IRC=2 # SHOW_IRC=1 to avoid showing the irc client version number, or SHOW_IRC=0 to disable client information completely.
INDENT=14 # Default indentation level INDENT=14 # Default indentation level
VBL=0 # Verbosity level defaults to 0, this can also be set with -v, -v2, -v3, etc as a parameter. VBL=0 # Verbosity level defaults to 0, this can also be set with -v, -v2, -v3, etc as a parameter.
IBSHELL=0 # Running in a shell? Defaults to false, and is determined later. IBSHELL=0 # Running in a shell? Defaults to false, and is determined later.
NOCPUCOUNT=0 # Wether or not the string "dual" or similar is found in cpuinfo output. If so, avoid dups. NO_CPU_COUNT=0 # Wether or not the string "dual" or similar is found in cpuinfo output. If so, avoid dups.
# In cases of derived distros where the version file of the base distro can also be found under /etc, the derived distro's # In cases of derived distros where the version file of the base distro can also be found under /etc, the derived distro's
# version file should go first. (Such as with Sabayon / Gentoo) # version file should go first. (Such as with Sabayon / Gentoo)
DIST_DERIV="antiX kanotix-version knoppix-version redhat-release sabayon-release sidux-version turbolinux-release zenwalk-version" DIST_DERIV="antiX kanotix-version knoppix-version redhat-release sabayon-release sidux-version turbolinux-release zenwalk-version"
@ -85,14 +85,14 @@ DIST_MAIN="gentoo-release mandrake-release redhat-release slackware-version SuSE
DIST_BLIST="debian_version ubuntu_version" DIST_BLIST="debian_version ubuntu_version"
FL1='-' # These two determine seperators in single line output, to force irc clients not to break off sections FL1='-' # These two determine seperators in single line output, to force irc clients not to break off sections
FL2='' FL2=''
# Precede a banword with $'\2' to prevent it from being subject to automated escaping by the mkbans routine # Precede a banword with $'\2' to prevent it from being subject to automated escaping by the make_bans routine
# $'\1' gets weird results : # $'\1' gets weird results :
# user@host $ ARR=($'\x01'"one two" three four); echo ${ARR[0]} | hd -v # user@host $ ARR=($'\x01'"one two" three four); echo ${ARR[0]} | hd -v
# 00000000 01 01 6f 6e 65 20 74 77 6f 0a |..one two.| # 00000000 01 01 6f 6e 65 20 74 77 6f 0a |..one two.|
NORMALBANS=(corporation communications technologies technology group $'\2'"\<ltd\>" ltd. $'\2'"\<inc\>" inc. $'\2'\<co\> co. "(tm)" "(r)" $'\2'"\(rev ..\)") NORMAL_BANS=(corporation communications technologies technology group $'\2'"\<ltd\>" ltd. $'\2'"\<inc\>" inc. $'\2'\<co\> co. "(tm)" "(r)" $'\2'"\(rev ..\)")
CPUBANS=(cpu processor $'\2'"[0-9.]+ *[MmGg][Hh][Zz]") CPU_BANS=(cpu processor $'\2'"[0-9.]+ *[MmGg][Hh][Zz]")
XPATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" # Extra path variable to make execute failures less likely, merged below X_PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" # Extra path variable to make execute failures less likely, merged below
PARAMLIMIT=30 # This is a variable that controls how many parameters infobash will parse in a /proc/<pid>/cmdline file before stopping. PARAMETER_LIMIT=30 # This is a variable that controls how many parameters infobash will parse in a /proc/<pid>/cmdline file before stopping.
CRAP=0 # New parameter CRAP=0 # New parameter
FLAG=0 # A throw-away 'flag' variable intended to be used throughout infobash FLAG=0 # A throw-away 'flag' variable intended to be used throughout infobash
KONVICFG="konversation/scripts/$SCRIPT_LOWER_CASE.conf" # relative path to $(kde-config --path data) KONVICFG="konversation/scripts/$SCRIPT_LOWER_CASE.conf" # relative path to $(kde-config --path data)
@ -139,7 +139,7 @@ print_screen_output()
} }
# Error handling # Error handling
error() error_handler()
{ {
case $1 in case $1 in
2) 2)
@ -165,7 +165,7 @@ error()
exit $1 exit $1
} }
debug_script() script_debugger()
{ {
if (( ALLUP )) if (( ALLUP ))
then then
@ -185,7 +185,7 @@ debug_script()
else else
if ((!DEBUG_FLOOD && DBI>10)) if ((!DEBUG_FLOOD && DBI>10))
then then
error 2 error_handler 2
fi fi
DBGBUF[DBI++]="$@" DBGBUF[DBI++]="$@"
fi fi
@ -197,30 +197,30 @@ eat()
} }
# Determine if any of the absolutely necessary tools are absent # Determine if any of the absolutely necessary tools are absent
checkdepend() check_script_depends()
{ {
if [ ! -d /proc/ ] if [ ! -d /proc/ ]
then then
error 6 error_handler 6
fi fi
if ((X)) if ((X))
then then
for I in xrandr xdpyinfo glxinfo for I in xrandr xdpyinfo glxinfo
do do
type -p $I >/dev/null || { debug_script "inxi: Resuming in non X mode: $I not found in path"; X=0; break; } type -p $I >/dev/null || { script_debugger "inxi: Resuming in non X mode: $I not found in path"; X=0; break; }
done done
fi fi
# bc removed from deps for now # bc removed from deps for now
for I in df free gawk grep hostname lspci ps readlink runlevel tr uname uptime wc for I in df free gawk grep hostname lspci ps readlink runlevel tr uname uptime wc
do do
type -p $I >/dev/null || error 5 "$I" type -p $I >/dev/null || error_handler 5 "$I"
done done
} }
# Filter boilerplate & buzzwords. # Filter boilerplate & buzzwords.
mkbans() make_bans()
{ {
unset MKBANS unset MKBANS
# Iterate over $@ # Iterate over $@
@ -240,7 +240,7 @@ mkbans()
# Set the colorscheme # Set the colorscheme
# $1 = <scheme number>|<"none"> # $1 = <scheme number>|<"none">
cscheme() set_color_scheme()
{ {
local I SCOLORS CLCODES local I SCOLORS CLCODES
@ -267,7 +267,7 @@ cscheme()
} }
# Parse the null separated commandline under /proc/<pid passed in $1>/cmdline # Parse the null separated commandline under /proc/<pid passed in $1>/cmdline
getcmdline() get_cmdline()
{ {
if [[ ! -e /proc/$1/cmdline ]] if [[ ! -e /proc/$1/cmdline ]]
then then
@ -291,7 +291,7 @@ getcmdline()
} }
# Determine where infobash was run from # Determine where infobash was run from
roots() get_start_app()
{ {
if tty >/dev/null if tty >/dev/null
then then
@ -366,7 +366,7 @@ roots()
;; ;;
*perl*) *perl*)
unset IRCV # KSirc is one of the possibilities now. KSirc is a wrapper around dsirc, a perl client unset IRCV # KSirc is one of the possibilities now. KSirc is a wrapper around dsirc, a perl client
getcmdline $PPID get_cmdline $PPID
for ((I=0; I<=$CMDLMAX; I++)) for ((I=0; I<=$CMDLMAX; I++))
do do
case ${CMDL[I]} in case ${CMDL[I]} in
@ -399,7 +399,7 @@ roots()
unset IRCV unset IRCV
;; ;;
esac esac
if [[ $SHOWIRC -lt 2 ]] if [[ $SHOW_IRC -lt 2 ]]
then then
unset IRCV unset IRCV
fi fi
@ -439,7 +439,7 @@ get_parameters()
VBL="$1" VBL="$1"
fi fi
fi fi
echo "$VBL" | grep -q '^[1-3]$' || error 4 "$VBL" echo "$VBL" | grep -q '^[1-3]$' || error_handler 4 "$VBL"
;; ;;
-U) -U)
print_screen_output "Updating $SCRIPT_NAME now..." print_screen_output "Updating $SCRIPT_NAME now..."
@ -460,10 +460,10 @@ get_parameters()
exit 0 exit 0
;; ;;
*) *)
echo "$1" | grep -q '^[0-9][0-9]\?$' || error 3 "$1" echo "$1" | grep -q '^[0-9][0-9]\?$' || error_handler 3 "$1"
if ! ((CSCHEME)) if ! ((CSCHEME))
then then
cscheme "$1" set_color_scheme "$1"
fi fi
;; ;;
esac esac
@ -489,11 +489,11 @@ then
X=1 X=1
fi fi
# Fallback paths put into $XPATH; This might, among others, help on gentoo. # Fallback paths put into $X_PATH; This might, among others, help on gentoo.
# Now, create a difference of $PATH and $XPATH and add that to $PATH: # Now, create a difference of $PATH and $X_PATH and add that to $PATH:
unset TPATH unset TPATH
IFS=":" IFS=":"
for I in $XPATH for I in $X_PATH
do do
FLAG=0 FLAG=0
for J in $PATH for J in $PATH
@ -514,19 +514,19 @@ PATH="${PATH}${TPATH}"
##/bin/sh -c 'echo "PATH in subshell=\"$PATH\""' ##/bin/sh -c 'echo "PATH in subshell=\"$PATH\""'
# Check for dependencies before running any commands in this script! So yes, here!! # Check for dependencies before running any commands in this script! So yes, here!!
checkdepend check_script_depends
# Do this after sourcing of config overrides so user can customize banwords # Do this after sourcing of config overrides so user can customize banwords
NORMALBANS=$(mkbans "${NORMALBANS[@]}") # Contrary to my previous belief, "${ARR[@]}" passes a quoted list, not one string NORMAL_BANS=$(make_bans "${NORMAL_BANS[@]}") # Contrary to my previous belief, "${ARR[@]}" passes a quoted list, not one string
CPUBANS=$(mkbans "${CPUBANS[@]}") CPU_BANS=$(make_bans "${CPU_BANS[@]}")
##echo "NORMALBANS='$NORMALBANS'" ##echo "NORMAL_BANS='$NORMAL_BANS'"
LSPCI=$(lspci -v | awk '{ gsub(/\(prog-if[^)]*\)/,""); print }') LSPCI=$(lspci -v | awk '{ gsub(/\(prog-if[^)]*\)/,""); print }')
######################################################################## ########################################################################
#### MAIN FUNCTIONS #### MAIN FUNCTIONS
######################################################################## ########################################################################
roots get_start_app
if ((KONVI)) if ((KONVI))
then then
@ -554,11 +554,11 @@ get_parameters "$@"
# If no colorscheme was set in the parameter handling routine, then set the default scheme # If no colorscheme was set in the parameter handling routine, then set the default scheme
if ! ((CSCHEME)) if ! ((CSCHEME))
then then
cscheme "$DEFSCHEME" set_color_scheme "$DEFAULT_SCHEME"
fi fi
ALLUP=1 ALLUP=1
debug_script "ALLUP=1 : inxi up and running.." script_debugger "ALLUP=1 : inxi up and running.."
HN=$(hostname) HN=$(hostname)
@ -575,7 +575,7 @@ elif (( ${#DISTGLOB[@]} > 1 ))
then then
for I in $DIST_DERIV $DIST_MAIN for I in $DIST_DERIV $DIST_MAIN
do do
# Only echo works with ${var[@]}, not print_screen_output() or debug_script() # Only echo works with ${var[@]}, not print_screen_output() or script_debugger()
# This is a known bug, search for the word "strange" inside comments # This is a known bug, search for the word "strange" inside comments
# echo "I='$I' DISTGLOB[@]='${DISTGLOB[@]}'" # echo "I='$I' DISTGLOB[@]='${DISTGLOB[@]}'"
if [[ " ${DISTGLOB[@]} " == *" $I "* ]] if [[ " ${DISTGLOB[@]} " == *" $I "* ]]
@ -647,8 +647,8 @@ for ((I=0;I<${#CPU[@]}-1;I++))
do do
IFS=","; CPUTMP=(${CPU[I]}); IFS="$OIFS" IFS=","; CPUTMP=(${CPU[I]}); IFS="$OIFS"
unset CPU[I] unset CPU[I]
CPUTMP[0]=$( sanitize NORMALBANS "${CPUTMP[0]}" ) CPUTMP[0]=$( sanitize NORMAL_BANS "${CPUTMP[0]}" )
CPUTMP[0]=$( sanitize CPUBANS "${CPUTMP[0]}" ) CPUTMP[0]=$( sanitize CPU_BANS "${CPUTMP[0]}" )
# I use all these loops so I can easily extend the cpu array created in the awk script above with more fields per cpu. # I use all these loops so I can easily extend the cpu array created in the awk script above with more fields per cpu.
for (( J=0; J<${#CPUTMP[@]}; J++ )) for (( J=0; J<${#CPUTMP[@]}; J++ ))
do do
@ -663,7 +663,7 @@ done
if echo "${CPU[@]}" | grep -qi 'dual\|triple\|quad' if echo "${CPU[@]}" | grep -qi 'dual\|triple\|quad'
then then
NOCPUCOUNT=1 NO_CPU_COUNT=1
else else
case $(( ${#CPU[@]}-1 )) in case $(( ${#CPU[@]}-1 )) in
2) 2)
@ -830,7 +830,7 @@ GFX=($( echo "$LSPCI" | gawk -F': ' '/VGA compatible controller/ { print $NF }'
IFS="$OIFS" IFS="$OIFS"
for (( I=0; I < ${#GFX[@]}; I++ )) for (( I=0; I < ${#GFX[@]}; I++ ))
do do
GFX[I]=$( sanitize NORMALBANS "${GFX[I]}" ) GFX[I]=$( sanitize NORMAL_BANS "${GFX[I]}" )
done done
# GFXMEM is UNUSED at the moment, because it shows AGP aperture size, which is not necessarily equal to GFX memory.. # GFXMEM is UNUSED at the moment, because it shows AGP aperture size, which is not necessarily equal to GFX memory..
@ -927,7 +927,7 @@ fi
# (IF VBL > 1) # (IF VBL > 1)
if ((VBL)) if ((VBL))
then then
if ((SHOWHOST)) if ((SHOW_HOST))
then then
LNINTRO=$(printf "${C1}%-${INDENT}s${C2} \"%s\"${C1} %s " "Host/Kernel/OS" "$HN" "running") LNINTRO=$(printf "${C1}%-${INDENT}s${C2} \"%s\"${C1} %s " "Host/Kernel/OS" "$HN" "running")
else else
@ -1010,7 +1010,7 @@ then
gsub(/realtek semiconductor/, "Realtek", nic) gsub(/realtek semiconductor/, "Realtek", nic)
gsub(/davicom semiconductor/, "Davicom", nic) gsub(/davicom semiconductor/, "Davicom", nic)
gsub(/,/," ", nic) gsub(/,/," ", nic)
gsub(/'"$NORMALBANS"'/, "", nic); gsub(/ [ ]+/," ", nic); gsub(/^ +| +$/,"", nic) gsub(/'"$NORMAL_BANS"'/, "", nic); gsub(/ [ ]+/," ", nic); gsub(/^ +| +$/,"", nic)
# The doublequotes above are necessary because of the pipes in the variable. # The doublequotes above are necessary because of the pipes in the variable.
eth[nic]++ eth[nic]++
while (getline && !/^$/) { while (getline && !/^$/) {
@ -1074,7 +1074,7 @@ then
LNLAST="${LNLAST}$( echo -ne " | ${C1}Runlevel${C2} ${RUNLVL}${CN}" )" LNLAST="${LNLAST}$( echo -ne " | ${C1}Runlevel${C2} ${RUNLVL}${CN}" )"
fi fi
if ((SHOWIRC)) if ((SHOW_IRC))
then then
LNLAST="${LNLAST}$( echo -ne " | ${C1}Client${C2} ${IRC}${IRCV}${CN}" )" LNLAST="${LNLAST}$( echo -ne " | ${C1}Client${C2} ${IRC}${IRCV}${CN}" )"
fi fi
@ -1085,7 +1085,7 @@ then
fi fi
print_screen_output "$LNLAST" print_screen_output "$LNLAST"
else # (IF VBL > 1 ... ELSE) else # (IF VBL > 1 ... ELSE)
#cscheme 12 #set_color_scheme 12
if ! ((IBSHELL)) if ! ((IBSHELL))
then then
for I in $C1 $C2 $CN ; do case "$I" in "$GREEN"|"$WHITE"|"$YELLOW"|"$CYAN") BGBLACK=1 ;; esac ; done for I in $C1 $C2 $CN ; do case "$I" in "$GREEN"|"$WHITE"|"$YELLOW"|"$CYAN") BGBLACK=1 ;; esac ; done
@ -1093,7 +1093,7 @@ else # (IF VBL > 1 ... ELSE)
#C1="${C1},1"; C2="${C2},1"; CN="${CN},1" #C1="${C1},1"; C2="${C2},1"; CN="${CN},1"
fi fi
LNSHORT=$(echo -ne "${C1}CPU${CN}[${C2}${CPUMODEL} clocked at ${CPUCLOCK}${CN}] ${C1}Kernel${CN}[${C2}${OSKERN}${CN}] ${C1}Up${CN}[${C2}${FL2}${FL1}${UPT}${FL1}${CN}] ${C1}Mem${CN}[${C2}${FL2}${FL1}${MEM}${FL1}${CN}] ${C1}HDD${CN}[${C2}${FL2}${FL1}${HDD}($HDDUSG)${FL1}${CN}] ${C1}Procs${CN}[${C2}${FL2}${FL1}${PROC}${FL1}${CN}]") LNSHORT=$(echo -ne "${C1}CPU${CN}[${C2}${CPUMODEL} clocked at ${CPUCLOCK}${CN}] ${C1}Kernel${CN}[${C2}${OSKERN}${CN}] ${C1}Up${CN}[${C2}${FL2}${FL1}${UPT}${FL1}${CN}] ${C1}Mem${CN}[${C2}${FL2}${FL1}${MEM}${FL1}${CN}] ${C1}HDD${CN}[${C2}${FL2}${FL1}${HDD}($HDDUSG)${FL1}${CN}] ${C1}Procs${CN}[${C2}${FL2}${FL1}${PROC}${FL1}${CN}]")
((SHOWIRC)) && LNSHORT="${LNSHORT}$(echo -ne " ${C1}Client${CN}[${C2}${IRC}${IRCV}${CN}]")" ((SHOW_IRC)) && LNSHORT="${LNSHORT}$(echo -ne " ${C1}Client${CN}[${C2}${IRC}${IRCV}${CN}]")"
((SCHEME)) && LNSHORT="${LNSHORT}$(echo -ne "$NORMAL")" ((SCHEME)) && LNSHORT="${LNSHORT}$(echo -ne "$NORMAL")"
print_screen_output "$LNSHORT" print_screen_output "$LNSHORT"
fi # (IF VBL > 1) fi # (IF VBL > 1)