slitaz-base-files view rootfs/usr/bin/man @ rev 296

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