xref: /titanic_44/usr/src/cmd/ypcmd/multi.awk.sh (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate#!/usr/bin/nawk -f
2*7c478bd9Sstevel@tonic-gate#
3*7c478bd9Sstevel@tonic-gate# CDDL HEADER START
4*7c478bd9Sstevel@tonic-gate#
5*7c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*7c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*7c478bd9Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*7c478bd9Sstevel@tonic-gate# with the License.
9*7c478bd9Sstevel@tonic-gate#
10*7c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*7c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*7c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
13*7c478bd9Sstevel@tonic-gate# and limitations under the License.
14*7c478bd9Sstevel@tonic-gate#
15*7c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*7c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*7c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*7c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*7c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*7c478bd9Sstevel@tonic-gate#
21*7c478bd9Sstevel@tonic-gate# CDDL HEADER END
22*7c478bd9Sstevel@tonic-gate#
23*7c478bd9Sstevel@tonic-gate#
24*7c478bd9Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
25*7c478bd9Sstevel@tonic-gate#
26*7c478bd9Sstevel@tonic-gate# Copyright (c) 1996, by Sun Microsystems, Inc.
27*7c478bd9Sstevel@tonic-gate# All rights reserved.
28*7c478bd9Sstevel@tonic-gate#
29*7c478bd9Sstevel@tonic-gate# Awk code to handle the creation of the YP_MULTI_ entries
30*7c478bd9Sstevel@tonic-gate# in the hosts.byname map.  Called by multi directly.
31*7c478bd9Sstevel@tonic-gate#
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate{
34*7c478bd9Sstevel@tonic-gate    # Here we loop through the list of hostnames
35*7c478bd9Sstevel@tonic-gate    # doing two separate things...
36*7c478bd9Sstevel@tonic-gate    # First, we're building a list of hostnames
37*7c478bd9Sstevel@tonic-gate    # for the current IP address ($1).
38*7c478bd9Sstevel@tonic-gate    # Second, if we've seen a name before then
39*7c478bd9Sstevel@tonic-gate    # we add the current address ($1) to a list
40*7c478bd9Sstevel@tonic-gate    # of address associated with this particular
41*7c478bd9Sstevel@tonic-gate    # name ($i).
42*7c478bd9Sstevel@tonic-gate    #
43*7c478bd9Sstevel@tonic-gate    # Note, that we're pretty careful about keeping
44*7c478bd9Sstevel@tonic-gate    # out duplicates (and this has a cost).
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate    for (i = 2; i <= NF; i++) {
47*7c478bd9Sstevel@tonic-gate	# Make the namelist for this address
48*7c478bd9Sstevel@tonic-gate	if (namelist[$1] == "") {
49*7c478bd9Sstevel@tonic-gate	    namelist[$1] = $i;
50*7c478bd9Sstevel@tonic-gate	} else if (namelist[$1] == $i) {
51*7c478bd9Sstevel@tonic-gate	    ;
52*7c478bd9Sstevel@tonic-gate	} else if (index(namelist[$1], $i) == 0) {
53*7c478bd9Sstevel@tonic-gate	    namelist[$1] = namelist[$1] " " $i;
54*7c478bd9Sstevel@tonic-gate	} else {
55*7c478bd9Sstevel@tonic-gate	    nf = 1;
56*7c478bd9Sstevel@tonic-gate	    numnames = split(namelist[$1], n);
57*7c478bd9Sstevel@tonic-gate	    for (j = 1; j <= numnames; j++) {
58*7c478bd9Sstevel@tonic-gate		if (n[j] == $i) {
59*7c478bd9Sstevel@tonic-gate		    nf = 0;
60*7c478bd9Sstevel@tonic-gate		    break;
61*7c478bd9Sstevel@tonic-gate		}
62*7c478bd9Sstevel@tonic-gate	    }
63*7c478bd9Sstevel@tonic-gate	    if (nf) {
64*7c478bd9Sstevel@tonic-gate		namelist[$1] = namelist[$1] " " $i;
65*7c478bd9Sstevel@tonic-gate		nf = 0;
66*7c478bd9Sstevel@tonic-gate	    }
67*7c478bd9Sstevel@tonic-gate	}
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate	# Do we have an address for this name?
70*7c478bd9Sstevel@tonic-gate        # If not, and it's not already there, add it.
71*7c478bd9Sstevel@tonic-gate	if (addr[$i] == "") {
72*7c478bd9Sstevel@tonic-gate	    addr[$i] = $1;
73*7c478bd9Sstevel@tonic-gate	} else if (index(addr[$i], $1) == 0) {
74*7c478bd9Sstevel@tonic-gate	    addr[$i] = addr[$i] "," $1
75*7c478bd9Sstevel@tonic-gate	}
76*7c478bd9Sstevel@tonic-gate    }
77*7c478bd9Sstevel@tonic-gate}
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gateEND {
80*7c478bd9Sstevel@tonic-gate    # There are now a bunch o addresses in the addr
81*7c478bd9Sstevel@tonic-gate    # array that are actually lists.  We go through
82*7c478bd9Sstevel@tonic-gate    # all of them here and build a list of hostname
83*7c478bd9Sstevel@tonic-gate    # aliases into the namelist array.
84*7c478bd9Sstevel@tonic-gate    #
85*7c478bd9Sstevel@tonic-gate    for (host in addr) {
86*7c478bd9Sstevel@tonic-gate	if (index(addr[host], ",") == 0)
87*7c478bd9Sstevel@tonic-gate	    continue;
88*7c478bd9Sstevel@tonic-gate	numaddr = split(addr[host], tmpaddr, ",");
89*7c478bd9Sstevel@tonic-gate	for (i = 1; i <= numaddr; i++) {
90*7c478bd9Sstevel@tonic-gate	    numnames = split(namelist[tmpaddr[i]], tmpname);
91*7c478bd9Sstevel@tonic-gate	    for (j = 1; j <= numnames; j++) {
92*7c478bd9Sstevel@tonic-gate		if (namelist[addr[host]] == "") {
93*7c478bd9Sstevel@tonic-gate		    namelist[addr[host]] = tmpname[j];
94*7c478bd9Sstevel@tonic-gate		    continue;
95*7c478bd9Sstevel@tonic-gate		}
96*7c478bd9Sstevel@tonic-gate		if (namelist[addr[host]] == tmpname[j]) {
97*7c478bd9Sstevel@tonic-gate		    continue;
98*7c478bd9Sstevel@tonic-gate		}
99*7c478bd9Sstevel@tonic-gate		if (index(namelist[addr[host]], tmpname[j]) == 0) {
100*7c478bd9Sstevel@tonic-gate		    namelist[addr[host]] = namelist[addr[host]] " " tmpname[j];
101*7c478bd9Sstevel@tonic-gate		    continue;
102*7c478bd9Sstevel@tonic-gate		} else {
103*7c478bd9Sstevel@tonic-gate		    nf = 1;
104*7c478bd9Sstevel@tonic-gate		    for (k = 1; k <= numnames; k++) {
105*7c478bd9Sstevel@tonic-gate			if (tmpname[j] == tmpname[k]) {
106*7c478bd9Sstevel@tonic-gate			    nf = 0;
107*7c478bd9Sstevel@tonic-gate			    break;
108*7c478bd9Sstevel@tonic-gate			}
109*7c478bd9Sstevel@tonic-gate		    }
110*7c478bd9Sstevel@tonic-gate		    if (nf == 1) {
111*7c478bd9Sstevel@tonic-gate			namelist[addr[host]] = namelist[addr[host]] " " tmpname[j];
112*7c478bd9Sstevel@tonic-gate			nf = 1;
113*7c478bd9Sstevel@tonic-gate		    }
114*7c478bd9Sstevel@tonic-gate		}
115*7c478bd9Sstevel@tonic-gate	    }
116*7c478bd9Sstevel@tonic-gate	}
117*7c478bd9Sstevel@tonic-gate    }
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate    # Now do that funky output thang...
120*7c478bd9Sstevel@tonic-gate    for (host in addr) {
121*7c478bd9Sstevel@tonic-gate	if (index(addr[host], ",")) {
122*7c478bd9Sstevel@tonic-gate	    printf("YP_MULTI_");
123*7c478bd9Sstevel@tonic-gate	}
124*7c478bd9Sstevel@tonic-gate	printf("%s %s\t%s\n",
125*7c478bd9Sstevel@tonic-gate	       host, addr[host], namelist[addr[host]]);
126*7c478bd9Sstevel@tonic-gate    }
127*7c478bd9Sstevel@tonic-gate}
128