mdoc2man.awk 8.41 KB
Newer Older
1 2
#!/usr/bin/awk
#
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 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#  v3, I put the program under a proper license
#      Dan Nelson <dnelson@allantgroup.com> added .An, .Aq and fixed a typo
#  v2, fixed to work on GNU awk --posix and MacOS X
#  v1, first attempt, didn't work on MacOS X
#
# Copyright (c) 2003 Peter Stuge <stuge-mdoc2man@cdy.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


BEGIN {
  optlist=0
  oldoptlist=0
  nospace=0
  synopsis=0
  reference=0
  block=0
  ext=0
  extopt=0
  literal=0
  prenl=0
38
  breakw=0
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  line=""
}

function wtail() {
  retval=""
  while(w<nwords) {
    if(length(retval))
      retval=retval OFS
    retval=retval words[++w]
  }
  return retval
}

function add(str) {
  for(;prenl;prenl--)
    line=line "\n"
  line=line str
}

! /^\./ {
  for(;prenl;prenl--)
    print ""
  print
  if(literal)
    print ".br"
  next
}

/^\.\\"/ { next }

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