Print this page
9718 update mandoc to 1.14.4
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mandoc/mdoc_man.c
+++ new/usr/src/cmd/mandoc/mdoc_man.c
1 -/* $Id: mdoc_man.c,v 1.122 2017/06/14 22:51:25 schwarze Exp $ */
1 +/* $Id: mdoc_man.c,v 1.126 2018/04/11 17:11:13 schwarze Exp $ */
2 2 /*
3 - * Copyright (c) 2011-2017 Ingo Schwarze <schwarze@openbsd.org>
3 + * Copyright (c) 2011-2018 Ingo Schwarze <schwarze@openbsd.org>
4 4 *
5 5 * Permission to use, copy, modify, and distribute this software for any
6 6 * purpose with or without fee is hereby granted, provided that the above
7 7 * copyright notice and this permission notice appear in all copies.
8 8 *
9 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 16 */
17 17 #include "config.h"
18 18
19 19 #include <sys/types.h>
20 20
21 21 #include <assert.h>
22 22 #include <stdio.h>
23 23 #include <stdlib.h>
24 24 #include <string.h>
25 25
26 26 #include "mandoc_aux.h"
27 27 #include "mandoc.h"
28 28 #include "roff.h"
29 29 #include "mdoc.h"
30 30 #include "man.h"
31 31 #include "out.h"
32 32 #include "main.h"
33 33
34 34 #define DECL_ARGS const struct roff_meta *meta, struct roff_node *n
35 35
36 36 typedef int (*int_fp)(DECL_ARGS);
37 37 typedef void (*void_fp)(DECL_ARGS);
38 38
39 39 struct manact {
40 40 int_fp cond; /* DON'T run actions */
41 41 int_fp pre; /* pre-node action */
42 42 void_fp post; /* post-node action */
43 43 const char *prefix; /* pre-node string constant */
44 44 const char *suffix; /* post-node string constant */
45 45 };
46 46
47 47 static int cond_body(DECL_ARGS);
48 48 static int cond_head(DECL_ARGS);
49 49 static void font_push(char);
50 50 static void font_pop(void);
51 51 static int man_strlen(const char *);
52 52 static void mid_it(void);
53 53 static void post__t(DECL_ARGS);
54 54 static void post_aq(DECL_ARGS);
55 55 static void post_bd(DECL_ARGS);
56 56 static void post_bf(DECL_ARGS);
57 57 static void post_bk(DECL_ARGS);
58 58 static void post_bl(DECL_ARGS);
59 59 static void post_dl(DECL_ARGS);
60 60 static void post_en(DECL_ARGS);
61 61 static void post_enc(DECL_ARGS);
62 62 static void post_eo(DECL_ARGS);
63 63 static void post_fa(DECL_ARGS);
64 64 static void post_fd(DECL_ARGS);
65 65 static void post_fl(DECL_ARGS);
66 66 static void post_fn(DECL_ARGS);
67 67 static void post_fo(DECL_ARGS);
68 68 static void post_font(DECL_ARGS);
69 69 static void post_in(DECL_ARGS);
70 70 static void post_it(DECL_ARGS);
71 71 static void post_lb(DECL_ARGS);
72 72 static void post_nm(DECL_ARGS);
73 73 static void post_percent(DECL_ARGS);
74 74 static void post_pf(DECL_ARGS);
75 75 static void post_sect(DECL_ARGS);
76 76 static void post_vt(DECL_ARGS);
77 77 static int pre__t(DECL_ARGS);
78 78 static int pre_an(DECL_ARGS);
79 79 static int pre_ap(DECL_ARGS);
80 80 static int pre_aq(DECL_ARGS);
81 81 static int pre_bd(DECL_ARGS);
82 82 static int pre_bf(DECL_ARGS);
83 83 static int pre_bk(DECL_ARGS);
84 84 static int pre_bl(DECL_ARGS);
85 85 static void pre_br(DECL_ARGS);
86 86 static int pre_dl(DECL_ARGS);
87 87 static int pre_en(DECL_ARGS);
88 88 static int pre_enc(DECL_ARGS);
89 89 static int pre_em(DECL_ARGS);
90 90 static int pre_skip(DECL_ARGS);
91 91 static int pre_eo(DECL_ARGS);
92 92 static int pre_ex(DECL_ARGS);
93 93 static int pre_fa(DECL_ARGS);
94 94 static int pre_fd(DECL_ARGS);
95 95 static int pre_fl(DECL_ARGS);
96 96 static int pre_fn(DECL_ARGS);
97 97 static int pre_fo(DECL_ARGS);
98 98 static void pre_ft(DECL_ARGS);
99 99 static int pre_Ft(DECL_ARGS);
100 100 static int pre_in(DECL_ARGS);
101 101 static int pre_it(DECL_ARGS);
102 102 static int pre_lk(DECL_ARGS);
103 103 static int pre_li(DECL_ARGS);
104 104 static int pre_nm(DECL_ARGS);
105 105 static int pre_no(DECL_ARGS);
106 106 static int pre_ns(DECL_ARGS);
107 107 static void pre_onearg(DECL_ARGS);
108 108 static int pre_pp(DECL_ARGS);
109 109 static int pre_rs(DECL_ARGS);
110 110 static int pre_sm(DECL_ARGS);
111 111 static void pre_sp(DECL_ARGS);
112 112 static int pre_sect(DECL_ARGS);
113 113 static int pre_sy(DECL_ARGS);
114 114 static void pre_syn(const struct roff_node *);
115 115 static void pre_ta(DECL_ARGS);
116 116 static int pre_vt(DECL_ARGS);
117 117 static int pre_xr(DECL_ARGS);
118 118 static void print_word(const char *);
119 119 static void print_line(const char *, int);
120 120 static void print_block(const char *, int);
121 121 static void print_offs(const char *, int);
122 122 static void print_width(const struct mdoc_bl *,
123 123 const struct roff_node *);
124 124 static void print_count(int *);
125 125 static void print_node(DECL_ARGS);
126 126
127 127 static const void_fp roff_manacts[ROFF_MAX] = {
128 128 pre_br, /* br */
129 129 pre_onearg, /* ce */
130 130 pre_ft, /* ft */
131 131 pre_onearg, /* ll */
132 132 pre_onearg, /* mc */
133 133 pre_onearg, /* po */
134 134 pre_onearg, /* rj */
135 135 pre_sp, /* sp */
136 136 pre_ta, /* ta */
137 137 pre_onearg, /* ti */
138 138 };
139 139
140 140 static const struct manact __manacts[MDOC_MAX - MDOC_Dd] = {
141 141 { NULL, NULL, NULL, NULL, NULL }, /* Dd */
142 142 { NULL, NULL, NULL, NULL, NULL }, /* Dt */
143 143 { NULL, NULL, NULL, NULL, NULL }, /* Os */
144 144 { NULL, pre_sect, post_sect, ".SH", NULL }, /* Sh */
145 145 { NULL, pre_sect, post_sect, ".SS", NULL }, /* Ss */
146 146 { NULL, pre_pp, NULL, NULL, NULL }, /* Pp */
147 147 { cond_body, pre_dl, post_dl, NULL, NULL }, /* D1 */
148 148 { cond_body, pre_dl, post_dl, NULL, NULL }, /* Dl */
149 149 { cond_body, pre_bd, post_bd, NULL, NULL }, /* Bd */
150 150 { NULL, NULL, NULL, NULL, NULL }, /* Ed */
151 151 { cond_body, pre_bl, post_bl, NULL, NULL }, /* Bl */
152 152 { NULL, NULL, NULL, NULL, NULL }, /* El */
153 153 { NULL, pre_it, post_it, NULL, NULL }, /* It */
154 154 { NULL, pre_em, post_font, NULL, NULL }, /* Ad */
155 155 { NULL, pre_an, NULL, NULL, NULL }, /* An */
156 156 { NULL, pre_ap, NULL, NULL, NULL }, /* Ap */
157 157 { NULL, pre_em, post_font, NULL, NULL }, /* Ar */
158 158 { NULL, pre_sy, post_font, NULL, NULL }, /* Cd */
159 159 { NULL, pre_sy, post_font, NULL, NULL }, /* Cm */
160 160 { NULL, pre_li, post_font, NULL, NULL }, /* Dv */
161 161 { NULL, pre_li, post_font, NULL, NULL }, /* Er */
162 162 { NULL, pre_li, post_font, NULL, NULL }, /* Ev */
163 163 { NULL, pre_ex, NULL, NULL, NULL }, /* Ex */
164 164 { NULL, pre_fa, post_fa, NULL, NULL }, /* Fa */
165 165 { NULL, pre_fd, post_fd, NULL, NULL }, /* Fd */
166 166 { NULL, pre_fl, post_fl, NULL, NULL }, /* Fl */
167 167 { NULL, pre_fn, post_fn, NULL, NULL }, /* Fn */
168 168 { NULL, pre_Ft, post_font, NULL, NULL }, /* Ft */
169 169 { NULL, pre_sy, post_font, NULL, NULL }, /* Ic */
170 170 { NULL, pre_in, post_in, NULL, NULL }, /* In */
171 171 { NULL, pre_li, post_font, NULL, NULL }, /* Li */
172 172 { cond_head, pre_enc, NULL, "\\- ", NULL }, /* Nd */
173 173 { NULL, pre_nm, post_nm, NULL, NULL }, /* Nm */
174 174 { cond_body, pre_enc, post_enc, "[", "]" }, /* Op */
175 175 { NULL, pre_Ft, post_font, NULL, NULL }, /* Ot */
176 176 { NULL, pre_em, post_font, NULL, NULL }, /* Pa */
177 177 { NULL, pre_ex, NULL, NULL, NULL }, /* Rv */
178 178 { NULL, NULL, NULL, NULL, NULL }, /* St */
179 179 { NULL, pre_em, post_font, NULL, NULL }, /* Va */
180 180 { NULL, pre_vt, post_vt, NULL, NULL }, /* Vt */
181 181 { NULL, pre_xr, NULL, NULL, NULL }, /* Xr */
182 182 { NULL, NULL, post_percent, NULL, NULL }, /* %A */
183 183 { NULL, pre_em, post_percent, NULL, NULL }, /* %B */
184 184 { NULL, NULL, post_percent, NULL, NULL }, /* %D */
185 185 { NULL, pre_em, post_percent, NULL, NULL }, /* %I */
186 186 { NULL, pre_em, post_percent, NULL, NULL }, /* %J */
187 187 { NULL, NULL, post_percent, NULL, NULL }, /* %N */
188 188 { NULL, NULL, post_percent, NULL, NULL }, /* %O */
189 189 { NULL, NULL, post_percent, NULL, NULL }, /* %P */
190 190 { NULL, NULL, post_percent, NULL, NULL }, /* %R */
191 191 { NULL, pre__t, post__t, NULL, NULL }, /* %T */
192 192 { NULL, NULL, post_percent, NULL, NULL }, /* %V */
193 193 { NULL, NULL, NULL, NULL, NULL }, /* Ac */
194 194 { cond_body, pre_aq, post_aq, NULL, NULL }, /* Ao */
↓ open down ↓ |
181 lines elided |
↑ open up ↑ |
195 195 { cond_body, pre_aq, post_aq, NULL, NULL }, /* Aq */
196 196 { NULL, NULL, NULL, NULL, NULL }, /* At */
197 197 { NULL, NULL, NULL, NULL, NULL }, /* Bc */
198 198 { NULL, pre_bf, post_bf, NULL, NULL }, /* Bf */
199 199 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bo */
200 200 { cond_body, pre_enc, post_enc, "[", "]" }, /* Bq */
201 201 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bsx */
202 202 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bx */
203 203 { NULL, pre_skip, NULL, NULL, NULL }, /* Db */
204 204 { NULL, NULL, NULL, NULL, NULL }, /* Dc */
205 - { cond_body, pre_enc, post_enc, "\\(Lq", "\\(Rq" }, /* Do */
206 - { cond_body, pre_enc, post_enc, "\\(Lq", "\\(Rq" }, /* Dq */
205 + { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Do */
206 + { cond_body, pre_enc, post_enc, "\\(lq", "\\(rq" }, /* Dq */
207 207 { NULL, NULL, NULL, NULL, NULL }, /* Ec */
208 208 { NULL, NULL, NULL, NULL, NULL }, /* Ef */
209 209 { NULL, pre_em, post_font, NULL, NULL }, /* Em */
210 210 { cond_body, pre_eo, post_eo, NULL, NULL }, /* Eo */
211 211 { NULL, pre_bk, post_bk, NULL, NULL }, /* Fx */
212 212 { NULL, pre_sy, post_font, NULL, NULL }, /* Ms */
213 213 { NULL, pre_no, NULL, NULL, NULL }, /* No */
214 214 { NULL, pre_ns, NULL, NULL, NULL }, /* Ns */
215 215 { NULL, pre_bk, post_bk, NULL, NULL }, /* Nx */
216 216 { NULL, pre_bk, post_bk, NULL, NULL }, /* Ox */
217 217 { NULL, NULL, NULL, NULL, NULL }, /* Pc */
218 218 { NULL, NULL, post_pf, NULL, NULL }, /* Pf */
219 219 { cond_body, pre_enc, post_enc, "(", ")" }, /* Po */
220 220 { cond_body, pre_enc, post_enc, "(", ")" }, /* Pq */
221 221 { NULL, NULL, NULL, NULL, NULL }, /* Qc */
222 222 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Ql */
223 223 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qo */
224 224 { cond_body, pre_enc, post_enc, "\"", "\"" }, /* Qq */
225 225 { NULL, NULL, NULL, NULL, NULL }, /* Re */
226 226 { cond_body, pre_rs, NULL, NULL, NULL }, /* Rs */
227 227 { NULL, NULL, NULL, NULL, NULL }, /* Sc */
228 228 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* So */
229 229 { cond_body, pre_enc, post_enc, "\\(oq", "\\(cq" }, /* Sq */
230 230 { NULL, pre_sm, NULL, NULL, NULL }, /* Sm */
231 231 { NULL, pre_em, post_font, NULL, NULL }, /* Sx */
232 232 { NULL, pre_sy, post_font, NULL, NULL }, /* Sy */
233 233 { NULL, pre_li, post_font, NULL, NULL }, /* Tn */
234 234 { NULL, NULL, NULL, NULL, NULL }, /* Ux */
235 235 { NULL, NULL, NULL, NULL, NULL }, /* Xc */
236 236 { NULL, NULL, NULL, NULL, NULL }, /* Xo */
237 237 { NULL, pre_fo, post_fo, NULL, NULL }, /* Fo */
238 238 { NULL, NULL, NULL, NULL, NULL }, /* Fc */
239 239 { cond_body, pre_enc, post_enc, "[", "]" }, /* Oo */
240 240 { NULL, NULL, NULL, NULL, NULL }, /* Oc */
241 241 { NULL, pre_bk, post_bk, NULL, NULL }, /* Bk */
242 242 { NULL, NULL, NULL, NULL, NULL }, /* Ek */
243 243 { NULL, NULL, NULL, NULL, NULL }, /* Bt */
244 244 { NULL, NULL, NULL, NULL, NULL }, /* Hf */
245 245 { NULL, pre_em, post_font, NULL, NULL }, /* Fr */
246 246 { NULL, NULL, NULL, NULL, NULL }, /* Ud */
247 247 { NULL, NULL, post_lb, NULL, NULL }, /* Lb */
248 248 { NULL, pre_pp, NULL, NULL, NULL }, /* Lp */
249 249 { NULL, pre_lk, NULL, NULL, NULL }, /* Lk */
250 250 { NULL, pre_em, post_font, NULL, NULL }, /* Mt */
251 251 { cond_body, pre_enc, post_enc, "{", "}" }, /* Brq */
252 252 { cond_body, pre_enc, post_enc, "{", "}" }, /* Bro */
253 253 { NULL, NULL, NULL, NULL, NULL }, /* Brc */
254 254 { NULL, NULL, post_percent, NULL, NULL }, /* %C */
255 255 { NULL, pre_skip, NULL, NULL, NULL }, /* Es */
256 256 { cond_body, pre_en, post_en, NULL, NULL }, /* En */
257 257 { NULL, pre_bk, post_bk, NULL, NULL }, /* Dx */
258 258 { NULL, NULL, post_percent, NULL, NULL }, /* %Q */
259 259 { NULL, NULL, post_percent, NULL, NULL }, /* %U */
260 260 { NULL, NULL, NULL, NULL, NULL }, /* Ta */
261 261 };
262 262 static const struct manact *const manacts = __manacts - MDOC_Dd;
263 263
264 264 static int outflags;
265 265 #define MMAN_spc (1 << 0) /* blank character before next word */
266 266 #define MMAN_spc_force (1 << 1) /* even before trailing punctuation */
267 267 #define MMAN_nl (1 << 2) /* break man(7) code line */
268 268 #define MMAN_br (1 << 3) /* break output line */
269 269 #define MMAN_sp (1 << 4) /* insert a blank output line */
270 270 #define MMAN_PP (1 << 5) /* reset indentation etc. */
271 271 #define MMAN_Sm (1 << 6) /* horizontal spacing mode */
272 272 #define MMAN_Bk (1 << 7) /* word keep mode */
273 273 #define MMAN_Bk_susp (1 << 8) /* suspend this (after a macro) */
274 274 #define MMAN_An_split (1 << 9) /* author mode is "split" */
275 275 #define MMAN_An_nosplit (1 << 10) /* author mode is "nosplit" */
276 276 #define MMAN_PD (1 << 11) /* inter-paragraph spacing disabled */
277 277 #define MMAN_nbrword (1 << 12) /* do not break the next word */
278 278
279 279 #define BL_STACK_MAX 32
280 280
281 281 static int Bl_stack[BL_STACK_MAX]; /* offsets [chars] */
282 282 static int Bl_stack_post[BL_STACK_MAX]; /* add final .RE */
283 283 static int Bl_stack_len; /* number of nested Bl blocks */
284 284 static int TPremain; /* characters before tag is full */
285 285
286 286 static struct {
287 287 char *head;
288 288 char *tail;
289 289 size_t size;
290 290 } fontqueue;
291 291
292 292
293 293 static int
294 294 man_strlen(const char *cp)
295 295 {
296 296 size_t rsz;
297 297 int skip, sz;
298 298
299 299 sz = 0;
300 300 skip = 0;
301 301 for (;;) {
302 302 rsz = strcspn(cp, "\\");
303 303 if (rsz) {
304 304 cp += rsz;
305 305 if (skip) {
306 306 skip = 0;
307 307 rsz--;
308 308 }
309 309 sz += rsz;
310 310 }
311 311 if ('\0' == *cp)
312 312 break;
313 313 cp++;
314 314 switch (mandoc_escape(&cp, NULL, NULL)) {
315 315 case ESCAPE_ERROR:
316 316 return sz;
317 317 case ESCAPE_UNICODE:
318 318 case ESCAPE_NUMBERED:
319 319 case ESCAPE_SPECIAL:
320 320 case ESCAPE_OVERSTRIKE:
321 321 if (skip)
322 322 skip = 0;
323 323 else
324 324 sz++;
325 325 break;
326 326 case ESCAPE_SKIPCHAR:
327 327 skip = 1;
328 328 break;
329 329 default:
330 330 break;
331 331 }
332 332 }
333 333 return sz;
334 334 }
335 335
336 336 static void
337 337 font_push(char newfont)
338 338 {
339 339
340 340 if (fontqueue.head + fontqueue.size <= ++fontqueue.tail) {
341 341 fontqueue.size += 8;
342 342 fontqueue.head = mandoc_realloc(fontqueue.head,
343 343 fontqueue.size);
344 344 }
345 345 *fontqueue.tail = newfont;
346 346 print_word("");
347 347 printf("\\f");
348 348 putchar(newfont);
349 349 outflags &= ~MMAN_spc;
350 350 }
351 351
352 352 static void
353 353 font_pop(void)
354 354 {
355 355
356 356 if (fontqueue.tail > fontqueue.head)
357 357 fontqueue.tail--;
358 358 outflags &= ~MMAN_spc;
359 359 print_word("");
360 360 printf("\\f");
361 361 putchar(*fontqueue.tail);
362 362 }
363 363
364 364 static void
365 365 print_word(const char *s)
366 366 {
367 367
368 368 if ((MMAN_PP | MMAN_sp | MMAN_br | MMAN_nl) & outflags) {
369 369 /*
370 370 * If we need a newline, print it now and start afresh.
371 371 */
372 372 if (MMAN_PP & outflags) {
373 373 if (MMAN_sp & outflags) {
374 374 if (MMAN_PD & outflags) {
375 375 printf("\n.PD");
376 376 outflags &= ~MMAN_PD;
377 377 }
378 378 } else if ( ! (MMAN_PD & outflags)) {
379 379 printf("\n.PD 0");
380 380 outflags |= MMAN_PD;
381 381 }
382 382 printf("\n.PP\n");
383 383 } else if (MMAN_sp & outflags)
384 384 printf("\n.sp\n");
385 385 else if (MMAN_br & outflags)
386 386 printf("\n.br\n");
387 387 else if (MMAN_nl & outflags)
388 388 putchar('\n');
389 389 outflags &= ~(MMAN_PP|MMAN_sp|MMAN_br|MMAN_nl|MMAN_spc);
390 390 if (1 == TPremain)
391 391 printf(".br\n");
392 392 TPremain = 0;
393 393 } else if (MMAN_spc & outflags) {
394 394 /*
395 395 * If we need a space, only print it if
396 396 * (1) it is forced by `No' or
397 397 * (2) what follows is not terminating punctuation or
398 398 * (3) what follows is longer than one character.
399 399 */
400 400 if (MMAN_spc_force & outflags || '\0' == s[0] ||
401 401 NULL == strchr(".,:;)]?!", s[0]) || '\0' != s[1]) {
402 402 if (MMAN_Bk & outflags &&
403 403 ! (MMAN_Bk_susp & outflags))
404 404 putchar('\\');
405 405 putchar(' ');
406 406 if (TPremain)
407 407 TPremain--;
408 408 }
409 409 }
410 410
411 411 /*
412 412 * Reassign needing space if we're not following opening
413 413 * punctuation.
414 414 */
415 415 if (MMAN_Sm & outflags && ('\0' == s[0] ||
416 416 (('(' != s[0] && '[' != s[0]) || '\0' != s[1])))
417 417 outflags |= MMAN_spc;
418 418 else
419 419 outflags &= ~MMAN_spc;
420 420 outflags &= ~(MMAN_spc_force | MMAN_Bk_susp);
421 421
422 422 for ( ; *s; s++) {
423 423 switch (*s) {
424 424 case ASCII_NBRSP:
425 425 printf("\\ ");
426 426 break;
427 427 case ASCII_HYPH:
428 428 putchar('-');
429 429 break;
430 430 case ASCII_BREAK:
431 431 printf("\\:");
432 432 break;
433 433 case ' ':
434 434 if (MMAN_nbrword & outflags) {
435 435 printf("\\ ");
436 436 break;
437 437 }
438 438 /* FALLTHROUGH */
439 439 default:
440 440 putchar((unsigned char)*s);
441 441 break;
442 442 }
443 443 if (TPremain)
444 444 TPremain--;
445 445 }
446 446 outflags &= ~MMAN_nbrword;
447 447 }
448 448
449 449 static void
450 450 print_line(const char *s, int newflags)
451 451 {
452 452
453 453 outflags |= MMAN_nl;
454 454 print_word(s);
455 455 outflags |= newflags;
456 456 }
457 457
458 458 static void
459 459 print_block(const char *s, int newflags)
460 460 {
461 461
462 462 outflags &= ~MMAN_PP;
463 463 if (MMAN_sp & outflags) {
464 464 outflags &= ~(MMAN_sp | MMAN_br);
465 465 if (MMAN_PD & outflags) {
466 466 print_line(".PD", 0);
467 467 outflags &= ~MMAN_PD;
468 468 }
469 469 } else if (! (MMAN_PD & outflags))
470 470 print_line(".PD 0", MMAN_PD);
471 471 outflags |= MMAN_nl;
472 472 print_word(s);
473 473 outflags |= MMAN_Bk_susp | newflags;
474 474 }
475 475
476 476 static void
477 477 print_offs(const char *v, int keywords)
478 478 {
479 479 char buf[24];
480 480 struct roffsu su;
481 481 const char *end;
482 482 int sz;
483 483
484 484 print_line(".RS", MMAN_Bk_susp);
485 485
486 486 /* Convert v into a number (of characters). */
487 487 if (NULL == v || '\0' == *v || (keywords && !strcmp(v, "left")))
488 488 sz = 0;
489 489 else if (keywords && !strcmp(v, "indent"))
490 490 sz = 6;
491 491 else if (keywords && !strcmp(v, "indent-two"))
492 492 sz = 12;
493 493 else {
494 494 end = a2roffsu(v, &su, SCALE_EN);
495 495 if (end == NULL || *end != '\0')
496 496 sz = man_strlen(v);
497 497 else if (SCALE_EN == su.unit)
498 498 sz = su.scale;
499 499 else {
500 500 /*
501 501 * XXX
502 502 * If we are inside an enclosing list,
503 503 * there is no easy way to add the two
504 504 * indentations because they are provided
505 505 * in terms of different units.
506 506 */
507 507 print_word(v);
508 508 outflags |= MMAN_nl;
509 509 return;
510 510 }
511 511 }
512 512
513 513 /*
514 514 * We are inside an enclosing list.
515 515 * Add the two indentations.
516 516 */
517 517 if (Bl_stack_len)
518 518 sz += Bl_stack[Bl_stack_len - 1];
519 519
520 520 (void)snprintf(buf, sizeof(buf), "%dn", sz);
521 521 print_word(buf);
522 522 outflags |= MMAN_nl;
523 523 }
524 524
525 525 /*
526 526 * Set up the indentation for a list item; used from pre_it().
527 527 */
528 528 static void
529 529 print_width(const struct mdoc_bl *bl, const struct roff_node *child)
530 530 {
531 531 char buf[24];
532 532 struct roffsu su;
533 533 const char *end;
534 534 int numeric, remain, sz, chsz;
535 535
536 536 numeric = 1;
537 537 remain = 0;
538 538
539 539 /* Convert the width into a number (of characters). */
540 540 if (bl->width == NULL)
541 541 sz = (bl->type == LIST_hang) ? 6 : 0;
542 542 else {
543 543 end = a2roffsu(bl->width, &su, SCALE_MAX);
544 544 if (end == NULL || *end != '\0')
545 545 sz = man_strlen(bl->width);
546 546 else if (SCALE_EN == su.unit)
547 547 sz = su.scale;
548 548 else {
549 549 sz = 0;
550 550 numeric = 0;
551 551 }
552 552 }
553 553
554 554 /* XXX Rough estimation, might have multiple parts. */
555 555 if (bl->type == LIST_enum)
556 556 chsz = (bl->count > 8) + 1;
557 557 else if (child != NULL && child->type == ROFFT_TEXT)
558 558 chsz = man_strlen(child->string);
559 559 else
560 560 chsz = 0;
561 561
562 562 /* Maybe we are inside an enclosing list? */
563 563 mid_it();
564 564
565 565 /*
566 566 * Save our own indentation,
567 567 * such that child lists can use it.
568 568 */
569 569 Bl_stack[Bl_stack_len++] = sz + 2;
570 570
571 571 /* Set up the current list. */
572 572 if (chsz > sz && bl->type != LIST_tag)
573 573 print_block(".HP", 0);
574 574 else {
575 575 print_block(".TP", 0);
576 576 remain = sz + 2;
577 577 }
578 578 if (numeric) {
579 579 (void)snprintf(buf, sizeof(buf), "%dn", sz + 2);
580 580 print_word(buf);
581 581 } else
582 582 print_word(bl->width);
583 583 TPremain = remain;
584 584 }
585 585
586 586 static void
587 587 print_count(int *count)
588 588 {
589 589 char buf[24];
590 590
591 591 (void)snprintf(buf, sizeof(buf), "%d.\\&", ++*count);
592 592 print_word(buf);
593 593 }
594 594
595 595 void
596 596 man_man(void *arg, const struct roff_man *man)
597 597 {
598 598
599 599 /*
600 600 * Dump the keep buffer.
601 601 * We're guaranteed by now that this exists (is non-NULL).
602 602 * Flush stdout afterward, just in case.
↓ open down ↓ |
386 lines elided |
↑ open up ↑ |
603 603 */
604 604 fputs(mparse_getkeep(man_mparse(man)), stdout);
605 605 fflush(stdout);
606 606 }
607 607
608 608 void
609 609 man_mdoc(void *arg, const struct roff_man *mdoc)
610 610 {
611 611 struct roff_node *n;
612 612
613 + printf(".\\\" Automatically generated from an mdoc input file."
614 + " Do not edit.\n");
615 + for (n = mdoc->first->child; n != NULL; n = n->next) {
616 + if (n->type != ROFFT_COMMENT)
617 + break;
618 + printf(".\\\"%s\n", n->string);
619 + }
620 +
613 621 printf(".TH \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"\n",
614 622 mdoc->meta.title,
615 623 (mdoc->meta.msec == NULL ? "" : mdoc->meta.msec),
616 624 mdoc->meta.date, mdoc->meta.os, mdoc->meta.vol);
617 625
618 626 /* Disable hyphenation and if nroff, disable justification. */
619 627 printf(".nh\n.if n .ad l");
620 628
621 629 outflags = MMAN_nl | MMAN_Sm;
622 630 if (0 == fontqueue.size) {
623 631 fontqueue.size = 8;
624 632 fontqueue.head = fontqueue.tail = mandoc_malloc(8);
625 633 *fontqueue.tail = 'R';
626 634 }
627 - for (n = mdoc->first->child; n != NULL; n = n->next)
635 + for (; n != NULL; n = n->next)
628 636 print_node(&mdoc->meta, n);
629 637 putchar('\n');
630 638 }
631 639
632 640 static void
633 641 print_node(DECL_ARGS)
634 642 {
635 643 const struct manact *act;
636 644 struct roff_node *sub;
637 645 int cond, do_sub;
638 646
639 647 if (n->flags & NODE_NOPRT)
640 648 return;
641 649
642 650 /*
643 651 * Break the line if we were parsed subsequent the current node.
644 652 * This makes the page structure be more consistent.
645 653 */
646 654 if (MMAN_spc & outflags && NODE_LINE & n->flags)
647 655 outflags |= MMAN_nl;
648 656
649 657 act = NULL;
650 658 cond = 0;
651 659 do_sub = 1;
652 660 n->flags &= ~NODE_ENDED;
653 661
654 662 if (n->type == ROFFT_TEXT) {
655 663 /*
656 664 * Make sure that we don't happen to start with a
657 665 * control character at the start of a line.
658 666 */
659 667 if (MMAN_nl & outflags &&
660 668 ('.' == *n->string || '\'' == *n->string)) {
661 669 print_word("");
662 670 printf("\\&");
663 671 outflags &= ~MMAN_spc;
664 672 }
665 673 if (n->flags & NODE_DELIMC)
666 674 outflags &= ~(MMAN_spc | MMAN_spc_force);
667 675 else if (outflags & MMAN_Sm)
668 676 outflags |= MMAN_spc_force;
669 677 print_word(n->string);
670 678 if (n->flags & NODE_DELIMO)
671 679 outflags &= ~(MMAN_spc | MMAN_spc_force);
672 680 else if (outflags & MMAN_Sm)
673 681 outflags |= MMAN_spc;
674 682 } else if (n->tok < ROFF_MAX) {
675 683 (*roff_manacts[n->tok])(meta, n);
676 684 return;
677 685 } else {
678 686 assert(n->tok >= MDOC_Dd && n->tok < MDOC_MAX);
679 687 /*
680 688 * Conditionally run the pre-node action handler for a
681 689 * node.
682 690 */
683 691 act = manacts + n->tok;
684 692 cond = act->cond == NULL || (*act->cond)(meta, n);
685 693 if (cond && act->pre != NULL &&
686 694 (n->end == ENDBODY_NOT || n->child != NULL))
687 695 do_sub = (*act->pre)(meta, n);
688 696 }
689 697
690 698 /*
691 699 * Conditionally run all child nodes.
692 700 * Note that this iterates over children instead of using
693 701 * recursion. This prevents unnecessary depth in the stack.
694 702 */
695 703 if (do_sub)
696 704 for (sub = n->child; sub; sub = sub->next)
697 705 print_node(meta, sub);
698 706
699 707 /*
700 708 * Lastly, conditionally run the post-node handler.
701 709 */
702 710 if (NODE_ENDED & n->flags)
703 711 return;
704 712
705 713 if (cond && act->post)
706 714 (*act->post)(meta, n);
707 715
708 716 if (ENDBODY_NOT != n->end)
709 717 n->body->flags |= NODE_ENDED;
710 718 }
711 719
712 720 static int
713 721 cond_head(DECL_ARGS)
714 722 {
715 723
716 724 return n->type == ROFFT_HEAD;
717 725 }
718 726
719 727 static int
720 728 cond_body(DECL_ARGS)
721 729 {
722 730
723 731 return n->type == ROFFT_BODY;
724 732 }
725 733
726 734 static int
727 735 pre_enc(DECL_ARGS)
728 736 {
729 737 const char *prefix;
730 738
731 739 prefix = manacts[n->tok].prefix;
732 740 if (NULL == prefix)
733 741 return 1;
734 742 print_word(prefix);
735 743 outflags &= ~MMAN_spc;
736 744 return 1;
737 745 }
738 746
739 747 static void
740 748 post_enc(DECL_ARGS)
741 749 {
742 750 const char *suffix;
743 751
744 752 suffix = manacts[n->tok].suffix;
745 753 if (NULL == suffix)
746 754 return;
747 755 outflags &= ~(MMAN_spc | MMAN_nl);
748 756 print_word(suffix);
749 757 }
750 758
751 759 static int
752 760 pre_ex(DECL_ARGS)
753 761 {
754 762 outflags |= MMAN_br | MMAN_nl;
755 763 return 1;
756 764 }
757 765
758 766 static void
759 767 post_font(DECL_ARGS)
760 768 {
761 769
762 770 font_pop();
763 771 }
764 772
765 773 static void
766 774 post_percent(DECL_ARGS)
767 775 {
768 776
769 777 if (pre_em == manacts[n->tok].pre)
770 778 font_pop();
771 779 if (n->next) {
772 780 print_word(",");
773 781 if (n->prev && n->prev->tok == n->tok &&
774 782 n->next->tok == n->tok)
775 783 print_word("and");
776 784 } else {
777 785 print_word(".");
778 786 outflags |= MMAN_nl;
779 787 }
780 788 }
781 789
782 790 static int
783 791 pre__t(DECL_ARGS)
784 792 {
785 793
786 794 if (n->parent->tok == MDOC_Rs && n->parent->norm->Rs.quote_T) {
787 795 print_word("\\(lq");
788 796 outflags &= ~MMAN_spc;
789 797 } else
790 798 font_push('I');
791 799 return 1;
792 800 }
793 801
794 802 static void
795 803 post__t(DECL_ARGS)
796 804 {
797 805
798 806 if (n->parent->tok == MDOC_Rs && n->parent->norm->Rs.quote_T) {
799 807 outflags &= ~MMAN_spc;
800 808 print_word("\\(rq");
801 809 } else
802 810 font_pop();
803 811 post_percent(meta, n);
804 812 }
805 813
806 814 /*
807 815 * Print before a section header.
808 816 */
809 817 static int
810 818 pre_sect(DECL_ARGS)
811 819 {
812 820
813 821 if (n->type == ROFFT_HEAD) {
814 822 outflags |= MMAN_sp;
815 823 print_block(manacts[n->tok].prefix, 0);
816 824 print_word("");
817 825 putchar('\"');
818 826 outflags &= ~MMAN_spc;
819 827 }
820 828 return 1;
821 829 }
822 830
823 831 /*
824 832 * Print subsequent a section header.
825 833 */
826 834 static void
827 835 post_sect(DECL_ARGS)
828 836 {
829 837
830 838 if (n->type != ROFFT_HEAD)
831 839 return;
832 840 outflags &= ~MMAN_spc;
833 841 print_word("");
834 842 putchar('\"');
835 843 outflags |= MMAN_nl;
836 844 if (MDOC_Sh == n->tok && SEC_AUTHORS == n->sec)
837 845 outflags &= ~(MMAN_An_split | MMAN_An_nosplit);
838 846 }
839 847
840 848 /* See mdoc_term.c, synopsis_pre() for comments. */
841 849 static void
842 850 pre_syn(const struct roff_node *n)
843 851 {
844 852
845 853 if (NULL == n->prev || ! (NODE_SYNPRETTY & n->flags))
846 854 return;
847 855
848 856 if (n->prev->tok == n->tok &&
849 857 MDOC_Ft != n->tok &&
850 858 MDOC_Fo != n->tok &&
851 859 MDOC_Fn != n->tok) {
852 860 outflags |= MMAN_br;
853 861 return;
854 862 }
855 863
856 864 switch (n->prev->tok) {
857 865 case MDOC_Fd:
858 866 case MDOC_Fn:
859 867 case MDOC_Fo:
860 868 case MDOC_In:
861 869 case MDOC_Vt:
862 870 outflags |= MMAN_sp;
863 871 break;
864 872 case MDOC_Ft:
865 873 if (MDOC_Fn != n->tok && MDOC_Fo != n->tok) {
866 874 outflags |= MMAN_sp;
867 875 break;
868 876 }
869 877 /* FALLTHROUGH */
870 878 default:
871 879 outflags |= MMAN_br;
872 880 break;
873 881 }
874 882 }
875 883
876 884 static int
877 885 pre_an(DECL_ARGS)
878 886 {
879 887
880 888 switch (n->norm->An.auth) {
881 889 case AUTH_split:
882 890 outflags &= ~MMAN_An_nosplit;
883 891 outflags |= MMAN_An_split;
884 892 return 0;
885 893 case AUTH_nosplit:
886 894 outflags &= ~MMAN_An_split;
887 895 outflags |= MMAN_An_nosplit;
888 896 return 0;
889 897 default:
890 898 if (MMAN_An_split & outflags)
891 899 outflags |= MMAN_br;
892 900 else if (SEC_AUTHORS == n->sec &&
893 901 ! (MMAN_An_nosplit & outflags))
894 902 outflags |= MMAN_An_split;
895 903 return 1;
896 904 }
897 905 }
898 906
899 907 static int
900 908 pre_ap(DECL_ARGS)
901 909 {
902 910
903 911 outflags &= ~MMAN_spc;
904 912 print_word("'");
905 913 outflags &= ~MMAN_spc;
906 914 return 0;
907 915 }
908 916
909 917 static int
910 918 pre_aq(DECL_ARGS)
911 919 {
912 920
913 921 print_word(n->child != NULL && n->child->next == NULL &&
914 922 n->child->tok == MDOC_Mt ? "<" : "\\(la");
915 923 outflags &= ~MMAN_spc;
916 924 return 1;
917 925 }
918 926
919 927 static void
920 928 post_aq(DECL_ARGS)
921 929 {
922 930
923 931 outflags &= ~(MMAN_spc | MMAN_nl);
924 932 print_word(n->child != NULL && n->child->next == NULL &&
925 933 n->child->tok == MDOC_Mt ? ">" : "\\(ra");
926 934 }
927 935
928 936 static int
929 937 pre_bd(DECL_ARGS)
930 938 {
931 939
932 940 outflags &= ~(MMAN_PP | MMAN_sp | MMAN_br);
933 941
934 942 if (DISP_unfilled == n->norm->Bd.type ||
935 943 DISP_literal == n->norm->Bd.type)
936 944 print_line(".nf", 0);
937 945 if (0 == n->norm->Bd.comp && NULL != n->parent->prev)
938 946 outflags |= MMAN_sp;
939 947 print_offs(n->norm->Bd.offs, 1);
940 948 return 1;
941 949 }
942 950
943 951 static void
944 952 post_bd(DECL_ARGS)
945 953 {
946 954
947 955 /* Close out this display. */
948 956 print_line(".RE", MMAN_nl);
949 957 if (DISP_unfilled == n->norm->Bd.type ||
950 958 DISP_literal == n->norm->Bd.type)
951 959 print_line(".fi", MMAN_nl);
952 960
953 961 /* Maybe we are inside an enclosing list? */
954 962 if (NULL != n->parent->next)
955 963 mid_it();
956 964 }
957 965
958 966 static int
959 967 pre_bf(DECL_ARGS)
960 968 {
961 969
962 970 switch (n->type) {
963 971 case ROFFT_BLOCK:
964 972 return 1;
965 973 case ROFFT_BODY:
966 974 break;
967 975 default:
968 976 return 0;
969 977 }
970 978 switch (n->norm->Bf.font) {
971 979 case FONT_Em:
972 980 font_push('I');
973 981 break;
974 982 case FONT_Sy:
975 983 font_push('B');
976 984 break;
977 985 default:
978 986 font_push('R');
979 987 break;
980 988 }
981 989 return 1;
982 990 }
983 991
984 992 static void
985 993 post_bf(DECL_ARGS)
986 994 {
987 995
988 996 if (n->type == ROFFT_BODY)
989 997 font_pop();
990 998 }
991 999
992 1000 static int
993 1001 pre_bk(DECL_ARGS)
994 1002 {
995 1003 switch (n->type) {
996 1004 case ROFFT_BLOCK:
997 1005 return 1;
998 1006 case ROFFT_BODY:
999 1007 case ROFFT_ELEM:
1000 1008 outflags |= MMAN_Bk;
1001 1009 return 1;
1002 1010 default:
1003 1011 return 0;
1004 1012 }
1005 1013 }
1006 1014
1007 1015 static void
1008 1016 post_bk(DECL_ARGS)
1009 1017 {
1010 1018 switch (n->type) {
1011 1019 case ROFFT_ELEM:
1012 1020 while ((n = n->parent) != NULL)
1013 1021 if (n->tok == MDOC_Bk)
1014 1022 return;
1015 1023 /* FALLTHROUGH */
1016 1024 case ROFFT_BODY:
1017 1025 outflags &= ~MMAN_Bk;
1018 1026 break;
1019 1027 default:
1020 1028 break;
1021 1029 }
1022 1030 }
1023 1031
1024 1032 static int
1025 1033 pre_bl(DECL_ARGS)
1026 1034 {
1027 1035 size_t icol;
1028 1036
1029 1037 /*
1030 1038 * print_offs() will increase the -offset to account for
1031 1039 * a possible enclosing .It, but any enclosed .It blocks
1032 1040 * just nest and do not add up their indentation.
1033 1041 */
1034 1042 if (n->norm->Bl.offs) {
1035 1043 print_offs(n->norm->Bl.offs, 0);
1036 1044 Bl_stack[Bl_stack_len++] = 0;
1037 1045 }
1038 1046
1039 1047 switch (n->norm->Bl.type) {
1040 1048 case LIST_enum:
1041 1049 n->norm->Bl.count = 0;
1042 1050 return 1;
1043 1051 case LIST_column:
1044 1052 break;
1045 1053 default:
1046 1054 return 1;
1047 1055 }
1048 1056
1049 1057 if (n->child != NULL) {
1050 1058 print_line(".TS", MMAN_nl);
1051 1059 for (icol = 0; icol < n->norm->Bl.ncols; icol++)
1052 1060 print_word("l");
1053 1061 print_word(".");
1054 1062 }
1055 1063 outflags |= MMAN_nl;
1056 1064 return 1;
1057 1065 }
1058 1066
1059 1067 static void
1060 1068 post_bl(DECL_ARGS)
1061 1069 {
1062 1070
1063 1071 switch (n->norm->Bl.type) {
1064 1072 case LIST_column:
1065 1073 if (n->child != NULL)
1066 1074 print_line(".TE", 0);
1067 1075 break;
1068 1076 case LIST_enum:
1069 1077 n->norm->Bl.count = 0;
1070 1078 break;
1071 1079 default:
1072 1080 break;
1073 1081 }
1074 1082
1075 1083 if (n->norm->Bl.offs) {
1076 1084 print_line(".RE", MMAN_nl);
1077 1085 assert(Bl_stack_len);
1078 1086 Bl_stack_len--;
1079 1087 assert(0 == Bl_stack[Bl_stack_len]);
1080 1088 } else {
1081 1089 outflags |= MMAN_PP | MMAN_nl;
1082 1090 outflags &= ~(MMAN_sp | MMAN_br);
1083 1091 }
1084 1092
1085 1093 /* Maybe we are inside an enclosing list? */
1086 1094 if (NULL != n->parent->next)
1087 1095 mid_it();
1088 1096
1089 1097 }
1090 1098
1091 1099 static void
1092 1100 pre_br(DECL_ARGS)
1093 1101 {
1094 1102 outflags |= MMAN_br;
1095 1103 }
1096 1104
1097 1105 static int
1098 1106 pre_dl(DECL_ARGS)
1099 1107 {
1100 1108
1101 1109 print_offs("6n", 0);
1102 1110 return 1;
1103 1111 }
1104 1112
1105 1113 static void
1106 1114 post_dl(DECL_ARGS)
1107 1115 {
1108 1116
1109 1117 print_line(".RE", MMAN_nl);
1110 1118
1111 1119 /* Maybe we are inside an enclosing list? */
1112 1120 if (NULL != n->parent->next)
1113 1121 mid_it();
1114 1122 }
1115 1123
1116 1124 static int
1117 1125 pre_em(DECL_ARGS)
1118 1126 {
1119 1127
1120 1128 font_push('I');
1121 1129 return 1;
1122 1130 }
1123 1131
1124 1132 static int
1125 1133 pre_en(DECL_ARGS)
1126 1134 {
1127 1135
1128 1136 if (NULL == n->norm->Es ||
1129 1137 NULL == n->norm->Es->child)
1130 1138 return 1;
1131 1139
1132 1140 print_word(n->norm->Es->child->string);
1133 1141 outflags &= ~MMAN_spc;
1134 1142 return 1;
1135 1143 }
1136 1144
1137 1145 static void
1138 1146 post_en(DECL_ARGS)
1139 1147 {
1140 1148
1141 1149 if (NULL == n->norm->Es ||
1142 1150 NULL == n->norm->Es->child ||
1143 1151 NULL == n->norm->Es->child->next)
1144 1152 return;
1145 1153
1146 1154 outflags &= ~MMAN_spc;
1147 1155 print_word(n->norm->Es->child->next->string);
1148 1156 return;
1149 1157 }
1150 1158
1151 1159 static int
1152 1160 pre_eo(DECL_ARGS)
1153 1161 {
1154 1162
1155 1163 if (n->end == ENDBODY_NOT &&
1156 1164 n->parent->head->child == NULL &&
1157 1165 n->child != NULL &&
1158 1166 n->child->end != ENDBODY_NOT)
1159 1167 print_word("\\&");
1160 1168 else if (n->end != ENDBODY_NOT ? n->child != NULL :
1161 1169 n->parent->head->child != NULL && (n->child != NULL ||
1162 1170 (n->parent->tail != NULL && n->parent->tail->child != NULL)))
1163 1171 outflags &= ~(MMAN_spc | MMAN_nl);
1164 1172 return 1;
1165 1173 }
1166 1174
1167 1175 static void
1168 1176 post_eo(DECL_ARGS)
1169 1177 {
1170 1178 int body, tail;
1171 1179
1172 1180 if (n->end != ENDBODY_NOT) {
1173 1181 outflags |= MMAN_spc;
1174 1182 return;
1175 1183 }
1176 1184
1177 1185 body = n->child != NULL || n->parent->head->child != NULL;
1178 1186 tail = n->parent->tail != NULL && n->parent->tail->child != NULL;
1179 1187
1180 1188 if (body && tail)
1181 1189 outflags &= ~MMAN_spc;
1182 1190 else if ( ! (body || tail))
1183 1191 print_word("\\&");
1184 1192 else if ( ! tail)
1185 1193 outflags |= MMAN_spc;
1186 1194 }
1187 1195
1188 1196 static int
1189 1197 pre_fa(DECL_ARGS)
1190 1198 {
1191 1199 int am_Fa;
1192 1200
1193 1201 am_Fa = MDOC_Fa == n->tok;
1194 1202
1195 1203 if (am_Fa)
1196 1204 n = n->child;
1197 1205
1198 1206 while (NULL != n) {
1199 1207 font_push('I');
1200 1208 if (am_Fa || NODE_SYNPRETTY & n->flags)
1201 1209 outflags |= MMAN_nbrword;
1202 1210 print_node(meta, n);
1203 1211 font_pop();
1204 1212 if (NULL != (n = n->next))
1205 1213 print_word(",");
1206 1214 }
1207 1215 return 0;
1208 1216 }
1209 1217
1210 1218 static void
1211 1219 post_fa(DECL_ARGS)
1212 1220 {
1213 1221
1214 1222 if (NULL != n->next && MDOC_Fa == n->next->tok)
1215 1223 print_word(",");
1216 1224 }
1217 1225
1218 1226 static int
1219 1227 pre_fd(DECL_ARGS)
1220 1228 {
1221 1229
1222 1230 pre_syn(n);
1223 1231 font_push('B');
1224 1232 return 1;
1225 1233 }
1226 1234
1227 1235 static void
1228 1236 post_fd(DECL_ARGS)
1229 1237 {
1230 1238
1231 1239 font_pop();
1232 1240 outflags |= MMAN_br;
1233 1241 }
1234 1242
1235 1243 static int
1236 1244 pre_fl(DECL_ARGS)
1237 1245 {
1238 1246
1239 1247 font_push('B');
1240 1248 print_word("\\-");
1241 1249 if (n->child != NULL)
1242 1250 outflags &= ~MMAN_spc;
1243 1251 return 1;
1244 1252 }
1245 1253
1246 1254 static void
1247 1255 post_fl(DECL_ARGS)
1248 1256 {
1249 1257
1250 1258 font_pop();
1251 1259 if (!(n->child != NULL ||
1252 1260 n->next == NULL ||
1253 1261 n->next->type == ROFFT_TEXT ||
1254 1262 n->next->flags & NODE_LINE))
1255 1263 outflags &= ~MMAN_spc;
1256 1264 }
1257 1265
1258 1266 static int
1259 1267 pre_fn(DECL_ARGS)
1260 1268 {
1261 1269
1262 1270 pre_syn(n);
1263 1271
1264 1272 n = n->child;
1265 1273 if (NULL == n)
1266 1274 return 0;
1267 1275
1268 1276 if (NODE_SYNPRETTY & n->flags)
1269 1277 print_block(".HP 4n", MMAN_nl);
1270 1278
1271 1279 font_push('B');
1272 1280 print_node(meta, n);
1273 1281 font_pop();
1274 1282 outflags &= ~MMAN_spc;
1275 1283 print_word("(");
1276 1284 outflags &= ~MMAN_spc;
1277 1285
1278 1286 n = n->next;
1279 1287 if (NULL != n)
1280 1288 pre_fa(meta, n);
1281 1289 return 0;
1282 1290 }
1283 1291
1284 1292 static void
1285 1293 post_fn(DECL_ARGS)
1286 1294 {
1287 1295
1288 1296 print_word(")");
1289 1297 if (NODE_SYNPRETTY & n->flags) {
1290 1298 print_word(";");
1291 1299 outflags |= MMAN_PP;
1292 1300 }
1293 1301 }
1294 1302
1295 1303 static int
1296 1304 pre_fo(DECL_ARGS)
1297 1305 {
1298 1306
1299 1307 switch (n->type) {
1300 1308 case ROFFT_BLOCK:
1301 1309 pre_syn(n);
1302 1310 break;
1303 1311 case ROFFT_HEAD:
1304 1312 if (n->child == NULL)
1305 1313 return 0;
1306 1314 if (NODE_SYNPRETTY & n->flags)
1307 1315 print_block(".HP 4n", MMAN_nl);
1308 1316 font_push('B');
1309 1317 break;
1310 1318 case ROFFT_BODY:
1311 1319 outflags &= ~(MMAN_spc | MMAN_nl);
1312 1320 print_word("(");
1313 1321 outflags &= ~MMAN_spc;
1314 1322 break;
1315 1323 default:
1316 1324 break;
1317 1325 }
1318 1326 return 1;
1319 1327 }
1320 1328
1321 1329 static void
1322 1330 post_fo(DECL_ARGS)
1323 1331 {
1324 1332
1325 1333 switch (n->type) {
1326 1334 case ROFFT_HEAD:
1327 1335 if (n->child != NULL)
1328 1336 font_pop();
1329 1337 break;
1330 1338 case ROFFT_BODY:
1331 1339 post_fn(meta, n);
1332 1340 break;
1333 1341 default:
1334 1342 break;
1335 1343 }
1336 1344 }
1337 1345
1338 1346 static int
1339 1347 pre_Ft(DECL_ARGS)
1340 1348 {
1341 1349
1342 1350 pre_syn(n);
1343 1351 font_push('I');
1344 1352 return 1;
1345 1353 }
1346 1354
1347 1355 static void
1348 1356 pre_ft(DECL_ARGS)
1349 1357 {
1350 1358 print_line(".ft", 0);
1351 1359 print_word(n->child->string);
1352 1360 outflags |= MMAN_nl;
1353 1361 }
1354 1362
1355 1363 static int
1356 1364 pre_in(DECL_ARGS)
1357 1365 {
1358 1366
1359 1367 if (NODE_SYNPRETTY & n->flags) {
1360 1368 pre_syn(n);
1361 1369 font_push('B');
1362 1370 print_word("#include <");
1363 1371 outflags &= ~MMAN_spc;
1364 1372 } else {
1365 1373 print_word("<");
1366 1374 outflags &= ~MMAN_spc;
1367 1375 font_push('I');
1368 1376 }
1369 1377 return 1;
1370 1378 }
1371 1379
1372 1380 static void
1373 1381 post_in(DECL_ARGS)
1374 1382 {
1375 1383
1376 1384 if (NODE_SYNPRETTY & n->flags) {
1377 1385 outflags &= ~MMAN_spc;
1378 1386 print_word(">");
1379 1387 font_pop();
1380 1388 outflags |= MMAN_br;
1381 1389 } else {
1382 1390 font_pop();
1383 1391 outflags &= ~MMAN_spc;
1384 1392 print_word(">");
1385 1393 }
1386 1394 }
1387 1395
1388 1396 static int
1389 1397 pre_it(DECL_ARGS)
1390 1398 {
1391 1399 const struct roff_node *bln;
1392 1400
1393 1401 switch (n->type) {
1394 1402 case ROFFT_HEAD:
1395 1403 outflags |= MMAN_PP | MMAN_nl;
1396 1404 bln = n->parent->parent;
1397 1405 if (0 == bln->norm->Bl.comp ||
1398 1406 (NULL == n->parent->prev &&
1399 1407 NULL == bln->parent->prev))
1400 1408 outflags |= MMAN_sp;
↓ open down ↓ |
763 lines elided |
↑ open up ↑ |
1401 1409 outflags &= ~MMAN_br;
1402 1410 switch (bln->norm->Bl.type) {
1403 1411 case LIST_item:
1404 1412 return 0;
1405 1413 case LIST_inset:
1406 1414 case LIST_diag:
1407 1415 case LIST_ohang:
1408 1416 if (bln->norm->Bl.type == LIST_diag)
1409 1417 print_line(".B \"", 0);
1410 1418 else
1411 - print_line(".R \"", 0);
1419 + print_line(".BR \\& \"", 0);
1412 1420 outflags &= ~MMAN_spc;
1413 1421 return 1;
1414 1422 case LIST_bullet:
1415 1423 case LIST_dash:
1416 1424 case LIST_hyphen:
1417 1425 print_width(&bln->norm->Bl, NULL);
1418 1426 TPremain = 0;
1419 1427 outflags |= MMAN_nl;
1420 1428 font_push('B');
1421 1429 if (LIST_bullet == bln->norm->Bl.type)
1422 1430 print_word("\\(bu");
1423 1431 else
1424 1432 print_word("-");
1425 1433 font_pop();
1426 1434 outflags |= MMAN_nl;
1427 1435 return 0;
1428 1436 case LIST_enum:
1429 1437 print_width(&bln->norm->Bl, NULL);
1430 1438 TPremain = 0;
1431 1439 outflags |= MMAN_nl;
1432 1440 print_count(&bln->norm->Bl.count);
1433 1441 outflags |= MMAN_nl;
1434 1442 return 0;
1435 1443 case LIST_hang:
1436 1444 print_width(&bln->norm->Bl, n->child);
1437 1445 TPremain = 0;
1438 1446 outflags |= MMAN_nl;
1439 1447 return 1;
1440 1448 case LIST_tag:
1441 1449 print_width(&bln->norm->Bl, n->child);
1442 1450 putchar('\n');
1443 1451 outflags &= ~MMAN_spc;
1444 1452 return 1;
1445 1453 default:
1446 1454 return 1;
1447 1455 }
1448 1456 default:
1449 1457 break;
1450 1458 }
1451 1459 return 1;
1452 1460 }
1453 1461
1454 1462 /*
1455 1463 * This function is called after closing out an indented block.
1456 1464 * If we are inside an enclosing list, restore its indentation.
1457 1465 */
1458 1466 static void
1459 1467 mid_it(void)
1460 1468 {
1461 1469 char buf[24];
1462 1470
1463 1471 /* Nothing to do outside a list. */
1464 1472 if (0 == Bl_stack_len || 0 == Bl_stack[Bl_stack_len - 1])
1465 1473 return;
1466 1474
1467 1475 /* The indentation has already been set up. */
1468 1476 if (Bl_stack_post[Bl_stack_len - 1])
1469 1477 return;
1470 1478
1471 1479 /* Restore the indentation of the enclosing list. */
1472 1480 print_line(".RS", MMAN_Bk_susp);
1473 1481 (void)snprintf(buf, sizeof(buf), "%dn",
1474 1482 Bl_stack[Bl_stack_len - 1]);
1475 1483 print_word(buf);
1476 1484
1477 1485 /* Remeber to close out this .RS block later. */
1478 1486 Bl_stack_post[Bl_stack_len - 1] = 1;
1479 1487 }
1480 1488
1481 1489 static void
1482 1490 post_it(DECL_ARGS)
1483 1491 {
1484 1492 const struct roff_node *bln;
1485 1493
1486 1494 bln = n->parent->parent;
1487 1495
1488 1496 switch (n->type) {
1489 1497 case ROFFT_HEAD:
1490 1498 switch (bln->norm->Bl.type) {
1491 1499 case LIST_diag:
1492 1500 outflags &= ~MMAN_spc;
1493 1501 print_word("\\ ");
1494 1502 break;
1495 1503 case LIST_ohang:
1496 1504 outflags |= MMAN_br;
1497 1505 break;
1498 1506 default:
1499 1507 break;
1500 1508 }
1501 1509 break;
1502 1510 case ROFFT_BODY:
1503 1511 switch (bln->norm->Bl.type) {
1504 1512 case LIST_bullet:
1505 1513 case LIST_dash:
1506 1514 case LIST_hyphen:
1507 1515 case LIST_enum:
1508 1516 case LIST_hang:
1509 1517 case LIST_tag:
1510 1518 assert(Bl_stack_len);
1511 1519 Bl_stack[--Bl_stack_len] = 0;
1512 1520
1513 1521 /*
1514 1522 * Our indentation had to be restored
1515 1523 * after a child display or child list.
1516 1524 * Close out that indentation block now.
1517 1525 */
1518 1526 if (Bl_stack_post[Bl_stack_len]) {
1519 1527 print_line(".RE", MMAN_nl);
1520 1528 Bl_stack_post[Bl_stack_len] = 0;
1521 1529 }
1522 1530 break;
1523 1531 case LIST_column:
1524 1532 if (NULL != n->next) {
1525 1533 putchar('\t');
1526 1534 outflags &= ~MMAN_spc;
1527 1535 }
1528 1536 break;
1529 1537 default:
1530 1538 break;
1531 1539 }
1532 1540 break;
1533 1541 default:
1534 1542 break;
1535 1543 }
1536 1544 }
1537 1545
1538 1546 static void
1539 1547 post_lb(DECL_ARGS)
↓ open down ↓ |
118 lines elided |
↑ open up ↑ |
1540 1548 {
1541 1549
1542 1550 if (SEC_LIBRARY == n->sec)
1543 1551 outflags |= MMAN_br;
1544 1552 }
1545 1553
1546 1554 static int
1547 1555 pre_lk(DECL_ARGS)
1548 1556 {
1549 1557 const struct roff_node *link, *descr, *punct;
1550 - int display;
1551 1558
1552 1559 if ((link = n->child) == NULL)
1553 1560 return 0;
1554 1561
1555 1562 /* Find beginning of trailing punctuation. */
1556 1563 punct = n->last;
1557 1564 while (punct != link && punct->flags & NODE_DELIMC)
1558 1565 punct = punct->prev;
1559 1566 punct = punct->next;
1560 1567
1561 1568 /* Link text. */
1562 1569 if ((descr = link->next) != NULL && descr != punct) {
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
1563 1570 font_push('I');
1564 1571 while (descr != punct) {
1565 1572 print_word(descr->string);
1566 1573 descr = descr->next;
1567 1574 }
1568 1575 font_pop();
1569 1576 print_word(":");
1570 1577 }
1571 1578
1572 1579 /* Link target. */
1573 - display = man_strlen(link->string) >= 26;
1574 - if (display) {
1575 - print_line(".RS", MMAN_Bk_susp);
1576 - print_word("6n");
1577 - outflags |= MMAN_nl;
1578 - }
1579 1580 font_push('B');
1580 1581 print_word(link->string);
1581 1582 font_pop();
1582 1583
1583 1584 /* Trailing punctuation. */
1584 1585 while (punct != NULL) {
1585 1586 print_word(punct->string);
1586 1587 punct = punct->next;
1587 1588 }
1588 - if (display)
1589 - print_line(".RE", MMAN_nl);
1590 1589 return 0;
1591 1590 }
1592 1591
1593 1592 static void
1594 1593 pre_onearg(DECL_ARGS)
1595 1594 {
1596 1595 outflags |= MMAN_nl;
1597 1596 print_word(".");
1598 1597 outflags &= ~MMAN_spc;
1599 1598 print_word(roff_name[n->tok]);
1600 1599 if (n->child != NULL)
1601 1600 print_word(n->child->string);
1602 1601 outflags |= MMAN_nl;
1603 1602 if (n->tok == ROFF_ce)
1604 1603 for (n = n->child->next; n != NULL; n = n->next)
1605 1604 print_node(meta, n);
1606 1605 }
1607 1606
1608 1607 static int
1609 1608 pre_li(DECL_ARGS)
1610 1609 {
1611 1610
1612 1611 font_push('R');
1613 1612 return 1;
1614 1613 }
1615 1614
1616 1615 static int
1617 1616 pre_nm(DECL_ARGS)
1618 1617 {
1619 1618 char *name;
1620 1619
1621 1620 if (n->type == ROFFT_BLOCK) {
1622 1621 outflags |= MMAN_Bk;
1623 1622 pre_syn(n);
1624 1623 }
1625 1624 if (n->type != ROFFT_ELEM && n->type != ROFFT_HEAD)
1626 1625 return 1;
1627 1626 name = n->child == NULL ? NULL : n->child->string;
1628 1627 if (NULL == name)
1629 1628 return 0;
1630 1629 if (n->type == ROFFT_HEAD) {
1631 1630 if (NULL == n->parent->prev)
1632 1631 outflags |= MMAN_sp;
1633 1632 print_block(".HP", 0);
1634 1633 printf(" %dn", man_strlen(name) + 1);
1635 1634 outflags |= MMAN_nl;
1636 1635 }
1637 1636 font_push('B');
1638 1637 return 1;
1639 1638 }
1640 1639
1641 1640 static void
1642 1641 post_nm(DECL_ARGS)
1643 1642 {
1644 1643
1645 1644 switch (n->type) {
1646 1645 case ROFFT_BLOCK:
1647 1646 outflags &= ~MMAN_Bk;
1648 1647 break;
1649 1648 case ROFFT_HEAD:
1650 1649 case ROFFT_ELEM:
1651 1650 if (n->child != NULL && n->child->string != NULL)
1652 1651 font_pop();
1653 1652 break;
1654 1653 default:
1655 1654 break;
1656 1655 }
1657 1656 }
1658 1657
1659 1658 static int
1660 1659 pre_no(DECL_ARGS)
1661 1660 {
1662 1661
1663 1662 outflags |= MMAN_spc_force;
1664 1663 return 1;
1665 1664 }
1666 1665
1667 1666 static int
1668 1667 pre_ns(DECL_ARGS)
1669 1668 {
1670 1669
1671 1670 outflags &= ~MMAN_spc;
1672 1671 return 0;
1673 1672 }
1674 1673
1675 1674 static void
1676 1675 post_pf(DECL_ARGS)
1677 1676 {
1678 1677
1679 1678 if ( ! (n->next == NULL || n->next->flags & NODE_LINE))
1680 1679 outflags &= ~MMAN_spc;
1681 1680 }
1682 1681
1683 1682 static int
1684 1683 pre_pp(DECL_ARGS)
1685 1684 {
1686 1685
1687 1686 if (MDOC_It != n->parent->tok)
1688 1687 outflags |= MMAN_PP;
1689 1688 outflags |= MMAN_sp | MMAN_nl;
1690 1689 outflags &= ~MMAN_br;
1691 1690 return 0;
1692 1691 }
1693 1692
1694 1693 static int
1695 1694 pre_rs(DECL_ARGS)
1696 1695 {
1697 1696
1698 1697 if (SEC_SEE_ALSO == n->sec) {
1699 1698 outflags |= MMAN_PP | MMAN_sp | MMAN_nl;
1700 1699 outflags &= ~MMAN_br;
1701 1700 }
1702 1701 return 1;
1703 1702 }
1704 1703
1705 1704 static int
1706 1705 pre_skip(DECL_ARGS)
1707 1706 {
1708 1707
1709 1708 return 0;
1710 1709 }
1711 1710
1712 1711 static int
1713 1712 pre_sm(DECL_ARGS)
1714 1713 {
1715 1714
1716 1715 if (NULL == n->child)
1717 1716 outflags ^= MMAN_Sm;
1718 1717 else if (0 == strcmp("on", n->child->string))
1719 1718 outflags |= MMAN_Sm;
1720 1719 else
1721 1720 outflags &= ~MMAN_Sm;
1722 1721
1723 1722 if (MMAN_Sm & outflags)
1724 1723 outflags |= MMAN_spc;
1725 1724
1726 1725 return 0;
1727 1726 }
1728 1727
1729 1728 static void
1730 1729 pre_sp(DECL_ARGS)
1731 1730 {
1732 1731 if (outflags & MMAN_PP) {
1733 1732 outflags &= ~MMAN_PP;
1734 1733 print_line(".PP", 0);
1735 1734 } else {
1736 1735 print_line(".sp", 0);
1737 1736 if (n->child != NULL)
1738 1737 print_word(n->child->string);
1739 1738 }
1740 1739 outflags |= MMAN_nl;
1741 1740 }
1742 1741
1743 1742 static int
1744 1743 pre_sy(DECL_ARGS)
1745 1744 {
1746 1745
1747 1746 font_push('B');
1748 1747 return 1;
1749 1748 }
1750 1749
1751 1750 static void
1752 1751 pre_ta(DECL_ARGS)
1753 1752 {
1754 1753 print_line(".ta", 0);
1755 1754 for (n = n->child; n != NULL; n = n->next)
1756 1755 print_word(n->string);
1757 1756 outflags |= MMAN_nl;
1758 1757 }
1759 1758
1760 1759 static int
1761 1760 pre_vt(DECL_ARGS)
1762 1761 {
1763 1762
1764 1763 if (NODE_SYNPRETTY & n->flags) {
1765 1764 switch (n->type) {
1766 1765 case ROFFT_BLOCK:
1767 1766 pre_syn(n);
1768 1767 return 1;
1769 1768 case ROFFT_BODY:
1770 1769 break;
1771 1770 default:
1772 1771 return 0;
1773 1772 }
1774 1773 }
1775 1774 font_push('I');
1776 1775 return 1;
1777 1776 }
1778 1777
1779 1778 static void
1780 1779 post_vt(DECL_ARGS)
1781 1780 {
1782 1781
1783 1782 if (n->flags & NODE_SYNPRETTY && n->type != ROFFT_BODY)
1784 1783 return;
1785 1784 font_pop();
1786 1785 }
1787 1786
1788 1787 static int
1789 1788 pre_xr(DECL_ARGS)
1790 1789 {
1791 1790
1792 1791 n = n->child;
1793 1792 if (NULL == n)
1794 1793 return 0;
1795 1794 print_node(meta, n);
1796 1795 n = n->next;
1797 1796 if (NULL == n)
1798 1797 return 0;
1799 1798 outflags &= ~MMAN_spc;
1800 1799 print_word("(");
1801 1800 print_node(meta, n);
1802 1801 print_word(")");
1803 1802 return 0;
1804 1803 }
↓ open down ↓ |
205 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX