3

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.

  1. PATH=/bin:/usr/bin
  2. TMP=/tmp/proto$$
  3. HDIR=
  4.  
  5. trap "rm -f $TMP" SIGINT SIGQUIT
  6.  
  7. proto ()
  8. {
  9.     for f in $*
  10.     do
  11.         echo "#ifdef __STDC__"
  12.         cat $f | sed -e '/^#if.*STANDALONE/,$d' | awk '/^[A-Za-z].*\(.*\) {.*$/ && !/static/ && !/typedef/ {\
  13.         printf( "extern %s;\n", $0 ); }' | sed -e 's/ {//'
  14.         echo "#else"
  15.         cat $f | sed -e '/^#if.*STANDALONE/,$d' | awk '/^[A-Za-z].*\(.*\) {.*$/ && !/static/ && !/typedef/ {\
  16.         printf( "extern %s;\n", $0 ); }' | sed -e 's/(.*$/();/'
  17.         echo "#endif"
  18.     done
  19. }
  1. for file_c in $*
  2. do
  3.     if [ ! -f $file_c ]; then echo "$file_c?"; exit 1; fi
  4.  
  5.     file_h=`basename $file_c .c`.h
  6.     if [ ! -f $file_h ]; then
  7.         if [ "$HDIR" != "" ]; then
  8.             if [ ! -f $HDIR/$file_h ];
  9.             then echo "$file_h?"; exit 1
  10.             else file_h=$HDIR/$file_h
  11.             fi
  12.         else echo "$file_h?"; exit 1
  13.         fi
  14.     fi
  15.  
  16.     proto $file_c >$TMP
  17.  
  18.     if [ `grep -c '__STDC__' $file_h` != 0 ]
  19.     then
  20.         >/dev/null ed $file_h <<-__A_MARK__
  21. /__STDC__/,/endif/d
  22. -1r $TMP
  23. w
  24. q
  25. __A_MARK__
  26.     else
  27.         >/dev/null ed $file_h <<-__A_MARK__
  28. $
  29. ?endif?-1r $TMP
  30. w
  31. q
  32. __A_MARK__
  33.     fi
  34. done

Terminates by deleting the temporary file.

  1. rm -fr $TMP
  2. exit 0

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].