summaryrefslogtreecommitdiff
path: root/mcon/U/myhostname.U
blob: 3e33f7055364c37f1647810cbcd62a9aa1bd0d66 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
?RCS: $Id$
?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: $Log: myhostname.U,v $
?RCS: Revision 3.0.1.3  1997/02/28  16:15:55  ram
?RCS: patch61: improved hostname lookup by using ypmatch when NIS is used
?RCS:
?RCS: Revision 3.0.1.2  1994/10/29  16:25:43  ram
?RCS: patch36: call ./xenix explicitely instead of relying on PATH
?RCS: patch36: now uses new Tr unit to convert to/from lowercase
?RCS:
?RCS: Revision 3.0.1.1  1994/06/20  07:06:20  ram
?RCS: patch30: now a little more clever for domain name guessing
?RCS:
?RCS: Revision 3.0  1993/08/18  12:09:18  ram
?RCS: Baseline for dist 3.0 netwide release.
?RCS:
?MAKE:myhostname phostname mydomain: contains sed test Myread Oldconfig \
	Guess Loc awk echo sort uniq +usrinc rm hostcat Tr osname
?MAKE:	-pick add $@ %<
?S:myhostname (hostname):
?S:	This variable contains the eventual value of the MYHOSTNAME symbol,
?S:	which is the name of the host the program is going to run on.
?S:	The domain is not kept with hostname, but must be gotten from mydomain.
?S:	The dot comes with mydomain, and need not be supplied by the program.
?S:.
?S:mydomain:
?S:	This variable contains the eventual value of the MYDOMAIN symbol,
?S:	which is the domain of the host the program is going to run on.
?S:	The domain must be appended to myhostname to form a complete host name.
?S:	The dot comes with mydomain, and need not be supplied by the program.
?S:.
?S:phostname:
?S:	This variable contains the eventual value of the PHOSTNAME symbol,
?S:	which is a command that can be fed to popen() to get the host name.
?S:	The program should probably not presume that the domain is or isn't
?S:	there already.
?S:.
?C:MYHOSTNAME (HOSTNAME):
?C:	This symbol contains name of the host the program is going to run on.
?C:	The domain is not kept with hostname, but must be gotten from MYDOMAIN.
?C:	The dot comes with MYDOMAIN, and need not be supplied by the program.
?C:	If gethostname() or uname() exist, MYHOSTNAME may be ignored. If MYDOMAIN
?C:	is not used, MYHOSTNAME will hold the name derived from PHOSTNAME.
?C:.
?C:MYDOMAIN:
?C:	This symbol contains the domain of the host the program is going to
?C:	run on.  The domain must be appended to HOSTNAME to form a complete
?C:	host name.  The dot comes with MYDOMAIN, and need not be supplied by
?C:	the program.  If the host name is derived from PHOSTNAME, the domain
?C:	may or may not already be there, and the program should check.
?C:.
?H:#define MYHOSTNAME "$myhostname"		/**/
?H:#define MYDOMAIN "$mydomain"		/**/
?H:.
?T:cont i tans tmp_re file
: now get the host name
echo " "
echo "Figuring out host name..." >&4
case "$myhostname" in
'') cont=true
	echo 'Maybe "hostname" will work...'
	if tans=`sh -c hostname 2>&1` ; then
		myhostname=$tans
		phostname=hostname
		cont=''
	fi
	;;
*) cont='';;
esac
if $test "$cont"; then
	if ./xenix; then
		echo 'Oh, dear.  Maybe "/etc/systemid" is the key...'
		if tans=`cat /etc/systemid 2>&1` ; then
			myhostname=$tans
			phostname='cat /etc/systemid'
			echo "Whadyaknow.  Xenix always was a bit strange..."
			cont=''
		fi
	elif $test -r /etc/systemid; then
		echo "(What is a non-Xenix system doing with /etc/systemid?)"
	fi
fi
if $test "$cont"; then
	echo 'No, maybe "uuname -l" will work...'
	if tans=`sh -c 'uuname -l' 2>&1` ; then
		myhostname=$tans
		phostname='uuname -l'
	else
		echo 'Strange.  Maybe "uname -n" will work...'
		if tans=`sh -c 'uname -n' 2>&1` ; then
			myhostname=$tans
			phostname='uname -n'
		else
			echo 'Oh well, maybe I can mine it out of whoami.h...'
			if tans=`sh -c $contains' sysname $usrinc/whoami.h' 2>&1` ; then
				myhostname=`echo "$tans" | $sed 's/^.*"\(.*\)"/\1/'`
				phostname="sed -n -e '"'/sysname/s/^.*\"\\(.*\\)\"/\1/{'"' -e p -e q -e '}' <$usrinc/whoami.h"
			else
				case "$myhostname" in
				'') echo "Does this machine have an identity crisis or something?"
					phostname='';;
				*)
					echo "Well, you said $myhostname before..."
					phostname='echo $myhostname';;
				esac
			fi
		fi
	fi
fi
case "$myhostname" in
'') myhostname=noname ;;
esac
: you do not want to know about this
set $myhostname
myhostname=$1

: verify guess
if $test "$myhostname" ; then
	dflt=y
	rp='Your host name appears to be "'$myhostname'".'" Right?"
	. ./myread
	case "$ans" in
	y*) ;;
	*) myhostname='';;
	esac
