[PATCH v2] scripts: add typdef removal tool

Jiri Slaby jirislaby at gmail.com
Fri Aug 21 08:48:51 UTC 2009


Hi!

On 08/17/2009 08:02 AM, Amerigo Wang wrote:
...
>> --- /dev/null
>> +++ b/scripts/remove-typedef
>> @@ -0,0 +1,70 @@
>> +#!/bin/bash
>> +#
>> +# Copyright 2009 Joe Perches <joe at perches.com>
>> +# Copyright 2009 Luis R. Rodriguez <mcgrof at gmail.com>
>> +#
>> +# Lets you remove typedefs from C/header files.
>> +#
>> +# We do simple sed substituation for logical places where you would

substitution

>> +function remove_typedef()
>> +{
>> +	if [ ! -f $1 ]; then
>> +		return;
>> +	fi
>> +
>> +	# This replaces the typedef usages with simple structs
>> +	sed -r -i -e "s/\b$from\b/struct $to/g" $1
>> +	sed -r -i -e "s/\bP$from\b/struct $to \*/g" $1
>> +	sed -r -i -e "s/struct $to\s*\*\s*\b/struct $to \*/g" $1
>> +	sed -r -i -e "s/\(struct $to\s*\*\)\s*/\(struct $to \*\)/g" $1

Could that be one sed with multiple -e's?

>> +	# This replaces the typedef declaration for a simple struct declaration - style 0
>> +	perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+([\d\D]+?)\s*\{([\d\D]+?)\}\s*struct\s+$to\b[^;]*;/struct $to \{\2\};/g; print; }" $1

This is safe until bash devs start to use $/ as something. It should be
escaped.

>> +	# This replaces the typedef declaration for a simple struct declaration - style 1
>> +	perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+_$from\s*\{([\d\D]+?)\}\s*struct\s+$to\b[^;]*;/struct $to \{\1\};/g; print; }" $1
>> +
>> +	# This replaces the typedef declaration for a simple struct declaration - style 2
>> +	perl -i -e "local $/; while(<>) { s/\btypedef\s+struct\s+$to\s*\{([\d\D]+?)\}\s*$from\b[^;]*;/struct $to \{\1\};/g; print; }" $1

And possibly one
perl -ipe "BEGIN { \$/ = '' };
	s/x/y/g;
	s/y/z/g;"
?

Or do the sed work by perl too?

>> +}
>> +
>> +if [[ $# -lt 3 ]]; then

Could that be [ ] instead of [[ ]]?

>> +	usage
>> +fi
>> +
>> +from=$1
>> +to=$2
>> +shift
>> +echo "Converting typedef $from to struct $to"
>> +
>> +while shift; do
>> +	REM_PATH=$1
>> +
>> +	if [ -z $REM_PATH ]; then
>> +		continue;
>> +	fi
>> +
>> +	if [ ! -d $REM_PATH ]; then
>> +		echo "No directory $REM_PATH";
>> +		continue;
>> +	fi
>> +
>> +	for i in $(find $REM_PATH -type f -name *.c); do
>> +		remove_typedef $i
>> +	done
>> +
>> +	for i in $(find $REM_PATH -type f -name *.h); do
>> +		remove_typedef $i
>> +	done
> 
> These two 'find's can be merged:
> 
>   find $REM_PATH -type f -name '*.c' -o -name '*.h'

Yes, please enclose those wildcards into ''. Also REM_PATH can be with
spaces, use "" for it, $i and $1 etc.



More information about the devel mailing list