1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Grid data. This is the basic data structure that represents what is shown on
28  * screen.
29  *
30  * A grid is a grid of cells (struct grid_cell). Lines are not allocated until
31  * cells in that line are written to. The grid is split into history and
32  * viewable data with the history starting at row (line) 0 and extending to
33  * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
34  * functions in this file work on absolute coordinates, grid-view.c has
35  * functions which work on the screen data.
36  */
37 
38 /* Default grid cell data. */
39 const struct grid_cell grid_default_cell = {
40 	0, 0, 8, 8, { { ' ' }, 0, 1, 1 }
41 };
42 const struct grid_cell_entry grid_default_entry = {
43 	0, { .data = { 0, 8, 8, ' ' } }
44 };
45 
46 void	grid_reflow_copy(struct grid_line *, u_int, struct grid_line *l,
47 	    u_int, u_int);
48 void	grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int);
49 void	grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int,
50 	    u_int);
51 void	grid_reflow_move(struct grid *, u_int *, struct grid_line *);
52 size_t	grid_string_cells_fg(const struct grid_cell *, int *);
53 size_t	grid_string_cells_bg(const struct grid_cell *, int *);
54 void	grid_string_cells_code(const struct grid_cell *,
55 	    const struct grid_cell *, char *, size_t, int);
56 
57 /* Copy default into a cell. */
58 static void
grid_clear_cell(struct grid * gd,u_int px,u_int py)59 grid_clear_cell(struct grid *gd, u_int px, u_int py)
60 {
61 	gd->linedata[py].celldata[px] = grid_default_entry;
62 }
63 
64 /* Check grid y position. */
65 static int
grid_check_y(struct grid * gd,u_int py)66 grid_check_y(struct grid *gd, u_int py)
67 {
68 	if ((py) >= (gd)->hsize + (gd)->sy) {
69 		log_debug("y out of range: %u", py);
70 		return (-1);
71 	}
72 	return (0);
73 }
74 
75 /* Compare grid cells. Return 1 if equal, 0 if not. */
76 int
grid_cells_equal(const struct grid_cell * gca,const struct grid_cell * gcb)77 grid_cells_equal(const struct grid_cell *gca, const struct grid_cell *gcb)
78 {
79 	if (gca->fg != gcb->fg || gca->bg != gcb->bg)
80 		return (0);
81 	if (gca->attr != gcb->attr || gca->flags != gcb->flags)
82 		return (0);
83 	if (gca->data.width != gcb->data.width)
84 		return (0);
85 	if (gca->data.size != gcb->data.size)
86 		return (0);
87 	return (memcmp(gca->data.data, gcb->data.data, gca->data.size) == 0);
88 }
89 
90 /* Create a new grid. */
91 struct grid *
grid_create(u_int sx,u_int sy,u_int hlimit)92 grid_create(u_int sx, u_int sy, u_int hlimit)
93 {
94 	struct grid	*gd;
95 
96 	gd = xmalloc(sizeof *gd);
97 	gd->sx = sx;
98 	gd->sy = sy;
99 
100 	gd->flags = GRID_HISTORY;
101 
102 	gd->hscrolled = 0;
103 	gd->hsize = 0;
104 	gd->hlimit = hlimit;
105 
106 	gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
107 
108 	return (gd);
109 }
110 
111 /* Destroy grid. */
112 void
grid_destroy(struct grid * gd)113 grid_destroy(struct grid *gd)
114 {
115 	struct grid_line	*gl;
116 	u_int			 yy;
117 
118 	for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
119 		gl = &gd->linedata[yy];
120 		free(gl->celldata);
121 		free(gl->extddata);
122 	}
123 
124 	free(gd->linedata);
125 
126 	free(gd);
127 }
128 
129 /* Compare grids. */
130 int
grid_compare(struct grid * ga,struct grid * gb)131 grid_compare(struct grid *ga, struct grid *gb)
132 {
133 	struct grid_line	*gla, *glb;
134 	struct grid_cell	 gca, gcb;
135 	u_int			 xx, yy;
136 
137 	if (ga->sx != gb->sx || ga->sy != gb->sy)
138 		return (1);
139 
140 	for (yy = 0; yy < ga->sy; yy++) {
141 		gla = &ga->linedata[yy];
142 		glb = &gb->linedata[yy];
143 		if (gla->cellsize != glb->cellsize)
144 			return (1);
145 		for (xx = 0; xx < gla->cellsize; xx++) {
146 			grid_get_cell(ga, xx, yy, &gca);
147 			grid_get_cell(gb, xx, yy, &gcb);
148 			if (!grid_cells_equal(&gca, &gcb))
149 				return (1);
150 		}
151 	}
152 
153 	return (0);
154 }
155 
156 /*
157  * Collect lines from the history if at the limit. Free the top (oldest) 10%
158  * and shift up.
159  */
160 void
grid_collect_history(struct grid * gd)161 grid_collect_history(struct grid *gd)
162 {
163 	u_int	yy;
164 
165 	if (gd->hsize < gd->hlimit)
166 		return;
167 
168 	yy = gd->hlimit / 10;
169 	if (yy < 1)
170 		yy = 1;
171 
172 	grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
173 	gd->hsize -= yy;
174 	if (gd->hscrolled > gd->hsize)
175 		gd->hscrolled = gd->hsize;
176 }
177 
178 /*
179  * Scroll the entire visible screen, moving one line into the history. Just
180  * allocate a new line at the bottom and move the history size indicator.
181  */
182 void
grid_scroll_history(struct grid * gd)183 grid_scroll_history(struct grid *gd)
184 {
185 	u_int	yy;
186 
187 	yy = gd->hsize + gd->sy;
188 	gd->linedata = xreallocarray(gd->linedata, yy + 1,
189 	    sizeof *gd->linedata);
190 	memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
191 
192 	gd->hscrolled++;
193 	gd->hsize++;
194 }
195 
196 /* Clear the history. */
197 void
grid_clear_history(struct grid * gd)198 grid_clear_history(struct grid *gd)
199 {
200 	grid_clear_lines(gd, 0, gd->hsize);
201 	grid_move_lines(gd, 0, gd->hsize, gd->sy);
202 
203 	gd->hscrolled = 0;
204 	gd->hsize = 0;
205 
206 	gd->linedata = xreallocarray(gd->linedata, gd->sy,
207 	    sizeof *gd->linedata);
208 }
209 
210 /* Scroll a region up, moving the top line into the history. */
211 void
grid_scroll_history_region(struct grid * gd,u_int upper,u_int lower)212 grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
213 {
214 	struct grid_line	*gl_history, *gl_upper, *gl_lower;
215 	u_int			 yy;
216 
217 	/* Create a space for a new line. */
218 	yy = gd->hsize + gd->sy;
219 	gd->linedata = xreallocarray(gd->linedata, yy + 1,
220 	    sizeof *gd->linedata);
221 
222 	/* Move the entire screen down to free a space for this line. */
223 	gl_history = &gd->linedata[gd->hsize];
224 	memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
225 
226 	/* Adjust the region and find its start and end. */
227 	upper++;
228 	gl_upper = &gd->linedata[upper];
229 	lower++;
230 	gl_lower = &gd->linedata[lower];
231 
232 	/* Move the line into the history. */
233 	memcpy(gl_history, gl_upper, sizeof *gl_history);
234 
235 	/* Then move the region up and clear the bottom line. */
236 	memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
237 	memset(gl_lower, 0, sizeof *gl_lower);
238 
239 	/* Move the history offset down over the line. */
240 	gd->hscrolled++;
241 	gd->hsize++;
242 }
243 
244 /* Expand line to fit to cell. */
245 void
grid_expand_line(struct grid * gd,u_int py,u_int sx)246 grid_expand_line(struct grid *gd, u_int py, u_int sx)
247 {
248 	struct grid_line	*gl;
249 	u_int			 xx;
250 
251 	gl = &gd->linedata[py];
252 	if (sx <= gl->cellsize)
253 		return;
254 
255 	gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
256 	for (xx = gl->cellsize; xx < sx; xx++)
257 		grid_clear_cell(gd, xx, py);
258 	gl->cellsize = sx;
259 }
260 
261 /* Peek at grid line. */
262 const struct grid_line *
grid_peek_line(struct grid * gd,u_int py)263 grid_peek_line(struct grid *gd, u_int py)
264 {
265 	if (grid_check_y(gd, py) != 0)
266 		return (NULL);
267 	return (&gd->linedata[py]);
268 }
269 
270 /* Get cell for reading. */
271 void
grid_get_cell(struct grid * gd,u_int px,u_int py,struct grid_cell * gc)272 grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
273 {
274 	struct grid_line	*gl;
275 	struct grid_cell_entry	*gce;
276 
277 	if (grid_check_y(gd, py) != 0 || px >= gd->linedata[py].cellsize) {
278 		memcpy(gc, &grid_default_cell, sizeof *gc);
279 		return;
280 	}
281 
282 	gl = &gd->linedata[py];
283 	gce = &gl->celldata[px];
284 
285 	if (gce->flags & GRID_FLAG_EXTENDED) {
286 		if (gce->offset >= gl->extdsize)
287 			memcpy(gc, &grid_default_cell, sizeof *gc);
288 		else
289 			memcpy(gc, &gl->extddata[gce->offset], sizeof *gc);
290 		return;
291 	}
292 
293 	gc->flags = gce->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
294 	gc->attr = gce->data.attr;
295 	gc->fg = gce->data.fg;
296 	if (gce->flags & GRID_FLAG_FG256)
297 		gc->fg |= COLOUR_FLAG_256;
298 	gc->bg = gce->data.bg;
299 	if (gce->flags & GRID_FLAG_BG256)
300 		gc->bg |= COLOUR_FLAG_256;
301 	utf8_set(&gc->data, gce->data.data);
302 }
303 
304 /* Set cell at relative position. */
305 void
grid_set_cell(struct grid * gd,u_int px,u_int py,const struct grid_cell * gc)306 grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
307 {
308 	struct grid_line	*gl;
309 	struct grid_cell_entry	*gce;
310 	struct grid_cell 	*gcp;
311 	int			 extended;
312 
313 	if (grid_check_y(gd, py) != 0)
314 		return;
315 
316 	grid_expand_line(gd, py, px + 1);
317 
318 	gl = &gd->linedata[py];
319 	gce = &gl->celldata[px];
320 
321 	extended = (gce->flags & GRID_FLAG_EXTENDED);
322 	if (!extended && (gc->data.size != 1 || gc->data.width != 1))
323 		extended = 1;
324 	if (!extended && ((gc->fg & COLOUR_FLAG_RGB) ||
325 	    (gc->bg & COLOUR_FLAG_RGB)))
326 		extended = 1;
327 	if (extended) {
328 		gl->flags |= GRID_LINE_EXTENDED;
329 
330 		if (~gce->flags & GRID_FLAG_EXTENDED) {
331 			gl->extddata = xreallocarray(gl->extddata,
332 			    gl->extdsize + 1, sizeof *gl->extddata);
333 			gce->offset = gl->extdsize++;
334 			gce->flags = gc->flags | GRID_FLAG_EXTENDED;
335 		}
336 
337 		if (gce->offset >= gl->extdsize)
338 			fatalx("offset too big");
339 		gcp = &gl->extddata[gce->offset];
340 		memcpy(gcp, gc, sizeof *gcp);
341 		return;
342 	}
343 
344 	gce->flags = gc->flags;
345 	gce->data.attr = gc->attr;
346 	gce->data.fg = gc->fg & 0xff;
347 	if (gc->fg & COLOUR_FLAG_256)
348 		gce->flags |= GRID_FLAG_FG256;
349 	gce->data.bg = gc->bg & 0xff;
350 	if (gc->bg & COLOUR_FLAG_256)
351 		gce->flags |= GRID_FLAG_BG256;
352 	gce->data.data = gc->data.data[0];
353 }
354 
355 /* Clear area. */
356 void
grid_clear(struct grid * gd,u_int px,u_int py,u_int nx,u_int ny)357 grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
358 {
359 	u_int	xx, yy;
360 
361 	if (nx == 0 || ny == 0)
362 		return;
363 
364 	if (px == 0 && nx == gd->sx) {
365 		grid_clear_lines(gd, py, ny);
366 		return;
367 	}
368 
369 	if (grid_check_y(gd, py) != 0)
370 		return;
371 	if (grid_check_y(gd, py + ny - 1) != 0)
372 		return;
373 
374 	for (yy = py; yy < py + ny; yy++) {
375 		if (px >= gd->linedata[yy].cellsize)
376 			continue;
377 		if (px + nx >= gd->linedata[yy].cellsize) {
378 			gd->linedata[yy].cellsize = px;
379 			continue;
380 		}
381 		for (xx = px; xx < px + nx; xx++) {
382 			if (xx >= gd->linedata[yy].cellsize)
383 				break;
384 			grid_clear_cell(gd, xx, yy);
385 		}
386 	}
387 }
388 
389 /* Clear lines. This just frees and truncates the lines. */
390 void
grid_clear_lines(struct grid * gd,u_int py,u_int ny)391 grid_clear_lines(struct grid *gd, u_int py, u_int ny)
392 {
393 	struct grid_line	*gl;
394 	u_int			 yy;
395 
396 	if (ny == 0)
397 		return;
398 
399 	if (grid_check_y(gd, py) != 0)
400 		return;
401 	if (grid_check_y(gd, py + ny - 1) != 0)
402 		return;
403 
404 	for (yy = py; yy < py + ny; yy++) {
405 		gl = &gd->linedata[yy];
406 		free(gl->celldata);
407 		free(gl->extddata);
408 		memset(gl, 0, sizeof *gl);
409 	}
410 }
411 
412 /* Move a group of lines. */
413 void
grid_move_lines(struct grid * gd,u_int dy,u_int py,u_int ny)414 grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
415 {
416 	u_int	yy;
417 
418 	if (ny == 0 || py == dy)
419 		return;
420 
421 	if (grid_check_y(gd, py) != 0)
422 		return;
423 	if (grid_check_y(gd, py + ny - 1) != 0)
424 		return;
425 	if (grid_check_y(gd, dy) != 0)
426 		return;
427 	if (grid_check_y(gd, dy + ny - 1) != 0)
428 		return;
429 
430 	/* Free any lines which are being replaced. */
431 	for (yy = dy; yy < dy + ny; yy++) {
432 		if (yy >= py && yy < py + ny)
433 			continue;
434 		grid_clear_lines(gd, yy, 1);
435 	}
436 
437 	memmove(&gd->linedata[dy], &gd->linedata[py],
438 	    ny * (sizeof *gd->linedata));
439 
440 	/* Wipe any lines that have been moved (without freeing them). */
441 	for (yy = py; yy < py + ny; yy++) {
442 		if (yy >= dy && yy < dy + ny)
443 			continue;
444 		memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
445 	}
446 }
447 
448 /* Move a group of cells. */
449 void
grid_move_cells(struct grid * gd,u_int dx,u_int px,u_int py,u_int nx)450 grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
451 {
452 	struct grid_line	*gl;
453 	u_int			 xx;
454 
455 	if (nx == 0 || px == dx)
456 		return;
457 
458 	if (grid_check_y(gd, py) != 0)
459 		return;
460 	gl = &gd->linedata[py];
461 
462 	grid_expand_line(gd, py, px + nx);
463 	grid_expand_line(gd, py, dx + nx);
464 	memmove(&gl->celldata[dx], &gl->celldata[px],
465 	    nx * sizeof *gl->celldata);
466 
467 	/* Wipe any cells that have been moved. */
468 	for (xx = px; xx < px + nx; xx++) {
469 		if (xx >= dx && xx < dx + nx)
470 			continue;
471 		grid_clear_cell(gd, xx, py);
472 	}
473 }
474 
475 /* Get ANSI foreground sequence. */
476 size_t
grid_string_cells_fg(const struct grid_cell * gc,int * values)477 grid_string_cells_fg(const struct grid_cell *gc, int *values)
478 {
479 	size_t	n;
480 	u_char	r, g, b;
481 
482 	n = 0;
483 	if (gc->fg & COLOUR_FLAG_256) {
484 		values[n++] = 38;
485 		values[n++] = 5;
486 		values[n++] = gc->fg & 0xff;
487 	} else if (gc->fg & COLOUR_FLAG_RGB) {
488 		values[n++] = 38;
489 		values[n++] = 2;
490 		colour_split_rgb(gc->fg, &r, &g, &b);
491 		values[n++] = r;
492 		values[n++] = g;
493 		values[n++] = b;
494 	} else {
495 		switch (gc->fg) {
496 		case 0:
497 		case 1:
498 		case 2:
499 		case 3:
500 		case 4:
501 		case 5:
502 		case 6:
503 		case 7:
504 			values[n++] = gc->fg + 30;
505 			break;
506 		case 8:
507 			values[n++] = 39;
508 			break;
509 		case 90:
510 		case 91:
511 		case 92:
512 		case 93:
513 		case 94:
514 		case 95:
515 		case 96:
516 		case 97:
517 			values[n++] = gc->fg;
518 			break;
519 		}
520 	}
521 	return (n);
522 }
523 
524 /* Get ANSI background sequence. */
525 size_t
grid_string_cells_bg(const struct grid_cell * gc,int * values)526 grid_string_cells_bg(const struct grid_cell *gc, int *values)
527 {
528 	size_t	n;
529 	u_char	r, g, b;
530 
531 	n = 0;
532 	if (gc->bg & COLOUR_FLAG_256) {
533 		values[n++] = 48;
534 		values[n++] = 5;
535 		values[n++] = gc->bg & 0xff;
536 	} else if (gc->bg & COLOUR_FLAG_RGB) {
537 		values[n++] = 48;
538 		values[n++] = 2;
539 		colour_split_rgb(gc->bg, &r, &g, &b);
540 		values[n++] = r;
541 		values[n++] = g;
542 		values[n++] = b;
543 	} else {
544 		switch (gc->bg) {
545 		case 0:
546 		case 1:
547 		case 2:
548 		case 3:
549 		case 4:
550 		case 5:
551 		case 6:
552 		case 7:
553 			values[n++] = gc->bg + 40;
554 			break;
555 		case 8:
556 			values[n++] = 49;
557 			break;
558 		case 100:
559 		case 101:
560 		case 102:
561 		case 103:
562 		case 104:
563 		case 105:
564 		case 106:
565 		case 107:
566 			values[n++] = gc->bg - 10;
567 			break;
568 		}
569 	}
570 	return (n);
571 }
572 
573 /*
574  * Returns ANSI code to set particular attributes (colour, bold and so on)
575  * given a current state. The output buffer must be able to hold at least 57
576  * bytes.
577  */
578 void
grid_string_cells_code(const struct grid_cell * lastgc,const struct grid_cell * gc,char * buf,size_t len,int escape_c0)579 grid_string_cells_code(const struct grid_cell *lastgc,
580     const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
581 {
582 	int	oldc[64], newc[64], s[128];
583 	size_t	noldc, nnewc, n, i;
584 	u_int	attr = gc->attr;
585 	u_int	lastattr = lastgc->attr;
586 	char	tmp[64];
587 
588 	struct {
589 		u_int	mask;
590 		u_int	code;
591 	} attrs[] = {
592 		{ GRID_ATTR_BRIGHT, 1 },
593 		{ GRID_ATTR_DIM, 2 },
594 		{ GRID_ATTR_ITALICS, 3 },
595 		{ GRID_ATTR_UNDERSCORE, 4 },
596 		{ GRID_ATTR_BLINK, 5 },
597 		{ GRID_ATTR_REVERSE, 7 },
598 		{ GRID_ATTR_HIDDEN, 8 }
599 	};
600 	n = 0;
601 
602 	/* If any attribute is removed, begin with 0. */
603 	for (i = 0; i < nitems(attrs); i++) {
604 		if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
605 			s[n++] = 0;
606 			lastattr &= GRID_ATTR_CHARSET;
607 			break;
608 		}
609 	}
610 	/* For each attribute that is newly set, add its code. */
611 	for (i = 0; i < nitems(attrs); i++) {
612 		if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
613 			s[n++] = attrs[i].code;
614 	}
615 
616 	/* If the foreground colour changed, append its parameters. */
617 	nnewc = grid_string_cells_fg(gc, newc);
618 	noldc = grid_string_cells_fg(lastgc, oldc);
619 	if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
620 		for (i = 0; i < nnewc; i++)
621 			s[n++] = newc[i];
622 	}
623 
624 	/* If the background colour changed, append its parameters. */
625 	nnewc = grid_string_cells_bg(gc, newc);
626 	noldc = grid_string_cells_bg(lastgc, oldc);
627 	if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
628 		for (i = 0; i < nnewc; i++)
629 			s[n++] = newc[i];
630 	}
631 
632 	/* If there are any parameters, append an SGR code. */
633 	*buf = '\0';
634 	if (n > 0) {
635 		if (escape_c0)
636 			strlcat(buf, "\\033[", len);
637 		else
638 			strlcat(buf, "\033[", len);
639 		for (i = 0; i < n; i++) {
640 			if (i + 1 < n)
641 				xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
642 			else
643 				xsnprintf(tmp, sizeof tmp, "%d", s[i]);
644 			strlcat(buf, tmp, len);
645 		}
646 		strlcat(buf, "m", len);
647 	}
648 
649 	/* Append shift in/shift out if needed. */
650 	if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
651 		if (escape_c0)
652 			strlcat(buf, "\\016", len);  /* SO */
653 		else
654 			strlcat(buf, "\016", len);  /* SO */
655 	}
656 	if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
657 		if (escape_c0)
658 			strlcat(buf, "\\017", len);  /* SI */
659 		else
660 			strlcat(buf, "\017", len);  /* SI */
661 	}
662 }
663 
664 /* Convert cells into a string. */
665 char *
grid_string_cells(struct grid * gd,u_int px,u_int py,u_int nx,struct grid_cell ** lastgc,int with_codes,int escape_c0,int trim)666 grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
667     struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
668 {
669 	struct grid_cell	 gc;
670 	static struct grid_cell	 lastgc1;
671 	const char		*data;
672 	char			*buf, code[128];
673 	size_t			 len, off, size, codelen;
674 	u_int			 xx;
675 	const struct grid_line	*gl;
676 
677 	if (lastgc != NULL && *lastgc == NULL) {
678 		memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
679 		*lastgc = &lastgc1;
680 	}
681 
682 	len = 128;
683 	buf = xmalloc(len);
684 	off = 0;
685 
686 	gl = grid_peek_line(gd, py);
687 	for (xx = px; xx < px + nx; xx++) {
688 		if (gl == NULL || xx >= gl->cellsize)
689 			break;
690 		grid_get_cell(gd, xx, py, &gc);
691 		if (gc.flags & GRID_FLAG_PADDING)
692 			continue;
693 
694 		if (with_codes) {
695 			grid_string_cells_code(*lastgc, &gc, code, sizeof code,
696 			    escape_c0);
697 			codelen = strlen(code);
698 			memcpy(*lastgc, &gc, sizeof **lastgc);
699 		} else
700 			codelen = 0;
701 
702 		data = gc.data.data;
703 		size = gc.data.size;
704 		if (escape_c0 && size == 1 && *data == '\\') {
705 			data = "\\\\";
706 			size = 2;
707 		}
708 
709 		while (len < off + size + codelen + 1) {
710 			buf = xreallocarray(buf, 2, len);
711 			len *= 2;
712 		}
713 
714 		if (codelen != 0) {
715 			memcpy(buf + off, code, codelen);
716 			off += codelen;
717 		}
718 		memcpy(buf + off, data, size);
719 		off += size;
720 	}
721 
722 	if (trim) {
723 		while (off > 0 && buf[off - 1] == ' ')
724 			off--;
725 	}
726 	buf[off] = '\0';
727 
728 	return (buf);
729 }
730 
731 /*
732  * Duplicate a set of lines between two grids. If there aren't enough lines in
733  * either source or destination, the number of lines is limited to the number
734  * available.
735  */
736 void
grid_duplicate_lines(struct grid * dst,u_int dy,struct grid * src,u_int sy,u_int ny)737 grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
738     u_int ny)
739 {
740 	struct grid_line	*dstl, *srcl;
741 	u_int			 yy;
742 
743 	if (dy + ny > dst->hsize + dst->sy)
744 		ny = dst->hsize + dst->sy - dy;
745 	if (sy + ny > src->hsize + src->sy)
746 		ny = src->hsize + src->sy - sy;
747 	grid_clear_lines(dst, dy, ny);
748 
749 	for (yy = 0; yy < ny; yy++) {
750 		srcl = &src->linedata[sy];
751 		dstl = &dst->linedata[dy];
752 
753 		memcpy(dstl, srcl, sizeof *dstl);
754 		if (srcl->cellsize != 0) {
755 			dstl->celldata = xreallocarray(NULL,
756 			    srcl->cellsize, sizeof *dstl->celldata);
757 			memcpy(dstl->celldata, srcl->celldata,
758 			    srcl->cellsize * sizeof *dstl->celldata);
759 		} else
760 			dstl->celldata = NULL;
761 
762 		if (srcl->extdsize != 0) {
763 			dstl->extdsize = srcl->extdsize;
764 			dstl->extddata = xreallocarray(NULL, dstl->extdsize,
765 			    sizeof *dstl->extddata);
766 			memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
767 			    sizeof *dstl->extddata);
768 		}
769 
770 		sy++;
771 		dy++;
772 	}
773 }
774 
775 /* Copy a section of a line. */
776 void
grid_reflow_copy(struct grid_line * dst_gl,u_int to,struct grid_line * src_gl,u_int from,u_int to_copy)777 grid_reflow_copy(struct grid_line *dst_gl, u_int to, struct grid_line *src_gl,
778     u_int from, u_int to_copy)
779 {
780 	struct grid_cell_entry	*gce;
781 	u_int			 i, was;
782 
783 	memcpy(&dst_gl->celldata[to], &src_gl->celldata[from],
784 	    to_copy * sizeof *dst_gl->celldata);
785 
786 	for (i = to; i < to + to_copy; i++) {
787 		gce = &dst_gl->celldata[i];
788 		if (~gce->flags & GRID_FLAG_EXTENDED)
789 			continue;
790 		was = gce->offset;
791 
792 		dst_gl->extddata = xreallocarray(dst_gl->extddata,
793 		    dst_gl->extdsize + 1, sizeof *dst_gl->extddata);
794 		gce->offset = dst_gl->extdsize++;
795 		memcpy(&dst_gl->extddata[gce->offset], &src_gl->extddata[was],
796 		    sizeof *dst_gl->extddata);
797 	}
798 }
799 
800 /* Join line data. */
801 void
grid_reflow_join(struct grid * dst,u_int * py,struct grid_line * src_gl,u_int new_x)802 grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
803     u_int new_x)
804 {
805 	struct grid_line	*dst_gl = &dst->linedata[(*py) - 1];
806 	u_int			 left, to_copy, ox, nx;
807 
808 	/* How much is left on the old line? */
809 	left = new_x - dst_gl->cellsize;
810 
811 	/* Work out how much to append. */
812 	to_copy = src_gl->cellsize;
813 	if (to_copy > left)
814 		to_copy = left;
815 	ox = dst_gl->cellsize;
816 	nx = ox + to_copy;
817 
818 	/* Resize the destination line. */
819 	dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
820 	    sizeof *dst_gl->celldata);
821 	dst_gl->cellsize = nx;
822 
823 	/* Append as much as possible. */
824 	grid_reflow_copy(dst_gl, ox, src_gl, 0, to_copy);
825 
826 	/* If there is any left in the source, split it. */
827 	if (src_gl->cellsize > to_copy) {
828 		dst_gl->flags |= GRID_LINE_WRAPPED;
829 
830 		src_gl->cellsize -= to_copy;
831 		grid_reflow_split(dst, py, src_gl, new_x, to_copy);
832 	}
833 }
834 
835 /* Split line data. */
836 void
grid_reflow_split(struct grid * dst,u_int * py,struct grid_line * src_gl,u_int new_x,u_int offset)837 grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
838     u_int new_x, u_int offset)
839 {
840 	struct grid_line	*dst_gl = NULL;
841 	u_int			 to_copy;
842 
843 	/* Loop and copy sections of the source line. */
844 	while (src_gl->cellsize > 0) {
845 		/* Create new line. */
846 		if (*py >= dst->hsize + dst->sy)
847 			grid_scroll_history(dst);
848 		dst_gl = &dst->linedata[*py];
849 		(*py)++;
850 
851 		/* How much should we copy? */
852 		to_copy = new_x;
853 		if (to_copy > src_gl->cellsize)
854 			to_copy = src_gl->cellsize;
855 
856 		/* Expand destination line. */
857 		dst_gl->celldata = xreallocarray(NULL, to_copy,
858 		    sizeof *dst_gl->celldata);
859 		dst_gl->cellsize = to_copy;
860 		dst_gl->flags |= GRID_LINE_WRAPPED;
861 
862 		/* Copy the data. */
863 		grid_reflow_copy(dst_gl, 0, src_gl, offset, to_copy);
864 
865 		/* Move offset and reduce old line size. */
866 		offset += to_copy;
867 		src_gl->cellsize -= to_copy;
868 	}
869 
870 	/* Last line is not wrapped. */
871 	if (dst_gl != NULL)
872 		dst_gl->flags &= ~GRID_LINE_WRAPPED;
873 }
874 
875 /* Move line data. */
876 void
grid_reflow_move(struct grid * dst,u_int * py,struct grid_line * src_gl)877 grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
878 {
879 	struct grid_line	*dst_gl;
880 
881 	/* Create new line. */
882 	if (*py >= dst->hsize + dst->sy)
883 		grid_scroll_history(dst);
884 	dst_gl = &dst->linedata[*py];
885 	(*py)++;
886 
887 	/* Copy the old line. */
888 	memcpy(dst_gl, src_gl, sizeof *dst_gl);
889 	dst_gl->flags &= ~GRID_LINE_WRAPPED;
890 
891 	/* Clear old line. */
892 	src_gl->celldata = NULL;
893 	src_gl->extddata = NULL;
894 }
895 
896 /*
897  * Reflow lines from src grid into dst grid of width new_x. Returns number of
898  * lines fewer in the visible area. The source grid is destroyed.
899  */
900 u_int
grid_reflow(struct grid * dst,struct grid * src,u_int new_x)901 grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
902 {
903 	u_int			 py, sy, line;
904 	int			 previous_wrapped;
905 	struct grid_line	*src_gl;
906 
907 	py = 0;
908 	sy = src->sy;
909 
910 	previous_wrapped = 0;
911 	for (line = 0; line < sy + src->hsize; line++) {
912 		src_gl = src->linedata + line;
913 		if (!previous_wrapped) {
914 			/* Wasn't wrapped. If smaller, move to destination. */
915 			if (src_gl->cellsize <= new_x)
916 				grid_reflow_move(dst, &py, src_gl);
917 			else
918 				grid_reflow_split(dst, &py, src_gl, new_x, 0);
919 		} else {
920 			/* Previous was wrapped. Try to join. */
921 			grid_reflow_join(dst, &py, src_gl, new_x);
922 		}
923 		previous_wrapped = (src_gl->flags & GRID_LINE_WRAPPED);
924 
925 		/* This is where we started scrolling. */
926 		if (line == sy + src->hsize - src->hscrolled - 1)
927 			dst->hscrolled = 0;
928 	}
929 
930 	grid_destroy(src);
931 
932 	if (py > sy)
933 		return (0);
934 	return (sy - py);
935 }
936