#!/bin/sh
#
# Script to generate an HTML index of all C code from the current directory
# downwards (skipping directories ending in ~). The header comment in each
# file is listed, and each function's prototype and comment are given. A
# list of "TODO:" comments is also generated.
#
# Outputs the HTML on standard output.
#
# If a parameter is given, it is the prefix to put before any "view file"
# links, eg ".." to link to "../dir/file.c" instead of "dir/file.c".
#
# Skips any files containing the string !NOINDEX.
#
# Requires ctags and cproto.
#
command -v "ctags" >/dev/null 2>&1 || { echo "${0##*/}: ctags: command not found" 1>&2; exit 1; }
command -v "cproto" >/dev/null 2>&1 || { echo "${0##*/}: cproto: command not found" 1>&2; exit 1; }
linkPrefix=$1
test -n "${linkPrefix}" || linkPrefix="."
# Add "/" to the link prefix, and empty it entirely if it ends up just "./".
linkPrefix=$(echo "${linkPrefix}" | sed 's,/*$,,')
test "$linkPrefix" = "." && linkPrefix="" || linkPrefix="${linkPrefix}/"
# Convert the given string to HTML-escaped values (<, >, & escaped) on
# stdout.
#
html_safe () {
echo "$*" \
| sed -e 's|&|\&|g;s|<|\<|g;s|>|\>|g'
}
# Convert the given string to HTML-escaped values (<, >, & escaped) on
# stdout, also adding a
to the end of each line.
#
html_safebr () {
echo "$*" \
| sed -e 's|&|\&|g;s|<|\<|g;s|>|\>|g;s/$/
/'
}
allEligibleFiles=$(
find . -name '*~' -prune -o -type f -name '*.c' \
-exec grep -FL '!NOINDEX' /dev/null '{}' ';'
)
ctagsOutput=$(
echo "${allEligibleFiles}" \
| ctags -nRf- -L- --c-types=f \
| sed 's/ .\// /;s/;" .*$//'
)
sourceFiles=$(echo "${ctagsOutput}" | cut -d ' ' -f 2 | sort | uniq)
echo '
\0'
echo ''"${sourceFile}"' | '
echo '- | ' echo ''"$(html_safe "${fileShortDescription}")"' | ' echo '
' html_safebr "${fileLongDescription}" echo '' if [ -n "${functionPrototypes}" ]; then echo '
Functions defined:
' echo '/;s|$|[' echo 'Top |' echo 'To Do |' echo 'Functions ]
' done echo ''"${functionName}"'' \
'['"${sourceFile}"']'
echo ''"${functionName}"'' \
'['"${sourceFile}"']'
echo '
'"$(html_safe "${functionPrototype}")"'
' html_safebr "${functionHeaderComment}" echo '' echo '
[' echo 'Top |' echo 'To Do |' echo 'Files ]
' done echo ''"${sourceFile}"''
echo '