I want to read the prototype of a C function through the source code of a big project.
I know the function name, and its return type, and that its prototype will be defined in a *.h
file.
I'd use grep(1)
, but I want to be able to read multi-line prototypes, so it's discarded.
So what I usually do is:
- project:
glibc
- return type:
int
- function name:
cacheflush
syscall='cacheflush';
find glibc/ -name '*.h' \
|xargs sed -n "/^[a-z ]*int ${syscall}[ ]*(/,/^$/p";
But that prints some unwanted lines after the ones I want:
$ find glibc/ -name '*.h' \
|xargs sed -n "/^[a-z ]*int ${syscall}[ ]*(/,/^$/p";
extern int cacheflush (void *__addr, int __nbytes, int __op) __THROW;
#endif
extern int cacheflush (void *__addr, const int __nbytes,
const int __op) __THROW;
#endif
extern int cacheflush (void *__addr, const int __nbytes, const int __op) __THROW;
#endif
extern int _flush_cache (char *__addr, const int __nbytes, const int __op) __THROW;
extern int cacheflush (void *__addr, const int __nbytes, const int __op) __THROW;
#endif
extern int _flush_cache (char *__addr, const int __nbytes, const int __op) __THROW;
I'd like to be able to replace the end pattern /^$/
-> /;/
, but then it'll only work when the function prototype spans across multiple lines. Is it possible to tell sed(1)
that the end pattern might be on the same line as the begin pattern, so that the output would be the following?:
$ find glibc/ -name '*.h' | xargs sed magic;
extern int cacheflush (void *__addr, int __nbytes, int __op) __THROW;
extern int cacheflush (void *__addr, const int __nbytes,
const int __op) __THROW;
extern int cacheflush (void *__addr, const int __nbytes, const int __op) __THROW;
extern int cacheflush (void *__addr, const int __nbytes, const int __op) __THROW;