Thursday, January 10, 2008

Manpage Creation Shell Script For Unix Or Linux

Hey there,

I've finally cranked out the first version of my little manpage generator for Linux and Unix. It's slightly bent toward Solaris, since I had to do all the shell scripting on a Sun box and only had access to their selection of manpage categories at hand (Near the end of the script, where it decides under what directory it will put your manpage).

You'll need to run this script as the root user (assuming you want to actually put your manpages in the man directories). You can run it as a regular user and have it just produce a manpage in your current working directory, also (This will happen by default if you don't have permission to write to your man directories, or they don't exist). On both Solaris Unix and RedHat Linux (probably all other distro's, too), you can then preview your manpage by updating your environment's MANPATH variable, like so:

MANPATH=$MANPATH:.;export MANPATH <--- Assuming sh, jsh, Posix shell, etc.

or

export MANPATH=$MANPATH:. <--- Assuming pretty much every other shell.

Then just type:

man "whateverYouCalledYourManpage"

and you can preview what it looks like using Linux or Unix's built-in parsers.

Here's hoping you find some use for this. Since I write a lot of scripts that are too long to put on this site (who knows; maybe someday ;), I'm going to enjoy creating a whole bunch of these and installing them on our servers so I can let everyone know they can RTFM. Hopefully, with this simple shell script, you'll be able to enjoy the same benefit :)

Best Wishes,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/ksh
#
# manmake - Simple Manpage File Generator
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

trap 'echo;echo "Cleaning up and bailing out!";rm -f addfile partemp0 partemp1 opt0 opt1 seealsofile;exit' 1 2 3 15

