Annotation of mandoc/mdoc_term.c, Revision 1.80
1.80 ! kristaps 1: /* $Id: mdoc_term.c,v 1.79 2009/09/24 09:20:02 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);
116: static int termp_brq_pre(DECL_ARGS);
117: static int termp_bt_pre(DECL_ARGS);
118: static int termp_cd_pre(DECL_ARGS);
119: static int termp_d1_pre(DECL_ARGS);
120: static int termp_dq_pre(DECL_ARGS);
121: static int termp_ex_pre(DECL_ARGS);
122: static int termp_fa_pre(DECL_ARGS);
123: static int termp_fl_pre(DECL_ARGS);
124: static int termp_fn_pre(DECL_ARGS);
125: static int termp_fo_pre(DECL_ARGS);
126: static int termp_ft_pre(DECL_ARGS);
127: static int termp_in_pre(DECL_ARGS);
128: static int termp_it_pre(DECL_ARGS);
129: static int termp_lk_pre(DECL_ARGS);
130: static int termp_nd_pre(DECL_ARGS);
131: static int termp_nm_pre(DECL_ARGS);
132: static int termp_ns_pre(DECL_ARGS);
133: static int termp_op_pre(DECL_ARGS);
134: static int termp_pf_pre(DECL_ARGS);
135: static int termp_pq_pre(DECL_ARGS);
136: static int termp_qq_pre(DECL_ARGS);
137: static int termp_rs_pre(DECL_ARGS);
138: static int termp_rv_pre(DECL_ARGS);
139: static int termp_sh_pre(DECL_ARGS);
140: static int termp_sm_pre(DECL_ARGS);
1.45 kristaps 141: static int termp_sp_pre(DECL_ARGS);
1.31 kristaps 142: static int termp_sq_pre(DECL_ARGS);
143: static int termp_ss_pre(DECL_ARGS);
1.69 kristaps 144: static int termp_under_pre(DECL_ARGS);
1.31 kristaps 145: static int termp_ud_pre(DECL_ARGS);
146: static int termp_xr_pre(DECL_ARGS);
147: static int termp_xx_pre(DECL_ARGS);
148:
1.70 kristaps 149: static const struct termact termacts[MDOC_MAX] = {
1.14 kristaps 150: { termp_ap_pre, NULL }, /* Ap */
1.1 kristaps 151: { NULL, NULL }, /* Dd */
152: { NULL, NULL }, /* Dt */
153: { NULL, NULL }, /* Os */
154: { termp_sh_pre, termp_sh_post }, /* Sh */
155: { termp_ss_pre, termp_ss_post }, /* Ss */
1.76 kristaps 156: { termp_sp_pre, NULL }, /* Pp */
1.1 kristaps 157: { termp_d1_pre, termp_d1_post }, /* D1 */
158: { termp_d1_pre, termp_d1_post }, /* Dl */
159: { termp_bd_pre, termp_bd_post }, /* Bd */
160: { NULL, NULL }, /* Ed */
161: { NULL, termp_bl_post }, /* Bl */
162: { NULL, NULL }, /* El */
163: { termp_it_pre, termp_it_post }, /* It */
164: { NULL, NULL }, /* Ad */
1.61 kristaps 165: { termp_an_pre, termp_an_post }, /* An */
1.69 kristaps 166: { termp_under_pre, NULL }, /* Ar */
1.1 kristaps 167: { termp_cd_pre, NULL }, /* Cd */
1.69 kristaps 168: { termp_bold_pre, NULL }, /* Cm */
1.1 kristaps 169: { NULL, NULL }, /* Dv */
170: { NULL, NULL }, /* Er */
171: { NULL, NULL }, /* Ev */
172: { termp_ex_pre, NULL }, /* Ex */
173: { termp_fa_pre, NULL }, /* Fa */
1.69 kristaps 174: { termp_bold_pre, termp_fd_post }, /* Fd */
1.1 kristaps 175: { termp_fl_pre, NULL }, /* Fl */
176: { termp_fn_pre, termp_fn_post }, /* Fn */
177: { termp_ft_pre, termp_ft_post }, /* Ft */
1.69 kristaps 178: { termp_bold_pre, NULL }, /* Ic */
1.1 kristaps 179: { termp_in_pre, termp_in_post }, /* In */
180: { NULL, NULL }, /* Li */
181: { termp_nd_pre, NULL }, /* Nd */
182: { termp_nm_pre, NULL }, /* Nm */
183: { termp_op_pre, termp_op_post }, /* Op */
184: { NULL, NULL }, /* Ot */
1.69 kristaps 185: { termp_under_pre, NULL }, /* Pa */
1.1 kristaps 186: { termp_rv_pre, NULL }, /* Rv */
1.36 kristaps 187: { NULL, NULL }, /* St */
1.69 kristaps 188: { termp_under_pre, NULL }, /* Va */
1.70 kristaps 189: { termp_under_pre, termp_vt_post }, /* Vt */
1.1 kristaps 190: { termp_xr_pre, NULL }, /* Xr */
191: { NULL, termp____post }, /* %A */
192: { NULL, termp____post }, /* %B */
193: { NULL, termp____post }, /* %D */
194: { NULL, termp____post }, /* %I */
1.69 kristaps 195: { termp_under_pre, termp____post }, /* %J */
1.1 kristaps 196: { NULL, termp____post }, /* %N */
197: { NULL, termp____post }, /* %O */
198: { NULL, termp____post }, /* %P */
199: { NULL, termp____post }, /* %R */
1.69 kristaps 200: { termp_under_pre, termp____post }, /* %T */
1.1 kristaps 201: { NULL, termp____post }, /* %V */
202: { NULL, NULL }, /* Ac */
203: { termp_aq_pre, termp_aq_post }, /* Ao */
204: { termp_aq_pre, termp_aq_post }, /* Aq */
1.35 kristaps 205: { NULL, NULL }, /* At */
1.1 kristaps 206: { NULL, NULL }, /* Bc */
207: { termp_bf_pre, NULL }, /* Bf */
208: { termp_bq_pre, termp_bq_post }, /* Bo */
209: { termp_bq_pre, termp_bq_post }, /* Bq */
1.26 kristaps 210: { termp_xx_pre, NULL }, /* Bsx */
1.1 kristaps 211: { NULL, termp_bx_post }, /* Bx */
212: { NULL, NULL }, /* Db */
213: { NULL, NULL }, /* Dc */
214: { termp_dq_pre, termp_dq_post }, /* Do */
215: { termp_dq_pre, termp_dq_post }, /* Dq */
216: { NULL, NULL }, /* Ec */
217: { NULL, NULL }, /* Ef */
1.69 kristaps 218: { termp_under_pre, NULL }, /* Em */
1.1 kristaps 219: { NULL, NULL }, /* Eo */
1.26 kristaps 220: { termp_xx_pre, NULL }, /* Fx */
1.80 ! kristaps 221: { termp_bold_pre, NULL }, /* Ms */ /* FIXME: convert to symbol? */
1.1 kristaps 222: { NULL, NULL }, /* No */
223: { termp_ns_pre, NULL }, /* Ns */
1.26 kristaps 224: { termp_xx_pre, NULL }, /* Nx */
225: { termp_xx_pre, NULL }, /* Ox */
1.1 kristaps 226: { NULL, NULL }, /* Pc */
227: { termp_pf_pre, termp_pf_post }, /* Pf */
228: { termp_pq_pre, termp_pq_post }, /* Po */
229: { termp_pq_pre, termp_pq_post }, /* Pq */
230: { NULL, NULL }, /* Qc */
231: { termp_sq_pre, termp_sq_post }, /* Ql */
232: { termp_qq_pre, termp_qq_post }, /* Qo */
233: { termp_qq_pre, termp_qq_post }, /* Qq */
234: { NULL, NULL }, /* Re */
235: { termp_rs_pre, NULL }, /* Rs */
236: { NULL, NULL }, /* Sc */
237: { termp_sq_pre, termp_sq_post }, /* So */
238: { termp_sq_pre, termp_sq_post }, /* Sq */
239: { termp_sm_pre, NULL }, /* Sm */
1.69 kristaps 240: { termp_under_pre, NULL }, /* Sx */
241: { termp_bold_pre, NULL }, /* Sy */
1.1 kristaps 242: { NULL, NULL }, /* Tn */
1.26 kristaps 243: { termp_xx_pre, NULL }, /* Ux */
1.1 kristaps 244: { NULL, NULL }, /* Xc */
245: { NULL, NULL }, /* Xo */
246: { termp_fo_pre, termp_fo_post }, /* Fo */
247: { NULL, NULL }, /* Fc */
248: { termp_op_pre, termp_op_post }, /* Oo */
249: { NULL, NULL }, /* Oc */
250: { NULL, NULL }, /* Bk */
251: { NULL, NULL }, /* Ek */
252: { termp_bt_pre, NULL }, /* Bt */
253: { NULL, NULL }, /* Hf */
254: { NULL, NULL }, /* Fr */
255: { termp_ud_pre, NULL }, /* Ud */
1.37 kristaps 256: { NULL, termp_lb_post }, /* Lb */
1.76 kristaps 257: { termp_sp_pre, NULL }, /* Lp */
1.1 kristaps 258: { termp_lk_pre, NULL }, /* Lk */
1.69 kristaps 259: { termp_under_pre, NULL }, /* Mt */
1.1 kristaps 260: { termp_brq_pre, termp_brq_post }, /* Brq */
261: { termp_brq_pre, termp_brq_post }, /* Bro */
262: { NULL, NULL }, /* Brc */
263: { NULL, NULL }, /* %C */
264: { NULL, NULL }, /* Es */
265: { NULL, NULL }, /* En */
1.26 kristaps 266: { termp_xx_pre, NULL }, /* Dx */
1.1 kristaps 267: { NULL, NULL }, /* %Q */
1.76 kristaps 268: { termp_sp_pre, NULL }, /* br */
1.45 kristaps 269: { termp_sp_pre, NULL }, /* sp */
1.1 kristaps 270: };
271:
1.2 kristaps 272: #ifdef __linux__
1.9 kristaps 273: extern size_t strlcpy(char *, const char *, size_t);
274: extern size_t strlcat(char *, const char *, size_t);
1.2 kristaps 275: #endif
276:
1.1 kristaps 277: static int arg_hasattr(int, const struct mdoc_node *);
278: static int arg_getattrs(const int *, int *, size_t,
279: const struct mdoc_node *);
280: static int arg_getattr(int, const struct mdoc_node *);
281: static size_t arg_offset(const struct mdoc_argv *);
282: static size_t arg_width(const struct mdoc_argv *, int);
283: static int arg_listtype(const struct mdoc_node *);
1.54 kristaps 284: static void fmt_block_vspace(struct termp *,
1.1 kristaps 285: const struct mdoc_node *,
286: const struct mdoc_node *);
1.3 kristaps 287: static void print_node(DECL_ARGS);
1.70 kristaps 288: static void print_head(DECL_ARGS);
1.3 kristaps 289: static void print_body(DECL_ARGS);
1.70 kristaps 290: static void print_foot(DECL_ARGS);
1.1 kristaps 291:
292:
1.70 kristaps 293: void
294: mdoc_run(struct termp *p, const struct mdoc *mdoc)
1.1 kristaps 295: {
1.70 kristaps 296: const struct mdoc_node *n;
297: const struct mdoc_meta *m;
298:
299: n = mdoc_node(mdoc);
300: m = mdoc_meta(mdoc);
301:
302: print_head(p, NULL, m, n);
303: if (n->child)
304: print_body(p, NULL, m, n->child);
305: print_foot(p, NULL, m, n);
1.1 kristaps 306: }
307:
308:
1.3 kristaps 309: static void
1.1 kristaps 310: print_body(DECL_ARGS)
311: {
312:
1.3 kristaps 313: print_node(p, pair, meta, node);
1.70 kristaps 314: if (node->next)
315: print_body(p, pair, meta, node->next);
1.1 kristaps 316: }
317:
318:
1.70 kristaps 319: /* ARGSUSED */
1.3 kristaps 320: static void
1.1 kristaps 321: print_node(DECL_ARGS)
322: {
1.70 kristaps 323: int chld, bold, under;
1.1 kristaps 324: struct termpair npair;
1.30 kristaps 325: size_t offset, rmargin;
1.1 kristaps 326:
1.70 kristaps 327: chld = 1;
1.29 kristaps 328: offset = p->offset;
1.30 kristaps 329: rmargin = p->rmargin;
1.69 kristaps 330: bold = p->bold;
331: under = p->under;
1.29 kristaps 332:
1.70 kristaps 333: bzero(&npair, sizeof(struct termpair));
1.1 kristaps 334: npair.ppair = pair;
1.46 kristaps 335:
1.1 kristaps 336: if (MDOC_TEXT != node->type) {
337: if (termacts[node->tok].pre)
1.70 kristaps 338: chld = (*termacts[node->tok].pre)
339: (p, &npair, meta, node);
340: } else
1.1 kristaps 341: term_word(p, node->string);
342:
1.70 kristaps 343: if (chld && node->child)
344: print_body(p, &npair, meta, node->child);
1.1 kristaps 345:
1.70 kristaps 346: /*
347: * XXX - if bold/under were to span scopes, this wouldn't be
348: * possible, but because decoration is always in-scope, we can
349: * get away with this.
350: */
1.1 kristaps 351:
1.69 kristaps 352: p->bold = bold;
353: p->under = under;
1.46 kristaps 354:
1.1 kristaps 355: if (MDOC_TEXT != node->type)
356: if (termacts[node->tok].post)
1.70 kristaps 357: (*termacts[node->tok].post)
358: (p, &npair, meta, node);
1.29 kristaps 359:
360: p->offset = offset;
1.30 kristaps 361: p->rmargin = rmargin;
1.1 kristaps 362: }
363:
364:
1.70 kristaps 365: /* ARGSUSED */
1.3 kristaps 366: static void
1.70 kristaps 367: print_foot(DECL_ARGS)
1.1 kristaps 368: {
369: struct tm *tm;
370: char *buf, *os;
371:
1.9 kristaps 372: /*
373: * Output the footer in new-groff style, that is, three columns
374: * with the middle being the manual date and flanking columns
375: * being the operating system:
376: *
377: * SYSTEM DATE SYSTEM
378: */
379:
1.1 kristaps 380: if (NULL == (buf = malloc(p->rmargin)))
1.70 kristaps 381: err(EXIT_FAILURE, "malloc");
1.1 kristaps 382: if (NULL == (os = malloc(p->rmargin)))
1.70 kristaps 383: err(EXIT_FAILURE, "malloc");
1.1 kristaps 384:
385: tm = localtime(&meta->date);
386:
1.42 kristaps 387: if (0 == strftime(buf, p->rmargin, "%B %e, %Y", tm))
1.70 kristaps 388: err(EXIT_FAILURE, "strftime");
1.1 kristaps 389:
390: (void)strlcpy(os, meta->os, p->rmargin);
391:
392: term_vspace(p);
393:
1.9 kristaps 394: p->offset = 0;
395: p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
1.1 kristaps 396: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
397:
398: term_word(p, os);
399: term_flushln(p);
400:
1.9 kristaps 401: p->offset = p->rmargin;
402: p->rmargin = p->maxrmargin - strlen(os);
1.1 kristaps 403: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.9 kristaps 404:
405: term_word(p, buf);
406: term_flushln(p);
407:
1.1 kristaps 408: p->offset = p->rmargin;
409: p->rmargin = p->maxrmargin;
410: p->flags &= ~TERMP_NOBREAK;
1.9 kristaps 411: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.1 kristaps 412:
1.9 kristaps 413: term_word(p, os);
1.1 kristaps 414: term_flushln(p);
415:
1.9 kristaps 416: p->offset = 0;
417: p->rmargin = p->maxrmargin;
418: p->flags = 0;
419:
1.1 kristaps 420: free(buf);
421: free(os);
422: }
423:
424:
1.75 kristaps 425: /* FIXME: put in utility library. */
1.70 kristaps 426: /* ARGSUSED */
1.3 kristaps 427: static void
1.70 kristaps 428: print_head(DECL_ARGS)
1.1 kristaps 429: {
430: char *buf, *title;
431:
432: p->rmargin = p->maxrmargin;
433: p->offset = 0;
434:
435: if (NULL == (buf = malloc(p->rmargin)))
1.70 kristaps 436: err(EXIT_FAILURE, "malloc");
1.1 kristaps 437: if (NULL == (title = malloc(p->rmargin)))
1.70 kristaps 438: err(EXIT_FAILURE, "malloc");
1.1 kristaps 439:
440: /*
441: * The header is strange. It has three components, which are
442: * really two with the first duplicated. It goes like this:
443: *
444: * IDENTIFIER TITLE IDENTIFIER
445: *
446: * The IDENTIFIER is NAME(SECTION), which is the command-name
447: * (if given, or "unknown" if not) followed by the manual page
448: * section. These are given in `Dt'. The TITLE is a free-form
449: * string depending on the manual volume. If not specified, it
450: * switches on the manual section.
451: */
452:
453: assert(meta->vol);
454: (void)strlcpy(buf, meta->vol, p->rmargin);
455:
456: if (meta->arch) {
457: (void)strlcat(buf, " (", p->rmargin);
458: (void)strlcat(buf, meta->arch, p->rmargin);
459: (void)strlcat(buf, ")", p->rmargin);
460: }
461:
1.70 kristaps 462: snprintf(title, p->rmargin, "%s(%d)", meta->title, meta->msec);
1.1 kristaps 463:
464: p->offset = 0;
1.8 kristaps 465: p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2;
1.1 kristaps 466: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
467:
468: term_word(p, title);
469: term_flushln(p);
470:
471: p->offset = p->rmargin;
472: p->rmargin = p->maxrmargin - strlen(title);
1.9 kristaps 473: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1.1 kristaps 474:
475: term_word(p, buf);
476: term_flushln(p);
477:
478: p->offset = p->rmargin;
479: p->rmargin = p->maxrmargin;
480: p->flags &= ~TERMP_NOBREAK;
481: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
482:
483: term_word(p, title);
484: term_flushln(p);
485:
1.9 kristaps 486: p->offset = 0;
1.1 kristaps 487: p->rmargin = p->maxrmargin;
488: p->flags &= ~TERMP_NOSPACE;
489:
490: free(title);
491: free(buf);
492: }
493:
494:
1.72 kristaps 495: /* FIXME: put in utility file for front-ends. */
1.1 kristaps 496: static size_t
497: arg_width(const struct mdoc_argv *arg, int pos)
498: {
499: int i, len;
1.40 kristaps 500: const char *p;
1.1 kristaps 501:
502: assert(pos < (int)arg->sz && pos >= 0);
503: assert(arg->value[pos]);
504:
1.40 kristaps 505: p = arg->value[pos];
506:
507: if (0 == (len = (int)strlen(p)))
1.1 kristaps 508: return(0);
509:
510: for (i = 0; i < len - 1; i++)
1.40 kristaps 511: if ( ! isdigit((u_char)p[i]))
1.1 kristaps 512: break;
513:
1.40 kristaps 514: if (i == len - 1)
515: if ('n' == p[len - 1] || 'm' == p[len - 1])
516: return((size_t)atoi(p) + 2);
1.1 kristaps 517:
1.40 kristaps 518: return((size_t)len + 2);
1.1 kristaps 519: }
520:
521:
1.73 kristaps 522: /* FIXME: put in utility file for front-ends. */
1.1 kristaps 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: */
1.74 kristaps 764: for (i = 0, n = node->prev; n &&
1.78 kristaps 765: i < (int)bl->args->argv[vals[2]].sz;
1.58 kristaps 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. */
1.78 kristaps 771: if (i < (int)bl->args->argv[vals[2]].sz)
1.58 kristaps 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_rs_pre(DECL_ARGS)
1165: {
1166:
1.80 ! kristaps 1167: if (SEC_SEE_ALSO != node->sec)
! 1168: return(1);
1.1 kristaps 1169: if (MDOC_BLOCK == node->type && node->prev)
1170: term_vspace(p);
1171: return(1);
1172: }
1173:
1174:
1175: /* ARGSUSED */
1176: static int
1177: termp_rv_pre(DECL_ARGS)
1178: {
1.68 kristaps 1179: const struct mdoc_node *nn;
1.1 kristaps 1180:
1181: term_newln(p);
1182: term_word(p, "The");
1183:
1.68 kristaps 1184: nn = node->child;
1185: assert(nn);
1186: for ( ; nn; nn = nn->next) {
1.69 kristaps 1187: p->bold++;
1.68 kristaps 1188: term_word(p, nn->string);
1.69 kristaps 1189: p->bold--;
1.68 kristaps 1190: p->flags |= TERMP_NOSPACE;
1191: if (nn->next && NULL == nn->next->next)
1192: term_word(p, "(), and");
1193: else if (nn->next)
1194: term_word(p, "(),");
1195: else
1196: term_word(p, "()");
1197: }
1198:
1199: if (node->child->next)
1200: term_word(p, "functions return");
1201: else
1202: term_word(p, "function returns");
1.1 kristaps 1203:
1.68 kristaps 1204: term_word(p, "the value 0 if successful; otherwise the value "
1205: "-1 is returned and the global variable");
1.1 kristaps 1206:
1.69 kristaps 1207: p->under++;
1.1 kristaps 1208: term_word(p, "errno");
1.69 kristaps 1209: p->under--;
1.1 kristaps 1210:
1211: term_word(p, "is set to indicate the error.");
1212:
1.68 kristaps 1213: return(0);
1.1 kristaps 1214: }
1215:
1216:
1217: /* ARGSUSED */
1218: static int
1219: termp_ex_pre(DECL_ARGS)
1220: {
1.68 kristaps 1221: const struct mdoc_node *nn;
1.1 kristaps 1222:
1.68 kristaps 1223: term_word(p, "The");
1.1 kristaps 1224:
1.68 kristaps 1225: nn = node->child;
1226: assert(nn);
1227: for ( ; nn; nn = nn->next) {
1.69 kristaps 1228: p->bold++;
1.68 kristaps 1229: term_word(p, nn->string);
1.69 kristaps 1230: p->bold--;
1.68 kristaps 1231: p->flags |= TERMP_NOSPACE;
1232: if (nn->next && NULL == nn->next->next)
1233: term_word(p, ", and");
1234: else if (nn->next)
1235: term_word(p, ",");
1236: else
1237: p->flags &= ~TERMP_NOSPACE;
1238: }
1239:
1240: if (node->child->next)
1241: term_word(p, "utilities exit");
1242: else
1243: term_word(p, "utility exits");
1244:
1245: term_word(p, "0 on success, and >0 if an error occurs.");
1.1 kristaps 1246:
1.68 kristaps 1247: return(0);
1.1 kristaps 1248: }
1249:
1250:
1251: /* ARGSUSED */
1252: static int
1253: termp_nd_pre(DECL_ARGS)
1254: {
1.25 kristaps 1255:
1256: if (MDOC_BODY != node->type)
1257: return(1);
1.30 kristaps 1258:
1.27 kristaps 1259: #if defined(__OpenBSD__) || defined(__linux__)
1.41 kristaps 1260: term_word(p, "\\(en");
1.23 kristaps 1261: #else
1262: term_word(p, "\\(em");
1263: #endif
1.1 kristaps 1264: return(1);
1265: }
1266:
1267:
1268: /* ARGSUSED */
1269: static void
1270: termp_bl_post(DECL_ARGS)
1271: {
1272:
1273: if (MDOC_BLOCK == node->type)
1274: term_newln(p);
1275: }
1276:
1277:
1278: /* ARGSUSED */
1279: static void
1280: termp_op_post(DECL_ARGS)
1281: {
1282:
1283: if (MDOC_BODY != node->type)
1284: return;
1285: p->flags |= TERMP_NOSPACE;
1286: term_word(p, "\\(rB");
1287: }
1288:
1289:
1290: /* ARGSUSED */
1291: static int
1292: termp_xr_pre(DECL_ARGS)
1293: {
1294: const struct mdoc_node *n;
1295:
1.11 kristaps 1296: assert(node->child && MDOC_TEXT == node->child->type);
1297: n = node->child;
1298:
1.1 kristaps 1299: term_word(p, n->string);
1300: if (NULL == (n = n->next))
1301: return(0);
1302: p->flags |= TERMP_NOSPACE;
1303: term_word(p, "(");
1304: p->flags |= TERMP_NOSPACE;
1305: term_word(p, n->string);
1306: p->flags |= TERMP_NOSPACE;
1307: term_word(p, ")");
1308: return(0);
1309: }
1310:
1311:
1312: /* ARGSUSED */
1313: static void
1314: termp_vt_post(DECL_ARGS)
1315: {
1316:
1.32 kristaps 1317: if (node->sec != SEC_SYNOPSIS)
1318: return;
1319: if (node->next && MDOC_Vt == node->next->tok)
1320: term_newln(p);
1321: else if (node->next)
1.1 kristaps 1322: term_vspace(p);
1323: }
1324:
1325:
1326: /* ARGSUSED */
1327: static int
1.69 kristaps 1328: termp_bold_pre(DECL_ARGS)
1.1 kristaps 1329: {
1330:
1.69 kristaps 1331: p->bold++;
1.1 kristaps 1332: return(1);
1333: }
1334:
1335:
1336: /* ARGSUSED */
1337: static void
1338: termp_fd_post(DECL_ARGS)
1339: {
1340:
1341: if (node->sec != SEC_SYNOPSIS)
1342: return;
1.29 kristaps 1343:
1.1 kristaps 1344: term_newln(p);
1345: if (node->next && MDOC_Fd != node->next->tok)
1346: term_vspace(p);
1347: }
1348:
1349:
1350: /* ARGSUSED */
1351: static int
1352: termp_sh_pre(DECL_ARGS)
1353: {
1.64 kristaps 1354: /*
1.65 kristaps 1355: * XXX: undocumented: using two `Sh' macros in sequence has no
1356: * vspace between calls, only a newline.
1.64 kristaps 1357: */
1.1 kristaps 1358: switch (node->type) {
1.65 kristaps 1359: case (MDOC_BLOCK):
1360: if (node->prev && MDOC_Sh == node->prev->tok)
1361: if (NULL == node->prev->body->child)
1362: break;
1363: term_vspace(p);
1364: break;
1.1 kristaps 1365: case (MDOC_HEAD):
1.69 kristaps 1366: p->bold++;
1.1 kristaps 1367: break;
1368: case (MDOC_BODY):
1369: p->offset = INDENT;
1370: break;
1371: default:
1372: break;
1373: }
1374: return(1);
1375: }
1376:
1377:
1378: /* ARGSUSED */
1379: static void
1380: termp_sh_post(DECL_ARGS)
1381: {
1382:
1383: switch (node->type) {
1384: case (MDOC_HEAD):
1385: term_newln(p);
1386: break;
1387: case (MDOC_BODY):
1388: term_newln(p);
1389: p->offset = 0;
1390: break;
1391: default:
1392: break;
1393: }
1394: }
1395:
1396:
1397: /* ARGSUSED */
1398: static int
1399: termp_op_pre(DECL_ARGS)
1400: {
1401:
1402: switch (node->type) {
1403: case (MDOC_BODY):
1404: term_word(p, "\\(lB");
1405: p->flags |= TERMP_NOSPACE;
1406: break;
1407: default:
1408: break;
1409: }
1410: return(1);
1411: }
1412:
1413:
1414: /* ARGSUSED */
1415: static int
1416: termp_bt_pre(DECL_ARGS)
1417: {
1418:
1419: term_word(p, "is currently in beta test.");
1420: return(1);
1421: }
1422:
1423:
1424: /* ARGSUSED */
1425: static void
1426: termp_lb_post(DECL_ARGS)
1427: {
1428:
1429: term_newln(p);
1430: }
1431:
1432:
1433: /* ARGSUSED */
1434: static int
1435: termp_ud_pre(DECL_ARGS)
1436: {
1437:
1438: term_word(p, "currently under development.");
1439: return(1);
1440: }
1441:
1442:
1443: /* ARGSUSED */
1444: static int
1445: termp_d1_pre(DECL_ARGS)
1446: {
1447:
1.4 kristaps 1448: if (MDOC_BLOCK != node->type)
1.1 kristaps 1449: return(1);
1450: term_newln(p);
1.29 kristaps 1451: p->offset += (INDENT + 1);
1.1 kristaps 1452: return(1);
1453: }
1454:
1455:
1456: /* ARGSUSED */
1457: static void
1458: termp_d1_post(DECL_ARGS)
1459: {
1460:
1.4 kristaps 1461: if (MDOC_BLOCK != node->type)
1.1 kristaps 1462: return;
1463: term_newln(p);
1464: }
1465:
1466:
1467: /* ARGSUSED */
1468: static int
1469: termp_aq_pre(DECL_ARGS)
1470: {
1471:
1472: if (MDOC_BODY != node->type)
1473: return(1);
1474: term_word(p, "\\(la");
1475: p->flags |= TERMP_NOSPACE;
1476: return(1);
1477: }
1478:
1479:
1480: /* ARGSUSED */
1481: static void
1482: termp_aq_post(DECL_ARGS)
1483: {
1484:
1485: if (MDOC_BODY != node->type)
1486: return;
1487: p->flags |= TERMP_NOSPACE;
1488: term_word(p, "\\(ra");
1489: }
1490:
1491:
1492: /* ARGSUSED */
1493: static int
1494: termp_ft_pre(DECL_ARGS)
1495: {
1496:
1497: if (SEC_SYNOPSIS == node->sec)
1498: if (node->prev && MDOC_Fo == node->prev->tok)
1499: term_vspace(p);
1.69 kristaps 1500: p->under++;
1.1 kristaps 1501: return(1);
1502: }
1503:
1504:
1505: /* ARGSUSED */
1506: static void
1507: termp_ft_post(DECL_ARGS)
1508: {
1509:
1510: if (SEC_SYNOPSIS == node->sec)
1511: term_newln(p);
1512: }
1513:
1514:
1515: /* ARGSUSED */
1516: static int
1517: termp_fn_pre(DECL_ARGS)
1518: {
1519: const struct mdoc_node *n;
1520:
1.11 kristaps 1521: assert(node->child && MDOC_TEXT == node->child->type);
1.1 kristaps 1522:
1523: /* FIXME: can be "type funcname" "type varname"... */
1524:
1.69 kristaps 1525: p->bold++;
1.1 kristaps 1526: term_word(p, node->child->string);
1.69 kristaps 1527: p->bold--;
1.1 kristaps 1528:
1529: p->flags |= TERMP_NOSPACE;
1530: term_word(p, "(");
1531:
1532: for (n = node->child->next; n; n = n->next) {
1.69 kristaps 1533: p->under++;
1.1 kristaps 1534: term_word(p, n->string);
1.69 kristaps 1535: p->under--;
1.1 kristaps 1536: if (n->next)
1537: term_word(p, ",");
1538: }
1539:
1540: term_word(p, ")");
1541:
1542: if (SEC_SYNOPSIS == node->sec)
1543: term_word(p, ";");
1544:
1545: return(0);
1546: }
1547:
1548:
1549: /* ARGSUSED */
1550: static void
1551: termp_fn_post(DECL_ARGS)
1552: {
1553:
1554: if (node->sec == SEC_SYNOPSIS && node->next)
1555: term_vspace(p);
1556: }
1557:
1558:
1559: /* ARGSUSED */
1560: static int
1561: termp_fa_pre(DECL_ARGS)
1562: {
1563: struct mdoc_node *n;
1564:
1565: if (node->parent->tok != MDOC_Fo) {
1.69 kristaps 1566: p->under++;
1.1 kristaps 1567: return(1);
1568: }
1569:
1570: for (n = node->child; n; n = n->next) {
1.69 kristaps 1571: p->under++;
1.1 kristaps 1572: term_word(p, n->string);
1.69 kristaps 1573: p->under--;
1.1 kristaps 1574: if (n->next)
1575: term_word(p, ",");
1576: }
1577:
1578: if (node->child && node->next && node->next->tok == MDOC_Fa)
1579: term_word(p, ",");
1580:
1581: return(0);
1582: }
1583:
1584:
1585: /* ARGSUSED */
1586: static int
1587: termp_bd_pre(DECL_ARGS)
1588: {
1.60 kristaps 1589: int i, type;
1.1 kristaps 1590:
1591: /*
1592: * This is fairly tricky due primarily to crappy documentation.
1593: * If -ragged or -filled are specified, the block does nothing
1594: * but change the indentation.
1595: *
1596: * If, on the other hand, -unfilled or -literal are specified,
1597: * then the game changes. Text is printed exactly as entered in
1598: * the display: if a macro line, a newline is appended to the
1599: * line. Blank lines are allowed.
1600: */
1601:
1.54 kristaps 1602: if (MDOC_BLOCK == node->type) {
1603: fmt_block_vspace(p, node, node);
1604: return(1);
1605: } else if (MDOC_BODY != node->type)
1.1 kristaps 1606: return(1);
1607:
1.60 kristaps 1608: assert(node->parent->args);
1.11 kristaps 1609:
1.60 kristaps 1610: for (type = -1, i = 0; -1 == type &&
1.1 kristaps 1611: i < (int)node->parent->args->argc; i++) {
1612: switch (node->parent->args->argv[i].arg) {
1613: case (MDOC_Ragged):
1614: /* FALLTHROUGH */
1615: case (MDOC_Filled):
1616: /* FALLTHROUGH */
1617: case (MDOC_Unfilled):
1618: /* FALLTHROUGH */
1619: case (MDOC_Literal):
1620: type = node->parent->args->argv[i].arg;
1621: break;
1622: default:
1623: break;
1624: }
1625: }
1.60 kristaps 1626:
1627: assert(type > -1);
1.1 kristaps 1628:
1629: i = arg_getattr(MDOC_Offset, node->parent);
1.60 kristaps 1630: if (-1 != i)
1.1 kristaps 1631: p->offset += arg_offset(&node->parent->args->argv[i]);
1632:
1633: switch (type) {
1634: case (MDOC_Literal):
1635: /* FALLTHROUGH */
1636: case (MDOC_Unfilled):
1637: break;
1638: default:
1639: return(1);
1640: }
1641:
1642: for (node = node->child; node; node = node->next) {
1.60 kristaps 1643: p->flags |= TERMP_NOSPACE;
1644: print_node(p, pair, meta, node);
1645: if (node->next)
1.1 kristaps 1646: term_flushln(p);
1647: }
1648:
1649: return(0);
1650: }
1651:
1652:
1653: /* ARGSUSED */
1654: static void
1655: termp_bd_post(DECL_ARGS)
1656: {
1657:
1658: if (MDOC_BODY != node->type)
1659: return;
1.60 kristaps 1660: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1661: term_flushln(p);
1662: }
1663:
1664:
1665: /* ARGSUSED */
1666: static int
1667: termp_qq_pre(DECL_ARGS)
1668: {
1669:
1670: if (MDOC_BODY != node->type)
1671: return(1);
1672: term_word(p, "\"");
1673: p->flags |= TERMP_NOSPACE;
1674: return(1);
1675: }
1676:
1677:
1678: /* ARGSUSED */
1679: static void
1680: termp_qq_post(DECL_ARGS)
1681: {
1682:
1683: if (MDOC_BODY != node->type)
1684: return;
1685: p->flags |= TERMP_NOSPACE;
1686: term_word(p, "\"");
1687: }
1688:
1689:
1690: /* ARGSUSED */
1691: static void
1692: termp_bx_post(DECL_ARGS)
1693: {
1694:
1695: if (node->child)
1696: p->flags |= TERMP_NOSPACE;
1697: term_word(p, "BSD");
1698: }
1699:
1700:
1701: /* ARGSUSED */
1702: static int
1.26 kristaps 1703: termp_xx_pre(DECL_ARGS)
1.1 kristaps 1704: {
1.26 kristaps 1705: const char *pp;
1.1 kristaps 1706:
1.26 kristaps 1707: pp = NULL;
1708: switch (node->tok) {
1709: case (MDOC_Bsx):
1710: pp = "BSDI BSD/OS";
1711: break;
1712: case (MDOC_Dx):
1713: pp = "DragonFlyBSD";
1714: break;
1715: case (MDOC_Fx):
1716: pp = "FreeBSD";
1717: break;
1718: case (MDOC_Nx):
1719: pp = "NetBSD";
1720: break;
1721: case (MDOC_Ox):
1722: pp = "OpenBSD";
1723: break;
1724: case (MDOC_Ux):
1725: pp = "UNIX";
1726: break;
1727: default:
1728: break;
1729: }
1.1 kristaps 1730:
1.26 kristaps 1731: assert(pp);
1732: term_word(p, pp);
1.1 kristaps 1733: return(1);
1734: }
1735:
1736:
1737: /* ARGSUSED */
1738: static int
1739: termp_sq_pre(DECL_ARGS)
1740: {
1741:
1742: if (MDOC_BODY != node->type)
1743: return(1);
1744: term_word(p, "\\(oq");
1745: p->flags |= TERMP_NOSPACE;
1746: return(1);
1747: }
1748:
1749:
1750: /* ARGSUSED */
1751: static void
1752: termp_sq_post(DECL_ARGS)
1753: {
1754:
1755: if (MDOC_BODY != node->type)
1756: return;
1757: p->flags |= TERMP_NOSPACE;
1758: term_word(p, "\\(aq");
1759: }
1760:
1761:
1762: /* ARGSUSED */
1763: static int
1764: termp_pf_pre(DECL_ARGS)
1765: {
1766:
1767: p->flags |= TERMP_IGNDELIM;
1768: return(1);
1769: }
1770:
1771:
1772: /* ARGSUSED */
1773: static void
1774: termp_pf_post(DECL_ARGS)
1775: {
1776:
1777: p->flags &= ~TERMP_IGNDELIM;
1778: p->flags |= TERMP_NOSPACE;
1779: }
1780:
1781:
1782: /* ARGSUSED */
1783: static int
1784: termp_ss_pre(DECL_ARGS)
1785: {
1786:
1787: switch (node->type) {
1788: case (MDOC_BLOCK):
1789: term_newln(p);
1790: if (node->prev)
1791: term_vspace(p);
1792: break;
1793: case (MDOC_HEAD):
1.69 kristaps 1794: p->bold++;
1.5 kristaps 1795: p->offset = HALFINDENT;
1.1 kristaps 1796: break;
1797: default:
1798: break;
1799: }
1800:
1801: return(1);
1802: }
1803:
1804:
1805: /* ARGSUSED */
1806: static void
1807: termp_ss_post(DECL_ARGS)
1808: {
1809:
1.29 kristaps 1810: if (MDOC_HEAD == node->type)
1.1 kristaps 1811: term_newln(p);
1812: }
1813:
1814:
1815: /* ARGSUSED */
1816: static int
1817: termp_cd_pre(DECL_ARGS)
1818: {
1819:
1.69 kristaps 1820: p->bold++;
1.1 kristaps 1821: term_newln(p);
1822: return(1);
1823: }
1824:
1825:
1826: /* ARGSUSED */
1827: static int
1828: termp_in_pre(DECL_ARGS)
1829: {
1830:
1.69 kristaps 1831: p->bold++;
1.22 kristaps 1832: if (SEC_SYNOPSIS == node->sec)
1833: term_word(p, "#include");
1834:
1.1 kristaps 1835: term_word(p, "<");
1836: p->flags |= TERMP_NOSPACE;
1837: return(1);
1838: }
1839:
1840:
1841: /* ARGSUSED */
1842: static void
1843: termp_in_post(DECL_ARGS)
1844: {
1845:
1.69 kristaps 1846: p->bold++;
1.79 kristaps 1847: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1848: term_word(p, ">");
1.69 kristaps 1849: p->bold--;
1.1 kristaps 1850:
1851: if (SEC_SYNOPSIS != node->sec)
1852: return;
1.22 kristaps 1853:
1854: term_newln(p);
1855: /*
1856: * XXX Not entirely correct. If `.In foo bar' is specified in
1857: * the SYNOPSIS section, then it produces a single break after
1858: * the <foo>; mandoc asserts a vertical space. Since this
1859: * construction is rarely used, I think it's fine.
1860: */
1.1 kristaps 1861: if (node->next && MDOC_In != node->next->tok)
1862: term_vspace(p);
1863: }
1864:
1865:
1866: /* ARGSUSED */
1867: static int
1.45 kristaps 1868: termp_sp_pre(DECL_ARGS)
1869: {
1870: int i, len;
1871:
1.77 kristaps 1872: switch (node->tok) {
1.76 kristaps 1873: case (MDOC_sp):
1874: len = node->child ? atoi(node->child->string) : 1;
1875: break;
1876: case (MDOC_br):
1877: len = 0;
1878: break;
1879: default:
1880: len = 1;
1881: break;
1.45 kristaps 1882: }
1883:
1884: if (0 == len)
1885: term_newln(p);
1886: for (i = 0; i < len; i++)
1887: term_vspace(p);
1888:
1889: return(0);
1890: }
1891:
1892:
1893: /* ARGSUSED */
1894: static int
1.1 kristaps 1895: termp_brq_pre(DECL_ARGS)
1896: {
1897:
1898: if (MDOC_BODY != node->type)
1899: return(1);
1900: term_word(p, "\\(lC");
1901: p->flags |= TERMP_NOSPACE;
1902: return(1);
1903: }
1904:
1905:
1906: /* ARGSUSED */
1907: static void
1908: termp_brq_post(DECL_ARGS)
1909: {
1910:
1911: if (MDOC_BODY != node->type)
1912: return;
1913: p->flags |= TERMP_NOSPACE;
1914: term_word(p, "\\(rC");
1915: }
1916:
1917:
1918: /* ARGSUSED */
1919: static int
1920: termp_bq_pre(DECL_ARGS)
1921: {
1922:
1923: if (MDOC_BODY != node->type)
1924: return(1);
1925: term_word(p, "\\(lB");
1926: p->flags |= TERMP_NOSPACE;
1927: return(1);
1928: }
1929:
1930:
1931: /* ARGSUSED */
1932: static void
1933: termp_bq_post(DECL_ARGS)
1934: {
1935:
1936: if (MDOC_BODY != node->type)
1937: return;
1938: p->flags |= TERMP_NOSPACE;
1939: term_word(p, "\\(rB");
1940: }
1941:
1942:
1943: /* ARGSUSED */
1944: static int
1945: termp_pq_pre(DECL_ARGS)
1946: {
1947:
1948: if (MDOC_BODY != node->type)
1949: return(1);
1950: term_word(p, "\\&(");
1951: p->flags |= TERMP_NOSPACE;
1952: return(1);
1953: }
1954:
1955:
1956: /* ARGSUSED */
1957: static void
1958: termp_pq_post(DECL_ARGS)
1959: {
1960:
1961: if (MDOC_BODY != node->type)
1962: return;
1963: term_word(p, ")");
1964: }
1965:
1966:
1967: /* ARGSUSED */
1968: static int
1969: termp_fo_pre(DECL_ARGS)
1970: {
1971: const struct mdoc_node *n;
1972:
1973: if (MDOC_BODY == node->type) {
1.33 kristaps 1974: p->flags |= TERMP_NOSPACE;
1.1 kristaps 1975: term_word(p, "(");
1976: p->flags |= TERMP_NOSPACE;
1977: return(1);
1978: } else if (MDOC_HEAD != node->type)
1979: return(1);
1980:
1.69 kristaps 1981: p->bold++;
1.1 kristaps 1982: for (n = node->child; n; n = n->next) {
1.11 kristaps 1983: assert(MDOC_TEXT == n->type);
1.1 kristaps 1984: term_word(p, n->string);
1985: }
1.69 kristaps 1986: p->bold--;
1.1 kristaps 1987:
1988: return(0);
1989: }
1990:
1991:
1992: /* ARGSUSED */
1993: static void
1994: termp_fo_post(DECL_ARGS)
1995: {
1996:
1997: if (MDOC_BODY != node->type)
1998: return;
1999: p->flags |= TERMP_NOSPACE;
2000: term_word(p, ")");
2001: p->flags |= TERMP_NOSPACE;
2002: term_word(p, ";");
2003: term_newln(p);
2004: }
2005:
2006:
2007: /* ARGSUSED */
2008: static int
2009: termp_bf_pre(DECL_ARGS)
2010: {
2011: const struct mdoc_node *n;
2012:
1.28 kristaps 2013: if (MDOC_HEAD == node->type)
1.1 kristaps 2014: return(0);
1.28 kristaps 2015: else if (MDOC_BLOCK != node->type)
1.1 kristaps 2016: return(1);
2017:
2018: if (NULL == (n = node->head->child)) {
2019: if (arg_hasattr(MDOC_Emphasis, node))
1.69 kristaps 2020: p->under++;
1.1 kristaps 2021: else if (arg_hasattr(MDOC_Symbolic, node))
1.69 kristaps 2022: p->bold++;
1.1 kristaps 2023:
2024: return(1);
2025: }
2026:
1.11 kristaps 2027: assert(MDOC_TEXT == n->type);
1.1 kristaps 2028: if (0 == strcmp("Em", n->string))
1.69 kristaps 2029: p->under++;
1.1 kristaps 2030: else if (0 == strcmp("Sy", n->string))
1.69 kristaps 2031: p->bold++;
1.1 kristaps 2032:
2033: return(1);
2034: }
2035:
2036:
2037: /* ARGSUSED */
2038: static int
2039: termp_sm_pre(DECL_ARGS)
2040: {
2041:
1.11 kristaps 2042: assert(node->child && MDOC_TEXT == node->child->type);
1.1 kristaps 2043: if (0 == strcmp("on", node->child->string)) {
2044: p->flags &= ~TERMP_NONOSPACE;
2045: p->flags &= ~TERMP_NOSPACE;
2046: } else
2047: p->flags |= TERMP_NONOSPACE;
2048:
2049: return(0);
2050: }
2051:
2052:
2053: /* ARGSUSED */
2054: static int
2055: termp_ap_pre(DECL_ARGS)
2056: {
2057:
2058: p->flags |= TERMP_NOSPACE;
2059: term_word(p, "\\(aq");
2060: p->flags |= TERMP_NOSPACE;
2061: return(1);
2062: }
2063:
2064:
2065: /* ARGSUSED */
2066: static void
2067: termp____post(DECL_ARGS)
2068: {
2069:
2070: p->flags |= TERMP_NOSPACE;
2071: term_word(p, node->next ? "," : ".");
2072: }
2073:
2074:
2075: /* ARGSUSED */
2076: static int
2077: termp_lk_pre(DECL_ARGS)
2078: {
2079: const struct mdoc_node *n;
2080:
1.11 kristaps 2081: assert(node->child);
2082: n = node->child;
1.1 kristaps 2083:
1.12 kristaps 2084: if (NULL == n->next) {
1.69 kristaps 2085: p->under++;
1.12 kristaps 2086: return(1);
2087: }
2088:
1.69 kristaps 2089: p->under++;
1.1 kristaps 2090: term_word(p, n->string);
2091: p->flags |= TERMP_NOSPACE;
2092: term_word(p, ":");
1.69 kristaps 2093: p->under--;
1.1 kristaps 2094:
1.69 kristaps 2095: p->bold++;
1.12 kristaps 2096: for (n = n->next; n; n = n->next)
1.1 kristaps 2097: term_word(p, n->string);
1.69 kristaps 2098: p->bold--;
1.12 kristaps 2099:
1.1 kristaps 2100: return(0);
2101: }
2102:
2103:
2104: /* ARGSUSED */
2105: static int
1.69 kristaps 2106: termp_under_pre(DECL_ARGS)
1.1 kristaps 2107: {
2108:
1.69 kristaps 2109: p->under++;
1.1 kristaps 2110: return(1);
2111: }
CVSweb