]> git.cameronkatri.com Git - mandoc.git/blob - term.c
2f10c0a53dcc6b5bfd21fdc810dc683e7e137d02
[mandoc.git] / term.c
1 /* $Id: term.c,v 1.33 2009/03/01 23:27:14 kristaps Exp $ */
2 /*
3 * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
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
28 #define INDENT 6
29
30 /*
31 * Performs actions on nodes of the abstract syntax tree. Both pre- and
32 * post-fix operations are defined here.
33 */
34
35 /* FIXME: indent/tab. */
36 /* FIXME: macro arguments can be escaped. */
37
38 #define TTYPE_PROG 0
39 #define TTYPE_CMD_FLAG 1
40 #define TTYPE_CMD_ARG 2
41 #define TTYPE_SECTION 3
42 #define TTYPE_FUNC_DECL 4
43 #define TTYPE_VAR_DECL 5
44 #define TTYPE_FUNC_TYPE 6
45 #define TTYPE_FUNC_NAME 7
46 #define TTYPE_FUNC_ARG 8
47 #define TTYPE_LINK 9
48 #define TTYPE_SSECTION 10
49 #define TTYPE_FILE 11
50 #define TTYPE_EMPH 12
51 #define TTYPE_CONFIG 13
52 #define TTYPE_CMD 14
53 #define TTYPE_INCLUDE 15
54 #define TTYPE_SYMB 16
55 #define TTYPE_SYMBOL 17
56 #define TTYPE_NMAX 18
57
58 /*
59 * These define "styles" for element types, like command arguments or
60 * executable names. This is useful when multiple macros must decorate
61 * the same thing (like .Ex -std cmd and .Nm cmd).
62 */
63
64 const int ttypes[TTYPE_NMAX] = {
65 TERMP_BOLD, /* TTYPE_PROG */
66 TERMP_BOLD, /* TTYPE_CMD_FLAG */
67 TERMP_UNDERLINE, /* TTYPE_CMD_ARG */
68 TERMP_BOLD, /* TTYPE_SECTION */
69 TERMP_BOLD, /* TTYPE_FUNC_DECL */
70 TERMP_UNDERLINE, /* TTYPE_VAR_DECL */
71 TERMP_UNDERLINE, /* TTYPE_FUNC_TYPE */
72 TERMP_BOLD, /* TTYPE_FUNC_NAME */
73 TERMP_UNDERLINE, /* TTYPE_FUNC_ARG */
74 TERMP_UNDERLINE, /* TTYPE_LINK */
75 TERMP_BOLD, /* TTYPE_SSECTION */
76 TERMP_UNDERLINE, /* TTYPE_FILE */
77 TERMP_UNDERLINE, /* TTYPE_EMPH */
78 TERMP_BOLD, /* TTYPE_CONFIG */
79 TERMP_BOLD, /* TTYPE_CMD */
80 TERMP_BOLD, /* TTYPE_INCLUDE */
81 TERMP_BOLD, /* TTYPE_SYMB */
82 TERMP_BOLD /* TTYPE_SYMBOL */
83 };
84
85 static int arg_hasattr(int, size_t,
86 const struct mdoc_arg *);
87 static int arg_getattr(int, size_t,
88 const struct mdoc_arg *);
89 static size_t arg_offset(const struct mdoc_arg *);
90 static size_t arg_width(const struct mdoc_arg *);
91
92 /*
93 * What follows describes prefix and postfix operations for the abstract
94 * syntax tree descent.
95 */
96
97 #define DECL_ARGS \
98 struct termp *p, \
99 struct termpair *pair, \
100 const struct mdoc_meta *meta, \
101 const struct mdoc_node *node
102
103 #define DECL_PRE(name) \
104 static int name##_pre(DECL_ARGS)
105 #define DECL_POST(name) \
106 static void name##_post(DECL_ARGS)
107 #define DECL_PREPOST(name) \
108 DECL_PRE(name); \
109 DECL_POST(name);
110
111 DECL_PREPOST(termp__t);
112 DECL_PREPOST(termp_aq);
113 DECL_PREPOST(termp_bd);
114 DECL_PREPOST(termp_bq);
115 DECL_PREPOST(termp_d1);
116 DECL_PREPOST(termp_dq);
117 DECL_PREPOST(termp_fd);
118 DECL_PREPOST(termp_fn);
119 DECL_PREPOST(termp_fo);
120 DECL_PREPOST(termp_ft);
121 DECL_PREPOST(termp_in);
122 DECL_PREPOST(termp_it);
123 DECL_PREPOST(termp_op);
124 DECL_PREPOST(termp_pf);
125 DECL_PREPOST(termp_pq);
126 DECL_PREPOST(termp_qq);
127 DECL_PREPOST(termp_sh);
128 DECL_PREPOST(termp_ss);
129 DECL_PREPOST(termp_sq);
130 DECL_PREPOST(termp_vt);
131
132 DECL_PRE(termp_ar);
133 DECL_PRE(termp_at);
134 DECL_PRE(termp_bf);
135 DECL_PRE(termp_bsx);
136 DECL_PRE(termp_bt);
137 DECL_PRE(termp_cd);
138 DECL_PRE(termp_cm);
139 DECL_PRE(termp_em);
140 DECL_PRE(termp_ex);
141 DECL_PRE(termp_fa);
142 DECL_PRE(termp_fl);
143 DECL_PRE(termp_fx);
144 DECL_PRE(termp_ic);
145 DECL_PRE(termp_ms);
146 DECL_PRE(termp_nd);
147 DECL_PRE(termp_nm);
148 DECL_PRE(termp_ns);
149 DECL_PRE(termp_nx);
150 DECL_PRE(termp_ox);
151 DECL_PRE(termp_pa);
152 DECL_PRE(termp_pp);
153 DECL_PRE(termp_rs);
154 DECL_PRE(termp_rv);
155 DECL_PRE(termp_sm);
156 DECL_PRE(termp_st);
157 DECL_PRE(termp_sx);
158 DECL_PRE(termp_sy);
159 DECL_PRE(termp_ud);
160 DECL_PRE(termp_ux);
161 DECL_PRE(termp_va);
162 DECL_PRE(termp_xr);
163
164 DECL_POST(termp___);
165 DECL_POST(termp_bl);
166 DECL_POST(termp_bx);
167
168 const struct termact __termacts[MDOC_MAX] = {
169 { NULL, NULL }, /* \" */
170 { NULL, NULL }, /* Dd */
171 { NULL, NULL }, /* Dt */
172 { NULL, NULL }, /* Os */
173 { termp_sh_pre, termp_sh_post }, /* Sh */
174 { termp_ss_pre, termp_ss_post }, /* Ss */
175 { termp_pp_pre, NULL }, /* Pp */
176 { termp_d1_pre, termp_d1_post }, /* D1 */
177 { termp_d1_pre, termp_d1_post }, /* Dl */
178 { termp_bd_pre, termp_bd_post }, /* Bd */
179 { NULL, NULL }, /* Ed */
180 { NULL, termp_bl_post }, /* Bl */
181 { NULL, NULL }, /* El */
182 { termp_it_pre, termp_it_post }, /* It */
183 { NULL, NULL }, /* Ad */
184 { NULL, NULL }, /* An */
185 { termp_ar_pre, NULL }, /* Ar */
186 { termp_cd_pre, NULL }, /* Cd */
187 { termp_cm_pre, NULL }, /* Cm */
188 { NULL, NULL }, /* Dv */
189 { NULL, NULL }, /* Er */
190 { NULL, NULL }, /* Ev */
191 { termp_ex_pre, NULL }, /* Ex */
192 { termp_fa_pre, NULL }, /* Fa */
193 { termp_fd_pre, termp_fd_post }, /* Fd */
194 { termp_fl_pre, NULL }, /* Fl */
195 { termp_fn_pre, termp_fn_post }, /* Fn */
196 { termp_ft_pre, termp_ft_post }, /* Ft */
197 { termp_ic_pre, NULL }, /* Ic */
198 { termp_in_pre, termp_in_post }, /* In */
199 { NULL, NULL }, /* Li */
200 { termp_nd_pre, NULL }, /* Nd */
201 { termp_nm_pre, NULL }, /* Nm */
202 { termp_op_pre, termp_op_post }, /* Op */
203 { NULL, NULL }, /* Ot */
204 { termp_pa_pre, NULL }, /* Pa */
205 { termp_rv_pre, NULL }, /* Rv */
206 { termp_st_pre, NULL }, /* St */
207 { termp_va_pre, NULL }, /* Va */
208 { termp_vt_pre, termp_vt_post }, /* Vt */
209 { termp_xr_pre, NULL }, /* Xr */
210 { NULL, termp____post }, /* %A */
211 { NULL, termp____post }, /* %B */
212 { NULL, termp____post }, /* %D */
213 { NULL, termp____post }, /* %I */
214 { NULL, termp____post }, /* %J */
215 { NULL, termp____post }, /* %N */
216 { NULL, termp____post }, /* %O */
217 { NULL, termp____post }, /* %P */
218 { NULL, termp____post }, /* %R */
219 { termp__t_pre, termp__t_post }, /* %T */
220 { NULL, termp____post }, /* %V */
221 { NULL, NULL }, /* Ac */
222 { termp_aq_pre, termp_aq_post }, /* Ao */
223 { termp_aq_pre, termp_aq_post }, /* Aq */
224 { termp_at_pre, NULL }, /* At */
225 { NULL, NULL }, /* Bc */
226 { termp_bf_pre, NULL }, /* Bf */
227 { termp_bq_pre, termp_bq_post }, /* Bo */
228 { termp_bq_pre, termp_bq_post }, /* Bq */
229 { termp_bsx_pre, NULL }, /* Bsx */
230 { NULL, termp_bx_post }, /* Bx */
231 { NULL, NULL }, /* Db */
232 { NULL, NULL }, /* Dc */
233 { termp_dq_pre, termp_dq_post }, /* Do */
234 { termp_dq_pre, termp_dq_post }, /* Dq */
235 { NULL, NULL }, /* Ec */
236 { NULL, NULL }, /* Ef */
237 { termp_em_pre, NULL }, /* Em */
238 { NULL, NULL }, /* Eo */
239 { termp_fx_pre, NULL }, /* Fx */
240 { termp_ms_pre, NULL }, /* Ms */
241 { NULL, NULL }, /* No */
242 { termp_ns_pre, NULL }, /* Ns */
243 { termp_nx_pre, NULL }, /* Nx */
244 { termp_ox_pre, NULL }, /* Ox */
245 { NULL, NULL }, /* Pc */
246 { termp_pf_pre, termp_pf_post }, /* Pf */
247 { termp_pq_pre, termp_pq_post }, /* Po */
248 { termp_pq_pre, termp_pq_post }, /* Pq */
249 { NULL, NULL }, /* Qc */
250 { termp_sq_pre, termp_sq_post }, /* Ql */
251 { termp_qq_pre, termp_qq_post }, /* Qo */
252 { termp_qq_pre, termp_qq_post }, /* Qq */
253 { NULL, NULL }, /* Re */
254 { termp_rs_pre, NULL }, /* Rs */
255 { NULL, NULL }, /* Sc */
256 { termp_sq_pre, termp_sq_post }, /* So */
257 { termp_sq_pre, termp_sq_post }, /* Sq */
258 { termp_sm_pre, NULL }, /* Sm */
259 { termp_sx_pre, NULL }, /* Sx */
260 { termp_sy_pre, NULL }, /* Sy */
261 { NULL, NULL }, /* Tn */
262 { termp_ux_pre, NULL }, /* Ux */
263 { NULL, NULL }, /* Xc */
264 { NULL, NULL }, /* Xo */
265 { termp_fo_pre, termp_fo_post }, /* Fo */
266 { NULL, NULL }, /* Fc */
267 { termp_op_pre, termp_op_post }, /* Oo */
268 { NULL, NULL }, /* Oc */
269 { NULL, NULL }, /* Bk */
270 { NULL, NULL }, /* Ek */
271 { termp_bt_pre, NULL }, /* Bt */
272 { NULL, NULL }, /* Hf */
273 { NULL, NULL }, /* Fr */
274 { termp_ud_pre, NULL }, /* Ud */
275 };
276
277 const struct termact *termacts = __termacts;
278
279
280 static size_t
281 arg_width(const struct mdoc_arg *arg)
282 {
283 size_t v;
284 int i, len;
285
286 assert(*arg->value);
287 if (0 == strcmp(*arg->value, "indent"))
288 return(INDENT);
289 if (0 == strcmp(*arg->value, "indent-two"))
290 return(INDENT * 2);
291
292 len = (int)strlen(*arg->value);
293 assert(len > 0);
294
295 for (i = 0; i < len - 1; i++)
296 if ( ! isdigit((int)(*arg->value)[i]))
297 break;
298
299 if (i == len - 1) {
300 if ('n' == (*arg->value)[len - 1]) {
301 v = (size_t)atoi(*arg->value);
302 return(v);
303 }
304
305 }
306 return(strlen(*arg->value) + 1);
307 }
308
309
310 static size_t
311 arg_offset(const struct mdoc_arg *arg)
312 {
313
314 /* TODO */
315 assert(*arg->value);
316 if (0 == strcmp(*arg->value, "indent"))
317 return(INDENT);
318 if (0 == strcmp(*arg->value, "indent-two"))
319 return(INDENT * 2);
320 return(strlen(*arg->value));
321 }
322
323
324 static int
325 arg_hasattr(int arg, size_t argc, const struct mdoc_arg *argv)
326 {
327
328 return(-1 != arg_getattr(arg, argc, argv));
329 }
330
331
332 static int
333 arg_getattr(int arg, size_t argc, const struct mdoc_arg *argv)
334 {
335 int i;
336
337 for (i = 0; i < (int)argc; i++)
338 if (argv[i].arg == arg)
339 return(i);
340 return(-1);
341 }
342
343
344 /* ARGSUSED */
345 static int
346 termp_dq_pre(DECL_ARGS)
347 {
348
349 if (MDOC_BODY != node->type)
350 return(1);
351
352 word(p, "``");
353 p->flags |= TERMP_NOSPACE;
354 return(1);
355 }
356
357
358 /* ARGSUSED */
359 static void
360 termp_dq_post(DECL_ARGS)
361 {
362
363 if (MDOC_BODY != node->type)
364 return;
365
366 p->flags |= TERMP_NOSPACE;
367 word(p, "''");
368 }
369
370
371 /* ARGSUSED */
372 static int
373 termp_it_pre(DECL_ARGS)
374 {
375 const struct mdoc_node *n, *it;
376 const struct mdoc_block *bl;
377 char buf[7], *tp;
378 int i, type;
379 size_t width, offset;
380
381 switch (node->type) {
382 case (MDOC_BODY):
383 /* FALLTHROUGH */
384 case (MDOC_HEAD):
385 it = node->parent;
386 break;
387 case (MDOC_BLOCK):
388 it = node;
389 break;
390 default:
391 return(1);
392 }
393
394 n = it->parent->parent;
395 bl = &n->data.block;
396
397 if (MDOC_BLOCK == node->type) {
398 newln(p);
399 if ( ! arg_hasattr(MDOC_Compact, bl->argc, bl->argv))
400 if (node->prev || n->prev)
401 vspace(p);
402 return(1);
403 }
404
405 /* Get our list type. */
406
407 for (type = -1, i = 0; i < (int)bl->argc; i++)
408 switch (bl->argv[i].arg) {
409 case (MDOC_Bullet):
410 /* FALLTHROUGH */
411 case (MDOC_Dash):
412 /* FALLTHROUGH */
413 case (MDOC_Enum):
414 /* FALLTHROUGH */
415 case (MDOC_Hyphen):
416 /* FALLTHROUGH */
417 case (MDOC_Tag):
418 /* FALLTHROUGH */
419 case (MDOC_Ohang):
420 type = bl->argv[i].arg;
421 i = (int)bl->argc;
422 break;
423 default:
424 errx(1, "list type not supported");
425 /* NOTREACHED */
426 }
427
428 assert(-1 != type);
429
430 /* Save our existing (inherited) margin and offset. */
431
432 pair->offset = p->offset;
433 pair->rmargin = p->rmargin;
434
435 /* Get list width and offset. */
436
437 i = arg_getattr(MDOC_Width, bl->argc, bl->argv);
438 width = i >= 0 ? arg_width(&bl->argv[i]) : 0;
439
440 i = arg_getattr(MDOC_Offset, bl->argc, bl->argv);
441 offset = i >= 0 ? arg_offset(&bl->argv[i]) : 0;
442
443 /* Override the width. */
444
445 switch (type) {
446 case (MDOC_Bullet):
447 /* FALLTHROUGH */
448 case (MDOC_Dash):
449 /* FALLTHROUGH */
450 case (MDOC_Enum):
451 /* FALLTHROUGH */
452 case (MDOC_Hyphen):
453 width = width > 6 ? width : 6;
454 break;
455 case (MDOC_Tag):
456 /* FIXME: auto-size. */
457 if (0 == width)
458 errx(1, "need non-zero -width");
459 break;
460 default:
461 break;
462 }
463
464 /* Word-wrap control. */
465
466 p->flags |= TERMP_NOSPACE;
467
468 switch (type) {
469 case (MDOC_Bullet):
470 /* FALLTHROUGH */
471 case (MDOC_Dash):
472 /* FALLTHROUGH */
473 case (MDOC_Enum):
474 /* FALLTHROUGH */
475 case (MDOC_Hyphen):
476 /* FALLTHROUGH */
477 case (MDOC_Tag):
478 if (MDOC_HEAD == node->type)
479 p->flags |= TERMP_NOBREAK;
480 else if (MDOC_BODY == node->type)
481 p->flags |= TERMP_NOLPAD;
482 break;
483 default:
484 break;
485 }
486
487 /*
488 * Get a token to use as the HEAD lead-in. If NULL, we use the
489 * HEAD child.
490 */
491
492 tp = NULL;
493
494 if (MDOC_HEAD == node->type) {
495 if (arg_hasattr(MDOC_Bullet, bl->argc, bl->argv))
496 tp = "\\[bu]";
497 if (arg_hasattr(MDOC_Dash, bl->argc, bl->argv))
498 tp = "\\-";
499 if (arg_hasattr(MDOC_Enum, bl->argc, bl->argv)) {
500 (pair->ppair->ppair->count)++;
501 (void)snprintf(buf, sizeof(buf), "%d.",
502 pair->ppair->ppair->count);
503 tp = buf;
504 }
505 if (arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv))
506 tp = "\\-";
507 }
508
509 /* Margin control. */
510
511 p->offset += offset;
512
513 switch (type) {
514 case (MDOC_Bullet):
515 /* FALLTHROUGH */
516 case (MDOC_Dash):
517 /* FALLTHROUGH */
518 case (MDOC_Enum):
519 /* FALLTHROUGH */
520 case (MDOC_Hyphen):
521 /* FALLTHROUGH */
522 case (MDOC_Tag):
523 if (MDOC_HEAD == node->type)
524 p->rmargin = p->offset + width;
525 else if (MDOC_BODY == node->type)
526 p->offset += width;
527 break;
528 default:
529 break;
530 }
531
532 if (NULL == tp)
533 return(1);
534
535 word(p, tp);
536 return(0);
537 }
538
539
540 /* ARGSUSED */
541 static void
542 termp_it_post(DECL_ARGS)
543 {
544
545 if (MDOC_BODY != node->type && MDOC_HEAD != node->type)
546 return;
547
548 flushln(p);
549
550 p->offset = pair->offset;
551 p->rmargin = pair->rmargin;
552
553 if (MDOC_HEAD == node->type)
554 p->flags &= ~TERMP_NOBREAK;
555 else if (MDOC_BODY == node->type)
556 p->flags &= ~TERMP_NOLPAD;
557 }
558
559
560 /* ARGSUSED */
561 static int
562 termp_nm_pre(DECL_ARGS)
563 {
564
565 if (SEC_SYNOPSIS == node->sec)
566 newln(p);
567
568 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_PROG]);
569 if (NULL == node->child)
570 word(p, meta->name);
571
572 return(1);
573 }
574
575
576 /* ARGSUSED */
577 static int
578 termp_fl_pre(DECL_ARGS)
579 {
580
581 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_FLAG]);
582 word(p, "\\-");
583 p->flags |= TERMP_NOSPACE;
584 return(1);
585 }
586
587
588 /* ARGSUSED */
589 static int
590 termp_ar_pre(DECL_ARGS)
591 {
592
593 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_ARG]);
594 if (NULL == node->child) {
595 word(p, "file");
596 word(p, "...");
597 }
598 return(1);
599 }
600
601
602 /* ARGSUSED */
603 static int
604 termp_ns_pre(DECL_ARGS)
605 {
606
607 p->flags |= TERMP_NOSPACE;
608 return(1);
609 }
610
611
612 /* ARGSUSED */
613 static int
614 termp_pp_pre(DECL_ARGS)
615 {
616
617 vspace(p);
618 return(1);
619 }
620
621
622 /* ARGSUSED */
623 static int
624 termp_st_pre(DECL_ARGS)
625 {
626 const char *tp;
627
628 assert(1 == node->data.elem.argc);
629
630 tp = mdoc_st2a(node->data.elem.argv[0].arg);
631 word(p, tp);
632
633 return(1);
634 }
635
636
637 /* ARGSUSED */
638 static int
639 termp_rs_pre(DECL_ARGS)
640 {
641
642 if (MDOC_BLOCK == node->type && node->prev)
643 vspace(p);
644 return(1);
645 }
646
647
648 /* ARGSUSED */
649 static int
650 termp_rv_pre(DECL_ARGS)
651 {
652 int i;
653
654 i = arg_getattr(MDOC_Std, node->data.elem.argc,
655 node->data.elem.argv);
656 assert(i >= 0);
657
658 newln(p);
659 word(p, "The");
660
661 p->flags |= ttypes[TTYPE_FUNC_NAME];
662 word(p, *node->data.elem.argv[i].value);
663 p->flags &= ~ttypes[TTYPE_FUNC_NAME];
664
665 word(p, "() function returns the value 0 if successful;");
666 word(p, "otherwise the value -1 is returned and the");
667 word(p, "global variable");
668
669 p->flags |= ttypes[TTYPE_VAR_DECL];
670 word(p, "errno");
671 p->flags &= ~ttypes[TTYPE_VAR_DECL];
672
673 word(p, "is set to indicate the error.");
674
675 return(1);
676 }
677
678
679 /* ARGSUSED */
680 static int
681 termp_ex_pre(DECL_ARGS)
682 {
683 int i;
684
685 i = arg_getattr(MDOC_Std, node->data.elem.argc,
686 node->data.elem.argv);
687 assert(i >= 0);
688
689 word(p, "The");
690 p->flags |= ttypes[TTYPE_PROG];
691 word(p, *node->data.elem.argv[i].value);
692 p->flags &= ~ttypes[TTYPE_PROG];
693 word(p, "utility exits 0 on success, and >0 if an error occurs.");
694
695 return(1);
696 }
697
698
699 /* ARGSUSED */
700 static int
701 termp_nd_pre(DECL_ARGS)
702 {
703
704 word(p, "\\-");
705 return(1);
706 }
707
708
709 /* ARGSUSED */
710 static void
711 termp_bl_post(DECL_ARGS)
712 {
713
714 if (MDOC_BLOCK == node->type)
715 newln(p);
716 }
717
718
719 /* ARGSUSED */
720 static void
721 termp_op_post(DECL_ARGS)
722 {
723
724 if (MDOC_BODY != node->type)
725 return;
726 p->flags |= TERMP_NOSPACE;
727 word(p, "\\(rB");
728 }
729
730
731 /* ARGSUSED */
732 static int
733 termp_xr_pre(DECL_ARGS)
734 {
735 const struct mdoc_node *n;
736
737 n = node->child;
738 assert(n);
739
740 assert(MDOC_TEXT == n->type);
741 word(p, n->data.text.string);
742
743 if (NULL == (n = n->next))
744 return(0);
745
746 assert(MDOC_TEXT == n->type);
747 p->flags |= TERMP_NOSPACE;
748 word(p, "(");
749 p->flags |= TERMP_NOSPACE;
750 word(p, n->data.text.string);
751 p->flags |= TERMP_NOSPACE;
752 word(p, ")");
753
754 return(0);
755 }
756
757
758 /* ARGSUSED */
759 static int
760 termp_vt_pre(DECL_ARGS)
761 {
762
763 /* FIXME: this can be "type name". */
764 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_VAR_DECL]);
765 return(1);
766 }
767
768
769 /* ARGSUSED */
770 static void
771 termp_vt_post(DECL_ARGS)
772 {
773
774 if (node->sec == SEC_SYNOPSIS)
775 vspace(p);
776 }
777
778
779 /* ARGSUSED */
780 static int
781 termp_fd_pre(DECL_ARGS)
782 {
783
784 /*
785 * FIXME: this naming is bad. This value is used, in general,
786 * for the #include header or other preprocessor statement.
787 */
788 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_DECL]);
789 return(1);
790 }
791
792
793 /* ARGSUSED */
794 static void
795 termp_fd_post(DECL_ARGS)
796 {
797
798 if (node->sec != SEC_SYNOPSIS)
799 return;
800 newln(p);
801 if (node->next && MDOC_Fd != node->next->tok)
802 vspace(p);
803 }
804
805
806 /* ARGSUSED */
807 static int
808 termp_sh_pre(DECL_ARGS)
809 {
810
811 switch (node->type) {
812 case (MDOC_HEAD):
813 vspace(p);
814 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SECTION]);
815 break;
816 case (MDOC_BODY):
817 p->offset = INDENT;
818 break;
819 default:
820 break;
821 }
822 return(1);
823 }
824
825
826 /* ARGSUSED */
827 static void
828 termp_sh_post(DECL_ARGS)
829 {
830
831 switch (node->type) {
832 case (MDOC_HEAD):
833 newln(p);
834 break;
835 case (MDOC_BODY):
836 newln(p);
837 p->offset = 0;
838 break;
839 default:
840 break;
841 }
842 }
843
844
845 /* ARGSUSED */
846 static int
847 termp_op_pre(DECL_ARGS)
848 {
849
850 switch (node->type) {
851 case (MDOC_BODY):
852 word(p, "\\(lB");
853 p->flags |= TERMP_NOSPACE;
854 break;
855 default:
856 break;
857 }
858 return(1);
859 }
860
861
862 /* ARGSUSED */
863 static int
864 termp_bt_pre(DECL_ARGS)
865 {
866
867 word(p, "is currently in beta test.");
868 return(1);
869 }
870
871
872 /* ARGSUSED */
873 static int
874 termp_ud_pre(DECL_ARGS)
875 {
876
877 word(p, "currently under development.");
878 return(1);
879 }
880
881
882 /* ARGSUSED */
883 static int
884 termp_d1_pre(DECL_ARGS)
885 {
886
887 if (MDOC_BODY != node->type)
888 return(1);
889 newln(p);
890 p->offset += (pair->offset = INDENT);
891 return(1);
892 }
893
894
895 /* ARGSUSED */
896 static void
897 termp_d1_post(DECL_ARGS)
898 {
899
900 if (MDOC_BODY != node->type)
901 return;
902 newln(p);
903 p->offset -= pair->offset;
904 }
905
906
907 /* ARGSUSED */
908 static int
909 termp_aq_pre(DECL_ARGS)
910 {
911
912 if (MDOC_BODY != node->type)
913 return(1);
914 word(p, "<");
915 p->flags |= TERMP_NOSPACE;
916 return(1);
917 }
918
919
920 /* ARGSUSED */
921 static void
922 termp_aq_post(DECL_ARGS)
923 {
924
925 if (MDOC_BODY != node->type)
926 return;
927 p->flags |= TERMP_NOSPACE;
928 word(p, ">");
929 }
930
931
932 /* ARGSUSED */
933 static int
934 termp_ft_pre(DECL_ARGS)
935 {
936
937 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_TYPE]);
938 return(1);
939 }
940
941
942 /* ARGSUSED */
943 static void
944 termp_ft_post(DECL_ARGS)
945 {
946
947 if (node->sec == SEC_SYNOPSIS)
948 newln(p);
949 }
950
951
952 /* ARGSUSED */
953 static int
954 termp_fn_pre(DECL_ARGS)
955 {
956 const struct mdoc_node *n;
957
958 assert(node->child);
959 assert(MDOC_TEXT == node->child->type);
960
961 /* FIXME: can be "type funcname" "type varname"... */
962
963 p->flags |= ttypes[TTYPE_FUNC_NAME];
964 word(p, node->child->data.text.string);
965 p->flags &= ~ttypes[TTYPE_FUNC_NAME];
966
967 word(p, "(");
968
969 p->flags |= TERMP_NOSPACE;
970 for (n = node->child->next; n; n = n->next) {
971 assert(MDOC_TEXT == n->type);
972 p->flags |= ttypes[TTYPE_FUNC_ARG];
973 word(p, n->data.text.string);
974 p->flags &= ~ttypes[TTYPE_FUNC_ARG];
975 if (n->next)
976 word(p, ",");
977 }
978
979 word(p, ")");
980
981 if (SEC_SYNOPSIS == node->sec)
982 word(p, ";");
983
984 return(0);
985 }
986
987
988 /* ARGSUSED */
989 static void
990 termp_fn_post(DECL_ARGS)
991 {
992
993 if (node->sec == SEC_SYNOPSIS && node->next)
994 vspace(p);
995
996 }
997
998
999 /* ARGSUSED */
1000 static int
1001 termp_sx_pre(DECL_ARGS)
1002 {
1003
1004 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_LINK]);
1005 return(1);
1006 }
1007
1008
1009 /* ARGSUSED */
1010 static int
1011 termp_fa_pre(DECL_ARGS)
1012 {
1013 struct mdoc_node *n;
1014
1015 if (node->parent->tok != MDOC_Fo) {
1016 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_ARG]);
1017 return(1);
1018 }
1019
1020 for (n = node->child; n; n = n->next) {
1021 assert(MDOC_TEXT == n->type);
1022
1023 p->flags |= ttypes[TTYPE_FUNC_ARG];
1024 word(p, n->data.text.string);
1025 p->flags &= ~ttypes[TTYPE_FUNC_ARG];
1026
1027 if (n->next)
1028 word(p, ",");
1029 }
1030
1031 if (node->next && node->next->tok == MDOC_Fa)
1032 word(p, ",");
1033
1034 return(0);
1035 }
1036
1037
1038 /* ARGSUSED */
1039 static int
1040 termp_va_pre(DECL_ARGS)
1041 {
1042
1043 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_VAR_DECL]);
1044 return(1);
1045 }
1046
1047
1048 /* ARGSUSED */
1049 static int
1050 termp_bd_pre(DECL_ARGS)
1051 {
1052 const struct mdoc_block *bl;
1053 const struct mdoc_node *n;
1054 int i;
1055
1056 if (MDOC_BLOCK == node->type) {
1057 if (node->prev)
1058 vspace(p);
1059 return(1);
1060 } else if (MDOC_BODY != node->type)
1061 return(1);
1062
1063 assert(MDOC_BLOCK == node->parent->type);
1064 pair->offset = p->offset;
1065
1066 bl = &node->parent->data.block;
1067
1068 i = arg_getattr(MDOC_Offset, bl->argc, bl->argv);
1069 if (-1 != i) {
1070 assert(1 == bl->argv[i].sz);
1071 p->offset += arg_offset(&bl->argv[i]);
1072 }
1073
1074 p->flags |= TERMP_LITERAL;
1075
1076 for (n = node->child; n; n = n->next) {
1077 if (MDOC_TEXT != n->type)
1078 errx(1, "non-text displays unsupported");
1079 if ((*n->data.text.string)) {
1080 word(p, n->data.text.string);
1081 flushln(p);
1082 } else
1083 vspace(p);
1084
1085 }
1086
1087 p->flags &= ~TERMP_LITERAL;
1088 return(0);
1089 }
1090
1091
1092 /* ARGSUSED */
1093 static void
1094 termp_bd_post(DECL_ARGS)
1095 {
1096
1097 if (MDOC_BODY != node->type)
1098 return;
1099 newln(p);
1100 p->offset = pair->offset;
1101 }
1102
1103
1104 /* ARGSUSED */
1105 static int
1106 termp_qq_pre(DECL_ARGS)
1107 {
1108
1109 if (MDOC_BODY != node->type)
1110 return(1);
1111 word(p, "\"");
1112 p->flags |= TERMP_NOSPACE;
1113 return(1);
1114 }
1115
1116
1117 /* ARGSUSED */
1118 static void
1119 termp_qq_post(DECL_ARGS)
1120 {
1121
1122 if (MDOC_BODY != node->type)
1123 return;
1124 p->flags |= TERMP_NOSPACE;
1125 word(p, "\"");
1126 }
1127
1128
1129 /* ARGSUSED */
1130 static int
1131 termp_bsx_pre(DECL_ARGS)
1132 {
1133
1134 word(p, "BSDI BSD/OS");
1135 return(1);
1136 }
1137
1138
1139 /* ARGSUSED */
1140 static void
1141 termp_bx_post(DECL_ARGS)
1142 {
1143
1144 p->flags |= TERMP_NOSPACE;
1145 word(p, "BSD");
1146 }
1147
1148
1149 /* ARGSUSED */
1150 static int
1151 termp_ox_pre(DECL_ARGS)
1152 {
1153
1154 word(p, "OpenBSD");
1155 return(1);
1156 }
1157
1158
1159 /* ARGSUSED */
1160 static int
1161 termp_ux_pre(DECL_ARGS)
1162 {
1163
1164 word(p, "UNIX");
1165 return(1);
1166 }
1167
1168
1169 /* ARGSUSED */
1170 static int
1171 termp_fx_pre(DECL_ARGS)
1172 {
1173
1174 word(p, "FreeBSD");
1175 return(1);
1176 }
1177
1178
1179 /* ARGSUSED */
1180 static int
1181 termp_nx_pre(DECL_ARGS)
1182 {
1183
1184 word(p, "NetBSD");
1185 return(1);
1186 }
1187
1188
1189 /* ARGSUSED */
1190 static int
1191 termp_sq_pre(DECL_ARGS)
1192 {
1193
1194 if (MDOC_BODY != node->type)
1195 return(1);
1196 word(p, "`");
1197 p->flags |= TERMP_NOSPACE;
1198 return(1);
1199 }
1200
1201
1202 /* ARGSUSED */
1203 static void
1204 termp_sq_post(DECL_ARGS)
1205 {
1206
1207 if (MDOC_BODY != node->type)
1208 return;
1209 p->flags |= TERMP_NOSPACE;
1210 word(p, "\'");
1211 }
1212
1213
1214 /* ARGSUSED */
1215 static int
1216 termp_pf_pre(DECL_ARGS)
1217 {
1218
1219 p->flags |= TERMP_IGNDELIM;
1220 return(1);
1221 }
1222
1223
1224 /* ARGSUSED */
1225 static void
1226 termp_pf_post(DECL_ARGS)
1227 {
1228
1229 p->flags &= ~TERMP_IGNDELIM;
1230 p->flags |= TERMP_NOSPACE;
1231 }
1232
1233
1234 /* ARGSUSED */
1235 static int
1236 termp_ss_pre(DECL_ARGS)
1237 {
1238
1239 switch (node->type) {
1240 case (MDOC_HEAD):
1241 vspace(p);
1242 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SSECTION]);
1243 p->offset = INDENT / 2;
1244 break;
1245 default:
1246 break;
1247 }
1248
1249 return(1);
1250 }
1251
1252
1253 /* ARGSUSED */
1254 static void
1255 termp_ss_post(DECL_ARGS)
1256 {
1257
1258 switch (node->type) {
1259 case (MDOC_HEAD):
1260 newln(p);
1261 p->offset = INDENT;
1262 break;
1263 default:
1264 break;
1265 }
1266 }
1267
1268
1269 /* ARGSUSED */
1270 static int
1271 termp_pa_pre(DECL_ARGS)
1272 {
1273
1274 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FILE]);
1275 return(1);
1276 }
1277
1278
1279 /* ARGSUSED */
1280 static int
1281 termp_em_pre(DECL_ARGS)
1282 {
1283
1284 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
1285 return(1);
1286 }
1287
1288
1289 /* ARGSUSED */
1290 static int
1291 termp_cd_pre(DECL_ARGS)
1292 {
1293
1294 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CONFIG]);
1295 newln(p);
1296 return(1);
1297 }
1298
1299
1300 /* ARGSUSED */
1301 static int
1302 termp_cm_pre(DECL_ARGS)
1303 {
1304
1305 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_FLAG]);
1306 return(1);
1307 }
1308
1309
1310 /* ARGSUSED */
1311 static int
1312 termp_ic_pre(DECL_ARGS)
1313 {
1314
1315 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD]);
1316 return(1);
1317 }
1318
1319
1320 /* ARGSUSED */
1321 static int
1322 termp_in_pre(DECL_ARGS)
1323 {
1324
1325 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_INCLUDE]);
1326 word(p, "#include");
1327 word(p, "<");
1328 p->flags |= TERMP_NOSPACE;
1329 return(1);
1330 }
1331
1332
1333 /* ARGSUSED */
1334 static void
1335 termp_in_post(DECL_ARGS)
1336 {
1337
1338 p->flags |= TERMP_NOSPACE;
1339 word(p, ">");
1340
1341 newln(p);
1342 if (SEC_SYNOPSIS != node->sec)
1343 return;
1344 if (node->next && MDOC_In != node->next->tok)
1345 vspace(p);
1346 }
1347
1348
1349 /* ARGSUSED */
1350 static int
1351 termp_at_pre(DECL_ARGS)
1352 {
1353 enum mdoc_att c;
1354
1355 c = ATT_DEFAULT;
1356 if (node->child) {
1357 assert(MDOC_TEXT == node->child->type);
1358 c = mdoc_atoatt(node->child->data.text.string);
1359 }
1360
1361 word(p, mdoc_att2a(c));
1362 return(0);
1363 }
1364
1365
1366 /* ARGSUSED */
1367 static int
1368 termp_bq_pre(DECL_ARGS)
1369 {
1370
1371 if (MDOC_BODY != node->type)
1372 return(1);
1373 word(p, "[");
1374 p->flags |= TERMP_NOSPACE;
1375 return(1);
1376 }
1377
1378
1379 /* ARGSUSED */
1380 static void
1381 termp_bq_post(DECL_ARGS)
1382 {
1383
1384 if (MDOC_BODY != node->type)
1385 return;
1386 word(p, "]");
1387 }
1388
1389
1390 /* ARGSUSED */
1391 static int
1392 termp_pq_pre(DECL_ARGS)
1393 {
1394
1395 if (MDOC_BODY != node->type)
1396 return(1);
1397 word(p, "\\&(");
1398 p->flags |= TERMP_NOSPACE;
1399 return(1);
1400 }
1401
1402
1403 /* ARGSUSED */
1404 static void
1405 termp_pq_post(DECL_ARGS)
1406 {
1407
1408 if (MDOC_BODY != node->type)
1409 return;
1410 word(p, ")");
1411 }
1412
1413
1414 /* ARGSUSED */
1415 static int
1416 termp_fo_pre(DECL_ARGS)
1417 {
1418 const struct mdoc_node *n;
1419
1420 if (MDOC_BODY == node->type) {
1421 word(p, "(");
1422 p->flags |= TERMP_NOSPACE;
1423 return(1);
1424 } else if (MDOC_HEAD != node->type)
1425 return(1);
1426
1427 /* XXX - groff shows only first parameter */
1428
1429 p->flags |= ttypes[TTYPE_FUNC_NAME];
1430 for (n = node->child; n; n = n->next) {
1431 assert(MDOC_TEXT == n->type);
1432 word(p, n->data.text.string);
1433 }
1434 p->flags &= ~ttypes[TTYPE_FUNC_NAME];
1435
1436 return(0);
1437 }
1438
1439
1440 /* ARGSUSED */
1441 static void
1442 termp_fo_post(DECL_ARGS)
1443 {
1444
1445 if (MDOC_BODY != node->type)
1446 return;
1447 word(p, ")");
1448 word(p, ";");
1449 newln(p);
1450 }
1451
1452
1453 /* ARGSUSED */
1454 static int
1455 termp_bf_pre(DECL_ARGS)
1456 {
1457 const struct mdoc_node *n;
1458 const struct mdoc_block *b;
1459
1460 /* XXX - we skip over possible trailing HEAD tokens. */
1461
1462 if (MDOC_HEAD == node->type)
1463 return(0);
1464 else if (MDOC_BLOCK != node->type)
1465 return(1);
1466
1467 b = &node->data.block;
1468
1469 if (NULL == (n = b->head->child)) {
1470 if (arg_hasattr(MDOC_Emphasis, b->argc, b->argv))
1471 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
1472 else if (arg_hasattr(MDOC_Symbolic, b->argc, b->argv))
1473 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMB]);
1474
1475 return(1);
1476 }
1477
1478 assert(MDOC_TEXT == n->type);
1479
1480 if (0 == strcmp("Em", n->data.text.string))
1481 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
1482 else if (0 == strcmp("Sy", n->data.text.string))
1483 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_EMPH]);
1484
1485 return(1);
1486 }
1487
1488
1489 /* ARGSUSED */
1490 static int
1491 termp_sy_pre(DECL_ARGS)
1492 {
1493
1494 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMB]);
1495 return(1);
1496 }
1497
1498
1499 /* ARGSUSED */
1500 static int
1501 termp_ms_pre(DECL_ARGS)
1502 {
1503
1504 TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SYMBOL]);
1505 return(1);
1506 }
1507
1508
1509
1510 /* ARGSUSED */
1511 static int
1512 termp_sm_pre(DECL_ARGS)
1513 {
1514
1515 #if notyet
1516 assert(node->child);
1517 if (0 == strcmp("off", node->child->data.text.string)) {
1518 p->flags &= ~TERMP_NONOSPACE;
1519 p->flags &= ~TERMP_NOSPACE;
1520 } else {
1521 p->flags |= TERMP_NONOSPACE;
1522 p->flags |= TERMP_NOSPACE;
1523 }
1524 #endif
1525
1526 return(0);
1527 }
1528
1529
1530 /* ARGSUSED */
1531 static int
1532 termp__t_pre(DECL_ARGS)
1533 {
1534
1535 word(p, "\"");
1536 p->flags |= TERMP_NOSPACE;
1537 return(1);
1538 }
1539
1540
1541 /* ARGSUSED */
1542 static void
1543 termp__t_post(DECL_ARGS)
1544 {
1545
1546 p->flags |= TERMP_NOSPACE;
1547 word(p, "\"");
1548 word(p, node->next ? "," : ".");
1549 }
1550
1551
1552 /* ARGSUSED */
1553 static void
1554 termp____post(DECL_ARGS)
1555 {
1556
1557 p->flags |= TERMP_NOSPACE;
1558 word(p, node->next ? "," : ".");
1559 }