File: mkhpath.sh

package info (click to toggle)
smail 3.2.0.102-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,228 kB
  • ctags: 3,924
  • sloc: ansic: 41,366; sh: 3,434; makefile: 2,349; awk: 689; perl: 598; yacc: 427; sed: 2
file content (232 lines) | stat: -rw-r--r-- 6,606 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
# @(#) mkhpath.sh,v 1.5 1992/07/11 11:40:06 tron Exp

#
#    Copyright (C) 1988 Ronald S. Karr and Landon Curt Noll
#    Copyright (C) 1992 Ronald S. Karr
#
# See the file COPYING, distributed with smail, for restriction
# and warranty information.

# mkhostpath - build a pathalias database from a 'hosts' table
#
# Usage: mkhostpath [-n netname] [-c cost] [-g gateway] [-d] [ - | filename ]
#
#	-n netname	- form a map (not pathalais output) name the net netname
#	-c cost		- set cost to gateway, or network is no gateway
#	-g gateway	- set the gateway for hosts to be gateway
#	-d		- only print lines that has domain hostnames
#	-		- read hosts table from stdin
#	filename	- read hosts table from filename
#	<no arg>	- read hosts from /etc/hosts
#
# The mkhostpath(8) command reads a hosts table from 'filename' or
# standard input if '-' is used, and constructs routes to that network.
# The hosts table is assumed to be in the format of the /etc/hosts
# database file.  That is, the first field (internet address) and any,
# domain based name (field containing a '.') are ignored.  The hosts
# 'localhost' and 'loghost' are also ignored.  Comment lines, beginning
# with a '#', and blank lines are also ignored.  If '-d' is specified
# and address does NOT contain an alias that is a domain based name,
# then that line is ignored.  (useful to ignore test lines)
#
# By default, mkhostpath(8) builds route table to the network in the format
# of the output of pathalias(8) with the '-i' option.  The '-n' network flag
# overrides this default.
#
# If a gateway is specified with the '-g' option, but the '-n' network
# option is not used, then all routes to the network (except for the route
# to the gateway) are prepended with the route "gateway!".  A route to from
# the localhost to the gateway is explicitly formed.
#
# If neither the '-g' gateway nor the '-n' network options are given,
# then direct routes from the localhost to each host are constructed.
#
# The mkhostpath command will build a pathalias map to the network if the
# name of the network is given via the '-n' option.  The construction of
# the map depends on if the '-g' gateway option is given.
#
# When the '-g' gateway and the '-n' network options are both used, then a
# route from the localhost to the gateway is established, and the gateway is
# inserted into the network list.  The localhost is not added to
# the network list, even if it appears as a sitename in the hosts table.
# The cost of the link between the localhost and the gateway is 'LOCAL',
# unless overridden by the '-c' cost option.  The cost of routes inside
# the network is fixed at 'LOCAL' and is not changed by the '-c' cost option.
#
# If no gateway is specified with the '-n' network option, then it is
# assumed that the localhost is in the network and thus the localhost
# is inserted into the network list.  The cost of routes inside the network
# is assumed to be 'LOCAL', unless changed by the '-c' cost option.
#
# The value of the '-c' option may be any valid pathalias(8) cost expression.
# The '-c' flag is ignored if the '-n' flag is not given.


# base directory for smail library/utility
#
# These directories should be owned by root, and only writeable by root
#
PATH="X_UTIL_PATH_X:X_SECURE_PATH_X"; export PATH
UTIL_BIN_DIR=X_UTIL_BIN_DIR_X
TMPDIR=X_TMP_DIR_X

# standard locations
#
DCASEHOST=dcasehost
SORT=sort
HOSTS=/etc/hosts
SMAIL=X_SMAIL_NAME_X
GETOPT=$UTIL_BIN_DIR/getopt

# find out who we are
hostname="`$SMAIL -bP uucp_name`"

