pman 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. CREDIT="man for puppy linux - 22 Dec 2009"
  18. if [ -x /usr/local/bin/man ];then
  19. CREDIT="$CREDIT
  20. If you like to use original version of man, try '/usr/local/bin/man'.
  21. "
  22. fi
  23. SECTION=""
  24. NAME="$1"
  25. [ $# -gt 1 ] && SECTION="$1" && NAME="$2"
  26. case "$1" in
  27. *-h*|'') echo "$CREDIT
  28. usage: $(basename $0) [section] name"
  29. exit;;
  30. esac
  31. 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|"
  32. # set locale
  33. for lng in C $(echo $LANGUAGE|cut -d':' -f1) $LC_ALL $LANG;do :;done # ex. ja_JP.UTF-8
  34. lng1=$(echo $lng|cut -d'.' -f1) # ex. ja_JP
  35. lng2=$(echo $lng|cut -d'_' -f1) # ex. ja
  36. LANGS="$lng,$lng1,$lng2,C"
  37. # handlers
  38. HTMLVIEWERS="$BROWSER defaulthtmlviewer defaultbrowser dillo seamonkey mozilla firefox opera"
  39. TEXTVIEWERS="defaulttextviewer leafpad nicoedit geany gedit"
  40. MANHANDLERS="man2html nroff"
  41. if [ "$DISPLAY" = "" ]; then
  42. HTMLVIEWERS="w3m lynx"
  43. TEXTVIEWERS="more less"
  44. MANHANDLERS="nroff"
  45. echo $lng2 | grep -qE 'ja|ko|th|zh' && export LANG=C && LANGS=C
  46. fi
  47. HTMLVIEWER=""
  48. for PROG in $HTMLVIEWERS;do
  49. which $PROG >/dev/null && HTMLVIEWER=$PROG && break
  50. done
  51. TEXTVIEWER=""
  52. for PROG in $TEXTVIEWERS;do
  53. which $PROG >/dev/null && TEXTVIEWER=$PROG && break
  54. done
  55. # look up man page
  56. MANHANDLER=""
  57. for PROG in $MANHANDLERS; do
  58. which $PROG >/dev/null && MANHANDLER=$PROG && break
  59. done
  60. if [ "$MANHANDLER" != "" ]; then
  61. [ "$MANPATH" = "" ] && MANPATH="/usr/man:/usr/share/man:/usr/local/man:/usr/local/share/man"
  62. MANDIRS=$(echo $MANPATH| tr ':' ',')
  63. MANLANGS="$LANGS"
  64. [ "$MANHANDLER" != "man2html" ] && echo $lng2 | grep -qE 'ja|ko|th|zh' && export MANLANGS=C
  65. [ "$SECTION" != "" ] && SEC="$SECTION" || SEC='*'
  66. #[ "$MANDIRS" != "" ] && \
  67. 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)"
  68. if [ "$FNDMAN" != "" ]; then
  69. case $MANHANDLER in
  70. nroff)
  71. MORE="less -FRis"
  72. which more >/dev/null && MORE="more"
  73. if echo "$FNDMAN" | grep -q '.gz$' ;then
  74. zcat "$FNDMAN" | nroff -mandoc | $MORE
  75. else
  76. nroff -mandoc "$FNDMAN" | $MORE
  77. fi
  78. exit
  79. ;;
  80. man2html)
  81. TMPFILE=/tmp/$(basename "$FNDMAN" .gz | tr '.' '_').html
  82. if echo "$FNDMAN" | grep -q '.gz$' ;then
  83. zcat "$FNDMAN" | man2html > $TMPFILE
  84. else
  85. man2html "$FNDMAN" > $TMPFILE
  86. fi
  87. #120317 L18L: enable correct Character Encoding: UTF-8
  88. sed 's/<\/HEAD><BODY>/<meta http-equiv="Content-Type" content="text\/html; charset=UTF-8" \/><\/HEAD><BODY>/' $TMPFILE > ${TMPFILE}1
  89. mv -f ${TMPFILE}1 $TMPFILE
  90. $HTMLVIEWER $TMPFILE
  91. rm -f $TMPFILE
  92. exit
  93. ;;
  94. esac
  95. fi
  96. fi
  97. # look up help file
  98. HELPDIR=/usr/share/doc/help
  99. BASE=$(basename $NAME |cut -d'.' -f1)
  100. FNDTXT="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.txt -not -type d 2>/dev/null| head -n 1)"
  101. [ "$FNDTXT" = "" ] && \
  102. FNDHTM="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.htm* -not -type d 2>/dev/null| head -n 1)"
  103. SYMLNKS="`find /usr/share/doc -maxdepth 1 -type l 2>/dev/null | tr "\n" " "`"
  104. [ "$FNDTXT" = "" -a "$FNDHTM" = "" ] && \
  105. FNDTXT="`find /usr/share/doc -maxdepth 3 -mount -xtype f -iname ${NAME}.txt 2>/dev/null`"
  106. #find does not follow symlinks in paths unless followed by at least a "/", need this crap...
  107. if [ "$FNDTXT" = "" -a "$FNDHTM" = "" ];then
  108. FNDSH="$(eval find -L $HELPDIR/{$LANGS} -maxdepth 1 -iname $BASE.sh -not -type d 2>/dev/null| head -n 1)"
  109. [ -f "$FNDSH" ] && exec "$FNDSH"
  110. for ONELNK in $SYMLNKS
  111. do
  112. [ ! "`echo -n "$ONELNK" | grep -i "${NAME}.txt$"`" = "" ] && FNDTXT="$ONELNK"
  113. [ ! "$FNDTXT" = "" ] && break
  114. FNDTXT="`find ${ONELNK}/ -mount -maxdepth 3 -xtype f -iname ${NAME}.txt 2>/dev/null`"
  115. [ ! "$FNDTXT" = "" ] && break
  116. done
  117. fi
  118. if [ "$FNDTXT" != "" ];then
  119. FNDTXT="$(echo "$FNDTXT" | head -n 1)"
  120. exec $TEXTVIEWER "$FNDTXT"
  121. else
  122. [ "$HTMLVIEWER" = "" ] && exit
  123. [ "$FNDHTM" = "" ] && \
  124. FNDHTM="`find /usr/share/doc -maxdepth 9 -mount -xtype f -iname ${NAME}.htm* 2>/dev/null`"
  125. #v2.12 improved find code contributed by Dougal...
  126. if [ "$FNDHTM" = "" ];then
  127. FNDDIR="`find /usr/share/doc -maxdepth 9 -mount -type d -iname ${NAME}* 2>/dev/null`"
  128. [ $? -eq 0 ] && [ -f $FNDDIR/index.html ] && FNDHTM=$FNDDIR/index.html
  129. fi
  130. #find does not follow symlinks in paths unless followed by at least a "/", need this crap...
  131. if [ "$FNDHTM" = "" ];then
  132. for ONELNK in $SYMLNKS
  133. do
  134. [ ! "`echo -n "$ONELNK" | grep -i "${NAME}.htm"`" = "" ] && FNDHTM="$ONELNK"
  135. [ ! "$FNDHTM" = "" ] && break
  136. FNDHTM="`find ${ONELNK}/ -mount -maxdepth 3 -xtype f -iname ${NAME}.htm* 2>/dev/null`"
  137. [ ! "$FNDHTM" = "" ] && break
  138. done
  139. fi
  140. if [ ! "$FNDHTM" = "" ];then
  141. FNDHTM="$(echo "$FNDHTM"| head -n 1)"
  142. [ "`echo -n "$FNDHTM"| cut -b 1`" = "/" ] && FNDHTM=file://$FNDHTM
  143. exec $HTMLVIEWER "$FNDHTM"
  144. fi
  145. fi
  146. #w464 search pkg homepages db...
  147. pPATTERN='^'"${NAME}"' '
  148. HOMESITE="`grep -i "$pPATTERN" /root/.packages/PKGS_HOMEPAGES | head -n 1 | cut -f 2 -d ' '`"
  149. if [ "$SECTION" = "" -a "$HOMESITE" != "" ];then
  150. exec $HTMLVIEWER $HOMESITE
  151. fi
  152. if [ "$lng2" = "ja" ]; then
  153. # Japanese Puppy Wiki
  154. url="http://puppylinux.jp/w/index.php?${NAME}"
  155. wget -t 1 -T 5 -O - "$url" 2>/dev/null | fgrep -q "<title>${NAME} -" && \
  156. exec $HTMLVIEWER "$url"
  157. # JP project
  158. SECS="Sec$SECTION=on"
  159. [ "$SECTION" = "" ] && SECS="Sec1=on&Sec2=on&Sec3=on&Sec4=on&Sec5=on&Sec6=on&Sec7=on&Sec8=on"
  160. url="http://search.linux.or.jp/cgi-bin/JM/man.cgi?Pagename=${NAME}&${SECS}"
  161. if REPLY=$(LANG=C wget -t 1 -T 5 --spider "$url" 2>&1);then
  162. echo "$REPLY" | fgrep -q 'nosuchpage.html' || \
  163. exec $HTMLVIEWER "$url"
  164. fi
  165. fi
  166. #exec defaulthtmlviewer http://en.wikipedia.org/wiki/${NAME}
  167. #exec defaulthtmlviewer http://unixhelp.ed.ac.uk/CGI/man-cgi?${NAME}
  168. #exec defaulthtmlviewer http://threads.seas.gwu.edu/cgi-bin/man2web?program=${NAME}
  169. #v431 fix thanks to technosaurus...
  170. [ "${SECTION}" != "" ] && exec $HTMLVIEWER "http://linux.die.net/man/${SECTION}/${NAME}"
  171. exec $HTMLVIEWER "http://www.google.com/search?&q=man+\"${NAME}\"+site:linux.die.net&btnI=Search"
  172. ###END###