10f66f451Sopenharmony_ci#include "toys.h"
20f66f451Sopenharmony_ci
30f66f451Sopenharmony_ci// The design idea here is indexing a big blob of (potentially mmaped) data
40f66f451Sopenharmony_ci// instead of copying the data into a zillion seperate malloc()s.
50f66f451Sopenharmony_ci
60f66f451Sopenharmony_ci// A linestack is an array of struct ptr_len, with a currently used len
70f66f451Sopenharmony_ci// and max tracking the memory allocation. This indexes existing string data,
80f66f451Sopenharmony_ci// the lifetime of which is tracked externally.
90f66f451Sopenharmony_ci
100f66f451Sopenharmony_ci// Insert one stack into another before position in old stack.
110f66f451Sopenharmony_ci// (Does not copy contents of strings, just shuffles index array contents.)
120f66f451Sopenharmony_civoid linestack_addstack(struct linestack **lls, struct linestack *throw,
130f66f451Sopenharmony_ci  long pos)
140f66f451Sopenharmony_ci{
150f66f451Sopenharmony_ci  struct linestack *catch = *lls;
160f66f451Sopenharmony_ci
170f66f451Sopenharmony_ci  if (CFG_TOYBOX_DEBUG)
180f66f451Sopenharmony_ci    if (pos > catch->len) error_exit("linestack_addstack past end.");
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci  // Make a hole, allocating more space if necessary.
210f66f451Sopenharmony_ci  if (catch->len+throw->len >= catch->max) {
220f66f451Sopenharmony_ci    // New size rounded up to next multiple of 64, allocate and copy start.
230f66f451Sopenharmony_ci    catch->max = ((catch->len+throw->len)|63)+1;
240f66f451Sopenharmony_ci    *lls = xmalloc(sizeof(struct linestack)+catch->max*sizeof(struct ptr_len));
250f66f451Sopenharmony_ci    memcpy(*lls, catch, sizeof(struct linestack)+pos*sizeof(struct ptr_len));
260f66f451Sopenharmony_ci  }
270f66f451Sopenharmony_ci
280f66f451Sopenharmony_ci  // Copy end (into new allocation if necessary)
290f66f451Sopenharmony_ci  if (pos != catch->len)
300f66f451Sopenharmony_ci    memmove((*lls)->idx+pos+throw->len, catch->idx+pos,
310f66f451Sopenharmony_ci      (catch->len-pos)*sizeof(struct ptr_len));
320f66f451Sopenharmony_ci
330f66f451Sopenharmony_ci  // Cleanup if we had to realloc.
340f66f451Sopenharmony_ci  if (catch != *lls) {
350f66f451Sopenharmony_ci    free(catch);
360f66f451Sopenharmony_ci    catch = *lls;
370f66f451Sopenharmony_ci  }
380f66f451Sopenharmony_ci
390f66f451Sopenharmony_ci  // Copy new chunk we made space for
400f66f451Sopenharmony_ci  memcpy(catch->idx+pos, throw->idx, throw->len*sizeof(struct ptr_len));
410f66f451Sopenharmony_ci  catch->len += throw->len;
420f66f451Sopenharmony_ci}
430f66f451Sopenharmony_ci
440f66f451Sopenharmony_ci// Insert one line/len into a linestack at pos
450f66f451Sopenharmony_civoid linestack_insert(struct linestack **lls, long pos, char *line, long len)
460f66f451Sopenharmony_ci{
470f66f451Sopenharmony_ci  // alloca() was in 32V and Turbo C for DOS, but isn't in posix or c99.
480f66f451Sopenharmony_ci  // This allocates enough memory for the linestack to have one ptr_len.
490f66f451Sopenharmony_ci  // (Even if a compiler adds gratuitous padidng that just makes it bigger.)
500f66f451Sopenharmony_ci  struct {
510f66f451Sopenharmony_ci    struct ptr_len pl;
520f66f451Sopenharmony_ci    struct linestack ls;
530f66f451Sopenharmony_ci  } ls;
540f66f451Sopenharmony_ci
550f66f451Sopenharmony_ci  ls.ls.len = ls.ls.max = 1;
560f66f451Sopenharmony_ci  ls.ls.idx[0].ptr = line;
570f66f451Sopenharmony_ci  ls.ls.idx[0].len = len;
580f66f451Sopenharmony_ci  linestack_addstack(lls, &ls.ls, pos);
590f66f451Sopenharmony_ci}
600f66f451Sopenharmony_ci
610f66f451Sopenharmony_civoid linestack_append(struct linestack **lls, char *line)
620f66f451Sopenharmony_ci{
630f66f451Sopenharmony_ci  linestack_insert(lls, (*lls)->len, line, strlen(line));
640f66f451Sopenharmony_ci}
650f66f451Sopenharmony_ci
660f66f451Sopenharmony_cistruct linestack *linestack_load(char *name)
670f66f451Sopenharmony_ci{
680f66f451Sopenharmony_ci  FILE *fp = fopen(name, "r");
690f66f451Sopenharmony_ci  struct linestack *ls;
700f66f451Sopenharmony_ci
710f66f451Sopenharmony_ci  if (!fp) return 0;
720f66f451Sopenharmony_ci
730f66f451Sopenharmony_ci  ls = xzalloc(sizeof(struct linestack));
740f66f451Sopenharmony_ci
750f66f451Sopenharmony_ci  for (;;) {
760f66f451Sopenharmony_ci    char *line = 0;
770f66f451Sopenharmony_ci    ssize_t len;
780f66f451Sopenharmony_ci
790f66f451Sopenharmony_ci    if ((len = getline(&line, (void *)&len, fp))<1) break;
800f66f451Sopenharmony_ci    if (line[len-1]=='\n') len--;
810f66f451Sopenharmony_ci    linestack_insert(&ls, ls->len, line, len);
820f66f451Sopenharmony_ci  }
830f66f451Sopenharmony_ci  fclose(fp);
840f66f451Sopenharmony_ci
850f66f451Sopenharmony_ci  return ls;
860f66f451Sopenharmony_ci}
870f66f451Sopenharmony_ci
880f66f451Sopenharmony_ci// Show width many columns, negative means from right edge, out=0 just measure
890f66f451Sopenharmony_ci// if escout, send it unprintable chars, otherwise pass through raw data.
900f66f451Sopenharmony_ci// Returns width in columns, moves *str to end of data consumed.
910f66f451Sopenharmony_ciint crunch_str(char **str, int width, FILE *out, char *escmore,
920f66f451Sopenharmony_ci  int (*escout)(FILE *out, int cols, int wc))
930f66f451Sopenharmony_ci{
940f66f451Sopenharmony_ci  int columns = 0, col, bytes;
950f66f451Sopenharmony_ci  char *start, *end;
960f66f451Sopenharmony_ci  unsigned wc;
970f66f451Sopenharmony_ci
980f66f451Sopenharmony_ci  for (end = start = *str; *end; columns += col, end += bytes) {
990f66f451Sopenharmony_ci    if ((bytes = utf8towc(&wc, end, 4))>0 && (col = wcwidth(wc))>=0) {
1000f66f451Sopenharmony_ci      if (!escmore || wc>255 || !strchr(escmore, wc)) {
1010f66f451Sopenharmony_ci        if (width-columns<col) break;
1020f66f451Sopenharmony_ci        if (out) fwrite(end, bytes, 1, out);
1030f66f451Sopenharmony_ci
1040f66f451Sopenharmony_ci        continue;
1050f66f451Sopenharmony_ci      }
1060f66f451Sopenharmony_ci    }
1070f66f451Sopenharmony_ci
1080f66f451Sopenharmony_ci    if (bytes<1) {
1090f66f451Sopenharmony_ci      bytes = 1;
1100f66f451Sopenharmony_ci      wc = *end;
1110f66f451Sopenharmony_ci    }
1120f66f451Sopenharmony_ci    col = width-columns;
1130f66f451Sopenharmony_ci    if (col<1) break;
1140f66f451Sopenharmony_ci    if (escout) {
1150f66f451Sopenharmony_ci      if ((col = escout(out, col, wc))<0) break;
1160f66f451Sopenharmony_ci    } else if (out) fwrite(end, 1, bytes, out);
1170f66f451Sopenharmony_ci  }
1180f66f451Sopenharmony_ci  *str = end;
1190f66f451Sopenharmony_ci
1200f66f451Sopenharmony_ci  return columns;
1210f66f451Sopenharmony_ci}
1220f66f451Sopenharmony_ci
1230f66f451Sopenharmony_ci
1240f66f451Sopenharmony_ci// standard escapes: ^X if <32, <XX> if invalid UTF8, U+XXXX if UTF8 !iswprint()
1250f66f451Sopenharmony_ciint crunch_escape(FILE *out, int cols, int wc)
1260f66f451Sopenharmony_ci{
1270f66f451Sopenharmony_ci  char buf[11];
1280f66f451Sopenharmony_ci  int rc;
1290f66f451Sopenharmony_ci
1300f66f451Sopenharmony_ci  if (wc<' ') rc = sprintf(buf, "^%c", '@'+wc);
1310f66f451Sopenharmony_ci  else if (wc<256) rc = sprintf(buf, "<%02X>", wc);
1320f66f451Sopenharmony_ci  else rc = sprintf(buf, "U+%04X", wc);
1330f66f451Sopenharmony_ci
1340f66f451Sopenharmony_ci  if (rc > cols) buf[rc = cols] = 0;
1350f66f451Sopenharmony_ci  if (out) fputs(buf, out);
1360f66f451Sopenharmony_ci
1370f66f451Sopenharmony_ci  return rc;
1380f66f451Sopenharmony_ci}
1390f66f451Sopenharmony_ci
1400f66f451Sopenharmony_ci// Display "standard" escapes in reverse video.
1410f66f451Sopenharmony_ciint crunch_rev_escape(FILE *out, int cols, int wc)
1420f66f451Sopenharmony_ci{
1430f66f451Sopenharmony_ci  int rc;
1440f66f451Sopenharmony_ci
1450f66f451Sopenharmony_ci  tty_esc("7m");
1460f66f451Sopenharmony_ci  rc = crunch_escape(out, cols, wc);
1470f66f451Sopenharmony_ci  tty_esc("27m");
1480f66f451Sopenharmony_ci
1490f66f451Sopenharmony_ci  return rc;
1500f66f451Sopenharmony_ci}
1510f66f451Sopenharmony_ci
1520f66f451Sopenharmony_ci// Write width chars at start of string to strdout with standard escapes
1530f66f451Sopenharmony_ci// Returns length in columns so caller can pad it out with spaces.
1540f66f451Sopenharmony_ciint draw_str(char *start, int width)
1550f66f451Sopenharmony_ci{
1560f66f451Sopenharmony_ci  return crunch_str(&start, width, stdout, 0, crunch_rev_escape);
1570f66f451Sopenharmony_ci}
1580f66f451Sopenharmony_ci
1590f66f451Sopenharmony_ci// Return utf8 columns
1600f66f451Sopenharmony_ciint utf8len(char *str)
1610f66f451Sopenharmony_ci{
1620f66f451Sopenharmony_ci  return crunch_str(&str, INT_MAX, 0, 0, crunch_rev_escape);
1630f66f451Sopenharmony_ci}
1640f66f451Sopenharmony_ci
1650f66f451Sopenharmony_ci// Return bytes used by (up to) this many columns
1660f66f451Sopenharmony_ciint utf8skip(char *str, int width)
1670f66f451Sopenharmony_ci{
1680f66f451Sopenharmony_ci  char *s = str;
1690f66f451Sopenharmony_ci
1700f66f451Sopenharmony_ci  crunch_str(&s, width, 0, 0, crunch_rev_escape);
1710f66f451Sopenharmony_ci
1720f66f451Sopenharmony_ci  return s-str;
1730f66f451Sopenharmony_ci}
1740f66f451Sopenharmony_ci
1750f66f451Sopenharmony_ci// Print utf8 to stdout with standard escapes, trimmed to width and padded
1760f66f451Sopenharmony_ci// out to padto. If padto<0 left justify. Returns columns printed
1770f66f451Sopenharmony_ciint draw_trim_esc(char *str, int padto, int width, char *escmore,
1780f66f451Sopenharmony_ci  int (*escout)(FILE *out, int cols, int wc))
1790f66f451Sopenharmony_ci{
1800f66f451Sopenharmony_ci  int apad = abs(padto), len = utf8len(str);
1810f66f451Sopenharmony_ci
1820f66f451Sopenharmony_ci  if (padto>=0 && len>width) str += utf8skip(str, len-width);
1830f66f451Sopenharmony_ci  if (len>width) len = width;
1840f66f451Sopenharmony_ci
1850f66f451Sopenharmony_ci  // Left pad if right justified
1860f66f451Sopenharmony_ci  if (padto>0 && apad>len) printf("%*s", apad-len, "");
1870f66f451Sopenharmony_ci  crunch_str(&str, len, stdout, 0, crunch_rev_escape);
1880f66f451Sopenharmony_ci  if (padto<0 && apad>len) printf("%*s", apad-len, "");
1890f66f451Sopenharmony_ci
1900f66f451Sopenharmony_ci  return (apad > len) ? apad : len;
1910f66f451Sopenharmony_ci}
1920f66f451Sopenharmony_ci
1930f66f451Sopenharmony_ci// draw_trim_esc() with default escape
1940f66f451Sopenharmony_ciint draw_trim(char *str, int padto, int width)
1950f66f451Sopenharmony_ci{
1960f66f451Sopenharmony_ci  return draw_trim_esc(str, padto, width, 0, 0);
1970f66f451Sopenharmony_ci}
198