# parse - need public domain getopt - XXX
#
gateway=
gate=
GATE=0
netname=
cost=LOCAL
dotline=0
PROG=$0
set -- `$GETOPT -n $PROG -q n:c:g:d $*`
if [ "$?" -ne 0 ]; then
	echo "usage: $PROG [-n netname] [-c cost] [-g gateway] [-d] [- | filename]" 2>&1
	exit 1
fi
for i in $*; do
	case "$i" in
	-g) gate="$2"; gateway="$gate!"; GATE=1; shift 2;;
	-n) netname="$2"; shift 2;;
	-c) cost="$2"; shift 2;;
	-d) dotline=1; shift;;
	--) shift; break;;
	esac
done

# select where input is to come from
#
if [ "$#" -eq 1 ]; then
	case "$1" in
	-)	cat ;;			# `-' then stdin
	*)	cat "$1" ;;		# filename then that file
	esac
else 
	cat $HOSTS		# no argument then /etc/hosts
fi |
sed 's/[	 ]*#.*$//' |	# strip #-style comments
if [ -z "$netname" ]; then

	# pathalias style output

	# Make entries for lines which contain more than just an
	# initial inet address, localhost or loghost 
	awk 'NF > 1 {
	    if (DOTLINE == 1) {
		found = 0;
		for (i = 2; i <= NF && found == 0; ++i) {
		    if ($i ~ /\./) {
			found = 1;
		    }
		}
		if (found == 0) {
		    continue;
		}
	    }
	    for (i = 2; i <= NF; i++) {
		if ($i !~ /\./ && $i != "'$hostname'" && $i != "localhost" && $i != "loghost" && $i != "'$gate'") {
		    if (length($i) < 8) {
			tab = "\t\t";
		    } else {
			tab = "\t";
		    }
		    if (GATE > 0) {
			print $i tab "'$gate'!"$i"!%s";
		    } else {
			print $i tab $i"!%s";
		    }
		}
		if ($i == "'$hostname'") {
		    if (length($i) < 8) {
			tab = "\t\t";
		    } else {
			tab = "\t";
		    }
		    print $i tab "!%s	0";
		}
	    }
	}
	END {
	    if (GATE > 0) {
		if (length("'$gate'") < 8) {
		    tab = "\t\t";
		} else {
		    tab = "\t";
		}
		print "'$gate'" tab "'$gate'!%s";
	    }
	}' GATE="$GATE" DOTLINE=$dotline - | $DCASEHOST | $SORT -u -T $TMPDIR

else

	# output in the format of pathalias map input 

	# output the initial map data
	echo "file { [mkhostpath] }"
	if [ "$GATE" -gt 0 ]; then
		echo "$hostname	$gate($cost)"
		# the gateway will be the first listed site
		first_site="$gate";
	else
		first_site="$hostname";
	fi

	# Make entries for lines which contain more than just an
	# initial inet address, localhost or loghost.  Force the
	# first_site to be first in the list.

	awk 'BEGIN {
	    line = "'$netname' = { '$first_site'";
	    line_len = length(line);
	}
	NF > 1 {
	    if (DOTLINE == 1) {
		found = 0;
		for (i = 2; i <= NF && found == 0; ++i) {
		    if ($i ~ /\./) {
			found = 1;
		    }
		}
		if (found == 0) {
		    continue;
		}
	    }
	    for (i = 2; i <= NF; i++) {
		if ($i !~ /\./ && $i != "localhost" && $i != "loghost" && $i != "'$first_site'" && $i != "'$hostname'") {
		    field_len = length($i);
		    if (field_len+line_len > 76) {
			print line",";
			line = "    "$i;
			line_len = 4 + field_len;
		    } else {
		        line = line", "$i;
			line_len += field_len + 2;
		    }
		}
	    }
	}
	END {
	    line2 = " }(LOCAL)";
	    field_len = length(line2);
	    if (field_len+line_len > 76) {
		print line;
		print "    " line2;
	    } else {
		print line line2;
	    }
	}' GATE="$GATE" DOTLINE=$dotline -

fi
exit 0