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