summaryrefslogblamecommitdiffstats
path: root/bin/grepc
blob: 427a416f71a08c3a6f4ac4ec15203f1a029c9ef3 (plain) (tree)
1
2
3
4
5
6
7
8
9


           





                                          
                       

                        
                     

                                
                
                                             



                                                


                                

                                   
                                                   


 

                           
                                


                                             




                      

                                


 

                              
                                
                     
                           
                                                                          


 

                          

                                
               
                                                                                                              




                         

                                
               
                                                                                                      




                     

                             


 
                                   
 
                                

                                                 
                                                         


 
                                  
 

                                   
               
                                               


 
                      
 

                                      


 











                                              

                                       

                                          
               
                                                                                                                                 


 
                                    
 
                                
                           
                   
                                         




                                               
                                


                                                                                                   






                                                          

                                                                        
                                                                           
                          

                                                     





                             
                                       

                                                             




                     

                                          




               
                         
                                 
                        
                         
                         
                        


 
          
#!/bin/bash


if (($# != 1)); then
	>&2 echo "Usage: $0 <identifier>";
	exit 1;
fi;


function grepc_helper()
{
	find . -type f \
	| grep "$1" \
	| xargs grep -lPI "$2" \
	| xargs grep -lP  "$3" \
	| sort \
	| xargs pcregrep -Mn "$4" /dev/null \
	| sed -E 's/^[^: ]+:[0-9]+:/\n\n&\n\n/';
}


function grepc_macro_simple()
{
	grepc_helper '\.[ch]$' \
	  "#\s*define\s+$1\b[^(]" \
	  "." \
	  "(?s)#\s*define\s+$1\b(?!\().*?[^\\\\]$";
}


function grepc_macro_func()
{
	grepc_helper '\.[ch]$' \
	  "#\s*define\s+$1\(" \
	  "." \
	  "(?s)#\s*define\s+$1\(.*?[^\\\\]$";
}


function grepc_macro()
{
	grepc_macro_simple "$1";
	grepc_macro_func "$1";
}


function grepc_enum_constant()
{
	grepc_helper '\.[ch]$' \
	  "^enum\s" \
	  "^\s*$1\s*[,=]" \
	  "(?s)\benum\b\s*([\w\s[\]]|::)*\s*{[^}]*^\s*$1\s*[=,].*?^}.*?;";
}


function grepc_func_decl()
{
	grepc_helper '\.[ch]$' \
	  "\b$1\s*\(" \
	  "." \
	  "(?s)^[\w[]([\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**$1\s*\([\w\s\(,\)[\]*]+?(...)?\)[\w\s\(,\)[:\]]*;";
}


function grepc_func_def()
{
	grepc_helper '\.[ch]$' \
	  "\b$1\s*\(" \
	  "." \
	  "(?s)^[\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+\**$1\s*\([\w\s\(,\)[\]*]+?(...)?\)\s*{.*?^}";
}


function grepc_func()
{
	grepc_func_decl "$1";
	grepc_func_def "$1";
}


function grepc_linux_syscall_decl()
{
	grepc_helper '\.[ch]$' \
	  "^asmlinkage\s+[\w\s]+\**sys_$1\s*\(" \
	  "." \
	  "(?s)^asmlinkage\s+[\w\s]+\**sys_$1\s*\(.*?\)";
}


function grepc_linux_syscall_def()
{
	grepc_helper '\.c$' \
	  "SYSCALL_DEFINE.\($1\b" \
	  "." \
	  "(?s)^\w*SYSCALL_DEFINE.\($1\b.*?^}";
}


function grepc_linux()
{
	grepc_linux_syscall_decl "$1";
	grepc_linux_syscall_def "$1";
}


function grepc_glibc_math()
{
	grepc_func_def "M_DECL_FUNC \(__$1\)";
}


function grepc_glibc()
{
	grepc_glibc_math "$1";
}


function grepc_type_struct_union_enum()
{
	grepc_helper '\.[ch]$' \
	  "\b(struct|union|enum)\s+$1\b" \
	  "." \
	  "(?s)^(?!^\s*typedef\b)([\w[]([\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+)?\b(struct|union|enum)\s+$1\b\s*[\w\s[\]]*{.*?^}.*?;";
}


function grepc_type_typedef_simple()
{
	grepc_helper '\.[ch]$' \
	  "^\s*typedef\s" \
	  "\b$1;" \
	  "(?s)^\s*typedef\s+[^{};]+$1;";
}


function grepc_type_typedef_struct_union_enum()
{
	grepc_helper '\.[ch]$' \
	  "^\s*typedef\s+(struct|union|enum)\b[^;]*$" \
	  "^(\s\s)?}\s*$1;" \
	  "(?s)^\s*typedef\s+(struct|union|enum)\s+(?:(?!^(\s\s)?}|^\s*typedef).)*^(\s\s)?}\s*$1;";
}


function grepc_type_typedef_underlying_struct_union_enum()
{
	find . -type f \
	| grep '\.[ch]$' \
	| xargs grep -hP "^\s*typedef\s+(struct|union|enum)\s+.*\b$1;" \
	| sed -E -e 's/^\s*typedef\s+//' -e "s/\s*\**\b$1;.*//" \
	| sed -E -e 's/^struct\s+//' -e 's/^union\s+//' -e 's/^enum\s+//' \
	| while read t; do
		test "$1" != "$t" \
		&& grepc_type_struct_union_enum "$t";
	done;
}


function grepc_type_typedef()
{
	grepc_type_typedef_simple "$1";
	grepc_type_typedef_struct_union_enum "$1";
	grepc_type_typedef_underlying_struct_union_enum "$1";
}


function grepc_type()
{
	grepc_type_struct_union_enum "$1";
	grepc_type_typedef "$1";
}


function main()
{
	grepc_macro "$1";
	grepc_enum_constant "$1";
	grepc_func "$1";
	grepc_linux "$1";
	grepc_glibc "$1";
	grepc_type "$1";
}


main "$1";