man_term.c revision 1.7 1 1.7 joerg /* $Vendor-Id: man_term.c,v 1.105 2011/03/22 10:13:01 kristaps 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.1 joerg #define INDENT 7
37 1.1 joerg #define HALFINDENT 3
38 1.1 joerg
39 1.2 joerg /* FIXME: have PD set the default vspace width. */
40 1.2 joerg
41 1.1 joerg struct mtermp {
42 1.1 joerg int fl;
43 1.1 joerg #define MANT_LITERAL (1 << 0)
44 1.1 joerg /*
45 1.1 joerg * Default amount to indent the left margin after leading text
46 1.1 joerg * has been printed (e.g., `HP' left-indent, `TP' and `IP' body
47 1.1 joerg * indent). This needs to be saved because `HP' and so on, if
48 1.1 joerg * not having a specified value, must default.
49 1.1 joerg *
50 1.1 joerg * Note that this is the indentation AFTER the left offset, so
51 1.1 joerg * the total offset is usually offset + lmargin.
52 1.1 joerg */
53 1.1 joerg size_t lmargin;
54 1.1 joerg /*
55 1.1 joerg * The default offset, i.e., the amount between any text and the
56 1.1 joerg * page boundary.
57 1.1 joerg */
58 1.1 joerg size_t offset;
59 1.1 joerg };
60 1.1 joerg
61 1.1 joerg #define DECL_ARGS struct termp *p, \
62 1.1 joerg struct mtermp *mt, \
63 1.1 joerg const struct man_node *n, \
64 1.1 joerg const struct man_meta *m
65 1.1 joerg
66 1.1 joerg struct termact {
67 1.1 joerg int (*pre)(DECL_ARGS);
68 1.1 joerg void (*post)(DECL_ARGS);
69 1.2 joerg int flags;
70 1.2 joerg #define MAN_NOTEXT (1 << 0) /* Never has text children. */
71 1.1 joerg };
72 1.1 joerg
73 1.4 joerg static int a2width(const struct termp *, const char *);
74 1.4 joerg static size_t a2height(const struct termp *, const char *);
75 1.1 joerg
76 1.2 joerg static void print_man_nodelist(DECL_ARGS);
77 1.2 joerg static void print_man_node(DECL_ARGS);
78 1.3 joerg static void print_man_head(struct termp *, const void *);
79 1.2 joerg static void print_man_foot(struct termp *, const void *);
80 1.1 joerg static void print_bvspace(struct termp *,
81 1.1 joerg const struct man_node *);
82 1.1 joerg
83 1.6 joerg static int pre_alternate(DECL_ARGS);
84 1.1 joerg static int pre_B(DECL_ARGS);
85 1.1 joerg static int pre_HP(DECL_ARGS);
86 1.1 joerg static int pre_I(DECL_ARGS);
87 1.1 joerg static int pre_IP(DECL_ARGS);
88 1.1 joerg static int pre_PP(DECL_ARGS);
89 1.1 joerg static int pre_RS(DECL_ARGS);
90 1.1 joerg static int pre_SH(DECL_ARGS);
91 1.1 joerg static int pre_SS(DECL_ARGS);
92 1.1 joerg static int pre_TP(DECL_ARGS);
93 1.1 joerg static int pre_ign(DECL_ARGS);
94 1.5 joerg static int pre_in(DECL_ARGS);
95 1.5 joerg static int pre_literal(DECL_ARGS);
96 1.1 joerg static int pre_sp(DECL_ARGS);
97 1.6 joerg static int pre_ft(DECL_ARGS);
98 1.1 joerg
99 1.1 joerg static void post_IP(DECL_ARGS);
100 1.1 joerg static void post_HP(DECL_ARGS);
101 1.1 joerg static void post_RS(DECL_ARGS);
102 1.1 joerg static void post_SH(DECL_ARGS);
103 1.1 joerg static void post_SS(DECL_ARGS);
104 1.1 joerg static void post_TP(DECL_ARGS);
105 1.1 joerg
106 1.1 joerg static const struct termact termacts[MAN_MAX] = {
107 1.5 joerg { pre_sp, NULL, MAN_NOTEXT }, /* br */
108 1.2 joerg { NULL, NULL, 0 }, /* TH */
109 1.2 joerg { pre_SH, post_SH, 0 }, /* SH */
110 1.2 joerg { pre_SS, post_SS, 0 }, /* SS */
111 1.2 joerg { pre_TP, post_TP, 0 }, /* TP */
112 1.2 joerg { pre_PP, NULL, 0 }, /* LP */
113 1.2 joerg { pre_PP, NULL, 0 }, /* PP */
114 1.2 joerg { pre_PP, NULL, 0 }, /* P */
115 1.2 joerg { pre_IP, post_IP, 0 }, /* IP */
116 1.2 joerg { pre_HP, post_HP, 0 }, /* HP */
117 1.2 joerg { NULL, NULL, 0 }, /* SM */
118 1.2 joerg { pre_B, NULL, 0 }, /* SB */
119 1.6 joerg { pre_alternate, NULL, 0 }, /* BI */
120 1.6 joerg { pre_alternate, NULL, 0 }, /* IB */
121 1.6 joerg { pre_alternate, NULL, 0 }, /* BR */
122 1.6 joerg { pre_alternate, NULL, 0 }, /* RB */
123 1.2 joerg { NULL, NULL, 0 }, /* R */
124 1.2 joerg { pre_B, NULL, 0 }, /* B */
125 1.2 joerg { pre_I, NULL, 0 }, /* I */
126 1.6 joerg { pre_alternate, NULL, 0 }, /* IR */
127 1.6 joerg { pre_alternate, NULL, 0 }, /* RI */
128 1.7 joerg { pre_ign, NULL, MAN_NOTEXT }, /* na */
129 1.2 joerg { pre_sp, NULL, MAN_NOTEXT }, /* sp */
130 1.5 joerg { pre_literal, NULL, 0 }, /* nf */
131 1.5 joerg { pre_literal, NULL, 0 }, /* fi */
132 1.2 joerg { NULL, NULL, 0 }, /* RE */
133 1.2 joerg { pre_RS, post_RS, 0 }, /* RS */
134 1.2 joerg { pre_ign, NULL, 0 }, /* DT */
135 1.2 joerg { pre_ign, NULL, 0 }, /* UC */
136 1.2 joerg { pre_ign, NULL, 0 }, /* PD */
137 1.2 joerg { pre_ign, NULL, 0 }, /* AT */
138 1.5 joerg { pre_in, NULL, MAN_NOTEXT }, /* in */
139 1.6 joerg { pre_ft, NULL, MAN_NOTEXT }, /* ft */
140 1.1 joerg };
141 1.1 joerg
142 1.1 joerg
143 1.1 joerg
144 1.1 joerg void
145 1.1 joerg terminal_man(void *arg, const struct man *man)
146 1.1 joerg {
147 1.1 joerg struct termp *p;
148 1.1 joerg const struct man_node *n;
149 1.1 joerg const struct man_meta *m;
150 1.1 joerg struct mtermp mt;
151 1.1 joerg
152 1.1 joerg p = (struct termp *)arg;
153 1.1 joerg
154 1.2 joerg p->overstep = 0;
155 1.2 joerg p->maxrmargin = p->defrmargin;
156 1.4 joerg p->tabwidth = term_len(p, 5);
157 1.2 joerg
158 1.1 joerg if (NULL == p->symtab)
159 1.1 joerg switch (p->enc) {
160 1.1 joerg case (TERMENC_ASCII):
161 1.1 joerg p->symtab = chars_init(CHARS_ASCII);
162 1.1 joerg break;
163 1.1 joerg default:
164 1.1 joerg abort();
165 1.1 joerg /* NOTREACHED */
166 1.1 joerg }
167 1.1 joerg
168 1.1 joerg n = man_node(man);
169 1.1 joerg m = man_meta(man);
170 1.1 joerg
171 1.3 joerg term_begin(p, print_man_head, print_man_foot, m);
172 1.1 joerg p->flags |= TERMP_NOSPACE;
173 1.1 joerg
174 1.1 joerg mt.fl = 0;
175 1.4 joerg mt.lmargin = term_len(p, INDENT);
176 1.4 joerg mt.offset = term_len(p, INDENT);
177 1.1 joerg
178 1.1 joerg if (n->child)
179 1.2 joerg print_man_nodelist(p, &mt, n->child, m);
180 1.2 joerg
181 1.2 joerg term_end(p);
182 1.1 joerg }
183 1.1 joerg
184 1.1 joerg
185 1.4 joerg static size_t
186 1.4 joerg a2height(const struct termp *p, const char *cp)
187 1.1 joerg {
188 1.1 joerg struct roffsu su;
189 1.1 joerg
190 1.4 joerg if ( ! a2roffsu(cp, &su, SCALE_VS))
191 1.4 joerg SCALE_VS_INIT(&su, term_strlen(p, cp));
192 1.1 joerg
193 1.4 joerg return(term_vspan(p, &su));
194 1.1 joerg }
195 1.1 joerg
196 1.1 joerg
197 1.1 joerg static int
198 1.4 joerg a2width(const struct termp *p, const char *cp)
199 1.1 joerg {
200 1.1 joerg struct roffsu su;
201 1.1 joerg
202 1.4 joerg if ( ! a2roffsu(cp, &su, SCALE_BU))
203 1.1 joerg return(-1);
204 1.1 joerg
205 1.4 joerg return((int)term_hspan(p, &su));
206 1.1 joerg }
207 1.1 joerg
208 1.1 joerg
209 1.1 joerg static void
210 1.1 joerg print_bvspace(struct termp *p, const struct man_node *n)
211 1.1 joerg {
212 1.1 joerg term_newln(p);
213 1.1 joerg
214 1.7 joerg if (n->body && n->body->child && MAN_TBL == n->body->child->type)
215 1.7 joerg return;
216 1.7 joerg
217 1.1 joerg if (NULL == n->prev)
218 1.1 joerg return;
219 1.1 joerg
220 1.1 joerg if (MAN_SS == n->prev->tok)
221 1.1 joerg return;
222 1.1 joerg if (MAN_SH == n->prev->tok)
223 1.1 joerg return;
224 1.1 joerg
225 1.1 joerg term_vspace(p);
226 1.1 joerg }
227 1.1 joerg
228 1.1 joerg
229 1.1 joerg /* ARGSUSED */
230 1.1 joerg static int
231 1.1 joerg pre_ign(DECL_ARGS)
232 1.1 joerg {
233 1.1 joerg
234 1.1 joerg return(0);
235 1.1 joerg }
236 1.1 joerg
237 1.1 joerg
238 1.1 joerg /* ARGSUSED */
239 1.1 joerg static int
240 1.1 joerg pre_I(DECL_ARGS)
241 1.1 joerg {
242 1.1 joerg
243 1.2 joerg term_fontrepl(p, TERMFONT_UNDER);
244 1.1 joerg return(1);
245 1.1 joerg }
246 1.1 joerg
247 1.1 joerg
248 1.1 joerg /* ARGSUSED */
249 1.1 joerg static int
250 1.5 joerg pre_literal(DECL_ARGS)
251 1.1 joerg {
252 1.1 joerg
253 1.5 joerg term_newln(p);
254 1.6 joerg
255 1.6 joerg if (MAN_nf == n->tok)
256 1.5 joerg mt->fl |= MANT_LITERAL;
257 1.6 joerg else
258 1.5 joerg mt->fl &= ~MANT_LITERAL;
259 1.5 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.5 joerg switch (n->tok) {
418 1.5 joerg case (MAN_br):
419 1.5 joerg len = 0;
420 1.5 joerg break;
421 1.5 joerg default:
422 1.5 joerg len = n->child ? a2height(p, n->child->string) : 1;
423 1.5 joerg break;
424 1.5 joerg }
425 1.5 joerg
426 1.5 joerg if (0 == len)
427 1.5 joerg term_newln(p);
428 1.5 joerg for (i = 0; i < len; i++)
429 1.5 joerg term_vspace(p);
430 1.1 joerg
431 1.1 joerg return(0);
432 1.1 joerg }
433 1.1 joerg
434 1.1 joerg
435 1.1 joerg /* ARGSUSED */
436 1.1 joerg static int
437 1.1 joerg pre_HP(DECL_ARGS)
438 1.1 joerg {
439 1.1 joerg size_t len;
440 1.1 joerg int ival;
441 1.1 joerg const struct man_node *nn;
442 1.1 joerg
443 1.1 joerg switch (n->type) {
444 1.1 joerg case (MAN_BLOCK):
445 1.1 joerg print_bvspace(p, n);
446 1.1 joerg return(1);
447 1.1 joerg case (MAN_BODY):
448 1.1 joerg p->flags |= TERMP_NOBREAK;
449 1.1 joerg p->flags |= TERMP_TWOSPACE;
450 1.1 joerg break;
451 1.1 joerg default:
452 1.1 joerg return(0);
453 1.1 joerg }
454 1.1 joerg
455 1.1 joerg len = mt->lmargin;
456 1.1 joerg ival = -1;
457 1.1 joerg
458 1.1 joerg /* Calculate offset. */
459 1.1 joerg
460 1.1 joerg if (NULL != (nn = n->parent->head->child))
461 1.4 joerg if ((ival = a2width(p, nn->string)) >= 0)
462 1.1 joerg len = (size_t)ival;
463 1.1 joerg
464 1.1 joerg if (0 == len)
465 1.4 joerg len = term_len(p, 1);
466 1.1 joerg
467 1.1 joerg p->offset = mt->offset;
468 1.1 joerg p->rmargin = mt->offset + len;
469 1.1 joerg
470 1.1 joerg if (ival >= 0)
471 1.1 joerg mt->lmargin = (size_t)ival;
472 1.1 joerg
473 1.1 joerg return(1);
474 1.1 joerg }
475 1.1 joerg
476 1.1 joerg
477 1.1 joerg /* ARGSUSED */
478 1.1 joerg static void
479 1.1 joerg post_HP(DECL_ARGS)
480 1.1 joerg {
481 1.1 joerg
482 1.1 joerg switch (n->type) {
483 1.1 joerg case (MAN_BLOCK):
484 1.1 joerg term_flushln(p);
485 1.1 joerg break;
486 1.1 joerg case (MAN_BODY):
487 1.1 joerg term_flushln(p);
488 1.1 joerg p->flags &= ~TERMP_NOBREAK;
489 1.1 joerg p->flags &= ~TERMP_TWOSPACE;
490 1.1 joerg p->offset = mt->offset;
491 1.1 joerg p->rmargin = p->maxrmargin;
492 1.1 joerg break;
493 1.1 joerg default:
494 1.1 joerg break;
495 1.1 joerg }
496 1.1 joerg }
497 1.1 joerg
498 1.1 joerg
499 1.1 joerg /* ARGSUSED */
500 1.1 joerg static int
501 1.1 joerg pre_PP(DECL_ARGS)
502 1.1 joerg {
503 1.1 joerg
504 1.1 joerg switch (n->type) {
505 1.1 joerg case (MAN_BLOCK):
506 1.4 joerg mt->lmargin = term_len(p, INDENT);
507 1.1 joerg print_bvspace(p, n);
508 1.1 joerg break;
509 1.1 joerg default:
510 1.1 joerg p->offset = mt->offset;
511 1.1 joerg break;
512 1.1 joerg }
513 1.1 joerg
514 1.6 joerg return(MAN_HEAD != n->type);
515 1.1 joerg }
516 1.1 joerg
517 1.1 joerg
518 1.1 joerg /* ARGSUSED */
519 1.1 joerg static int
520 1.1 joerg pre_IP(DECL_ARGS)
521 1.1 joerg {
522 1.1 joerg const struct man_node *nn;
523 1.1 joerg size_t len;
524 1.6 joerg int savelit, ival;
525 1.1 joerg
526 1.1 joerg switch (n->type) {
527 1.1 joerg case (MAN_BODY):
528 1.1 joerg p->flags |= TERMP_NOLPAD;
529 1.1 joerg p->flags |= TERMP_NOSPACE;
530 1.1 joerg break;
531 1.1 joerg case (MAN_HEAD):
532 1.1 joerg p->flags |= TERMP_NOBREAK;
533 1.1 joerg break;
534 1.1 joerg case (MAN_BLOCK):
535 1.1 joerg print_bvspace(p, n);
536 1.1 joerg /* FALLTHROUGH */
537 1.1 joerg default:
538 1.1 joerg return(1);
539 1.1 joerg }
540 1.1 joerg
541 1.1 joerg len = mt->lmargin;
542 1.1 joerg ival = -1;
543 1.1 joerg
544 1.6 joerg /* Calculate the offset from the optional second argument. */
545 1.1 joerg if (NULL != (nn = n->parent->head->child))
546 1.6 joerg if (NULL != (nn = nn->next))
547 1.4 joerg if ((ival = a2width(p, nn->string)) >= 0)
548 1.1 joerg len = (size_t)ival;
549 1.1 joerg
550 1.1 joerg switch (n->type) {
551 1.1 joerg case (MAN_HEAD):
552 1.1 joerg /* Handle zero-width lengths. */
553 1.1 joerg if (0 == len)
554 1.4 joerg len = term_len(p, 1);
555 1.1 joerg
556 1.1 joerg p->offset = mt->offset;
557 1.1 joerg p->rmargin = mt->offset + len;
558 1.1 joerg if (ival < 0)
559 1.1 joerg break;
560 1.1 joerg
561 1.1 joerg /* Set the saved left-margin. */
562 1.1 joerg mt->lmargin = (size_t)ival;
563 1.1 joerg
564 1.6 joerg savelit = MANT_LITERAL & mt->fl;
565 1.6 joerg mt->fl &= ~MANT_LITERAL;
566 1.6 joerg
567 1.6 joerg if (n->child)
568 1.6 joerg print_man_node(p, mt, n->child, m);
569 1.6 joerg
570 1.6 joerg if (savelit)
571 1.6 joerg mt->fl |= MANT_LITERAL;
572 1.6 joerg
573 1.1 joerg return(0);
574 1.1 joerg case (MAN_BODY):
575 1.1 joerg p->offset = mt->offset + len;
576 1.1 joerg p->rmargin = p->maxrmargin;
577 1.1 joerg break;
578 1.1 joerg default:
579 1.1 joerg break;
580 1.1 joerg }
581 1.1 joerg
582 1.1 joerg return(1);
583 1.1 joerg }
584 1.1 joerg
585 1.1 joerg
586 1.1 joerg /* ARGSUSED */
587 1.1 joerg static void
588 1.1 joerg post_IP(DECL_ARGS)
589 1.1 joerg {
590 1.1 joerg
591 1.1 joerg switch (n->type) {
592 1.1 joerg case (MAN_HEAD):
593 1.1 joerg term_flushln(p);
594 1.1 joerg p->flags &= ~TERMP_NOBREAK;
595 1.1 joerg p->rmargin = p->maxrmargin;
596 1.1 joerg break;
597 1.1 joerg case (MAN_BODY):
598 1.6 joerg term_newln(p);
599 1.1 joerg p->flags &= ~TERMP_NOLPAD;
600 1.1 joerg break;
601 1.1 joerg default:
602 1.1 joerg break;
603 1.1 joerg }
604 1.1 joerg }
605 1.1 joerg
606 1.1 joerg
607 1.1 joerg /* ARGSUSED */
608 1.1 joerg static int
609 1.1 joerg pre_TP(DECL_ARGS)
610 1.1 joerg {
611 1.1 joerg const struct man_node *nn;
612 1.1 joerg size_t len;
613 1.6 joerg int savelit, ival;
614 1.1 joerg
615 1.1 joerg switch (n->type) {
616 1.1 joerg case (MAN_HEAD):
617 1.1 joerg p->flags |= TERMP_NOBREAK;
618 1.1 joerg break;
619 1.1 joerg case (MAN_BODY):
620 1.1 joerg p->flags |= TERMP_NOLPAD;
621 1.1 joerg p->flags |= TERMP_NOSPACE;
622 1.1 joerg break;
623 1.1 joerg case (MAN_BLOCK):
624 1.1 joerg print_bvspace(p, n);
625 1.1 joerg /* FALLTHROUGH */
626 1.1 joerg default:
627 1.1 joerg return(1);
628 1.1 joerg }
629 1.1 joerg
630 1.1 joerg len = (size_t)mt->lmargin;
631 1.1 joerg ival = -1;
632 1.1 joerg
633 1.1 joerg /* Calculate offset. */
634 1.1 joerg
635 1.2 joerg if (NULL != (nn = n->parent->head->child)) {
636 1.2 joerg while (nn && MAN_TEXT != nn->type)
637 1.2 joerg nn = nn->next;
638 1.2 joerg if (nn && nn->next)
639 1.4 joerg if ((ival = a2width(p, nn->string)) >= 0)
640 1.1 joerg len = (size_t)ival;
641 1.2 joerg }
642 1.1 joerg
643 1.1 joerg switch (n->type) {
644 1.1 joerg case (MAN_HEAD):
645 1.1 joerg /* Handle zero-length properly. */
646 1.1 joerg if (0 == len)
647 1.4 joerg len = term_len(p, 1);
648 1.1 joerg
649 1.1 joerg p->offset = mt->offset;
650 1.1 joerg p->rmargin = mt->offset + len;
651 1.1 joerg
652 1.6 joerg savelit = MANT_LITERAL & mt->fl;
653 1.6 joerg mt->fl &= ~MANT_LITERAL;
654 1.6 joerg
655 1.1 joerg /* Don't print same-line elements. */
656 1.6 joerg for (nn = n->child; nn; nn = nn->next)
657 1.1 joerg if (nn->line > n->line)
658 1.2 joerg print_man_node(p, mt, nn, m);
659 1.1 joerg
660 1.6 joerg if (savelit)
661 1.6 joerg mt->fl |= MANT_LITERAL;
662 1.6 joerg
663 1.1 joerg if (ival >= 0)
664 1.1 joerg mt->lmargin = (size_t)ival;
665 1.1 joerg
666 1.1 joerg return(0);
667 1.1 joerg case (MAN_BODY):
668 1.1 joerg p->offset = mt->offset + len;
669 1.1 joerg p->rmargin = p->maxrmargin;
670 1.1 joerg break;
671 1.1 joerg default:
672 1.1 joerg break;
673 1.1 joerg }
674 1.1 joerg
675 1.1 joerg return(1);
676 1.1 joerg }
677 1.1 joerg
678 1.1 joerg
679 1.1 joerg /* ARGSUSED */
680 1.1 joerg static void
681 1.1 joerg post_TP(DECL_ARGS)
682 1.1 joerg {
683 1.1 joerg
684 1.1 joerg switch (n->type) {
685 1.1 joerg case (MAN_HEAD):
686 1.1 joerg term_flushln(p);
687 1.1 joerg p->flags &= ~TERMP_NOBREAK;
688 1.1 joerg p->flags &= ~TERMP_TWOSPACE;
689 1.1 joerg p->rmargin = p->maxrmargin;
690 1.1 joerg break;
691 1.1 joerg case (MAN_BODY):
692 1.6 joerg term_newln(p);
693 1.1 joerg p->flags &= ~TERMP_NOLPAD;
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.4 joerg mt->lmargin = term_len(p, INDENT);
709 1.4 joerg mt->offset = term_len(p, INDENT);
710 1.1 joerg /* If following a prior empty `SS', no vspace. */
711 1.1 joerg if (n->prev && MAN_SS == n->prev->tok)
712 1.1 joerg if (NULL == n->prev->body->child)
713 1.1 joerg break;
714 1.1 joerg if (NULL == n->prev)
715 1.1 joerg break;
716 1.1 joerg term_vspace(p);
717 1.1 joerg break;
718 1.1 joerg case (MAN_HEAD):
719 1.2 joerg term_fontrepl(p, TERMFONT_BOLD);
720 1.4 joerg p->offset = term_len(p, HALFINDENT);
721 1.1 joerg break;
722 1.1 joerg case (MAN_BODY):
723 1.1 joerg p->offset = mt->offset;
724 1.1 joerg break;
725 1.1 joerg default:
726 1.1 joerg break;
727 1.1 joerg }
728 1.1 joerg
729 1.1 joerg return(1);
730 1.1 joerg }
731 1.1 joerg
732 1.1 joerg
733 1.1 joerg /* ARGSUSED */
734 1.1 joerg static void
735 1.1 joerg post_SS(DECL_ARGS)
736 1.1 joerg {
737 1.1 joerg
738 1.1 joerg switch (n->type) {
739 1.1 joerg case (MAN_HEAD):
740 1.1 joerg term_newln(p);
741 1.1 joerg break;
742 1.1 joerg case (MAN_BODY):
743 1.1 joerg term_newln(p);
744 1.1 joerg break;
745 1.1 joerg default:
746 1.1 joerg break;
747 1.1 joerg }
748 1.1 joerg }
749 1.1 joerg
750 1.1 joerg
751 1.1 joerg /* ARGSUSED */
752 1.1 joerg static int
753 1.1 joerg pre_SH(DECL_ARGS)
754 1.1 joerg {
755 1.1 joerg
756 1.1 joerg switch (n->type) {
757 1.1 joerg case (MAN_BLOCK):
758 1.4 joerg mt->lmargin = term_len(p, INDENT);
759 1.4 joerg mt->offset = term_len(p, INDENT);
760 1.1 joerg /* If following a prior empty `SH', no vspace. */
761 1.1 joerg if (n->prev && MAN_SH == n->prev->tok)
762 1.1 joerg if (NULL == n->prev->body->child)
763 1.1 joerg break;
764 1.2 joerg /* If the first macro, no vspae. */
765 1.2 joerg if (NULL == n->prev)
766 1.2 joerg break;
767 1.1 joerg term_vspace(p);
768 1.1 joerg break;
769 1.1 joerg case (MAN_HEAD):
770 1.2 joerg term_fontrepl(p, TERMFONT_BOLD);
771 1.1 joerg p->offset = 0;
772 1.1 joerg break;
773 1.1 joerg case (MAN_BODY):
774 1.1 joerg p->offset = mt->offset;
775 1.1 joerg break;
776 1.1 joerg default:
777 1.1 joerg break;
778 1.1 joerg }
779 1.1 joerg
780 1.1 joerg return(1);
781 1.1 joerg }
782 1.1 joerg
783 1.1 joerg
784 1.1 joerg /* ARGSUSED */
785 1.1 joerg static void
786 1.1 joerg post_SH(DECL_ARGS)
787 1.1 joerg {
788 1.1 joerg
789 1.1 joerg switch (n->type) {
790 1.1 joerg case (MAN_HEAD):
791 1.1 joerg term_newln(p);
792 1.1 joerg break;
793 1.1 joerg case (MAN_BODY):
794 1.1 joerg term_newln(p);
795 1.1 joerg break;
796 1.1 joerg default:
797 1.1 joerg break;
798 1.1 joerg }
799 1.1 joerg }
800 1.1 joerg
801 1.1 joerg
802 1.1 joerg /* ARGSUSED */
803 1.1 joerg static int
804 1.1 joerg pre_RS(DECL_ARGS)
805 1.1 joerg {
806 1.1 joerg const struct man_node *nn;
807 1.1 joerg int ival;
808 1.1 joerg
809 1.1 joerg switch (n->type) {
810 1.1 joerg case (MAN_BLOCK):
811 1.1 joerg term_newln(p);
812 1.1 joerg return(1);
813 1.1 joerg case (MAN_HEAD):
814 1.1 joerg return(0);
815 1.1 joerg default:
816 1.1 joerg break;
817 1.1 joerg }
818 1.1 joerg
819 1.1 joerg if (NULL == (nn = n->parent->head->child)) {
820 1.4 joerg mt->offset = mt->lmargin + term_len(p, INDENT);
821 1.1 joerg p->offset = mt->offset;
822 1.1 joerg return(1);
823 1.1 joerg }
824 1.1 joerg
825 1.4 joerg if ((ival = a2width(p, nn->string)) < 0)
826 1.1 joerg return(1);
827 1.1 joerg
828 1.4 joerg mt->offset = term_len(p, INDENT) + (size_t)ival;
829 1.1 joerg p->offset = mt->offset;
830 1.1 joerg
831 1.1 joerg return(1);
832 1.1 joerg }
833 1.1 joerg
834 1.1 joerg
835 1.1 joerg /* ARGSUSED */
836 1.1 joerg static void
837 1.1 joerg post_RS(DECL_ARGS)
838 1.1 joerg {
839 1.1 joerg
840 1.1 joerg switch (n->type) {
841 1.1 joerg case (MAN_BLOCK):
842 1.4 joerg mt->offset = mt->lmargin = term_len(p, INDENT);
843 1.1 joerg break;
844 1.2 joerg case (MAN_HEAD):
845 1.2 joerg break;
846 1.1 joerg default:
847 1.1 joerg term_newln(p);
848 1.4 joerg p->offset = term_len(p, INDENT);
849 1.1 joerg break;
850 1.1 joerg }
851 1.1 joerg }
852 1.1 joerg
853 1.1 joerg
854 1.1 joerg static void
855 1.2 joerg print_man_node(DECL_ARGS)
856 1.1 joerg {
857 1.2 joerg size_t rm, rmax;
858 1.2 joerg int c;
859 1.1 joerg
860 1.1 joerg switch (n->type) {
861 1.1 joerg case(MAN_TEXT):
862 1.7 joerg /*
863 1.7 joerg * If we have a blank line, output a vertical space.
864 1.7 joerg * If we have a space as the first character, break
865 1.7 joerg * before printing the line's data.
866 1.7 joerg */
867 1.7 joerg if ('\0' == *n->string) {
868 1.1 joerg term_vspace(p);
869 1.7 joerg return;
870 1.7 joerg } else if (' ' == *n->string && MAN_LINE & n->flags)
871 1.7 joerg term_newln(p);
872 1.2 joerg
873 1.1 joerg term_word(p, n->string);
874 1.2 joerg
875 1.7 joerg /*
876 1.7 joerg * If we're in a literal context, make sure that words
877 1.7 joerg * togehter on the same line stay together. This is a
878 1.7 joerg * POST-printing call, so we check the NEXT word. Since
879 1.7 joerg * -man doesn't have nested macros, we don't need to be
880 1.7 joerg * more specific than this.
881 1.7 joerg */
882 1.7 joerg if (MANT_LITERAL & mt->fl &&
883 1.7 joerg (NULL == n->next ||
884 1.7 joerg n->next->line > n->line)) {
885 1.2 joerg rm = p->rmargin;
886 1.2 joerg rmax = p->maxrmargin;
887 1.2 joerg p->rmargin = p->maxrmargin = TERM_MAXMARGIN;
888 1.1 joerg p->flags |= TERMP_NOSPACE;
889 1.1 joerg term_flushln(p);
890 1.6 joerg p->flags &= ~TERMP_NOLPAD;
891 1.2 joerg p->rmargin = rm;
892 1.2 joerg p->maxrmargin = rmax;
893 1.1 joerg }
894 1.7 joerg
895 1.7 joerg if (MAN_EOS & n->flags)
896 1.7 joerg p->flags |= TERMP_SENTENCE;
897 1.7 joerg return;
898 1.7 joerg case (MAN_EQN):
899 1.7 joerg term_word(p, n->eqn->data);
900 1.7 joerg return;
901 1.6 joerg case (MAN_TBL):
902 1.7 joerg /*
903 1.7 joerg * Tables are preceded by a newline. Then process a
904 1.7 joerg * table line, which will cause line termination,
905 1.7 joerg */
906 1.6 joerg if (TBL_SPAN_FIRST & n->span->flags)
907 1.6 joerg term_newln(p);
908 1.6 joerg term_tbl(p, n->span);
909 1.7 joerg return;
910 1.1 joerg default:
911 1.1 joerg break;
912 1.1 joerg }
913 1.1 joerg
914 1.7 joerg if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
915 1.7 joerg term_fontrepl(p, TERMFONT_NONE);
916 1.7 joerg
917 1.7 joerg c = 1;
918 1.7 joerg if (termacts[n->tok].pre)
919 1.7 joerg c = (*termacts[n->tok].pre)(p, mt, n, m);
920 1.7 joerg
921 1.1 joerg if (c && n->child)
922 1.2 joerg print_man_nodelist(p, mt, n->child, m);
923 1.1 joerg
924 1.7 joerg if (termacts[n->tok].post)
925 1.7 joerg (*termacts[n->tok].post)(p, mt, n, m);
926 1.7 joerg if ( ! (MAN_NOTEXT & termacts[n->tok].flags))
927 1.7 joerg term_fontrepl(p, TERMFONT_NONE);
928 1.2 joerg
929 1.2 joerg if (MAN_EOS & n->flags)
930 1.2 joerg p->flags |= TERMP_SENTENCE;
931 1.1 joerg }
932 1.1 joerg
933 1.1 joerg
934 1.1 joerg static void
935 1.2 joerg print_man_nodelist(DECL_ARGS)
936 1.1 joerg {
937 1.1 joerg
938 1.2 joerg print_man_node(p, mt, n, m);
939 1.1 joerg if ( ! n->next)
940 1.1 joerg return;
941 1.2 joerg print_man_nodelist(p, mt, n->next, m);
942 1.1 joerg }
943 1.1 joerg
944 1.1 joerg
945 1.1 joerg static void
946 1.2 joerg print_man_foot(struct termp *p, const void *arg)
947 1.1 joerg {
948 1.2 joerg const struct man_meta *meta;
949 1.2 joerg
950 1.2 joerg meta = (const struct man_meta *)arg;
951 1.1 joerg
952 1.2 joerg term_fontrepl(p, TERMFONT_NONE);
953 1.1 joerg
954 1.1 joerg term_vspace(p);
955 1.2 joerg term_vspace(p);
956 1.2 joerg term_vspace(p);
957 1.1 joerg
958 1.1 joerg p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
959 1.7 joerg p->rmargin = p->maxrmargin - term_strlen(p, meta->date);
960 1.1 joerg p->offset = 0;
961 1.1 joerg
962 1.6 joerg /* term_strlen() can return zero. */
963 1.6 joerg if (p->rmargin == p->maxrmargin)
964 1.6 joerg p->rmargin--;
965 1.6 joerg
966 1.1 joerg if (meta->source)
967 1.1 joerg term_word(p, meta->source);
968 1.1 joerg if (meta->source)
969 1.1 joerg term_word(p, "");
970 1.1 joerg term_flushln(p);
971 1.1 joerg
972 1.1 joerg p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
973 1.1 joerg p->offset = p->rmargin;
974 1.1 joerg p->rmargin = p->maxrmargin;
975 1.1 joerg p->flags &= ~TERMP_NOBREAK;
976 1.1 joerg
977 1.7 joerg term_word(p, meta->date);
978 1.1 joerg term_flushln(p);
979 1.1 joerg }
980 1.1 joerg
981 1.1 joerg
982 1.1 joerg static void
983 1.2 joerg print_man_head(struct termp *p, const void *arg)
984 1.1 joerg {
985 1.2 joerg char buf[BUFSIZ], title[BUFSIZ];
986 1.2 joerg size_t buflen, titlen;
987 1.2 joerg const struct man_meta *m;
988 1.2 joerg
989 1.2 joerg m = (const struct man_meta *)arg;
990 1.2 joerg
991 1.2 joerg /*
992 1.2 joerg * Note that old groff would spit out some spaces before the
993 1.2 joerg * header. We discontinue this strange behaviour, but at one
994 1.2 joerg * point we did so here.
995 1.2 joerg */
996 1.1 joerg
997 1.1 joerg p->rmargin = p->maxrmargin;
998 1.2 joerg
999 1.1 joerg p->offset = 0;
1000 1.2 joerg buf[0] = title[0] = '\0';
1001 1.1 joerg
1002 1.2 joerg if (m->vol)
1003 1.2 joerg strlcpy(buf, m->vol, BUFSIZ);
1004 1.4 joerg buflen = term_strlen(p, buf);
1005 1.1 joerg
1006 1.2 joerg snprintf(title, BUFSIZ, "%s(%s)", m->title, m->msec);
1007 1.4 joerg titlen = term_strlen(p, title);
1008 1.1 joerg
1009 1.1 joerg p->offset = 0;
1010 1.2 joerg p->rmargin = 2 * (titlen+1) + buflen < p->maxrmargin ?
1011 1.4 joerg (p->maxrmargin -
1012 1.4 joerg term_strlen(p, buf) + term_len(p, 1)) / 2 :
1013 1.2 joerg p->maxrmargin - buflen;
1014 1.1 joerg p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
1015 1.1 joerg
1016 1.1 joerg term_word(p, title);
1017 1.1 joerg term_flushln(p);
1018 1.1 joerg
1019 1.1 joerg p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1020 1.1 joerg p->offset = p->rmargin;
1021 1.2 joerg p->rmargin = p->offset + buflen + titlen < p->maxrmargin ?
1022 1.2 joerg p->maxrmargin - titlen : p->maxrmargin;
1023 1.1 joerg
1024 1.1 joerg term_word(p, buf);
1025 1.1 joerg term_flushln(p);
1026 1.1 joerg
1027 1.1 joerg p->flags &= ~TERMP_NOBREAK;
1028 1.2 joerg if (p->rmargin + titlen <= p->maxrmargin) {
1029 1.2 joerg p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
1030 1.2 joerg p->offset = p->rmargin;
1031 1.2 joerg p->rmargin = p->maxrmargin;
1032 1.2 joerg term_word(p, title);
1033 1.2 joerg term_flushln(p);
1034 1.2 joerg }
1035 1.1 joerg
1036 1.1 joerg p->rmargin = p->maxrmargin;
1037 1.1 joerg p->offset = 0;
1038 1.1 joerg p->flags &= ~TERMP_NOSPACE;
1039 1.1 joerg
1040 1.2 joerg /*
1041 1.2 joerg * Groff likes to have some leading spaces before content. Well
1042 1.2 joerg * that's fine by me.
1043 1.2 joerg */
1044 1.2 joerg
1045 1.2 joerg term_vspace(p);
1046 1.2 joerg term_vspace(p);
1047 1.2 joerg term_vspace(p);
1048 1.1 joerg }
1049