Annotation of mandoc/mdoc_term.c, Revision 1.73
1.73 ! kristaps 1: /* $Id: mdoc_term.c,v 1.72 2009/09/20 11:05:22 kristaps Exp $ */
1.1 kristaps 2: /*
1.7 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.6 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.6 kristaps 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 16: */
17: #include <sys/types.h>
18:
19: #include <assert.h>
20: #include <ctype.h>
21: #include <err.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
25:
26: #include "term.h"
27: #include "mdoc.h"
28:
1.71 kristaps 29: /* FIXME: check HANG lists: they seem to be broken... :
30: * .Bl -hang -width Ds
31: * .It a
32: * b
33: * .It Fl f Ns Ar option...
34: * Override default compiler behaviour. See
35: * .Sx Compiler Options
36: * for details.
37: * Override default compiler behaviour. See
38: * .Sx Compiler Options
39: * for details.
40: * Override default compiler behaviour. See
41: * .Sx Compiler Options
42: * for details.
43: * Override default compiler behaviour. See
44: * .Sx Compiler Options
45: * for details.
46: * .
47: * .It a sasd fasd as afsd sfad sfds sadfs sd sfd ssfad asfd
48: * Override default compiler behaviour. See
49: * .Sx Compiler Options
50: * for details.
51: * Override default compiler behaviour. See
52: * .Sx Compiler Options
53: * for details.
54: * Override default compiler behaviour. See
55: * .Sx Compiler Options
56: * for details.
57: * Override default compiler behaviour. See
58: * .Sx Compiler Options
59: * for details.
60: * .El
61: *
62: */
63:
1.66 kristaps 64: #define INDENT 5
65: #define HALFINDENT 3
66:
1.1 kristaps 67: struct termpair {
68: struct termpair *ppair;
1.31 kristaps 69: int flag;
70: int count;
1.1 kristaps 71: };
72:
1.31 kristaps 73: #define DECL_ARGS struct termp *p, \
74: struct termpair *pair, \
75: const struct mdoc_meta *meta, \
76: const struct mdoc_node *node
1.1 kristaps 77:
78: struct termact {
79: int (*pre)(DECL_ARGS);
80: void (*post)(DECL_ARGS);
81: };
82:
1.31 kristaps 83: static void termp____post(DECL_ARGS);
1.61 kristaps 84: static void termp_an_post(DECL_ARGS);
1.31 kristaps 85: static void termp_aq_post(DECL_ARGS);
86: static void termp_bd_post(DECL_ARGS);
87: static void termp_bl_post(DECL_ARGS);
88: static void termp_bq_post(DECL_ARGS);
89: static void termp_brq_post(DECL_ARGS);
90: static void termp_bx_post(DECL_ARGS);
91: static void termp_d1_post(DECL_ARGS);
92: static void termp_dq_post(DECL_ARGS);
93: static void termp_fd_post(DECL_ARGS);
94: static void termp_fn_post(DECL_ARGS);
95: static void termp_fo_post(DECL_ARGS);
96: static void termp_ft_post(DECL_ARGS);
97: static void termp_in_post(DECL_ARGS);
98: static void termp_it_post(DECL_ARGS);
99: static void termp_lb_post(DECL_ARGS);
100: static void termp_op_post(DECL_ARGS);
101: static void termp_pf_post(DECL_ARGS);
102: static void termp_pq_post(DECL_ARGS);
103: static void termp_qq_post(DECL_ARGS);
104: static void termp_sh_post(DECL_ARGS);
105: static void termp_sq_post(DECL_ARGS);
106: static void termp_ss_post(DECL_ARGS);
107: static void termp_vt_post(DECL_ARGS);
108:
1.61 kristaps 109: static int termp_an_pre(DECL_ARGS);
1.31 kristaps 110: static int termp_ap_pre(DECL_ARGS);
111: static int termp_aq_pre(DECL_ARGS);
112: static int termp_bd_pre(DECL_ARGS);
113: static int termp_bf_pre(DECL_ARGS);
1.69 kristaps 114: static int termp_bold_pre(DECL_ARGS);
1.31 kristaps 115: static int termp_bq_pre(DECL_ARGS);
1.44 kristaps 116: static int termp_br_pre(DECL_ARGS);
1.31 kristaps 117: static int termp_brq_pre(DECL_ARGS);
118: static int termp_bt_pre(DECL_ARGS);
119: static int termp_cd_pre(DECL_ARGS);
120: static int termp_d1_pre(DECL_ARGS);
121: static int termp_dq_pre(DECL_ARGS);
122: static int termp_ex_pre(DECL_ARGS);
123: static int termp_fa_pre(DECL_ARGS);
124: static int termp_fl_pre(DECL_ARGS);
125: static int termp_fn_pre(DECL_ARGS);
126: static int termp_fo_pre(DECL_ARGS);
127: static int termp_ft_pre(DECL_ARGS);
128: static int termp_in_pre(DECL_ARGS);
129: static int termp_it_pre(DECL_ARGS);
130: static int termp_lk_pre(DECL_ARGS);
131: static int termp_nd_pre(DECL_ARGS);
132: static int termp_nm_pre(DECL_ARGS);
133: static int termp_ns_pre(DECL_ARGS);
134: static int termp_op_pre(DECL_ARGS);
135: static int termp_pf_pre(DECL_ARGS);
136: static int termp_pp_pre(DECL_ARGS);
137: static int termp_pq_pre(DECL_ARGS);
138: static int termp_qq_pre(DECL_ARGS);
139: static int termp_rs_pre(DECL_ARGS);
140: static int termp_rv_pre(DECL_ARGS);
141: static int termp_sh_pre(DECL_ARGS);
142: static int termp_sm_pre(DECL_ARGS);
1.45 kristaps 143: static int termp_sp_pre(DECL_ARGS);
1.31 kristaps 144: static int termp_sq_pre(DECL_ARGS);
145: static int termp_ss_pre(DECL_ARGS);
1.69 kristaps 146: static int termp_under_pre(DECL_ARGS);
1.31 kristaps 147: static int termp_ud_pre(DECL_ARGS);
148: static int termp_xr_pre(DECL_ARGS);
149: static int termp_xx_pre(DECL_ARGS);
150:
1.70 kristaps 151: static const struct termact termacts[MDOC_MAX] = {
1.14 kristaps 152: { termp_ap_pre, NULL }, /* Ap */
1.1 kristaps 153: { NULL, NULL }, /* Dd */
154: { NULL, NULL }, /* Dt */
155: { NULL, NULL }, /* Os */
156: { termp_sh_pre, termp_sh_post }, /* Sh */
157: { termp_ss_pre, termp_ss_post }, /* Ss */
158: { termp_pp_pre, NULL }, /* Pp */
159: { termp_d1_pre, termp_d1_post }, /* D1 */
160: { termp_d1_pre, termp_d1_post }, /* Dl */
161: { termp_bd_pre, termp_bd_post }, /* Bd */
162: { NULL, NULL }, /* Ed */
163: { NULL, termp_bl_post }, /* Bl */
164: { NULL, NULL }, /* El */
165: { termp_it_pre, termp_it_post }, /* It */
166: { NULL, NULL }, /* Ad */
1.61 kristaps 167: { termp_an_pre, termp_an_post }, /* An */
1.69 kristaps 168: { termp_under_pre, NULL }, /* Ar */
1.1 kristaps 169: { termp_cd_pre, NULL }, /* Cd */
1.69 kristaps 170: { termp_bold_pre, NULL }, /* Cm */
1.1 kristaps 171: { NULL, NULL }, /* Dv */
172: { NULL, NULL }, /* Er */
173: { NULL, NULL }, /* Ev */
174: { termp_ex_pre, NULL }, /* Ex */
175: { termp_fa_pre, NULL }, /* Fa */
1.69 kristaps 176: { termp_bold_pre, termp_fd_post }, /* Fd */
1.1 kristaps 177: { termp_fl_pre, NULL }, /* Fl */
178: { termp_fn_pre, termp_fn_post }, /* Fn */
179: { termp_ft_pre, termp_ft_post }, /* Ft */
1.69 kristaps 180: { termp_bold_pre, NULL }, /* Ic */
1.1 kristaps 181: { termp_in_pre, termp_in_post }, /* In */
182: { NULL, NULL }, /* Li */
183: { termp_nd_pre, NULL }, /* Nd */
184: { termp_nm_pre, NULL }, /* Nm */
185: { termp_op_pre, termp_op_post }, /* Op */
186: { NULL, NULL }, /* Ot */
1.69 kristaps 187: { termp_under_pre, NULL }, /* Pa */
1.1 kristaps 188: { termp_rv_pre, NULL }, /* Rv */
1.36 kristaps 189: { NULL, NULL }, /* St */
1.69 kristaps 190: { termp_under_pre, NULL }, /* Va */
1.70 kristaps 191: { termp_under_pre, termp_vt_post }, /* Vt */
1.1 kristaps 192: { termp_xr_pre, NULL }, /* Xr */
193: { NULL, termp____post }, /* %A */
194: { NULL, termp____post }, /* %B */
195: { NULL, termp____post }, /* %D */
196: { NULL, termp____post }, /* %I */
1.69 kristaps 197: { termp_under_pre, termp____post }, /* %J */
1.1 kristaps 198: { NULL, termp____post }, /* %N */
199: { NULL, termp____post }, /* %O */
200: { NULL, termp____post }, /* %P */
201: { NULL, termp____post }, /* %R */
1.69 kristaps 202: { termp_under_pre, termp____post }, /* %T */
1.1 kristaps 203: { NULL, termp____post }, /* %V */
204: { NULL, NULL }, /* Ac */
205: { termp_aq_pre, termp_aq_post }, /* Ao */
206: { termp_aq_pre, termp_aq_post }, /* Aq */
1.35 kristaps 207: { NULL, NULL }, /* At */
1.1 kristaps 208: { NULL, NULL }, /* Bc */
209: { termp_bf_pre, NULL }, /* Bf */
210: { termp_bq_pre, termp_bq_post }, /* Bo */
211: { termp_bq_pre, termp_bq_post }, /* Bq */
1.26 kristaps 212: { termp_xx_pre, NULL }, /* Bsx */
1.1 kristaps 213: { NULL, termp_bx_post }, /* Bx */
214: { NULL, NULL }, /* Db */
215: { NULL, NULL }, /* Dc */
216: { termp_dq_pre, termp_dq_post }, /* Do */
217: { termp_dq_pre, termp_dq_post }, /* Dq */
218: { NULL, NULL }, /* Ec */
219: { NULL, NULL }, /* Ef */
1.69 kristaps 220: { termp_under_pre, NULL }, /* Em */
1.1 kristaps 221: { NULL, NULL }, /* Eo */
1.26 kristaps 222: { termp_xx_pre, NULL }, /* Fx */
1.69 kristaps 223: { termp_bold_pre, NULL }, /* Ms */
1.1 kristaps 224: { NULL, NULL }, /* No */
225: { termp_ns_pre, NULL }, /* Ns */
1.26 kristaps 226: { termp_xx_pre, NULL }, /* Nx */
227: { termp_xx_pre, NULL }, /* Ox */
1.1 kristaps 228: { NULL, NULL }, /* Pc */
229: { termp_pf_pre, termp_pf_post }, /* Pf */
230: { termp_pq_pre, termp_pq_post }, /* Po */
231: { termp_pq_pre, termp_pq_post }, /* Pq */
232: { NULL, NULL }, /* Qc */
233: { termp_sq_pre, termp_sq_post }, /* Ql */
234: { termp_qq_pre, termp_qq_post }, /* Qo */
235: { termp_qq_pre, termp_qq_post }, /* Qq */
236: { NULL, NULL }, /* Re */
237: { termp_rs_pre, NULL }, /* Rs */
238: { NULL, NULL }, /* Sc */
239: { termp_sq_pre, termp_sq_post }, /* So */
240: { termp_sq_pre, termp_sq_post }, /* Sq */
241: { termp_sm_pre, NULL }, /* Sm */
1.69 kristaps 242: { termp_under_pre, NULL }, /* Sx */
243: { termp_bold_pre, NULL }, /* Sy */
1.1 kristaps 244: { NULL, NULL }, /* Tn */
1.26 kristaps 245: { termp_xx_pre, NULL }, /* Ux */
1.1 kristaps 246: { NULL, NULL }, /* Xc */
247: { NULL, NULL }, /* Xo */
248: { termp_fo_pre, termp_fo_post }, /* Fo */
249: { NULL, NULL }, /* Fc */
250: { termp_op_pre, termp_op_post }, /* Oo */
251: { NULL, NULL }, /* Oc */
252: { NULL, NULL }, /* Bk */
253: { NULL, NULL }, /* Ek */
254: { termp_bt_pre, NULL }, /* Bt */
255: { NULL, NULL }, /* Hf */
256: { NULL, NULL }, /* Fr */
257: { termp_ud_pre, NULL }, /* Ud */
1.37 kristaps 258: { NULL, termp_lb_post }, /* Lb */
1.14 kristaps 259: { termp_pp_pre, NULL }, /* Lp */
1.1 kristaps 260: { termp_lk_pre, NULL }, /* Lk */
1.69 kristaps 261: { termp_under_pre, NULL }, /* Mt */
1.1 kristaps 262: { termp_brq_pre, termp_brq_post }, /* Brq */
263: { termp_brq_pre, termp_brq_post }, /* Bro */
264: { NULL, NULL }, /* Brc */
265: { NULL, NULL }, /* %C */
266: { NULL, NULL }, /* Es */
267: { NULL, NULL }, /* En */
1.26 kristaps 268: { termp_xx_pre, NULL }, /* Dx */
1.1 kristaps 269: { NULL, NULL }, /* %Q */
1.44 kristaps 270: { termp_br_pre, NULL }, /* br */
1.45 kristaps 271: { termp_sp_pre, NULL }, /* sp */
1.1 kristaps 272: };
273:
1.2 kristaps 274: #ifdef __linux__
1.9 kristaps 275: extern size_t strlcpy(char *, const char *, size_t);
276: extern size_t strlcat(char *, const char *, size_t);
1.2 kristaps 277: #endif
278:
1.1 kristaps 279: static int arg_hasattr(int, const struct mdoc_node *);
280: static int arg_getattrs(const int *, int *, size_t,
281: const struct mdoc_node *);
282: static int arg_getattr(int, const struct mdoc_node *);
283: static size_t arg_offset(const struct mdoc_argv *);
284: static size_t arg_width(const struct mdoc_argv *, int);
285: static int arg_listtype(const struct mdoc_node *);
1.54 kristaps 286: static void fmt_block_vspace(struct termp *,
1.1 kristaps 287: const struct mdoc_node *,
288: const struct mdoc_node *);
1.3 kristaps 289: static void print_node(DECL_ARGS);
1.70 kristaps 290: static void print_head(DECL_ARGS);
1.3 kristaps 291: static void print_body(DECL_ARGS);
1.70 kristaps 292: static void print_foot(DECL_ARGS);
1.1 kristaps 293:
294:
1.70 kristaps 295: void
296: mdoc_run(struct termp *p, const struct mdoc *mdoc)
1.1 kristaps 297: {
1.70 kristaps 298: const struct mdoc_node *n;
299: const struct mdoc_meta *m;
300:
301: n = mdoc_node(mdoc);
302: m = mdoc_meta(mdoc);
303:
304: print_head(p, NULL, m, n);
305: if (n->child)
306: print_body(p, NULL, m, n->child);
307: print_foot(p, NULL, m, n);
1.1 kristaps 308: }
309:
310:
1.3 kristaps 311: static void
1.1 kristaps 312: print_body(DECL_ARGS)
313: {
314:
1.3 kristaps 315: print_node(p, pair, meta, node);
1.70 kristaps 316: if (node->next)
317: print_body(p, pair, meta, node->next);
1.1 kristaps 318: }
319:
320:
1.70 kristaps 321: /* ARGSUSED */
1.3 kristaps 322: static void
1.1 kristaps 323: print_node(DECL_ARGS)
324: {
1.70 kristaps 325: int chld, bold, under;
1.1 kristaps 326: struct termpair npair;
1.30 kristaps 327: size_t offset, rmargin;
1.1 kristaps 328:
1.70 kristaps 329: chld = 1;
1.29 kristaps 330: offset = p->offset;
1.30 kristaps 331: rmargin = p->rmargin;
1.69 kristaps 332: bold = p->bold;
333: under = p->under;
1.29 kristaps 334:
1.70 kristaps 335: bzero(&npair, sizeof(struct termpair));
1.1 kristaps 336: npair.ppair = pair;
1.46 kristaps 337:
1.1 kristaps 338: if (MDOC_TEXT != node->type) {
339: if (termacts[node->tok].pre)
1.70 kristaps 340: chld = (*termacts[node->tok].pre)
341: (p, &npair, meta, node);
342: } else
1.1 kristaps 343: term_word(p, node->string);
344:
1.70 kristaps 345: if (chld && node->child)
346: print_body(p, &npair, meta, node->child);
1.1 kristaps 347:
1.70 kristaps 348: /*
349: * XXX - if bold/under were to span scopes, this wouldn't be
350: * possible, but because decoration is always in-scope, we can
351: * get away with this.
352: */
1.1 kristaps 353:
1.69 kristaps 354: p->bold = bold;
355: p->under = under;
1.46 kristaps 356:
1.1 kristaps 357: if (MDOC_TEXT != node->type)
358: if (termacts[node->tok].post)
1.70 kristaps 359: (*termacts[node->tok].post)
360: (p, &npair, meta, node);
1.29 kristaps 361:
362: p->offset = offset;
1.30 kristaps 363: p->rmargin = rmargin;
1.1 kristaps 364: }
365:
366:
1.70 kristaps 367: /* ARGSUSED */
1.3 kristaps 368: static void
1.70 kristaps 369: print_foot(DECL_ARGS)
1.1 kristaps 370: {
371: struct tm *tm;
372: char *buf, *os;
373:
1.9 kristaps 374: /*
375: * Output the footer in new-groff style, that is, three columns
376: * with the middle being the manual date and flanking columns
377: * being the operating system:
378: *
379: * SYSTEM DATE SYSTEM
380: */
381:
1.1 kristaps 382: if (NULL == (buf = malloc(p->rmargin)))
1.70 kristaps 383: err(EXIT_FAILURE, "malloc");
1.1 kristaps 384: if (NULL == (os = malloc(p->rmargin)))
1.70 kristaps 385: err(EXIT_FAILURE, "malloc");
1.1 kristaps 386:
387: tm = localtime(&meta->date);
388:
1.42 kristaps 389: if (0 == strftime(buf, p->rmargin, "%B %e, %Y", tm))
1.70 kristaps 390: err(EXIT_FAILURE, "strftime");
1.1 kristaps 391:
392: (void)strlcpy(os, meta->os, p->rmargin);
393:
394: term_vspace(p);
395:
1.9 kristaps 396: p->offset = 0;
397: p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
1.1 kristaps 398: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
399:
400: term_word(p, os);
401: term_flushln(p);
402:
1.9 kristaps 403: p->offset = p->rmargin;
404: p->rmargin = p->maxrmargin - strlen(os);
1.1 kristaps 405: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.9 kristaps 406:
407: term_word(p, buf);
408: term_flushln(p);
409:
1.1 kristaps 410: p->offset = p->rmargin;
411: p->rmargin = p->maxrmargin;
412: p->flags &= ~TERMP_NOBREAK;
1.9 kristaps 413: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.1 kristaps 414:
1.9 kristaps 415: term_word(p, os);
1.1 kristaps 416: term_flushln(p);
417:
1.9 kristaps 418: p->offset = 0;
419: p->rmargin = p->maxrmargin;
420: p->flags = 0;
421:
1.1 kristaps 422: free(buf);
423: free(os);
424: }
425:
426:
1.70 kristaps 427: /* ARGSUSED */
1.3 kristaps 428: static void
1.70 kristaps 429: print_head(DECL_ARGS)
1.1 kristaps 430: {
431: char *buf, *title;
432:
433: p->rmargin = p->maxrmargin;
434: p->offset = 0;
435:
436: if (NULL == (buf = malloc(p->rmargin)))
1.70 kristaps 437: err(EXIT_FAILURE, "malloc");
1.1 kristaps 438: if (NULL == (title = malloc(p->rmargin)))
1.70 kristaps 439: err(EXIT_FAILURE, "malloc");
1.1 kristaps 440:
441: /*
442: * The header is strange. It has three components, which are
443: * really two with the first duplicated. It goes like this:
444: *
445: * IDENTIFIER TITLE IDENTIFIER
446: *
447: * The IDENTIFIER is NAME(SECTION), which is the command-name
448: * (if given, or "unknown" if not) followed by the manual page
449: * section. These are given in `Dt'. The TITLE is a free-form
450: * string depending on the manual volume. If not specified, it
451: * switches on the manual section.
452: */
453:
454: assert(meta->vol);
455: (void)strlcpy(buf, meta->vol, p->rmargin);
456:
457: if (meta->arch) {
458: (void)strlcat(buf, " (", p->rmargin);
459: (void)strlcat(buf, meta->arch, p->rmargin);
460: (void)strlcat(buf, ")", p->rmargin);
461: }
462:
1.70 kristaps 463: snprintf(title, p->rmargin, "%s(%d)", meta->title, meta->msec);
1.1 kristaps 464:
465: p->offset = 0;
1.8 kristaps 466: p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
1.1 kristaps 467: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
468:
469: term_word(p, title);
470: term_flushln(p);
471:
472: p->offset = p->rmargin;
473: p->rmargin = p->maxrmargin - strlen(title);
1.9 kristaps 474: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.1 kristaps 475:
476: term_word(p, buf);
477: term_flushln(p);
478:
479: p->offset = p->rmargin;
480: p->rmargin = p->maxrmargin;
481: p->flags &= ~TERMP_NOBREAK;
482: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
483:
484: term_word(p, title);
485: term_flushln(p);
486:
1.9 kristaps 487: p->offset = 0;
1.1 kristaps 488: p->rmargin = p->maxrmargin;
489: p->flags &= ~TERMP_NOSPACE;
490:
491: free(title);
492: free(buf);
493: }
494:
495:
1.72 kristaps 496: /* FIXME: put in utility file for front-ends. */
1.1 kristaps 497: static size_t
498: arg_width(const struct mdoc_argv *arg, int pos)
499: {
500: int i, len;
1.40 kristaps 501: const char *p;
1.1 kristaps 502:
503: assert(pos < (int)arg->sz && pos >= 0);
504: assert(arg->value[pos]);
505:
1.40 kristaps 506: p = arg->value[pos];
507:
508: if (0 == (len = (int)strlen(p)))
1.1 kristaps 509: return(0);
510:
511: for (i = 0; i < len - 1; i++)
1.40 kristaps 512: if ( ! isdigit((u_char)p[i]))
1.1 kristaps 513: break;
514:
1.40 kristaps 515: if (i == len - 1)
516: if ('n' == p[len - 1] || 'm' == p[len - 1])
517: return((size_t)atoi(p) + 2);
1.1 kristaps 518:
1.40 kristaps 519: return((size_t)len + 2);
1.1 kristaps 520: }
521:
522:
1.73 ! kristaps 523: /* FIXME: put in utility file for front-ends. */
1.1 kristaps 524: static int
525: arg_listtype(const struct mdoc_node *n)
526: {
527: int i, len;
528:
529: assert(MDOC_BLOCK == n->type);
530:
531: len = (int)(n->args ? n->args->argc : 0);
532:
533: for (i = 0; i < len; i++)
534: switch (n->args->argv[i].arg) {
535: case (MDOC_Bullet):
536: /* FALLTHROUGH */
537: case (MDOC_Dash):
538: /* FALLTHROUGH */
539: case (MDOC_Enum):
540: /* FALLTHROUGH */
541: case (MDOC_Hyphen):
542: /* FALLTHROUGH */
543: case (MDOC_Tag):
544: /* FALLTHROUGH */
545: case (MDOC_Inset):
546: /* FALLTHROUGH */
547: case (MDOC_Diag):
548: /* FALLTHROUGH */
549: case (MDOC_Item):
550: /* FALLTHROUGH */
551: case (MDOC_Column):
552: /* FALLTHROUGH */
1.38 kristaps 553: case (MDOC_Hang):
554: /* FALLTHROUGH */
1.1 kristaps 555: case (MDOC_Ohang):
556: return(n->args->argv[i].arg);
557: default:
558: break;
559: }
560:
1.38 kristaps 561: return(-1);
1.1 kristaps 562: }
563:
564:
1.72 kristaps 565: /* FIXME: put in utility file for front-ends. */
1.1 kristaps 566: static size_t
567: arg_offset(const struct mdoc_argv *arg)
568: {
1.40 kristaps 569: int len, i;
570: const char *p;
1.1 kristaps 571:
572: assert(*arg->value);
1.40 kristaps 573: p = *arg->value;
574:
575: if (0 == strcmp(p, "left"))
1.19 kristaps 576: return(0);
1.40 kristaps 577: if (0 == strcmp(p, "indent"))
1.16 kristaps 578: return(INDENT + 1);
1.40 kristaps 579: if (0 == strcmp(p, "indent-two"))
1.20 kristaps 580: return((INDENT + 1) * 2);
1.10 kristaps 581:
1.40 kristaps 582: if (0 == (len = (int)strlen(p)))
583: return(0);
584:
585: for (i = 0; i < len - 1; i++)
586: if ( ! isdigit((u_char)p[i]))
587: break;
588:
589: if (i == len - 1)
590: if ('n' == p[len - 1] || 'm' == p[len - 1])
591: return((size_t)atoi(p));
1.11 kristaps 592:
1.40 kristaps 593: return((size_t)len);
1.1 kristaps 594: }
595:
596:
597: static int
598: arg_hasattr(int arg, const struct mdoc_node *n)
599: {
600:
601: return(-1 != arg_getattr(arg, n));
602: }
603:
604:
605: static int
606: arg_getattr(int v, const struct mdoc_node *n)
607: {
608: int val;
609:
610: return(arg_getattrs(&v, &val, 1, n) ? val : -1);
611: }
612:
613:
614: static int
615: arg_getattrs(const int *keys, int *vals,
616: size_t sz, const struct mdoc_node *n)
617: {
618: int i, j, k;
619:
620: if (NULL == n->args)
621: return(0);
622:
623: for (k = i = 0; i < (int)n->args->argc; i++)
624: for (j = 0; j < (int)sz; j++)
625: if (n->args->argv[i].arg == keys[j]) {
626: vals[j] = i;
627: k++;
628: }
629: return(k);
630: }
631:
632:
633: /* ARGSUSED */
1.54 kristaps 634: static void
1.1 kristaps 635: fmt_block_vspace(struct termp *p,
636: const struct mdoc_node *bl,
637: const struct mdoc_node *node)
638: {
639: const struct mdoc_node *n;
640:
641: term_newln(p);
642:
1.59 kristaps 643: if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Compact, bl))
1.54 kristaps 644: return;
1.63 kristaps 645: assert(node);
1.54 kristaps 646:
647: /*
648: * Search through our prior nodes. If we follow a `Ss' or `Sh',
649: * then don't vspace.
650: */
1.1 kristaps 651:
652: for (n = node; n; n = n->parent) {
653: if (MDOC_BLOCK != n->type)
654: continue;
655: if (MDOC_Ss == n->tok)
1.54 kristaps 656: return;
1.1 kristaps 657: if (MDOC_Sh == n->tok)
1.54 kristaps 658: return;
1.1 kristaps 659: if (NULL == n->prev)
660: continue;
661: break;
662: }
663:
1.54 kristaps 664: /*
1.55 kristaps 665: * XXX - not documented: a `-column' does not ever assert vspace
666: * within the list.
1.54 kristaps 667: */
668:
1.59 kristaps 669: if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Column, bl))
1.54 kristaps 670: if (node->prev && MDOC_It == node->prev->tok)
671: return;
672:
1.55 kristaps 673: /*
674: * XXX - not documented: a `-diag' without a body does not
675: * assert a vspace prior to the next element.
676: */
1.59 kristaps 677: if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Diag, bl))
1.55 kristaps 678: if (node->prev && MDOC_It == node->prev->tok) {
679: assert(node->prev->body);
680: if (NULL == node->prev->body->child)
681: return;
682: }
683:
1.54 kristaps 684: term_vspace(p);
1.1 kristaps 685: }
686:
687:
688: /* ARGSUSED */
689: static int
690: termp_dq_pre(DECL_ARGS)
691: {
692:
693: if (MDOC_BODY != node->type)
694: return(1);
695:
696: term_word(p, "\\(lq");
697: p->flags |= TERMP_NOSPACE;
698: return(1);
699: }
700:
701:
702: /* ARGSUSED */
703: static void
704: termp_dq_post(DECL_ARGS)
705: {
706:
707: if (MDOC_BODY != node->type)
708: return;
709:
710: p->flags |= TERMP_NOSPACE;
711: term_word(p, "\\(rq");
712: }
713:
714:
715: /* ARGSUSED */
716: static int
717: termp_it_pre(DECL_ARGS)
718: {
719: const struct mdoc_node *bl, *n;
720: char buf[7];
1.39 kristaps 721: int i, type, keys[3], vals[3];
1.1 kristaps 722: size_t width, offset;
723:
1.54 kristaps 724: if (MDOC_BLOCK == node->type) {
725: fmt_block_vspace(p, node->parent->parent, node);
726: return(1);
727: }
1.1 kristaps 728:
729: bl = node->parent->parent->parent;
730:
731: /* Save parent attributes. */
732:
733: pair->flag = p->flags;
734:
735: /* Get list width and offset. */
736:
737: keys[0] = MDOC_Width;
738: keys[1] = MDOC_Offset;
739: keys[2] = MDOC_Column;
740:
741: vals[0] = vals[1] = vals[2] = -1;
742:
743: width = offset = 0;
744:
745: (void)arg_getattrs(keys, vals, 3, bl);
746:
747: type = arg_listtype(bl);
1.38 kristaps 748: assert(-1 != type);
1.1 kristaps 749:
750: /* Calculate real width and offset. */
751:
752: switch (type) {
753: case (MDOC_Column):
754: if (MDOC_BODY == node->type)
755: break;
1.58 kristaps 756: /*
757: * Work around groff's column handling. The offset is
758: * equal to the sum of all widths leading to the current
759: * column (plus the -offset value). If this column
760: * exceeds the stated number of columns, the width is
761: * set as 0, else it's the stated column width (later
762: * the 0 will be adjusted to default 10 or, if in the
763: * last column case, set to stretch to the margin).
764: */
765: for (i = 0, n = node->prev; n && n &&
766: i < (int)bl->args[vals[2]].argv->sz;
767: n = n->prev, i++)
1.1 kristaps 768: offset += arg_width
769: (&bl->args->argv[vals[2]], i);
1.58 kristaps 770:
771: /* Whether exceeds maximum column. */
772: if (i < (int)bl->args[vals[2]].argv->sz)
773: width = arg_width(&bl->args->argv[vals[2]], i);
774: else
775: width = 0;
776:
1.1 kristaps 777: if (vals[1] >= 0)
778: offset += arg_offset(&bl->args->argv[vals[1]]);
779: break;
780: default:
781: if (vals[0] >= 0)
782: width = arg_width(&bl->args->argv[vals[0]], 0);
783: if (vals[1] >= 0)
1.19 kristaps 784: offset += arg_offset(&bl->args->argv[vals[1]]);
1.1 kristaps 785: break;
786: }
787:
788: /*
789: * List-type can override the width in the case of fixed-head
790: * values (bullet, dash/hyphen, enum). Tags need a non-zero
1.67 kristaps 791: * offset.
1.1 kristaps 792: */
793:
794: switch (type) {
795: case (MDOC_Bullet):
796: /* FALLTHROUGH */
797: case (MDOC_Dash):
798: /* FALLTHROUGH */
799: case (MDOC_Hyphen):
800: if (width < 4)
801: width = 4;
802: break;
1.17 kristaps 803: case (MDOC_Enum):
804: if (width < 5)
805: width = 5;
806: break;
1.38 kristaps 807: case (MDOC_Hang):
1.39 kristaps 808: if (0 == width)
809: width = 8;
810: break;
1.49 kristaps 811: case (MDOC_Column):
812: /* FALLTHROUGH */
1.1 kristaps 813: case (MDOC_Tag):
814: if (0 == width)
815: width = 10;
816: break;
817: default:
818: break;
819: }
820:
821: /*
1.17 kristaps 822: * Whitespace control. Inset bodies need an initial space,
823: * while diagonal bodies need two.
1.1 kristaps 824: */
825:
1.51 kristaps 826: p->flags |= TERMP_NOSPACE;
827:
1.1 kristaps 828: switch (type) {
1.51 kristaps 829: case (MDOC_Diag):
1.62 kristaps 830: if (MDOC_BODY == node->type)
831: term_word(p, "\\ \\ ");
1.51 kristaps 832: break;
1.1 kristaps 833: case (MDOC_Inset):
834: if (MDOC_BODY == node->type)
1.51 kristaps 835: term_word(p, "\\ ");
1.1 kristaps 836: break;
837: default:
838: break;
839: }
840:
1.51 kristaps 841: p->flags |= TERMP_NOSPACE;
842:
1.1 kristaps 843: switch (type) {
844: case (MDOC_Diag):
845: if (MDOC_HEAD == node->type)
1.69 kristaps 846: p->bold++;
1.1 kristaps 847: break;
848: default:
849: break;
850: }
851:
852: /*
853: * Pad and break control. This is the tricker part. Lists with
854: * set right-margins for the head get TERMP_NOBREAK because, if
855: * they overrun the margin, they wrap to the new margin.
856: * Correspondingly, the body for these types don't left-pad, as
857: * the head will pad out to to the right.
858: */
859:
860: switch (type) {
861: case (MDOC_Bullet):
862: /* FALLTHROUGH */
863: case (MDOC_Dash):
864: /* FALLTHROUGH */
865: case (MDOC_Enum):
866: /* FALLTHROUGH */
867: case (MDOC_Hyphen):
1.39 kristaps 868: if (MDOC_HEAD == node->type)
869: p->flags |= TERMP_NOBREAK;
870: else
871: p->flags |= TERMP_NOLPAD;
872: break;
1.38 kristaps 873: case (MDOC_Hang):
1.39 kristaps 874: if (MDOC_HEAD == node->type)
875: p->flags |= TERMP_NOBREAK;
876: else
877: p->flags |= TERMP_NOLPAD;
878:
1.59 kristaps 879: if (MDOC_HEAD != node->type)
880: break;
881:
882: /*
883: * This is ugly. If `-hang' is specified and the body
884: * is a `Bl' or `Bd', then we want basically to nullify
885: * the "overstep" effect in term_flushln() and treat
886: * this as a `-ohang' list instead.
887: */
888: if (node->next->child &&
889: (MDOC_Bl == node->next->child->tok ||
890: MDOC_Bd == node->next->child->tok)) {
891: p->flags &= ~TERMP_NOBREAK;
892: p->flags &= ~TERMP_NOLPAD;
893: } else
1.39 kristaps 894: p->flags |= TERMP_HANG;
895: break;
1.1 kristaps 896: case (MDOC_Tag):
897: if (MDOC_HEAD == node->type)
1.51 kristaps 898: p->flags |= TERMP_NOBREAK | TERMP_TWOSPACE;
1.1 kristaps 899: else
900: p->flags |= TERMP_NOLPAD;
1.38 kristaps 901:
1.39 kristaps 902: if (MDOC_HEAD != node->type)
903: break;
904: if (NULL == node->next || NULL == node->next->child)
905: p->flags |= TERMP_DANGLE;
1.1 kristaps 906: break;
907: case (MDOC_Column):
908: if (MDOC_HEAD == node->type) {
909: assert(node->next);
910: if (MDOC_BODY == node->next->type)
911: p->flags &= ~TERMP_NOBREAK;
912: else
913: p->flags |= TERMP_NOBREAK;
914: if (node->prev)
915: p->flags |= TERMP_NOLPAD;
916: }
917: break;
918: case (MDOC_Diag):
919: if (MDOC_HEAD == node->type)
920: p->flags |= TERMP_NOBREAK;
921: break;
922: default:
923: break;
924: }
925:
926: /*
927: * Margin control. Set-head-width lists have their right
928: * margins shortened. The body for these lists has the offset
929: * necessarily lengthened. Everybody gets the offset.
930: */
931:
932: p->offset += offset;
933:
934: switch (type) {
1.59 kristaps 935: case (MDOC_Hang):
936: /*
937: * Same stipulation as above, regarding `-hang'. We
938: * don't want to recalculate rmargin and offsets when
939: * using `Bd' or `Bl' within `-hang' overstep lists.
940: */
941: if (MDOC_HEAD == node->type && node->next->child &&
942: (MDOC_Bl == node->next->child->tok ||
943: MDOC_Bd == node->next->child->tok))
944: break;
945: /* FALLTHROUGH */
1.1 kristaps 946: case (MDOC_Bullet):
947: /* FALLTHROUGH */
948: case (MDOC_Dash):
949: /* FALLTHROUGH */
950: case (MDOC_Enum):
951: /* FALLTHROUGH */
952: case (MDOC_Hyphen):
953: /* FALLTHROUGH */
954: case (MDOC_Tag):
1.49 kristaps 955: assert(width);
1.1 kristaps 956: if (MDOC_HEAD == node->type)
957: p->rmargin = p->offset + width;
958: else
959: p->offset += width;
960: break;
961: case (MDOC_Column):
1.49 kristaps 962: assert(width);
1.1 kristaps 963: p->rmargin = p->offset + width;
1.50 kristaps 964: /*
965: * XXX - this behaviour is not documented: the
966: * right-most column is filled to the right margin.
967: */
968: if (MDOC_HEAD == node->type &&
969: MDOC_BODY == node->next->type)
970: p->rmargin = p->maxrmargin;
1.1 kristaps 971: break;
972: default:
973: break;
974: }
975:
976: /*
977: * The dash, hyphen, bullet and enum lists all have a special
1.15 kristaps 978: * HEAD character (temporarily bold, in some cases).
1.1 kristaps 979: */
980:
981: if (MDOC_HEAD == node->type)
982: switch (type) {
983: case (MDOC_Bullet):
1.69 kristaps 984: p->bold++;
1.1 kristaps 985: term_word(p, "\\[bu]");
1.69 kristaps 986: p->bold--;
1.1 kristaps 987: break;
988: case (MDOC_Dash):
989: /* FALLTHROUGH */
990: case (MDOC_Hyphen):
1.69 kristaps 991: p->bold++;
1.27 kristaps 992: term_word(p, "\\(hy");
1.69 kristaps 993: p->bold--;
1.1 kristaps 994: break;
995: case (MDOC_Enum):
996: (pair->ppair->ppair->count)++;
997: (void)snprintf(buf, sizeof(buf), "%d.",
998: pair->ppair->ppair->count);
999: term_word(p, buf);
1000: break;
1001: default:
1002: break;
1003: }
1004:
1005: /*
1006: * If we're not going to process our children, indicate so here.
1007: */
1008:
1009: switch (type) {
1010: case (MDOC_Bullet):
1011: /* FALLTHROUGH */
1012: case (MDOC_Item):
1013: /* FALLTHROUGH */
1014: case (MDOC_Dash):
1015: /* FALLTHROUGH */
1016: case (MDOC_Hyphen):
1017: /* FALLTHROUGH */
1018: case (MDOC_Enum):
1019: if (MDOC_HEAD == node->type)
1020: return(0);
1021: break;
1022: case (MDOC_Column):
1023: if (MDOC_BODY == node->type)
1024: return(0);
1025: break;
1026: default:
1027: break;
1028: }
1029:
1030: return(1);
1031: }
1032:
1033:
1034: /* ARGSUSED */
1035: static void
1036: termp_it_post(DECL_ARGS)
1037: {
1038: int type;
1039:
1040: if (MDOC_BODY != node->type && MDOC_HEAD != node->type)
1041: return;
1042:
1043: type = arg_listtype(node->parent->parent->parent);
1.38 kristaps 1044: assert(-1 != type);
1.1 kristaps 1045:
1046: switch (type) {
1047: case (MDOC_Item):
1048: /* FALLTHROUGH */
1.53 kristaps 1049: case (MDOC_Diag):
1050: /* FALLTHROUGH */
1.1 kristaps 1051: case (MDOC_Inset):
1052: if (MDOC_BODY == node->type)
1053: term_flushln(p);
1054: break;
1055: case (MDOC_Column):
1056: if (MDOC_HEAD == node->type)
1057: term_flushln(p);
1058: break;
1059: default:
1060: term_flushln(p);
1061: break;
1062: }
1063:
1064: p->flags = pair->flag;
1065: }
1066:
1067:
1068: /* ARGSUSED */
1069: static int
1070: termp_nm_pre(DECL_ARGS)
1071: {
1072:
1073: if (SEC_SYNOPSIS == node->sec)
1074: term_newln(p);
1.69 kristaps 1075: p->bold++;
1.1 kristaps 1076: if (NULL == node->child)
1077: term_word(p, meta->name);
1078: return(1);
1079: }
1080:
1081:
1082: /* ARGSUSED */
1083: static int
1084: termp_fl_pre(DECL_ARGS)
1085: {
1086:
1.69 kristaps 1087: p->bold++;
1.1 kristaps 1088: term_word(p, "\\-");
1089: p->flags |= TERMP_NOSPACE;
1090: return(1);
1091: }
1092:
1093:
1094: /* ARGSUSED */
1095: static int
1.61 kristaps 1096: termp_an_pre(DECL_ARGS)
1097: {
1098:
1099: if (NULL == node->child)
1100: return(1);
1101:
1102: /*
1103: * XXX: this is poorly documented. If not in the AUTHORS
1104: * section, `An -split' will cause newlines to occur before the
1105: * author name. If in the AUTHORS section, by default, the
1106: * first `An' invocation is nosplit, then all subsequent ones,
1107: * regardless of whether interspersed with other macros/text,
1108: * are split. -split, in this case, will override the condition
1109: * of the implied first -nosplit.
1110: */
1111:
1112: if (node->sec == SEC_AUTHORS) {
1113: if ( ! (TERMP_ANPREC & p->flags)) {
1114: if (TERMP_SPLIT & p->flags)
1115: term_newln(p);
1116: return(1);
1117: }
1118: if (TERMP_NOSPLIT & p->flags)
1119: return(1);
1120: term_newln(p);
1121: return(1);
1122: }
1123:
1124: if (TERMP_SPLIT & p->flags)
1125: term_newln(p);
1126:
1127: return(1);
1128: }
1129:
1130:
1131: /* ARGSUSED */
1132: static void
1133: termp_an_post(DECL_ARGS)
1134: {
1135:
1136: if (node->child) {
1137: if (SEC_AUTHORS == node->sec)
1138: p->flags |= TERMP_ANPREC;
1139: return;
1140: }
1141:
1142: if (arg_getattr(MDOC_Split, node) > -1) {
1143: p->flags &= ~TERMP_NOSPLIT;
1144: p->flags |= TERMP_SPLIT;
1145: } else {
1146: p->flags &= ~TERMP_SPLIT;
1147: p->flags |= TERMP_NOSPLIT;
1148: }
1149:
1150: }
1151:
1152:
1153: /* ARGSUSED */
1154: static int
1.1 kristaps 1155: termp_ns_pre(DECL_ARGS)
1156: {
1157:
1158: p->flags |= TERMP_NOSPACE;
1159: return(1);
1160: }
1161:
1162:
1163: /* ARGSUSED */
1164: static int
1165: termp_pp_pre(DECL_ARGS)
1166: {
1167:
1168: term_vspace(p);
1169: return(1);
1170: }
1171:
1172:
1173: /* ARGSUSED */
1174: static int
1175: termp_rs_pre(DECL_ARGS)
1176: {
1177:
1178: if (MDOC_BLOCK == node->type && node->prev)
1179: term_vspace(p);
1180: return(1);
1181: }
1182:
1183:
1184: /* ARGSUSED */
1185: static int
1186: termp_rv_pre(DECL_ARGS)
1187: {
1.68 kristaps 1188: const struct mdoc_node *nn;
1.1 kristaps 1189:
1190: term_newln(p);
1191: term_word(p, "The");
1192:
1.68 kristaps 1193: nn = node->child;
1194: assert(nn);
1195: for ( ; nn; nn = nn->next) {
1.69 kristaps 1196: p->bold++;
1.68 kristaps 1197: term_word(p, nn->string);
1.69 kristaps 1198: p->bold--;
1.68 kristaps 1199: p->flags |= TERMP_NOSPACE;
1200: if (nn->next && NULL == nn->next->next)
1201: term_word(p, "(), and");
1202: else if (nn->next)
1203: term_word(p, "(),");
1204: else
1205: term_word(p, "()");
1206: }
1207:
1208: if (node->child->next)
1209: term_word(p, "functions return");
1210: else
1211: term_word(p, "function returns");
1.1 kristaps 1212:
1.68 kristaps 1213: term_word(p, "the value 0 if successful; otherwise the value "
1214: "-1 is returned and the global variable");
1.1 kristaps 1215:
1.69 kristaps 1216: p->under++;
1.1 kristaps 1217: term_word(p, "errno");
1.69 kristaps 1218: p->under--;
1.1 kristaps 1219:
1220: term_word(p, "is set to indicate the error.");
1221:
1.68 kristaps 1222: return(0);
1.1 kristaps 1223: }
1224:
1225:
1226: /* ARGSUSED */
1227: static int
1228: termp_ex_pre(DECL_ARGS)
1229: {
1.68 kristaps 1230: const struct mdoc_node *nn;
1.1 kristaps 1231:
1.68 kristaps 1232: term_word(p, "The");
1.1 kristaps 1233:
1.68 kristaps 1234: nn = node->child;
1235: assert(nn);
1236: for ( ; nn; nn = nn->next) {
1.69 kristaps 1237: p->bold++;
1.68 kristaps 1238: term_word(p, nn->string);
1.69 kristaps 1239: p->bold--;
1.68 kristaps 1240: p->flags |= TERMP_NOSPACE;
1241: if (nn->next && NULL == nn->next->next)
1242: term_word(p, ", and");
1243: else if (nn->next)
1244: term_word(p, ",");
1245: else
1246: p->flags &= ~TERMP_NOSPACE;
1247: }
1248:
1249: if (node->child->next)
1250: term_word(p, "utilities exit");
1251: else
1252: term_word(p, "utility exits");
1253:
1254: term_word(p, "0 on success, and >0 if an error occurs.");
1.1 kristaps 1255:
1.68 kristaps 1256: return(0);
1.1 kristaps 1257: }
1258:
1259:
1260: /* ARGSUSED */
1261: static int
1262: termp_nd_pre(DECL_ARGS)
1263: {
1.25 kristaps 1264:
1265: if (MDOC_BODY != node->type)
1266: return(1);
1.30 kristaps 1267:
1.27 kristaps 1268: #if defined(__OpenBSD__) || defined(__linux__)
1.41 kristaps 1269: term_word(p, "\\(en");
1.23 kristaps 1270: #else
1271: term_word(p, "\\(em");
1272: #endif
1.1 kristaps 1273: return(1);
1274: }
1275:
1276:
1277: /* ARGSUSED */
1278: static void
1279: termp_bl_post(DECL_ARGS)
1280: {
1281:
1282: if (MDOC_BLOCK == node->type)
1283: term_newln(p);
1284: }
1285:
1286:
1287: /* ARGSUSED */
1288: static void
1289: termp_op_post(DECL_ARGS)
1290: {
1291:
1292: if (MDOC_BODY != node->type)
1293: return;
1294: p->flags |= TERMP_NOSPACE;
1295: term_word(p, "\\(rB");
1296: }
1297:
1298:
1299: /* ARGSUSED */
1300: static int
1301: termp_xr_pre(DECL_ARGS)
1302: {
1303: const struct mdoc_node *n;
1304:
1.11 kristaps 1305: assert(node->child && MDOC_TEXT == node->child->type);
1306: n = node->child;
1307:
1.1 kristaps 1308: term_word(p, n->string);
1309: if (NULL == (n = n->next))
1310: return(0);
1311: p->flags |= TERMP_NOSPACE;
1312: term_word(p, "(");
1313: p->flags |= TERMP_NOSPACE;
1314: term_word(p, n->string);
1315: p->flags |= TERMP_NOSPACE;
1316: term_word(p, ")");
1317: return(0);
1318: }
1319:
1320:
1321: /* ARGSUSED */
1322: static void
1323: termp_vt_post(DECL_ARGS)
1324: {
1325:
1.32 kristaps 1326: if (node->sec != SEC_SYNOPSIS)
1327: return;
1328: if (node->next && MDOC_Vt == node->next->tok)
1329: term_newln(p);
1330: else if (node->next)
1.1 kristaps 1331: term_vspace(p);
1332: }
1333:
1334:
1335: /* ARGSUSED */
1336: static int
1.69 kristaps 1337: termp_bold_pre(DECL_ARGS)
1.1 kristaps 1338: {
1339:
1.69 kristaps 1340: p->bold++;
1.1 kristaps 1341: return(1);
1342: }
1343:
1344:
1345: /* ARGSUSED */
1346: static void
1347: termp_fd_post(DECL_ARGS)
1348: {
1349:
1350: if (node->sec != SEC_SYNOPSIS)
1351: return;
1.29 kristaps 1352:
1.1 kristaps 1353: term_newln(p);
1354: if (node->next && MDOC_Fd != node->next->tok)
1355: term_vspace(p);
1356: }
1357:
1358:
1359: /* ARGSUSED */
1360: static int
1361: termp_sh_pre(DECL_ARGS)
1362: {
1.64 kristaps 1363: /*
1.65 kristaps 1364: * XXX: undocumented: using two `Sh' macros in sequence has no
1365: * vspace between calls, only a newline.
1.64 kristaps 1366: */
1.1 kristaps 1367: switch (node->type) {
1.65 kristaps 1368: case (MDOC_BLOCK):
1369: if (node->prev && MDOC_Sh == node->prev->tok)
1370: if (NULL == node->prev->body->child)
1371: break;
1372: term_vspace(p);
1373: break;
1.1 kristaps 1374: case (MDOC_HEAD):
1.69 kristaps 1375: p->bold++;
1.1 kristaps 1376: break;
1377: case (MDOC_BODY):
1378: p->offset = INDENT;
1379: break;
1380: default:
1381: break;
1382: }
1383: return(1);
1384: }
1385:
1386:
1387: /* ARGSUSED */
1388: static void
1389: termp_sh_post(DECL_ARGS)
1390: {
1391:
1392: switch (node->type) {
1393: case (MDOC_HEAD):
1394: term_newln(p);
1395: break;
1396: case (MDOC_BODY):
1397: term_newln(p);
1398: p->offset = 0;
1399: break;
1400: default:
1401: break;
1402: }
1403: }
1404:
1405:
1406: /* ARGSUSED */
1407: static int
1408: termp_op_pre(DECL_ARGS)
1409: {
1410:
1411: switch (node->type) {
1412: case (MDOC_BODY):
1413: term_word(p, "\\(lB");
1414: p->flags |= TERMP_NOSPACE;
1415: break;
1416: default:
1417: break;
1418: }
1419: return(1);
1420: }
1421:
1422:
1423: /* ARGSUSED */
1424: static int
1425: termp_bt_pre(DECL_ARGS)
1426: {
1427:
1428: term_word(p, "is currently in beta test.");
1429: return(1);
1430: }
1431:
1432:
1433: /* ARGSUSED */
1434: static void
1435: termp_lb_post(DECL_ARGS)
1436: {
1437:
1438: term_newln(p);
1439: }
1440:
1441:
1442: /* ARGSUSED */
1443: static int
1444: termp_ud_pre(DECL_ARGS)
1445: {
1446:
1447: term_word(p, "currently under development.");
1448: return(1);
1449: }
1450:
1451:
1452: /* ARGSUSED */
1453: static int
1454: termp_d1_pre(DECL_ARGS)
1455: {
1456:
1.4 kristaps 1457: if (MDOC_BLOCK != node->type)
1.1 kristaps 1458: return(1);
1459: term_newln(p);
1.29 kristaps 1460: p->offset += (INDENT + 1);
1.1 kristaps 1461: return(1);
1462: }
1463:
1464:
1465: /* ARGSUSED */
1466: static void
1467: termp_d1_post(DECL_ARGS)
1468: {
1469:
1.4 kristaps 1470: if (MDOC_BLOCK != node->type)
1.1 kristaps 1471: return;
1472: term_newln(p);
1473: }
1474:
1475:
1476: /* ARGSUSED */
1477: static int
1478: termp_aq_pre(DECL_ARGS)
1479: {
1480:
1481: if (MDOC_BODY != node->type)
1482: return(1);
1483: term_word(p, "\\(la");
1484: p->flags |= TERMP_NOSPACE;
1485: return(1);
1486: }
1487:
1488:
1489: /* ARGSUSED */
1490: static void
1491: termp_aq_post(DECL_ARGS)
1492: {
1493:
1494: if (MDOC_BODY != node->type)
1495: return;
1496: p->flags |= TERMP_NOSPACE;
1497: term_word(p, "\\(ra");
1498: }
1499:
1500:
1501: /* ARGSUSED */
1502: static int
1503: termp_ft_pre(DECL_ARGS)
1504: {
1505:
1506: if (SEC_SYNOPSIS == node->sec)
1507: if (node->prev && MDOC_Fo == node->prev->tok)
1508: term_vspace(p);
1.69 kristaps 1509: p->under++;
1.1 kristaps 1510: return(1);
1511: }
1512:
1513:
1514: /* ARGSUSED */
1515: static void
1516: termp_ft_post(DECL_ARGS)
1517: {
1518:
1519: if (SEC_SYNOPSIS == node->sec)
1520: term_newln(p);
1521: }
1522:
1523:
1524: /* ARGSUSED */
1525: static int
1526: termp_fn_pre(DECL_ARGS)
1527: {
1528: const struct mdoc_node *n;
1529:
1.11 kristaps 1530: assert(node->child && MDOC_TEXT == node->child->type);
1.1 kristaps 1531:
1532: /* FIXME: can be "type funcname" "type varname"... */
1533:
1.69 kristaps 1534: p->bold++;
1.1 kristaps 1535: term_word(p, node->child->string);
1.69 kristaps 1536: p->bold--;
1.1 kristaps 1537:
1538: p->flags |= TERMP_NOSPACE;
1539: term_word(p, "(");
1540:
1541: for (n = node->child->next; n; n = n->next) {
1.69 kristaps 1542: p->under++;
1.1 kristaps 1543: term_word(p, n->string);
1.69 kristaps 1544: p->under--;
1.1 kristaps 1545: if (n->next)
1546: term_word(p, ",");
1547: }
1548:
1549: term_word(p, ")");
1550:
1551: if (SEC_SYNOPSIS == node->sec)
1552: term_word(p, ";");
1553:
1554: return(0);
1555: }
1556:
1557:
1558: /* ARGSUSED */
1559: static void
1560: termp_fn_post(DECL_ARGS)
1561: {
1562:
1563: if (node->sec == SEC_SYNOPSIS && node->next)
1564: term_vspace(p);
1565: }
1566:
1567:
1568: /* ARGSUSED */
1569: static int
1570: termp_fa_pre(DECL_ARGS)
1571: {
1572: struct mdoc_node *n;
1573:
1574: if (node->parent->tok != MDOC_Fo) {
1.69 kristaps 1575: p->under++;
1.1 kristaps 1576: return(1);
1577: }
1578:
1579: for (n = node->child; n; n = n->next) {
1.69 kristaps 1580: p->under++;
1.1 kristaps 1581: term_word(p, n->string);
1.69 kristaps 1582: p->under--;
1.1 kristaps 1583: if (n->next)
1584: term_word(p, ",");
1585: }
1586:
1587: if (node->child && node->next && node->next->tok == MDOC_Fa)
1588: term_word(p, ",");
1589:
1590: return(0);
1591: }
1592:
1593:
1594: /* ARGSUSED */
1595: static int
1596: termp_bd_pre(DECL_ARGS)
1597: {
1.60 kristaps 1598: int i, type;
1.1 kristaps 1599:
1600: /*
1601: * This is fairly tricky due primarily to crappy documentation.
1602: * If -ragged or -filled are specified, the block does nothing
1603: * but change the indentation.
1604: *
1605: * If, on the other hand, -unfilled or -literal are specified,
1606: * then the game changes. Text is printed exactly as entered in
1607: * the display: if a macro line, a newline is appended to the
1608: * line. Blank lines are allowed.
1609: */
1610:
1.54 kristaps 1611: if (MDOC_BLOCK == node->type) {
1612: fmt_block_vspace(p, node, node);
1613: return(1);
1614: } else if (MDOC_BODY != node->type)
1.1 kristaps 1615: return(1);
1616:
1.60 kristaps 1617: assert(node->parent->args);
1.11 kristaps 1618:
1.60 kristaps 1619: for (type = -1, i = 0; -1 == type &&
1.1 kristaps 1620: i < (int)node->parent->args->argc; i++) {
1621: switch (node->parent->args->argv[i].arg) {
1622: case (MDOC_Ragged):
1623: /* FALLTHROUGH */
1624: case (MDOC_Filled):
1625: /* FALLTHROUGH */
1626: case (MDOC_Unfilled):
1627: /* FALLTHROUGH */
1628: case (MDOC_Literal):
1629: type = node->parent->args->argv[i].arg;
1630: break;
1631: default:
1632: break;
1633: }
1634: }
1.60 kristaps 1635:
1636: assert(type > -1);
1.1 kristaps 1637:
1638: i = arg_getattr(MDOC_Offset, node->parent);
1.60 kristaps 1639: if (-1 != i)
1.1 kristaps 1640: p->offset += arg_offset(&node->parent->args->argv[i]);
1641:
1642: switch (type) {
1643: case (MDOC_Literal):
1644: /* FALLTHROUGH */
1645: case (MDOC_Unfilled):
1646: break;
1647: default:
1648: return(1);
1649: }
1650:
1651: for (node = node->child; node; node = node->next) {
1.60 kristaps 1652: p->flags |= TERMP_NOSPACE;
1653: print_node(p, pair, meta, node);
1654: if (node->next)
1.1 kristaps 1655: term_flushln(p);
1656: }
1657:
1658: return(0);
1659: }
1660:
1661:
1662: /* ARGSUSED */
1663: static void
1664: termp_bd_post(DECL_ARGS)
1665: {
1666:
1667: if (MDOC_BODY != node->type)
1668: return;
1.60 kristaps 1669: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1670: term_flushln(p);
1671: }
1672:
1673:
1674: /* ARGSUSED */
1675: static int
1676: termp_qq_pre(DECL_ARGS)
1677: {
1678:
1679: if (MDOC_BODY != node->type)
1680: return(1);
1681: term_word(p, "\"");
1682: p->flags |= TERMP_NOSPACE;
1683: return(1);
1684: }
1685:
1686:
1687: /* ARGSUSED */
1688: static void
1689: termp_qq_post(DECL_ARGS)
1690: {
1691:
1692: if (MDOC_BODY != node->type)
1693: return;
1694: p->flags |= TERMP_NOSPACE;
1695: term_word(p, "\"");
1696: }
1697:
1698:
1699: /* ARGSUSED */
1700: static void
1701: termp_bx_post(DECL_ARGS)
1702: {
1703:
1704: if (node->child)
1705: p->flags |= TERMP_NOSPACE;
1706: term_word(p, "BSD");
1707: }
1708:
1709:
1710: /* ARGSUSED */
1711: static int
1.26 kristaps 1712: termp_xx_pre(DECL_ARGS)
1.1 kristaps 1713: {
1.26 kristaps 1714: const char *pp;
1.1 kristaps 1715:
1.26 kristaps 1716: pp = NULL;
1717: switch (node->tok) {
1718: case (MDOC_Bsx):
1719: pp = "BSDI BSD/OS";
1720: break;
1721: case (MDOC_Dx):
1722: pp = "DragonFlyBSD";
1723: break;
1724: case (MDOC_Fx):
1725: pp = "FreeBSD";
1726: break;
1727: case (MDOC_Nx):
1728: pp = "NetBSD";
1729: break;
1730: case (MDOC_Ox):
1731: pp = "OpenBSD";
1732: break;
1733: case (MDOC_Ux):
1734: pp = "UNIX";
1735: break;
1736: default:
1737: break;
1738: }
1.1 kristaps 1739:
1.26 kristaps 1740: assert(pp);
1741: term_word(p, pp);
1.1 kristaps 1742: return(1);
1743: }
1744:
1745:
1746: /* ARGSUSED */
1747: static int
1748: termp_sq_pre(DECL_ARGS)
1749: {
1750:
1751: if (MDOC_BODY != node->type)
1752: return(1);
1753: term_word(p, "\\(oq");
1754: p->flags |= TERMP_NOSPACE;
1755: return(1);
1756: }
1757:
1758:
1759: /* ARGSUSED */
1760: static void
1761: termp_sq_post(DECL_ARGS)
1762: {
1763:
1764: if (MDOC_BODY != node->type)
1765: return;
1766: p->flags |= TERMP_NOSPACE;
1767: term_word(p, "\\(aq");
1768: }
1769:
1770:
1771: /* ARGSUSED */
1772: static int
1773: termp_pf_pre(DECL_ARGS)
1774: {
1775:
1776: p->flags |= TERMP_IGNDELIM;
1777: return(1);
1778: }
1779:
1780:
1781: /* ARGSUSED */
1782: static void
1783: termp_pf_post(DECL_ARGS)
1784: {
1785:
1786: p->flags &= ~TERMP_IGNDELIM;
1787: p->flags |= TERMP_NOSPACE;
1788: }
1789:
1790:
1791: /* ARGSUSED */
1792: static int
1793: termp_ss_pre(DECL_ARGS)
1794: {
1795:
1796: switch (node->type) {
1797: case (MDOC_BLOCK):
1798: term_newln(p);
1799: if (node->prev)
1800: term_vspace(p);
1801: break;
1802: case (MDOC_HEAD):
1.69 kristaps 1803: p->bold++;
1.5 kristaps 1804: p->offset = HALFINDENT;
1.1 kristaps 1805: break;
1806: default:
1807: break;
1808: }
1809:
1810: return(1);
1811: }
1812:
1813:
1814: /* ARGSUSED */
1815: static void
1816: termp_ss_post(DECL_ARGS)
1817: {
1818:
1.29 kristaps 1819: if (MDOC_HEAD == node->type)
1.1 kristaps 1820: term_newln(p);
1821: }
1822:
1823:
1824: /* ARGSUSED */
1825: static int
1826: termp_cd_pre(DECL_ARGS)
1827: {
1828:
1.69 kristaps 1829: p->bold++;
1.1 kristaps 1830: term_newln(p);
1831: return(1);
1832: }
1833:
1834:
1835: /* ARGSUSED */
1836: static int
1837: termp_in_pre(DECL_ARGS)
1838: {
1839:
1.69 kristaps 1840: p->bold++;
1.22 kristaps 1841: if (SEC_SYNOPSIS == node->sec)
1842: term_word(p, "#include");
1843:
1.1 kristaps 1844: term_word(p, "<");
1845: p->flags |= TERMP_NOSPACE;
1846: return(1);
1847: }
1848:
1849:
1850: /* ARGSUSED */
1851: static void
1852: termp_in_post(DECL_ARGS)
1853: {
1854:
1.69 kristaps 1855: p->bold++;
1.1 kristaps 1856: term_word(p, ">");
1.69 kristaps 1857: p->bold--;
1.1 kristaps 1858:
1859: if (SEC_SYNOPSIS != node->sec)
1860: return;
1.22 kristaps 1861:
1862: term_newln(p);
1863: /*
1864: * XXX Not entirely correct. If `.In foo bar' is specified in
1865: * the SYNOPSIS section, then it produces a single break after
1866: * the <foo>; mandoc asserts a vertical space. Since this
1867: * construction is rarely used, I think it's fine.
1868: */
1.1 kristaps 1869: if (node->next && MDOC_In != node->next->tok)
1870: term_vspace(p);
1871: }
1872:
1873:
1874: /* ARGSUSED */
1875: static int
1.45 kristaps 1876: termp_sp_pre(DECL_ARGS)
1877: {
1878: int i, len;
1879:
1880: if (NULL == node->child) {
1881: term_vspace(p);
1882: return(0);
1883: }
1884:
1885: len = atoi(node->child->string);
1886: if (0 == len)
1887: term_newln(p);
1888: for (i = 0; i < len; i++)
1889: term_vspace(p);
1890:
1891: return(0);
1892: }
1893:
1894:
1895: /* ARGSUSED */
1896: static int
1.44 kristaps 1897: termp_br_pre(DECL_ARGS)
1898: {
1899:
1900: term_newln(p);
1901: return(1);
1902: }
1903:
1904:
1905: /* ARGSUSED */
1906: static int
1.1 kristaps 1907: termp_brq_pre(DECL_ARGS)
1908: {
1909:
1910: if (MDOC_BODY != node->type)
1911: return(1);
1912: term_word(p, "\\(lC");
1913: p->flags |= TERMP_NOSPACE;
1914: return(1);
1915: }
1916:
1917:
1918: /* ARGSUSED */
1919: static void
1920: termp_brq_post(DECL_ARGS)
1921: {
1922:
1923: if (MDOC_BODY != node->type)
1924: return;
1925: p->flags |= TERMP_NOSPACE;
1926: term_word(p, "\\(rC");
1927: }
1928:
1929:
1930: /* ARGSUSED */
1931: static int
1932: termp_bq_pre(DECL_ARGS)
1933: {
1934:
1935: if (MDOC_BODY != node->type)
1936: return(1);
1937: term_word(p, "\\(lB");
1938: p->flags |= TERMP_NOSPACE;
1939: return(1);
1940: }
1941:
1942:
1943: /* ARGSUSED */
1944: static void
1945: termp_bq_post(DECL_ARGS)
1946: {
1947:
1948: if (MDOC_BODY != node->type)
1949: return;
1950: p->flags |= TERMP_NOSPACE;
1951: term_word(p, "\\(rB");
1952: }
1953:
1954:
1955: /* ARGSUSED */
1956: static int
1957: termp_pq_pre(DECL_ARGS)
1958: {
1959:
1960: if (MDOC_BODY != node->type)
1961: return(1);
1962: term_word(p, "\\&(");
1963: p->flags |= TERMP_NOSPACE;
1964: return(1);
1965: }
1966:
1967:
1968: /* ARGSUSED */
1969: static void
1970: termp_pq_post(DECL_ARGS)
1971: {
1972:
1973: if (MDOC_BODY != node->type)
1974: return;
1975: term_word(p, ")");
1976: }
1977:
1978:
1979: /* ARGSUSED */
1980: static int
1981: termp_fo_pre(DECL_ARGS)
1982: {
1983: const struct mdoc_node *n;
1984:
1985: if (MDOC_BODY == node->type) {
1.33 kristaps 1986: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1987: term_word(p, "(");
1988: p->flags |= TERMP_NOSPACE;
1989: return(1);
1990: } else if (MDOC_HEAD != node->type)
1991: return(1);
1992:
1.69 kristaps 1993: p->bold++;
1.1 kristaps 1994: for (n = node->child; n; n = n->next) {
1.11 kristaps 1995: assert(MDOC_TEXT == n->type);
1.1 kristaps 1996: term_word(p, n->string);
1997: }
1.69 kristaps 1998: p->bold--;
1.1 kristaps 1999:
2000: return(0);
2001: }
2002:
2003:
2004: /* ARGSUSED */
2005: static void
2006: termp_fo_post(DECL_ARGS)
2007: {
2008:
2009: if (MDOC_BODY != node->type)
2010: return;
2011: p->flags |= TERMP_NOSPACE;
2012: term_word(p, ")");
2013: p->flags |= TERMP_NOSPACE;
2014: term_word(p, ";");
2015: term_newln(p);
2016: }
2017:
2018:
2019: /* ARGSUSED */
2020: static int
2021: termp_bf_pre(DECL_ARGS)
2022: {
2023: const struct mdoc_node *n;
2024:
1.28 kristaps 2025: if (MDOC_HEAD == node->type)
1.1 kristaps 2026: return(0);
1.28 kristaps 2027: else if (MDOC_BLOCK != node->type)
1.1 kristaps 2028: return(1);
2029:
2030: if (NULL == (n = node->head->child)) {
2031: if (arg_hasattr(MDOC_Emphasis, node))
1.69 kristaps 2032: p->under++;
1.1 kristaps 2033: else if (arg_hasattr(MDOC_Symbolic, node))
1.69 kristaps 2034: p->bold++;
1.1 kristaps 2035:
2036: return(1);
2037: }
2038:
1.11 kristaps 2039: assert(MDOC_TEXT == n->type);
1.1 kristaps 2040: if (0 == strcmp("Em", n->string))
1.69 kristaps 2041: p->under++;
1.1 kristaps 2042: else if (0 == strcmp("Sy", n->string))
1.69 kristaps 2043: p->bold++;
1.1 kristaps 2044:
2045: return(1);
2046: }
2047:
2048:
2049: /* ARGSUSED */
2050: static int
2051: termp_sm_pre(DECL_ARGS)
2052: {
2053:
1.11 kristaps 2054: assert(node->child && MDOC_TEXT == node->child->type);
1.1 kristaps 2055: if (0 == strcmp("on", node->child->string)) {
2056: p->flags &= ~TERMP_NONOSPACE;
2057: p->flags &= ~TERMP_NOSPACE;
2058: } else
2059: p->flags |= TERMP_NONOSPACE;
2060:
2061: return(0);
2062: }
2063:
2064:
2065: /* ARGSUSED */
2066: static int
2067: termp_ap_pre(DECL_ARGS)
2068: {
2069:
2070: p->flags |= TERMP_NOSPACE;
2071: term_word(p, "\\(aq");
2072: p->flags |= TERMP_NOSPACE;
2073: return(1);
2074: }
2075:
2076:
2077: /* ARGSUSED */
2078: static void
2079: termp____post(DECL_ARGS)
2080: {
2081:
2082: p->flags |= TERMP_NOSPACE;
2083: term_word(p, node->next ? "," : ".");
2084: }
2085:
2086:
2087: /* ARGSUSED */
2088: static int
2089: termp_lk_pre(DECL_ARGS)
2090: {
2091: const struct mdoc_node *n;
2092:
1.11 kristaps 2093: assert(node->child);
2094: n = node->child;
1.1 kristaps 2095:
1.12 kristaps 2096: if (NULL == n->next) {
1.69 kristaps 2097: p->under++;
1.12 kristaps 2098: return(1);
2099: }
2100:
1.69 kristaps 2101: p->under++;
1.1 kristaps 2102: term_word(p, n->string);
2103: p->flags |= TERMP_NOSPACE;
2104: term_word(p, ":");
1.69 kristaps 2105: p->under--;
1.1 kristaps 2106:
1.69 kristaps 2107: p->bold++;
1.12 kristaps 2108: for (n = n->next; n; n = n->next)
1.1 kristaps 2109: term_word(p, n->string);
1.69 kristaps 2110: p->bold--;
1.12 kristaps 2111:
1.1 kristaps 2112: return(0);
2113: }
2114:
2115:
2116: /* ARGSUSED */
2117: static int
1.69 kristaps 2118: termp_under_pre(DECL_ARGS)
1.1 kristaps 2119: {
2120:
1.69 kristaps 2121: p->under++;
1.1 kristaps 2122: return(1);
2123: }
CVSweb