man_term.c revision 1.8 1 1.8 joerg /* $Vendor-Id: man_term.c,v 1.121 2011/09/21 09:57:13 schwarze Exp $ */
2 1.1 joerg /*
3 1.7 joerg * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
4 1.6 joerg * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze (at) openbsd.org>
5 1.1 joerg *
6 1.1 joerg * Permission to use, copy, modify, and distribute this software for any
7 1.1 joerg * purpose with or without fee is hereby granted, provided that the above
8 1.1 joerg * copyright notice and this permission notice appear in all copies.
9 1.1 joerg *
10 1.1 joerg * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 joerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 joerg * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 joerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 joerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 joerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 joerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 joerg */
18 1.2 joerg #ifdef HAVE_CONFIG_H
19 1.2 joerg #include "config.h"
20 1.2 joerg #endif
21 1.2 joerg
22 1.1 joerg #include <sys/types.h>
23 1.1 joerg
24 1.1 joerg #include <assert.h>
25 1.1 joerg #include <ctype.h>
26 1.1 joerg #include <stdio.h>
27 1.1 joerg #include <stdlib.h>
28 1.1 joerg #include <string.h>
29 1.1 joerg
30 1.2 joerg #include "mandoc.h"
31 1.1 joerg #include "out.h"
32 1.1 joerg #include "man.h"
33 1.1 joerg #include "term.h"
34 1.1 joerg #include "main.h"
35 1.1 joerg
36 1.8 joerg #define INDENT 7 /* fixed-width char full-indent */
37 1.8 joerg #define HALFINDENT 3 /* fixed-width char half-indent */
38 1.8 joerg #define MAXMARGINS 64 /* maximum number of indented scopes */
39 1.1 joerg
40 1.2 joerg /* FIXME: have PD set the default vspace width. */
41 1.2 joerg
42 1.1 joerg struct mtermp {
43 1.1 joerg int fl;
44 1.1 joerg #define MANT_LITERAL (1 << 0)
45 1.8 joerg size_t lmargin[MAXMARGINS]; /* margins (incl. visible page) */
46 1.8 joerg int lmargincur; /* index of current margin */
47 1.8 joerg int lmarginsz; /* actual number of nested margins */
48 1.8 joerg size_t offset; /* default offset to visible page */
49 1.1 joerg };
50 1.1 joerg
51 1.1 joerg #define DECL_ARGS struct termp *p, \
52 1.1 joerg struct mtermp *mt, \
53 1.1 joerg const struct man_node *n, \
54 1.1 joerg const struct man_meta *m
55 1.1 joerg
56 1.1 joerg struct termact {
57 1.1 joerg int (*pre)(DECL_ARGS);
58 1.1 joerg void (*post)(DECL_ARGS);
59 1.2 joerg int flags;
60 1.2 joerg #define MAN_NOTEXT (1 << 0) /* Never has text children. */
61 1.1 joerg };
62 1.1 joerg
63 1.4 joerg static int a2width(const struct termp *, const char *);
64 1.4 joerg static size_t a2height(const struct termp *, const char *);
65 1.1 joerg
66 1.2 joerg static void print_man_nodelist(DECL_ARGS);
67 1.2 joerg static void print_man_node(DECL_ARGS);
68 1.3 joerg static void print_man_head(struct termp *, const void *);
69 1.2 joerg static void print_man_foot(struct termp *, const void *);
70 1.1 joerg static void print_bvspace(struct termp *,
71 1.1 joerg const struct man_node *);
72 1.1 joerg
73 1.6 joerg static int pre_alternate(DECL_ARGS);
74 1.1 joerg static int pre_B(DECL_ARGS);
75 1.1 joerg static int pre_HP(DECL_ARGS);
76 1.1 joerg static int pre_I(DECL_ARGS);
77 1.1 joerg static int pre_IP(DECL_ARGS);
78 1.1 joerg static int pre_PP(DECL_ARGS);
79 1.1 joerg static int pre_RS(DECL_ARGS);
80 1.1 joerg static int pre_SH(DECL_ARGS);
81 1.1 joerg static int pre_SS(DECL_ARGS);
82 1.1 joerg static int pre_TP(DECL_ARGS);
83 1.1 joerg static int pre_ign(DECL_ARGS);
84 1.5 joerg static int pre_in(DECL_ARGS);
85 1.5 joerg static int pre_literal(DECL_ARGS);
86 1.1 joerg static int pre_sp(DECL_ARGS);
87 1.6 joerg static int pre_ft(DECL_ARGS);
88 1.1 joerg
89 1.1 joerg static void post_IP(DECL_ARGS);
90 1.1 joerg static void post_HP(DECL_ARGS);
91 1.1 joerg static void post_RS(DECL_ARGS);
92 1.1 joerg static void post_SH(DECL_ARGS);
93 1.1 joerg static void post_SS(DECL_ARGS);
94 1.1 joerg static void post_TP(DECL_ARGS);
95 1.1 joerg
96 1.1 joerg static const struct termact termacts[MAN_MAX] = {
97 1.5 joerg { pre_sp, NULL, MAN_NOTEXT }, /* br */
98 1.2 joerg { NULL, NULL, 0 }, /* TH */
99 1.2 joerg { pre_SH, post_SH, 0 }, /* SH */
100 1.2 joerg { pre_SS, post_SS, 0 }, /* SS */
101 1.2 joerg { pre_TP, post_TP, 0 }, /* TP */
102 1.2 joerg { pre_PP, NULL, 0 }, /* LP */
103 1.2 joerg { pre_PP, NULL, 0 }, /* PP */
104 1.2 joerg { pre_PP, NULL, 0 }, /* P */
105 1.2 joerg { pre_IP, post_IP, 0 }, /* IP */
106 1.2 joerg { pre_HP, post_HP, 0 }, /* HP */
107 1.2 joerg { NULL, NULL, 0 }, /* SM */
108 1.2 joerg { pre_B, NULL, 0 }, /* SB */
109 1.6 joerg { pre_alternate, NULL, 0 }, /* BI */
110 1.6 joerg { pre_alternate, NULL, 0 }, /* IB */
111 1.6 joerg { pre_alternate, NULL, 0 }, /* BR */
112 1.6 joerg { pre_alternate, NULL, 0 }, /* RB */
113 1.2 joerg { NULL, NULL, 0 }, /* R */
114 1.2 joerg { pre_B, NULL, 0 }, /* B */
115 1.2 joerg { pre_I, NULL, 0 }, /* I */
116 1.6 joerg { pre_alternate, NULL, 0 }, /* IR */
117 1.6 joerg { pre_alternate, NULL, 0 }, /* RI */
118 1.7 joerg { pre_ign, NULL, MAN_NOTEXT }, /* na */
119 1.2 joerg { pre_sp, NULL, MAN_NOTEXT }, /* sp */
120 1.5 joerg { pre_literal, NULL, 0 }, /* nf */
121 1.5 joerg { pre_literal, NULL, 0 }, /* fi */
122 1.2 joerg { NULL, NULL, 0 }, /* RE */
123 1.2 joerg { pre_RS, post_RS, 0 }, /* RS */
124 1.2 joerg { pre_ign, NULL, 0 }, /* DT */
125 1.2 joerg { pre_ign, NULL, 0 }, /* UC */
126 1.2 joerg { pre_ign, NULL, 0 }, /* PD */
127 1.2 joerg { pre_ign, NULL, 0 }, /* AT */
128 1.5 joerg { pre_in, NULL, MAN_NOTEXT }, /* in */
129 1.6 joerg { pre_ft, NULL, MAN_NOTEXT }, /* ft */
130 1.1 joerg };
131 1.1 joerg
132 1.1 joerg
133 1.1 joerg
134 1.1 joerg void
135 1.1 joerg terminal_man(void *arg, const struct man *man)
136 1.1 joerg {
137 1.1 joerg struct termp *p;
138 1.1 joerg const struct man_node *n;
139 1.1 joerg const struct man_meta *m;
140 1.1 joerg struct mtermp mt;
141 1.1 joerg
142 1.1 joerg p = (struct termp *)arg;
143 1.1 joerg
144 1.2 joerg p->overstep = 0;
145 1.2 joerg p->maxrmargin = p->defrmargin;
146 1.4 joerg p->tabwidth = term_len(p, 5);
147 1.2 joerg
148 1.1 joerg if (NULL == p->symtab)
149 1.8 joerg p->symtab = mchars_alloc();
150 1.1 joerg
151 1.1 joerg n = man_node(man);
152 1.1 joerg m = man_meta(man);
153 1.1 joerg
154 1.3 joerg term_begin(p, print_man_head, print_man_foot, m);
155 1.1 joerg p->flags |= TERMP_NOSPACE;
156 1.1 joerg
157 1.8 joerg memset(&mt, 0, sizeof(struct mtermp));
158 1.8 joerg
159 1.8 joerg mt.lmargin[mt.lmargincur] = term_len(p, INDENT);
160 1.4 joerg mt.offset = term_len(p, INDENT);
161 1.1 joerg
162 1.1 joerg if (n->child)
163 1.2 joerg print_man_nodelist(p, &mt, n->child, m);
164 1.2 joerg
165 1.2 joerg term_end(p);
166 1.1 joerg }
167 1.1 joerg
168 1.1 joerg
169 1.4 joerg static size_t
170 1.4 joerg a2height(const struct termp *p, const char *cp)
171 1.1 joerg {
172 1.1 joerg struct roffsu su;
173 1.1 joerg
174 1.4 joerg if ( ! a2roffsu(cp, &su, SCALE_VS))
175 1.8 joerg SCALE_VS_INIT(&su, atoi(cp));
176 1.1 joerg
177 1.4 joerg return(term_vspan(p, &su));
178 1.1 joerg }
179 1.1 joerg
180 1.1 joerg
181 1.1 joerg static int
182 1.4 joerg a2width(const struct termp *p, const char *cp)
183 1.1 joerg {
184 1.1 joerg struct roffsu su;
185 1.1 joerg
186 1.4 joerg if ( ! a2roffsu(cp, &su, SCALE_BU))
187 1.1 joerg return(-1);
188 1.1 joerg
189 1.4 joerg return((int)term_hspan(p, &su));
190 1.1 joerg }
191 1.1 joerg
192 1.8 joerg /*
193 1.8 joerg * Printing leading vertical space before a block.
194 1.8 joerg * This is used for the paragraph macros.
195 1.8 joerg * The rules are pretty simple, since there's very little nesting going
196 1.8 joerg * on here. Basically, if we're the first within another block (SS/SH),
197 1.8 joerg * then don't emit vertical space. If we are (RS), then do. If not the
198 1.8 joerg * first, print it.
199 1.8 joerg */
200 1.1 joerg static void
201 1.1 joerg print_bvspace(struct termp *p, const struct man_node *n)
202 1.1 joerg {
203 1.8 joerg
204 1.1 joerg term_newln(p);
205 1.1 joerg
206 1.8 joerg if (n->body && n->body->child)
207 1.8 joerg if (MAN_TBL == n->body->child->type)
208 1.8 joerg return;
209 1.7 joerg
210 1.8 joerg if (MAN_ROOT == n->parent->type || MAN_RS != n->parent->tok)
211 1.8 joerg if (NULL == n->prev)
212 1.8 joerg return;
213 1.1 joerg
214 1.1 joerg term_vspace(p);
215 1.1 joerg }
216 1.1 joerg
217 1.1 joerg /* ARGSUSED */
218 1.1 joerg static int
219 1.1 joerg pre_ign(DECL_ARGS)
220 1.1 joerg {
221 1.1 joerg
222 1.1 joerg return(0);
223 1.1 joerg }
224 1.1 joerg
225 1.1 joerg
226 1.1 joerg /* ARGSUSED */
227 1.1 joerg static int
228 1.1 joerg pre_I(DECL_ARGS)
229 1.1 joerg {
230 1.1 joerg
231 1.2 joerg term_fontrepl(p, TERMFONT_UNDER);
232 1.1 joerg return(1);
233 1.1 joerg }
234 1.1 joerg
235 1.1 joerg
236 1.1 joerg /* ARGSUSED */
237 1.1 joerg static int
238 1.5 joerg pre_literal(DECL_ARGS)
239 1.1 joerg {
240 1.1 joerg
241 1.5 joerg term_newln(p);
242 1.6 joerg
243 1.6 joerg if (MAN_nf == n->tok)
244 1.5 joerg mt->fl |= MANT_LITERAL;
245 1.6 joerg else
246 1.5 joerg mt->fl &= ~MANT_LITERAL;
247 1.5 joerg
248 1.8 joerg /*
249 1.8 joerg * Unlike .IP and .TP, .HP does not have a HEAD.
250 1.8 joerg * So in case a second call to term_flushln() is needed,
251 1.8 joerg * indentation has to be set up explicitly.
252 1.8 joerg */
253 1.8 joerg if (MAN_HP == n->parent->tok && p->rmargin < p->maxrmargin) {
254 1.8 joerg p->offset = p->rmargin;
255 1.8 joerg p->rmargin = p->maxrmargin;
256 1.8 joerg p->flags &= ~(TERMP_NOBREAK | TERMP_TWOSPACE);
257 1.8 joerg p->flags |= TERMP_NOSPACE;
258 1.8 joerg }
259 1.8 joerg
260 1.7 joerg return(0);
261 1.1 joerg }
262 1.1 joerg
263 1.1 joerg /* ARGSUSED */
264 1.1 joerg static int
265 1.6 joerg pre_alternate(DECL_ARGS)
266 1.1 joerg {
267 1.6 joerg enum termfont font[2];
268 1.6 joerg const struct man_node *nn;
269 1.6 joerg int savelit, i;
270 1.1 joerg
271 1.6 joerg switch (n->tok) {
272 1.6 joerg case (MAN_RB):
273 1.6 joerg font[0] = TERMFONT_NONE;
274 1.6 joerg font[1] = TERMFONT_BOLD;
275 1.6 joerg break;
276 1.6 joerg case (MAN_RI):
277 1.6 joerg font[0] = TERMFONT_NONE;
278 1.6 joerg font[1] = TERMFONT_UNDER;
279 1.6 joerg break;
280 1.6 joerg case (MAN_BR):
281 1.6 joerg font[0] = TERMFONT_BOLD;
282 1.6 joerg font[1] = TERMFONT_NONE;
283 1.6 joerg break;
284 1.6 joerg case (MAN_BI):
285 1.6 joerg font[0] = TERMFONT_BOLD;
286 1.6 joerg font[1] = TERMFONT_UNDER;
287 1.6 joerg break;
288 1.6 joerg case (MAN_IR):
289 1.6 joerg font[0] = TERMFONT_UNDER;
290 1.6 joerg font[1] = TERMFONT_NONE;
291 1.6 joerg break;
292 1.6 joerg case (MAN_IB):
293 1.6 joerg font[0] = TERMFONT_UNDER;
294 1.6 joerg font[1] = TERMFONT_BOLD;
295 1.6 joerg break;
296 1.6 joerg default:
297 1.6 joerg abort();
298 1.6 joerg }
299 1.1 joerg
300 1.6 joerg savelit = MANT_LITERAL & mt->fl;
301 1.6 joerg mt->fl &= ~MANT_LITERAL;
302 1.1 joerg
303 1.6 joerg for (i = 0, nn = n->child; nn; nn = nn->next, i = 1 - i) {
304 1.6 joerg term_fontrepl(p, font[i]);
305 1.6 joerg if (savelit && NULL == nn->next)
306 1.6 joerg mt->fl |= MANT_LITERAL;
307 1.2 joerg print_man_node(p, mt, nn, m);
308 1.6 joerg if (nn->next)
309 1.6 joerg p->flags |= TERMP_NOSPACE;
310 1.1 joerg }
311 1.6 joerg
312 1.1 joerg return(0);
313 1.1 joerg }
314 1.1 joerg
315 1.1 joerg /* ARGSUSED */
316 1.1 joerg static int
317 1.6 joerg pre_B(DECL_ARGS)
318 1.1 joerg {
319 1.1 joerg
320 1.6 joerg term_fontrepl(p, TERMFONT_BOLD);
321 1.6 joerg return(1);
322 1.1 joerg }
323 1.1 joerg
324 1.1 joerg /* ARGSUSED */
325 1.1 joerg static int
326 1.6 joerg pre_ft(DECL_ARGS)
327 1.1 joerg {
328 1.6 joerg const char *cp;
329 1.1 joerg
330 1.6 joerg if (NULL == n->child) {
331 1.6 joerg term_fontlast(p);
332 1.6 joerg return(0);
333 1.6 joerg }
334 1.1 joerg
335 1.6 joerg cp = n->child->string;
336 1.6 joerg switch (*cp) {
337 1.6 joerg case ('4'):
338 1.6 joerg /* FALLTHROUGH */
339 1.6 joerg case ('3'):
340 1.6 joerg /* FALLTHROUGH */
341 1.6 joerg case ('B'):
342 1.6 joerg term_fontrepl(p, TERMFONT_BOLD);
343 1.6 joerg break;
344 1.6 joerg case ('2'):
345 1.6 joerg /* FALLTHROUGH */
346 1.6 joerg case ('I'):
347 1.6 joerg term_fontrepl(p, TERMFONT_UNDER);
348 1.6 joerg break;
349 1.6 joerg case ('P'):
350 1.6 joerg term_fontlast(p);
351 1.6 joerg break;
352 1.6 joerg case ('1'):
353 1.6 joerg /* FALLTHROUGH */
354 1.6 joerg case ('C'):
355 1.6 joerg /* FALLTHROUGH */
356 1.6 joerg case ('R'):
357 1.6 joerg term_fontrepl(p, TERMFONT_NONE);
358 1.6 joerg break;
359 1.6 joerg default:
360 1.6 joerg break;
361 1.1 joerg }
362 1.1 joerg return(0);
363 1.1 joerg }
364 1.1 joerg
365 1.1 joerg /* ARGSUSED */
366 1.1 joerg static int
367 1.5 joerg pre_in(DECL_ARGS)
368 1.1 joerg {
369 1.5 joerg int len, less;
370 1.5 joerg size_t v;
371 1.5 joerg const char *cp;
372 1.5 joerg
373 1.5 joerg term_newln(p);
374 1.5 joerg
375 1.5 joerg if (NULL == n->child) {
376 1.5 joerg p->offset = mt->offset;
377 1.5 joerg return(0);
378 1.5 joerg }
379 1.5 joerg
380 1.5 joerg cp = n->child->string;
381 1.5 joerg less = 0;
382 1.5 joerg
383 1.5 joerg if ('-' == *cp)
384 1.5 joerg less = -1;
385 1.5 joerg else if ('+' == *cp)
386 1.5 joerg less = 1;
387 1.5 joerg else
388 1.5 joerg cp--;
389 1.5 joerg
390 1.5 joerg if ((len = a2width(p, ++cp)) < 0)
391 1.5 joerg return(0);
392 1.1 joerg
393 1.5 joerg v = (size_t)len;
394 1.1 joerg
395 1.5 joerg if (less < 0)
396 1.5 joerg p->offset -= p->offset > v ? v : p->offset;
397 1.5 joerg else if (less > 0)
398 1.5 joerg p->offset += v;
399 1.5 joerg else
400 1.5 joerg p->offset = v;
401 1.1 joerg
402 1.7 joerg /* Don't let this creep beyond the right margin. */
403 1.7 joerg
404 1.7 joerg if (p->offset > p->rmargin)
405 1.7 joerg p->offset = p->rmargin;
406 1.7 joerg
407 1.1 joerg return(0);
408 1.1 joerg }
409 1.1 joerg
410 1.1 joerg
411 1.1 joerg /* ARGSUSED */
412 1.1 joerg static int
413 1.5 joerg pre_sp(DECL_ARGS)
414 1.1 joerg {
415 1.5 joerg size_t i, len;
416 1.5 joerg
417 1.8 joerg if ((NULL == n->prev && n->parent)) {
418 1.8 joerg if (MAN_SS == n->parent->tok)
419 1.8 joerg return(0);
420 1.8 joerg if (MAN_SH == n->parent->tok)
421 1.8 joerg return(0);
422 1.8 joerg }
423 1.8 joerg
424 1.5 joerg switch (n->tok) {
425 1.5 joerg case (MAN_br):
426 1.5 joerg len = 0;
427 1.5 joerg break;
428 1.5 joerg default:
429 1.5 joerg len = n->child ? a2height(p, n->child->string) : 1;
430 1.5 joerg break;
431 1.5 joerg }
432 1.5 joerg
433 1.5 joerg if (0 == len)
434 1.5 joerg term_newln(p);
435 1.5 joerg for (i = 0; i < len; i++)
436 1.5 joerg term_vspace(p);
437 1.1 joerg
438 1.1 joerg return(0);
439 1.1 joerg }
440 1.1 joerg
441 1.1 joerg
442 1.1 joerg /* ARGSUSED */
443 1.1 joerg static int
444 1.1 joerg pre_HP(DECL_ARGS)
445 1.1 joerg {
446 1.8 joerg size_t len, one;
447 1.1 joerg int ival;
448 1.1 joerg const struct man_node *nn;
449 1.1 joerg
450 1.1 joerg switch (n->type) {
451 1.1 joerg case (MAN_BLOCK):
452 1.1 joerg print_bvspace(p, n);
453 1.1 joerg return(1);
454 1.1 joerg case (MAN_BODY):
455 1.1 joerg p->flags |= TERMP_NOBREAK;
456 1.1 joerg p->flags |= TERMP_TWOSPACE;
457 1.1 joerg break;
458 1.1 joerg default:
459 1.1 joerg return(0);
460 1.1 joerg }
461 1.1 joerg
462 1.8 joerg len = mt->lmargin[mt->lmargincur];
463 1.1 joerg ival = -1;
464 1.1 joerg
465 1.1 joerg /* Calculate offset. */
466 1.1 joerg
467 1.1 joerg if (NULL != (nn = n->parent->head->child))
468 1.4 joerg if ((ival = a2width(p, nn->string)) >= 0)
469 1.1 joerg len = (size_t)ival;
470 1.1 joerg
471 1.8 joerg one = term_len(p, 1);
472 1.8 joerg if (len < one)
473 1.8 joerg len = one;
474 1.1 joerg
475 1.1 joerg p->offset = mt->offset;
476 1.1 joerg p->rmargin = mt->offset + len;
477 1.1 joerg
478 1.1 joerg if (ival >= 0)
479 1.8 joerg mt->lmargin[mt->lmargincur] = (size_t)ival;
480 1.1 joerg
481 1.1 joerg return(1);
482 1.1 joerg }
483 1.1 joerg
484 1.1 joerg
485 1.1 joerg /* ARGSUSED */
486 1.1 joerg static void
487 1.1 joerg post_HP(DECL_ARGS)
488 1.1 joerg {
489 1.1 joerg
490 1.1 joerg switch (n->type) {
491 1.1 joerg case (MAN_BLOCK):
492 1.1 joerg term_flushln(p);
493 1.1 joerg break;
494 1.1 joerg case (MAN_BODY):
495 1.1 joerg term_flushln(p);
496 1.1 joerg p->flags &= ~TERMP_NOBREAK;
497 1.1 joerg p->flags &= ~TERMP_TWOSPACE;
498 1.1 joerg p->offset = mt->offset;
499 1.1 joerg p->rmargin = p->maxrmargin;
500 1.1 joerg break;
501 1.1 joerg default:
502 1.1 joerg break;
503 1.1 joerg }
504 1.1 joerg }
505 1.1 joerg
506 1.1 joerg
507 1.1 joerg /* ARGSUSED */
508 1.1 joerg static int
509 1.1 joerg pre_PP(DECL_ARGS)
510 1.1 joerg {
511 1.1 joerg
512 1.1 joerg switch (n->type) {
513 1.1 joerg case (MAN_BLOCK):
514 1.8 joerg mt->lmargin[mt->lmargincur] = term_len(p, INDENT);
515 1.1 joerg print_bvspace(p, n);
516 1.1 joerg break;
517 1.1 joerg default:
518 1.1 joerg p->offset = mt->offset;
519 1.1 joerg break;
520 1.1 joerg }
521 1.1 joerg
522 1.6 joerg return(MAN_HEAD != n->type);
523 1.1 joerg }
524 1.1 joerg
525 1.1 joerg
526 1.1 joerg /* ARGSUSED */
527 1.1 joerg static int
528 1.1 joerg pre_IP(DECL_ARGS)
529 1.1 joerg {
530 1.1 joerg const struct man_node *nn;
531 1.1 joerg size_t len;
532 1.6 joerg int savelit, ival;
533 1.1 joerg
534 1.1 joerg switch (n->type) {
535 1.1 joerg case (MAN_BODY):
536 1.1 joerg p->flags |= TERMP_NOSPACE;
537 1.1 joerg break;
538 1.1 joerg case (MAN_HEAD):
539 1.1 joerg p->flags |= TERMP_NOBREAK;
540 1.1 joerg break;
541 1.1 joerg case (MAN_BLOCK):
542 1.1 joerg print_bvspace(p, n);
543 1.1 joerg /* FALLTHROUGH */
544 1.1 joerg default:
545 1.1 joerg return(1);
546 1.1 joerg }
547 1.1 joerg
548 1.8 joerg len = mt->lmargin[mt->lmargincur];
549 1.1 joerg ival = -1;
550 1.1 joerg
551 1.6 joerg /* Calculate the offset from the optional second argument. */
552 1.1 joerg if (NULL != (nn = n->parent->head->child))
553 1.6 joerg if (NULL != (nn = nn->next))
554 1.4 joerg if ((ival = a2width(p, nn->string)) >= 0)
555 1.1 joerg len = (size_t)ival;
556 1.1 joerg
557 1.1 joerg switch (n->type) {
558 1.1 joerg case (MAN_HEAD):
559 1.1 joerg /* Handle zero-width lengths. */
560 1.1 joerg if (0 == len)
561 1.4 joerg len = term_len(p, 1);
562 1.1 joerg
563 1.1 joerg p->offset = mt->offset;
564 1.1 joerg p->rmargin = mt->offset + len;
565 1.1 joerg if (ival < 0)
566 1.1 joerg break;
567 1.1 joerg
568 1.1 joerg /* Set the saved left-margin. */
569 1.8 joerg mt->lmargin[mt->lmargincur] = (size_t)ival;
570 1.1 joerg
571 1.6 joerg savelit = MANT_LITERAL & mt->fl;
572 1.6 joerg mt->fl &= ~MANT_LITERAL;
573 1.6 joerg
574 1.6 joerg if (n->child)
575 1.6 joerg print_man_node(p, mt, n->child, m);
576 1.6 joerg
577 1.6 joerg if (savelit)
578 1.6 joerg mt->fl |= MANT_LITERAL;
579 1.6 joerg
580 1.1 joerg return(0);
581 1.1 joerg case (MAN_BODY):
582 1.1 joerg p->offset = mt->offset + len;
583 1.1 joerg p->rmargin = p->maxrmargin;
584 1.1 joerg break;
585 1.1 joerg default:
586 1.1 joerg break;
587 1.1 joerg }
588 1.1 joerg
589 1.1 joerg return(1);
590 1.1 joerg }
591 1.1 joerg
592 1.1 joerg
593 1.1 joerg /* ARGSUSED */
594 1.1 joerg static void
595 1.1 joerg post_IP(DECL_ARGS)
596 1.1 joerg {
597 1.1 joerg
598 1.1 joerg switch (n->type) {
599 1.1 joerg case (MAN_HEAD):
600 1.1 joerg term_flushln(p);
601 1.1 joerg p->flags &= ~TERMP_NOBREAK;
602 1.1 joerg p->rmargin = p->maxrmargin;
603 1.1 joerg break;
604 1.1 joerg case (MAN_BODY):
605 1.6 joerg term_newln(p);
606 1.1 joerg break;
607 1.1 joerg default:
608 1.1 joerg break;
609 1.1 joerg }
610 1.1 joerg }
611 1.1 joerg
612 1.1 joerg
613 1.1 joerg /* ARGSUSED */
614 1.1 joerg static int
615 1.1 joerg pre_TP(DECL_ARGS)
616 1.1 joerg {
617 1.1 joerg const struct man_node *nn;
618 1.1 joerg size_t len;
619 1.6 joerg int savelit, ival;
620 1.1 joerg
621 1.1 joerg switch (n->type) {
622 1.1 joerg case (MAN_HEAD):
623 1.1 joerg p->flags |= TERMP_NOBREAK;
624 1.1 joerg break;
625 1.1 joerg case (MAN_BODY):
626 1.1 joerg p->flags |= TERMP_NOSPACE;
627 1.1 joerg break;
628 1.1 joerg case (MAN_BLOCK):
629 1.1 joerg print_bvspace(p, n);
630 1.1 joerg /* FALLTHROUGH */
631 1.1 joerg default:
632 1.1 joerg return(1);
633 1.1 joerg }
634 1.1 joerg
635 1.8 joerg len = (size_t)mt->lmargin[mt->lmargincur];
636 1.1 joerg ival = -1;
637 1.1 joerg
638 1.1 joerg /* Calculate offset. */
639 1.1 joerg
640 1.8 joerg if (NULL != (nn = n->parent->head->child))
641 1.8 joerg if (nn->string && nn->parent->line == nn->line)
642 1.4 joerg if ((ival = a2width(p, nn->string)) >= 0)
643 1.1 joerg len = (size_t)ival;
644 1.1 joerg
645 1.1 joerg switch (n->type) {
646 1.1 joerg case (MAN_HEAD):
647 1.1 joerg /* Handle zero-length properly. */
648 1.1 joerg if (0 == len)
649 1.4 joerg len = term_len(p, 1);
650 1.1 joerg
651 1.1 joerg p->offset = mt->offset;
652 1.1 joerg p->rmargin = mt->offset + len;
653 1.1 joerg
654 1.6 joerg savelit = MANT_LITERAL & mt->fl;
655 1.6 joerg mt->fl &= ~MANT_LITERAL;
656 1.6 joerg
657 1.1 joerg /* Don't print same-line elements. */
658 1.6 joerg for (nn = n->child; nn; nn = nn->next)
659 1.1 joerg if (nn->line > n->line)
660 1.2 joerg print_man_node(p, mt, nn, m);
661 1.1 joerg
662 1.6 joerg if (savelit)
663 1.6 joerg mt->fl |= MANT_LITERAL;
664 1.1 joerg if (ival >= 0)
665 1.8 joerg mt->lmargin[mt->lmargincur] = (size_t)ival;
666 1.1 joerg
667 1.1 joerg return(0);
668 1.1 joerg case (MAN_BODY):
669 1.1 joerg p->offset = mt->offset + len;
670 1.1 joerg p->rmargin = p->maxrmargin;
671 1.1 joerg break;
672 1.1 joerg default:
673 1.1 joerg break;
674 1.1 joerg }
675 1.1 joerg
676 1.1 joerg return(1);
677 1.1 joerg }
678 1.1 joerg
679 1.1 joerg
680 1.1 joerg /* ARGSUSED */
681 1.1 joerg static void
682 1.1 joerg post_TP(DECL_ARGS)
683 1.1 joerg {
684 1.1 joerg
685 1.1 joerg switch (n->type) {
686 1.1 joerg case (MAN_HEAD):
687 1.1 joerg term_flushln(p);
688 1.1 joerg p->flags &= ~TERMP_NOBREAK;
689 1.1 joerg p->flags &= ~TERMP_TWOSPACE;
690 1.1 joerg p->rmargin = p->maxrmargin;
691 1.1 joerg break;
692 1.1 joerg case (MAN_BODY):
693 1.6 joerg term_newln(p);
694 1.1 joerg break;
695 1.1 joerg default:
696 1.1 joerg break;
697 1.1 joerg }
698 1.1 joerg }
699 1.1 joerg
700 1.1 joerg
701 1.1 joerg /* ARGSUSED */
702 1.1 joerg static int
703 1.1 joerg pre_SS(DECL_ARGS)
704 1.1 joerg {
705 1.1 joerg
706 1.1 joerg switch (n->type) {
707 1.1 joerg case (MAN_BLOCK):
708 1.8 joerg mt->fl &= ~MANT_LITERAL;
709 1.8 joerg mt->lmargin[mt->lmargincur] = term_len(p, INDENT);
710 1.4 joerg mt->offset = term_len(p, INDENT);
711 1.1 joerg /* If following a prior empty `SS', no vspace. */
712 1.1 joerg if (n->prev && MAN_SS == n->prev->tok)
713 1.1 joerg if (NULL == n->prev->body->child)
714 1.1 joerg break;
715 1.1 joerg if (NULL == n->prev)
716 1.1 joerg break;
717 1.1 joerg term_vspace(p);
718 1.1 joerg break;
719 1.1 joerg case (MAN_HEAD):
720 1.2 joerg term_fontrepl(p, TERMFONT_BOLD);
721 1.4 joerg p->offset = term_len(p, HALFINDENT);
722 1.1 joerg break;
723 1.1 joerg case (MAN_BODY):
724 1.1 joerg p->offset = mt->offset;
725 1.1 joerg break;
726 1.1 joerg default:
727 1.1 joerg break;
728 1.1 joerg }
729 1.1 joerg
730 1.1 joerg return(1);
731 1.1 joerg }
732 1.1 joerg
733 1.1 joerg
734 1.1 joerg /* ARGSUSED */
735 1.1 joerg static void
736 1.1 joerg post_SS(DECL_ARGS)
737 1.1 joerg {
738 1.1 joerg
739 1.1 joerg switch (n->type) {
740 1.1 joerg case (MAN_HEAD):
741 1.1 joerg term_newln(p);
742 1.1 joerg break;
743 1.1 joerg case (MAN_BODY):
744 1.1 joerg term_newln(p);
745 1.1 joerg break;
746 1.1 joerg default:
747 1.1 joerg break;
748 1.1 joerg }
749 1.1 joerg }
750 1.1 joerg
751 1.1 joerg
752 1.1 joerg /* ARGSUSED */
753 1.1 joerg static int
754 1.1 joerg pre_SH(DECL_ARGS)
755 1.1 joerg {
756 1.1 joerg
757 1.1 joerg switch (n->type) {
758 1.1 joerg case (MAN_BLOCK):
759 1.8 joerg mt->fl &= ~MANT_LITERAL;
760 1.8 joerg mt->lmargin[mt->lmargincur] = term_len(p, INDENT);
761 1.4 joerg mt->offset = term_len(p, INDENT);
762 1.1 joerg /* If following a prior empty `SH', no vspace. */
763 1.1 joerg if (n->prev && MAN_SH == n->prev->tok)
764 1.1 joerg if (NULL == n->prev->body->child)
765 1.1 joerg break;
766 1.2 joerg /* If the first macro, no vspae. */
767 1.2 joerg if (NULL == n->prev)
768 1.2 joerg break;
769 1.1 joerg term_vspace(p);
770 1.1 joerg break;
771 1.1 joerg case (MAN_HEAD):
772 1.2 joerg term_fontrepl(p, TERMFONT_BOLD);
773 1.1 joerg p->offset = 0;
774 1.1 joerg break;
775 1.1 joerg case (MAN_BODY):
776 1.1 joerg p->offset = mt->offset;
777 1.1 joerg break;
778 1.1 joerg default:
779 1.1 joerg break;
780 1.1 joerg }
781 1.1 joerg
782 1.1 joerg return(1);
783 1.1 joerg }
784 1.1 joerg
785 1.1 joerg
786 1.1 joerg /* ARGSUSED */
787 1.1 joerg static void
788 1.1 joerg post_SH(DECL_ARGS)
789 1.1 joerg {
790 1.1 joerg
791 1.1 joerg switch (n->type) {
792 1.1 joerg case (MAN_HEAD):
793 1.1 joerg term_newln(p);
794 1.1 joerg break;
795 1.1 joerg case (MAN_BODY):
796 1.1 joerg term_newln(p);
797 1.1 joerg break;
798 1.1 joerg default:
799 1.1 joerg break;
800 1.1 joerg }
801 1.1 joerg }
802 1.1 joerg
803 1.1 joerg /* ARGSUSED */
804 1.1 joerg static int
805 1.1 joerg pre_RS(DECL_ARGS)
806 1.1 joerg {
807 1.8 joerg int ival;
808 1.8 joerg size_t sz;
809 1.1 joerg
810 1.1 joerg switch (n->type) {
811 1.1 joerg case (MAN_BLOCK):
812 1.1 joerg term_newln(p);
813 1.1 joerg return(1);
814 1.1 joerg case (MAN_HEAD):
815 1.1 joerg return(0);
816 1.1 joerg default:
817 1.1 joerg break;
818 1.1 joerg }
819 1.1 joerg
820 1.8 joerg sz = term_len(p, INDENT);
821 1.8 joerg
822 1.8 joerg if (NULL != (n = n->parent->head->child))
823 1.8 joerg if ((ival = a2width(p, n->string)) >= 0)
824 1.8 joerg sz = (size_t)ival;
825 1.1 joerg
826 1.8 joerg mt->offset += sz;
827 1.8 joerg p->rmargin = p->maxrmargin;
828 1.8 joerg p->offset = mt->offset < p->rmargin ? mt->offset : p->rmargin;
829 1.1 joerg
830 1.8 joerg if (++mt->lmarginsz < MAXMARGINS)
831 1.8 joerg mt->lmargincur = mt->lmarginsz;
832 1.1 joerg
833 1.8 joerg mt->lmargin[mt->lmargincur] = mt->lmargin[mt->lmargincur - 1];
834 1.1 joerg return(1);
835 1.1 joerg }
836 1.1 joerg
837 1.1 joerg /* ARGSUSED */
838 1.1 joerg static void
839 1.1 joerg post_RS(DECL_ARGS)
840 1.1 joerg {
841 1.8 joerg int ival;
842 1.8 joerg size_t sz;
843 1.1 joerg
844 1.1 joerg switch (n->type) {
845 1.1 joerg case (MAN_BLOCK):
846 1.8 joerg return;
847 1.2 joerg case (MAN_HEAD):
848 1.8 joerg return;
849 1.1 joerg default:
850 1.1 joerg term_newln(p);
851 1.1 joerg break;
852 1.1 joerg }
853 1.8 joerg
854 1.8 joerg sz = term_len(p, INDENT);
855 1.8 joerg
856 1.8 joerg if (NULL != (n = n->parent->head->child))
857 1.8 joerg if ((ival = a2width(p, n->string)) >= 0)
858 1.8 joerg sz = (size_t)ival;
859 1.8 joerg
860 1.8 joerg mt->offset = mt->offset < sz ? 0 : mt->offset - sz;
861 1.8 joerg p->offset = mt->offset;
862 1.8 joerg
863 1.8 joerg if (--mt->lmarginsz < MAXMARGINS)
864 1.8 joerg mt->lmargincur = mt->lmarginsz;
865 1.1 joerg }
866 1.1 joerg
867 1.1 joerg static void
868 1.2 joerg print_man_node(DECL_ARGS)
869 1.1 joerg {
870 1.2 joerg size_t rm, rmax;
871 1.2 joerg int c;
872 1.1 joerg
873 1.1 joerg switch (n->type) {
874 1.1 joerg case(MAN_TEXT):
875 1.7 joerg /*
876 1.7 joerg * If we have a blank line, output a vertical space.
877 1.7 joerg * If we have a space as the first character, break
878 1.7 joerg * before printing the line's data.
879 1.7 joerg */
880 1.7 joerg if ('\0' == *n->string) {
881 1.1 joerg term_vspace(p);
882 1.7 joerg return;
883 1.7 joerg } else if (' ' == *n->string && MAN_LINE & n->flags)
884 1.7 joerg term_newln(p);
885 1.2 joerg
886 1.1 joerg term_word(p, n->string);
887 1.2 joerg
888 1.7 joerg /*
889 1.7 joerg * If we're in a literal context, make sure that words
890 1.7 joerg * togehter on the same line stay together. This is a
891 1.7 joerg * POST-printing call, so we check the NEXT word. Since
892 1.7 joerg * -man doesn't have nested macros, we don't need to be
893 1.7 joerg * more specific than this.
894 1.7 joerg */
895 1.8 joerg if (MANT_LITERAL & mt->fl && ! (TERMP_NOBREAK & p->flags) &&
896 1.7 joerg (NULL == n->next ||
897 1.7 joerg n->next->line > n->line)) {
898 1.2 joerg rm = p->rmargin;
899 1.2 joerg rmax = p->maxrmargin;
900 1.2 joerg p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
901 1.1 joerg p->flags |= TERMP_NOSPACE;
902 1.1 joerg term_flushln(p);
903 1.2 joerg p->rmargin = rm;
904 1.2 joerg p->maxrmargin = rmax;
905 1.1 joerg }
906 1.7 joerg
907 1.7 joerg if (MAN_EOS & n->flags)
908 1.7 joerg p->flags |= TERMP_SENTENCE;
909 1.7 joerg return;
910 1.7 joerg case (MAN_EQN):
911 1.8 joerg term_eqn(p, n->eqn);
912 1.7 joerg return;
913 1.6 joerg case (MAN_TBL):
914 1.7 joerg /*
915 1.7 joerg * Tables are preceded by a newline. Then process a
916 1.7 joerg * table line, which will cause line termination,
917 1.7 joerg */
918 1.6 joerg if (TBL_SPAN_FIRST & n->span->flags)
919 1.6 joerg term_newln(p);
920 1.6 joerg term_tbl(p, n->span);
921 1.7 joerg return;
922 1.1 joerg default:
923 1.1 joerg break;
924 1.1 joerg }
925 1.1 joerg
926 1.7 joerg if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
927 1.7 joerg term_fontrepl(p, TERMFONT_NONE);
928 1.7 joerg
929 1.7 joerg c = 1;
930 1.7 joerg if (termacts[n->tok].pre)
931 1.7 joerg c = (*termacts[n->tok].pre)(p, mt, n, m);
932 1.7 joerg
933 1.1 joerg if (c && n->child)
934 1.2 joerg print_man_nodelist(p, mt, n->child, m);
935 1.1 joerg
936 1.7 joerg if (termacts[n->tok].post)
937 1.7 joerg (*termacts[n->tok].post)(p, mt, n, m);
938 1.7 joerg if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
939 1.7 joerg term_fontrepl(p, TERMFONT_NONE);
940 1.2 joerg
941 1.2 joerg if (MAN_EOS & n->flags)
942 1.2 joerg p->flags |= TERMP_SENTENCE;
943 1.1 joerg }
944 1.1 joerg
945 1.1 joerg
946 1.1 joerg static void
947 1.2 joerg print_man_nodelist(DECL_ARGS)
948 1.1 joerg {
949 1.1 joerg
950 1.2 joerg print_man_node(p, mt, n, m);
951 1.1 joerg if ( ! n->next)
952 1.1 joerg return;
953 1.2 joerg print_man_nodelist(p, mt, n->next, m);
954 1.1 joerg }
955 1.1 joerg
956 1.1 joerg
957 1.1 joerg static void
958 1.2 joerg print_man_foot(struct termp *p, const void *arg)
959 1.1 joerg {
960 1.2 joerg const struct man_meta *meta;
961 1.2 joerg
962 1.2 joerg meta = (const struct man_meta *)arg;
963 1.1 joerg
964 1.2 joerg term_fontrepl(p, TERMFONT_NONE);
965 1.1 joerg
966 1.1 joerg term_vspace(p);
967 1.2 joerg term_vspace(p);
968 1.2 joerg term_vspace(p);
969 1.1 joerg
970 1.1 joerg p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
971 1.7 joerg p->rmargin = p->maxrmargin - term_strlen(p, meta->date);
972 1.1 joerg p->offset = 0;
973 1.1 joerg
974 1.6 joerg /* term_strlen() can return zero. */
975 1.6 joerg if (p->rmargin == p->maxrmargin)
976 1.6 joerg p->rmargin--;
977 1.6 joerg
978 1.1 joerg if (meta->source)
979 1.1 joerg term_word(p, meta->source);
980 1.1 joerg if (meta->source)
981 1.1 joerg term_word(p, "");
982 1.1 joerg term_flushln(p);
983 1.1 joerg
984 1.8 joerg p->flags |= TERMP_NOSPACE;
985 1.1 joerg p->offset = p->rmargin;
986 1.1 joerg p->rmargin = p->maxrmargin;
987 1.1 joerg p->flags &= ~TERMP_NOBREAK;
988 1.1 joerg
989 1.7 joerg term_word(p, meta->date);
990 1.1 joerg term_flushln(p);
991 1.1 joerg }
992 1.1 joerg
993 1.1 joerg
994 1.1 joerg static void
995 1.2 joerg print_man_head(struct termp *p, const void *arg)
996 1.1 joerg {
997 1.2 joerg char buf[BUFSIZ], title[BUFSIZ];
998 1.2 joerg size_t buflen, titlen;
999 1.2 joerg const struct man_meta *m;
1000 1.2 joerg
1001 1.2 joerg m = (const struct man_meta *)arg;
1002 1.2 joerg
1003 1.2 joerg /*
1004 1.2 joerg * Note that old groff would spit out some spaces before the
1005 1.2 joerg * header. We discontinue this strange behaviour, but at one
1006 1.2 joerg * point we did so here.
1007 1.2 joerg */
1008 1.1 joerg
1009 1.8 joerg p->offset = 0;
1010 1.1 joerg p->rmargin = p->maxrmargin;
1011 1.2 joerg
1012 1.2 joerg buf[0] = title[0] = '\0';
1013 1.1 joerg
1014 1.2 joerg if (m->vol)
1015 1.2 joerg strlcpy(buf, m->vol, BUFSIZ);
1016 1.4 joerg buflen = term_strlen(p, buf);
1017 1.1 joerg
1018 1.2 joerg snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1019 1.4 joerg titlen = term_strlen(p, title);
1020 1.1 joerg
1021 1.8 joerg p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1022 1.1 joerg p->offset = 0;
1023 1.2 joerg p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1024 1.4 joerg (p->maxrmargin -
1025 1.4 joerg term_strlen(p, buf) + term_len(p, 1)) / 2 :
1026 1.2 joerg p->maxrmargin - buflen;
1027 1.1 joerg
1028 1.1 joerg term_word(p, title);
1029 1.1 joerg term_flushln(p);
1030 1.1 joerg
1031 1.8 joerg p->flags |= TERMP_NOSPACE;
1032 1.1 joerg p->offset = p->rmargin;
1033 1.2 joerg p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
1034 1.2 joerg p->maxrmargin - titlen : p->maxrmargin;
1035 1.1 joerg
1036 1.1 joerg term_word(p, buf);
1037 1.1 joerg term_flushln(p);
1038 1.1 joerg
1039 1.1 joerg p->flags &= ~TERMP_NOBREAK;
1040 1.2 joerg if (p->rmargin + titlen <= p->maxrmargin) {
1041 1.8 joerg p->flags |= TERMP_NOSPACE;
1042 1.2 joerg p->offset = p->rmargin;
1043 1.2 joerg p->rmargin = p->maxrmargin;
1044 1.2 joerg term_word(p, title);
1045 1.2 joerg term_flushln(p);
1046 1.2 joerg }
1047 1.1 joerg
1048 1.8 joerg p->flags &= ~TERMP_NOSPACE;
1049 1.8 joerg p->offset = 0;
1050 1.1 joerg p->rmargin = p->maxrmargin;
1051 1.1 joerg
1052 1.2 joerg /*
1053 1.2 joerg * Groff likes to have some leading spaces before content. Well
1054 1.2 joerg * that's fine by me.
1055 1.2 joerg */
1056 1.2 joerg
1057 1.2 joerg term_vspace(p);
1058 1.2 joerg term_vspace(p);
1059 1.2 joerg term_vspace(p);
1060 1.1 joerg }
1061