home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / contrib / smail / smail-3.1 / smail-3 / smail-3.1.28 / util / mkhpath.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1992-07-11  |  7KB  |  233 lines

  1. #!/bin/sh
  2. # @(#)util/mkhpath.sh    1.5 7/11/92 11:40:06
  3.  
  4. #
  5. #    Copyright (C) 1988 Ronald S. Karr and Landon Curt Noll
  6. #    Copyright (C) 1992 Ronald S. Karr
  7. #
  8. # See the file COPYING, distributed with smail, for restriction
  9. # and warranty information.
  10.  
  11. # mkhostpath - build a pathalias database from a 'hosts' table
  12. #
  13. # Usage: mkhostpath [-n netname] [-c cost] [-g gateway] [-d] [ - | filename ]
  14. #
  15. #    -n netname    - form a map (not pathalais output) name the net netname
  16. #    -c cost        - set cost to gateway, or network is no gateway
  17. #    -g gateway    - set the gateway for hosts to be gateway
  18. #    -d        - only print lines that has domain hostnames
  19. #    -        - read hosts table from stdin
  20. #    filename    - read hosts table from filename
  21. #    <no arg>    - read hosts from /etc/hosts
  22. #
  23. # The mkhostpath(8) command reads a hosts table from 'filename' or
  24. # standard input if '-' is used, and constructs routes to that network.
  25. # The hosts table is assumed to be in the format of the /etc/hosts
  26. # database file.  That is, the first field (internet address) and any,
  27. # domain based name (field containing a '.') are ignored.  The hosts
  28. # 'localhost' and 'loghost' are also ignored.  Comment lines, beginning
  29. # with a '#', and blank lines are also ignored.  If '-d' is specified
  30. # and address does NOT contain an alias that is a domain based name,
  31. # then that line is ignored.  (useful to ignore test lines)
  32. #
  33. # By default, mkhostpath(8) builds route table to the network in the format
  34. # of the output of pathalias(8) with the '-i' option.  The '-n' network flag
  35. # overrides this default.
  36. #
  37. # If a gateway is specified with the '-g' option, but the '-n' network
  38. # option is not used, then all routes to the network (except for the route
  39. # to the gateway) are prepended with the route "gateway!".  A route to from
  40. # the localhost to the gateway is explicitly formed.
  41. #
  42. # If neither the '-g' gateway nor the '-n' network options are given,
  43. # then direct routes from the localhost to each host are constructed.
  44. #
  45. # The mkhostpath command will build a pathalias map to the network if the
  46. # name of the network is given via the '-n' option.  The construction of
  47. # the map depends on if the '-g' gateway option is given.
  48. #
  49. # When the '-g' gateway and the '-n' network options are both used, then a
  50. # route from the localhost to the gateway is established, and the gateway is
  51. # inserted into the network list.  The localhost is not added to
  52. # the network list, even if it appears as a sitename in the hosts table.
  53. # The cost of the link between the localhost and the gateway is 'LOCAL',
  54. # unless overridden by the '-c' cost option.  The cost of routes inside
  55. # the network is fixed at 'LOCAL' and is not changed by the '-c' cost option.
  56. #
  57. # If no gateway is specified with the '-n' network option, then it is
  58. # assumed that the localhost is in the network and thus the localhost
  59. # is inserted into the network list.  The cost of routes inside the network
  60. # is assumed to be 'LOCAL', unless changed by the '-c' cost option.
  61. #
  62. # The value of the '-c' option may be any valid pathalias(8) cost expression.
  63. # The '-c' flag is ignored if the '-n' flag is not given.
  64.  
  65.  
  66. # base directory for smail library/utility
  67. #
  68. # These directories should be owned by root, and only writeable by root
  69. #
  70. PATH="X_UTIL_PATH_X:X_SECURE_PATH_X"; export PATH
  71. UTIL_BIN_DIR=X_UTIL_BIN_DIR_X
  72. TMPDIR=X_TMP_DIR_X
  73.  
  74. # standard locations
  75. #
  76. DCASEHOST=dcasehost
  77. SORT=sort
  78. HOSTS=/etc/hosts
  79. SMAIL=X_SMAIL_NAME_X
  80. GETOPT=$UTIL_BIN_DIR/getopt
  81.  
  82. # find out who we are
  83. hostname="`$SMAIL -bP uucp_name`"
  84.  
  85. # parse - need public domain getopt - XXX
  86. #
  87. gateway=
  88. gate=
  89. GATE=0
  90. netname=
  91. cost=LOCAL
  92. dotline=0
  93. PROG=$0
  94. set -- `$GETOPT -n $PROG -q n:c:g:d $*`
  95. if [ "$?" -ne 0 ]; then
  96.     echo "usage: $PROG [-n netname] [-c cost] [-g gateway] [-d] [- | filename]" 2>&1
  97.     exit 1
  98. fi
  99. for i in $*; do
  100.     case "$i" in
  101.     -g) gate="$2"; gateway="$gate!"; GATE=1; shift 2;;
  102.     -n) netname="$2"; shift 2;;
  103.     -c) cost="$2"; shift 2;;
  104.     -d) dotline=1; shift;;
  105.     --) shift; break;;
  106.     esac
  107. done
  108.  
  109. # select where input is to come from
  110. #
  111. if [ "$#" -eq 1 ]; then
  112.     case "$1" in
  113.     -)    cat ;;            # `-' then stdin
  114.     *)    cat "$1" ;;        # filename then that file
  115.     esac
  116. else 
  117.     cat $HOSTS        # no argument then /etc/hosts
  118. fi |
  119. sed 's/[     ]*#.*$//' |    # strip #-style comments
  120. if [ -z "$netname" ]; then
  121.  
  122.     # pathalias style output
  123.  
  124.     # Make entries for lines which contain more than just an
  125.     # initial inet address, localhost or loghost 
  126.     awk 'NF > 1 {
  127.         if (DOTLINE == 1) {
  128.         found = 0;
  129.         for (i = 2; i <= NF && found == 0; ++i) {
  130.             if ($i ~ /\./) {
  131.             found = 1;
  132.             }
  133.         }
  134.         if (found == 0) {
  135.             continue;
  136.         }
  137.         }
  138.         for (i = 2; i <= NF; i++) {
  139.         if ($i !~ /\./ && $i != "'$hostname'" && $i != "localhost" && $i != "loghost" && $i != "'$gate'") {
  140.             if (length($i) < 8) {
  141.             tab = "\t\t";
  142.             } else {
  143.             tab = "\t";
  144.             }
  145.             if (GATE > 0) {
  146.             print $i tab "'$gate'!"$i"!%s";
  147.             } else {
  148.             print $i tab $i"!%s";
  149.             }
  150.         }
  151.         if ($i == "'$hostname'") {
  152.             if (length($i) < 8) {
  153.             tab = "\t\t";
  154.             } else {
  155.             tab = "\t";
  156.             }
  157.             print $i tab "!%s    0";
  158.         }
  159.         }
  160.     }
  161.     END {
  162.         if (GATE > 0) {
  163.         if (length("'$gate'") < 8) {
  164.             tab = "\t\t";
  165.         } else {
  166.             tab = "\t";
  167.         }
  168.         print "'$gate'" tab "'$gate'!%s";
  169.         }
  170.     }' GATE="$GATE" DOTLINE=$dotline - | $DCASEHOST | $SORT -u -T $TMPDIR
  171.  
  172. else
  173.  
  174.     # output in the format of pathalias map input 
  175.  
  176.     # output the initial map data
  177.     echo "file { [mkhostpath] }"
  178.     if [ "$GATE" -gt 0 ]; then
  179.         echo "$hostname    $gate($cost)"
  180.         # the gateway will be the first listed site
  181.         first_site="$gate";
  182.     else
  183.         first_site="$hostname";
  184.     fi
  185.  
  186.     # Make entries for lines which contain more than just an
  187.     # initial inet address, localhost or loghost.  Force the
  188.     # first_site to be first in the list.
  189.  
  190.     awk 'BEGIN {
  191.         line = "'$netname' = { '$first_site'";
  192.         line_len = length(line);
  193.     }
  194.     NF > 1 {
  195.         if (DOTLINE == 1) {
  196.         found = 0;
  197.         for (i = 2; i <= NF && found == 0; ++i) {
  198.             if ($i ~ /\./) {
  199.             found = 1;
  200.             }
  201.         }
  202.         if (found == 0) {
  203.             continue;
  204.         }
  205.         }
  206.         for (i = 2; i <= NF; i++) {
  207.         if ($i !~ /\./ && $i != "localhost" && $i != "loghost" && $i != "'$first_site'" && $i != "'$hostname'") {
  208.             field_len = length($i);
  209.             if (field_len+line_len > 76) {
  210.             print line",";
  211.             line = "    "$i;
  212.             line_len = 4 + field_len;
  213.             } else {
  214.                 line = line", "$i;
  215.             line_len += field_len + 2;
  216.             }
  217.         }
  218.         }
  219.     }
  220.     END {
  221.         line2 = " }(LOCAL)";
  222.         field_len = length(line2);
  223.         if (field_len+line_len > 76) {
  224.         print line;
  225.         print "    " line2;
  226.         } else {
  227.         print line line2;
  228.         }
  229.     }' GATE="$GATE" DOTLINE=$dotline -
  230.  
  231. fi
  232. exit 0
  233.