]>
Commit | Line | Data |
---|---|---|
4bbf95fa | 1 | #!/usr/bin/awk |
2 | # | |
f43b2695 | 3 | # $Id$ |
4 | # | |
4bbf95fa | 5 | # Version history: |
f43b2695 | 6 | # v4+ Adapted for OpenSSH Portable (see cvs Id and history) |
4bbf95fa | 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 | ||
26 | ||
27 | BEGIN { | |
28 | optlist=0 | |
29 | oldoptlist=0 | |
30 | nospace=0 | |
31 | synopsis=0 | |
32 | reference=0 | |
33 | block=0 | |
34 | ext=0 | |
35 | extopt=0 | |
36 | literal=0 | |
37 | prenl=0 | |
544842de | 38 | breakw=0 |
4bbf95fa | 39 | line="" |
40 | } | |
41 | ||
42 | function wtail() { | |
43 | retval="" | |
44 | while(w<nwords) { | |
45 | if(length(retval)) | |
46 | retval=retval OFS | |
47 | retval=retval words[++w] | |
48 | } | |
49 | return retval | |
50 | } | |
51 | ||
52 | function add(str) { | |
53 | for(;prenl;prenl--) | |
54 | line=line "\n" | |
55 | line=line str | |
56 | } | |
57 | ||
58 | ! /^\./ { | |
59 | for(;prenl;prenl--) | |
60 | print "" | |
61 | ||
62 | if(literal) | |
63 | print ".br" | |
64 | next | |
65 | } | |
66 | ||
67 | /^\.\\"/ { next } | |
68 | ||
69 | { | |
70 | option=0 | |
71 | parens=0 | |
72 | angles=0 | |
73 | sub("^\\.","") | |
74 | nwords=split($0,words) | |
75 | for(w=1;w<=nwords;w++) { | |
76 | skip=0 | |
77 | if(match(words[w],"^Li|Pf$")) { | |
78 | skip=1 | |
79 | } else if(match(words[w],"^Xo$")) { | |
80 | skip=1 | |
81 | ext=1 | |
82 | if(length(line)&&!(match(line," $")||prenl)) | |
aff51935 | 83 | add(OFS) |
4bbf95fa | 84 | } else if(match(words[w],"^Xc$")) { |
85 | skip=1 | |
86 | ext=0 | |
87 | if(!extopt) | |
aff51935 | 88 | prenl++ |
4bbf95fa | 89 | w=nwords |
90 | } else if(match(words[w],"^Bd$")) { | |
91 | skip=1 | |
92 | if(match(words[w+1],"-literal")) { | |
aff51935 | 93 | literal=1 |
94 | prenl++ | |
95 | w=nwords | |
4bbf95fa | 96 | } |
97 | } else if(match(words[w],"^Ed$")) { | |
98 | skip=1 | |
99 | literal=0 | |
100 | } else if(match(words[w],"^Ns$")) { | |
101 | skip=1 | |
102 | if(!nospace) | |
aff51935 | 103 | nospace=1 |
4bbf95fa | 104 | sub(" $","",line) |
105 | } else if(match(words[w],"^No$")) { | |
106 | skip=1 | |
107 | sub(" $","",line) | |
108 | add(words[++w]) | |
109 | } else if(match(words[w],"^Dq$")) { | |
110 | skip=1 | |
111 | add("``") | |
112 | add(words[++w]) | |
113 | while(w<nwords&&!match(words[w+1],"^[\\.,]")) | |
aff51935 | 114 | add(OFS words[++w]) |
4bbf95fa | 115 | add("''") |
116 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 117 | nospace=1 |
4bbf95fa | 118 | } else if(match(words[w],"^Sq|Ql$")) { |
119 | skip=1 | |
120 | add("`" words[++w] "'") | |
121 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 122 | nospace=1 |
4bbf95fa | 123 | } else if(match(words[w],"^Oo$")) { |
124 | skip=1 | |
125 | extopt=1 | |
126 | if(!nospace) | |
aff51935 | 127 | nospace=1 |
4bbf95fa | 128 | add("[") |
129 | } else if(match(words[w],"^Oc$")) { | |
130 | skip=1 | |
131 | extopt=0 | |
132 | add("]") | |
133 | } | |
134 | if(!skip) { | |
135 | if(!nospace&&length(line)&&!(match(line," $")||prenl)) | |
aff51935 | 136 | add(OFS) |
4bbf95fa | 137 | if(nospace==1) |
aff51935 | 138 | nospace=0 |
4bbf95fa | 139 | } |
140 | if(match(words[w],"^Dd$")) { | |
8c5a3b27 | 141 | if(match(words[w+1],"^\\$Mdocdate:")) { |
f43b2695 | 142 | w++; |
143 | if(match(words[w+4],"^\\$$")) { | |
144 | words[w+4] = "" | |
145 | } | |
146 | } | |
4bbf95fa | 147 | date=wtail() |
148 | next | |
149 | } else if(match(words[w],"^Dt$")) { | |
150 | id=wtail() | |
151 | next | |
cbcabe98 | 152 | } else if(match(words[w],"^Ox$")) { |
153 | add("OpenBSD") | |
154 | skip=1 | |
4bbf95fa | 155 | } else if(match(words[w],"^Os$")) { |
156 | add(".TH " id " \"" date "\" \"" wtail() "\"") | |
157 | } else if(match(words[w],"^Sh$")) { | |
158 | add(".SH") | |
159 | synopsis=match(words[w+1],"SYNOPSIS") | |
160 | } else if(match(words[w],"^Xr$")) { | |
161 | add("\\fB" words[++w] "\\fP(" words[++w] ")" words[++w]) | |
162 | } else if(match(words[w],"^Rs$")) { | |
163 | split("",refauthors) | |
164 | nrefauthors=0 | |
165 | reftitle="" | |
166 | refissue="" | |
167 | refdate="" | |
168 | refopt="" | |
757841b2 | 169 | refreport="" |
4bbf95fa | 170 | reference=1 |
171 | next | |
172 | } else if(match(words[w],"^Re$")) { | |
173 | prenl++ | |
174 | for(i=nrefauthors-1;i>0;i--) { | |
aff51935 | 175 | add(refauthors[i]) |
176 | if(i>1) | |
177 | add(", ") | |
4bbf95fa | 178 | } |
179 | if(nrefauthors>1) | |
aff51935 | 180 | add(" and ") |
757841b2 | 181 | if(nrefauthors>0) |
182 | add(refauthors[0] ", ") | |
183 | add("\\fI" reftitle "\\fP") | |
4bbf95fa | 184 | if(length(refissue)) |
aff51935 | 185 | add(", " refissue) |
757841b2 | 186 | if(length(refreport)) { |
187 | add(", " refreport) | |
188 | } | |
4bbf95fa | 189 | if(length(refdate)) |
aff51935 | 190 | add(", " refdate) |
4bbf95fa | 191 | if(length(refopt)) |
aff51935 | 192 | add(", " refopt) |
4bbf95fa | 193 | add(".") |
194 | reference=0 | |
195 | } else if(reference) { | |
196 | if(match(words[w],"^%A$")) { refauthors[nrefauthors++]=wtail() } | |
197 | if(match(words[w],"^%T$")) { | |
aff51935 | 198 | reftitle=wtail() |
199 | sub("^\"","",reftitle) | |
200 | sub("\"$","",reftitle) | |
4bbf95fa | 201 | } |
202 | if(match(words[w],"^%N$")) { refissue=wtail() } | |
203 | if(match(words[w],"^%D$")) { refdate=wtail() } | |
204 | if(match(words[w],"^%O$")) { refopt=wtail() } | |
757841b2 | 205 | if(match(words[w],"^%R$")) { refreport=wtail() } |
4bbf95fa | 206 | } else if(match(words[w],"^Nm$")) { |
207 | if(synopsis) { | |
aff51935 | 208 | add(".br") |
209 | prenl++ | |
4bbf95fa | 210 | } |
211 | n=words[++w] | |
212 | if(!length(name)) | |
aff51935 | 213 | name=n |
4bbf95fa | 214 | if(!length(n)) |
aff51935 | 215 | n=name |
4bbf95fa | 216 | add("\\fB" n "\\fP") |
217 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 218 | nospace=1 |
4bbf95fa | 219 | } else if(match(words[w],"^Nd$")) { |
220 | add("\\- " wtail()) | |
221 | } else if(match(words[w],"^Fl$")) { | |
222 | add("\\fB\\-" words[++w] "\\fP") | |
223 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 224 | nospace=1 |
4bbf95fa | 225 | } else if(match(words[w],"^Ar$")) { |
226 | add("\\fI") | |
227 | if(w==nwords) | |
aff51935 | 228 | add("file ...\\fP") |
4bbf95fa | 229 | else { |
aff51935 | 230 | add(words[++w] "\\fP") |
231 | while(match(words[w+1],"^\\|$")) | |
232 | add(OFS words[++w] " \\fI" words[++w] "\\fP") | |
4bbf95fa | 233 | } |
234 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 235 | nospace=1 |
4bbf95fa | 236 | } else if(match(words[w],"^Cm$")) { |
237 | add("\\fB" words[++w] "\\fP") | |
238 | while(w<nwords&&match(words[w+1],"^[\\.,:;)]")) | |
aff51935 | 239 | add(words[++w]) |
4bbf95fa | 240 | } else if(match(words[w],"^Op$")) { |
241 | option=1 | |
242 | if(!nospace) | |
aff51935 | 243 | nospace=1 |
4bbf95fa | 244 | add("[") |
245 | } else if(match(words[w],"^Pp$")) { | |
246 | prenl++ | |
247 | } else if(match(words[w],"^An$")) { | |
248 | prenl++ | |
249 | } else if(match(words[w],"^Ss$")) { | |
250 | add(".SS") | |
251 | } else if(match(words[w],"^Pa$")&&!option) { | |
252 | add("\\fI") | |
253 | w++ | |
254 | if(match(words[w],"^\\.")) | |
aff51935 | 255 | add("\\&") |
4bbf95fa | 256 | add(words[w] "\\fP") |
257 | while(w<nwords&&match(words[w+1],"^[\\.,:;)]")) | |
aff51935 | 258 | add(words[++w]) |
4bbf95fa | 259 | } else if(match(words[w],"^Dv$")) { |
260 | add(".BR") | |
261 | } else if(match(words[w],"^Em|Ev$")) { | |
262 | add(".IR") | |
263 | } else if(match(words[w],"^Pq$")) { | |
264 | add("(") | |
265 | nospace=1 | |
266 | parens=1 | |
267 | } else if(match(words[w],"^Aq$")) { | |
268 | add("<") | |
269 | nospace=1 | |
270 | angles=1 | |
271 | } else if(match(words[w],"^S[xy]$")) { | |
272 | add(".B " wtail()) | |
273 | } else if(match(words[w],"^Ic$")) { | |
274 | plain=1 | |
275 | add("\\fB") | |
276 | while(w<nwords) { | |
aff51935 | 277 | w++ |
278 | if(match(words[w],"^Op$")) { | |
279 | w++ | |
280 | add("[") | |
281 | words[nwords]=words[nwords] "]" | |
282 | } | |
283 | if(match(words[w],"^Ar$")) { | |
284 | add("\\fI" words[++w] "\\fP") | |
285 | } else if(match(words[w],"^[\\.,]")) { | |
286 | sub(" $","",line) | |
287 | if(plain) { | |
288 | add("\\fP") | |
289 | plain=0 | |
290 | } | |
291 | add(words[w]) | |
292 | } else { | |
293 | if(!plain) { | |
294 | add("\\fB") | |
295 | plain=1 | |
296 | } | |
297 | add(words[w]) | |
298 | } | |
299 | if(!nospace) | |
300 | add(OFS) | |
4bbf95fa | 301 | } |
302 | sub(" $","",line) | |
303 | if(plain) | |
aff51935 | 304 | add("\\fP") |
4bbf95fa | 305 | } else if(match(words[w],"^Bl$")) { |
306 | oldoptlist=optlist | |
307 | if(match(words[w+1],"-bullet")) | |
aff51935 | 308 | optlist=1 |
4bbf95fa | 309 | else if(match(words[w+1],"-enum")) { |
aff51935 | 310 | optlist=2 |
311 | enum=0 | |
4bbf95fa | 312 | } else if(match(words[w+1],"-tag")) |
aff51935 | 313 | optlist=3 |
4bbf95fa | 314 | else if(match(words[w+1],"-item")) |
aff51935 | 315 | optlist=4 |
4bbf95fa | 316 | else if(match(words[w+1],"-bullet")) |
aff51935 | 317 | optlist=1 |
4bbf95fa | 318 | w=nwords |
319 | } else if(match(words[w],"^El$")) { | |
320 | optlist=oldoptlist | |
544842de | 321 | } else if(match(words[w],"^Bk$")) { |
322 | if(match(words[w+1],"-words")) { | |
323 | w++ | |
324 | breakw=1 | |
325 | } | |
326 | } else if(match(words[w],"^Ek$")) { | |
327 | breakw=0 | |
4bbf95fa | 328 | } else if(match(words[w],"^It$")&&optlist) { |
329 | if(optlist==1) | |
aff51935 | 330 | add(".IP \\(bu") |
4bbf95fa | 331 | else if(optlist==2) |
aff51935 | 332 | add(".IP " ++enum ".") |
4bbf95fa | 333 | else if(optlist==3) { |
aff51935 | 334 | add(".TP") |
335 | prenl++ | |
78666263 | 336 | if(match(words[w+1],"^Pa$|^Ev$")) { |
aff51935 | 337 | add(".B") |
338 | w++ | |
339 | } | |
4bbf95fa | 340 | } else if(optlist==4) |
aff51935 | 341 | add(".IP") |
4bbf95fa | 342 | } else if(match(words[w],"^Sm$")) { |
343 | if(match(words[w+1],"off")) | |
aff51935 | 344 | nospace=2 |
4bbf95fa | 345 | else if(match(words[w+1],"on")) |
aff51935 | 346 | nospace=0 |
4bbf95fa | 347 | w++ |
348 | } else if(!skip) { | |
349 | add(words[w]) | |
350 | } | |
351 | } | |
352 | if(match(line,"^\\.[^a-zA-Z]")) | |
353 | sub("^\\.","",line) | |
354 | if(parens) | |
355 | add(")") | |
356 | if(angles) | |
357 | add(">") | |
358 | if(option) | |
359 | add("]") | |
360 | if(ext&&!extopt&&!match(line," $")) | |
361 | add(OFS) | |
362 | if(!ext&&!extopt&&length(line)) { | |
363 | print line | |
364 | prenl=0 | |
365 | line="" | |
366 | } | |
367 | } |