Simple #include dependency printer.

  • Doesn't handle circular dependencies
  • Searches for files in the current directory only
#!/bin/sh

# Prints a #include dependency tree
# Reads arguments from either command line or stdin (but not both)

DEPTH=

extract_filename()
{
    sed 's/\W\*#include\W["<]\([^">]\*\)[">].*$/\1/g'
}

extract_includes()
{
    DEPTH="$DEPTH    ";
    while read current; do
        echo "$DEPTH$current"
        grep '#include' "$current" | extract_filename | extract_includes 2> /dev/null
    done
}

# read from commandline
for FILE in $@; do
    echo "$FILE:"
    echo "$FILE" | extract_includes
done

# read from stdin
if [ -z $@ ]; then
    extract_includes
fi