From 84d1fb5ab023d8f8fa006f2141b00b37d67a8bfc Mon Sep 17 00:00:00 2001 From: inxi-svn Date: Thu, 6 Nov 2008 20:58:35 +0000 Subject: [PATCH] gawk code cleanup, got rid of all one line flow controls, got rid of all ; separated statements, made all gawk code line up with columns it occurs in. --- inxi | 159 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 91 insertions(+), 68 deletions(-) diff --git a/inxi b/inxi index 5a9cc23..9e64cdb 100755 --- a/inxi +++ b/inxi @@ -1,7 +1,7 @@ #!/bin/bash ######################################################################## #### Script Name: inxi -#### version: 0.3.26 +#### version: 0.3.27 #### Date: November 6 2008 ######################################################################## #### inxi is a fork of infobash, the original bash sys info script by locsmif @@ -43,7 +43,6 @@ ## a lot more data and verbosity levels going ### Variable initializations: null values - CMDL_MAX='' COLOR_SCHEME='' COLOR_SCHEME_SET='' @@ -302,15 +301,20 @@ remove_erroneous_chars() { ## RS is input record separator ## gsub is substitute; - gawk 'BEGIN { RS="" } { gsub(/\n$/,"") ## (newline; end of string) with (nothing) - gsub(/\n/," "); ## (newline) with (space) - gsub(/^ *| *$/, "") ## (pipe char) with (nothing) - gsub(/ +/, " ") ## ( +) with (space) - gsub(/ [ ]+/, " ") ## ([ ]+) with (space) - gsub(/^ +| +$/, "") ## (pipe char) with (nothing) - printf $0 }' "$1" ## prints (returns) cleaned input + gawk 'BEGIN { RS="" } { + gsub(/\n$/,"") ## (newline; end of string) with (nothing) + gsub(/\n/," "); ## (newline) with (space) + gsub(/^ *| *$/, "") ## (pipe char) with (nothing) + gsub(/ +/, " ") ## ( +) with (space) + gsub(/ [ ]+/, " ") ## ([ ]+) with (space) + gsub(/^ +| +$/, "") ## (pipe char) with (nothing) + printf $0 + }' "$1" ## prints (returns) cleaned input } +## note: this is now running inside each awk sequence directly to avoid exiting awk +## looping in bash through arrays, then re-entering awk 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() @@ -318,10 +322,13 @@ 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 + echo "$2" | gawk " + BEGIN { IGNORECASE=1 } { + gsub(/${!1}/,\"\") + gsub(/ [ ]+/,\" \") ## ([ ]+) with (space) + gsub(/^ +| +$/,\"\") ## (pipe char) with (nothing) + print ## prints (returns) cleaned input + }" } #### ------------------------------------------------------------------- @@ -375,7 +382,10 @@ make_ban_lists() 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 }' )" + ban_list="${ban_list}${ban_list+|}$( echo "$ban_string" | gawk '{ + gsub(/([\[\]+().])/,"\\\\&") + print + }' )" fi done @@ -603,17 +613,17 @@ get_start_client() KONVI=1 fi IRC_CLIENT_VERSION=" $( $irc_client_path -v | gawk ' - /Konversation:/ { - for ( i=2; i<=NF; i++ ) { - if (i == NF) { - print $i - } - else { - printf $i" " - } + /Konversation:/ { + for ( i=2; i<=NF; i++ ) { + if (i == NF) { + print $i } - exit - }' )" + else { + printf $i" " + } + } + exit + }' )" T=($IRC_CLIENT_VERSION) if [[ ${T[0]} == *+* ]];then # < Sho_> locsmif: The version numbers of SVN versions look like this: @@ -627,12 +637,12 @@ get_start_client() fi # Remove any dots except the first, and make sure there are no trailing zeroes, T2=$( echo "$T2" | gawk ' - { - sub(/\./, " ") - gsub(/\./, "") - sub(/ /, ".") - printf("%g\n", $0) - }' ) + { + sub(/\./, " ") + gsub(/\./, "") + sub(/ /, ".") + printf("%g\n", $0) + }' ) # Since Konversation 1.0, the DCOP interface has changed a bit: dcop "$DCPORT" Konversation ..etc # becomes : dcop "$DCPORT" default ... or dcop "$DCPORT" irc ..etc. So we check for versions smaller # than 1 and change the DCOP parameter/object accordingly. @@ -654,12 +664,14 @@ get_start_client() /Version/ { a=tolower($2) gsub(/[()]|bitchx-/,"",a) - print a; exit + print a + exit } $2 == "version" { a=tolower($3) sub(/bitchx-/,"",a) - print a; exit + print a + exit } ' )" IRC_CLIENT="BitchX" @@ -694,7 +706,11 @@ get_start_client() IRC_CLIENT="KVIrc" ;; *kopete*) - IRC_CLIENT_VERSION=" $( kopete -v | gawk '/Kopete:/ { print $2; exit }' )" + IRC_CLIENT_VERSION=" $( kopete -v | gawk ' + /Kopete:/ { + print $2 + exit + }' )" IRC_CLIENT="Kopete" ;; *perl*) @@ -713,7 +729,11 @@ get_start_client() # You can imagine how hosed I am if I try to make inxi find out dynamically with which path # KSirc was run by browsing up the process tree in /proc. That alone is straightjacket material. # (KSirc sucks anyway ;) - IRC_CLIENT_VERSION=" $( ksirc -v | gawk '/KSirc:/ { print $2; exit }' )" + IRC_CLIENT_VERSION=" $( ksirc -v | gawk ' + /KSirc:/ { + print $2 + exit + }' )" break ;; esac @@ -782,17 +802,17 @@ set_calculated_variables() get_audio_data() { local i='' - + tt='nvidia' IFS=$'\n' A_AUDIO_DATA=( $( echo "$lspci_data" | gawk -F': ' ' - { IGNORECASE=1 } - /multimedia audio controller|audio device/ { - gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF ) - gsub(/,/," ",$NF) - gsub(/^ +| +$/,"",$NF) - gsub(/ [ \t]+/," ",$NF) - print $NF - }' ) ) + { IGNORECASE=1 } + /multimedia audio controller|audio device/ { + gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF ) + gsub(/,/," ",$NF) + gsub(/^ +| +$/,"",$NF) + gsub(/ [ \t]+/," ",$NF) + print $NF + }' ) ) IFS="$ORIGINAL_IFS" # handle cases where card detection fails, like in PS3, where lspci gives no output, or headless boxes.. @@ -944,13 +964,14 @@ get_distro_data() # Debian pure should fall through here distro_file="issue" distro=$( gawk ' - BEGIN { RS="" } { - gsub(/\\[a-z]/, "") - gsub(/,/, " ") - gsub(/^ +| +$/, "") - gsub(/ [ \t]+/, " ") - print - }' "/etc/${distro_file}" ) + BEGIN + { RS="" } { + gsub(/\\[a-z]/, "") + gsub(/,/, " ") + gsub(/^ +| +$/, "") + gsub(/ [ \t]+/, " ") + print + }' "/etc/${distro_file}" ) fi if [[ ${#distro} -gt 80 && $B_HANDLE_CORRUPT_DATA != 'true' ]];then distro="${RED}/etc/${distro_file} corrupted, use -C to override${NORMAL}" @@ -975,15 +996,15 @@ get_graphics_card_data() IFS=$'\n' A_GFX_CARD_DATA=( $( echo "$lspci_data" | gawk -F': ' ' - { IGNORECASE=1 } - /VGA compatible controller/ { - gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF) - gsub(/,/, " ", $NF) - gsub(/^ +| +$/, "", $NF) - gsub(/ [ \t]+/, " ", $NF) - print $NF - }' - ) ) + { IGNORECASE=1 } + /vga compatible controller/ { + gsub(/'"$BAN_LIST_NORMAL"'/, "", $NF) + gsub(/,/, " ", $NF) + gsub(/^ +| +$/, "", $NF) + gsub(/ [ \t]+/, " ", $NF) + print $NF + }' + ) ) IFS="$ORIGINAL_IFS" # for (( i=0; i < ${#A_GFX_CARD_DATA[@]}; i++ )) # do @@ -1015,7 +1036,6 @@ get_graphics_glx_data() } BEGIN { IGNORECASE=1 } - /opengl renderer/ { if ($2 ~ /mesa/) { # Allow r300 et al, but not the rest @@ -1102,13 +1122,13 @@ get_graphics_x_data() x_version=$( xdpyinfo | awk '/version:/ { print $NF }' ) if [[ -z $x_version ]];then x_version=$(xdpyinfo | gawk -F': +' ' - BEGIN { IGNORECASE=1 } - /vendor release number/ { - gsub(/0+$/, "", $2) - gsub(/0+/, ".", $2) - print $2 - } - ') + BEGIN { IGNORECASE=1 } + /vendor release number/ { + gsub(/0+$/, "", $2) + gsub(/0+/, ".", $2) + print $2 + } + ') fi A_X_DATA[0]="$x_vendor" A_X_DATA[1]="$x_version" @@ -1229,7 +1249,10 @@ get_hard_drive_data() get_lspci_data() { - echo "$( lspci -v | awk '{ gsub(/\(prog-if[^)]*\)/,""); print }' )" + echo "$( lspci -v | awk '{ + gsub(/\(prog-if[^)]*\)/,"") + print + }' )" } ## return memory used/installed