Annotation of mandoc/mdoc_term.c, Revision 1.72
1.72 ! kristaps 1: /* $Id: mdoc_term.c,v 1.71 2009/09/17 13:17:30 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:
523: static int
524: arg_listtype(const struct mdoc_node *n)
525: {
526: int i, len;
527:
528: assert(MDOC_BLOCK == n->type);
529:
530: len = (int)(n->args ? n->args->argc : 0);
531:
532: for (i = 0; i < len; i++)
533: switch (n->args->argv[i].arg) {
534: case (MDOC_Bullet):
535: /* FALLTHROUGH */
536: case (MDOC_Dash):
537: /* FALLTHROUGH */
538: case (MDOC_Enum):
539: /* FALLTHROUGH */
540: case (MDOC_Hyphen):
541: /* FALLTHROUGH */
542: case (MDOC_Tag):
543: /* FALLTHROUGH */
544: case (MDOC_Inset):
545: /* FALLTHROUGH */
546: case (MDOC_Diag):
547: /* FALLTHROUGH */
548: case (MDOC_Item):
549: /* FALLTHROUGH */
550: case (MDOC_Column):
551: /* FALLTHROUGH */
1.38 kristaps 552: case (MDOC_Hang):
553: /* FALLTHROUGH */
1.1 kristaps 554: case (MDOC_Ohang):
555: return(n->args->argv[i].arg);
556: default:
557: break;
558: }
559:
1.38 kristaps 560: return(-1);
1.1 kristaps 561: }
562:
563:
1.72 ! kristaps 564: /* FIXME: put in utility file for front-ends. */
1.1 kristaps 565: static size_t
566: arg_offset(const struct mdoc_argv *arg)
567: {
1.40 kristaps 568: int len, i;
569: const char *p;
1.1 kristaps 570:
571: assert(*arg->value);
1.40 kristaps 572: p = *arg->value;
573:
574: if (0 == strcmp(p, "left"))
1.19 kristaps 575: return(0);
1.40 kristaps 576: if (0 == strcmp(p, "indent"))
1.16 kristaps 577: return(INDENT + 1);
1.40 kristaps 578: if (0 == strcmp(p, "indent-two"))
1.20 kristaps 579: return((INDENT + 1) * 2);
1.10 kristaps 580:
1.40 kristaps 581: if (0 == (len = (int)strlen(p)))
582: return(0);
583:
584: for (i = 0; i < len - 1; i++)
585: if ( ! isdigit((u_char)p[i]))
586: break;
587:
588: if (i == len - 1)
589: if ('n' == p[len - 1] || 'm' == p[len - 1])
590: return((size_t)atoi(p));
1.11 kristaps 591:
1.40 kristaps 592: return((size_t)len);
1.1 kristaps 593: }
594:
595:
596: static int
597: arg_hasattr(int arg, const struct mdoc_node *n)
598: {
599:
600: return(-1 != arg_getattr(arg, n));
601: }
602:
603:
604: static int
605: arg_getattr(int v, const struct mdoc_node *n)
606: {
607: int val;
608:
609: return(arg_getattrs(&v, &val, 1, n) ? val : -1);
610: }
611:
612:
613: static int
614: arg_getattrs(const int *keys, int *vals,
615: size_t sz, const struct mdoc_node *n)
616: {
617: int i, j, k;
618:
619: if (NULL == n->args)
620: return(0);
621:
622: for (k = i = 0; i < (int)n->args->argc; i++)
623: for (j = 0; j < (int)sz; j++)
624: if (n->args->argv[i].arg == keys[j]) {
625: vals[j] = i;
626: k++;
627: }
628: return(k);
629: }
630:
631:
632: /* ARGSUSED */
1.54 kristaps 633: static void
1.1 kristaps 634: fmt_block_vspace(struct termp *p,
635: const struct mdoc_node *bl,
636: const struct mdoc_node *node)
637: {
638: const struct mdoc_node *n;
639:
640: term_newln(p);
641:
1.59 kristaps 642: if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Compact, bl))
1.54 kristaps 643: return;
1.63 kristaps 644: assert(node);
1.54 kristaps 645:
646: /*
647: * Search through our prior nodes. If we follow a `Ss' or `Sh',
648: * then don't vspace.
649: */
1.1 kristaps 650:
651: for (n = node; n; n = n->parent) {
652: if (MDOC_BLOCK != n->type)
653: continue;
654: if (MDOC_Ss == n->tok)
1.54 kristaps 655: return;
1.1 kristaps 656: if (MDOC_Sh == n->tok)
1.54 kristaps 657: return;
1.1 kristaps 658: if (NULL == n->prev)
659: continue;
660: break;
661: }
662:
1.54 kristaps 663: /*
1.55 kristaps 664: * XXX - not documented: a `-column' does not ever assert vspace
665: * within the list.
1.54 kristaps 666: */
667:
1.59 kristaps 668: if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Column, bl))
1.54 kristaps 669: if (node->prev && MDOC_It == node->prev->tok)
670: return;
671:
1.55 kristaps 672: /*
673: * XXX - not documented: a `-diag' without a body does not
674: * assert a vspace prior to the next element.
675: */
1.59 kristaps 676: if (MDOC_Bl == bl->tok && arg_hasattr(MDOC_Diag, bl))
1.55 kristaps 677: if (node->prev && MDOC_It == node->prev->tok) {
678: assert(node->prev->body);
679: if (NULL == node->prev->body->child)
680: return;
681: }
682:
1.54 kristaps 683: term_vspace(p);
1.1 kristaps 684: }
685:
686:
687: /* ARGSUSED */
688: static int
689: termp_dq_pre(DECL_ARGS)
690: {
691:
692: if (MDOC_BODY != node->type)
693: return(1);
694:
695: term_word(p, "\\(lq");
696: p->flags |= TERMP_NOSPACE;
697: return(1);
698: }
699:
700:
701: /* ARGSUSED */
702: static void
703: termp_dq_post(DECL_ARGS)
704: {
705:
706: if (MDOC_BODY != node->type)
707: return;
708:
709: p->flags |= TERMP_NOSPACE;
710: term_word(p, "\\(rq");
711: }
712:
713:
714: /* ARGSUSED */
715: static int
716: termp_it_pre(DECL_ARGS)
717: {
718: const struct mdoc_node *bl, *n;
719: char buf[7];
1.39 kristaps 720: int i, type, keys[3], vals[3];
1.1 kristaps 721: size_t width, offset;
722:
1.54 kristaps 723: if (MDOC_BLOCK == node->type) {
724: fmt_block_vspace(p, node->parent->parent, node);
725: return(1);
726: }
1.1 kristaps 727:
728: bl = node->parent->parent->parent;
729:
730: /* Save parent attributes. */
731:
732: pair->flag = p->flags;
733:
734: /* Get list width and offset. */
735:
736: keys[0] = MDOC_Width;
737: keys[1] = MDOC_Offset;
738: keys[2] = MDOC_Column;
739:
740: vals[0] = vals[1] = vals[2] = -1;
741:
742: width = offset = 0;
743:
744: (void)arg_getattrs(keys, vals, 3, bl);
745:
746: type = arg_listtype(bl);
1.38 kristaps 747: assert(-1 != type);
1.1 kristaps 748:
749: /* Calculate real width and offset. */
750:
751: switch (type) {
752: case (MDOC_Column):
753: if (MDOC_BODY == node->type)
754: break;
1.58 kristaps 755: /*
756: * Work around groff's column handling. The offset is
757: * equal to the sum of all widths leading to the current
758: * column (plus the -offset value). If this column
759: * exceeds the stated number of columns, the width is
760: * set as 0, else it's the stated column width (later
761: * the 0 will be adjusted to default 10 or, if in the
762: * last column case, set to stretch to the margin).
763: */
764: for (i = 0, n = node->prev; n && n &&
765: i < (int)bl->args[vals[2]].argv->sz;
766: n = n->prev, i++)
1.1 kristaps 767: offset += arg_width
768: (&bl->args->argv[vals[2]], i);
1.58 kristaps 769:
770: /* Whether exceeds maximum column. */
771: if (i < (int)bl->args[vals[2]].argv->sz)
772: width = arg_width(&bl->args->argv[vals[2]], i);
773: else
774: width = 0;
775:
1.1 kristaps 776: if (vals[1] >= 0)
777: offset += arg_offset(&bl->args->argv[vals[1]]);
778: break;
779: default:
780: if (vals[0] >= 0)
781: width = arg_width(&bl->args->argv[vals[0]], 0);
782: if (vals[1] >= 0)
1.19 kristaps 783: offset += arg_offset(&bl->args->argv[vals[1]]);
1.1 kristaps 784: break;
785: }
786:
787: /*
788: * List-type can override the width in the case of fixed-head
789: * values (bullet, dash/hyphen, enum). Tags need a non-zero
1.67 kristaps 790: * offset.
1.1 kristaps 791: */
792:
793: switch (type) {
794: case (MDOC_Bullet):
795: /* FALLTHROUGH */
796: case (MDOC_Dash):
797: /* FALLTHROUGH */
798: case (MDOC_Hyphen):
799: if (width < 4)
800: width = 4;
801: break;
1.17 kristaps 802: case (MDOC_Enum):
803: if (width < 5)
804: width = 5;
805: break;
1.38 kristaps 806: case (MDOC_Hang):
1.39 kristaps 807: if (0 == width)
808: width = 8;
809: break;
1.49 kristaps 810: case (MDOC_Column):
811: /* FALLTHROUGH */
1.1 kristaps 812: case (MDOC_Tag):
813: if (0 == width)
814: width = 10;
815: break;
816: default:
817: break;
818: }
819:
820: /*
1.17 kristaps 821: * Whitespace control. Inset bodies need an initial space,
822: * while diagonal bodies need two.
1.1 kristaps 823: */
824:
1.51 kristaps 825: p->flags |= TERMP_NOSPACE;
826:
1.1 kristaps 827: switch (type) {
1.51 kristaps 828: case (MDOC_Diag):
1.62 kristaps 829: if (MDOC_BODY == node->type)
830: term_word(p, "\\ \\ ");
1.51 kristaps 831: break;
1.1 kristaps 832: case (MDOC_Inset):
833: if (MDOC_BODY == node->type)
1.51 kristaps 834: term_word(p, "\\ ");
1.1 kristaps 835: break;
836: default:
837: break;
838: }
839:
1.51 kristaps 840: p->flags |= TERMP_NOSPACE;
841:
1.1 kristaps 842: switch (type) {
843: case (MDOC_Diag):
844: if (MDOC_HEAD == node->type)
1.69 kristaps 845: p->bold++;
1.1 kristaps 846: break;
847: default:
848: break;
849: }
850:
851: /*
852: * Pad and break control. This is the tricker part. Lists with
853: * set right-margins for the head get TERMP_NOBREAK because, if
854: * they overrun the margin, they wrap to the new margin.
855: * Correspondingly, the body for these types don't left-pad, as
856: * the head will pad out to to the right.
857: */
858:
859: switch (type) {
860: case (MDOC_Bullet):
861: /* FALLTHROUGH */
862: case (MDOC_Dash):
863: /* FALLTHROUGH */
864: case (MDOC_Enum):
865: /* FALLTHROUGH */
866: case (MDOC_Hyphen):
1.39 kristaps 867: if (MDOC_HEAD == node->type)
868: p->flags |= TERMP_NOBREAK;
869: else
870: p->flags |= TERMP_NOLPAD;
871: break;
1.38 kristaps 872: case (MDOC_Hang):
1.39 kristaps 873: if (MDOC_HEAD == node->type)
874: p->flags |= TERMP_NOBREAK;
875: else
876: p->flags |= TERMP_NOLPAD;
877:
1.59 kristaps 878: if (MDOC_HEAD != node->type)
879: break;
880:
881: /*
882: * This is ugly. If `-hang' is specified and the body
883: * is a `Bl' or `Bd', then we want basically to nullify
884: * the "overstep" effect in term_flushln() and treat
885: * this as a `-ohang' list instead.
886: */
887: if (node->next->child &&
888: (MDOC_Bl == node->next->child->tok ||
889: MDOC_Bd == node->next->child->tok)) {
890: p->flags &= ~TERMP_NOBREAK;
891: p->flags &= ~TERMP_NOLPAD;
892: } else
1.39 kristaps 893: p->flags |= TERMP_HANG;
894: break;
1.1 kristaps 895: case (MDOC_Tag):
896: if (MDOC_HEAD == node->type)
1.51 kristaps 897: p->flags |= TERMP_NOBREAK | TERMP_TWOSPACE;
1.1 kristaps 898: else
899: p->flags |= TERMP_NOLPAD;
1.38 kristaps 900:
1.39 kristaps 901: if (MDOC_HEAD != node->type)
902: break;
903: if (NULL == node->next || NULL == node->next->child)
904: p->flags |= TERMP_DANGLE;
1.1 kristaps 905: break;
906: case (MDOC_Column):
907: if (MDOC_HEAD == node->type) {
908: assert(node->next);
909: if (MDOC_BODY == node->next->type)
910: p->flags &= ~TERMP_NOBREAK;
911: else
912: p->flags |= TERMP_NOBREAK;
913: if (node->prev)
914: p->flags |= TERMP_NOLPAD;
915: }
916: break;
917: case (MDOC_Diag):
918: if (MDOC_HEAD == node->type)
919: p->flags |= TERMP_NOBREAK;
920: break;
921: default:
922: break;
923: }
924:
925: /*
926: * Margin control. Set-head-width lists have their right
927: * margins shortened. The body for these lists has the offset
928: * necessarily lengthened. Everybody gets the offset.
929: */
930:
931: p->offset += offset;
932:
933: switch (type) {
1.59 kristaps 934: case (MDOC_Hang):
935: /*
936: * Same stipulation as above, regarding `-hang'. We
937: * don't want to recalculate rmargin and offsets when
938: * using `Bd' or `Bl' within `-hang' overstep lists.
939: */
940: if (MDOC_HEAD == node->type && node->next->child &&
941: (MDOC_Bl == node->next->child->tok ||
942: MDOC_Bd == node->next->child->tok))
943: break;
944: /* FALLTHROUGH */
1.1 kristaps 945: case (MDOC_Bullet):
946: /* FALLTHROUGH */
947: case (MDOC_Dash):
948: /* FALLTHROUGH */
949: case (MDOC_Enum):
950: /* FALLTHROUGH */
951: case (MDOC_Hyphen):
952: /* FALLTHROUGH */
953: case (MDOC_Tag):
1.49 kristaps 954: assert(width);
1.1 kristaps 955: if (MDOC_HEAD == node->type)
956: p->rmargin = p->offset + width;
957: else
958: p->offset += width;
959: break;
960: case (MDOC_Column):
1.49 kristaps 961: assert(width);
1.1 kristaps 962: p->rmargin = p->offset + width;
1.50 kristaps 963: /*
964: * XXX - this behaviour is not documented: the
965: * right-most column is filled to the right margin.
966: */
967: if (MDOC_HEAD == node->type &&
968: MDOC_BODY == node->next->type)
969: p->rmargin = p->maxrmargin;
1.1 kristaps 970: break;
971: default:
972: break;
973: }
974:
975: /*
976: * The dash, hyphen, bullet and enum lists all have a special
1.15 kristaps 977: * HEAD character (temporarily bold, in some cases).
1.1 kristaps 978: */
979:
980: if (MDOC_HEAD == node->type)
981: switch (type) {
982: case (MDOC_Bullet):
1.69 kristaps 983: p->bold++;
1.1 kristaps 984: term_word(p, "\\[bu]");
1.69 kristaps 985: p->bold--;
1.1 kristaps 986: break;
987: case (MDOC_Dash):
988: /* FALLTHROUGH */
989: case (MDOC_Hyphen):
1.69 kristaps 990: p->bold++;
1.27 kristaps 991: term_word(p, "\\(hy");
1.69 kristaps 992: p->bold--;
1.1 kristaps 993: break;
994: case (MDOC_Enum):
995: (pair->ppair->ppair->count)++;
996: (void)snprintf(buf, sizeof(buf), "%d.",
997: pair->ppair->ppair->count);
998: term_word(p, buf);
999: break;
1000: default:
1001: break;
1002: }
1003:
1004: /*
1005: * If we're not going to process our children, indicate so here.
1006: */
1007:
1008: switch (type) {
1009: case (MDOC_Bullet):
1010: /* FALLTHROUGH */
1011: case (MDOC_Item):
1012: /* FALLTHROUGH */
1013: case (MDOC_Dash):
1014: /* FALLTHROUGH */
1015: case (MDOC_Hyphen):
1016: /* FALLTHROUGH */
1017: case (MDOC_Enum):
1018: if (MDOC_HEAD == node->type)
1019: return(0);
1020: break;
1021: case (MDOC_Column):
1022: if (MDOC_BODY == node->type)
1023: return(0);
1024: break;
1025: default:
1026: break;
1027: }
1028:
1029: return(1);
1030: }
1031:
1032:
1033: /* ARGSUSED */
1034: static void
1035: termp_it_post(DECL_ARGS)
1036: {
1037: int type;
1038:
1039: if (MDOC_BODY != node->type && MDOC_HEAD != node->type)
1040: return;
1041:
1042: type = arg_listtype(node->parent->parent->parent);
1.38 kristaps 1043: assert(-1 != type);
1.1 kristaps 1044:
1045: switch (type) {
1046: case (MDOC_Item):
1047: /* FALLTHROUGH */
1.53 kristaps 1048: case (MDOC_Diag):
1049: /* FALLTHROUGH */
1.1 kristaps 1050: case (MDOC_Inset):
1051: if (MDOC_BODY == node->type)
1052: term_flushln(p);
1053: break;
1054: case (MDOC_Column):
1055: if (MDOC_HEAD == node->type)
1056: term_flushln(p);
1057: break;
1058: default:
1059: term_flushln(p);
1060: break;
1061: }
1062:
1063: p->flags = pair->flag;
1064: }
1065:
1066:
1067: /* ARGSUSED */
1068: static int
1069: termp_nm_pre(DECL_ARGS)
1070: {
1071:
1072: if (SEC_SYNOPSIS == node->sec)
1073: term_newln(p);
1.69 kristaps 1074: p->bold++;
1.1 kristaps 1075: if (NULL == node->child)
1076: term_word(p, meta->name);
1077: return(1);
1078: }
1079:
1080:
1081: /* ARGSUSED */
1082: static int
1083: termp_fl_pre(DECL_ARGS)
1084: {
1085:
1.69 kristaps 1086: p->bold++;
1.1 kristaps 1087: term_word(p, "\\-");
1088: p->flags |= TERMP_NOSPACE;
1089: return(1);
1090: }
1091:
1092:
1093: /* ARGSUSED */
1094: static int
1.61 kristaps 1095: termp_an_pre(DECL_ARGS)
1096: {
1097:
1098: if (NULL == node->child)
1099: return(1);
1100:
1101: /*
1102: * XXX: this is poorly documented. If not in the AUTHORS
1103: * section, `An -split' will cause newlines to occur before the
1104: * author name. If in the AUTHORS section, by default, the
1105: * first `An' invocation is nosplit, then all subsequent ones,
1106: * regardless of whether interspersed with other macros/text,
1107: * are split. -split, in this case, will override the condition
1108: * of the implied first -nosplit.
1109: */
1110:
1111: if (node->sec == SEC_AUTHORS) {
1112: if ( ! (TERMP_ANPREC & p->flags)) {
1113: if (TERMP_SPLIT & p->flags)
1114: term_newln(p);
1115: return(1);
1116: }
1117: if (TERMP_NOSPLIT & p->flags)
1118: return(1);
1119: term_newln(p);
1120: return(1);
1121: }
1122:
1123: if (TERMP_SPLIT & p->flags)
1124: term_newln(p);
1125:
1126: return(1);
1127: }
1128:
1129:
1130: /* ARGSUSED */
1131: static void
1132: termp_an_post(DECL_ARGS)
1133: {
1134:
1135: if (node->child) {
1136: if (SEC_AUTHORS == node->sec)
1137: p->flags |= TERMP_ANPREC;
1138: return;
1139: }
1140:
1141: if (arg_getattr(MDOC_Split, node) > -1) {
1142: p->flags &= ~TERMP_NOSPLIT;
1143: p->flags |= TERMP_SPLIT;
1144: } else {
1145: p->flags &= ~TERMP_SPLIT;
1146: p->flags |= TERMP_NOSPLIT;
1147: }
1148:
1149: }
1150:
1151:
1152: /* ARGSUSED */
1153: static int
1.1 kristaps 1154: termp_ns_pre(DECL_ARGS)
1155: {
1156:
1157: p->flags |= TERMP_NOSPACE;
1158: return(1);
1159: }
1160:
1161:
1162: /* ARGSUSED */
1163: static int
1164: termp_pp_pre(DECL_ARGS)
1165: {
1166:
1167: term_vspace(p);
1168: return(1);
1169: }
1170:
1171:
1172: /* ARGSUSED */
1173: static int
1174: termp_rs_pre(DECL_ARGS)
1175: {
1176:
1177: if (MDOC_BLOCK == node->type && node->prev)
1178: term_vspace(p);
1179: return(1);
1180: }
1181:
1182:
1183: /* ARGSUSED */
1184: static int
1185: termp_rv_pre(DECL_ARGS)
1186: {
1.68 kristaps 1187: const struct mdoc_node *nn;
1.1 kristaps 1188:
1189: term_newln(p);
1190: term_word(p, "The");
1191:
1.68 kristaps 1192: nn = node->child;
1193: assert(nn);
1194: for ( ; nn; nn = nn->next) {
1.69 kristaps 1195: p->bold++;
1.68 kristaps 1196: term_word(p, nn->string);
1.69 kristaps 1197: p->bold--;
1.68 kristaps 1198: p->flags |= TERMP_NOSPACE;
1199: if (nn->next && NULL == nn->next->next)
1200: term_word(p, "(), and");
1201: else if (nn->next)
1202: term_word(p, "(),");
1203: else
1204: term_word(p, "()");
1205: }
1206:
1207: if (node->child->next)
1208: term_word(p, "functions return");
1209: else
1210: term_word(p, "function returns");
1.1 kristaps 1211:
1.68 kristaps 1212: term_word(p, "the value 0 if successful; otherwise the value "
1213: "-1 is returned and the global variable");
1.1 kristaps 1214:
1.69 kristaps 1215: p->under++;
1.1 kristaps 1216: term_word(p, "errno");
1.69 kristaps 1217: p->under--;
1.1 kristaps 1218:
1219: term_word(p, "is set to indicate the error.");
1220:
1.68 kristaps 1221: return(0);
1.1 kristaps 1222: }
1223:
1224:
1225: /* ARGSUSED */
1226: static int
1227: termp_ex_pre(DECL_ARGS)
1228: {
1.68 kristaps 1229: const struct mdoc_node *nn;
1.1 kristaps 1230:
1.68 kristaps 1231: term_word(p, "The");
1.1 kristaps 1232:
1.68 kristaps 1233: nn = node->child;
1234: assert(nn);
1235: for ( ; nn; nn = nn->next) {
1.69 kristaps 1236: p->bold++;
1.68 kristaps 1237: term_word(p, nn->string);
1.69 kristaps 1238: p->bold--;
1.68 kristaps 1239: p->flags |= TERMP_NOSPACE;
1240: if (nn->next && NULL == nn->next->next)
1241: term_word(p, ", and");
1242: else if (nn->next)
1243: term_word(p, ",");
1244: else
1245: p->flags &= ~TERMP_NOSPACE;
1246: }
1247:
1248: if (node->child->next)
1249: term_word(p, "utilities exit");
1250: else
1251: term_word(p, "utility exits");
1252:
1253: term_word(p, "0 on success, and >0 if an error occurs.");
1.1 kristaps 1254:
1.68 kristaps 1255: return(0);
1.1 kristaps 1256: }
1257:
1258:
1259: /* ARGSUSED */
1260: static int
1261: termp_nd_pre(DECL_ARGS)
1262: {
1.25 kristaps 1263:
1264: if (MDOC_BODY != node->type)
1265: return(1);
1.30 kristaps 1266:
1.27 kristaps 1267: #if defined(__OpenBSD__) || defined(__linux__)
1.41 kristaps 1268: term_word(p, "\\(en");
1.23 kristaps 1269: #else
1270: term_word(p, "\\(em");
1271: #endif
1.1 kristaps 1272: return(1);
1273: }
1274:
1275:
1276: /* ARGSUSED */
1277: static void
1278: termp_bl_post(DECL_ARGS)
1279: {
1280:
1281: if (MDOC_BLOCK == node->type)
1282: term_newln(p);
1283: }
1284:
1285:
1286: /* ARGSUSED */
1287: static void
1288: termp_op_post(DECL_ARGS)
1289: {
1290:
1291: if (MDOC_BODY != node->type)
1292: return;
1293: p->flags |= TERMP_NOSPACE;
1294: term_word(p, "\\(rB");
1295: }
1296:
1297:
1298: /* ARGSUSED */
1299: static int
1300: termp_xr_pre(DECL_ARGS)
1301: {
1302: const struct mdoc_node *n;
1303:
1.11 kristaps 1304: assert(node->child && MDOC_TEXT == node->child->type);
1305: n = node->child;
1306:
1.1 kristaps 1307: term_word(p, n->string);
1308: if (NULL == (n = n->next))
1309: return(0);
1310: p->flags |= TERMP_NOSPACE;
1311: term_word(p, "(");
1312: p->flags |= TERMP_NOSPACE;
1313: term_word(p, n->string);
1314: p->flags |= TERMP_NOSPACE;
1315: term_word(p, ")");
1316: return(0);
1317: }
1318:
1319:
1320: /* ARGSUSED */
1321: static void
1322: termp_vt_post(DECL_ARGS)
1323: {
1324:
1.32 kristaps 1325: if (node->sec != SEC_SYNOPSIS)
1326: return;
1327: if (node->next && MDOC_Vt == node->next->tok)
1328: term_newln(p);
1329: else if (node->next)
1.1 kristaps 1330: term_vspace(p);
1331: }
1332:
1333:
1334: /* ARGSUSED */
1335: static int
1.69 kristaps 1336: termp_bold_pre(DECL_ARGS)
1.1 kristaps 1337: {
1338:
1.69 kristaps 1339: p->bold++;
1.1 kristaps 1340: return(1);
1341: }
1342:
1343:
1344: /* ARGSUSED */
1345: static void
1346: termp_fd_post(DECL_ARGS)
1347: {
1348:
1349: if (node->sec != SEC_SYNOPSIS)
1350: return;
1.29 kristaps 1351:
1.1 kristaps 1352: term_newln(p);
1353: if (node->next && MDOC_Fd != node->next->tok)
1354: term_vspace(p);
1355: }
1356:
1357:
1358: /* ARGSUSED */
1359: static int
1360: termp_sh_pre(DECL_ARGS)
1361: {
1.64 kristaps 1362: /*
1.65 kristaps 1363: * XXX: undocumented: using two `Sh' macros in sequence has no
1364: * vspace between calls, only a newline.
1.64 kristaps 1365: */
1.1 kristaps 1366: switch (node->type) {
1.65 kristaps 1367: case (MDOC_BLOCK):
1368: if (node->prev && MDOC_Sh == node->prev->tok)
1369: if (NULL == node->prev->body->child)
1370: break;
1371: term_vspace(p);
1372: break;
1.1 kristaps 1373: case (MDOC_HEAD):
1.69 kristaps 1374: p->bold++;
1.1 kristaps 1375: break;
1376: case (MDOC_BODY):
1377: p->offset = INDENT;
1378: break;
1379: default:
1380: break;
1381: }
1382: return(1);
1383: }
1384:
1385:
1386: /* ARGSUSED */
1387: static void
1388: termp_sh_post(DECL_ARGS)
1389: {
1390:
1391: switch (node->type) {
1392: case (MDOC_HEAD):
1393: term_newln(p);
1394: break;
1395: case (MDOC_BODY):
1396: term_newln(p);
1397: p->offset = 0;
1398: break;
1399: default:
1400: break;
1401: }
1402: }
1403:
1404:
1405: /* ARGSUSED */
1406: static int
1407: termp_op_pre(DECL_ARGS)
1408: {
1409:
1410: switch (node->type) {
1411: case (MDOC_BODY):
1412: term_word(p, "\\(lB");
1413: p->flags |= TERMP_NOSPACE;
1414: break;
1415: default:
1416: break;
1417: }
1418: return(1);
1419: }
1420:
1421:
1422: /* ARGSUSED */
1423: static int
1424: termp_bt_pre(DECL_ARGS)
1425: {
1426:
1427: term_word(p, "is currently in beta test.");
1428: return(1);
1429: }
1430:
1431:
1432: /* ARGSUSED */
1433: static void
1434: termp_lb_post(DECL_ARGS)
1435: {
1436:
1437: term_newln(p);
1438: }
1439:
1440:
1441: /* ARGSUSED */
1442: static int
1443: termp_ud_pre(DECL_ARGS)
1444: {
1445:
1446: term_word(p, "currently under development.");
1447: return(1);
1448: }
1449:
1450:
1451: /* ARGSUSED */
1452: static int
1453: termp_d1_pre(DECL_ARGS)
1454: {
1455:
1.4 kristaps 1456: if (MDOC_BLOCK != node->type)
1.1 kristaps 1457: return(1);
1458: term_newln(p);
1.29 kristaps 1459: p->offset += (INDENT + 1);
1.1 kristaps 1460: return(1);
1461: }
1462:
1463:
1464: /* ARGSUSED */
1465: static void
1466: termp_d1_post(DECL_ARGS)
1467: {
1468:
1.4 kristaps 1469: if (MDOC_BLOCK != node->type)
1.1 kristaps 1470: return;
1471: term_newln(p);
1472: }
1473:
1474:
1475: /* ARGSUSED */
1476: static int
1477: termp_aq_pre(DECL_ARGS)
1478: {
1479:
1480: if (MDOC_BODY != node->type)
1481: return(1);
1482: term_word(p, "\\(la");
1483: p->flags |= TERMP_NOSPACE;
1484: return(1);
1485: }
1486:
1487:
1488: /* ARGSUSED */
1489: static void
1490: termp_aq_post(DECL_ARGS)
1491: {
1492:
1493: if (MDOC_BODY != node->type)
1494: return;
1495: p->flags |= TERMP_NOSPACE;
1496: term_word(p, "\\(ra");
1497: }
1498:
1499:
1500: /* ARGSUSED */
1501: static int
1502: termp_ft_pre(DECL_ARGS)
1503: {
1504:
1505: if (SEC_SYNOPSIS == node->sec)
1506: if (node->prev && MDOC_Fo == node->prev->tok)
1507: term_vspace(p);
1.69 kristaps 1508: p->under++;
1.1 kristaps 1509: return(1);
1510: }
1511:
1512:
1513: /* ARGSUSED */
1514: static void
1515: termp_ft_post(DECL_ARGS)
1516: {
1517:
1518: if (SEC_SYNOPSIS == node->sec)
1519: term_newln(p);
1520: }
1521:
1522:
1523: /* ARGSUSED */
1524: static int
1525: termp_fn_pre(DECL_ARGS)
1526: {
1527: const struct mdoc_node *n;
1528:
1.11 kristaps 1529: assert(node->child && MDOC_TEXT == node->child->type);
1.1 kristaps 1530:
1531: /* FIXME: can be "type funcname" "type varname"... */
1532:
1.69 kristaps 1533: p->bold++;
1.1 kristaps 1534: term_word(p, node->child->string);
1.69 kristaps 1535: p->bold--;
1.1 kristaps 1536:
1537: p->flags |= TERMP_NOSPACE;
1538: term_word(p, "(");
1539:
1540: for (n = node->child->next; n; n = n->next) {
1.69 kristaps 1541: p->under++;
1.1 kristaps 1542: term_word(p, n->string);
1.69 kristaps 1543: p->under--;
1.1 kristaps 1544: if (n->next)
1545: term_word(p, ",");
1546: }
1547:
1548: term_word(p, ")");
1549:
1550: if (SEC_SYNOPSIS == node->sec)
1551: term_word(p, ";");
1552:
1553: return(0);
1554: }
1555:
1556:
1557: /* ARGSUSED */
1558: static void
1559: termp_fn_post(DECL_ARGS)
1560: {
1561:
1562: if (node->sec == SEC_SYNOPSIS && node->next)
1563: term_vspace(p);
1564: }
1565:
1566:
1567: /* ARGSUSED */
1568: static int
1569: termp_fa_pre(DECL_ARGS)
1570: {
1571: struct mdoc_node *n;
1572:
1573: if (node->parent->tok != MDOC_Fo) {
1.69 kristaps 1574: p->under++;
1.1 kristaps 1575: return(1);
1576: }
1577:
1578: for (n = node->child; n; n = n->next) {
1.69 kristaps 1579: p->under++;
1.1 kristaps 1580: term_word(p, n->string);
1.69 kristaps 1581: p->under--;
1.1 kristaps 1582: if (n->next)
1583: term_word(p, ",");
1584: }
1585:
1586: if (node->child && node->next && node->next->tok == MDOC_Fa)
1587: term_word(p, ",");
1588:
1589: return(0);
1590: }
1591:
1592:
1593: /* ARGSUSED */
1594: static int
1595: termp_bd_pre(DECL_ARGS)
1596: {
1.60 kristaps 1597: int i, type;
1.1 kristaps 1598:
1599: /*
1600: * This is fairly tricky due primarily to crappy documentation.
1601: * If -ragged or -filled are specified, the block does nothing
1602: * but change the indentation.
1603: *
1604: * If, on the other hand, -unfilled or -literal are specified,
1605: * then the game changes. Text is printed exactly as entered in
1606: * the display: if a macro line, a newline is appended to the
1607: * line. Blank lines are allowed.
1608: */
1609:
1.54 kristaps 1610: if (MDOC_BLOCK == node->type) {
1611: fmt_block_vspace(p, node, node);
1612: return(1);
1613: } else if (MDOC_BODY != node->type)
1.1 kristaps 1614: return(1);
1615:
1.60 kristaps 1616: assert(node->parent->args);
1.11 kristaps 1617:
1.60 kristaps 1618: for (type = -1, i = 0; -1 == type &&
1.1 kristaps 1619: i < (int)node->parent->args->argc; i++) {
1620: switch (node->parent->args->argv[i].arg) {
1621: case (MDOC_Ragged):
1622: /* FALLTHROUGH */
1623: case (MDOC_Filled):
1624: /* FALLTHROUGH */
1625: case (MDOC_Unfilled):
1626: /* FALLTHROUGH */
1627: case (MDOC_Literal):
1628: type = node->parent->args->argv[i].arg;
1629: break;
1630: default:
1631: break;
1632: }
1633: }
1.60 kristaps 1634:
1635: assert(type > -1);
1.1 kristaps 1636:
1637: i = arg_getattr(MDOC_Offset, node->parent);
1.60 kristaps 1638: if (-1 != i)
1.1 kristaps 1639: p->offset += arg_offset(&node->parent->args->argv[i]);
1640:
1641: switch (type) {
1642: case (MDOC_Literal):
1643: /* FALLTHROUGH */
1644: case (MDOC_Unfilled):
1645: break;
1646: default:
1647: return(1);
1648: }
1649:
1650: for (node = node->child; node; node = node->next) {
1.60 kristaps 1651: p->flags |= TERMP_NOSPACE;
1652: print_node(p, pair, meta, node);
1653: if (node->next)
1.1 kristaps 1654: term_flushln(p);
1655: }
1656:
1657: return(0);
1658: }
1659:
1660:
1661: /* ARGSUSED */
1662: static void
1663: termp_bd_post(DECL_ARGS)
1664: {
1665:
1666: if (MDOC_BODY != node->type)
1667: return;
1.60 kristaps 1668: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1669: term_flushln(p);
1670: }
1671:
1672:
1673: /* ARGSUSED */
1674: static int
1675: termp_qq_pre(DECL_ARGS)
1676: {
1677:
1678: if (MDOC_BODY != node->type)
1679: return(1);
1680: term_word(p, "\"");
1681: p->flags |= TERMP_NOSPACE;
1682: return(1);
1683: }
1684:
1685:
1686: /* ARGSUSED */
1687: static void
1688: termp_qq_post(DECL_ARGS)
1689: {
1690:
1691: if (MDOC_BODY != node->type)
1692: return;
1693: p->flags |= TERMP_NOSPACE;
1694: term_word(p, "\"");
1695: }
1696:
1697:
1698: /* ARGSUSED */
1699: static void
1700: termp_bx_post(DECL_ARGS)
1701: {
1702:
1703: if (node->child)
1704: p->flags |= TERMP_NOSPACE;
1705: term_word(p, "BSD");
1706: }
1707:
1708:
1709: /* ARGSUSED */
1710: static int
1.26 kristaps 1711: termp_xx_pre(DECL_ARGS)
1.1 kristaps 1712: {
1.26 kristaps 1713: const char *pp;
1.1 kristaps 1714:
1.26 kristaps 1715: pp = NULL;
1716: switch (node->tok) {
1717: case (MDOC_Bsx):
1718: pp = "BSDI BSD/OS";
1719: break;
1720: case (MDOC_Dx):
1721: pp = "DragonFlyBSD";
1722: break;
1723: case (MDOC_Fx):
1724: pp = "FreeBSD";
1725: break;
1726: case (MDOC_Nx):
1727: pp = "NetBSD";
1728: break;
1729: case (MDOC_Ox):
1730: pp = "OpenBSD";
1731: break;
1732: case (MDOC_Ux):
1733: pp = "UNIX";
1734: break;
1735: default:
1736: break;
1737: }
1.1 kristaps 1738:
1.26 kristaps 1739: assert(pp);
1740: term_word(p, pp);
1.1 kristaps 1741: return(1);
1742: }
1743:
1744:
1745: /* ARGSUSED */
1746: static int
1747: termp_sq_pre(DECL_ARGS)
1748: {
1749:
1750: if (MDOC_BODY != node->type)
1751: return(1);
1752: term_word(p, "\\(oq");
1753: p->flags |= TERMP_NOSPACE;
1754: return(1);
1755: }
1756:
1757:
1758: /* ARGSUSED */
1759: static void
1760: termp_sq_post(DECL_ARGS)
1761: {
1762:
1763: if (MDOC_BODY != node->type)
1764: return;
1765: p->flags |= TERMP_NOSPACE;
1766: term_word(p, "\\(aq");
1767: }
1768:
1769:
1770: /* ARGSUSED */
1771: static int
1772: termp_pf_pre(DECL_ARGS)
1773: {
1774:
1775: p->flags |= TERMP_IGNDELIM;
1776: return(1);
1777: }
1778:
1779:
1780: /* ARGSUSED */
1781: static void
1782: termp_pf_post(DECL_ARGS)
1783: {
1784:
1785: p->flags &= ~TERMP_IGNDELIM;
1786: p->flags |= TERMP_NOSPACE;
1787: }
1788:
1789:
1790: /* ARGSUSED */
1791: static int
1792: termp_ss_pre(DECL_ARGS)
1793: {
1794:
1795: switch (node->type) {
1796: case (MDOC_BLOCK):
1797: term_newln(p);
1798: if (node->prev)
1799: term_vspace(p);
1800: break;
1801: case (MDOC_HEAD):
1.69 kristaps 1802: p->bold++;
1.5 kristaps 1803: p->offset = HALFINDENT;
1.1 kristaps 1804: break;
1805: default:
1806: break;
1807: }
1808:
1809: return(1);
1810: }
1811:
1812:
1813: /* ARGSUSED */
1814: static void
1815: termp_ss_post(DECL_ARGS)
1816: {
1817:
1.29 kristaps 1818: if (MDOC_HEAD == node->type)
1.1 kristaps 1819: term_newln(p);
1820: }
1821:
1822:
1823: /* ARGSUSED */
1824: static int
1825: termp_cd_pre(DECL_ARGS)
1826: {
1827:
1.69 kristaps 1828: p->bold++;
1.1 kristaps 1829: term_newln(p);
1830: return(1);
1831: }
1832:
1833:
1834: /* ARGSUSED */
1835: static int
1836: termp_in_pre(DECL_ARGS)
1837: {
1838:
1.69 kristaps 1839: p->bold++;
1.22 kristaps 1840: if (SEC_SYNOPSIS == node->sec)
1841: term_word(p, "#include");
1842:
1.1 kristaps 1843: term_word(p, "<");
1844: p->flags |= TERMP_NOSPACE;
1845: return(1);
1846: }
1847:
1848:
1849: /* ARGSUSED */
1850: static void
1851: termp_in_post(DECL_ARGS)
1852: {
1853:
1.69 kristaps 1854: p->bold++;
1.1 kristaps 1855: term_word(p, ">");
1.69 kristaps 1856: p->bold--;
1.1 kristaps 1857:
1858: if (SEC_SYNOPSIS != node->sec)
1859: return;
1.22 kristaps 1860:
1861: term_newln(p);
1862: /*
1863: * XXX Not entirely correct. If `.In foo bar' is specified in
1864: * the SYNOPSIS section, then it produces a single break after
1865: * the <foo>; mandoc asserts a vertical space. Since this
1866: * construction is rarely used, I think it's fine.
1867: */
1.1 kristaps 1868: if (node->next && MDOC_In != node->next->tok)
1869: term_vspace(p);
1870: }
1871:
1872:
1873: /* ARGSUSED */
1874: static int
1.45 kristaps 1875: termp_sp_pre(DECL_ARGS)
1876: {
1877: int i, len;
1878:
1879: if (NULL == node->child) {
1880: term_vspace(p);
1881: return(0);
1882: }
1883:
1884: len = atoi(node->child->string);
1885: if (0 == len)
1886: term_newln(p);
1887: for (i = 0; i < len; i++)
1888: term_vspace(p);
1889:
1890: return(0);
1891: }
1892:
1893:
1894: /* ARGSUSED */
1895: static int
1.44 kristaps 1896: termp_br_pre(DECL_ARGS)
1897: {
1898:
1899: term_newln(p);
1900: return(1);
1901: }
1902:
1903:
1904: /* ARGSUSED */
1905: static int
1.1 kristaps 1906: termp_brq_pre(DECL_ARGS)
1907: {
1908:
1909: if (MDOC_BODY != node->type)
1910: return(1);
1911: term_word(p, "\\(lC");
1912: p->flags |= TERMP_NOSPACE;
1913: return(1);
1914: }
1915:
1916:
1917: /* ARGSUSED */
1918: static void
1919: termp_brq_post(DECL_ARGS)
1920: {
1921:
1922: if (MDOC_BODY != node->type)
1923: return;
1924: p->flags |= TERMP_NOSPACE;
1925: term_word(p, "\\(rC");
1926: }
1927:
1928:
1929: /* ARGSUSED */
1930: static int
1931: termp_bq_pre(DECL_ARGS)
1932: {
1933:
1934: if (MDOC_BODY != node->type)
1935: return(1);
1936: term_word(p, "\\(lB");
1937: p->flags |= TERMP_NOSPACE;
1938: return(1);
1939: }
1940:
1941:
1942: /* ARGSUSED */
1943: static void
1944: termp_bq_post(DECL_ARGS)
1945: {
1946:
1947: if (MDOC_BODY != node->type)
1948: return;
1949: p->flags |= TERMP_NOSPACE;
1950: term_word(p, "\\(rB");
1951: }
1952:
1953:
1954: /* ARGSUSED */
1955: static int
1956: termp_pq_pre(DECL_ARGS)
1957: {
1958:
1959: if (MDOC_BODY != node->type)
1960: return(1);
1961: term_word(p, "\\&(");
1962: p->flags |= TERMP_NOSPACE;
1963: return(1);
1964: }
1965:
1966:
1967: /* ARGSUSED */
1968: static void
1969: termp_pq_post(DECL_ARGS)
1970: {
1971:
1972: if (MDOC_BODY != node->type)
1973: return;
1974: term_word(p, ")");
1975: }
1976:
1977:
1978: /* ARGSUSED */
1979: static int
1980: termp_fo_pre(DECL_ARGS)
1981: {
1982: const struct mdoc_node *n;
1983:
1984: if (MDOC_BODY == node->type) {
1.33 kristaps 1985: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1986: term_word(p, "(");
1987: p->flags |= TERMP_NOSPACE;
1988: return(1);
1989: } else if (MDOC_HEAD != node->type)
1990: return(1);
1991:
1.69 kristaps 1992: p->bold++;
1.1 kristaps 1993: for (n = node->child; n; n = n->next) {
1.11 kristaps 1994: assert(MDOC_TEXT == n->type);
1.1 kristaps 1995: term_word(p, n->string);
1996: }
1.69 kristaps 1997: p->bold--;
1.1 kristaps 1998:
1999: return(0);
2000: }
2001:
2002:
2003: /* ARGSUSED */
2004: static void
2005: termp_fo_post(DECL_ARGS)
2006: {
2007:
2008: if (MDOC_BODY != node->type)
2009: return;
2010: p->flags |= TERMP_NOSPACE;
2011: term_word(p, ")");
2012: p->flags |= TERMP_NOSPACE;
2013: term_word(p, ";");
2014: term_newln(p);
2015: }
2016:
2017:
2018: /* ARGSUSED */
2019: static int
2020: termp_bf_pre(DECL_ARGS)
2021: {
2022: const struct mdoc_node *n;
2023:
1.28 kristaps 2024: if (MDOC_HEAD == node->type)
1.1 kristaps 2025: return(0);
1.28 kristaps 2026: else if (MDOC_BLOCK != node->type)
1.1 kristaps 2027: return(1);
2028:
2029: if (NULL == (n = node->head->child)) {
2030: if (arg_hasattr(MDOC_Emphasis, node))
1.69 kristaps 2031: p->under++;
1.1 kristaps 2032: else if (arg_hasattr(MDOC_Symbolic, node))
1.69 kristaps 2033: p->bold++;
1.1 kristaps 2034:
2035: return(1);
2036: }
2037:
1.11 kristaps 2038: assert(MDOC_TEXT == n->type);
1.1 kristaps 2039: if (0 == strcmp("Em", n->string))
1.69 kristaps 2040: p->under++;
1.1 kristaps 2041: else if (0 == strcmp("Sy", n->string))
1.69 kristaps 2042: p->bold++;
1.1 kristaps 2043:
2044: return(1);
2045: }
2046:
2047:
2048: /* ARGSUSED */
2049: static int
2050: termp_sm_pre(DECL_ARGS)
2051: {
2052:
1.11 kristaps 2053: assert(node->child && MDOC_TEXT == node->child->type);
1.1 kristaps 2054: if (0 == strcmp("on", node->child->string)) {
2055: p->flags &= ~TERMP_NONOSPACE;
2056: p->flags &= ~TERMP_NOSPACE;
2057: } else
2058: p->flags |= TERMP_NONOSPACE;
2059:
2060: return(0);
2061: }
2062:
2063:
2064: /* ARGSUSED */
2065: static int
2066: termp_ap_pre(DECL_ARGS)
2067: {
2068:
2069: p->flags |= TERMP_NOSPACE;
2070: term_word(p, "\\(aq");
2071: p->flags |= TERMP_NOSPACE;
2072: return(1);
2073: }
2074:
2075:
2076: /* ARGSUSED */
2077: static void
2078: termp____post(DECL_ARGS)
2079: {
2080:
2081: p->flags |= TERMP_NOSPACE;
2082: term_word(p, node->next ? "," : ".");
2083: }
2084:
2085:
2086: /* ARGSUSED */
2087: static int
2088: termp_lk_pre(DECL_ARGS)
2089: {
2090: const struct mdoc_node *n;
2091:
1.11 kristaps 2092: assert(node->child);
2093: n = node->child;
1.1 kristaps 2094:
1.12 kristaps 2095: if (NULL == n->next) {
1.69 kristaps 2096: p->under++;
1.12 kristaps 2097: return(1);
2098: }
2099:
1.69 kristaps 2100: p->under++;
1.1 kristaps 2101: term_word(p, n->string);
2102: p->flags |= TERMP_NOSPACE;
2103: term_word(p, ":");
1.69 kristaps 2104: p->under--;
1.1 kristaps 2105:
1.69 kristaps 2106: p->bold++;
1.12 kristaps 2107: for (n = n->next; n; n = n->next)
1.1 kristaps 2108: term_word(p, n->string);
1.69 kristaps 2109: p->bold--;
1.12 kristaps 2110:
1.1 kristaps 2111: return(0);
2112: }
2113:
2114:
2115: /* ARGSUSED */
2116: static int
1.69 kristaps 2117: termp_under_pre(DECL_ARGS)
1.1 kristaps 2118: {
2119:
1.69 kristaps 2120: p->under++;
1.1 kristaps 2121: return(1);
2122: }
CVSweb