Print this page
Update to 1.12.3.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mandoc/out.c
+++ new/usr/src/cmd/mandoc/out.c
1 -/* $Id: out.c,v 1.43 2011/09/20 23:05:49 schwarze Exp $ */
1 +/* $Id: out.c,v 1.46 2013/10/05 20:30:05 schwarze Exp $ */
2 2 /*
3 3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
5 5 *
6 6 * Permission to use, copy, modify, and distribute this software for any
7 7 * purpose with or without fee is hereby granted, provided that the above
8 8 * copyright notice and this permission notice appear in all copies.
9 9 *
10 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 17 */
18 18 #ifdef HAVE_CONFIG_H
19 19 #include "config.h"
20 20 #endif
21 21
22 22 #include <sys/types.h>
23 23
24 24 #include <assert.h>
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
25 25 #include <ctype.h>
26 26 #include <stdio.h>
27 27 #include <stdlib.h>
28 28 #include <string.h>
29 29 #include <time.h>
30 30
31 31 #include "mandoc.h"
32 32 #include "out.h"
33 33
34 34 static void tblcalc_data(struct rofftbl *, struct roffcol *,
35 - const struct tbl *, const struct tbl_dat *);
35 + const struct tbl_opts *, const struct tbl_dat *);
36 36 static void tblcalc_literal(struct rofftbl *, struct roffcol *,
37 37 const struct tbl_dat *);
38 38 static void tblcalc_number(struct rofftbl *, struct roffcol *,
39 - const struct tbl *, const struct tbl_dat *);
39 + const struct tbl_opts *, const struct tbl_dat *);
40 40
41 41 /*
42 42 * Convert a `scaling unit' to a consistent form, or fail. Scaling
43 43 * units are documented in groff.7, mdoc.7, man.7.
44 44 */
45 45 int
46 46 a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
47 47 {
48 48 char buf[BUFSIZ], hasd;
49 49 int i;
50 50 enum roffscale unit;
51 51
52 52 if ('\0' == *src)
53 53 return(0);
54 54
55 55 i = hasd = 0;
56 56
57 57 switch (*src) {
58 58 case ('+'):
59 59 src++;
60 60 break;
61 61 case ('-'):
62 62 buf[i++] = *src++;
63 63 break;
64 64 default:
65 65 break;
66 66 }
67 67
68 68 if ('\0' == *src)
69 69 return(0);
70 70
71 71 while (i < BUFSIZ) {
72 72 if ( ! isdigit((unsigned char)*src)) {
73 73 if ('.' != *src)
74 74 break;
75 75 else if (hasd)
76 76 break;
77 77 else
78 78 hasd = 1;
79 79 }
80 80 buf[i++] = *src++;
81 81 }
82 82
83 83 if (BUFSIZ == i || (*src && *(src + 1)))
84 84 return(0);
85 85
86 86 buf[i] = '\0';
87 87
88 88 switch (*src) {
89 89 case ('c'):
90 90 unit = SCALE_CM;
91 91 break;
92 92 case ('i'):
93 93 unit = SCALE_IN;
94 94 break;
95 95 case ('P'):
96 96 unit = SCALE_PC;
97 97 break;
98 98 case ('p'):
99 99 unit = SCALE_PT;
100 100 break;
101 101 case ('f'):
102 102 unit = SCALE_FS;
103 103 break;
104 104 case ('v'):
105 105 unit = SCALE_VS;
106 106 break;
107 107 case ('m'):
108 108 unit = SCALE_EM;
109 109 break;
110 110 case ('\0'):
111 111 if (SCALE_MAX == def)
112 112 return(0);
113 113 unit = SCALE_BU;
114 114 break;
115 115 case ('u'):
116 116 unit = SCALE_BU;
117 117 break;
118 118 case ('M'):
119 119 unit = SCALE_MM;
120 120 break;
121 121 case ('n'):
122 122 unit = SCALE_EN;
123 123 break;
124 124 default:
125 125 return(0);
126 126 }
127 127
128 128 /* FIXME: do this in the caller. */
129 129 if ((dst->scale = atof(buf)) < 0)
130 130 dst->scale = 0;
131 131 dst->unit = unit;
132 132 return(1);
133 133 }
134 134
↓ open down ↓ |
85 lines elided |
↑ open up ↑ |
135 135 /*
136 136 * Calculate the abstract widths and decimal positions of columns in a
137 137 * table. This routine allocates the columns structures then runs over
138 138 * all rows and cells in the table. The function pointers in "tbl" are
139 139 * used for the actual width calculations.
140 140 */
141 141 void
142 142 tblcalc(struct rofftbl *tbl, const struct tbl_span *sp)
143 143 {
144 144 const struct tbl_dat *dp;
145 - const struct tbl_head *hp;
146 145 struct roffcol *col;
147 146 int spans;
148 147
149 148 /*
150 149 * Allocate the master column specifiers. These will hold the
151 150 * widths and decimal positions for all cells in the column. It
152 151 * must be freed and nullified by the caller.
153 152 */
154 153
155 154 assert(NULL == tbl->cols);
156 155 tbl->cols = mandoc_calloc
157 - ((size_t)sp->tbl->cols, sizeof(struct roffcol));
156 + ((size_t)sp->opts->cols, sizeof(struct roffcol));
158 157
159 - hp = sp->head;
160 -
161 158 for ( ; sp; sp = sp->next) {
162 159 if (TBL_SPAN_DATA != sp->pos)
163 160 continue;
164 161 spans = 1;
165 162 /*
166 163 * Account for the data cells in the layout, matching it
167 164 * to data cells in the data section.
168 165 */
169 166 for (dp = sp->first; dp; dp = dp->next) {
170 167 /* Do not used spanned cells in the calculation. */
171 168 if (0 < --spans)
172 169 continue;
173 170 spans = dp->spans;
174 171 if (1 < spans)
175 172 continue;
176 173 assert(dp->layout);
177 174 col = &tbl->cols[dp->layout->head->ident];
178 - tblcalc_data(tbl, col, sp->tbl, dp);
175 + tblcalc_data(tbl, col, sp->opts, dp);
179 176 }
180 177 }
181 -
182 - /*
183 - * Calculate width of the spanners. These get one space for a
184 - * vertical line, two for a double-vertical line.
185 - */
186 -
187 - for ( ; hp; hp = hp->next) {
188 - col = &tbl->cols[hp->ident];
189 - switch (hp->pos) {
190 - case (TBL_HEAD_VERT):
191 - col->width = (*tbl->len)(1, tbl->arg);
192 - break;
193 - case (TBL_HEAD_DVERT):
194 - col->width = (*tbl->len)(2, tbl->arg);
195 - break;
196 - default:
197 - break;
198 - }
199 - }
200 178 }
201 179
202 180 static void
203 181 tblcalc_data(struct rofftbl *tbl, struct roffcol *col,
204 - const struct tbl *tp, const struct tbl_dat *dp)
182 + const struct tbl_opts *opts, const struct tbl_dat *dp)
205 183 {
206 184 size_t sz;
207 185
208 186 /* Branch down into data sub-types. */
209 187
210 188 switch (dp->layout->pos) {
211 189 case (TBL_CELL_HORIZ):
212 190 /* FALLTHROUGH */
213 191 case (TBL_CELL_DHORIZ):
214 192 sz = (*tbl->len)(1, tbl->arg);
215 193 if (col->width < sz)
216 194 col->width = sz;
217 195 break;
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
218 196 case (TBL_CELL_LONG):
219 197 /* FALLTHROUGH */
220 198 case (TBL_CELL_CENTRE):
221 199 /* FALLTHROUGH */
222 200 case (TBL_CELL_LEFT):
223 201 /* FALLTHROUGH */
224 202 case (TBL_CELL_RIGHT):
225 203 tblcalc_literal(tbl, col, dp);
226 204 break;
227 205 case (TBL_CELL_NUMBER):
228 - tblcalc_number(tbl, col, tp, dp);
206 + tblcalc_number(tbl, col, opts, dp);
229 207 break;
230 208 case (TBL_CELL_DOWN):
231 209 break;
232 210 default:
233 211 abort();
234 212 /* NOTREACHED */
235 213 }
236 214 }
237 215
238 216 static void
239 217 tblcalc_literal(struct rofftbl *tbl, struct roffcol *col,
240 218 const struct tbl_dat *dp)
241 219 {
242 220 size_t sz;
243 221 const char *str;
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
244 222
245 223 str = dp->string ? dp->string : "";
246 224 sz = (*tbl->slen)(str, tbl->arg);
247 225
248 226 if (col->width < sz)
249 227 col->width = sz;
250 228 }
251 229
252 230 static void
253 231 tblcalc_number(struct rofftbl *tbl, struct roffcol *col,
254 - const struct tbl *tp, const struct tbl_dat *dp)
232 + const struct tbl_opts *opts, const struct tbl_dat *dp)
255 233 {
256 234 int i;
257 235 size_t sz, psz, ssz, d;
258 236 const char *str;
259 237 char *cp;
260 238 char buf[2];
261 239
262 240 /*
263 241 * First calculate number width and decimal place (last + 1 for
264 242 * non-decimal numbers). If the stored decimal is subsequent to
265 243 * ours, make our size longer by that difference
↓ open down ↓ |
1 lines elided |
↑ open up ↑ |
266 244 * (right-"shifting"); similarly, if ours is subsequent the
267 245 * stored, then extend the stored size by the difference.
268 246 * Finally, re-assign the stored values.
269 247 */
270 248
271 249 str = dp->string ? dp->string : "";
272 250 sz = (*tbl->slen)(str, tbl->arg);
273 251
274 252 /* FIXME: TBL_DATA_HORIZ et al.? */
275 253
276 - buf[0] = tp->decimal;
254 + buf[0] = opts->decimal;
277 255 buf[1] = '\0';
278 256
279 257 psz = (*tbl->slen)(buf, tbl->arg);
280 258
281 - if (NULL != (cp = strrchr(str, tp->decimal))) {
259 + if (NULL != (cp = strrchr(str, opts->decimal))) {
282 260 buf[1] = '\0';
283 261 for (ssz = 0, i = 0; cp != &str[i]; i++) {
284 262 buf[0] = str[i];
285 263 ssz += (*tbl->slen)(buf, tbl->arg);
286 264 }
287 265 d = ssz + psz;
288 266 } else
289 267 d = sz + psz;
290 268
291 269 /* Adjust the settings for this column. */
292 270
293 271 if (col->decimal > d) {
294 272 sz += col->decimal - d;
295 273 d = col->decimal;
296 274 } else
297 275 col->width += d - col->decimal;
298 276
299 277 if (sz > col->width)
300 278 col->width = sz;
301 279 if (d > col->decimal)
302 280 col->decimal = d;
303 281 }
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX