#!/bin/bash
srcfile=new.c
diff=diff.txt
function parse_src_to_regions()
{
gawk '
BEGIN{
}
{
judge = $0~/^[0-z_]+(\[|\]|\*)*\s+(\[|\]|\*)*[0-z_]+\s*\(.*$/
if (judge){
split($0, strs, "\\(")
split(strs[1], strs2, "\\s+")
func_name = strs2[length(strs2)]
func_to_start[func_name] = NR
print(func_name":"NR)
}
}
END{
}
' $1
}
function get_regions_from_ranges()
{
gawk -F ":" '
BEGIN{
start = '$1'
end = '$2'
prevfunc = ""
prevline = -1
}
{
if ($2 > end){
exit;
}
if ($2 == end){
print($1":"$2)
exit;
}
if ($2 < start){
prevline = $2
prevfunc = $1
next
}
if ($2 == start){
print($1":"$2)
prevline = -1
next
}
if(prevline >= 0){
print("%#%"prevfunc":"prevline)
}
print($1":"$2)
}
END{
}
' $3
}
function get_start_to_end()
{
regions=`more $2`
for region in ${regions[*]}; do
OLD_IFS="$IFS"
IFS=":"
strs=($region)
IFS="$OLD_IFS"
func_name=${strs[0]}
nr=$((${strs[1]}))
gawk '
BEGIN{
in_double_quote=0
in_quote=0
level=0
in_func=0
in_comment=0
}
{
if(NR < '$nr'){
next
}
if ($0~/^[:blank:]*\/\//){
next
}
if($0~/^[:blank:]*\/\*/){
in_comment = 1
}
split($0, char_array, "")
for(i=1; i<=length(char_array); ++i){
char = char_array[i]
if(char == "\\"){
i++
continue
}
if(in_comment == 1){
if(char != "*"){
continue
}
if(i==length(char_array) || char_array[i+1]!="/"){
continue
}
i++
in_comment = 0
continue
}
if(in_quote==0 && in_double_quote==0){
if(char=="/" && i<length(char_array)){
if(char_array[i+1]=="/"){
next
}else if(char_array[i+1]=="*"){
i++
in_comment=1
continue
}
}
if(char=="\x27"){
in_quote=1
continue
}
if(char=="\""){
in_double_quote=1
continue
}
}else{
if(in_quote==1){
if(match(char, /'\''/)){
in_quote=0
}
continue
}
if(in_double_quote==1){
if(match(char, /\"/)){
in_double_quote=0
}
continue
}
}
if(in_func==0){
if(!match(char, /\{/)){
continue
}
in_func = 1;
level = 1;
}else{
if(match(char, /\{/) || match(char, /\}/)){
if(match(char, /\{/)){
level=level+1
}else{
level=level-1
if(level==0){
print('$nr'":"NR)
exit
}
}
}
}
}
/* print(NR": "in_func" "level) */
if(in_func>0 && level==0){
print('$nr'":"NR)
exit
}
}s
END{
}
' $1
done
}
function print_by_ranges()
{
ranges=`more $2`
whole=""
for range in ${ranges[*]}; do
whole=$whole$range":"
done
gawk '
BEGIN{
cur=1
str="'$whole'"
split(str, strs, ":")
}
{
if(NR>=strs[cur] && NR<=strs[cur+1]){
print($0)
}
if(NR==strs[cur+1]){
cur+=2
}
}
' $1
}
parse_src_to_regions $srcfile \
| get_regions_from_ranges $1 $2 \
| get_start_to_end $srcfile \
| print_by_ranges $srcfile
srcfile=new.c
diff=diff.txt
function parse_src_to_regions()
{
gawk '
BEGIN{
}
{
judge = $0~/^[0-z_]+(\[|\]|\*)*\s+(\[|\]|\*)*[0-z_]+\s*\(.*$/
if (judge){
split($0, strs, "\\(")
split(strs[1], strs2, "\\s+")
func_name = strs2[length(strs2)]
func_to_start[func_name] = NR
print(func_name":"NR)
}
}
END{
}
' $1
}
function get_regions_from_ranges()
{
gawk -F ":" '
BEGIN{
start = '$1'
end = '$2'
prevfunc = ""
prevline = -1
}
{
if ($2 > end){
exit;
}
if ($2 == end){
print($1":"$2)
exit;
}
if ($2 < start){
prevline = $2
prevfunc = $1
next
}
if ($2 == start){
print($1":"$2)
prevline = -1
next
}
if(prevline >= 0){
print("%#%"prevfunc":"prevline)
}
print($1":"$2)
}
END{
}
' $3
}
function get_start_to_end()
{
regions=`more $2`
for region in ${regions[*]}; do
OLD_IFS="$IFS"
IFS=":"
strs=($region)
IFS="$OLD_IFS"
func_name=${strs[0]}
nr=$((${strs[1]}))
gawk '
BEGIN{
in_double_quote=0
in_quote=0
level=0
in_func=0
in_comment=0
}
{
if(NR < '$nr'){
next
}
if ($0~/^[:blank:]*\/\//){
next
}
if($0~/^[:blank:]*\/\*/){
in_comment = 1
}
split($0, char_array, "")
for(i=1; i<=length(char_array); ++i){
char = char_array[i]
if(char == "\\"){
i++
continue
}
if(in_comment == 1){
if(char != "*"){
continue
}
if(i==length(char_array) || char_array[i+1]!="/"){
continue
}
i++
in_comment = 0
continue
}
if(in_quote==0 && in_double_quote==0){
if(char=="/" && i<length(char_array)){
if(char_array[i+1]=="/"){
next
}else if(char_array[i+1]=="*"){
i++
in_comment=1
continue
}
}
if(char=="\x27"){
in_quote=1
continue
}
if(char=="\""){
in_double_quote=1
continue
}
}else{
if(in_quote==1){
if(match(char, /'\''/)){
in_quote=0
}
continue
}
if(in_double_quote==1){
if(match(char, /\"/)){
in_double_quote=0
}
continue
}
}
if(in_func==0){
if(!match(char, /\{/)){
continue
}
in_func = 1;
level = 1;
}else{
if(match(char, /\{/) || match(char, /\}/)){
if(match(char, /\{/)){
level=level+1
}else{
level=level-1
if(level==0){
print('$nr'":"NR)
exit
}
}
}
}
}
/* print(NR": "in_func" "level) */
if(in_func>0 && level==0){
print('$nr'":"NR)
exit
}
}s
END{
}
' $1
done
}
function print_by_ranges()
{
ranges=`more $2`
whole=""
for range in ${ranges[*]}; do
whole=$whole$range":"
done
gawk '
BEGIN{
cur=1
str="'$whole'"
split(str, strs, ":")
}
{
if(NR>=strs[cur] && NR<=strs[cur+1]){
print($0)
}
if(NR==strs[cur+1]){
cur+=2
}
}
' $1
}
parse_src_to_regions $srcfile \
| get_regions_from_ranges $1 $2 \
| get_start_to_end $srcfile \
| print_by_ranges $srcfile