This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[UTIL] Perl system does not default to $_
[metaconfig.git] / dist / U / Findhdr.U
CommitLineData
33a01fd2 1?RCS: $Id$
d8875586
MBT
2?RCS:
3?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
4?RCS:
33a01fd2 5?RCS: You may redistribute only under the terms of the Artistic License,
d8875586
MBT
6?RCS: as specified in the README file that comes with the distribution.
7?RCS: You may reuse parts of this distribution only within the terms of
33a01fd2 8?RCS: that same Artistic License; a copy of which may be found at the root
d8875586
MBT
9?RCS: of the source tree for dist 4.0.
10?RCS:
11?RCS: Original Author: Thomas Neumann <tom@smart.bo.open.de>
12?RCS:
13?RCS: $Log: Findhdr.U,v $
14?RCS: Revision 3.0.1.2 1994/10/29 15:53:08 ram
15?RCS: patch36: added ?F: line for metalint file checking
16?RCS:
17?RCS: Revision 3.0.1.1 1994/05/06 14:03:56 ram
18?RCS: patch23: cppminus must be after other cppflags, not before
19?RCS:
20?RCS: Revision 3.0 1993/08/18 12:04:54 ram
21?RCS: Baseline for dist 3.0 netwide release.
22?RCS:
23?X:
24?X: This unit produces a findhdr script which is used to locate the header
25?X: files in $usrinc or other stranger places using cpp capabilities. The
26?X: script is given an include file base name, like 'stdio.h' or 'sys/file.h'
27?X: and it returns the full path of the include file and a zero status or an
28?X: empty string with an error status if the file could not be located.
29?X:
30?MAKE:Findhdr: grep test tr rm +usrinc awk cat startsh \
31 cppstdin cppminus +cppflags eunicefix osname
32?MAKE: -pick add $@ %<
33?LINT:define fieldn
34?S:fieldn:
35?S: This variable is used internally by Configure. It contains the position
36?S: of the included file name in cpp output. That is to say, when cpp
37?S: pre-processes a #include <file> line, it replaces it by a # line which
38?S: contains the original position in the input file and the full name of
39?S: included file, between "quotes".
40?S:.
41?V:fieldn
42?F:./findhdr !fieldn
43?T:cline pos wanted name awkprg cppfilter testaccess status usrincdir
44: determine filename position in cpp output
45echo " "
46echo "Computing filename position in cpp output for #include directives..." >&4
47echo '#include <stdio.h>' > foo.c
48case "$osname" in
49vos)
50 testaccess=-e
51?X: VOS: path component separator is >
52 cppfilter="tr '\\\\>' '/' |"
53 ;;
54*)
55 testaccess=-r
56 cppfilter=''
57 ;;
58esac
59$cat >fieldn <<EOF
60$startsh
61$cppstdin $cppflags $cppminus <foo.c 2>/dev/null | \
62$grep '^[ ]*#.*stdio\.h' | \
63while read cline; do
64 pos=1
65 set \$cline
66 while $test \$# -gt 0; do
67 if $test $testaccess \`echo \$1 | $tr -d '"'\`; then
68 echo "\$pos"
69 exit 0
70 fi
71 shift
72 pos=\`expr \$pos + 1\`
73 done
74done
75EOF
76chmod +x fieldn
77fieldn=`./fieldn`
78$rm -f foo.c fieldn
79case $fieldn in
80'') pos='???';;
811) pos=first;;
822) pos=second;;
833) pos=third;;
84*) pos="${fieldn}th";;
85esac
86echo "Your cpp writes the filename in the $pos field of the line."
87
88?X: To locate a header file, we cannot simply check for $usrinc/file.h, since
89?X: some machine have the headers in weird places and our only hope is that
90?X: the C pre-processor will know how to find those headers. Thank you NexT!
91: locate header file
92$cat >findhdr <<EOF
93$startsh
94wanted=\$1
95name=''
96for usrincdir in $usrinc; do
97 if test -f \$usrincdir/\$wanted; then
98 echo "\$usrincdir/\$wanted"
99 exit 0
100 fi
101done
102awkprg='{ print \$$fieldn }'
103echo "#include <\$wanted>" > foo\$\$.c
104$cppstdin $cppminus $cppflags < foo\$\$.c 2>/dev/null | \
105$cppfilter $grep "^[ ]*#.*\$wanted" | \
106while read cline; do
107 name=\`echo \$cline | $awk "\$awkprg" | $tr -d '"'\`
108 case "\$name" in
109 *[/\\\\]\$wanted) echo "\$name"; exit 1;;
110 *[\\\\/]\$wanted) echo "\$name"; exit 1;;
111 *) exit 2;;
112 esac
113done
114?X: status = 0: grep returned 0 lines, case statement not executed
115?X: status = 1: headerfile found
116?X: status = 2: while loop executed, no headerfile found
117status=\$?
118$rm -f foo\$\$.c
119if test \$status -eq 1; then
120 exit 0
121fi
122exit 1
123EOF
124chmod +x findhdr
125$eunicefix findhdr
126