clear;
echo
echo "Welcome to the Simple Man Source File Maker"
echo "-------------------------------------------"
echo "Please don't expect nuthin' fancy here..."
echo
echo "Control-C will quit you cleanly at any point!"
echo "Tap or pound on the return key to continue."
read OBLIGSTOP
echo
echo "PLEASE USE EXTREME CAUTION WITH THIS UTILITY!!!"
echo "Sure, it won't be the end of the world if it happens,"
echo "but please make sure that the file you're creating"
echo "a new manpage for does not share the name of a utility"
echo "already in existence. This will confuse the operating"
echo "system severely. ...It's not as smart as it would"
echo "like you to think."
echo
echo "For more information, check out the manpage on this"
echo "program before diving in!"
echo
echo "Hit return to continue..."
echo
read OBLIGTWO
echo
echo "What's the name of your program?"
echo
read ONE
echo
echo "Give a brief, one-line type description"
echo
read TWO
echo
echo "Give the full invocation path and any available"
echo "options for the command line"
echo
read THREE
echo
echo "Give the abolute pathnames of any other files"
echo "associated with this program, assuming there are"
echo "any. Type one entry per line. When you're "
echo "finished, type \"endoffile\", less the quotes,"
echo "on a line by itself and hit return."
echo "If there aren't any, just type endoffile."
echo
while read ADDFILE
do
if [[ $ADDFILE == endoffile ]]
then
break
else
echo $ADDFILE >> addfile
fi
done
echo
echo "What Platforms is it available for?"
echo
read FOUR
echo
echo "Now we'll start on descriptive paragraphs!"
echo
echo "When finished with one paragraph, type \"sunshine\","
echo "minus quotes, on a line, by itself, and hit return. You"
echo "can then enter another and another, ad infinitum."
echo "Type \"endoffile\", minus the quotes, on a line by"
echo "by itself to indicate you are through typing your"
echo "entry. Please note that a section describing"
echo "your command line options and their specifics"
echo "will follow!Also, please note that the buffer limit"
echo "is currently only one line. So be sure before you"
echo "hit return!"
echo
echo "Will you be entering this info at the command line"
echo "or feeding me a file?"
echo
echo "Enter \"file\" for file or \"com\" for command line..."
echo
read CHOICE
if [[ $CHOICE == com ]]
then
echo
echo "Begin Typing!"
echo
while read FIVE
do
if [[ $FIVE == endoffile ]]
then
break
else
echo $FIVE >> partemp0
fi
done
elif [[ $CHOICE == file ]]
then
echo
echo "Please enter the filename. Also, please be"
echo "advised that the file format should be exactly"
echo "the same as described above, however, you "
echo "should not include \"endoffile\" as the last"
echo "line as this will only end up as part of your"
echo "description!"
echo
read FILENAME
if [ -f $FILENAME ]
then
cat $FILENAME >> partemp0
else
echo
echo "$FILENAME isn't here for me to feed on."
echo "Looks like we'll have to start again!"
echo
rm -f addfile partemp0 partemp1 opt0 opt1 seealsofile
exit 1
fi
else
echo
echo "What in God's name happened?"
echo
rm -f addfile partemp0 partemp1 opt0 opt1 seealsofile
exit1
fi
echo
echo "Now we'll need your options! Enter"
echo "these by typing the option on one line"
echo "in the following format:"
echo " option x"
echo "where \"x\" is the letter or short name"
echo "of the option."
echo "Follow this line with a paragraph describing"
echo "the options attributes. Again, you can end"
echo "this section by entering \"endoffile\", minus"
echo "the quotes, on a line by itself! And, also"
echo "don't forget the buffer size of one line!!"
echo
echo "Will you be entering this info at the command line"
echo "or feeding me a file?"
echo
echo "Enter \"file\" for file or \"com\" for command line..."
echo
read CHOICE
if [[ $CHOICE == com ]]
then
echo
echo "Begin Typing!"
echo
while read TEN
do
if [[ $TEN == endoffile ]]
then
break
else
echo $TEN >> opt0
fi
done
elif [[ $CHOICE == file ]]
then
echo
echo "Please enter the filename. Also, please be"
echo "advised that the file format should be exactly"
echo "the same as described above, however, you "
echo "should not include \"endoffile\" as the last"
echo "line as this will only end up as part of your"
echo "description!"
echo
read FILENAME
if [ -f $FILENAME ]
then
cat $FILENAME >> opt0
else
echo
echo "$FILENAME isn't here for me to feed on."
echo "Looks like we'll have to start again!"
echo
rm -f addfile partemp0 partemp1 opt0 opt1 seealsofile
exit 1
fi
else
echo
echo "What in God's name happened?"
echo
rm -f addfile partemp0 partemp1 opt0 opt1 seealsofile
exit1
fi
echo
echo "Heck, we're almost done, here!"
echo "Now, all I need to know are the"
echo "names of any other man pages you'd"
echo "like to reference on yours. You can"
echo "just enter \"endoffile\", no quotes"
echo "if there aren't any"
echo "pertinent ones yet available!"
echo "Enter them all on separate lines!"
echo
while read TWENTY
do
if [[ $TWENTY == endoffile ]]
then
break
else
echo $TWENTY >> seealsofile
fi
done
echo
echo "Okay, now You'll need to decide "
echo "what category your manpage or"
echo "program fits into. Just enter the"
echo "number of the option that best suits"
echo "you. The options listed follow the"
echo "the Sun specifications to a fair degree"
echo "and may not truthfully represent the"
echo "man directory structure order on your"
echo "current operating system. If you're not"
echo "a seething purist, it shouldn't matter"
echo "much."
echo
echo "1. User Commands and Application Programs."
echo "2. System Calls and Error Numbers."
echo "3. C Libraries and Functions."
echo "4. File Formats."
echo "5. Headers, Tables and Macros."
echo "6. Games and Demos."
echo "7. Device and Network Interfaces."
echo "8. Additional software (Could be anything)."
echo "9. Device Driver Interfaces."
echo
read TWENTYONE
echo
echo "All right, hold on while I slap together"
echo "your man page and load it for you!"
echo
DATE=`date "+%b %d %Y"`
print ".\" @(#)${ONE}.1 1.29 95/10/09 SMI; from Sun" >> manfile
print ".TH ${ONE}.${TWENTYONE} \"${DATE}\"" >> manfile
print ".SH NAME" >> manfile
print ".LP" >> manfile
print "$ONE \ - $TWO" >> manfile
print ".SH SYNOPSIS" >> manfile
print ".LP" >> manfile
print ".B $THREE" >> manfile
print ".SH AVAILABILITY" >> manfile
print ".LP" >> manfile
print "$FOUR" >> manfile
print ".SH DESCRIPTION" >> manfile
print ".LP" >> manfile
print ".IX \"${ONE}\" \"\" \"\fL${ONE}\fP \ (em ${TWO}\"" >> manfile
print ".IX \"${TWO}\" \"\" \"${TWO}\"" >> manfile
print ".LP" >> manfile
if [ -f partemp0 ]
then
sed 's/^ *sunshine/.LP/' partemp0 >> partemp1
cat partemp1 2>/dev/null |while read LINE
do
print $LINE >> manfile
done
fi
print ".LP" >> manfile
print ".SH OPTIONS" >> manfile
print ".LP" >> manfile
if [ -f opt0 ]
then
sed 's/^ *option *\(.*\)/.LP\
.B "\1"\
.LP/' opt0 >> opt1
cat opt1 |while read LINE
do
print $LINE >> manfile
done
fi
print ".LP" >> manfile
print ".SH FILES" >> manfile
print ".LP" >> manfile
print "${THREE}" >> manfile
if [ -f addfile ]
then
cat addfile >> manfile
fi
print ".LP" >> manfile
print ".SH \"SEE ALSO\"" >> manfile
print ".LP" >> manfile
cat seealsofile |while read LINE
do
print ".BR $LINE" >> manfile
done
print "" >> manfile
mv manfile ${ONE}.${TWENTYONE}
if [ -d /usr/man/man${TWENTYONE} ]
then
cp ${ONE}.${TWENTYONE} /usr/man/man${TWENTYONE}/
chown bin /usr/man/man${TWENTYONE}/${ONE}.${TWENTYONE}
chgrp bin /usr/man/man${TWENTYONE}/${ONE}.${TWENTYONE}
chmod 444 /usr/man/man${TWENTYONE}/${ONE}.${TWENTYONE}
echo "Copied to /usr/man/man${TWENTYONE} - Left a copy"
echo "here as ${ONE}.${TWENTYONE} for your convenience"
elif [ -d /usr/share/man/man${TWENTYONE} ]
then
cp ${ONE}.${TWENTYONE} /usr/share/man/man${TWENTYONE}/
chown bin /usr/share/man/man${TWENTYONE}/${ONE}.${TWENTYONE}
chgrp bin /usr/share/man/man${TWENTYONE}/${ONE}.${TWENTYONE}
chmod 444 /usr/share/man/man${TWENTYONE}/${ONE}.${TWENTYONE}
echo "Copied to /usr/share/man/man${TWENTYONE} - Left a copy"
echo "here as ${ONE}.${TWENTYONE} for your convenience"
else
echo "Couldn't place the manfile in any normal man"
echo "directories!! The culprit is most likely the"
echo "number you chose. I've saved your completed"
echo "formatted file as ${ONE}.${TWENTYONE} in the"
echo "current directory. All you'll need to do is"
echo "create, or have someone else create, the "
echo "directory \"/usr/man/man${TWENTYONE}\" and"
echo "put this file in there!"
echo
echo "Example:"
echo "mkdir /usr/man/man${TWENTYONE}"
echo "mv ${ONE}${TWENTYONE} /usr/man/man.${TWENTYONE}/"
echo
echo "You should be all set!"
echo
rm -f addfile opt0 opt1 partemp0 partemp1 seealsofile
exit
fi
echo
echo "At the prompt, type \"man\" followed by the"
echo "name you entered for your program and hit return!"
echo
rm -f addfile opt0 opt1 partemp0 partemp1 seealsofile


, Mike