usr.sbin/makefs: Add -o c|C option to specify comp|check type
[dragonfly.git] / contrib / mdocml / term_tab.c
blob84b4c00c6e65f13bee9d8f885392f3b90b287edb
1 /* $Id: term_tab.c,v 1.6 2020/06/22 19:20:40 schwarze Exp $ */
2 /*
3 * Copyright (c) 2017 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.
17 #include "config.h"
19 #include <sys/types.h>
21 #include <stddef.h>
23 #include "mandoc_aux.h"
24 #include "out.h"
25 #include "term.h"
27 struct tablist {
28 size_t *t; /* Allocated array of tab positions. */
29 size_t s; /* Allocated number of positions. */
30 size_t n; /* Currently used number of positions. */
33 static struct {
34 struct tablist a; /* All tab positions for lookup. */
35 struct tablist p; /* Periodic tab positions to add. */
36 size_t d; /* Default tab width in units of n. */
37 } tabs;
40 void
41 term_tab_set(const struct termp *p, const char *arg)
43 static int recording_period;
45 struct roffsu su;
46 struct tablist *tl;
47 size_t pos;
48 int add;
50 /* Special arguments: clear all tabs or switch lists. */
52 if (arg == NULL) {
53 tabs.a.n = tabs.p.n = 0;
54 recording_period = 0;
55 if (tabs.d == 0) {
56 a2roffsu(".8i", &su, SCALE_IN);
57 tabs.d = term_hen(p, &su);
59 return;
61 if (arg[0] == 'T' && arg[1] == '\0') {
62 recording_period = 1;
63 return;
66 /* Parse the sign, the number, and the unit. */
68 if (*arg == '+') {
69 add = 1;
70 arg++;
71 } else
72 add = 0;
73 if (a2roffsu(arg, &su, SCALE_EM) == NULL)
74 return;
76 /* Select the list, and extend it if it is full. */
78 tl = recording_period ? &tabs.p : &tabs.a;
79 if (tl->n >= tl->s) {
80 tl->s += 8;
81 tl->t = mandoc_reallocarray(tl->t, tl->s, sizeof(*tl->t));
84 /* Append the new position. */
86 pos = term_hen(p, &su);
87 tl->t[tl->n] = pos;
88 if (add && tl->n)
89 tl->t[tl->n] += tl->t[tl->n - 1];
90 tl->n++;
94 * Simplified version without a parser,
95 * never incremental, never periodic, for use by tbl(7).
97 void
98 term_tab_iset(size_t inc)
100 if (tabs.a.n >= tabs.a.s) {
101 tabs.a.s += 8;
102 tabs.a.t = mandoc_reallocarray(tabs.a.t, tabs.a.s,
103 sizeof(*tabs.a.t));
105 tabs.a.t[tabs.a.n++] = inc;
108 size_t
109 term_tab_next(size_t prev)
111 size_t i, j;
113 for (i = 0;; i++) {
114 if (i == tabs.a.n) {
115 if (tabs.p.n == 0)
116 return prev;
117 tabs.a.n += tabs.p.n;
118 if (tabs.a.s < tabs.a.n) {
119 tabs.a.s = tabs.a.n;
120 tabs.a.t = mandoc_reallocarray(tabs.a.t,
121 tabs.a.s, sizeof(*tabs.a.t));
123 for (j = 0; j < tabs.p.n; j++)
124 tabs.a.t[i + j] = tabs.p.t[j] +
125 (i ? tabs.a.t[i - 1] : 0);
127 if (prev < tabs.a.t[i])
128 return tabs.a.t[i];