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

?X: To locate a header file, we cannot simply check for $usrinc/file.h, since
?X: some machine have the headers in weird places and our only hope is that
?X: the C pre-processor will know how to find those headers. Thank you NexT!
: locate header file
$cat >findhdr <<EOF
$startsh
wanted=\$1
name=''
for usrincdir in $usrinc; do
	if test -f \$usrincdir/\$wanted; then
		echo "\$usrincdir/\$wanted"
		exit 0
	fi
done
awkprg='{ print \$$fieldn }'
echo "#include <\$wanted>" > foo\$\$.c
$cppstdin $cppminus $cppflags < foo\$\$.c 2>/dev/null | \
$cppfilter $grep "^[ 	]*#.*\$wanted" | \
while read cline; do
	name=\`echo \$cline | $awk "\$awkprg" | $tr -d '"'\`
	case "\$name" in
	*[/\\\\]\$wanted) echo "\$name"; exit 1;;
	*[\\\\/]\$wanted) echo "\$name"; exit 1;;
	*) exit 2;;
	esac
done
?X: status = 0: grep returned 0 lines, case statement not executed
?X: status = 1: headerfile found
?X: status = 2: while loop executed, no headerfile found
status=\$?
$rm -f foo\$\$.c
if test \$status -eq 1; then
	exit 0
fi
exit 1
EOF
chmod +x findhdr
$eunicefix findhdr