Introduction

coccigrep is a semantic grep for the C language based on coccinelle. It can be used to find where a given structure is used in code files. coccigrep depends on the spatch program which comes with coccinelle.
Download and source
Latest version is 1.13: coccigrep-1.13.tar.gz
The source can be accessed via github.
Examples
To find where in a set of files the structure named Packet is used, you can run:
$ coccigrep -t Packet *c
source-af-packet.c:272: p = ptv->in_p;
source-af-packet.c:300: p->datalink = ptv->datalink;
source-af-packet.c:758: switch(p->datalink) {
To find where in a set of files the datalink attribute is used in the structure
named Packet, you can simply do:
$ coccigrep -t Packet -a datalink *c
source-af-packet.c:300: p->datalink = ptv->datalink;
source-af-packet.c:758: switch(p->datalink) {
source-erf-dag.c:525: p->datalink = LINKTYPE_ETHERNET;
If you want to be more precise and find where this attribute is set, you can use
the operation flag (-o). One of its value is set which indicate we only want
the match where the attribute is set:
$ coccigrep -t Packet -a datalink -o set source*c source-af-packet.c:300: p->datalink = ptv->datalink; source-erf-dag.c:525: p->datalink = LINKTYPE_ETHERNET;
coccigrep supports syntax highlighting through the pygments module. For example, running coccigrep -t Packet -a datalink -o test -c -A 3 -B 3 -f html /tmp/test.c will output to stdout some colorized HTML code:
/tmp/test.c: l.300 -3, l.300 +3, Packet *p
hdrp->sll_protocol = from.sll_protocol;
}
<span style="color: #008000; font-weight: bold">while</span> (<span style="color: #0000FF">p</span><span style="color: #666666">-></span>datalink <span style="color: #666666">>=</span> ptv<span style="color: #666666">-></span>datalink) {
SET_PKT_LEN(<span style="color: #0000FF">p</span>, caplen <span style="color: #666666">+</span> offset);
<span style="color: #008000; font-weight: bold">if</span> (PacketCopyData(<span style="color: #0000FF">p</span>, ptv<span style="color: #666666">-></span>data, GET_PKT_LEN(<span style="color: #0000FF">p</span>)) <span style="color: #666666">==</span> <span style="color: #666666">-1</span>) {
TmqhOutputPacketpool(ptv<span style="color: #666666">-></span>tv, <span style="color: #0000FF">p</span>);