]>
git.cameronkatri.com Git - mandoc.git/blob - term_tab.c
1 /* $Id: term_tab.c,v 1.7 2021/10/04 18:56:31 schwarze Exp $ */
3 * Copyright (c) 2017, 2021 Ingo Schwarze <schwarze@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
25 #include "mandoc_aux.h"
30 size_t *t
; /* Allocated array of tab positions. */
31 size_t s
; /* Allocated number of positions. */
32 size_t n
; /* Currently used number of positions. */
36 struct tablist a
; /* All tab positions for lookup. */
37 struct tablist p
; /* Periodic tab positions to add. */
38 struct tablist
*r
; /* Tablist currently being recorded. */
39 size_t d
; /* Default tab width in units of n. */
44 term_tab_set(const struct termp
*p
, const char *arg
)
51 /* Special arguments: clear all tabs or switch lists. */
54 tabs
.a
.n
= tabs
.p
.n
= 0;
57 a2roffsu(".8i", &su
, SCALE_IN
);
58 tabs
.d
= term_hen(p
, &su
);
62 if (arg
[0] == 'T' && arg
[1] == '\0') {
67 /* Parse the sign, the number, and the unit. */
74 if (a2roffsu(arg
, &su
, SCALE_EM
) == NULL
)
77 /* Select the list, and extend it if it is full. */
82 tl
->t
= mandoc_reallocarray(tl
->t
, tl
->s
, sizeof(*tl
->t
));
85 /* Append the new position. */
87 pos
= term_hen(p
, &su
);
90 tl
->t
[tl
->n
] += tl
->t
[tl
->n
- 1];
95 * Simplified version without a parser,
96 * never incremental, never periodic, for use by tbl(7).
99 term_tab_iset(size_t inc
)
101 if (tabs
.a
.n
>= tabs
.a
.s
) {
103 tabs
.a
.t
= mandoc_reallocarray(tabs
.a
.t
, tabs
.a
.s
,
106 tabs
.a
.t
[tabs
.a
.n
++] = inc
;
110 term_tab_next(size_t prev
)
118 tabs
.a
.n
+= tabs
.p
.n
;
119 if (tabs
.a
.s
< tabs
.a
.n
) {
121 tabs
.a
.t
= mandoc_reallocarray(tabs
.a
.t
,
122 tabs
.a
.s
, sizeof(*tabs
.a
.t
));
124 for (j
= 0; j
< tabs
.p
.n
; j
++)
125 tabs
.a
.t
[i
+ j
] = tabs
.p
.t
[j
] +
126 (i
? tabs
.a
.t
[i
- 1] : 0);
128 if (prev
< tabs
.a
.t
[i
])
138 memset(&tabs
, 0, sizeof(tabs
));