PageRenderTime 79ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/linkedfs/usr/sbin/makewhatis

https://bitbucket.org/harakiri/trk
Shell | 410 lines | 328 code | 20 blank | 62 comment | 64 complexity | fad47b1bac54447cad4b05879ed49dd4 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. #!/bin/sh
  2. # makewhatis: create the whatis database
  3. # Created: Sun Jun 14 10:49:37 1992
  4. # Revised: Sat Jan 8 14:12:37 1994 by faith@cs.unc.edu
  5. # Revised: Sat Mar 23 17:56:18 1996 by micheal@actrix.gen.nz
  6. # Linux-Mandrake adaptations by Chmouel Boudjnah <chmouel@mandrakesoft.com>
  7. # Copyright 1992, 1993, 1994 Rickard E. Faith (faith@cs.unc.edu)
  8. # May be freely distributed and modified as long as copyright is retained.
  9. #
  10. # Wed Dec 23 13:27:50 1992: Rik Faith (faith@cs.unc.edu) applied changes
  11. # based on Mitchum DSouza (mitchum.dsouza@mrc-apu.cam.ac.uk) cat patches.
  12. # Also, cleaned up code and make it work with NET-2 doc pages.
  13. #
  14. # makewhatis-1.4: aeb 940802, 941007, 950417
  15. # Fixed so that the -c option works correctly for the cat pages
  16. # on my machine. Fix for -u by Nan Zou (nan@ksu.ksu.edu).
  17. # Many minor changes.
  18. # The -s option is undocumented, and may well disappear again.
  19. #
  20. # Sat Mar 23 1996: Michael Hamilton (michael@actrix.gen.nz).
  21. # I changed the script to invoke gawk only once for each directory tree.
  22. # This speeds things up considerably (from 30 minutes down to 1.5 minutes
  23. # on my 486DX66).
  24. # 960401 - aeb: slight adaptation to work correctly with cat pages.
  25. # 960510 - added fixes by brennan@raven.ca.boeing.com, author of mawk.
  26. # 971012 - replaced "test -z" - it doesnt work on SunOS 4.1.3_U1.
  27. # 980710 - be more careful with TMPFILE
  28. # 000323 - do not change PATH, better treatment of catpages - Bryan Henderson
  29. # 011117 - avoid suspicious filenames
  30. # 030310 - find files only; fix LAPACK cruft; no /usr/man default;
  31. # use /dev/stderr instead of /dev/tty; handle files with strange names;
  32. # add support for chinese, hungarian, indonesian, japanese, korean,
  33. # polish, russian (Thierry Vignaud);
  34. #
  35. # Note for Slackware users: "makewhatis -v -w -c" will work.
  36. #
  37. # makewhatis aeb 030801 (from man-1.5m)
  38. program=`basename $0`
  39. # In case both /usr/man and /usr/share/man exist, the former is local
  40. # and should be first.
  41. # It is a bug to add /var/cache/man to DEFCATPATH.
  42. dm=
  43. for d in /usr/share/man /usr/man
  44. do
  45. if [ -d $d ]; then
  46. if [ x$dm = x ]; then dm=$d; else dm=$dm:$d; fi
  47. fi
  48. done
  49. DEFMANPATH=$dm
  50. # This file is specific to Linux-Mandrake.
  51. [ -f /etc/sysconfig/i18n ] && . /etc/sysconfig/i18n
  52. [ -d /usr/man/$LANG/ ] && DEFMANPATH="$DEFMANPATH:/usr/man/$LANG/"
  53. dc=
  54. for d in /var/cache/man /usr/share/man/preformat /usr/man/preformat /usr/share/man /usr/man
  55. do
  56. if [ -d $d ]; then
  57. if [ x$dc = x ]; then dc=$d; else dc=$dc:$d; fi
  58. fi
  59. done
  60. DEFCATPATH=$dc
  61. # In case /usr is read-only, make /usr/foo/whatis (etc) a symlink to
  62. # something like /var/cache/man/foo-whatis.
  63. # Some distributions make a single big /var/cache/man/whatis file,
  64. # but that leads to problems and bugs.
  65. # AWK=/usr/bin/gawk
  66. AWK=/bin/awk
  67. # Find a place for our temporary files. If security is not a concern, use
  68. # TMPFILE=/tmp/whatis$$; TMPFILEDIR=none
  69. # Of course makewhatis should only have the required permissions
  70. # (for reading and writing directories like /usr/man).
  71. # We try here to be careful (and avoid preconstructed symlinks)
  72. # in case makewhatis is run as root, by creating a subdirectory of /tmp.
  73. TMPFILEDIR=`mktemp -d /tmp/makewhatisXXXXXX`
  74. if [ $? -ne 0 ]; then
  75. echo "$0: Can't create temp file, exiting..."
  76. exit 1
  77. fi
  78. chmod 0700 $TMPFILEDIR
  79. TMPFILE=$TMPFILEDIR/w
  80. # make sure TMPFILEDIR is deleted if program is killed or terminates
  81. # (just delete this line if your shell doesnt know about trap)
  82. trap "rm -rf $TMPFILEDIR" 0 1 2 3 15
  83. # default find arg: no directories, no empty files
  84. findarg0="-type f -size +0"
  85. topath=manpath
  86. defmanpath=$DEFMANPATH
  87. defcatpath=
  88. sections="1 2 3 4 5 6 7 8 9 n l"
  89. for name in "$@"
  90. do
  91. if [ -n "$setsections" ]; then
  92. setsections=
  93. sections=$name
  94. continue
  95. fi
  96. case $name in
  97. --version|-V)
  98. echo "$program from man-1.5m"
  99. exit 0;;
  100. -c) topath=catpath
  101. defmanpath=
  102. defcatpath=$DEFCATPATH
  103. continue;;
  104. -s) setsections=1
  105. continue;;
  106. -u) if [ -e /var/cache/man/whatis ]; then
  107. findarg="-newer /var/cache/man/whatis"
  108. update=1
  109. fi
  110. continue;;
  111. -v) verbose=1
  112. continue;;
  113. -w) manpath=`man --path`
  114. catpath=$manpath
  115. continue;;
  116. -*) echo "Usage: makewhatis [-u] [-v] [-w] [manpath] [-c [catpath]]"
  117. echo " This will build the whatis database for the man pages"
  118. echo " found in manpath and the cat pages found in catpath."
  119. echo " -u: update database with new pages"
  120. echo " -v: verbose"
  121. echo " -w: use manpath obtained from \`man --path\`"
  122. echo " [manpath]: man directories (default: $DEFMANPATH)"
  123. echo " [catpath]: cat directories (default: the first existing"
  124. echo " directory in $DEFCATPATH)"
  125. exit;;
  126. *) if [ -d $name ]
  127. then
  128. eval $topath="\$$topath":$name
  129. else
  130. echo "No such directory $name"
  131. exit
  132. fi;;
  133. esac
  134. done
  135. manpath=`echo ${manpath-$defmanpath} | tr : ' '`
  136. if [ x"$catpath" = x ]; then
  137. for d in `echo $defcatpath | tr : ' '`
  138. do
  139. if [ -d $d ]; then catpath=$d; break; fi
  140. done
  141. fi
  142. catpath=`echo ${catpath} | tr : ' '`
  143. #WHATIS_DIR=$DESTDIR/var/cache/man/`echo $here|sed -e 's!.*/man/!!g'`
  144. WHATIS_DIR=$DESTDIR/var/cache/man/$LANG
  145. [[ -d $WHATIS_DIR ]] || mkdir -p $WHATIS_DIR/
  146. # first truncate all the whatis files that will be created new,
  147. # then only update - we might visit the same directory twice
  148. if [ x$update = x ]; then
  149. mkdir -p $WHATIS_DIR/
  150. /bin/echo -n > $WHATIS_DIR/whatis
  151. fi
  152. for pages in man cat
  153. do
  154. export pages
  155. eval path="\$$pages"path
  156. for mandir in $path
  157. do
  158. if [ x$verbose != x ]; then
  159. echo "about to enter $mandir" > /dev/stderr
  160. fi
  161. here=`pwd`
  162. cd $mandir
  163. for i in $sections
  164. do
  165. if [ -d ${pages}$i ]
  166. then
  167. cd ${pages}$i
  168. section=$i
  169. curdir=$mandir/${pages}$i
  170. export section verbose curdir
  171. find $mandir/${pages}$i/. -name '*' $findarg0 $findarg -print | $AWK '
  172. function readline() {
  173. if (use_zcat || use_bzcat) {
  174. result = (pipe_cmd | getline);
  175. if (result < 0) {
  176. print "Pipe error: " pipe_cmd " " ERRNO > "/dev/stderr";
  177. }
  178. } else {
  179. result = (getline < filename);
  180. if (result < 0) {
  181. print "Read file error: " filename " " ERRNO > "/dev/stderr";
  182. }
  183. }
  184. return result;
  185. }
  186. function closeline() {
  187. if (use_zcat || use_bzcat) {
  188. return close(pipe_cmd);
  189. } else {
  190. return close(filename);
  191. }
  192. }
  193. function do_one() {
  194. insh = 0; thisjoin = 1; done = 0;
  195. entire_line = "";
  196. if (verbose) {
  197. print "adding " filename > "/dev/stderr"
  198. }
  199. use_zcat = match(filename,"\\.Z$") ||
  200. match(filename,"\\.z$") || match(filename,"\\.gz$");
  201. if (!use_zcat)
  202. use_bzcat = match(filename,"\\.bz2");
  203. if (use_zcat || use_bzcat) {
  204. filename_no_gz = substr(filename, 0, RSTART - 1);
  205. } else {
  206. filename_no_gz = filename;
  207. }
  208. match(filename_no_gz, "/[^/]+$");
  209. progname = substr(filename, RSTART + 1, RLENGTH - 1);
  210. if (match(progname, "\\." section "[A-Za-z]+")) {
  211. actual_section = substr(progname, RSTART + 1, RLENGTH - 1);
  212. } else {
  213. actual_section = section;
  214. }
  215. sub(/\..*/, "", progname);
  216. if (use_zcat || use_bzcat) {
  217. if (use_zcat) {
  218. pipe_cmd = "zcat \"" filename "\"";
  219. } else {
  220. pipe_cmd = "bzcat \"" filename "\"";
  221. }
  222. # try to avoid suspicious stuff
  223. if (filename ~ /[;&|`$(]/) {
  224. print "ignored strange file name " filename " in " curdir > "/dev/stderr";
  225. return;
  226. }
  227. }
  228. while (!done && readline() > 0) {
  229. gsub(/.\b/, "");
  230. if (($1 ~ /^\.[Ss][Hh]/ &&
  231. ($2 ~ /[Nn][Aa][Mm][Ee]/ ||
  232. $2 ~ /^JMÉNO/ || $2 ~ /^NAVN/ || $2 ~ /^NUME/ ||
  233. $2 ~ /^BEZEICHNUNG/ || $2 ~ /^NOMBRE/ ||
  234. $2 ~ /^NIMI/ || $2 ~ /^NOM/ || $2 ~ /^IME/ ||
  235. $2 ~ /^N[ÉE]V/ || $2 ~ /^NAMA/ || $2 ~ /^ĚžÁ°/ ||
  236. $2 ~ /^ĚžžÎ/ || $2 ~ /^Ŕ̸§/ || $2 ~ /^NAZWA/ ||
  237. $2 ~ /^îáú÷áîéĺ/ || $2 ~ /^ĂűłĆ/ || $2 ~ /^ŚWşŮ/ ||
  238. $2 ~ /^NOME/ || $2 ~ /^NAAM/)) ||
  239. (pages == "cat" && $1 ~ /^NAME/)) {
  240. if (!insh) {
  241. insh = 1;
  242. } else {
  243. done = 1;
  244. }
  245. } else if (insh) {
  246. if ($1 ~ /^\.[Ss][HhYS]/ ||
  247. (pages == "cat" &&
  248. ($1 ~ /^S[yYeE]/ || $1 ~ /^DESCRIPTION/ ||
  249. $1 ~ /^COMMAND/ || $1 ~ /^OVERVIEW/ ||
  250. $1 ~ /^STRUCTURES/ || $1 ~ /^INTRODUCTION/ ||
  251. $0 ~ /^[^ ]/))) {
  252. # end insh for Synopsis, Syntax, but also for
  253. # DESCRIPTION (e.g., XFree86.1x),
  254. # COMMAND (e.g., xspread.1)
  255. # OVERVIEW (e.g., TclCommandWriting.3)
  256. # STRUCTURES (e.g., XEvent.3x)
  257. # INTRODUCTION (e.g., TclX.n)
  258. # and anything at all that begins in Column 1, so
  259. # is probably a section header.
  260. done = 1;
  261. } else {
  262. if ($0 ~ progname"-") { # Fix old cat pages
  263. sub(progname"-", progname" - ");
  264. }
  265. if ($0 ~ /[^ \\]-$/) {
  266. sub(/-$/, ""); # Handle Hyphenations
  267. nextjoin = 1;
  268. } else if ($0 ~ /\\c$/) {
  269. sub(/\\c$/, ""); # Handle Continuations
  270. nextjoin = 1;
  271. } else
  272. nextjoin = 0;
  273. sub(/^.[IB] /, ""); # Kill bold and italics
  274. sub(/^.BI /, ""); #
  275. sub(/^.SM /, ""); # Kill small
  276. sub(/^.Nm /, ""); # Kill bold
  277. sub(/^.Tn /, ""); # Kill normal
  278. sub(/^.Li /, ""); # Kill .Li
  279. sub(/^.Dq /, ""); # Kill .Dq
  280. sub(/^.Nd */, "- "); # Convert .Nd to dash
  281. sub(/\\\".*/, ""); # Trim pending comments
  282. sub(/ *$/, ""); # Trim pending spaces
  283. sub(/^\.$/, ""); # Kill blank comments
  284. sub(/^'"'"'.*/, ""); # Kill comment/troff lines
  285. sub(/^.in .*/, ""); # Kill various macros
  286. sub(/^.ti .*/, "");
  287. sub(/^.ta .*/, "");
  288. sub(/^.Vb .*/, "");
  289. sub(/^.[PLTH]P$/, ""); # .PP/.LP/.TP/.HP
  290. sub(/^.Pp$/, "");
  291. sub(/^.IX .*$/, "");
  292. sub(/^.nolinks$/, "");
  293. sub(/^.B$/, "");
  294. sub(/^.nf$/, "");
  295. if (($1 ~ /^\.../ || $1 == "") &&
  296. (entire_line ~ / - / || entire_line ~ / \\- /)) {
  297. # Assume that this ends the description of one line
  298. # Sometimes there are several descriptions in one page,
  299. # as in outb(2).
  300. handle_entire_line();
  301. entire_line = "";
  302. thisjoin = 1;
  303. } else {
  304. if (thisjoin) {
  305. entire_line = entire_line $0;
  306. } else {
  307. entire_line = entire_line " " $0;
  308. }
  309. thisjoin = nextjoin;
  310. }
  311. }
  312. }
  313. }
  314. handle_entire_line();
  315. closeline();
  316. }
  317. function handle_entire_line() {
  318. x = entire_line; # Keep it short
  319. gsub(/\015/, "", x); # Kill DOS remains
  320. gsub(/ /, " ", x); # Translate tabs to spaces
  321. gsub(/ +/, " ", x); # Collapse spaces
  322. gsub(/ *, */, ", ", x); # Fix comma spacings
  323. sub(/^ /, "", x); # Kill initial spaces
  324. sub(/ $/, "", x); # Kill trailing spaces
  325. sub(/__+/, "_", x); # Collapse underscores
  326. gsub(/\\f\(../, "", x); # Kill font changes
  327. gsub(/\\f[PRIB0123]/, "", x); # Kill font changes
  328. gsub(/\\s[-+0-9]*/, "", x); # Kill size changes
  329. gsub(/\\&/, "", x); # Kill \&
  330. gsub(/\\\|/, "", x); # Kill \|
  331. gsub(/\\\((ru|ul)/, "_", x); # Translate
  332. gsub(/\\\((mi|hy|em)/, "-", x); # Translate
  333. gsub(/\\\*\(../, "", x); # Kill troff strings
  334. gsub(/\\/, "", x); # Kill all backslashes
  335. gsub(/"/, "", x); # Kill quotes (from .Nd "foo bar")
  336. sub(/<h1 align=center>/, "", x);# Yuk! HTML cruft
  337. gsub(/\000.*/, "X", x); # Binary cruft in LAPACK pages
  338. gsub(/ +/, " ", x); # Collapse spaces (again)
  339. sub(/^ /, "", x); # Kill initial spaces (again)
  340. sub(/ $/, "", x); # Kill trailing spaces (again)
  341. sub(/\.$/, "", x); # Kill trailing period
  342. if (!match(x, / - /))
  343. return;
  344. after_dash = substr(x, RSTART);
  345. head = substr(x, 1, RSTART-1) ", ";
  346. while (match(head, /, /)) {
  347. prog = substr(head, 1, RSTART-1);
  348. head = substr(head, RSTART+2);
  349. if (prog != progname)
  350. prog = prog " [" progname "]";
  351. printf "%-*s (%s) %s\n", 20, prog, actual_section, after_dash;
  352. }
  353. }
  354. { # Main action - process each filename read in.
  355. filename = $0;
  356. do_one();
  357. }
  358. ' pages=$pages section=$section verbose=$verbose curdir=$curdir
  359. cd ..
  360. fi
  361. done > $TMPFILE
  362. cd $here
  363. if [ -f $WHATIS_DIR/whatis ]
  364. then
  365. cat $WHATIS_DIR/whatis >> $TMPFILE
  366. fi
  367. sed '/^$/d' < $TMPFILE | sort | uniq > $WHATIS_DIR/whatis
  368. chmod 644 $WHATIS_DIR/whatis
  369. rm $TMPFILE
  370. done
  371. done
  372. # remove tempdir
  373. rm -rf $TMPFILEDIR