Debian Codegraph @

src2nbcompat

raw
1 #!/bin/sh
2 #
3 # $NetBSD: src2nbcompat,v 1.11 2018/08/22 20:48:38 maya Exp $
4 #
5 # Copyright (c) 2003 The NetBSD Foundation, Inc.
6 # All rights reserved.
7 #
8 # This code is derived from software contributed to The NetBSD Foundation
9 # by Johnny C. Lam.
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32
33 if [ $# -lt 2 ]; then echo "src2nbcompat src dest"; exit 1; fi
34
35 src=$1
36 dest=$2
37
38 case $src in
39 /*) ;;
40 *) src=`pwd`/$src ;;
41 esac
42
43 case $dest in
44 /*) ;;
45 *) dest=`pwd`/$dest ;;
46 esac
47
48 make_dir()
49 {
50 _d="$1"
51 _destdir="$2"
52 _newd=${3-`echo $_d | sed 's,^\./,,'`}
53 case $_newd in
54 *CVS)
55 ;;
56 .)
57 ;;
58 *)
59 mkdir -p $_destdir/$_newd
60 ;;
61 esac
62 }
63
64 copy_file()
65 {
66 _f="$1"
67 _destdir="$2"
68 _newf=${3-`echo $_f | sed 's,^\./,,'`}
69 case $_newf in
70 *CVS/*)
71 # Skip copying over any CVS information.
72 ;;
73 *Makefile|*Makefile.inc)
74 # Skip copying over the Makefiles since we'll be using
75 # a GNU autoconf-based build infrastructure.
76 ;;
77 *.sh)
78 # Assume shell scripts will require variable substitution
79 # by the GNU configure script.
80 #
81 cp $_f $_destdir/$_newf.in
82 ;;
83 *.[ch])
84 # Add "#include <nbcompat.h>" before the first system
85 # header inclusion. Protect all system headers with
86 # "#if HAVE_FOO_H ... #endif" guards.
87 #
88 awk ' \
89 BEGIN { config = 0; nbcompat = 0 } \
90 /^#[ ]*if[ ]+HAVE_NBTOOL_CONFIG_H/ { \
91 if (config == 0) { \
92 print "#if HAVE_CONFIG_H"; \
93 print "#include \"config.h\""; \
94 print "#endif"; \
95 config = 1; \
96 } \
97 } \
98 /^#[ ]*include[ ]+/ { \
99 if (config == 0) { \
100 print "#if HAVE_CONFIG_H"; \
101 print "#include \"config.h\""; \
102 print "#endif"; \
103 config = 1; \
104 } \
105 } \
106 /^#[ ]*include[ ]+<[^ ]*>/ { \
107 guard = $0; \
108 sub("#[ ]*include[ ]+<", "HAVE_", guard); \
109 sub(">.*", "", guard); \
110 gsub("[-/.]", "_", guard); \
111 guard = toupper(guard); \
112 if (nbcompat == 0) { \
113 print "#include <nbcompat.h>"; \
114 nbcompat = 1; \
115 }; \
116 print "#if " guard; \
117 print $0; \
118 print "#endif"; \
119 next; \
120 } \
121 { print } \
122 ' $_f > $_destdir/$_newf.tmp
123 if [ -f $_destdir/$_newf ] && \
124 diff -q -I "\$Net""BSD.*\$" $_destdir/$_newf $_destdir/$_newf.tmp >/dev/null; then
125 rm -f $_destdir/$_newf.tmp
126 else
127 rm -f $_destdir/$_newf
128 mv $_destdir/$_newf.tmp $_destdir/$_newf
129 fi
130 ;;
131 *.[1-9])
132 # Copy the manpage over, and generate the catpage.
133 cp $_newf $_destdir/$_newf.tmp
134 if [ -f $_destdir/$_newf ] && \
135 diff -q -I "\$Net""BSD.*\$" $_destdir/$_newf $_destdir/$_newf.tmp >/dev/null; then
136 rm -f $_destdir/$_newf.tmp
137 else
138 mv -f $_destdir/$_newf.tmp $_destdir/$_newf
139 _catf=`echo $_newf | sed 's,\.\([1-9]\),.cat\1,'`
140 nroff -mandoc $_newf > $_destdir/$_catf
141 fi
142 ;;
143 *)
144 # Everything else just gets copied verbatim.
145 cp $_newf $_destdir/$_newf.tmp
146 if [ -f $_destdir/$_newf ] && \
147 diff -q -I "\$Net""BSD.*\$" $_destdir/$_newf $_destdir/$_newf.tmp >/dev/null; then
148 rm -f $_destdir/$_newf.tmp
149 else
150 mv -f $_destdir/$_newf.tmp $_destdir/$_newf
151 fi
152 ;;
153 esac
154 }
155
156 if [ -f "$src" ]; then
157 srcdir=`dirname $src`
158 destdir=`dirname $dest`
159 else
160 srcdir="$src"
161 destdir="$dest"
162 fi
163
164 echo "Preparing directory $destdir"
165 mkdir -p $destdir
166
167 if [ -f "$src" ]; then
168 echo "Copying $src to $dest"
169 cd $srcdir
170 copy_file `basename $src` $destdir `basename $dest`
171 else
172 echo "Copying files and directories to $destdir"
173 cd $srcdir
174 find . -type d -print |
175 while read d; do
176 make_dir $d $destdir
177 done
178 find . \! -type d -print |
179 while read f; do
180 copy_file $f $destdir
181 done
182 fi