#!/usr/bin/env perl

##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************



# Perl script to print out the synopsis from the man page for a given
# system call.  If the man page source just says to look at a
# different syscall for the description (e.g. wait and waitpid), find
# the alternate man page and display the synopsis from there.
#
# Written 6/4/98 by Derek Wright 


######################################################################
# Figure out where things are.
######################################################################

$uname = `uname`;

if( -d "/usr/man" ) {
    $mandir = "/usr/man";
} elsif ( -d "/usr/share/catman/p_man" )  {
    $mandir = "/usr/share/catman/p_man";
} else { 
    $mandir = "/notthere";
}

if( -f "$mandir/man2/open.2" ) {
    $manext = "";
    $cat = "cat";
} elsif( -f "$mandir/man2/open.2.gz" ) {
    $manext = ".gz";
    $cat = "zcat";
} elsif( -f "$mandir/man2/open.2.Z" ) {
    $manext = ".Z";
    $cat = "zcat";
} elsif( -f "$mandir/cat2/standard/open.z" ) {
    $manext = ".z";
    $cat = "gzcat";
}

######################################################################
# Do the work
######################################################################

$syscall = $ARGV[0];
$file = find_file( $syscall );
do_it( $file );
print "\n";
exit( 0 );

######################################################################
# Subroutines
######################################################################

sub find_file {
    my( $sys ) = $_[0];
    my( $file, $i );

	foreach $i ("2", "3", "3c", "3b", "3t", "3s", "3n", "3r" ) {
	    $file = "$mandir/man$i/$sys.$i$manext";
	    if( -f $file ) { return $file; }
	}
    return "";
}


# Do the work:  
# If we weren't passed anything, we've got to return.
# If what we were passed does have a man page, we've got to return. 
# If we can display the line from what we were passed, we're done.
# If not, recursively call ourselves with the output from
#    find_alternate(), which sees if there's a different man page we
#    should display for this syscall.
sub do_it {
    my( $file ) = $_[0];
    if( !$file ) {
	return 0;
    }
    if( ! -f $file ) {
	return 0;
    }
    if ( display_line( $file ) ) {
	return 0;
    } else {
	return do_it( find_alternate($file) );
    }
}


# Display the synopsis line from the given man page, if possible:
# "open( MAN, "$cat < $file |" )" open the output of 
#    "$cat < $file" as a pipe as file handle MAN.
# "undef $/" just undefines the line delimiter, so that we can grab
#            the whole file all at once.
# "$_ = <MAN>" grabs all of MAN and puts it in $_
# If we match the regexp, print out the synopsis and return success. 
# "/.*SH NAME(.|\n)*?\\-(.*)/" = a regexp to find the synopsis: 
#   ".*SH NAME" = everything upto and including "SH NAME"
#   "(.|\n)*" = anything, including a newline
#   "?" = only match what you need, not everything you can
#   "\\-" = match exactly "\-", which is what begins the synopsis
#   "(.*)" = everything upto a newline, save it into $2 ($2, since we 
#           already used ()'s around something earlier in this regexp)  
sub display_line {
    my( $file ) = $_[0];

    open( MAN, "$cat < $file |" );
    undef $/;
    $_ = <MAN>;

	if( /.*SH NAME(.|\n)*?\\-(.*)/ ) {
	    print $2;
	    return 1;
	} else {
	    return 0;
	}
}


# Open the given man page, seach for ".so", and grab everything after
# it (usually something like "man2/wait.2") as the "alternate man
# page" we should display. 
sub find_alternate {
    my( $file ) = $_[0];

    open( MAN, "$cat < $file |" );
    undef $/;
    $_ = <MAN>;

    if( /.*\.so\s*(.*)/ ) { 
	return "$mandir/$1$manext";
    } else {
	return 0;
    }
}





