This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Metaconfig unit change for #9113.
[metaconfig.git] / U / modified / myhostname.U
CommitLineData
edd6115f
JH
1?RCS: $Id: myhostname.U,v 3.0.1.3 1997/02/28 16:15:55 ram Exp $
2?RCS:
3?RCS: Copyright (c) 1991-1993, Raphael Manfredi
4?RCS:
5?RCS: You may redistribute only under the terms of the Artistic Licence,
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
8?RCS: that same Artistic Licence; a copy of which may be found at the root
9?RCS: of the source tree for dist 3.0.
10?RCS:
11?RCS: $Log: myhostname.U,v $
12?RCS: Revision 3.0.1.3 1997/02/28 16:15:55 ram
13?RCS: patch61: improved hostname lookup by using ypmatch when NIS is used
14?RCS:
15?RCS: Revision 3.0.1.2 1994/10/29 16:25:43 ram
16?RCS: patch36: call ./xenix explicitely instead of relying on PATH
17?RCS: patch36: now uses new Tr unit to convert to/from lowercase
18?RCS:
19?RCS: Revision 3.0.1.1 1994/06/20 07:06:20 ram
20?RCS: patch30: now a little more clever for domain name guessing
21?RCS:
22?RCS: Revision 3.0 1993/08/18 12:09:18 ram
23?RCS: Baseline for dist 3.0 netwide release.
24?RCS:
25?MAKE:myhostname phostname mydomain: contains sed test Myread Oldconfig \
df0bcc5d 26 Guess Loc awk echo sort uniq +usrinc rm hostcat Tr
edd6115f
JH
27?MAKE: -pick add $@ %<
28?S:myhostname (hostname):
29?S: This variable contains the eventual value of the MYHOSTNAME symbol,
30?S: which is the name of the host the program is going to run on.
31?S: The domain is not kept with hostname, but must be gotten from mydomain.
32?S: The dot comes with mydomain, and need not be supplied by the program.
33?S:.
34?S:mydomain:
35?S: This variable contains the eventual value of the MYDOMAIN symbol,
36?S: which is the domain of the host the program is going to run on.
37?S: The domain must be appended to myhostname to form a complete host name.
38?S: The dot comes with mydomain, and need not be supplied by the program.
39?S:.
40?S:phostname:
41?S: This variable contains the eventual value of the PHOSTNAME symbol,
42?S: which is a command that can be fed to popen() to get the host name.
43?S: The program should probably not presume that the domain is or isn't
44?S: there already.
45?S:.
46?C:MYHOSTNAME (HOSTNAME):
47?C: This symbol contains name of the host the program is going to run on.
48?C: The domain is not kept with hostname, but must be gotten from MYDOMAIN.
49?C: The dot comes with MYDOMAIN, and need not be supplied by the program.
50?C: If gethostname() or uname() exist, MYHOSTNAME may be ignored. If MYDOMAIN
51?C: is not used, MYHOSTNAME will hold the name derived from PHOSTNAME.
52?C:.
53?C:MYDOMAIN:
54?C: This symbol contains the domain of the host the program is going to
55?C: run on. The domain must be appended to HOSTNAME to form a complete
56?C: host name. The dot comes with MYDOMAIN, and need not be supplied by
57?C: the program. If the host name is derived from PHOSTNAME, the domain
58?C: may or may not already be there, and the program should check.
59?C:.
60?H:#define MYHOSTNAME "$myhostname" /**/
61?H:#define MYDOMAIN "$mydomain" /**/
62?H:.
63?T:cont i tans tmp_re
64: now get the host name
65echo " "
66echo "Figuring out host name..." >&4
67case "$myhostname" in
68'') cont=true
69 echo 'Maybe "hostname" will work...'
df0bcc5d 70 if tans=`sh -c hostname 2>&1` ; then
edd6115f
JH
71 myhostname=$tans
72 phostname=hostname
73 cont=''
74 fi
75 ;;
76*) cont='';;
77esac
78if $test "$cont"; then
79 if ./xenix; then
80 echo 'Oh, dear. Maybe "/etc/systemid" is the key...'
81 if tans=`cat /etc/systemid 2>&1` ; then
82 myhostname=$tans
83 phostname='cat /etc/systemid'
84 echo "Whadyaknow. Xenix always was a bit strange..."
85 cont=''
86 fi
87 elif $test -r /etc/systemid; then
88 echo "(What is a non-Xenix system doing with /etc/systemid?)"
89 fi
90fi
91if $test "$cont"; then
92 echo 'No, maybe "uuname -l" will work...'
df0bcc5d 93 if tans=`sh -c 'uuname -l' 2>&1` ; then
edd6115f
JH
94 myhostname=$tans
95 phostname='uuname -l'
96 else
97 echo 'Strange. Maybe "uname -n" will work...'
df0bcc5d 98 if tans=`sh -c 'uname -n' 2>&1` ; then
edd6115f
JH
99 myhostname=$tans
100 phostname='uname -n'
101 else
102 echo 'Oh well, maybe I can mine it out of whoami.h...'
df0bcc5d 103 if tans=`sh -c $contains' sysname $usrinc/whoami.h' 2>&1` ; then
edd6115f
JH
104 myhostname=`echo "$tans" | $sed 's/^.*"\(.*\)"/\1/'`
105 phostname="sed -n -e '"'/sysname/s/^.*\"\\(.*\\)\"/\1/{'"' -e p -e q -e '}' <$usrinc/whoami.h"
106 else
107 case "$myhostname" in
108 '') echo "Does this machine have an identity crisis or something?"
109 phostname='';;
110 *)
111 echo "Well, you said $myhostname before..."
112 phostname='echo $myhostname';;
113 esac
114 fi
115 fi
116 fi
117fi
118: you do not want to know about this
119set $myhostname
120myhostname=$1
121
122: verify guess
123if $test "$myhostname" ; then
124 dflt=y
125 rp='Your host name appears to be "'$myhostname'".'" Right?"
126 . ./myread
127 case "$ans" in
128 y*) ;;
129 *) myhostname='';;
130 esac
131fi
132
133: bad guess or no guess
134while $test "X$myhostname" = X ; do
135 dflt=''
136 rp="Please type the (one word) name of your host:"
137 . ./myread
138 myhostname="$ans"
139done
140
141: translate upper to lower if necessary
142case "$myhostname" in
143*[A-Z]*)
144 echo "(Normalizing case in your host name)"
145 myhostname=`echo $myhostname | ./tr '[A-Z]' '[a-z]'`
146 ;;
147esac
148
149?X: Do not ask for domain name if this is not used later on. In that
150?X: case, the hostname may keep its domain name, but it doesn't matter.
151@if MYDOMAIN || mydomain
152case "$myhostname" in
153*.*)
154 dflt=`expr "X$myhostname" : "X[^.]*\(\..*\)"`
155 myhostname=`expr "X$myhostname" : "X\([^.]*\)\."`
156 echo "(Trimming domain name from host name--host name is now $myhostname)"
157 ;;
158*) case "$mydomain" in
159 '')
160?X:
161?X: There is currently no way to say we do not want hostcat if mydomain is not
162?X: used. One way to achieve that would be to put that code in a mydomain.U
163?X: unit. However, we want to stick the sanity checks right after the domain
164?X: name computation, or if none is done, right after the hostname computation.
165?X:
166 {
167?X: If we use NIS, try ypmatch.
168 test "X$hostcat" = "Xypcat hosts" &&
169 ypmatch "$myhostname" hosts 2>/dev/null |\
170 $sed -e 's/[ ]*#.*//; s/$/ /' > hosts && \
171 $test -s hosts
172 } || {
173?X: Extract only the relevant hosts, reducing file size,
174?X: remove comments, insert trailing space for later use.
175?X: Test to be sure we *have* a hostcat command. os390 doesn't.
176 test "X$hostcat" != "X" &&
177 $hostcat | $sed -n -e "s/[ ]*#.*//; s/\$/ /
178 /[ ]$myhostname[ . ]/p" > hosts
179 }
180 tmp_re="[ . ]"
f9ecdbac
PP
181 if $test -f hosts; then
182 $test x`$awk "/[0-9].*[ ]$myhostname$tmp_re/ { sum++ }
edd6115f 183 END { print sum }" hosts` = x1 || tmp_re="[ ]"
f9ecdbac
PP
184 dflt=.`$awk "/[0-9].*[ ]$myhostname$tmp_re/ {for(i=2; i<=NF;i++) print \\\$i}" \
185 hosts | $sort | $uniq | \
186 $sed -n -e "s/$myhostname\.\([-a-zA-Z0-9_.]\)/\1/p"`
187 case `$echo X$dflt` in
188 X*\ *) echo "(Several hosts in the database matched hostname)"
189 dflt=.
190 ;;
191 X.) echo "(You do not have fully-qualified names in the hosts database)"
192 ;;
193 esac
194 else
195 echo "(I cannot locate a hosts database anywhere)"
edd6115f 196 dflt=.
f9ecdbac 197 fi
edd6115f
JH
198 case "$dflt" in
199 .)
200 tans=`./loc resolv.conf X /etc /usr/etc`
201 if $test -f "$tans"; then
202 echo "(Attempting domain name extraction from $tans)"
203?X: Look for either a search or a domain directive.
204 dflt=.`$sed -n -e 's/ / /g' \
205 -e 's/^search *\([^ ]*\).*/\1/p' $tans \
206 | ./tr '[A-Z]' '[a-z]' 2>/dev/null`
207 case "$dflt" in
208 .) dflt=.`$sed -n -e 's/ / /g' \
209 -e 's/^domain *\([^ ]*\).*/\1/p' $tans \
210 | ./tr '[A-Z]' '[a-z]' 2>/dev/null`
211 ;;
212 esac
213 fi
214 ;;
215 esac
216 case "$dflt" in
217 .) echo "(No help from resolv.conf either -- attempting clever guess)"
df0bcc5d 218 dflt=.`sh -c domainname 2>/dev/null`
edd6115f
JH
219 case "$dflt" in
220 '') dflt='.';;
221 .nis.*|.yp.*|.main.*) dflt=`echo $dflt | $sed -e 's/^\.[^.]*//'`;;
222 esac
223 ;;
224 esac
8b0d65e1
JH
225 case "$dflt$osname" in
226 .os390) echo "(Attempting domain name extraction from //'SYS1.TCPPARMS(TCPDATA)')"
227 dflt=.``awk '/^DOMAINORIGIN/ {print $2}' "//'SYS1.TCPPARMS(TCPDATA)'" 2>/dev/null`
228 ;;
229 esac
edd6115f
JH
230 case "$dflt" in
231 .) echo "(Lost all hope -- silly guess then)"
232 dflt='.uucp'
233 ;;
234 esac
235 $rm -f hosts
236 ;;
237 *) dflt="$mydomain";;
238 esac;;
239esac
240echo " "
241rp="What is your domain name?"
242. ./myread
243tans="$ans"
244case "$ans" in
245'') ;;
246.*) ;;
247*) tans=".$tans";;
248esac
249mydomain="$tans"
250
251: translate upper to lower if necessary
252case "$mydomain" in
253*[A-Z]*)
254 echo "(Normalizing case in your domain name)"
255 mydomain=`echo $mydomain | ./tr '[A-Z]' '[a-z]'`
256 ;;
257esac
258
259@end
260: a little sanity check here
261case "$phostname" in
262'') ;;
263*)
264 case `$phostname | ./tr '[A-Z]' '[a-z]'` in
265 $myhostname$mydomain|$myhostname) ;;
266 *)
267 case "$phostname" in
268 sed*)
269 echo "(That doesn't agree with your whoami.h file, by the way.)"
270 ;;
271 *)
272 echo "(That doesn't agree with your $phostname command, by the way.)"
273 ;;
274 esac
275 ;;
276 esac
277 ;;
278esac
279