blob: 9135af07e0ee557b50fb039e5dff64cb3ea6c916 [file] [log] [blame]
Darren Tucker167bd9c2003-09-07 12:34:54 +10001#!/usr/bin/awk
2#
3# Version history:
4# v3, I put the program under a proper license
5# Dan Nelson <dnelson@allantgroup.com> added .An, .Aq and fixed a typo
6# v2, fixed to work on GNU awk --posix and MacOS X
7# v1, first attempt, didn't work on MacOS X
8#
9# Copyright (c) 2003 Peter Stuge <stuge-mdoc2man@cdy.org>
10#
11# Permission to use, copy, modify, and distribute this software for any
12# purpose with or without fee is hereby granted, provided that the above
13# copyright notice and this permission notice appear in all copies.
14#
15# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23
24BEGIN {
25 optlist=0
26 oldoptlist=0
27 nospace=0
28 synopsis=0
29 reference=0
30 block=0
31 ext=0
32 extopt=0
33 literal=0
34 prenl=0
35 line=""
36}
37
38function wtail() {
39 retval=""
40 while(w<nwords) {
41 if(length(retval))
42 retval=retval OFS
43 retval=retval words[++w]
44 }
45 return retval
46}
47
48function add(str) {
49 for(;prenl;prenl--)
50 line=line "\n"
51 line=line str
52}
53
54! /^\./ {
55 for(;prenl;prenl--)
56 print ""
57 print
58 if(literal)
59 print ".br"
60 next
61}
62
63/^\.\\"/ { next }
64
65{
66 option=0
67 parens=0
68 angles=0
69 sub("^\\.","")
70 nwords=split($0,words)
71 for(w=1;w<=nwords;w++) {
72 skip=0
73 if(match(words[w],"^Li|Pf$")) {
74 skip=1
75 } else if(match(words[w],"^Xo$")) {
76 skip=1
77 ext=1
78 if(length(line)&&!(match(line," $")||prenl))
Damien Millera8e06ce2003-11-21 23:48:55 +110079 add(OFS)
Darren Tucker167bd9c2003-09-07 12:34:54 +100080 } else if(match(words[w],"^Xc$")) {
81 skip=1
82 ext=0
83 if(!extopt)
Damien Millera8e06ce2003-11-21 23:48:55 +110084 prenl++
Darren Tucker167bd9c2003-09-07 12:34:54 +100085 w=nwords
86 } else if(match(words[w],"^Bd$")) {
87 skip=1
88 if(match(words[w+1],"-literal")) {
Damien Millera8e06ce2003-11-21 23:48:55 +110089 literal=1
90 prenl++
91 w=nwords
Darren Tucker167bd9c2003-09-07 12:34:54 +100092 }
93 } else if(match(words[w],"^Ed$")) {
94 skip=1
95 literal=0
96 } else if(match(words[w],"^Ns$")) {
97 skip=1
98 if(!nospace)
Damien Millera8e06ce2003-11-21 23:48:55 +110099 nospace=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000100 sub(" $","",line)
101 } else if(match(words[w],"^No$")) {
102 skip=1
103 sub(" $","",line)
104 add(words[++w])
105 } else if(match(words[w],"^Dq$")) {
106 skip=1
107 add("``")
108 add(words[++w])
109 while(w<nwords&&!match(words[w+1],"^[\\.,]"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100110 add(OFS words[++w])
Darren Tucker167bd9c2003-09-07 12:34:54 +1000111 add("''")
112 if(!nospace&&match(words[w+1],"^[\\.,]"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100113 nospace=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000114 } else if(match(words[w],"^Sq|Ql$")) {
115 skip=1
116 add("`" words[++w] "'")
117 if(!nospace&&match(words[w+1],"^[\\.,]"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100118 nospace=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000119 } else if(match(words[w],"^Oo$")) {
120 skip=1
121 extopt=1
122 if(!nospace)
Damien Millera8e06ce2003-11-21 23:48:55 +1100123 nospace=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000124 add("[")
125 } else if(match(words[w],"^Oc$")) {
126 skip=1
127 extopt=0
128 add("]")
129 }
130 if(!skip) {
131 if(!nospace&&length(line)&&!(match(line," $")||prenl))
Damien Millera8e06ce2003-11-21 23:48:55 +1100132 add(OFS)
Darren Tucker167bd9c2003-09-07 12:34:54 +1000133 if(nospace==1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100134 nospace=0
Darren Tucker167bd9c2003-09-07 12:34:54 +1000135 }
136 if(match(words[w],"^Dd$")) {
137 date=wtail()
138 next
139 } else if(match(words[w],"^Dt$")) {
140 id=wtail()
141 next
142 } else if(match(words[w],"^Os$")) {
143 add(".TH " id " \"" date "\" \"" wtail() "\"")
144 } else if(match(words[w],"^Sh$")) {
145 add(".SH")
146 synopsis=match(words[w+1],"SYNOPSIS")
147 } else if(match(words[w],"^Xr$")) {
148 add("\\fB" words[++w] "\\fP(" words[++w] ")" words[++w])
149 } else if(match(words[w],"^Rs$")) {
150 split("",refauthors)
151 nrefauthors=0
152 reftitle=""
153 refissue=""
154 refdate=""
155 refopt=""
156 reference=1
157 next
158 } else if(match(words[w],"^Re$")) {
159 prenl++
160 for(i=nrefauthors-1;i>0;i--) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100161 add(refauthors[i])
162 if(i>1)
163 add(", ")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000164 }
165 if(nrefauthors>1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100166 add(" and ")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000167 add(refauthors[0] ", \\fI" reftitle "\\fP")
168 if(length(refissue))
Damien Millera8e06ce2003-11-21 23:48:55 +1100169 add(", " refissue)
Darren Tucker167bd9c2003-09-07 12:34:54 +1000170 if(length(refdate))
Damien Millera8e06ce2003-11-21 23:48:55 +1100171 add(", " refdate)
Darren Tucker167bd9c2003-09-07 12:34:54 +1000172 if(length(refopt))
Damien Millera8e06ce2003-11-21 23:48:55 +1100173 add(", " refopt)
Darren Tucker167bd9c2003-09-07 12:34:54 +1000174 add(".")
175 reference=0
176 } else if(reference) {
177 if(match(words[w],"^%A$")) { refauthors[nrefauthors++]=wtail() }
178 if(match(words[w],"^%T$")) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100179 reftitle=wtail()
180 sub("^\"","",reftitle)
181 sub("\"$","",reftitle)
Darren Tucker167bd9c2003-09-07 12:34:54 +1000182 }
183 if(match(words[w],"^%N$")) { refissue=wtail() }
184 if(match(words[w],"^%D$")) { refdate=wtail() }
185 if(match(words[w],"^%O$")) { refopt=wtail() }
186 } else if(match(words[w],"^Nm$")) {
187 if(synopsis) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100188 add(".br")
189 prenl++
Darren Tucker167bd9c2003-09-07 12:34:54 +1000190 }
191 n=words[++w]
192 if(!length(name))
Damien Millera8e06ce2003-11-21 23:48:55 +1100193 name=n
Darren Tucker167bd9c2003-09-07 12:34:54 +1000194 if(!length(n))
Damien Millera8e06ce2003-11-21 23:48:55 +1100195 n=name
Darren Tucker167bd9c2003-09-07 12:34:54 +1000196 add("\\fB" n "\\fP")
197 if(!nospace&&match(words[w+1],"^[\\.,]"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100198 nospace=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000199 } else if(match(words[w],"^Nd$")) {
200 add("\\- " wtail())
201 } else if(match(words[w],"^Fl$")) {
202 add("\\fB\\-" words[++w] "\\fP")
203 if(!nospace&&match(words[w+1],"^[\\.,]"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100204 nospace=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000205 } else if(match(words[w],"^Ar$")) {
206 add("\\fI")
207 if(w==nwords)
Damien Millera8e06ce2003-11-21 23:48:55 +1100208 add("file ...\\fP")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000209 else {
Damien Millera8e06ce2003-11-21 23:48:55 +1100210 add(words[++w] "\\fP")
211 while(match(words[w+1],"^\\|$"))
212 add(OFS words[++w] " \\fI" words[++w] "\\fP")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000213 }
214 if(!nospace&&match(words[w+1],"^[\\.,]"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100215 nospace=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000216 } else if(match(words[w],"^Cm$")) {
217 add("\\fB" words[++w] "\\fP")
218 while(w<nwords&&match(words[w+1],"^[\\.,:;)]"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100219 add(words[++w])
Darren Tucker167bd9c2003-09-07 12:34:54 +1000220 } else if(match(words[w],"^Op$")) {
221 option=1
222 if(!nospace)
Damien Millera8e06ce2003-11-21 23:48:55 +1100223 nospace=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000224 add("[")
225 } else if(match(words[w],"^Pp$")) {
226 prenl++
227 } else if(match(words[w],"^An$")) {
228 prenl++
229 } else if(match(words[w],"^Ss$")) {
230 add(".SS")
231 } else if(match(words[w],"^Pa$")&&!option) {
232 add("\\fI")
233 w++
234 if(match(words[w],"^\\."))
Damien Millera8e06ce2003-11-21 23:48:55 +1100235 add("\\&")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000236 add(words[w] "\\fP")
237 while(w<nwords&&match(words[w+1],"^[\\.,:;)]"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100238 add(words[++w])
Darren Tucker167bd9c2003-09-07 12:34:54 +1000239 } else if(match(words[w],"^Dv$")) {
240 add(".BR")
241 } else if(match(words[w],"^Em|Ev$")) {
242 add(".IR")
243 } else if(match(words[w],"^Pq$")) {
244 add("(")
245 nospace=1
246 parens=1
247 } else if(match(words[w],"^Aq$")) {
248 add("<")
249 nospace=1
250 angles=1
251 } else if(match(words[w],"^S[xy]$")) {
252 add(".B " wtail())
253 } else if(match(words[w],"^Ic$")) {
254 plain=1
255 add("\\fB")
256 while(w<nwords) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100257 w++
258 if(match(words[w],"^Op$")) {
259 w++
260 add("[")
261 words[nwords]=words[nwords] "]"
262 }
263 if(match(words[w],"^Ar$")) {
264 add("\\fI" words[++w] "\\fP")
265 } else if(match(words[w],"^[\\.,]")) {
266 sub(" $","",line)
267 if(plain) {
268 add("\\fP")
269 plain=0
270 }
271 add(words[w])
272 } else {
273 if(!plain) {
274 add("\\fB")
275 plain=1
276 }
277 add(words[w])
278 }
279 if(!nospace)
280 add(OFS)
Darren Tucker167bd9c2003-09-07 12:34:54 +1000281 }
282 sub(" $","",line)
283 if(plain)
Damien Millera8e06ce2003-11-21 23:48:55 +1100284 add("\\fP")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000285 } else if(match(words[w],"^Bl$")) {
286 oldoptlist=optlist
287 if(match(words[w+1],"-bullet"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100288 optlist=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000289 else if(match(words[w+1],"-enum")) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100290 optlist=2
291 enum=0
Darren Tucker167bd9c2003-09-07 12:34:54 +1000292 } else if(match(words[w+1],"-tag"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100293 optlist=3
Darren Tucker167bd9c2003-09-07 12:34:54 +1000294 else if(match(words[w+1],"-item"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100295 optlist=4
Darren Tucker167bd9c2003-09-07 12:34:54 +1000296 else if(match(words[w+1],"-bullet"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100297 optlist=1
Darren Tucker167bd9c2003-09-07 12:34:54 +1000298 w=nwords
299 } else if(match(words[w],"^El$")) {
300 optlist=oldoptlist
301 } else if(match(words[w],"^It$")&&optlist) {
302 if(optlist==1)
Damien Millera8e06ce2003-11-21 23:48:55 +1100303 add(".IP \\(bu")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000304 else if(optlist==2)
Damien Millera8e06ce2003-11-21 23:48:55 +1100305 add(".IP " ++enum ".")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000306 else if(optlist==3) {
Damien Millera8e06ce2003-11-21 23:48:55 +1100307 add(".TP")
308 prenl++
309 if(match(words[w+1],"^Pa|Ev$")) {
310 add(".B")
311 w++
312 }
Darren Tucker167bd9c2003-09-07 12:34:54 +1000313 } else if(optlist==4)
Damien Millera8e06ce2003-11-21 23:48:55 +1100314 add(".IP")
Darren Tucker167bd9c2003-09-07 12:34:54 +1000315 } else if(match(words[w],"^Sm$")) {
316 if(match(words[w+1],"off"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100317 nospace=2
Darren Tucker167bd9c2003-09-07 12:34:54 +1000318 else if(match(words[w+1],"on"))
Damien Millera8e06ce2003-11-21 23:48:55 +1100319 nospace=0
Darren Tucker167bd9c2003-09-07 12:34:54 +1000320 w++
321 } else if(!skip) {
322 add(words[w])
323 }
324 }
325 if(match(line,"^\\.[^a-zA-Z]"))
326 sub("^\\.","",line)
327 if(parens)
328 add(")")
329 if(angles)
330 add(">")
331 if(option)
332 add("]")
333 if(ext&&!extopt&&!match(line," $"))
334 add(OFS)
335 if(!ext&&!extopt&&length(line)) {
336 print line
337 prenl=0
338 line=""
339 }
340}