$OpenBSD: patch-bin_fnrancid_in,v 1.3 2009/02/11 13:17:28 pea Exp $
--- bin/fnrancid.in.orig	Fri Feb  8 10:59:08 2008
+++ bin/fnrancid.in	Tue Feb 10 14:21:53 2009
@@ -172,6 +172,7 @@ sub GetSystem {
 
     while (<INPUT>) {
 	tr/\015//d;
+	s/exit//gi ;
 	next if /^\s*$/;
 	last if(/$prompt/);
 	ProcessHistory("","","","$_");
@@ -202,6 +203,15 @@ sub GetConf {
 	    next;
 	}
 	ProcessHistory("","","","$_");
+if (length($host) == 0) {
+    if ($file) {
+	print(STDERR "Too few arguments: file name required\n");
+	exit(1);
+    } else {
+	print(STDERR "Too few arguments: host name required\n");
+	exit(1);
+    }
+}
 	#print STDOUT "$_";
     }
     $found_end=1;
@@ -214,7 +224,7 @@ sub DoNothing {print STDOUT;}
 # Main
 @commandtable = (
 	{'get system status'	=> 'GetSystem'},
-	{'get conf'		=> 'GetConf'}
+	{'show'		=> 'GetConf'}
 );
 # Use an array to preserve the order of the commands and a hash for mapping
 # commands to the subroutine and track commands that have been completed.
@@ -242,13 +252,13 @@ if ($file) {
     print STDERR "opening file $host\n" if ($debug);
     print STDOUT "opening file $host\n" if ($log);
     open(INPUT,"<$host") || die "open failed for $host: $!\n"; } else {
-    print STDERR "executing nlogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($debug);
-    print STDOUT "executing nlogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($log);
+    print STDERR "executing fnlogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($debug);
+    print STDOUT "executing fnlogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($log);
     if (defined($ENV{NOPIPE})) {
-	system "nlogin -t $timeo -c \"$cisco_cmds\" $host </dev/null > $host.raw 2>&1" || die "nlogin failed for $host: $!\n";
-	open(INPUT, "< $host.raw") || die "nlogin failed for $host: $!\n";
+	system "fnlogin -t $timeo -c \"$cisco_cmds\" $host </dev/null > $host.raw 2>&1" || die "fnlogin failed for $host: $!\n";
+	open(INPUT, "< $host.raw") || die "fnlogin failed for $host: $!\n";
     } else {
-	open(INPUT,"nlogin -t $timeo -c \"$cisco_cmds\" $host </dev/null |") || die "nlogin failed for $host: $!\n";
+	open(INPUT,"fnlogin -t $timeo -c \"$cisco_cmds\" $host </dev/null |") || die "fnlogin failed for $host: $!\n";
     }
 }
 
@@ -276,13 +286,32 @@ ProcessHistory("","","","!RANCID-CONTENT-TYPE: fortiga
 TOP: while(<INPUT>) {
     tr/\015//d;
     if (/^Error:/) {
-	print STDOUT ("$host nlogin error: $_");
-	print STDERR ("$host nlogin error: $_") if ($debug);
+	print STDOUT ("$host fnlogin error: $_");
+	print STDERR ("$host fnlogin error: $_") if ($debug);
 	last;
     }
-    while (/>\s*($cmds_regexp)\s*$/) {
-	$cmd = $1;
-	if (!defined($prompt)) { $prompt = " >\s*"; }
+    while (/^.+(#|\$)\s*($cmds_regexp)\s*$/) {
+		$cmd = $2;
+		# - FortiGate prompts end with either '#' or '$'. Further, they may
+# be prepended with a '~' if the hostname is too long. Therefore, 
+# we need to figure out what our prompt really is.
+		if (!defined($prompt)) {
+		    if ( $_ =~ m/^.+\~\$/ ) {
+			$prompt = '\~\$ .*' ;
+		    } else {
+			if ( $_ =~ m/^.+\$/ ) {
+			    $prompt = ' \$ .*' ;
+			} else {
+			    if ( $_ =~ m/^.+\~#/ ) {
+				 $prompt = '\~# .*' ;
+			     } else {
+				 if ( $_ =~ m/^.+#/ ) {
+				      $prompt = ' # .*' ;
+				  }
+			     }
+			}
+		    }
+		}
 	print STDERR ("HIT COMMAND:$_") if ($debug);
 	if (!defined($commands{$cmd})) {
 	    print STDERR "$host: found unexpected command - \"$cmd\"\n";
