On Tue, 12 Oct 2010, Aaen, Andreas.Bach wrote:

> I have a number of old C programs which store binary data based on a struct. 
> Newer versions of the same program uses an updated struct.
> Now I need to create a tool to update stored binary data to the new format.
> 
> So if you have a struct
> 
> typdef struct custom {
> int a;
> dword b;
> byte c;
> }
> 
> Then it would be nice if you could create a Cocinelle expression that parses 
> this struct and prints out "a b c".
> This could then easily be changed to offsetof(custom,a); offsetof(custom,b); 
> offsetoff(custom,c); and finally create a new C-program that provide you with 
> all the offsets of the structured data as defined by the pragma settings and 
> the compiler.
> 
> if you look at the metadecl possibilities that Coccinelle support, then how 
> does it parse a struct?
> 
> a Coccinelle spatch like this:
> ---------------------
> @r@
> identifier v;
> type t;
> @@
> typedef struct custom {
> ...
> t v;
> ...
> }


Typedef needs a name and a ; after it (typedef struct x { ... } y;)

But maybe you don't need the typedef?  The following parses as well:

@r@
identifier v;
type t;
@@
struct custom {
...
t v;
...
}

@ script:python @
t << r.t;
v << r.v;
@@
print "type" . t . "name" . v;

julia

> @ script:python @
> t << r.t;
> v << r.v;
> @@
> print "type" . t . "name" . v;
> ---------------------
> 
> obviously doesn't work, as it can't parse, but it should give an idea about 
> what I am trying to obtain.
> 
> Regards
> Andreas Bach Aaen
> 
_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to