Search code examples
parsingawktagsctagsexuberant-ctags

ctags: To get C function end line number


is it possible via ctags to get the function end line number as well

"ctags -x --c-kinds=f filename.c"

Above command lists the function definition start line-numbers. Wanted a way to get the function end line numbers.

Other Approaches:

awk 'NR > first && /^}$/ { print NR; exit }' first=$FIRST_LINE filename.c

This needs the code to be properly formatted

Example: filename.c

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 int main()
  4 {
  5    const char  *name;
  6 
  7     int a=0
  8     printf("name");
  9     printf("sssss: %s",name);
 10 
 11     return 0;
 12 }
 13 
 14 void code()
 15  {
 16         printf("Code \n");
 17  }
 18 
 19 int code2()
 20    {
 21         printf("code2 \n");
 22         return 1
 23    }
 24 

Input: filename and the function start line no.

Example:

    Input: filename.c  3
    Output: 12

    Input : filename.c 19
    Output : 23

Is there any better/simple way of doing this ?


Solution

  • awk to the rescue!

    doesn't handle curly braces within comments but should handle blocks within functions, please give it a try...

    $ awk -v s=3 'NR>=s && /{/              {c++} 
                  NR>=s && /}/ && c && !--c {print NR; exit}' file
    

    finds the matching brace for the first one after the specified start line number s.