1#!/usr/local/bin/perl
2#
3# Public domain
4#
5# manlinks.pl: Extract manual links from mdoc source.
6#
7
8my $man = $ARGV[0];
9my $ns = 0;
10
11unless ($man) {
12	print STDERR "Usage: $0 [man]\n";
13	exit 1;
14}
15
16$man =~ s/([\w\-]+)\.(\d)$/$1/;
17$section = $2;
18
19while (<STDIN>) {
20	if (/^\.\\"\s*MANLINK\s*\(([\w\-]+)\)/) {
21		print "MANLINKS+=$man.${section}:$1.${section}\n";
22		print "CATLINKS+=$man.cat${section}:$1.cat${section}\n";
23	}
24	if (/^\.nr nS 1/) { $ns = 1; }
25	elsif (/^\.nr nS 0/ || /^\.\\" NOMANLINK/) { $ns = 0; }
26	next unless $ns;
27	if (/^\.Fn ([\w\-]+)\s+/ &&
28	    $1.'.'.$section ne $man.'.'.$section) {
29		print "MANLINKS+=$man.${section}:$1.${section}\n";
30		print "CATLINKS+=$man.cat${section}:$1.cat${section}\n";
31	}
32}
33