Operating System - OpenVMS
1751765 Members
5159 Online
108781 Solutions
New Discussion юеВ

extract info from C header files

 
Barry McClintock
Occasional Contributor

extract info from C header files

I have *.h files that I'd like to process into a configuration file which has fieldname/datatype/size and optionally offset.

I've looked at PERL modules but I can't find anything that does this simply.
6 REPLIES 6
Hein van den Heuvel
Honored Contributor

Re: extract info from C header files


How about using the C compiler to generate a full listing with symbols and just parse that output?

For example a C source with:

$ type tmp.c
#include
#include

and a perl parser:

$ type h.pl
print "\n-- $1 --\n" if (/\sTag: (struct \w+)/);
if (/(\s*)(\S+)\s+\d+\s+(\d+\s\s|\d+\sbits).*Offset=([\.0-9]+).*\)\s+(.*)$/) {
$union_offset = $4."+" if ($5 eq "union ");
$union = (length($1) > 1) ? $union_offset : "";
printf "$1$2/$5/$3/$union$4\n";
}

With the following command:

$ pipe cc/list=sys$output/show=symbols tmp | perl -n h.pl

Will deliver:

-- struct fabdef --
fab$b_bid/unsigned char /1 /0
fab$b_bln/unsigned char /1 /1
fab$r_ifi_overlay/union /2 /2
fab$w_ifi/unsigned short /2 /2+0

:
fabdef$$_fill_9/unsigned int /4 /76

-- struct namdef --

-- struct rab64def --
rab64$b_bid/unsigned char /1 /0
rab64$b_bln/unsigned char /1 /1
:
rab64$r_rfa_overlay/union /6 /16
rab64$w_rfa/array of unsigned short /6 /16+0
rab64$r_rfa_fields/struct /6 /16+0
rab64$l_rfa0/unsigned int /4 /16+0
rab64$w_rfa4/unsigned short /2 /16+4
:

Now there are may be a few things to potentially fixup, around dealing spaces, and with bits and offsets in unions, but maybe this is all you need already ?!

If you do want more, your first step will probably be to put the 'automatic' $1 .. $5 variables in names variables like:

$spaces=$1; $name=$2; $size=$3; $offset=$4; $type=$5

hth,
Hein.
Barry McClintock
Occasional Contributor

Re: extract info from C header files

I really do appreciate your help. This is a little deep for me so I'll be working on it a bit.

Regards, Barry
Hein van den Heuvel
Honored Contributor

Re: extract info from C header files


>> This is a little deep for me

Ooops, I read "I've looked at PERL modules" and assumed a certain familirity.

The example given should work. Does it not?

Anyway, let me explain a little bit.
The C listing file, when /show=symb is active will have entries like:

#fabdef 399 80 Long Tag: struct fabdef
# fab$b_bid 401 1 Byte Member: (Offset=0) unsigned char
# fab$b_bln 402 1 Byte Member: (Offset=1) unsigned char
# fab$r_ifi_overlay 412 2 Byte Member: (Offset=2) union
# fab$r_ifi_bits 411 2 Byte Member: (Of
fset=0) struct

So now for the script. First the struct name. This is picked up by:
print "\n-- $1 --\n" if (/\sTag: (struct \w+)/);

Find the text "Tag: "... start remembering "(" if there is "struct " followed by some number of word characters (\w+). Stop remembering ")". Print what was remembered in $1.
If we wanted the structure size, the change the line to look for decimals (line number), whitspace (\s+) and remember the next set of decimals: ( (\d+) ):
print "\n-- $2 -- $1 bytes --\n" if (/\d+\s+(\d+).*Tag: (struct \w+)/);
and it will print:
-- struct fabdef -- 80 bytes --

The main line is admittedly convoluted.

if (/(\s*)(\S+)\s+\d+\s+(\d+\s\s|\d+\sbits).*Offset=([\.0-9]+).*\)\s+(.*)$/) {

Here are some parts:

(\s*) = Remember the first whitespace in $1
(\S+) = Remember the non-whitespace (name!) in $2
\s+\d+\s+ = look for, but ignore whitespace-decimal-whitespace
(\d+\s\s|\d+\sbits) = Look for and remember in $3 (size!) some decimals + whitespace OR decimals plust " bits"
.*Offset=([\.0-9]+).*\) = 'anything' followed by "Offset=" then remember in $4 (offset) any sequence of numbers or a period
\s+(.*)$/ = finally look for a space (after those numbers) then remember everything up to the end of line in $5 (type).

The next lines deal with the fact that the listing counts offsets from union starting with 0 in that union.

hth,
Hein.



Barry McClintock
Occasional Contributor

Re: extract info from C header files

I've been breaking out your REGEX and want to thank you again for your help.

I've worked with regular expressions but have never had the courage to do one this complex...mine are in a million steps!

Regards
labadie_1
Honored Contributor

Re: extract info from C header files

If you want to better understand the regular expressions, test them and so, get a Linux or Windows box, install Python, Pyqt and SIP, and use kodos

http://kodos.sourceforge.net/

see an example at
http://kodos.sourceforge.net/help/example.html

ps: ignore my attachment
Barry McClintock
Occasional Contributor

Re: extract info from C header files

Mr. van Den Heuvel gave a very good suggestion that is working fine.