Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 1 | #!/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 | |
| 24 | BEGIN { |
| 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 | |
| 38 | function 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 | |
| 48 | function 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 79 | add(OFS) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 80 | } else if(match(words[w],"^Xc$")) { |
| 81 | skip=1 |
| 82 | ext=0 |
| 83 | if(!extopt) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 84 | prenl++ |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 85 | w=nwords |
| 86 | } else if(match(words[w],"^Bd$")) { |
| 87 | skip=1 |
| 88 | if(match(words[w+1],"-literal")) { |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 89 | literal=1 |
| 90 | prenl++ |
| 91 | w=nwords |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 92 | } |
| 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 99 | nospace=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 100 | 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 110 | add(OFS words[++w]) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 111 | add("''") |
| 112 | if(!nospace&&match(words[w+1],"^[\\.,]")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 113 | nospace=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 114 | } else if(match(words[w],"^Sq|Ql$")) { |
| 115 | skip=1 |
| 116 | add("`" words[++w] "'") |
| 117 | if(!nospace&&match(words[w+1],"^[\\.,]")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 118 | nospace=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 119 | } else if(match(words[w],"^Oo$")) { |
| 120 | skip=1 |
| 121 | extopt=1 |
| 122 | if(!nospace) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 123 | nospace=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 124 | 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 132 | add(OFS) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 133 | if(nospace==1) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 134 | nospace=0 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 135 | } |
| 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 161 | add(refauthors[i]) |
| 162 | if(i>1) |
| 163 | add(", ") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 164 | } |
| 165 | if(nrefauthors>1) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 166 | add(" and ") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 167 | add(refauthors[0] ", \\fI" reftitle "\\fP") |
| 168 | if(length(refissue)) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 169 | add(", " refissue) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 170 | if(length(refdate)) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 171 | add(", " refdate) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 172 | if(length(refopt)) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 173 | add(", " refopt) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 174 | add(".") |
| 175 | reference=0 |
| 176 | } else if(reference) { |
| 177 | if(match(words[w],"^%A$")) { refauthors[nrefauthors++]=wtail() } |
| 178 | if(match(words[w],"^%T$")) { |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 179 | reftitle=wtail() |
| 180 | sub("^\"","",reftitle) |
| 181 | sub("\"$","",reftitle) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 182 | } |
| 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 188 | add(".br") |
| 189 | prenl++ |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 190 | } |
| 191 | n=words[++w] |
| 192 | if(!length(name)) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 193 | name=n |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 194 | if(!length(n)) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 195 | n=name |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 196 | add("\\fB" n "\\fP") |
| 197 | if(!nospace&&match(words[w+1],"^[\\.,]")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 198 | nospace=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 199 | } 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 204 | nospace=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 205 | } else if(match(words[w],"^Ar$")) { |
| 206 | add("\\fI") |
| 207 | if(w==nwords) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 208 | add("file ...\\fP") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 209 | else { |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 210 | add(words[++w] "\\fP") |
| 211 | while(match(words[w+1],"^\\|$")) |
| 212 | add(OFS words[++w] " \\fI" words[++w] "\\fP") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 213 | } |
| 214 | if(!nospace&&match(words[w+1],"^[\\.,]")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 215 | nospace=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 216 | } else if(match(words[w],"^Cm$")) { |
| 217 | add("\\fB" words[++w] "\\fP") |
| 218 | while(w<nwords&&match(words[w+1],"^[\\.,:;)]")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 219 | add(words[++w]) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 220 | } else if(match(words[w],"^Op$")) { |
| 221 | option=1 |
| 222 | if(!nospace) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 223 | nospace=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 224 | 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 235 | add("\\&") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 236 | add(words[w] "\\fP") |
| 237 | while(w<nwords&&match(words[w+1],"^[\\.,:;)]")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 238 | add(words[++w]) |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 239 | } 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 257 | 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 Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 281 | } |
| 282 | sub(" $","",line) |
| 283 | if(plain) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 284 | add("\\fP") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 285 | } else if(match(words[w],"^Bl$")) { |
| 286 | oldoptlist=optlist |
| 287 | if(match(words[w+1],"-bullet")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 288 | optlist=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 289 | else if(match(words[w+1],"-enum")) { |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 290 | optlist=2 |
| 291 | enum=0 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 292 | } else if(match(words[w+1],"-tag")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 293 | optlist=3 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 294 | else if(match(words[w+1],"-item")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 295 | optlist=4 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 296 | else if(match(words[w+1],"-bullet")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 297 | optlist=1 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 298 | 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 Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 303 | add(".IP \\(bu") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 304 | else if(optlist==2) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 305 | add(".IP " ++enum ".") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 306 | else if(optlist==3) { |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 307 | add(".TP") |
| 308 | prenl++ |
| 309 | if(match(words[w+1],"^Pa|Ev$")) { |
| 310 | add(".B") |
| 311 | w++ |
| 312 | } |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 313 | } else if(optlist==4) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 314 | add(".IP") |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 315 | } else if(match(words[w],"^Sm$")) { |
| 316 | if(match(words[w+1],"off")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 317 | nospace=2 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 318 | else if(match(words[w+1],"on")) |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 319 | nospace=0 |
Darren Tucker | 167bd9c | 2003-09-07 12:34:54 +1000 | [diff] [blame] | 320 | 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 | } |