mdoc2man.awk 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/usr/bin/awk
  2. #
  3. # $Id: mdoc2man.awk,v 1.9 2009/10/24 00:52:42 dtucker Exp $
  4. #
  5. # Version history:
  6. # v4+ Adapted for OpenSSH Portable (see cvs Id and history)
  7. # v3, I put the program under a proper license
  8. # Dan Nelson <dnelson@allantgroup.com> added .An, .Aq and fixed a typo
  9. # v2, fixed to work on GNU awk --posix and MacOS X
  10. # v1, first attempt, didn't work on MacOS X
  11. #
  12. # Copyright (c) 2003 Peter Stuge <stuge-mdoc2man@cdy.org>
  13. #
  14. # Permission to use, copy, modify, and distribute this software for any
  15. # purpose with or without fee is hereby granted, provided that the above
  16. # copyright notice and this permission notice appear in all copies.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  19. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  20. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  21. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  22. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  23. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  24. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25. BEGIN {
  26. optlist=0
  27. oldoptlist=0
  28. nospace=0
  29. synopsis=0
  30. reference=0
  31. block=0
  32. ext=0
  33. extopt=0
  34. literal=0
  35. prenl=0
  36. breakw=0
  37. line=""
  38. }
  39. function wtail() {
  40. retval=""
  41. while(w<nwords) {
  42. if(length(retval))
  43. retval=retval OFS
  44. retval=retval words[++w]
  45. }
  46. return retval
  47. }
  48. function add(str) {
  49. for(;prenl;prenl--)
  50. line=line "\n"
  51. line=line str
  52. }
  53. ! /^\./ {
  54. for(;prenl;prenl--)
  55. print ""
  56. print
  57. if(literal)
  58. print ".br"
  59. next
  60. }
  61. /^\.\\"/ { next }
  62. {
  63. option=0
  64. parens=0
  65. angles=0
  66. sub("^\\.","")
  67. nwords=split($0,words)
  68. for(w=1;w<=nwords;w++) {
  69. skip=0
  70. if(match(words[w],"^Li|Pf$")) {
  71. skip=1
  72. } else if(match(words[w],"^Xo$")) {
  73. skip=1
  74. ext=1
  75. if(length(line)&&!(match(line," $")||prenl))
  76. add(OFS)
  77. } else if(match(words[w],"^Xc$")) {
  78. skip=1
  79. ext=0
  80. if(!extopt)
  81. prenl++
  82. w=nwords
  83. } else if(match(words[w],"^Bd$")) {
  84. skip=1
  85. if(match(words[w+1],"-literal")) {
  86. literal=1
  87. prenl++
  88. w=nwords
  89. }
  90. } else if(match(words[w],"^Ed$")) {
  91. skip=1
  92. literal=0
  93. } else if(match(words[w],"^Ns$")) {
  94. skip=1
  95. if(!nospace)
  96. nospace=1
  97. sub(" $","",line)
  98. } else if(match(words[w],"^No$")) {
  99. skip=1
  100. sub(" $","",line)
  101. add(words[++w])
  102. } else if(match(words[w],"^Dq$")) {
  103. skip=1
  104. add("``")
  105. add(words[++w])
  106. while(w<nwords&&!match(words[w+1],"^[\\.,]"))
  107. add(OFS words[++w])
  108. add("''")
  109. if(!nospace&&match(words[w+1],"^[\\.,]"))
  110. nospace=1
  111. } else if(match(words[w],"^Sq|Ql$")) {
  112. skip=1
  113. add("`" words[++w] "'")
  114. if(!nospace&&match(words[w+1],"^[\\.,]"))
  115. nospace=1
  116. } else if(match(words[w],"^Oo$")) {
  117. skip=1
  118. extopt=1
  119. if(!nospace)
  120. nospace=1
  121. add("[")
  122. } else if(match(words[w],"^Oc$")) {
  123. skip=1
  124. extopt=0
  125. add("]")
  126. }
  127. if(!skip) {
  128. if(!nospace&&length(line)&&!(match(line," $")||prenl))
  129. add(OFS)
  130. if(nospace==1)
  131. nospace=0
  132. }
  133. if(match(words[w],"^Dd$")) {
  134. if(match(words[w+1],"^\\$Mdocdate:")) {
  135. w++;
  136. if(match(words[w+4],"^\\$$")) {
  137. words[w+4] = ""
  138. }
  139. }
  140. date=wtail()
  141. next
  142. } else if(match(words[w],"^Dt$")) {
  143. id=wtail()
  144. next
  145. } else if(match(words[w],"^Ux$")) {
  146. add("UNIX")
  147. skip=1
  148. } else if(match(words[w],"^Ox$")) {
  149. add("OpenBSD")
  150. skip=1
  151. } else if(match(words[w],"^Os$")) {
  152. add(".TH " id " \"" date "\" \"" wtail() "\"")
  153. } else if(match(words[w],"^Sh$")) {
  154. add(".SH")
  155. synopsis=match(words[w+1],"SYNOPSIS")
  156. } else if(match(words[w],"^Xr$")) {
  157. add("\\fB" words[++w] "\\fP(" words[++w] ")" words[++w])
  158. } else if(match(words[w],"^Rs$")) {
  159. split("",refauthors)
  160. nrefauthors=0
  161. reftitle=""
  162. refissue=""
  163. refdate=""
  164. refopt=""
  165. refreport=""
  166. reference=1
  167. next
  168. } else if(match(words[w],"^Re$")) {
  169. prenl++
  170. for(i=nrefauthors-1;i>0;i--) {
  171. add(refauthors[i])
  172. if(i>1)
  173. add(", ")
  174. }
  175. if(nrefauthors>1)
  176. add(" and ")
  177. if(nrefauthors>0)
  178. add(refauthors[0] ", ")
  179. add("\\fI" reftitle "\\fP")
  180. if(length(refissue))
  181. add(", " refissue)
  182. if(length(refreport)) {
  183. add(", " refreport)
  184. }
  185. if(length(refdate))
  186. add(", " refdate)
  187. if(length(refopt))
  188. add(", " refopt)
  189. add(".")
  190. reference=0
  191. } else if(reference) {
  192. if(match(words[w],"^%A$")) { refauthors[nrefauthors++]=wtail() }
  193. if(match(words[w],"^%T$")) {
  194. reftitle=wtail()
  195. sub("^\"","",reftitle)
  196. sub("\"$","",reftitle)
  197. }
  198. if(match(words[w],"^%N$")) { refissue=wtail() }
  199. if(match(words[w],"^%D$")) { refdate=wtail() }
  200. if(match(words[w],"^%O$")) { refopt=wtail() }
  201. if(match(words[w],"^%R$")) { refreport=wtail() }
  202. } else if(match(words[w],"^Nm$")) {
  203. if(synopsis) {
  204. add(".br")
  205. prenl++
  206. }
  207. n=words[++w]
  208. if(!length(name))
  209. name=n
  210. if(!length(n))
  211. n=name
  212. add("\\fB" n "\\fP")
  213. if(!nospace&&match(words[w+1],"^[\\.,]"))
  214. nospace=1
  215. } else if(match(words[w],"^Nd$")) {
  216. add("\\- " wtail())
  217. } else if(match(words[w],"^Fl$")) {
  218. add("\\fB\\-" words[++w] "\\fP")
  219. if(!nospace&&match(words[w+1],"^[\\.,]"))
  220. nospace=1
  221. } else if(match(words[w],"^Ar$")) {
  222. add("\\fI")
  223. if(w==nwords)
  224. add("file ...\\fP")
  225. else {
  226. add(words[++w] "\\fP")
  227. while(match(words[w+1],"^\\|$"))
  228. add(OFS words[++w] " \\fI" words[++w] "\\fP")
  229. }
  230. if(!nospace&&match(words[w+1],"^[\\.,]"))
  231. nospace=1
  232. } else if(match(words[w],"^Cm$")) {
  233. add("\\fB" words[++w] "\\fP")
  234. while(w<nwords&&match(words[w+1],"^[\\.,:;)]"))
  235. add(words[++w])
  236. } else if(match(words[w],"^Op$")) {
  237. option=1
  238. if(!nospace)
  239. nospace=1
  240. add("[")
  241. } else if(match(words[w],"^Pp$")) {
  242. prenl++
  243. } else if(match(words[w],"^An$")) {
  244. prenl++
  245. } else if(match(words[w],"^Ss$")) {
  246. add(".SS")
  247. } else if(match(words[w],"^Pa$")&&!option) {
  248. add("\\fI")
  249. w++
  250. if(match(words[w],"^\\."))
  251. add("\\&")
  252. add(words[w] "\\fP")
  253. while(w<nwords&&match(words[w+1],"^[\\.,:;)]"))
  254. add(words[++w])
  255. } else if(match(words[w],"^Dv$")) {
  256. add(".BR")
  257. } else if(match(words[w],"^Em|Ev$")) {
  258. add(".IR")
  259. } else if(match(words[w],"^Pq$")) {
  260. add("(")
  261. nospace=1
  262. parens=1
  263. } else if(match(words[w],"^Aq$")) {
  264. add("<")
  265. nospace=1
  266. angles=1
  267. } else if(match(words[w],"^S[xy]$")) {
  268. add(".B " wtail())
  269. } else if(match(words[w],"^Ic$")) {
  270. plain=1
  271. add("\\fB")
  272. while(w<nwords) {
  273. w++
  274. if(match(words[w],"^Op$")) {
  275. w++
  276. add("[")
  277. words[nwords]=words[nwords] "]"
  278. }
  279. if(match(words[w],"^Ar$")) {
  280. add("\\fI" words[++w] "\\fP")
  281. } else if(match(words[w],"^[\\.,]")) {
  282. sub(" $","",line)
  283. if(plain) {
  284. add("\\fP")
  285. plain=0
  286. }
  287. add(words[w])
  288. } else {
  289. if(!plain) {
  290. add("\\fB")
  291. plain=1
  292. }
  293. add(words[w])
  294. }
  295. if(!nospace)
  296. add(OFS)
  297. }
  298. sub(" $","",line)
  299. if(plain)
  300. add("\\fP")
  301. } else if(match(words[w],"^Bl$")) {
  302. oldoptlist=optlist
  303. if(match(words[w+1],"-bullet"))
  304. optlist=1
  305. else if(match(words[w+1],"-enum")) {
  306. optlist=2
  307. enum=0
  308. } else if(match(words[w+1],"-tag"))
  309. optlist=3
  310. else if(match(words[w+1],"-item"))
  311. optlist=4
  312. else if(match(words[w+1],"-bullet"))
  313. optlist=1
  314. w=nwords
  315. } else if(match(words[w],"^El$")) {
  316. optlist=oldoptlist
  317. } else if(match(words[w],"^Bk$")) {
  318. if(match(words[w+1],"-words")) {
  319. w++
  320. breakw=1
  321. }
  322. } else if(match(words[w],"^Ek$")) {
  323. breakw=0
  324. } else if(match(words[w],"^It$")&&optlist) {
  325. if(optlist==1)
  326. add(".IP \\(bu")
  327. else if(optlist==2)
  328. add(".IP " ++enum ".")
  329. else if(optlist==3) {
  330. add(".TP")
  331. prenl++
  332. if(match(words[w+1],"^Pa$|^Ev$")) {
  333. add(".B")
  334. w++
  335. }
  336. } else if(optlist==4)
  337. add(".IP")
  338. } else if(match(words[w],"^Sm$")) {
  339. if(match(words[w+1],"off"))
  340. nospace=2
  341. else if(match(words[w+1],"on"))
  342. nospace=0
  343. w++
  344. } else if(!skip) {
  345. add(words[w])
  346. }
  347. }
  348. if(match(line,"^\\.[^a-zA-Z]"))
  349. sub("^\\.","",line)
  350. if(parens)
  351. add(")")
  352. if(angles)
  353. add(">")
  354. if(option)
  355. add("]")
  356. if(ext&&!extopt&&!match(line," $"))
  357. add(OFS)
  358. if(!ext&&!extopt&&length(line)) {
  359. print line
  360. prenl=0
  361. line=""
  362. }
  363. }