3075
Comment: nevermind, got it.
|
3748
|
Deletions are marked like this. | Additions are marked like this. |
Line 19: | Line 19: |
local ns="[^[:space:]]" | |
Line 23: | Line 24: |
l="${l//@V/$V}" # variable/function/struct name l="${l//@Q2/[^[:alnum:]_>.]}" # 1 char left of variable/function/struct name l="${l//@Q/[^[:alnum:]_]}" # 1 char right of variable/function/struct name l="${l//@d/($int+[uUlLfF]?|$int+[uU]?[lL][lL]?|0x$hex+|$int+[lL][lL][uU]|$int*\.$int+[fF]?)}" # a number l="${l//@K/$up_+$AN_*}" # an definition or macro name (uppercase) l="${l//@s/$sp*}" # optional space l="${l//@S/$sp+}" # obligatory space l="${l//\\$em/$em}" # Because bash bangs otherwise l="${l//@w/($V|\.|->|\[$sp*[^][]+$sp*\]|\($sp*[^)(]*$sp*\))+}" # a variable/array/member/unnested function or macro l="${l//\\\(...\\\)/\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)}" # nested parentheses, 2 backrefs l="$(echo "$l" | sed -r "s/\\\\\{\.\.\.\\\\\}/\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}/g")" # nested curly brackets, 2 backrefs |
l="${l//\\\(-..\\\)/[^()|&;]*(\(...\)[^()|&;]*)*}" # 3 backrefs l="${l//\\\(...\\\)/\([^();]*(\([^();]*(\([^();]*\)[^();]*)*\)[^();]*)*\)}" # 2 backrefs l="${l//\\\[...\\\]/\[[^][]*(\[[^][]*(\[[^][]*\][^][]*)*\][^][]*)*\]}" # 2 backrefs l="${l//@V/$V}" l="${l//@Q2/[^[:alnum:]_>.]}" l="${l//@Q/[^[:alnum:]_]}" l="${l//@d/($int+[uUlLfF]?|$int+[uU]?[lL][lL]?|0x$hex+|$int+[lL][lL][uU]|$int*\.$int+[fF]?)}" l="${l//@K/$up_+$AN_*}" l="${l//@s/$sp*}" l="${l//@n/([^\n]*\n)*}" l="${l//@S/$sp+}" l="${l//\\$em/$em}" l="${l//@w/($V|\.|->|\[$sp*[^][]+$sp*\]|\($sp*[^)(]*$sp*\))+}" l="$(echo "$l" | sed -r "s/\\\\\{-\.\.\\\\\}/\[^\}\{\]*\(\\\\\{...\\\\\}\[^\}\{\]*\)*/g")" # 2 backrefs l="$(echo "$l" | sed -r "s/\\\\\{\.\.\.\\\\\}/\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}/g")" # 2 backrefs l="$(echo "$l" | sed -r "s/\\\\\{\.8\.\\\\\}/\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}/g")" # 2 backrefs |
Line 41: | Line 47: |
} " |
}" |
Line 48: | Line 53: |
ecsed() | ecsed2() |
Line 50: | Line 55: |
local app; if [ "${1:0:6}" = "--PrE=" ]; then app="${1:6}"; shift; fi |
local app not prt; |
Line 55: | Line 57: |
local Sp="[ ]" | |
Line 57: | Line 60: |
local not; while [ -n "$2" ]; do not="$not/$(bli2 "$app$1")/b;" shift; |
while [ $# -ne 1 ]; do if [ -n "$1" ]; then [ "${1:0:6}" = "--sed=" ] && not="${1:6}" || [ "${1:0:6}" = "--PrE=" ] && app="${1:6}" || [ "${1:0:6}" = "--PrT=" ] && prt="${1:6}" || not="$not /$(bli2 "$app$1")/d" fi shift |
Line 62: | Line 69: |
echo ":start /(--|$bol}$s)$/!{ N b start |
echo " /^--/b s/^($ns*)[:-]([0-9]+)[:-]/---[ vi \1 +\2 ]---\n/ :loop s/^$bol// /^(--$|\}$s)/!{ /\/\//s/$Sp\/\/.*$//g H \$!{ n b loop } |
Line 67: | Line 82: |
s/$sp*\/\*+([^*]*|\*[^\/])*\*+\// /g # remove C style comment s/(\/\/[^\n]*)?\n$bol/\n/g # remove C99 comment |
x /$(bli2 "$app$1")/!d /\/\*/s/$Sp*\/\*+([^*]*|\*[^\/])*\*+\/$Sp/ /g |
Line 70: | Line 86: |
s/^($ns*)[:-]([0-9]+)[:-]/---[ vi \1 +\2 ]---\n/ s/\n--$// /$(bli2 "$app$1")/p" |
p $prt d" |
Line 79: | Line 95: |
local opts; local o; local append; local gq; local sq; | local opts gq sq eopts prt file; |
Line 82: | Line 98: |
[ "${1:0:6}" != "--PrE=" ] && opts="$opts $1" || gq="${1:6}"; | [ "${1:0:6}" = "--PrE=" ] && gq="${1:6}" || [ "${1:0:6}" = "--PrT=" ] && prt="$1" || [ "${1:0:6}" = "--FiL=" ] && file="${1:6}" || [ "${1:0:6}" = "--sed=" ] && eopts="$1" || opts="${opts:+$opts }$1"; |
Line 85: | Line 104: |
if [ -z "$gq" ]; then | if [ -n "$file" ]; then sq="$(ecsed2 "$prt" "$eopts" "$@")" sed -n -r "$sq" "$file" elif [ -z "$gq" ]; then |
Line 87: | Line 109: |
shift; sq="$(ecsed2 "$@")" |
sq="$(ecsed2 "$prt" "$eopts" "${@:2}")" |
Line 91: | Line 112: |
sq="$(ecsed2 "--PrE=$gq" "$@")"; | sq="$(ecsed2 "$prt" "$eopts" "--PrE=$gq" "$@")"; |
Line 93: | Line 114: |
git grep -E -n $opts "$gq" | sed -n -r "$sq" | #echo "opts:$opts";echo "gq:$gq";echo "sq:$sq" git grep -E -n $opts "$gq" -- '*.c' '*.h' | sed -n -r "$sq" |
The script below can be used for static code analysis. It requires git, sed and grep
# (c) Roel Kluin, 2009, GPL v2. #!/bin/bash # blank_it: replaces spaces with bracket expressions to match # optional/obligatory spaces for usage with egrep. bli2() { local int="[[:digit:]]" local hex="[[:xdigit:]]" local up_="[[:upper:]_]" local al_="[[:alpha:]_]" local AN_="[[:upper:][:digit:]_]" local an_="[[:alnum:]_]" local sp="[[:space:]]" local ns="[^[:space:]]" local V="$al_+$an_*" local em='!' local l="$1" l="${l//\\\(-..\\\)/[^()|&;]*(\(...\)[^()|&;]*)*}" # 3 backrefs l="${l//\\\(...\\\)/\([^();]*(\([^();]*(\([^();]*\)[^();]*)*\)[^();]*)*\)}" # 2 backrefs l="${l//\\\[...\\\]/\[[^][]*(\[[^][]*(\[[^][]*\][^][]*)*\][^][]*)*\]}" # 2 backrefs l="${l//@V/$V}" l="${l//@Q2/[^[:alnum:]_>.]}" l="${l//@Q/[^[:alnum:]_]}" l="${l//@d/($int+[uUlLfF]?|$int+[uU]?[lL][lL]?|0x$hex+|$int+[lL][lL][uU]|$int*\.$int+[fF]?)}" l="${l//@K/$up_+$AN_*}" l="${l//@s/$sp*}" l="${l//@n/([^\n]*\n)*}" l="${l//@S/$sp+}" l="${l//\\$em/$em}" l="${l//@w/($V|\.|->|\[$sp*[^][]+$sp*\]|\($sp*[^)(]*$sp*\))+}" l="$(echo "$l" | sed -r "s/\\\\\{-\.\.\\\\\}/\[^\}\{\]*\(\\\\\{...\\\\\}\[^\}\{\]*\)*/g")" # 2 backrefs l="$(echo "$l" | sed -r "s/\\\\\{\.\.\.\\\\\}/\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}/g")" # 2 backrefs l="$(echo "$l" | sed -r "s/\\\\\{\.8\.\\\\\}/\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}/g")" # 2 backrefs echo "$l" | sed -r " :a s/($an_)$sp+($an_)/\1$sp+\2/g s/(\[\[:space:\]\]\*)*$sp+/$sp*/g $!{ N ba }" } # echo a sed script to catch from multiline grep $n (where n=$#) and filter out $1 up to ${n-1}. # e.g. grep -E -n -H -B1 -A10 "foo" ~/file | sed -n -r "$(ecsed2 "foo.*bar" "foo")" # searches for lines with `foo', not followed by `bar' ecsed2() { local app not prt; local sp="[[:space:]]"; # space local Sp="[ ]" local ns="[^[:space:]]"; # no space local bol="$ns*[:-][0-9]+[:-]" # begin of line while [ $# -ne 1 ]; do if [ -n "$1" ]; then [ "${1:0:6}" = "--sed=" ] && not="${1:6}" || [ "${1:0:6}" = "--PrE=" ] && app="${1:6}" || [ "${1:0:6}" = "--PrT=" ] && prt="${1:6}" || not="$not /$(bli2 "$app$1")/d" fi shift done echo " /^--/b s/^($ns*)[:-]([0-9]+)[:-]/---[ vi \1 +\2 ]---\n/ :loop s/^$bol// /^(--$|\}$s)/!{ /\/\//s/$Sp\/\/.*$//g H \$!{ n b loop } } x /$(bli2 "$app$1")/!d /\/\*/s/$Sp*\/\*+([^*]*|\*[^\/])*\*+\/$Sp/ /g $not p $prt d" } # gres does something like # git grep -E -n [-opts] "[parsed $1]" | sed -n -r "[parsed ${@:2}]" gres() { local opts gq sq eopts prt file; while [ $# -ne 0 ]; do [ "${1:0:1}" != "-" ] && break; [ "${1:0:6}" = "--PrE=" ] && gq="${1:6}" || [ "${1:0:6}" = "--PrT=" ] && prt="$1" || [ "${1:0:6}" = "--FiL=" ] && file="${1:6}" || [ "${1:0:6}" = "--sed=" ] && eopts="$1" || opts="${opts:+$opts }$1"; shift; done if [ -n "$file" ]; then sq="$(ecsed2 "$prt" "$eopts" "$@")" sed -n -r "$sq" "$file" elif [ -z "$gq" ]; then gq="$(bli2 "$1")"; sq="$(ecsed2 "$prt" "$eopts" "${@:2}")" else gq="$(bli2 "$gq")" sq="$(ecsed2 "$prt" "$eopts" "--PrE=$gq" "$@")"; fi #echo "opts:$opts";echo "gq:$gq";echo "sq:$sq" git grep -E -n $opts "$gq" -- '*.c' '*.h' | sed -n -r "$sq" }
To use, save, e.g. as ~/bin/cvars.sh and:
source ~/bin/cvars.sh
For examples see examples on [wiki:roelkluin my page]