Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!usc!ucsd!ucbvax!bloom-beacon!eru!hagbard!sunic!news.funet.fi!tukki.jyu.fi!tukki!tt From: tt@tarzan.jyu.fi (Tapani Tarvainen) Newsgroups: comp.lang.c Subject: Re: Argument declaration style (Was: ANSI C prototypes) Summary: sed script to extract function definitions Message-ID: Date: 7 Nov 90 11:19:29 GMT References: <1990Nov2.030556.27759@ccu.umanitoba.ca> <3933.27353319@cc.helsinki.fi> <_1X6_32@xds13.ferranti.com> <3944.27367fb2@cc.helsinki.fi> <1990Nov06.233654.29974@dirtydog.ima.isc.com> Sender: news@tukki.jyu.fi (News articles) Organization: University of Jyvaskyla Lines: 39 In-Reply-To: bzs@world.std.com's message of 7 Nov 90 01:58:43 GMT In article bzs@world.std.com (Barry Shein) writes: > It's unfortunate that grep doesn't let you specify what is to be > listed when a hit occurs, the perfect thing would be something like: > > grep ^foo( -L .-1,. *.c > > which would list the previous line and the hit (thus giving you > the type and the declaration) or even: > > grep ^foo( -L .-1,/^{/-1 *.c Try this: #!/bin/sh # EXDEF # extract C function definitions & declarations # usage: exdef function file(s) # Tapani Tarvainen 7 November 1990 f=$1 shift sed -e '/^[a-zA-Z_]/!d' -e ':L' -e '/^[^(]*[{;]/d' -e '/(/bR' -e N -e bL \ -e :R -e '/);/!{/{/!{' -e N -e bR -e '}' -e '}' -e "/$f(/!d" $@ Then exdef foo *.c should do what you want, with both old and new style. I just wrote that and it obviously isn't perfect, e.g., certain characters in comments within can cause trouble. Anyway, it prints function definitions from the type up to the { and declarations up to the semicolon, regardless of how many lines they occupy. If you want only definitions or only declarations it's easy enough to change (except it doesn't distinguish function typedef's from declarations; add -e '/^typedef/d' before $@ if you don't want them). -- Tapani Tarvainen (tarvaine@jyu.fi, tarvainen@finjyu.bitnet)