home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / info-service / gopher / Unix / GopherTools / gophertree1.0 < prev    next >
Encoding:
Text File  |  1992-08-17  |  6.0 KB  |  224 lines

  1. #!/usr/local/bin/perl
  2. # gophertree v1.0
  3. # Prints pretty indented listings of a Gopher menu tree
  4. # Copyright (C) 1992, Trustees of Michigan State University
  5. # Modifications:
  6. # Original author unknown
  7. # 07/07/92 Boone      Major conversion from gopherls
  8. # 08/14/92 Boone      Fixes: 
  9. #                        added code to allow command line limit on recursion 
  10. #                           depth
  11. #                        quit indenting after 15 levels to avoid filling the
  12. #                           title field with spaces
  13. #                        changed to use IP address instead of hostname when
  14. #                           checking for off-host links; this should make
  15. #                           aliased machine names (e.g. gopher.someschool.edu)
  16. #                           work much better
  17. #                     Enhancements:
  18. #                        Added option to list only directories
  19. #                        Changed limit on number of items listed to apply to
  20. #                           all types except directories, instead of just
  21. #                           files; still no per-type limits though
  22. #                        Changed command line processing to use Getopts, 
  23. #                        allowing better option processing
  24. # End Modifications
  25.  
  26. require "getopts.pl";
  27.  
  28. sub dokill
  29. {
  30.     kill 9,$child if $child;
  31. }
  32.  
  33. sub Opengopher
  34. {
  35.     $sockaddr++;
  36.     local($them,$port) = @_;    
  37.     $them = 'localhost' unless $them;
  38.  
  39.     $AF_INET = 2;
  40.     $SOCK_STREAM = 1;
  41.  
  42.     $SIG{'INT'} = 'dokill';
  43.  
  44.     $sockaddr = 'S n a4 x8';
  45.  
  46.     chop($hostname = `hostname`);
  47.  
  48.     ($name,$aliases,$proto) = getprotobyname('tcp');
  49.     ($name,$aliases,$port) = getservbyname($port,'tcp')
  50.     unless $port =~ /^\d+$/;;
  51.     ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
  52.     ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
  53.  
  54.     $this = pack($sockaddr, $AF_INET, $sockaddr, $thisaddr);
  55.     $that = pack($sockaddr, $AF_INET, $port, $thataddr);
  56.  
  57.     # Make the socket filehandle.
  58.     socket(S, $AF_INET, $SOCK_STREAM, $proto) || die $!;
  59.  
  60.     # Give the socket an address.
  61.     bind(S, $this) || die $!;
  62.  
  63.     # Call up the server.
  64.     connect(S,$that) || die $!;
  65.  
  66.     # Set socket to be command buffered.
  67.     select(S); $| = 1; select(STDOUT);
  68.  
  69. }
  70.  
  71. sub GetList 
  72. {
  73.     local($CurrentHost, $Port, $Path, $indent) = @_;
  74.     local(@dirx, $Name, $Obj, $fname, $ftype, $fhost, %i, $truncated);
  75.     &Opengopher($CurrentHost, $Port);
  76.     print S "$Path\n";
  77.     @dirx = <S>;
  78.     close(S);
  79.     $truncated = 0;
  80.     foreach (@dirx) 
  81.     {
  82.         last if /^\./;
  83.         chop; chop;
  84.         ($ObjName, $Path, $CurrentHost, $Port) = split('\t', $_);
  85.         $Name = substr($ObjName, 1);
  86.         $Obj = substr($ObjName, 0, 1);
  87.         $fname = $indent . $Name;
  88.         $ftype = "";
  89.         $ftype = "File" if ($Obj eq "0");
  90.         $ftype = "Dir" if ($Obj eq "1");
  91.         $ftype = "Phone" if ($Obj eq "2");
  92.         $ftype = "Error" if ($Obj eq "3");
  93.         $ftype = "MacHqx" if ($Obj eq "4");
  94.         $ftype = "PcHqx" if ($Obj eq "5");
  95.         $ftype = "Uue" if ($Obj eq "6");
  96.         $ftype = "Index" if ($Obj eq "7");
  97.         $ftype = "Telnet" if ($Obj eq "8");
  98.         $ftype = "Bin" if ($Obj eq "9");
  99.         $ftype = "File" if ($Obj eq "R");
  100.         $ftype = "TN3270" if ($Obj eq "T");
  101.         $ftype = "File" if ($Obj eq "e");
  102.         $ftype = "Ftp" if ($Obj eq "f");
  103.         $ftype = "HTML" if ($Obj eq "h");
  104.         $ftype = "Info" if ($Obj eq "i");
  105.         $ftype = "Mail" if ($Obj eq "m");
  106.         $ftype = "Sound" if ($Obj eq "s");
  107.         $ftype = "Index" if ($Obj eq "w");
  108.         $fhost = $CurrentHost;
  109.  
  110.         $writeme = 1;
  111.         if ((! $opt_d) && ($Obj ne "1") && ($i{$Obj} > $breaklong))
  112.         {
  113.             $writeme = 0;
  114.             $truncated = 1;
  115.         }
  116.         if ($Obj eq "i")        { $writeme = 0; }
  117.         if ($opt_d && ($Obj ne "1"))    { $writeme = 0; }
  118.         if ($writeme)            { write; }
  119.  
  120.         if ($hostable{$CurrentHost} eq "")
  121.         {
  122.             $hostable{$CurrentHost} =
  123.                 unpack("L", (gethostbyname($CurrentHost))[4]);
  124.         }
  125.  
  126.         if (($Obj eq "1") && 
  127.             ($hostable{$CurrentHost} eq $hostable{$firsthost}) && 
  128.             (($Port != $firstport) || ($Path != ""))) 
  129.         {
  130.             $newindent = $indent;
  131.             if ($depth < $nomoreindent)
  132.             {
  133.                 $newindent .= $iadd;
  134.             }
  135.             $depth++;
  136.             if ($depth <= $maxdepth)
  137.             {
  138.                 &GetList($CurrentHost, $Port, 
  139.                     $Path, $newindent);
  140.             }
  141.             $depth--;
  142.         }
  143.  
  144.         $i{$Obj}++;
  145.     }
  146.     if ($truncated) 
  147.     {
  148.         print "$indent    . . .\n";
  149.         --$-;
  150.     }
  151. }
  152.  
  153. # **************************************************************************
  154. # * Main
  155. # **************************************************************************
  156.  
  157. # Parse command line
  158.  
  159.     &Getopts("b:l:dr:");
  160.     if ($#ARGV < 1) 
  161.     {
  162.         print "Usage: gophertree [-d -bn -ln -rn] host port path\n";
  163.         exit(1);
  164.     }
  165.  
  166.     $firsthost = $CurrentHost = $ARGV[0];
  167.     $firstport = $Port = $ARGV[1];
  168.     $Path = "";
  169.     if ($#ARGV == 2) 
  170.     {
  171.         $Path = $ARGV[2];
  172.     }
  173.  
  174. # Initialize some variables
  175.  
  176.     ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  177.     $today = $mon+1 . "/" . $mday . "/" . $year;
  178.  
  179.     $^ = 'TOP';         # Report header format
  180.     $~ = 'STDOUT';      # Report body format
  181.     $indent = "";       # Goes in front of description to indicate treeness
  182.     $depth = 1;         # How deep into the maze are we?
  183.     $hostable{$firsthost} = unpack("L", (gethostbyname($firsthost))[4]);
  184.  
  185. # CHANGE--User-configurable defaults
  186.  
  187.     $breaklong = 15;    # CHANGE--Where to break long lists
  188.     $= = 55;            # CHANGE--Lines per page
  189.     $iadd = "  ";       # CHANGE--Amount to indent each new level
  190.     $nomoreindent = 15; # CHANGE--How far down before we quit indenting
  191.     $maxdepth = 999;    # CHANGE--How deep to go before "pruning" layers
  192.  
  193. # Stuff command line changes into the config variables
  194.  
  195.     if ($opt_b) { $breaklong = $opt_b; }
  196.     if ($opt_l) { $= = $opt_l; }
  197.     if ($opt_r) { $maxdepth = $opt_r; }
  198.  
  199. # Real work
  200.  
  201.     &GetList($CurrentHost, $Port, $Path, $indent);
  202.     exit(0);
  203.  
  204. #
  205. # Formats
  206. #
  207.  
  208. format TOP =
  209.  
  210. MSU Gopher Road Map                      Host: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  211. $firsthost
  212. Last Update: @<<<<<<<                                               Page: @>>
  213. $today $%
  214.  
  215. Lev Name                                       Type   Destination
  216. --- ------------------------------------------ ------ -------------------------
  217. .
  218.  
  219. format STDOUT =
  220. @>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<
  221. $depth $fname $ftype $fhost
  222. .
  223.