Commit | Line | Data |
---|---|---|
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 | |
70e7365a | 16 | ?RCS: patch36: call ./xenix explicitly instead of relying on PATH |
edd6115f JH |
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:. | |
77506990 | 63 | ?LINT:extern osname |
edd6115f JH |
64 | ?T:cont i tans tmp_re |
65 | : now get the host name | |
66 | echo " " | |
67 | echo "Figuring out host name..." >&4 | |
68 | case "$myhostname" in | |
69 | '') cont=true | |
70 | echo 'Maybe "hostname" will work...' | |
df0bcc5d | 71 | if tans=`sh -c hostname 2>&1` ; then |
edd6115f JH |
72 | myhostname=$tans |
73 | phostname=hostname | |
74 | cont='' | |
75 | fi | |
76 | ;; | |
77 | *) cont='';; | |
78 | esac | |
79 | if $test "$cont"; then | |
80 | if ./xenix; then | |
81 | echo 'Oh, dear. Maybe "/etc/systemid" is the key...' | |
82 | if tans=`cat /etc/systemid 2>&1` ; then | |
83 | myhostname=$tans | |
84 | phostname='cat /etc/systemid' | |
85 | echo "Whadyaknow. Xenix always was a bit strange..." | |
86 | cont='' | |
87 | fi | |
88 | elif $test -r /etc/systemid; then | |
89 | echo "(What is a non-Xenix system doing with /etc/systemid?)" | |
90 | fi | |
91 | fi | |
92 | if $test "$cont"; then | |
93 | echo 'No, maybe "uuname -l" will work...' | |
df0bcc5d | 94 | if tans=`sh -c 'uuname -l' 2>&1` ; then |
edd6115f JH |
95 | myhostname=$tans |
96 | phostname='uuname -l' | |
97 | else | |
98 | echo 'Strange. Maybe "uname -n" will work...' | |
df0bcc5d | 99 | if tans=`sh -c 'uname -n' 2>&1` ; then |
edd6115f JH |
100 | myhostname=$tans |
101 | phostname='uname -n' | |
102 | else | |
103 | echo 'Oh well, maybe I can mine it out of whoami.h...' | |
df0bcc5d | 104 | if tans=`sh -c $contains' sysname $usrinc/whoami.h' 2>&1` ; then |
edd6115f JH |
105 | myhostname=`echo "$tans" | $sed 's/^.*"\(.*\)"/\1/'` |
106 | phostname="sed -n -e '"'/sysname/s/^.*\"\\(.*\\)\"/\1/{'"' -e p -e q -e '}' <$usrinc/whoami.h" | |
107 | else | |
108 | case "$myhostname" in | |
109 | '') echo "Does this machine have an identity crisis or something?" | |
110 | phostname='';; | |
111 | *) | |
112 | echo "Well, you said $myhostname before..." | |
113 | phostname='echo $myhostname';; | |
114 | esac | |
115 | fi | |
116 | fi | |
117 | fi | |
118 | fi | |
ce23e7f2 JH |
119 | case "$myhostname" in |
120 | '') myhostname=noname ;; | |
121 | esac | |
edd6115f JH |
122 | : you do not want to know about this |
123 | set $myhostname | |
124 | myhostname=$1 | |
125 | ||
126 | : verify guess | |
127 | if $test "$myhostname" ; then | |
128 | dflt=y | |
129 | rp='Your host name appears to be "'$myhostname'".'" Right?" | |
130 | . ./myread | |
131 | case "$ans" in | |
132 | y*) ;; | |
133 | *) myhostname='';; | |
134 | esac | |
135 | fi | |
136 | ||
137 | : bad guess or no guess | |
138 | while $test "X$myhostname" = X ; do | |
139 | dflt='' | |
140 | rp="Please type the (one word) name of your host:" | |
141 | . ./myread | |
142 | myhostname="$ans" | |
143 | done | |
144 | ||
145 | : translate upper to lower if necessary | |
146 | case "$myhostname" in | |
147 | *[A-Z]*) | |
148 | echo "(Normalizing case in your host name)" | |
149 | myhostname=`echo $myhostname | ./tr '[A-Z]' '[a-z]'` | |
150 | ;; | |
151 | esac | |
152 | ||
153 | ?X: Do not ask for domain name if this is not used later on. In that | |
154 | ?X: case, the hostname may keep its domain name, but it doesn't matter. | |
155 | @if MYDOMAIN || mydomain | |
156 | case "$myhostname" in | |
157 | *.*) | |
158 | dflt=`expr "X$myhostname" : "X[^.]*\(\..*\)"` | |
159 | myhostname=`expr "X$myhostname" : "X\([^.]*\)\."` | |
160 | echo "(Trimming domain name from host name--host name is now $myhostname)" | |
161 | ;; | |
162 | *) case "$mydomain" in | |
163 | '') | |
164 | ?X: | |
165 | ?X: There is currently no way to say we do not want hostcat if mydomain is not | |
166 | ?X: used. One way to achieve that would be to put that code in a mydomain.U | |
167 | ?X: unit. However, we want to stick the sanity checks right after the domain | |
168 | ?X: name computation, or if none is done, right after the hostname computation. | |
169 | ?X: | |
170 | { | |
171 | ?X: If we use NIS, try ypmatch. | |
172 | test "X$hostcat" = "Xypcat hosts" && | |
173 | ypmatch "$myhostname" hosts 2>/dev/null |\ | |
174 | $sed -e 's/[ ]*#.*//; s/$/ /' > hosts && \ | |
175 | $test -s hosts | |
176 | } || { | |
177 | ?X: Extract only the relevant hosts, reducing file size, | |
178 | ?X: remove comments, insert trailing space for later use. | |
179 | ?X: Test to be sure we *have* a hostcat command. os390 doesn't. | |
180 | test "X$hostcat" != "X" && | |
181 | $hostcat | $sed -n -e "s/[ ]*#.*//; s/\$/ / | |
182 | /[ ]$myhostname[ . ]/p" > hosts | |
183 | } | |
184 | tmp_re="[ . ]" | |
f9ecdbac PP |
185 | if $test -f hosts; then |
186 | $test x`$awk "/[0-9].*[ ]$myhostname$tmp_re/ { sum++ } | |
edd6115f | 187 | END { print sum }" hosts` = x1 || tmp_re="[ ]" |
f9ecdbac PP |
188 | dflt=.`$awk "/[0-9].*[ ]$myhostname$tmp_re/ {for(i=2; i<=NF;i++) print \\\$i}" \ |
189 | hosts | $sort | $uniq | \ | |
190 | $sed -n -e "s/$myhostname\.\([-a-zA-Z0-9_.]\)/\1/p"` | |
191 | case `$echo X$dflt` in | |
192 | X*\ *) echo "(Several hosts in the database matched hostname)" | |
193 | dflt=. | |
194 | ;; | |
195 | X.) echo "(You do not have fully-qualified names in the hosts database)" | |
196 | ;; | |
197 | esac | |
198 | else | |
199 | echo "(I cannot locate a hosts database anywhere)" | |
edd6115f | 200 | dflt=. |
f9ecdbac | 201 | fi |
edd6115f JH |
202 | case "$dflt" in |
203 | .) | |
204 | tans=`./loc resolv.conf X /etc /usr/etc` | |
205 | if $test -f "$tans"; then | |
206 | echo "(Attempting domain name extraction from $tans)" | |
207 | ?X: Look for either a search or a domain directive. | |
208 | dflt=.`$sed -n -e 's/ / /g' \ | |
209 | -e 's/^search *\([^ ]*\).*/\1/p' $tans \ | |
7be4ba5e | 210 | -e 1q 2>/dev/null` |
edd6115f JH |
211 | case "$dflt" in |
212 | .) dflt=.`$sed -n -e 's/ / /g' \ | |
213 | -e 's/^domain *\([^ ]*\).*/\1/p' $tans \ | |
7be4ba5e | 214 | -e 1q 2>/dev/null` |
edd6115f JH |
215 | ;; |
216 | esac | |
217 | fi | |
218 | ;; | |
219 | esac | |
220 | case "$dflt" in | |
221 | .) echo "(No help from resolv.conf either -- attempting clever guess)" | |
df0bcc5d | 222 | dflt=.`sh -c domainname 2>/dev/null` |
edd6115f JH |
223 | case "$dflt" in |
224 | '') dflt='.';; | |
225 | .nis.*|.yp.*|.main.*) dflt=`echo $dflt | $sed -e 's/^\.[^.]*//'`;; | |
226 | esac | |
227 | ;; | |
228 | esac | |
8b0d65e1 JH |
229 | case "$dflt$osname" in |
230 | .os390) echo "(Attempting domain name extraction from //'SYS1.TCPPARMS(TCPDATA)')" | |
02129eaa | 231 | dflt=.`awk '/^DOMAINORIGIN/ {print $2}' "//'SYS1.TCPPARMS(TCPDATA)'" 2>/dev/null` |
8b0d65e1 JH |
232 | ;; |
233 | esac | |
edd6115f JH |
234 | case "$dflt" in |
235 | .) echo "(Lost all hope -- silly guess then)" | |
ce23e7f2 | 236 | dflt='.nonet' |
edd6115f JH |
237 | ;; |
238 | esac | |
239 | $rm -f hosts | |
240 | ;; | |
241 | *) dflt="$mydomain";; | |
242 | esac;; | |
243 | esac | |
244 | echo " " | |
245 | rp="What is your domain name?" | |
246 | . ./myread | |
247 | tans="$ans" | |
248 | case "$ans" in | |
249 | '') ;; | |
250 | .*) ;; | |
251 | *) tans=".$tans";; | |
252 | esac | |
253 | mydomain="$tans" | |
254 | ||
255 | : translate upper to lower if necessary | |
256 | case "$mydomain" in | |
257 | *[A-Z]*) | |
258 | echo "(Normalizing case in your domain name)" | |
259 | mydomain=`echo $mydomain | ./tr '[A-Z]' '[a-z]'` | |
260 | ;; | |
261 | esac | |
262 | ||
263 | @end | |
264 | : a little sanity check here | |
265 | case "$phostname" in | |
266 | '') ;; | |
267 | *) | |
268 | case `$phostname | ./tr '[A-Z]' '[a-z]'` in | |
269 | $myhostname$mydomain|$myhostname) ;; | |
270 | *) | |
271 | case "$phostname" in | |
272 | sed*) | |
273 | echo "(That doesn't agree with your whoami.h file, by the way.)" | |
274 | ;; | |
275 | *) | |
276 | echo "(That doesn't agree with your $phostname command, by the way.)" | |
277 | ;; | |
278 | esac | |
279 | ;; | |
280 | esac | |
281 | ;; | |
282 | esac | |
283 |