fi

: bad guess or no guess
while $test "X$myhostname" = X ; do
	dflt=''
	rp="Please type the (one word) name of your host:"
	. ./myread
	myhostname="$ans"
done

: translate upper to lower if necessary
case "$myhostname" in
*[A-Z]*)
	echo "(Normalizing case in your host name)"
	myhostname=`echo $myhostname | ./tr '[A-Z]' '[a-z]'`
	;;
esac

?X: Do not ask for domain name if this is not used later on. In that
?X: case, the hostname may keep its domain name, but it doesn't matter.
@if MYDOMAIN || mydomain
case "$myhostname" in
*.*)
	dflt=`expr "X$myhostname" : "X[^.]*\(\..*\)"`
	myhostname=`expr "X$myhostname" : "X\([^.]*\)\."`
	echo "(Trimming domain name from host name--host name is now $myhostname)"
	;;
*) case "$mydomain" in
	'')
?X:
?X: There is currently no way to say we do not want hostcat if mydomain is not
?X: used. One way to achieve that would be to put that code in a mydomain.U
?X: unit. However, we want to stick the sanity checks right after the domain
?X: name computation, or if none is done, right after the hostname computation.
?X:
		{
?X: If we use NIS, try ypmatch.
			test "X$hostcat" = "Xypcat hosts" &&
			ypmatch "$myhostname" hosts 2>/dev/null |\
				$sed -e 's/[	 ]*#.*//; s/$/ /' > hosts && \
			$test -s hosts
		} || {
?X: Extract only the relevant hosts, reducing file size,
?X: remove comments, insert trailing space for later use.
		     	$hostcat | $sed -n -e "s/[	 ]*#.*//; s/\$/ /
					/[	 ]$myhostname[	. ]/p" > hosts
		}
		tmp_re="[	. ]"
		$test x`$awk "/[0-9].*[	 ]$myhostname$tmp_re/ { sum++ }
			     END { print sum }" hosts` = x1 || tmp_re="[	 ]"
		dflt=.`$awk \
		"/[0-9].*[	 ]$myhostname$tmp_re/ {for(i=2; i<=NF;i++) print \\\$i}" \
			hosts | $sort | $uniq | \
			$sed -n -e "s/$myhostname\.\([-a-zA-Z0-9_.]\)/\1/p"`
		case `$echo X$dflt` in
		X*\ *)	echo "(Several hosts in /etc/hosts matched hostname)"
			dflt=.
			;;
?X: There is no /etc/hosts on os390
		X.)
			if $test -f /etc/hosts; then
				echo "(You do not have fully-qualified names in /etc/hosts)"
			else
				echo "(I cannot locate a hosts database anywhere)"
			fi
			;;
		esac
		case "$dflt" in
		.)
			tans=`./loc resolv.conf X /etc /usr/etc`
			if $test -f "$tans"; then
				echo "(Attempting domain name extraction from $tans)"
?X: Look for either a search or a domain directive.
				dflt=.`$sed -n -e 's/	/ /g' \
				  -e 's/^search  *\([^ ]*\).*/\1/p' $tans \
				  | ./tr '[A-Z]' '[a-z]' 2>/dev/null`
				case "$dflt" in
				.) dflt=.`$sed -n -e 's/	/ /g' \
				     -e 's/^domain  *\([^ ]*\).*/\1/p' $tans \
				     | ./tr '[A-Z]' '[a-z]' 2>/dev/null`
  					;;
				esac
			fi
			;;
		esac
		case "$dflt" in
		.) echo "(No help from resolv.conf either -- attempting clever guess)"
			dflt=.`sh -c domainname 2>/dev/null`
			case "$dflt" in
			'') dflt='.';;
			.nis.*|.yp.*|.main.*) dflt=`echo $dflt | $sed -e 's/^\.[^.]*//'`;;
			esac
			;;
		esac
		case "$dflt$osname" in
		.os390)
			file="//'SYS1.TCPPARMS(TCPDATA)'"
			echo "(Attempting domain name extraction from $file)"
			dflt=.`awk '/^DOMAINORIGIN/ {print $2}' "$file" 2>/dev/null`
			;;
			esac
		case "$dflt" in
		.) echo "(Lost all hope -- silly guess then)"
			dflt='.nonet'
			;;
		esac
		$rm -f hosts
		;;
	*) dflt="$mydomain";;
	esac;;
esac
echo " "
rp="What is your domain name?"
. ./myread
tans="$ans"
case "$ans" in
'') ;;
.*) ;;
*) tans=".$tans";;
esac
mydomain="$tans"

: translate upper to lower if necessary
case "$mydomain" in
*[A-Z]*)
	echo "(Normalizing case in your domain name)"
	mydomain=`echo $mydomain | ./tr '[A-Z]' '[a-z]'`
	;;
esac

@end
: a little sanity check here
case "$phostname" in
'') ;;
*)
	case `$phostname | ./tr '[A-Z]' '[a-z]'` in
	$myhostname$mydomain|$myhostname) ;;
	*)
		case "$phostname" in
		sed*)
			echo "(That doesn't agree with your whoami.h file, by the way.)"
			;;
		*)
			echo "(That doesn't agree with your $phostname command, by the way.)"
			;;
		esac
	;;
	esac
	;;
esac