1 /* $OpenBSD: tbl_layout.c,v 1.37 2021/08/10 12:36:42 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2012, 2014, 2015, 2017, 2020, 2021 5 * Ingo Schwarze <schwarze@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 #include <sys/types.h> 20 21 #include <ctype.h> 22 #include <stdint.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <time.h> 27 28 #include "mandoc_aux.h" 29 #include "mandoc.h" 30 #include "tbl.h" 31 #include "libmandoc.h" 32 #include "tbl_int.h" 33 34 struct tbl_phrase { 35 char name; 36 enum tbl_cellt key; 37 }; 38 39 static const struct tbl_phrase keys[] = { 40 { 'c', TBL_CELL_CENTRE }, 41 { 'r', TBL_CELL_RIGHT }, 42 { 'l', TBL_CELL_LEFT }, 43 { 'n', TBL_CELL_NUMBER }, 44 { 's', TBL_CELL_SPAN }, 45 { 'a', TBL_CELL_LONG }, 46 { '^', TBL_CELL_DOWN }, 47 { '-', TBL_CELL_HORIZ }, 48 { '_', TBL_CELL_HORIZ }, 49 { '=', TBL_CELL_DHORIZ } 50 }; 51 52 #define KEYS_MAX ((int)(sizeof(keys)/sizeof(keys[0]))) 53 54 static void mods(struct tbl_node *, struct tbl_cell *, 55 int, const char *, int *); 56 static void cell(struct tbl_node *, struct tbl_row *, 57 int, const char *, int *); 58 static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *, 59 enum tbl_cellt); 60 61 62 static void 63 mods(struct tbl_node *tbl, struct tbl_cell *cp, 64 int ln, const char *p, int *pos) 65 { 66 char *endptr; 67 unsigned long spacing; 68 size_t sz; 69 int isz; 70 enum mandoc_esc fontesc; 71 72 mod: 73 while (p[*pos] == ' ' || p[*pos] == '\t') 74 (*pos)++; 75 76 /* Row delimiters and cell specifiers end modifier lists. */ 77 78 if (strchr(".,-=^_ACLNRSaclnrs", p[*pos]) != NULL) 79 return; 80 81 /* Throw away parenthesised expression. */ 82 83 if ('(' == p[*pos]) { 84 (*pos)++; 85 while (p[*pos] && ')' != p[*pos]) 86 (*pos)++; 87 if (')' == p[*pos]) { 88 (*pos)++; 89 goto mod; 90 } 91 mandoc_msg(MANDOCERR_TBLLAYOUT_PAR, ln, *pos, NULL); 92 return; 93 } 94 95 /* Parse numerical spacing from modifier string. */ 96 97 if (isdigit((unsigned char)p[*pos])) { 98 if ((spacing = strtoul(p + *pos, &endptr, 10)) > 9) 99 mandoc_msg(MANDOCERR_TBLLAYOUT_SPC, ln, *pos, 100 "%lu", spacing); 101 else 102 cp->spacing = spacing; 103 *pos = endptr - p; 104 goto mod; 105 } 106 107 switch (tolower((unsigned char)p[(*pos)++])) { 108 case 'b': 109 cp->font = ESCAPE_FONTBOLD; 110 goto mod; 111 case 'd': 112 cp->flags |= TBL_CELL_BALIGN; 113 goto mod; 114 case 'e': 115 cp->flags |= TBL_CELL_EQUAL; 116 goto mod; 117 case 'f': 118 break; 119 case 'i': 120 cp->font = ESCAPE_FONTITALIC; 121 goto mod; 122 case 'm': 123 mandoc_msg(MANDOCERR_TBLLAYOUT_MOD, ln, *pos, "m"); 124 goto mod; 125 case 'p': 126 case 'v': 127 if (p[*pos] == '-' || p[*pos] == '+') 128 (*pos)++; 129 while (isdigit((unsigned char)p[*pos])) 130 (*pos)++; 131 goto mod; 132 case 't': 133 cp->flags |= TBL_CELL_TALIGN; 134 goto mod; 135 case 'u': 136 cp->flags |= TBL_CELL_UP; 137 goto mod; 138 case 'w': 139 sz = 0; 140 if (p[*pos] == '(') { 141 (*pos)++; 142 while (p[*pos + sz] != '\0' && p[*pos + sz] != ')') 143 sz++; 144 } else 145 while (isdigit((unsigned char)p[*pos + sz])) 146 sz++; 147 if (sz) { 148 free(cp->wstr); 149 cp->wstr = mandoc_strndup(p + *pos, sz); 150 *pos += sz; 151 if (p[*pos] == ')') 152 (*pos)++; 153 } 154 goto mod; 155 case 'x': 156 cp->flags |= TBL_CELL_WMAX; 157 goto mod; 158 case 'z': 159 cp->flags |= TBL_CELL_WIGN; 160 goto mod; 161 case '|': 162 if (cp->vert < 2) 163 cp->vert++; 164 else 165 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT, 166 ln, *pos - 1, NULL); 167 goto mod; 168 default: 169 mandoc_msg(MANDOCERR_TBLLAYOUT_CHAR, 170 ln, *pos - 1, "%c", p[*pos - 1]); 171 goto mod; 172 } 173 174 while (p[*pos] == ' ' || p[*pos] == '\t') 175 (*pos)++; 176 177 /* Ignore parenthised font names for now. */ 178 179 if (p[*pos] == '(') 180 goto mod; 181 182 isz = 0; 183 if (p[*pos] != '\0') 184 isz++; 185 if (strchr(" \t.", p[*pos + isz]) == NULL) 186 isz++; 187 188 fontesc = mandoc_font(p + *pos, isz); 189 190 switch (fontesc) { 191 case ESCAPE_FONTPREV: 192 case ESCAPE_ERROR: 193 mandoc_msg(MANDOCERR_FT_BAD, 194 ln, *pos, "TS %s", p + *pos - 1); 195 break; 196 default: 197 cp->font = fontesc; 198 break; 199 } 200 *pos += isz; 201 goto mod; 202 } 203 204 static void 205 cell(struct tbl_node *tbl, struct tbl_row *rp, 206 int ln, const char *p, int *pos) 207 { 208 int i; 209 enum tbl_cellt c; 210 211 /* Handle leading vertical lines */ 212 213 while (p[*pos] == ' ' || p[*pos] == '\t' || p[*pos] == '|') { 214 if (p[*pos] == '|') { 215 if (rp->vert < 2) 216 rp->vert++; 217 else 218 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT, 219 ln, *pos, NULL); 220 } 221 (*pos)++; 222 } 223 224 again: 225 while (p[*pos] == ' ' || p[*pos] == '\t') 226 (*pos)++; 227 228 if (p[*pos] == '.' || p[*pos] == '\0') 229 return; 230 231 /* Parse the column position (`c', `l', `r', ...). */ 232 233 for (i = 0; i < KEYS_MAX; i++) 234 if (tolower((unsigned char)p[*pos]) == keys[i].name) 235 break; 236 237 if (i == KEYS_MAX) { 238 mandoc_msg(MANDOCERR_TBLLAYOUT_CHAR, 239 ln, *pos, "%c", p[*pos]); 240 (*pos)++; 241 goto again; 242 } 243 c = keys[i].key; 244 245 /* Special cases of spanners. */ 246 247 if (c == TBL_CELL_SPAN) { 248 if (rp->last == NULL) 249 mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN, ln, *pos, NULL); 250 else if (rp->last->pos == TBL_CELL_HORIZ || 251 rp->last->pos == TBL_CELL_DHORIZ) 252 c = rp->last->pos; 253 } else if (c == TBL_CELL_DOWN && rp == tbl->first_row) 254 mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN, ln, *pos, NULL); 255 256 (*pos)++; 257 258 /* Allocate cell then parse its modifiers. */ 259 260 mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos); 261 } 262 263 void 264 tbl_layout(struct tbl_node *tbl, int ln, const char *p, int pos) 265 { 266 struct tbl_row *rp; 267 268 rp = NULL; 269 for (;;) { 270 /* Skip whitespace before and after each cell. */ 271 272 while (p[pos] == ' ' || p[pos] == '\t') 273 pos++; 274 275 switch (p[pos]) { 276 case ',': /* Next row on this input line. */ 277 pos++; 278 rp = NULL; 279 continue; 280 case '\0': /* Next row on next input line. */ 281 return; 282 case '.': /* End of layout. */ 283 pos++; 284 tbl->part = TBL_PART_DATA; 285 286 /* 287 * When the layout is completely empty, 288 * default to one left-justified column. 289 */ 290 291 if (tbl->first_row == NULL) { 292 tbl->first_row = tbl->last_row = 293 mandoc_calloc(1, sizeof(*rp)); 294 } 295 if (tbl->first_row->first == NULL) { 296 mandoc_msg(MANDOCERR_TBLLAYOUT_NONE, 297 ln, pos, NULL); 298 cell_alloc(tbl, tbl->first_row, 299 TBL_CELL_LEFT); 300 if (tbl->opts.lvert < tbl->first_row->vert) 301 tbl->opts.lvert = tbl->first_row->vert; 302 return; 303 } 304 305 /* 306 * Search for the widest line 307 * along the left and right margins. 308 */ 309 310 for (rp = tbl->first_row; rp; rp = rp->next) { 311 if (tbl->opts.lvert < rp->vert) 312 tbl->opts.lvert = rp->vert; 313 if (rp->last != NULL && 314 rp->last->col + 1 == tbl->opts.cols && 315 tbl->opts.rvert < rp->last->vert) 316 tbl->opts.rvert = rp->last->vert; 317 318 /* If the last line is empty, drop it. */ 319 320 if (rp->next != NULL && 321 rp->next->first == NULL) { 322 free(rp->next); 323 rp->next = NULL; 324 tbl->last_row = rp; 325 } 326 } 327 return; 328 default: /* Cell. */ 329 break; 330 } 331 332 /* 333 * If the last line had at least one cell, 334 * start a new one; otherwise, continue it. 335 */ 336 337 if (rp == NULL) { 338 if (tbl->last_row == NULL || 339 tbl->last_row->first != NULL) { 340 rp = mandoc_calloc(1, sizeof(*rp)); 341 if (tbl->last_row) 342 tbl->last_row->next = rp; 343 else 344 tbl->first_row = rp; 345 tbl->last_row = rp; 346 } else 347 rp = tbl->last_row; 348 } 349 cell(tbl, rp, ln, p, &pos); 350 } 351 } 352 353 static struct tbl_cell * 354 cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos) 355 { 356 struct tbl_cell *p, *pp; 357 358 p = mandoc_calloc(1, sizeof(*p)); 359 p->spacing = SIZE_MAX; 360 p->font = ESCAPE_FONTROMAN; 361 p->pos = pos; 362 363 if ((pp = rp->last) != NULL) { 364 pp->next = p; 365 p->col = pp->col + 1; 366 } else 367 rp->first = p; 368 rp->last = p; 369 370 if (tbl->opts.cols <= p->col) 371 tbl->opts.cols = p->col + 1; 372 373 return p; 374 } 375