sub cfghost {my $F='cfghost.pm'; local($cfile,$host) = @_;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Given a config-file path and a host name, we extract the info from the file #
# and  save it in @cfg.  We also look for a few specific lines and save their #
# data in some other global arrays.  We return the number of lines read  from #
# the config file, or undef if we can't open the file.                        #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($cfg,$flg,$i,$line,$lines,$opt,$pat);
	print V "$F: Called with V='$V'\n" if $V>1;
	print V "$F: cfile=\"$cfile\"\n" if $V>1;
	print V "$F:  host=\"$host\"\n" if $V>1;
	return undef unless $cfile;	# Fail if file name is missing or null
	@cfg = ();
	unless (open(CFG,$cfile)) {
		print V "$F: Can't read config file \"$cfile\" ($!)\n" if $V>1;
		return undef;		# Fail if we can't read the file
	}
	print V "$F: Reading config info from \"$cfile\"\n" if $V>1;
	$lines = 0;
line:
	for $line (<CFG>) {
		++$lines;
		$line =~ s/[\s\r]+$//;
		print V "$F: LINE:\"$line\"\n" if $V>2;
		next unless $line;			# Ignore blank lines
		if ($line =~ /^\s*#/) {		# Ignore comment lines
			print V "$F: Ignore: \"$line\"\n" if $V>3;
			next line;
		}
		if ($line =~ /^[+0-9]*\s*(C)[:\s]\s*(.*)/) {	# C: command to get file
			$Getcmd{"$1$host"} = $2;					# Getcmd{"Cfoo.bar"} is command for foo.bar
			print V "$F: Get command \"$2\" for $host (from cfg file)\n" if $V>1;
			next line;
		}
		if (($flg,$opt) = ($line =~ /^([-+])(CGI)\b/i)) {	# +CGI to allow CGI calls
			print V "$F: CGI option flg='$flg' opt='$opt' from \"$line\"\n" if $V>1;
			print V "$F: CGI option " . $Opt{CGI} . " [line=\"$line\"]\n" if $V>0;
			++$allowcgi if $flg;
			print V "$F: allowcgi=$allowcgi.\n" if $V>0;
			next line;
		}
		if ($line =~ /^[+0-9]*\s*(D)[:\s]\s*(\d+)/) {	# D: Depth limit
			$DepthHost{$host} = $2;
			print V "$F: Depth limit \"$2\" for $host (from cfg file)\n" if $V>1;
			next line;
		}
		if ($line =~ /^[+0-9]*\s*(P)[:\s]\s*(\d+)/) {	# P: Protocol to use for this host
			$ProtHost{$host} = $2;
			print V "$F: Protocol \"$2\" for $host (from cfg file)\n" if $V>1;
			next line;
		}
		if ($line =~ /^[+0-9]*\s*(T)[:\s]\s*(\d+)/) {	# T: Time limit; exit if no tunes found in this many seconds
			$giveuptime = int($2);	
			print V "$F: Time limit \"$2\" for $host (from cfg file)\n" if $V>1;
			next line;
		}
		if ($line =~ /^[+0-9]*\s*(L)[:\s]\s*(\d+)/) {	# L: Line max before giving up
			$maxlines = $2;
			print V "$F: maxlines=$maxlines for $host (from cfg file)\n" if $V>1;
			next line;
		}
		if ($line =~ m"^URL[:\s]+(.*)") {	# URL listed as such
			print V "$F: URL to scan: \"$1\"\n" if $V>1;
			&URL($1,1);	# Add to list of URLs to scan
			next line;
		}
		if ($line =~ m"^(https*)://([^/]+)") {	# Just a URL; scan it
			print V "$F: Scan URL \"$line\"\n" if $V>1;
			&URL($line,1);	# Add to list of URLs to scan
			next line;
		}
		print V "$F: Not a URL [$line]\n" if $V>2;
		if (($cfg,$flg,$opt) = ($line =~ /^[+\d]*\s*(\w)[:\s]\s*([-+]*)(.+)/)) {	# X:name
			print V "$F: $cfg: '$flg' \"$opt\" option.\n" if $V>1;
			if ($cfg eq 'O') {
				print V "$F: O: '$flg' \"$opt\" option.\n" if $V>1;
				if ($opt =~ /^(CGI|tagP)$/i) {	# On/off controls
					print V "$F: Set $opt option to '$flg'\n" if $V>1;
					$Opt{$opt} = $flg;	# Remember this option's on/off flag
				} elsif ($opt =~ /^U(\d+)$/i) {	# URL limit
					$maxurls = int($1);
					print V "$F: Limit to maxurls=$maxurls URLs\n" if $V>1;
				} else {
					print V "$F: Unrecognized option '$opt' in \"$line\"\n" if $V>1;
				}
			} else {
				print V "$F: Unrecognized config command '$cfg' in \"$line\"\n" if $V>1;
			}
			next line;
		}
		if ($line =~ m"^HTTP/([.\d]+)") {			# HTTP/1.0
			$HTTPversion = $1;		# Global notice of this value
			print V "$F: HTTPversion=\"$HTTPversion\"\n" if $V>0;
			next line;
		}
		if ($line =~ /^Crawl-delay:*\s*(\d+)/) {
			$crawldelay = int($1);		# Global notice of this value
			print V "$F: crawldelay=$crawldelay [$line]\n" if $V>0;
			next line;
		}
		if ($line =~ /^(delay):*\s*(\d+)/i) {		# delay: seconds
			print V "$F: crawldelay=$2 sec. [$line]\n" if $V>3;
			$crawldelay = int($2);			 # Min time between requests
			next line;
		}
		if ($line =~ m"^Avoid[:\s]\s*(.*)\s*$"i) {	# Hosts to avoid
			print V "$F: Avoid \"$1\"\n" if $V>1;
			&avoid($1);
			next line;
		}
		if ($line =~ /^Disallow:*\s*(.*)$/) {		# Disallow: URI
			if ($1) {
				$Disallow{$1}++;
				$hstmsg = "Disallow \"$1\"";
				print V "$F: $hstmsg\n" if $V>0;
			}
			next line;
		}
		if ($line =~ s"^(Rpl|Replace|Rewrite)[:\s]\s*(.+)$"$1"i) {    # URL edits
			$pat = $2;
			print V "$F: Rewrite \"$pat\"\n" if $V>1;
			&cfgRewrite($pat);
			next line;
		}
		if ($line =~ /^QuotesOK/) {		# Disallow: URI
			$QuotesOK = 1;
			print V "$F: Quotes allowed in URLs.\n" if $V>1;
			next line;
		}
		if ($line =~ /^SCDkludge/) {	# Ignore T: lines that look like dance code
			$SCDkludge = 1;
			print V "$F: SCDkludge: '$SCDkludge'\n" if $V>1;
			next line;
		}
		print V "$F: Config line \"$line\" not matched.\n" if $V>1;
		push @cfg, $line;
	}
	close CFG;
	return $lines;
}

1;
