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

 
                     
                                        



               
                  
 

                                            
                        





                                  
              


                                        
                
                                             
                                              


 
                    
 
                      
                                   
               
                                                     


 
                  
 
                      
                               
               
                                               


 
             
 

                                


 
                     
 
                      
                     

                                                                             


 
                 
 
                      
                       
               
                                                                                                              


 
                
 
                      
                       
               
                                                                                                      


 
            
 

                             


 
                          
 
                      
                                                 
               
                                                         


 
                         
 
                      
                                   
               
                                                      


 
             
 

                                      


 
                  




                                              
             




                              
                              
 
                      
                                          
               
                                                                                                                                    


 
                           
 
                      
                              
                   
                                            


 
                                      
 
                      
                                                          
                           
                                                                                                  


 
                                                 
 
                                                                                
                                                                 
                                                                           
                             

                                                     



             
                    
 
                                       

                                                             


 
            
 

                                          


 
      
 

                              
                         
                                 
                        
                         
                         
                        


 
          
#!/bin/sh


if [ $# -ne 1 ]; then
	>&2 echo "Usage: $0 IDENTIFIER";
	exit 1;
fi;


grepc_find_files()
{
	files="$(mktemp -t 'grepc.XXXXXX')";

	find . -type f \
	| grep -P '\.[ch]' \
	| xargs grep -lPI "$1\b" \
	>"$files";
}


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


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


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


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


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


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


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


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


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


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


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


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


grepc_glibc()
{
	grepc_glibc_math "$1";
}


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


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


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


grepc_type_typedef_underlying_struct_union_enum()
{
	xargs grep -hP "^\s*typedef\s+(struct|union|enum)\s+.*\b$1;" <"$files" \
	| 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 -r t; do
		test "$1" != "$t" \
		&& grepc_type_struct_union_enum "$t";
	done;
}


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


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


main()
{
	grepc_find_files "$1";

	grepc_macro "$1";
	grepc_enum_constant "$1";
	grepc_func "$1";
	grepc_linux "$1";
	grepc_glibc "$1";
	grepc_type "$1";
}


main "$1";