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