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