PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tuxcmd-modules-0.6.70/libarchive/libarchive-2.5.5/doc/mdoc2man.awk

#
AWK | 328 lines | 275 code | 15 blank | 38 comment | 0 complexity | 49c0867a8cea35258eb49261f4c820f1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. #!/usr/bin/awk
  2. #
  3. # Copyright (c) 2003 Peter Stuge <stuge-mdoc2man@cdy.org>
  4. #
  5. # Permission to use, copy, modify, and distribute this software for any
  6. # purpose with or without fee is hereby granted, provided that the above
  7. # copyright notice and this permission notice appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. # Dramatically overhauled by Tim Kientzle. This version almost
  17. # handles library-style pages with Fn, Ft, etc commands. Still
  18. # a lot of problems...
  19. BEGIN {
  20. displaylines = 0
  21. trailer = ""
  22. out = ""
  23. sep = ""
  24. nextsep = " "
  25. }
  26. # Add a word with appropriate preceding whitespace
  27. # Maintain a short queue of the expected upcoming word separators.
  28. function add(str) {
  29. out=out sep str
  30. sep = nextsep
  31. nextsep = " "
  32. }
  33. # Add a word with no following whitespace
  34. # Use for opening punctuation such as '('
  35. function addopen(str) {
  36. add(str)
  37. sep = ""
  38. }
  39. # Add a word with no preceding whitespace
  40. # Use for closing punctuation such as ')' or '.'
  41. function addclose(str) {
  42. sep = ""
  43. add(str)
  44. }
  45. # Add a word with no space before or after
  46. # Use for separating punctuation such as '='
  47. function addpunct(str) {
  48. sep = ""
  49. add(str)
  50. sep = ""
  51. }
  52. # Emit the current line so far
  53. function endline() {
  54. addclose(trailer)
  55. trailer = ""
  56. if(length(out) > 0) {
  57. print out
  58. out=""
  59. }
  60. if(displaylines > 0) {
  61. displaylines = displaylines - 1
  62. if (displaylines == 0)
  63. dispend()
  64. }
  65. # First word on next line has no preceding whitespace
  66. sep = ""
  67. }
  68. function linecmd(cmd) {
  69. endline()
  70. add(cmd)
  71. endline()
  72. }
  73. function breakline() {
  74. linecmd(".br")
  75. }
  76. # Start an indented display
  77. function dispstart() {
  78. linecmd(".RS")
  79. }
  80. # End an indented display
  81. function dispend() {
  82. linecmd(".RE")
  83. }
  84. # Collect rest of input line
  85. function wtail() {
  86. retval=""
  87. while(w<nwords) {
  88. if(length(retval))
  89. retval=retval " "
  90. retval=retval words[++w]
  91. }
  92. return retval
  93. }
  94. ! /^\./ {
  95. out = $0
  96. endline()
  97. next
  98. }
  99. /^\.\\"/ { next }
  100. {
  101. sub("^\\.","")
  102. nwords=split($0,words)
  103. # TODO: Instead of iterating 'w' over the array, have a separate
  104. # function that returns 'next word' and use that. This will allow
  105. # proper handling of double-quoted arguments as well.
  106. for(w=1;w<=nwords;w++) {
  107. if(match(words[w],"^Li$")) { # Literal; rest of line is unformatted
  108. dispstart()
  109. displaylines = 1
  110. } else if(match(words[w],"^Dl$")) { # Display literal
  111. dispstart()
  112. displaylines = 1
  113. } else if(match(words[w],"^Bd$")) { # Begin display
  114. if(match(words[w+1],"-literal")) {
  115. dispstart()
  116. displaylines=10000
  117. w=nwords
  118. }
  119. } else if(match(words[w],"^Ed$")) { # End display
  120. displaylines = 0
  121. dispend()
  122. } else if(match(words[w],"^Ns$")) { # Suppress space after next word
  123. nextsep = ""
  124. } else if(match(words[w],"^No$")) { # Normal text
  125. add(words[++w])
  126. } else if(match(words[w],"^Dq$")) { # Quote
  127. addopen("``")
  128. add(words[++w])
  129. while(w<nwords&&!match(words[w+1],"^[\\.,]"))
  130. add(words[++w])
  131. addclose("''")
  132. } else if(match(words[w],"^Oo$")) {
  133. addopen("[")
  134. } else if(match(words[w],"^Oc$")) {
  135. addclose("]")
  136. } else if(match(words[w],"^Ao$")) {
  137. addopen("<")
  138. } else if(match(words[w],"^Ac$")) {
  139. addclose(">")
  140. } else if(match(words[w],"^Dd$")) {
  141. date=wtail()
  142. next
  143. } else if(match(words[w],"^Dt$")) {
  144. id=wtail()
  145. next
  146. } else if(match(words[w],"^Ox$")) {
  147. add("OpenBSD")
  148. } else if(match(words[w],"^Fx$")) {
  149. add("FreeBSD")
  150. } else if(match(words[w],"^Nx$")) {
  151. add("NetBSD")
  152. } else if(match(words[w],"^St$")) {
  153. if (match(words[w+1], "^-p1003.1$")) {
  154. w++
  155. add("IEEE Std 1003.1 (``POSIX.1'')")
  156. } else if(match(words[w+1], "^-p1003.1-96$")) {
  157. w++
  158. add("ISO/IEC 9945-1:1996 (``POSIX.1'')")
  159. } else if(match(words[w+1], "^-p1003.1-88$")) {
  160. w++
  161. add("IEEE Std 1003.1-1988 (``POSIX.1'')")
  162. } else if(match(words[w+1], "^-p1003.1-2001$")) {
  163. w++
  164. add("IEEE Std 1003.1-2001 (``POSIX.1'')")
  165. } else if(match(words[w+1], "^-susv2$")) {
  166. w++
  167. add("Version 2 of the Single UNIX Specification (``SUSv2'')")
  168. }
  169. } else if(match(words[w],"^Ex$")) {
  170. if (match(words[w+1], "^-std$")) {
  171. w++
  172. add("The \\fB" name "\\fP utility exits 0 on success, and >0 if an error occurs.")
  173. }
  174. } else if(match(words[w],"^Os$")) {
  175. add(".TH " id " \"" date "\" \"" wtail() "\"")
  176. } else if(match(words[w],"^Sh$")) {
  177. add(".SH")
  178. section=words[w+1]
  179. } else if(match(words[w],"^Xr$")) {
  180. add("\\fB" words[++w] "\\fP(" words[++w] ")" words[++w])
  181. } else if(match(words[w],"^Nm$")) {
  182. if(match(section,"SYNOPSIS"))
  183. breakline()
  184. if(w>1)
  185. n=name
  186. else {
  187. n=words[++w]
  188. if(!length(name))
  189. name=n
  190. }
  191. if(!length(n))
  192. n=name
  193. add("\\fB" n "\\fP")
  194. } else if(match(words[w],"^Nd$")) {
  195. add("\\- " wtail())
  196. } else if(match(words[w],"^Fl$")) {
  197. add("\\fB\\-" words[++w] "\\fP")
  198. } else if(match(words[w],"^Ar$")) {
  199. addopen("\\fI")
  200. if(w==nwords)
  201. add("file ...\\fP")
  202. else {
  203. add(words[++w] "\\fP")
  204. while(match(words[w+1],"^\\|$"))
  205. add(words[++w] " \\fI" words[++w] "\\fP")
  206. }
  207. } else if(match(words[w],"^Cm$")) {
  208. add("\\fB" words[++w] "\\fP")
  209. } else if(match(words[w],"^Op$")) {
  210. addopen("[")
  211. option=1
  212. trailer="]" trailer
  213. } else if(match(words[w],"^Pp$")) {
  214. endline()
  215. } else if(match(words[w],"^An$")) {
  216. endline()
  217. } else if(match(words[w],"^Ss$")) {
  218. add(".SS")
  219. } else if(match(words[w],"^Ft$")) {
  220. addopen("\\fI")
  221. trailer = "\\fP" trailer
  222. displaylines = 1
  223. } else if(match(words[w],"^Fn$")) {
  224. # Using '.nh' to suppress hyphenation doesn't really work...
  225. # TODO: Fix this.
  226. add(".nh")
  227. endline()
  228. addopen("\\fB")
  229. w++
  230. add(words[w])
  231. addclose("\\fP")
  232. endline()
  233. add(".hy")
  234. endline()
  235. addpunct("(")
  236. # This is broken; Fn should peek ahead and put each double-quoted
  237. # arg in .Ty and be smarter about following punct.
  238. trailer = ");" trailer
  239. } else if(match(words[w],"^Va$")) {
  240. w++
  241. add("\\fI" words[w] "\\fP")
  242. } else if(match(words[w],"^In$")) {
  243. w++
  244. add("\\fB#include <" words[w] ">\\fP")
  245. breakline()
  246. } else if(match(words[w],"^Pa$")) {
  247. addopen("\\fI")
  248. w++
  249. if(match(words[w],"^\\."))
  250. add("\\&")
  251. add(words[w] "\\fP")
  252. } else if(match(words[w],"^Dv$")) {
  253. add(".BR")
  254. } else if(match(words[w],"^Em|Ev$")) {
  255. add(".IR")
  256. } else if(match(words[w],"^Pq$")) {
  257. addopen("(")
  258. trailer=")" trailer
  259. } else if(match(words[w],"^Aq$")) {
  260. addopen("<")
  261. trailer=">" trailer
  262. } else if(match(words[w],"^Brq$")) {
  263. addopen("{")
  264. trailer="}" trailer
  265. } else if(match(words[w],"^S[xy]$")) {
  266. add(".B " wtail())
  267. } else if(match(words[w],"^Ic$")) {
  268. add("\\fB")
  269. trailer="\\fP" trailer
  270. } else if(match(words[w],"^Bl$")) {
  271. oldoptlist=optlist
  272. if(match(words[w+1],"-bullet"))
  273. optlist=1
  274. else if(match(words[w+1],"-enum")) {
  275. optlist=2
  276. enum=0
  277. } else if(match(words[w+1],"-tag"))
  278. optlist=3
  279. else if(match(words[w+1],"-item"))
  280. optlist=4
  281. else if(match(words[w+1],"-bullet"))
  282. optlist=1
  283. w=nwords
  284. } else if(match(words[w],"^El$")) {
  285. optlist=oldoptlist
  286. } else if(match(words[w],"^It$")&&optlist) {
  287. if(optlist==1)
  288. add(".IP \\(bu")
  289. else if(optlist==2)
  290. add(".IP " ++enum ".")
  291. else if(optlist==3) {
  292. add(".TP")
  293. endline()
  294. if(match(words[w+1],"^Pa$|^Ev$")) {
  295. add(".B")
  296. w++
  297. }
  298. } else if(optlist==4)
  299. add(".IP")
  300. } else if(match(words[w],"^[=]$")) {
  301. addpunct(words[w])
  302. } else if(match(words[w],"^[\[{(]$")) {
  303. addopen(words[w])
  304. } else if(match(words[w],"^[\\\])}.,;:]$")) {
  305. addclose(words[w])
  306. } else {
  307. add(words[w])
  308. }
  309. }
  310. if(match(out,"^\\.[^a-zA-Z]"))
  311. sub("^\\.","",out)
  312. endline()
  313. }