slitaz-base-files annotate rootfs/usr/bin/man @ rev 340

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 26 08:22:10 2019 +0100 (2019-02-26)
parents ecb326507194
children
rev   line source
pankso@67 1 #!/bin/sh
pankso@67 2 #
pankso@67 3 # Tiny man fake using online manuals.
al@272 4 # Copyright (C) 2009-2015 SliTaz GNU/Linux.
pankso@67 5 #
pankso@139 6 . /lib/libtaz.sh
pankso@67 7
al@179 8 # Internationalization.
al@179 9 TEXTDOMAIN='slitaz-base'
al@179 10 . /etc/locale.conf
al@179 11 export TEXTDOMAIN LANG
al@179 12
llevrel@290 13 display_html() {
llevrel@290 14 if [ -x /usr/bin/retawq ]; then
llevrel@290 15 retawq --dump=file://"$1" | less -M
llevrel@290 16 else
llevrel@290 17 # Rationale: busybox "less" is unable to use control chars, so we're
llevrel@290 18 # stuck with regular text (no bold, colors...), and use other ways to
llevrel@290 19 # emphasis: quotes, underscores, brackets, tabs.
llevrel@290 20 # Explanation for following sequence:
llevrel@290 21 # 1) keep only what's in the HTML body
llevrel@290 22 # 2) make sure <pre> and </pre> are on a line of their own (see 3)
llevrel@290 23 # 3a) newlines must be obeyed between <pre> tags, so explicitly add <br>
llevrel@290 24 # because newlines are going to be stripped; also remove <em> (added
llevrel@290 25 # emphasis characters would be misleading)
llevrel@290 26 # 3b) the </pre> line is matched also, but should not get a <br> added,
llevrel@290 27 # so remove it (let it on the <pre> line, starts a new paragraph)
llevrel@290 28 # 4) strip newlines, remove comments (should be done earlier)
llevrel@290 29 # 5) emphasize headers and titles with a Tab, add newline after
llevrel@290 30 # 6) add newline for <br> <p> </p> <section> <article[ id=...]>
llevrel@290 31 # 7) suppress <a [href=...]> </a> and some other tags
llevrel@290 32 # 8) add newline and hyphen for list items, newline for list end
llevrel@290 33 # 9) emphasize <tt> and <code> blocks by ‘’ ; <em> by _
llevrel@290 34 # 10) render x-details between brackets []
llevrel@290 35 # 11) remove start-of-line and end-of-line spaces
llevrel@290 36 # 12) render HTML-encoded chars
llevrel@290 37 # 13) wrap at word boundaries
llevrel@290 38 cat "$1" | sed '1,/<body>/d; /<\/body>/d' \
llevrel@290 39 | sed -r 's!(.)<(/?)pre>!\1\n<\2pre>!g; s!<(/?)pre>(.)!<\1pre>\n\2!g;' \
llevrel@290 40 | sed -r '/<pre>/,/<\/pre>/{s!$!<br>!g; s!</?em>!!g}; \
llevrel@290 41 /<\/pre>/s!<br>$!!' | tr '\n' ' ' \
llevrel@290 42 | sed -r 's%<!-- .* -->%%g; \
llevrel@290 43 s!<(header|h[1-4])>! !g; s!<footer>!\n !; \
llevrel@290 44 s!</(header|h[1-4]|footer)>!\n!g; \
llevrel@290 45 s!<(br|/?p|section|article[^>]*)>!\n!g; \
llevrel@290 46 s!<(a [^>]*|/a|/section|/article|/html|/?pre|/?sup)>!!g; \
llevrel@290 47 s!<li>!\n — !g; s!<ul>!!g; s!</(li|ul)>!\n!g; \
llevrel@290 48 s!<(tt|code)>!‘!g; s!</(tt|code)>!’!g; s!</?em>!_!g; \
llevrel@290 49 s!<x-details>![!g; s!</x-details>!]!g; \
llevrel@290 50 s!&lt;!<!g; s!&gt;!>!g; s!&copy;!©!g; s!&quot;!"!g; s!&amp;!\&!g; \
llevrel@290 51 s!&reg;!™!g; ' | sed -r 's!(^ *| *$)!!' \
llevrel@290 52 | fold -s | less -M
pankso@245 53 fi
pankso@245 54 }
pankso@67 55
pankso@67 56 case "$1" in
pankso@67 57 ''|-*)
al@219 58 emsg "$(_ '<b>Usage:</b> man [section] command')"
pankso@67 59 return ;;
pankso@67 60 esac
pankso@67 61
al@272 62 SECTION='all'
llevrel@290 63 MAN_SECTION='[1-8]'
llevrel@290 64 MAN_LANG=$(locale | sed -nr 's/LC_MESSAGES="?([^"_.]*).*"?/\1/p')
al@272 65 MSG=''
pankso@67 66
pankso@67 67 if [ -n "$2" ]; then
al@272 68 SECTION="$1"
al@272 69 MAN_SECTION="$1"
al@272 70 MSG=" $(_n 'in section %s' "$SECTION")"
pankso@67 71 shift
pankso@67 72 fi
pankso@67 73
al@272 74 TOPIC="$1"
pankso@67 75
llevrel@290 76 # localized SliTaz doc?
llevrel@290 77 DOC=$(find /usr/share/doc/"$TOPIC" /usr/share/doc/slitaz* \
llevrel@290 78 -name "$TOPIC".$MAN_LANG.html 2>/dev/null | head -1)
llevrel@290 79 # generic SliTaz doc?
llevrel@290 80 [ -n "$DOC" ] || DOC=$(find /usr/share/doc/"$TOPIC" /usr/share/doc/slitaz* \
llevrel@290 81 -name "$TOPIC".html 2>/dev/null | head -1)
llevrel@290 82 # other doc?
llevrel@290 83 [ -n "$DOC" ] || DOC=$(find /usr/share/doc \
llevrel@290 84 -name "$TOPIC".html 2>/dev/null | head -1)
llevrel@290 85
llevrel@290 86 if [ -n "$DOC" ]; then
llevrel@290 87 display_html "$DOC"
pankso@67 88 return
llevrel@290 89 elif [ -f "/usr/share/doc/slitaz/$TOPIC.txt" ]; then
llevrel@290 90 # SliTaz tools/libraries documentation (man-alike format)
llevrel@290 91 less -M "/usr/share/doc/slitaz/$TOPIC.txt"
pankso@139 92 return
pankso@67 93 fi
pankso@67 94
llevrel@290 95 MANPAGE=$(find /usr/share/man/$MAN_LANG*/man$MAN_SECTION \
llevrel@290 96 /usr/local/share/man/$MAN_LANG*/man$MAN_SECTION \
llevrel@290 97 /usr/local/man/$MAN_LANG*/man$MAN_SECTION \
llevrel@290 98 /usr/share/man/man$MAN_SECTION \
llevrel@290 99 /usr/local/share/man/man$MAN_SECTION \
llevrel@290 100 /usr/local/man/man$MAN_SECTION \
llevrel@290 101 -name "$TOPIC".$MAN_SECTION\* 2>/dev/null)
llevrel@290 102 if [ -n "$MANPAGE" ]; then
llevrel@290 103 case "$MANPAGE" in
llevrel@290 104 *html) display_html "$MANPAGE"
llevrel@290 105 return;;
llevrel@290 106 esac
llevrel@290 107 # "less"-ing a manpage is a BAD IDEA: man format is unreadable as is.
llevrel@290 108 # Use nroff if available; it outputs control chars, which busybox less
llevrel@290 109 # cannot handle: use "more" instead (or GNU less)
llevrel@290 110 if [ -x /usr/bin/nroff ]; then
pascal@340 111 if [ x$(readlink $(which less)) = x/bin/busybox ]; then
llevrel@290 112 VIEW_CMD="more"
llevrel@290 113 else
llevrel@290 114 VIEW_CMD="less -rM"
al@194 115 fi
llevrel@290 116 case "$MANPAGE" in
llevrel@290 117 *gz) (zcat "$MANPAGE" || unlzma -c "$MANPAGE" 2>/dev/null) | \
llevrel@290 118 nroff -man | $VIEW_CMD;;
llevrel@290 119 *) nroff -man "$MANPAGE" | $VIEW_CMD;;
llevrel@290 120 esac
pankso@67 121 return
llevrel@290 122 else
llevrel@290 123 _ 'Found local manpage %s but no tool to display it.' $MANPAGE
llevrel@290 124 _ 'Consider installing groff by running: %s' \
llevrel@290 125 "su -c 'tazpkg get-install groff'"
llevrel@290 126 # do not quit, go on searching online
pankso@67 127 fi
mojo@266 128 fi
pankso@67 129
pascal@340 130 if [ "$SECTION" = 'all' ]; then
llevrel@290 131 #search alphabetically
llevrel@290 132 LETTER=$(printf "${TOPIC::1}" | tr 'A-Z[:punct:][:digit:]' a-z00)
pascal@340 133 if [ $LETTER = '0' ]; then LETTER=other; fi
llevrel@290 134 SECTIONS=$(wget -q -O - http://linux.die.net/man/$LETTER.html | \
llevrel@290 135 sed -n -r "s%.*href=\"(.)/$TOPIC\".*%\1%p")
llevrel@290 136 [ -n "$SECTIONS" ] || { _ 'No manual entry for %s' "$TOPIC"; exit 0;}
llevrel@296 137 if [ $(printf '%s\n' "$SECTIONS"|wc -l) -eq 1 ]; then
llevrel@296 138 SECTION=$SECTIONS
llevrel@296 139 else
llevrel@296 140 _n '%s found in the following sections:\n%s\nPlease choose one: ' \
llevrel@296 141 "$TOPIC" "$SECTIONS"
llevrel@296 142 read SECTION
llevrel@296 143 fi
llevrel@290 144 fi
llevrel@290 145
llevrel@290 146 MANURL="http://linux.die.net/man/$SECTION/$TOPIC"
llevrel@290 147
llevrel@290 148 if ! wget -q --spider "$MANURL" 2>/dev/null ; then
llevrel@290 149 _ 'No manual entry for %s' "$TOPIC$MSG"
llevrel@290 150 exit 0
llevrel@290 151 fi
llevrel@290 152
llevrel@290 153 if [ -x /usr/bin/retawq ]; then
llevrel@290 154 retawq "$MANURL"
llevrel@290 155 else
llevrel@290 156 URL="http://www.w3.org/services/html2txt?url=$MANURL&noinlinerefs=on&nonums=on"
llevrel@290 157 wget -q -O - "$URL" | tail -n+3 | \
llevrel@290 158 sed 's!\[[0-9]*\]!!g;/\[INS: :INS\]/d;/^ Site Search$/,$d' | less -M
llevrel@290 159 fi
llevrel@290 160
pankso@67 161 exit 0