pman 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/bin/sh
  2. #Barry Kauler 2006. written for puppy, www.puppylinux.com
  3. #license gpl2 (ref: /usr/share/doc/legal)
  4. #a script to replace the man command.
  5. #w464 updated for PKGS_HOMEPAGES homepages db.
  6. #w482 changed die.net/man/1 url.
  7. # 24aug09 i18n by Shinobar
  8. # 27aug09 page1-8 for JM search
  9. # 28aug09 run *.sh if found
  10. # 2sep09 look up japanese puppy wiki
  11. # 5sep09 read man pages with devx
  12. # 12sep09 remove extentions such as .txt, .html, excute .sh
  13. #v431 fix looking up linux.die.net
  14. # v431JP suppress some locale if no X
  15. # dev431JPbeta2: man [section] name
  16. #120317 L18L: enable correct Character Encoding: UTF-8
  17. # jlst: groff support
  18. CREDIT="man for puppy linux"
  19. if [ -x /usr/local/bin/man ];then
  20. CREDIT="${CREDIT}\nIf you like to use original version of man, try '/usr/local/bin/man'.\n"
  21. fi
  22. SECTION=""
  23. NAME="$1"
  24. [ $# -gt 1 ] && SECTION="$1" && NAME="$2"
  25. case "$1" in
  26. *-h*|'') echo -e "${CREDIT}\nusage: $(basename $0) [section] name" ; exit;;
  27. esac
  28. BBAPPLETS="|addgroup|adduser|adjtimex|ar|arping|ash|awk|basename|bunzip2|busybox|bzcat|cal|cat|chgrp|chmod|chown|chroot|chvt|clear|cmp|cp|cpio|crond|crontab|cut|date|dc|dd|deallocvt|delgroup|deluser|devfsd|df|dirname|dmesg|dos2unix|dpkg|dpkg-deb|du|dumpkmap|dumpleases|echo|egrep|env|expr|false|fbset|fdflush|fdformat|fdisk|fgrep|find|fold|free|freeramdisk|fsck.minix|ftpget|ftpput|getopt|getty|grep|gunzip|gzip|halt|hdparm|head|hexdump|hostid|hostname|httpd|hush|hwclock|id|ifconfig|ifdown|ifup|inetd|init|insmod|install|ip|ipaddr|ipcalc|iplink|iproute|iptunnel|kill|killall|klogd|lash|last|length|linuxrc|ln|loadfont|loadkmap|logger|login|logname|logread|losetup|ls|lsmod|makedevs|md5sum|mesg|mkdir|mkfifo|mkfs.minix|mknod|mkswap|mktemp|modprobe|more|mount|msh|mt|mv|nameif|nc|netstat|nslookup|od|openvt|passwd|patch|pidof|ping|ping6|pipe_progress|pivot_root|poweroff|printf|ps|pwd|rdate|readlink|realpath|reboot|renice|reset|rm|rmdir|rmmod|route|rpm|rpm2cpio|run-parts|rx|sed|seq|setkeycodes|sha1sum|sleep|sort|start-stop-daemon|strings|stty|su|sulogin|swapoff|swapon|sync|sysctl|syslogd|tail|tar|tee|telnet|telnetd|test|tftp|time|top|touch|tr|traceroute|true|tty|udhcpc|udhcpd|umount|uname|uncompress|uniq|unix2dos|unzip|uptime|usleep|uudecode|uuencode|vconfig|vi|vlock|watch|watchdog|wc|wget|which|who|whoami|xargs|yes|zcat|"
  29. # set locale
  30. for lng in C $(echo $LANGUAGE|cut -d':' -f1) $LC_ALL $LANG;do :;done # ex. ja_JP.UTF-8
  31. lng1=$(echo $lng|cut -d'.' -f1) # ex. ja_JP
  32. lng2=$(echo $lng|cut -d'_' -f1) # ex. ja
  33. LANGS="$lng,$lng1,$lng2,C"
  34. # handlers
  35. HTMLVIEWERS="$BROWSER defaulthtmlviewer defaultbrowser dillo seamonkey mozilla firefox opera"
  36. TEXTVIEWERS="defaulttextviewer leafpad nicoedit geany gedit"
  37. MANHANDLERS="groff man2html nroff"
  38. if [ "$DISPLAY" = "" ]; then
  39. HTMLVIEWERS="w3m lynx"
  40. TEXTVIEWERS="more less"
  41. MANHANDLERS="groff nroff"
  42. echo $lng2 | grep -qE 'ja|ko|th|zh' && export LANG=C && LANGS=C
  43. fi
  44. HTMLVIEWER=""
  45. for PROG in $HTMLVIEWERS;do
  46. which $PROG >/dev/null && HTMLVIEWER=$PROG && break
  47. done
  48. TEXTVIEWER=""
  49. for PROG in $TEXTVIEWERS;do
  50. which $PROG >/dev/null && TEXTVIEWER=$PROG && break
  51. done
  52. # look up man page
  53. MANHANDLER=""
  54. for PROG in $MANHANDLERS; do
  55. which $PROG >/dev/null && MANHANDLER=$PROG && break
  56. done
  57. if [ "$DISPLAY" ]; then
  58. htmlvieweverprog=$HTMLVIEWER
  59. if [[ $HTMLVIEWER == default* ]] ; then
  60. #determine what program is run by defaulthtmlviewer or defaultbrowser
  61. htmlvieweverprog=$(sed -e '/#.*/d' -e 's/.*exec //' -e 's/^ //g' -e 's/^\t//' -e '/^$/d' -e 's/ .*//' $(which $HTMLVIEWER) | head -1 )
  62. fi
  63. echo $HTMLVIEWER
  64. echo $htmlvieweverprog
  65. case $htmlvieweverprog in
  66. palemoon|firefox|seamonkey|opera|google-chrome|qtweb)
  67. echo "DONTDELETE"
  68. DONTDELETE=1
  69. ;;
  70. "") echo "ERROR reading $HTMLVIEWER" ; exit 1 ;;
  71. esac
  72. fi
  73. if [ "$MANHANDLER" != "" ]; then
  74. [ "$MANPATH" = "" ] && MANPATH="/usr/man:/usr/share/man:/usr/local/man:/usr/local/share/man"
  75. MANDIRS=$(echo $MANPATH| tr ':' ',')
  76. MANLANGS="$LANGS"
  77. [ "$MANHANDLER" != "man2html" ] && echo $lng2 | grep -qE 'ja|ko|th|zh' && export MANLANGS=C
  78. [ "$SECTION" != "" ] && SEC="$SECTION" || SEC='*'
  79. #[ "$MANDIRS" != "" ] && \
  80. FNDMAN="$(eval find -L {$MANDIRS}/{{$MANLANGS}/man${SEC},man${SEC}} -maxdepth 1 -iname ${NAME}.[0-9]* -not -type d 2>/dev/null|head -n 1)"
  81. if [ "$FNDMAN" != "" ]; then
  82. case $MANHANDLER in
  83. nroff)
  84. MORE="less -FRis"
  85. which more >/dev/null && MORE="more"
  86. if echo "$FNDMAN" | grep -q '.gz$' ;then
  87. zcat "$FNDMAN" | nroff -mandoc | $MORE
  88. else
  89. nroff -mandoc "$FNDMAN" | $MORE
  90. fi
  91. exit
  92. ;;
  93. man2html)
  94. TMPFILE=/tmp/$(basename "$FNDMAN" .gz | tr '.' '_').html
  95. [ "$DONTDELETE" ] && TMPFILE=/tmp/pman_tmp.html
  96. if echo "$FNDMAN" | grep -q '.gz$' ;then
  97. zcat "$FNDMAN" | man2html > $TMPFILE
  98. else
  99. man2html "$FNDMAN" > $TMPFILE
  100. fi
  101. #120317 L18L: enable correct Character Encoding: UTF-8
  102. sed 's/<\/HEAD><BODY>/<meta http-equiv="Content-Type" content="text\/html; charset=UTF-8" \/><\/HEAD><BODY>/' $TMPFILE > ${TMPFILE}1
  103. mv -f ${TMPFILE}1 $TMPFILE
  104. $HTMLVIEWER $TMPFILE
  105. [ "$DONTDELETE" ] && exit
  106. rm -f $TMPFILE
  107. exit
  108. ;;
  109. groff)
  110. if [ "$DISPLAY" ] ; then
  111. TMPFILE=/tmp/$(basename "$FNDMAN" .gz | tr '.' '_').html
  112. [ "$DONTDELETE" ] && TMPFILE=/tmp/pman_tmp.html
  113. if echo "$FNDMAN" | grep -q '.gz$' ;then
  114. zcat "$FNDMAN" | groff -mandoc -Thtml > $TMPFILE
  115. else
  116. groff -mandoc -Thtml "$FNDMAN" > $TMPFILE
  117. fi
  118. #120317 L18L: enable correct Character Encoding: UTF-8
  119. sed 's/<\/HEAD><BODY>/<meta http-equiv="Content-Type" content="text\/html; charset=UTF-8" \/><\/HEAD><BODY>/' $TMPFILE > ${TMPFILE}1
  120. mv -f ${TMPFILE}1 $TMPFILE
  121. rm -f grohtml-*
  122. rm -f /tmp/grohtml-*
  123. $HTMLVIEWER $TMPFILE
  124. [ "$DONTDELETE" ] && exit
  125. rm -f $TMPFILE
  126. else
  127. MORE="less -FRis"
  128. which more >/dev/null && MORE="more"
  129. if echo "$FNDMAN" | grep -q '.gz$' ;then
  130. zcat "$FNDMAN" | groff -mandoc | $MORE
  131. else
  132. groff -mandoc "$FNDMAN" | $MORE
  133. fi
  134. fi
  135. exit
  136. ;;
  137. esac
  138. fi
  139. fi
  140. # look up help file
  141. HELPDIR=/usr/share/doc/help
  142. BASE=$(basename $NAME |cut -d'.' -f1)
  143. FNDTXT="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.txt -not -type d 2>/dev/null| head -n 1)"
  144. [ "$FNDTXT" = "" ] && \
  145. FNDHTM="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.htm* -not -type d 2>/dev/null| head -n 1)"
  146. SYMLNKS="`find /usr/share/doc -maxdepth 1 -type l 2>/dev/null | tr "\n" " "`"
  147. [ "$FNDTXT" = "" -a "$FNDHTM" = "" ] && \
  148. FNDTXT="`find /usr/share/doc -maxdepth 3 -mount -xtype f -iname ${NAME}.txt 2>/dev/null`"
  149. #find does not follow symlinks in paths unless followed by at least a "/", need this crap...
  150. if [ "$FNDTXT" = "" -a "$FNDHTM" = "" ];then
  151. FNDSH="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.sh -not -type d 2>/dev/null| head -n 1)"
  152. [ -f "$FNDSH" ] && exec "$FNDSH"
  153. for ONELNK in $SYMLNKS
  154. do
  155. [ ! "`echo -n "$ONELNK" | grep -i "${NAME}.txt$"`" = "" ] && FNDTXT="$ONELNK"
  156. [ ! "$FNDTXT" = "" ] && break
  157. FNDTXT="`find ${ONELNK}/ -mount -maxdepth 3 -xtype f -iname ${NAME}.txt 2>/dev/null`"
  158. [ ! "$FNDTXT" = "" ] && break
  159. done
  160. fi
  161. if [ "$FNDTXT" != "" ];then
  162. FNDTXT="$(echo "$FNDTXT" | head -n 1)"
  163. exec $TEXTVIEWER "$FNDTXT"
  164. else
  165. [ "$HTMLVIEWER" = "" ] && exit
  166. [ "$FNDHTM" = "" ] && \
  167. FNDHTM="`find /usr/share/doc -maxdepth 9 -mount -xtype f -iname ${NAME}.htm* 2>/dev/null`"
  168. #v2.12 improved find code contributed by Dougal...
  169. if [ "$FNDHTM" = "" ];then
  170. FNDDIR="`find /usr/share/doc -maxdepth 9 -mount -type d -iname ${NAME}* 2>/dev/null`"
  171. [ $? -eq 0 ] && [ -f $FNDDIR/index.html ] && FNDHTM=$FNDDIR/index.html
  172. fi
  173. #find does not follow symlinks in paths unless followed by at least a "/", need this crap...
  174. if [ "$FNDHTM" = "" ];then
  175. for ONELNK in $SYMLNKS
  176. do
  177. [ ! "`echo -n "$ONELNK" | grep -i "${NAME}.htm"`" = "" ] && FNDHTM="$ONELNK"
  178. [ ! "$FNDHTM" = "" ] && break
  179. FNDHTM="`find ${ONELNK}/ -mount -maxdepth 3 -xtype f -iname ${NAME}.htm* 2>/dev/null`"
  180. [ ! "$FNDHTM" = "" ] && break
  181. done
  182. fi
  183. if [ ! "$FNDHTM" = "" ];then
  184. FNDHTM="$(echo "$FNDHTM"| head -n 1)"
  185. [ "`echo -n "$FNDHTM"| cut -b 1`" = "/" ] && FNDHTM=file://$FNDHTM
  186. exec $HTMLVIEWER "$FNDHTM"
  187. fi
  188. fi
  189. #w464 search pkg homepages db...
  190. pPATTERN='^'"${NAME}"' '
  191. HOMESITE="`grep -i "$pPATTERN" /root/.packages/PKGS_HOMEPAGES | head -n 1 | cut -f 2 -d ' '`"
  192. if [ "$SECTION" = "" -a "$HOMESITE" != "" ];then
  193. exec $HTMLVIEWER $HOMESITE
  194. fi
  195. if [ "$lng2" = "ja" ]; then
  196. # Japanese Puppy Wiki
  197. url="http://puppylinux.jp/w/index.php?${NAME}"
  198. wget -t 1 -T 5 -O - "$url" 2>/dev/null | fgrep -q "<title>${NAME} -" && \
  199. exec $HTMLVIEWER "$url"
  200. # JP project
  201. SECS="Sec$SECTION=on"
  202. [ "$SECTION" = "" ] && SECS="Sec1=on&Sec2=on&Sec3=on&Sec4=on&Sec5=on&Sec6=on&Sec7=on&Sec8=on"
  203. url="http://search.linux.or.jp/cgi-bin/JM/man.cgi?Pagename=${NAME}&${SECS}"
  204. if REPLY=$(LANG=C wget -t 1 -T 5 --spider "$url" 2>&1);then
  205. echo "$REPLY" | fgrep -q 'nosuchpage.html' || \
  206. exec $HTMLVIEWER "$url"
  207. fi
  208. fi
  209. #exec defaulthtmlviewer http://en.wikipedia.org/wiki/${NAME}
  210. #exec defaulthtmlviewer http://unixhelp.ed.ac.uk/CGI/man-cgi?${NAME}
  211. #exec defaulthtmlviewer http://threads.seas.gwu.edu/cgi-bin/man2web?program=${NAME}
  212. #v431 fix thanks to technosaurus...
  213. [ "${SECTION}" != "" ] && exec $HTMLVIEWER "http://linux.die.net/man/${SECTION}/${NAME}"
  214. exec $HTMLVIEWER "http://www.duckduckgo.com/?q=man+\"${NAME}\"+site:linux.die.net"
  215. ###END###