#!/bin/bash # # Copyright 2009 Joe Perches # Copyright 2010 Luis R. Rodriguez # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # Lets you remove typedefs, or standard data type redefinitions # from C/header files. # # We do simple sed substituation for logical places where you would # use the typedef, and also replace the typedef declaration to a simple # struct declaration. function usage() { echo "Usage $0 [-s] " exit 1 } 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 # 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 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 } function convert_standard_datatype() { sed -r -i -e "s/\b$from\b/$to/g" $1 } if [[ $# -lt 3 ]]; then usage fi STD="" if [[ $1 -eq "-s" ]]; then STD="y" shift 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 \*.[ch]); do if [[ $STD -eq "y" ]]; then convert_standard_datatype $i else remove_typedef $i fi done done