1 |
|
|
/* $OpenBSD: term_tab.c,v 1.4 2017/06/17 14:55:02 schwarze Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org> |
4 |
|
|
* |
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. |
8 |
|
|
* |
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. |
16 |
|
|
*/ |
17 |
|
|
#include <sys/types.h> |
18 |
|
|
|
19 |
|
|
#include <stddef.h> |
20 |
|
|
|
21 |
|
|
#include "mandoc_aux.h" |
22 |
|
|
#include "out.h" |
23 |
|
|
#include "term.h" |
24 |
|
|
|
25 |
|
|
struct tablist { |
26 |
|
|
size_t *t; /* Allocated array of tab positions. */ |
27 |
|
|
size_t s; /* Allocated number of positions. */ |
28 |
|
|
size_t n; /* Currently used number of positions. */ |
29 |
|
|
}; |
30 |
|
|
|
31 |
|
|
static struct { |
32 |
|
|
struct tablist a; /* All tab positions for lookup. */ |
33 |
|
|
struct tablist p; /* Periodic tab positions to add. */ |
34 |
|
|
size_t d; /* Default tab width in units of n. */ |
35 |
|
|
} tabs; |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
void |
39 |
|
|
term_tab_set(const struct termp *p, const char *arg) |
40 |
|
|
{ |
41 |
|
|
static int recording_period; |
42 |
|
|
|
43 |
|
31612 |
struct roffsu su; |
44 |
|
|
struct tablist *tl; |
45 |
|
|
size_t pos; |
46 |
|
|
int add; |
47 |
|
|
|
48 |
|
|
/* Special arguments: clear all tabs or switch lists. */ |
49 |
|
|
|
50 |
✓✓ |
15806 |
if (arg == NULL) { |
51 |
|
6735 |
tabs.a.n = tabs.p.n = 0; |
52 |
|
6735 |
recording_period = 0; |
53 |
✓✓ |
6735 |
if (tabs.d == 0) { |
54 |
|
2150 |
a2roffsu(".8i", &su, SCALE_IN); |
55 |
|
2150 |
tabs.d = term_hen(p, &su); |
56 |
|
2150 |
} |
57 |
|
6735 |
return; |
58 |
|
|
} |
59 |
✓✓✓✗
|
13584 |
if (arg[0] == 'T' && arg[1] == '\0') { |
60 |
|
4513 |
recording_period = 1; |
61 |
|
4513 |
return; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
/* Parse the sign, the number, and the unit. */ |
65 |
|
|
|
66 |
✓✓ |
4558 |
if (*arg == '+') { |
67 |
|
|
add = 1; |
68 |
|
18 |
arg++; |
69 |
|
18 |
} else |
70 |
|
|
add = 0; |
71 |
✗✓ |
4558 |
if (a2roffsu(arg, &su, SCALE_EM) == NULL) |
72 |
|
|
return; |
73 |
|
|
|
74 |
|
|
/* Select the list, and extend it if it is full. */ |
75 |
|
|
|
76 |
|
4558 |
tl = recording_period ? &tabs.p : &tabs.a; |
77 |
✓✓ |
4558 |
if (tl->n >= tl->s) { |
78 |
|
2150 |
tl->s += 8; |
79 |
|
2150 |
tl->t = mandoc_reallocarray(tl->t, tl->s, sizeof(*tl->t)); |
80 |
|
2150 |
} |
81 |
|
|
|
82 |
|
|
/* Append the new position. */ |
83 |
|
|
|
84 |
|
4558 |
pos = term_hen(p, &su); |
85 |
|
4558 |
tl->t[tl->n] = pos; |
86 |
✓✓✓✗
|
4576 |
if (add && tl->n) |
87 |
|
18 |
tl->t[tl->n] += tl->t[tl->n - 1]; |
88 |
|
4558 |
tl->n++; |
89 |
|
20364 |
} |
90 |
|
|
|
91 |
|
|
/* |
92 |
|
|
* Simplified version without a parser, |
93 |
|
|
* never incremental, never periodic, for use by tbl(7). |
94 |
|
|
*/ |
95 |
|
|
void |
96 |
|
|
term_tab_iset(size_t inc) |
97 |
|
|
{ |
98 |
✓✓ |
11710 |
if (tabs.a.n >= tabs.a.s) { |
99 |
|
95 |
tabs.a.s += 8; |
100 |
|
95 |
tabs.a.t = mandoc_reallocarray(tabs.a.t, tabs.a.s, |
101 |
|
|
sizeof(*tabs.a.t)); |
102 |
|
95 |
} |
103 |
|
5855 |
tabs.a.t[tabs.a.n++] = inc; |
104 |
|
5855 |
} |
105 |
|
|
|
106 |
|
|
size_t |
107 |
|
|
term_tab_next(size_t prev) |
108 |
|
|
{ |
109 |
|
|
size_t i, j; |
110 |
|
|
|
111 |
|
8231 |
for (i = 0;; i++) { |
112 |
✓✓ |
5127 |
if (i == tabs.a.n) { |
113 |
✓✓ |
314 |
if (tabs.p.n == 0) |
114 |
|
21 |
return prev; |
115 |
|
293 |
tabs.a.n += tabs.p.n; |
116 |
✓✓ |
293 |
if (tabs.a.s < tabs.a.n) { |
117 |
|
254 |
tabs.a.s = tabs.a.n; |
118 |
|
254 |
tabs.a.t = mandoc_reallocarray(tabs.a.t, |
119 |
|
|
tabs.a.s, sizeof(*tabs.a.t)); |
120 |
|
254 |
} |
121 |
✓✓ |
1208 |
for (j = 0; j < tabs.p.n; j++) |
122 |
|
622 |
tabs.a.t[i + j] = tabs.p.t[j] + |
123 |
✓✓ |
866 |
(i ? tabs.a.t[i - 1] : 0); |
124 |
|
|
} |
125 |
✓✓ |
5106 |
if (prev < tabs.a.t[i]) |
126 |
|
3083 |
return tabs.a.t[i]; |
127 |
|
|
} |
128 |
|
3104 |
} |