home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / fdecs < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  5.2 KB

  1. Path: wugate!wucs1!uunet!allbery
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Newsgroups: comp.sources.misc
  4. Subject: v06i073: fdecs - generate pANS prototypes from K&R C code
  5. Message-ID: <8903141605.AA01288@m10ux>
  6. Date: 22 Mar 89 05:36:39 GMT
  7. Sender: allbery@uunet.UU.NET
  8. Reply-To: mnc@m10ux.UUCP (52171.3-Michael Condict-- MHx5911 (MH0000)M999)
  9. Lines: 235
  10. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  11.  
  12. Posting-number: Volume 6, Issue 73
  13. Submitted-by: mnc@m10ux.UUCP (52171.3-Michael Condict-- MHx5911 (MH0000)M999)
  14. Archive-name: fdecs
  15.  
  16. [My, there's a lot of this kind of thing floating around recently.  ++bsa]
  17.  
  18. This shell script (fdecs) extracts ANSI-C function prototype declarations,
  19. suitable for use in a .h file, from C source files that contain old-style
  20. function definitions.  See the comment at the top of fdecs for more info.
  21.  
  22. The only option is "-Idir", which causes the C source to be run through the
  23. C preprocessor before the prototype extraction is done.
  24.  
  25. Mike Condict
  26.  
  27. ---------------------------- cut here --------------------------------------
  28. #!/bin/sh
  29. # This is a shar archive.
  30. # The rest of this file is a shell script which will extract:
  31. #
  32. # fdecs fdecs1.sed fdecs2.sed fdecs3.sed
  33. #
  34. # To extract the files from this shell archive file simply
  35. # create a directory for this file, move the archive file
  36. # to it and enter the command
  37. #
  38. #    sh filename
  39. # The files will be extracted automatically.
  40. # Note: Do not use csh.
  41. #
  42. # Archive created: Tue Mar 14 10:29:50 EST 1989
  43. #
  44. echo x - fdecs
  45. sed 's/^X//' > fdecs << '@FUNKY STUFF@'
  46.  
  47. # This script, given C source files, finds all the function definitions
  48. # of the form "type_decl f(a,b,...) type_decl a; type_decl b; ... { body }",
  49. # and outputs a file of extern function declarations suitable for a .h file.
  50. # There need not be a type_decl for each argument (defaults to int) and the
  51. # type_decl's can be in a different order from the arg list.  Multiple args
  52. # can be declared in the standard abbreviated fashion: "int a, *b;".
  53. #
  54. # The only restriction is that the type_decl's must be no more complicated
  55. # than what can be formed with type words, names, "*" and "[]".  That is the
  56. # type_decl's may not contain "struct ... {...}" or "float (*x)()" although,
  57. # e.g., "struct A" is allowed.
  58. #
  59. # The output is one line of the form 
  60. #
  61. #    type_decl f(type_decl a, type_decl b, . . . );
  62. #
  63. # for each function f defined in the source files, i.e. a legal ANSI-C
  64. # function prototype.
  65.  
  66. SEDDIR=~mnc/src/fdecs
  67.  
  68. Iflag=""
  69. while [ "$#" -gt 0 ]; do
  70.     case "$1" in
  71.     -I*)    Iflag="$Iflag $1"; shift
  72.         ;;
  73.     -*)    echo "Usage: $0 [ -Iincl_dir ] ... file ..."
  74.         echo "       (use -I only if C preprocessor is wanted)"
  75.         exit 1
  76.         ;;
  77.     *)    break ;;
  78.     esac
  79. done
  80.  
  81. if [ "$#" -eq 0 ] ; then
  82.     sed -f $SEDDIR/fdecs1.sed |
  83.     sed -f $SEDDIR/fdecs2.sed | sed -f $SEDDIR/fdecs3.sed
  84. elif [ "X$Iflag" != X ] ; then
  85.     # User wants to run the C files through the preprocessor, with
  86.     # the specified -I flag.  We delete the contents of the
  87.     # .h files from the preprocessor's output.  We also need to
  88.     # delete any occurrences of '^$filename:', which some versions
  89.     # of cc -E put out at the beginning of every file:
  90.     cc -E $Iflag "$@" | \
  91.         sed -e '#n No automatic printing
  92.  
  93.             # Delete contents of included .h files:
  94.             : chkhdr
  95.             /^# [1-9][0-9]* ".*\.h"/{
  96.                 : delhdr
  97.                     n
  98.                 /^#/!b delhdr
  99.                 b chkhdr
  100.             }
  101.             # Delete "filename:" inserted by "cc -E":
  102.             /^[^     ]*\.[ch]:[     ]*$/d
  103.             ' \
  104.             -f $SEDDIR/fdecs1.sed \
  105.             -e 'p' |
  106.         sed -f $SEDDIR/fdecs2.sed | sed -f $SEDDIR/fdecs3.sed
  107. else
  108.     sed -f $SEDDIR/fdecs1.sed "$@" |
  109.         sed -f $SEDDIR/fdecs2.sed | sed -f $SEDDIR/fdecs3.sed
  110. fi
  111. @FUNKY STUFF@
  112. ls -l fdecs
  113. echo x - fdecs1.sed
  114. sed 's/^X//' > fdecs1.sed << '@FUNKY STUFF@'
  115.  
  116. : morebs
  117. /\\$/ {
  118.     N
  119.     s/\\\n//
  120.     b morebs
  121. }
  122.  
  123. /^[     ]*$/d
  124.  
  125. : delcom
  126. /\/\*/{
  127.     s/@/ /g
  128.     s:/\*:@:
  129.  
  130.     : morecm
  131.     /\*\//!{
  132.         s/@.*/@/
  133.         N
  134.         b morecm
  135.     }
  136.  
  137.     s/\$/ /g
  138.  
  139.     s:\*/:$:
  140.     s/@[^$]*\$/ /
  141.  
  142.     b delcom
  143. }
  144.  
  145. /^#/d
  146.  
  147. s/\\"/ /g
  148. s/"[^"]*"/0/g
  149. s/\\'/ /g
  150. s/'[^']*'/0/g
  151.  
  152. /^[     ]*$/d
  153. @FUNKY STUFF@
  154. ls -l fdecs1.sed
  155. echo x - fdecs2.sed
  156. sed 's/^X//' > fdecs2.sed << '@FUNKY STUFF@'
  157. : delcbrc
  158. /{/{
  159.     s/{[^{}]*/{/g
  160.     : getcbrc
  161.     /}/!{
  162.         N
  163.         s/{[^{}]*/{/g
  164.         b getcbrc
  165.     }
  166.     s/{[^{}]*}/#/g
  167.     b delcbrc
  168. }
  169. s/#/{}/g
  170. @FUNKY STUFF@
  171. ls -l fdecs2.sed
  172. echo x - fdecs3.sed
  173. sed 's/^X//' > fdecs3.sed << '@FUNKY STUFF@'
  174.  
  175.  
  176. : doline
  177.  
  178.  
  179. : getsbr
  180. /[;{]/!{
  181.     N
  182.     b getsbr
  183. }
  184.  
  185. s/\n/ /g
  186. s/[     ][     ]*/ /g
  187.  
  188.  
  189. /;/{
  190.     /^[^;]*) *[a-zA-Z_{]/!{
  191.         s/^[^;]*;//
  192.         /^ *$/d
  193.         b doline
  194.     }
  195. }
  196.  
  197.  
  198. /[;)] *{}/!{
  199.     N
  200.     b doline
  201. }
  202.  
  203. s/^  *//
  204. s/( */(/g
  205. s/ *)/)/g
  206. s/ *, */, /g
  207. s/ *; */; /g
  208. s/ *\*/ \*/g
  209. s/\*  */\*/g
  210.  
  211.  
  212.  
  213. /() *{/b no_arg
  214.     s/, /, %/g
  215.     s/(/(, %/
  216.  
  217.     t more_t
  218.     : more_t
  219.         s/, %\([A-Za-z_][A-Za-z0-9_]*\)\(.*[);] *\)\([A-Za-z_][A-Za-z0-9_{} ]*\)\( *\)\(\**\1[][]*\)\([,;]\)/, \3 \5\2\3\4@\6/
  220.         s/, %\([A-Za-z_][A-Za-z0-9_]*\)\(.*[);] *\)\([A-Za-z_][A-Za-z0-9_{} ]*\)\( [^;]*, %\)\(\**\1[][]*\)\([,;]\)/, \3 \5\2\3\4@\6/
  221.     t more_t
  222.     
  223.     s/(, /(/
  224.     
  225.     s/%/int /g
  226.  
  227.     s?(\([^)]*\)).*?(\1);?
  228.  
  229.     / register/{
  230.         s/\([A-Za-z_0-9]\) register\([^A-Za-z0-9_]\)/\1\2/g
  231.         s/ register\([^A-Za-z0-9_][^A-Za-z0-9_]*[A-Za-z_][A-Za-z0-9_]*\),/ int\1,/g
  232.         s/ register\([^A-Za-z0-9_]\)/ \1/g
  233.     }
  234.  
  235.     s/   */ /g
  236.  
  237.     b done_a
  238. : no_arg
  239.     s/).*/void);/
  240. : done_a
  241. @FUNKY STUFF@
  242. ls -l fdecs3.sed
  243. # The following exit is to ensure that extra garbage 
  244. # after the end of the shar file will be ignored.
  245. exit 0
  246.