uninclude 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #! /bin/sh
  2. # (C) 1998, 2007 Free Software Foundation
  3. # Originally by Alexandre Oliva <oliva@lsd.ic.unicamp.br>
  4. # This gawk/shell script is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License as published
  6. # by the Free Software Foundation; either version 3, or (at your option)
  7. # any later version.
  8. # Given a preprocessed C/C++ code snippet, this script will replace any
  9. # standard header files with an actual #include <...> directive.
  10. # Example:
  11. # # 1 "test.c"
  12. # # 1 "/usr/include/stdio.h" 1 3
  13. # <snip>
  14. # # 1 "test.c" 2
  15. #
  16. # main() { printf("Hello world!\n"); }
  17. # is replaced with
  18. # # 1 "test.c"
  19. # #include <stdio.h>
  20. # main() { printf("Hello world!\n"); }
  21. # Header files whose pathnames contain any of the following patterns
  22. # are considered as standard headers: usr/include, g++-include,
  23. # include/g++, include/c++/<version>, gcc-lib/<anything>/include.
  24. gawk ${EXCLUDEPATT+-vexclude="$EXCLUDEPATT"} \
  25. ${INCLUDEPATT+-vinclude="$INCLUDEPATT"} '
  26. BEGIN {
  27. skipping = 0;
  28. cppline = "^# [0-9]+ \"[^\"]*/(usr/include|g\\+\\+-include|include/g\\+\\+|include/c\\+\\+/[^/]+|gcc-lib/[^\"]+/include|gcc/include)/([^\"]+)\"( [1-4])*$"
  29. }
  30. !skipping && $0 ~ cppline &&
  31. (exclude == "" || $3 !~ exclude) && (include == "" || $3 ~ include) {
  32. skipping = 1;
  33. printf "%s\n", "#include <" gensub(cppline, "\\2", 1, $0) ">"
  34. next;
  35. }
  36. skipping && /^# [0-9]+ / && $3 == lastincluded {
  37. skipping = 0;
  38. next;
  39. }
  40. !skipping && /^# [0-9]+ / {
  41. lastincluded = $3;
  42. }
  43. !skipping { print }
  44. ' ${1+"$@"}