> Hi!
> I want to add debug macros to my C source files. I want the word TRACE
> to show up after every '{' and ';' unless ';' is preceeded by 'return'
> or 'int' or 'float' etc...
> Here's what I got so far:
> #!/bin/sh
> find /home/epkolbl/workspace/rsd2/ -name '*.c' | while read file
> do
> awk '{ print $0 }' $file | sed 's/;/;TRACE/g' | sed 's/{/{TRACE/g'
> done
> However this piece of script adds TRACE after ';' even though it is
> preceeded by 'return' etc.
> I have run out of ideas. Could someone please help me?
For starters, throw out awk here. You can give multiple commands
to sed, and sed will execute each in turn on each line.
sed -e 's/;/;TRACE/g' -e 's/{/{TRACE/g' $file
Then make sure you save the file. Getting the whole shebang on the screen
is OK while learning shell programming, but if you also plan to compile
it...
find /home/epkolbl/workspace/rsd2 -name \*.c |
while read file; do
# keep the original file with extension -orig.c
tmpfile=${file%.c}-trace.c
bckfile=${file%.c}-orig.c
sed -e 's/;/;TRACE/g' -e 's/{/{TRACE/g' $file > $tmpfile &&
mv -f $file $bckfile &&
mv -f $tmpfile $file
done
Then let's see what we can do with the int, return and float.
I am afraid that if you want a fully correct parsing of the files
you will have to yank a few tens of thousand lines of code from the
gcc compiler, and use that to make you own rewriter.
But if you can manually ensure that the file does not contain
multiple statements on the same line when one or more of them
are declarations or returns, and if you can manually ensure that
all declarations have ';' on the same line as the 'int' or
'float', then we can do it with an approximation. Also look
out for "myinteger = (int) *chrpointer;" - you will not get
TRACE here either. Other things that will not work as you
probably want are
typedef int i32_t;
i32_t variable;
You probably don't want TRACE after the declaration
of "variable".
int my_array[] = {
ONE, /* A well known constant numeral */
TWO /* Another hero of the evereday life */
}; /* to TRACE or not to TRACE, that's the maze. */
But given enough caveats, try this code:
LC_ALL=C sed -e
'/\(^\|[^_0-9A-Za-z]\)\(int\|float\|return\)\([^_0-9A-Za-z]\|$\)/ {
p
d
}' -e 's/;/;TRACE/g' -e 's/{/{TRACE/g' $file > $tmpfile &&
The four lines just above replace exactly one line in the code
further up. You do need the other lines of the command further
up. If you include only almost all the backslashes in your copy,
dont expect it to almost work.
-Enrique