> Is there and easy way to get the text between the external braces?
You can match matched braces with the (?R) construct introduced in perl
5.10, but you have to be careful about braces inside quotes and
comments. (You can probably ignore the possibility of unmatched braces
inside macro definitions and expansions.)
If you can assume the C is 'properly' formatted, of course, it becomes
much easier: the only time you get an open brace in the first column is
when it is starting a function, and the end of the function is the next
close brace which is also in the first column. Depending on what you're
trying to do you could consider running the C through indent(1) with
some appropriate set of options, since that has something approximating
a C parser.
On 2012-10-01, Eli the Bearded <*...@eli.users.panix.com> wrote:
> In comp.lang.perl.misc, <jose.luis.fdez.d...@gmail.com> wrote:
>> Given the function name, is there an easy way to extract the function
>> text from a C file in Perl?
> You are in a world of hurt if the programmer hates you.
>:r macro.c
> #include <stdio.h>
> #define PROGRAMMER_HATES_YOU }
Note that such buffoonery in the source will also likely defeat numerous
popular programming tools like cscope, mkid and ctags. Oh, not to mention
syntax highlighting and automatic code indentation in your text editor, and the
reporting of function names in context diff hunks.
> In comp.lang.perl.misc, <jose.luis.fdez.d...@gmail.com> wrote:
> > Given the function name, is there an easy way to extract the function
> > text from a C file in Perl?
> You are in a world of hurt if the programmer hates you.
> :r macro.c
> #include <stdio.h>
> #define PROGRAMMER_HATES_YOU }
So run it through cpp before trying to parse it... Whether that's useful
or not depends on what exactly the OP is trying to do with these
function bodies, which he hasn't said.
> On 2012-10-01, Eli the Bearded <*...@eli.users.panix.com> wrote:
> > In comp.lang.perl.misc, <jose.luis.fdez.d...@gmail.com> wrote:
> >> Given the function name, is there an easy way to extract the function
> >> text from a C file in Perl?
> > You are in a world of hurt if the programmer hates you.
> >:r macro.c
> > #include <stdio.h>
> > #define PROGRAMMER_HATES_YOU }
> Note that such buffoonery in the source will also likely defeat numerous
> popular programming tools like cscope, mkid and ctags. Oh, not to mention
> syntax highlighting and automatic code indentation in your text editor, and the
> reporting of function names in context diff hunks.
though in all cases when these macros are used correctly the visible
braces and brackets end up balancing. (The STMT_END definition is
particularly evil...)