home *** CD-ROM | disk | FTP | other *** search
- #!/bin/sh
- #----------------------------------------------------------------------
- # NOM
- # man2html = Transforme un fichier de type Man au format HTML.
- #
- # SYNTAXE
- # man2html -h|H|M (aide)
- # man2html fichier_format_man > fichier_html
- #
- #----------------------------------------------------------------------
- # VERSION
- # 1.2
- #
- # DESCRIPTION
- # * Cette procedure permet de convertir un fichier au format
- # "man" (nroff), en un fichier au format HTML visualisable par
- # mosaic. Dans le cas ou des references de type "See ..." sont
- # inscrites dans le fichier man, la procedure genere une ancre
- # HTML, et permet l'accession au chapitre mentionne.
- # Il se peut que des references ne fonctionnent pas (ex. xv: See
- # "Appendix..."), car elles se referent a des documents papier !
- # Il n'y a pas encore de controle sur les ancres, mais cela se
- # fera certainement dans un proche avenir...
- #
- # * La procedure fonctionne tres bien avec des fichiers de taille
- # non importante. L'utilisation d'une telle procedure sur les
- # manuels Unix (ls, cp, etc.), n'est pas garantie.
- #
- # * La procedure test si le fichier passe en argument est compresse
- # ou non, et se charge d'une decompression le cas echeant.
- #
- #
- # OPTIONS
- # -h|H aide courte ou longue.
- #
- # -M affiche l'aide dans une fenetre Mosaic.
- #
- # EXEMPLE(S)
- # * Lancement de l'application avec affichage de l'aide longue:
- # $ man2html -H
- #
- # * Affichage de l'aide longue au format Mosaic:
- # $ man2html -W
- #
- # * Genere un fichier html du fichier "man" de la commande Unix ls:
- # $ man2html xv.1 > ls.html
- #
- #
- # DEPENDANCE(S)
- # awk, vi.
- #
- # REMARQUE(S)
- # * Il est vrai qu'il existe deja des procedures dans le domaine
- # public qui normalement s'occupe de transformer du man en html
- # j'en ai essaye quelques une en perl (elles ne fonctionnaient pas),
- # je n'ai malheureuse pas reussi a les faire fonctionner correctment,
- # nul n'est parfait, mais c'est promis des que je peux j'apprends le
- # perl !
- # Et je suis encore moins parfait, dans la mesure ou cette procedure
- # ne fonctionne pas sur tout les fichiers Man, car la procedure ne fait
- # pas d'interpretation a la mode nroff. Elle se contente juste de
- # remplacer les balises nroff par des balises html, du moins elle
- # essaye... Car il est fort possible que certaines balises ne soient
- # pas reconnues. Si cela arrive envoyez moi un courrier pour que je
- # puisse rectifier la procedure.
- #
- # * Procedure distribuee en Freeware/Gratuiciel.
- #
- #
- # AUTEUR
- # Bertrand LE QUELLEC
- #
- # E-MAIL
- # Copyright (c) 1998 Bertrand LE QUELLEC, All Rigths Reserved.
- #
- # CONTACT
- # eMail : blq@mygale.org ou blq@wanadoo.fr
- # Url : http://www.mygale.org/09/blq/ ou http://perso.wanadoo.fr/blq/
- #
- # Cree : 04/04/95 - Modifie le 17/03/98
- #----------------------------------------------------------------------
- # Permission to use, copy, modify, distribute, and sell this software
- # and its documentation for any purpose is hereby granted without fee,
- # provided that the above copyright notice appear in all copies and
- # that both that copyright notice and this permission notice appear in
- # supporting documentation, and that the name of Bertrand LE QUELLEC
- # not be used in advertising or publicity pertaining to distribution
- # of the software without specific, written prior permission. Bertrand
- # LE QUELLEC makes no representations about the suitability of
- # this software for any purpose. It is provided "as is" without
- # express or implied warranty.
- #
- # THIS SOFTWARE IS PROVIDED BY Bertrand LE QUELLEC `AS IS' AND ANY
- # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Bertrand LE QUELLEC BE
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- #----------------------------------------------------------------------
-
- # ------------------------------------------
- # ZONE DE DEFINITION DES VARIABLES GLOBALES:
- # ------------------------------------------
- NOM_PROCEDURE=`basename $0`
- LA_PROCEDURE=$0
- CD=`pwd`
- RM=0
- AWK=`[ -f /usr/bin/nawk ] && echo "nawk" || echo "awk" `
-
-
- ##
- ## Variables particulieres:
- ##
- DATE=`date "+%d/%m/%y"`
- MODE_COULEUR=0
-
-
- ##
- ## Les variables concernant l'utilisateur:
- ##
- DEFAUT_AUTEUR=`whoami`
- DEFAUT_EDITEUR=${EDITOR}
- DEFAUT_SHELL=${SHELL}
-
-
- #----------------------------------------------------------------------
- # ZONE DE DEFINITION DES FONCTIONS LOCALES:
- # -----------------------------------------
- # La fonction doit etre precedee d'un commentaire avec en debut de ligne
- # le caractere # (diese) repete deux fois.
-
- ##
- ## En cas d'une interruption la fonction suivante est executee.
- ## Les interruptions sont des signaux envoyes au processus du script,
- ## par la commande Unix: kill(1). La "capture" de ces signaux est
- ## identifiee par la commande Shell: trap definie apres la zone des
- ## options.
- ##
- TrapInterrup()
- {
- # Vous pouvez executer des instructions avant une sortie
- # par signal (ou brutale!).
- exit 1
- }
-
-
- ##
- ## Renvoie le PATH et nom d'un ou plusieurs executables.
- ## Si le ou les executables sont introuvables la fonction ne renvoie rien.
- ## Dans le cas d'un alias la fonction ne renvoie rien, car le PATH n'est
- ## pas recuperable.
- ##
- Type()
- {
- [ $# -ne 0 ] && type $* | $AWK '$2=="is" && $4!="alias" {print $NF}'
- }
-
-
- ##
- ## Affiche l'aide indiquee en en-tete du script shell.
- ## Ne pas supprimer car cette fonction est utilisee a l'appel de la
- ## procedure avec les options -H ou -H
- ##
- AideProc()
- {
- fichier=$1
- niveau=$2
-
- [ -z "$fichier" ] && {
- Erreur "Aide(): nombre d'argument incorect."
- return 0
- }
-
- [ ! -f "$fichier" ] && {
- Erreur "Aide(): impossible d'atteindre le fichier $fichier."
- return 0
- }
-
- [ ${niveau:=2} = 2 ] && {
- $AWK 'BEGIN {i=2}
- /^#(#+|-+|÷+|\*+|\\+|\++|=+|\.+|\/+)+ *$/{
- if(--i<=0)exit
- next
- }
- /^#[ ]*[A-Z]*[ ]*$/{
- next
- }
- /^#[^!]/{
- print substr($0,1,80)
- }
- ' $fichier | sed -e 's/^#[ ]*//'
- } || {
- $AWK '\
- /^#(#+|-+|÷+|\*+|\\+|\++|=+|\.+|\/+)+ *$/{
- if(--i<=0) exit
- next
- }
- /^#[^!]/{
- print substr($0,2,80)
- }
- ' i=$niveau $fichier | pg -e
- }
- }
-
- ##
- ## Renvoie le type de la machine ou l'on se trouve et surtout le
- ## systeme d'exploitation en cours. La fonction renvoie un des numero
- ## suivant en reponse:
- ##
- ## inconue 0
- ##
- ## SunOs 1
- ## Sun Solaris 2
- ## HP s700 3
- ## HP s800 4
- ## HP s300 5
- ## Silicon Graphic 6
- ## IBM AIX 7
- ##
- ## Pour recuperer le code retour de la fonction, utilisez la variable $?
- ##
- Machine()
- {
- os=`uname`
-
- [ "$os" = "SunOS" ] && return 1
-
- [ "$os" = "HP-UX" ] && {
- if uname -a | grep "PA-RISC" 1> /dev/null
- then
- return 3
- elif uname -a | grep "HP-PA" 1> /dev/null
- return 4
- then
- return 5
- fi
- }
-
- [ "$os" = "IRIX" ] && return 6
-
- [ "$os" = "AIX" ] && return 7
-
- return 0
- }
-
-
- ##
- ## Affiche un message en rouge si l'on se trouve dans une fenetre hpterm
- ## et se met en attente de 2 secondes.
- ## Il est possible de recuperer le code retour de la fonction.
- ##
- Erreur()
- {
- [ $# -eq 0 ] && return -1
-
- echo "\n\t${ROUGE}$*${NORMAL}"
- sleep 2
-
- return 0
- }
-
-
- #----------------------------------------------------------------------
- # DEFENITION DES FONCTIONS LOCALES AU PROGRAMME:
- # ----------------------------------------------
-
-
- #----------------------------------------------------------------------
- # RECUPERATION DES OPTIONS:
- # -------------------------
- set -- `getopt hHx $*`
- for opt in $*
- do
- case $opt in
- -h) AideProc $LA_PROCEDURE ; exit 0 ;;
- -H) AideProc $LA_PROCEDURE 3 ; exit 0 ;;
-
- -x) set -x ; shift ;;
-
- --) shift ; break ;;
- esac
- done
-
-
- #----------------------------------------------------------------------
- # AUTRES DEFINITIONS:
- # -------------------
- trap TrapInterrup 3 2 15 # Recuperation des interruptions 3, 2 et 15
-
- #----------------------------------------------------------------------
- # DEBUT ZONE DU PROGRAMME:
- # ------------------------
-
- [ ! -f "$1" ] && {
- Erreur "Il maque le fichier en entree."
- AideProc $LA_PROCEDURE
-
- exit 0
- }
-
- fichier=$1
- [ `file $fichier | grep -c compress` -gt 1 ] && {
- cp $fichier /tmp/$fichier.Z
- uncompress /tmp/$fichier.Z
- $fichier=/tmp/$fichier
- RM=1
- }
-
- name=`basename $fichier`
- cp $fichier /tmp/${NOM_PROCEDURE}.$$
-
- vi /tmp/${NOM_PROCEDURE}.$$ << ETIQ > /dev/null 2>&1
- :1,\$s%\\\\s[-+][0-9]\([^\]*\)\\\\s[-+][0-9]%\1%g
- :1,\$s%\\\\fB\([^\]*\)\\\\fP%<B>\1</B>%g
- :1,\$s%\\\\fB\([^\]*\)\\\\fR%<B>\1</B>%g
- :1,\$s%\\\\fB\([^\]*\)\\\\fI\([^\]*\)\\\\fR%<B>\1</B><I>\2</I>%g
- :1,\$s%\\\\fI\([^\]*\)\\\\fP%<I>\1</I>%g
- :1,\$s%\\\\fI\([^\]*\)\\\\fR%<I>\1</I>%g
- :1,\$s%\\\\fI\([^\]*\)\\\\fB\([^\]*\)\\\\fR%<I>\1</I><B>\2</B>%g
- :1,\$s%\.BI\(.*\) \(.*\)$%<B>\1</B> <I>\2</I>%g
- :1,\$s%\.B[IR] \(.*\)%<B>\1</B>%g
- :1,\$s%\.B\(.*\)%<B>\1</B>%g
- :1,\$s%\.IP \(.*\) [0-9]i%\.IP \1%g
- :1,\$s%\.I[^P] \(.*\)%<I>\1</I>%g
- :1,\$s%\.I\(.*\)\\\\fP%<I>\1</I>%g
- :1,\$s%\.I\(.*\)%<I>\1</I>%g
- :1,\$s%\\\\-\(.*\)%-\1%g
- :1,\$s%[sS]ee \"\(.*\)\"%See \"<A HREF\=\"$name.html#\1\">\1</A>\"%g
- :1,\$s%[sS]ee \(.*\) \"\(.*\)\"%See \1 \"<A HREF\=\"$name.html#\2\">\2</A>\"%g
- :1,\$s%\\\\0\\\\0\\\\(rh\\\\0\\\\0% => %g
- :1,\$s%\\\\&%%g
- :wq
- ETIQ
-
- echo "
- <HTML><HEAD>
- <TITLE>Manuel de la commande: `echo $name|cut -d. -f1`</TITLE>
- </HEAD><BODY>
- "
-
- $AWK '
- BEGIN {
- chapitre=0
- section=0
- sous_chapitre=0
-
- premier=1
- marque=0
- gras=0
- italique=0
- ip=0
- sh=0
- last=0
- tab=0
- see=0
- ul=0
- }
-
- /^\.ad/{
- next
- }
-
- /^\.ns/{
- next
- }
-
- /^\.de/{
- next
- }
-
- /^\.\\"/{
- next
- }
-
- /^\.UC/{
- next
- }
-
- /^\.sp[ \t]*$/{
- print "<BR><BR>"
- next
- }
-
- /^\.ev/{
- next
- }
-
- /^\.t[ia] /{
- next
- }
-
- /^\.nf/{
- if(ul) print "</UL>"
-
- ul=1
- print "<UL>"
- next
- }
-
- /^\.fi/{
- if(ul) print "</UL>"
- ul=0
- next
- }
-
- /^\.i[nf] /{
- next
- }
-
- /^\.d[es] /{
- next
- }
-
- /^\.n[hfs][ \t]*$/{
- next
- }
-
- /^\.na[ \t]*$/{
- print "<BR>"
- next
- }
-
- /^\.ft/{
- next
- }
-
- /^\.R[ES][ \t]*/{
- next
- }
-
- /^\.TH/{
- if(marque) marque=0
- if(tab) tab=0
- if(ip == 1) print "</UL>"
- if(ul) print "</UL>"
-
- sub(/\.TH/, "", $0)
-
- print "<BR>"
- printf("<H1> <A HREF=%s.html>", nom_appli)
- printf("%s", $1) | "dd conv=ucase 2> /dev/null | cat"
- close("dd conv=ucase 2> /dev/null | cat")
- printf("(%s)", $2)
- print "</A></H1>"
-
- ip=0
- sh=0
- see=0
- ul=0
-
- next
- }
-
- /^\.SH /{
- see=0
-
- if(ip == 1) print "</UL>"
- if(ul) print "</UL>"
- if(sous_chapitre) print "</UL>"
- if(section) print "</UL>"
- if(chapitre) print "</P></DD></DT>"
-
- see=index($0, "SEE ALSO")
-
- sub(/\.SH /, "", $0)
- gsub(/\t/, " ", $0)
-
- if(premier == 0) print "<HR SIZE=5>"
-
- printf("<H2><A NAME=\"%s\">%s</H2>\n", $0, $0)
- print "<DT><DD><P>"
-
- chapitre=1
- sous_chapitre=0
- section=0
- last=2
- marque=0
- tab=0
- ip=0
- premier=0
- sh=1
- ul=0
-
- next
- }
-
- /^\.SS /{
- if(ip == 1) print "</UL>"
- if(ul) print "</UL>"
- if(sous_chapitre) print "</UL>"
- if(section) print "</UL>"
-
- sub(/\.SS /, "", $0)
- gsub(/\t/, " ", $0)
- printf("<H3><A NAME=\"%s\"> <IMG SRC=ball.gif> %s</H3>\n", $0, $0)
- print "<UL>"
-
- section=1
- last=2
- ip=0
- sh=0
- sous_chapitre=0
- marque=0
- tab=0
- see=0
- ul=0
-
- next
- }
-
- /^\.TP [0-9]*[\.]?[0-9]*/{
- if(ip == 1) print "</UL>"
- if(ul) print "</UL>"
- if(last == 0) print "<BR>"
-
- marque=1
- tab=1
- ip=0
- sh=0
- see=0
- ul=0
-
- next
- }
-
- /^\.[HT]P[ \t]*$/ {
- if(ip == 1) print "</UL>"
- if(ul) print "</UL>"
- if(last == 0) print "<BR>"
-
- marque=1
- tab=1
- ip=0
- sh=0
- see=0
-
- next
- }
-
- /\\\(bu/{
- if(ip == 1) print "</UL>"
-
- ip=0
- sh=0
- last=0
- see=0
- ul=0
-
- next
- }
-
- /^\.P[PD][ \t]*[0-9]*/{
- ip=0
- sh=0
- see=0
-
- next
- }
-
- /^\.IP[ \t]*$/{
- if(ip == 1) print "</UL>"
- if(ul) print "</UL>"
- if(last == 0) print "<BR>"
-
- print "<BR><UL>"
-
- ip=1
- marque=0
- tab=0
- sh=0
- last=0
- see=0
- ul=0
-
- next
- }
-
- /^\.IP [ \t]*"[0-9]+\." [0-9]*[ \t]*$/{
- if(ip == 1) print "</UL>"
- if(ul) print "</UL>"
-
- gsub(/\.IP [ \t]*/, "", $0)
- gsub(/"/, "", $0)
- gsub(/ [0-9]*[ \t]*$/, "", $0)
-
- printf("<UL><LI>%s ", $0)
- getline
- print $0
-
- ip=1
- marque=0
- tab=0
- sh=0
- last=0
- see=0
- ul=0
-
- next
- }
-
- /^\.IP/{
- if(ip == 1) print "</UL>"
- if(ul) print "</UL>"
-
- gsub(/\.IP/, "", $0)
- gsub(/"/, "", $0)
-
- if(length($0) > 2) printf("<H3><IMG SRC=ball.gif> %s</H3>\n", $0)
-
- print "<UL><LI>"
-
- ip=1
- sh=0
- marque=0
- tab=0
- last=0
- see=0
- ul=0
-
- next
- }
-
- /^\.LP/{
-
- next
- }
-
- /^$/{
- if(last != 2)
- {
- print "<BR><BR>"
- last=1
- }
-
- next
- }
-
- /\\fB/ {
- if(gras) gsub(/\\fP/, "</B>", $0)
-
- gsub(/\\fB/, "<B>", $0)
- gras=1
- }
-
- /\\fI/ {
- if(italique) gsub(/\\fP/, "", $0)
-
- gsub(/\\fI/, "", $0)
- italique=1
- }
-
- /\\fP/ {
- if(gras)
- {
- gras=0
- gsub(/\\fP/, "</B>", $0)
- }
-
- if(italique)
- {
- italique=0
- gsub(/\\fP/, "", $0)
- }
-
- gsub(/\\fP/, "", $0)
- }
-
- /\.br/ {
- if(marque || last == 1)
- sub(/\.br/, "", $0)
- else
- sub(/\.br/, "<BR>", $0)
-
- print $0
-
- next
- }
-
- /[sS]ee / {
- pos1=index($0, ".html#")
- pos2=index($0, "\">")
- pos1=pos1+length(".html#")
- pos2=pos2+length("\">")
-
- if(pos2 > 0)
- {
- printf("%s", substr($0, 0, pos1-1))
- print substr($0, pos1, pos2-pos1) | "dd conv=ucase 2> /dev/null | cat"
- close("dd conv=ucase 2> /dev/null | cat")
- print substr($0, pos2, length($0)-pos2)
-
- next
- }
- }
- {
- if(marque)
- {
- marque=0
- if(last == 1)
- printf("<IMG SRC=ball.gif> %s\n", $0)
- else
- printf("<BR><IMG SRC=ball.gif> %s\n", $0)
- }
- else
- {
- if(ul)
- print $0, "<BR>"
- else
- {
- if (see)
- {
- for(i=1; i <= NF; i++)
- {
- if(index($i, "<"))
- {
- printf("%s ", $i)
- continue
- }
-
- lefichier=$i
- par=index($i, "(")
-
- if (par)
- lefichier=substr($i, 0, par-1)
-
- rep_fichier=lefichier
- if (system("ls " rep_fichier " 1> /dev/null 2> /dev/null && exit 1 || exit 0"))
- printf(" <A HREF=%s.html>%s</A>", lefichier, $i)
- else
- printf("%s ", $i)
- }
-
- print ""
- }
- else
- print $0
- }
- }
-
- sh=0
- last=0
- }
-
- END {
- if(ip == 2) print "</UL>"
- if(ul) print "</UL>"
- if(sous_chapitre) print "</UL>"
- if(section) print "</P></DD></DT>"
- if(chapitre) print "</P></DD></DT>"
-
- print "</BODY></HTML>"
- }' file=$name nom_appli=`echo $name | cut -d. -f1` /tmp/${NOM_PROCEDURE}.$$
-
- rm -f /tmp/${NOM_PROCEDURE}.$$
- [ $RM -eq 1 ] && rm -f $fichier
-