kratos
1
hi,
i need to write a bash script that does two things.
the program will take from the command line a file name, which is a C code, and an integer, which is the size of my indentation
i would then have to indent every nested code by the number of columns provided by the user in the command line
for the next step, i have to identify all #define's and convert from lowercase to uppercase whatever is being defined.
for example,
#define print(x,y)
would become
#define PRINT(x,y)
and from then on, every occurrence of print should appear as PRINT
i would appreciate any help
thanks,
otheus
2
The first step you can do with either indent or expand. Shell script is finished.
The second step is trickier -- especially since you theoretically must concern yourself with conditional defines. So if you have:
#if ONE_WAY
# define PRINT(x,y)
#else
# define PRINT_T(x,y)
#endif
So you can try this type of thing:
perl -ne '/^[[:space:]]*#[[:space:]]*define[[:space:]]*(\w+)/ && print $1," ",uc($1),"\n";' >subwords.tmp
Now you have a file "subwords.tmp" that you have to do a gobal search/replace for in the source files. A slow, but simple way is this:
cat subwords.tmp |
while read word replacement; do
perl -ipe "s/\b${word}\b/\b${replacement}\b/g" source.c
done
Make sure you make backup copies before trying any of this.