#!/bin/bash
srcfile=new.c
diff=diff.txt
function parse_src_to_regions()
{
gawk '
{
judge = $0~/^[0-9a-zA-Z_]+(\[|\]|\*)*\s+(\[|\]|\*)*[0-9a-zA-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)
}
}
' $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~/^\s*\/\//){
next
}
if($0~/^\s*\/\*/){
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 $5`
whole=""
for range in ${ranges[*]}; do
whole=$whole$range":"
done
gawk '
BEGIN{
cur=1
str="'$whole'"
split(str, strs, ":")
diff_start = '$2'
diff_end = '$3'
diff_started = 0
have_region=1
}
{
if(cur+1<=length(strs)){
func_start = strs[cur]
func_end = strs[cur+1]
have_region = 1
}else{
func_start = 8888888
func_end = -1
have_region = 0
}
if(have_region==1){
if (func_end < diff_start){
cur+=2
next
}else if (func_start > diff_end){
exit
}
}
if(NR<=func_end && NR>=func_start){/*in-function modification*/
print($0)
diff_started = 1
}else if (NR < func_start && NR>=diff_start && NR<=diff_end){/* handle globals */
if((diff_started==0 && $0~/^\s+/) || (NR==diff_end && $0!~/;\s*$/)){
print("'$srcfile'"" line "NR": //error: parse_err=1, cannot handle partial global modification.")
exit
}
glo_judge = /^[0-9a-zA-Z_]+[\[\]\*]*\s+([0-9a-zA-Z_]+)/
glo_judge2 = /^extern\s+[0-9a-zA-Z_]+[\[\]\*]*\s+([0-9a-zA-Z_]+)/
if(match($0, glo_judge2, grp)){
while((getline gls < "'$4'""_new_globals.txt") > 0){
if(grp[1] == gls){
diff_started = 1
next
}
}
}
if($1!="extern"){
if(match($0, glo_judge, grp)){
print grp[1] >> "'$4'""_new_globals.txt"
}
print($0)
}
diff_started = 1
}
if(NR==func_end){
cur+=2
}
}
' $1
}
parse_src_to_regions $srcfile \
| get_regions_from_ranges $1 $2 \
| get_start_to_end $srcfile \
| print_by_ranges $srcfile $1 $2 src_output.c
srcfile=new.c
diff=diff.txt
function parse_src_to_regions()
{
gawk '
{
judge = $0~/^[0-9a-zA-Z_]+(\[|\]|\*)*\s+(\[|\]|\*)*[0-9a-zA-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)
}
}
' $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~/^\s*\/\//){
next
}
if($0~/^\s*\/\*/){
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 $5`
whole=""
for range in ${ranges[*]}; do
whole=$whole$range":"
done
gawk '
BEGIN{
cur=1
str="'$whole'"
split(str, strs, ":")
diff_start = '$2'
diff_end = '$3'
diff_started = 0
have_region=1
}
{
if(cur+1<=length(strs)){
func_start = strs[cur]
func_end = strs[cur+1]
have_region = 1
}else{
func_start = 8888888
func_end = -1
have_region = 0
}
if(have_region==1){
if (func_end < diff_start){
cur+=2
next
}else if (func_start > diff_end){
exit
}
}
if(NR<=func_end && NR>=func_start){/*in-function modification*/
print($0)
diff_started = 1
}else if (NR < func_start && NR>=diff_start && NR<=diff_end){/* handle globals */
if((diff_started==0 && $0~/^\s+/) || (NR==diff_end && $0!~/;\s*$/)){
print("'$srcfile'"" line "NR": //error: parse_err=1, cannot handle partial global modification.")
exit
}
glo_judge = /^[0-9a-zA-Z_]+[\[\]\*]*\s+([0-9a-zA-Z_]+)/
glo_judge2 = /^extern\s+[0-9a-zA-Z_]+[\[\]\*]*\s+([0-9a-zA-Z_]+)/
if(match($0, glo_judge2, grp)){
while((getline gls < "'$4'""_new_globals.txt") > 0){
if(grp[1] == gls){
diff_started = 1
next
}
}
}
if($1!="extern"){
if(match($0, glo_judge, grp)){
print grp[1] >> "'$4'""_new_globals.txt"
}
print($0)
}
diff_started = 1
}
if(NR==func_end){
cur+=2
}
}
' $1
}
parse_src_to_regions $srcfile \
| get_regions_from_ranges $1 $2 \
| get_start_to_end $srcfile \
| print_by_ranges $srcfile $1 $2 src_output.c