GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/grid.c Lines: 0 382 0.0 %
Date: 2016-12-06 Branches: 0 254 0.0 %

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