7
Prototyping
The prototype utility program is a shell script which reads a C file and adds the signatures of all the public functions it defines in its header file.
The test code signaled by a #if defined(STANDALONE)
is ignored.
You can easily change the marker in the script.
prototype can be safely used with an existing header file.
The command will not overwrite the definitions already present in the file.
It only changes all the lines between #ifdef __STDC__
and #endif
.
NOTE: The header file must already exists.
- PATH=/bin:/usr/bin
- TMP=/tmp/proto$$
- HDIR=
- trap "rm -f $TMP" SIGINT SIGQUIT
- proto ()
- {
- for f in $*
- do
- echo "#ifdef __STDC__"
- cat $f | sed -e '/^#if.*STANDALONE/,$d' | awk '/^[A-Za-z].*\(.*\) {.*$/ && !/static/ && !/typedef/ {\
- printf( "extern %s;\n", $0 ); }' | sed -e 's/ {//'
- echo "#else"
- cat $f | sed -e '/^#if.*STANDALONE/,$d' | awk '/^[A-Za-z].*\(.*\) {.*$/ && !/static/ && !/typedef/ {\
- printf( "extern %s;\n", $0 ); }' | sed -e 's/(.*$/();/'
- echo "#endif"
- done
- }
- for file_c in $*
- do
- if [ ! -f $file_c ]; then echo "$file_c?"; exit 1; fi
- file_h=`basename $file_c .c`.h
- if [ ! -f $file_h ]; then
- if [ "$HDIR" != "" ]; then
- if [ ! -f $HDIR/$file_h ];
- then echo "$file_h?"; exit 1
- else file_h=$HDIR/$file_h
- fi
- else echo "$file_h?"; exit 1
- fi
- fi
- proto $file_c >$TMP
- if [ `grep -c '__STDC__' $file_h` != 0 ]
- then
- >/dev/null ed $file_h <<-__A_MARK__
- /__STDC__/,/endif/d
- -1r $TMP
- w
- q
- __A_MARK__
- else
- >/dev/null ed $file_h <<-__A_MARK__
- $
- ?endif?-1r $TMP
- w
- q
- __A_MARK__
- fi
- done
Terminates by deleting the temporary file.
- rm -fr $TMP
- exit 0
Comments