Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 1 | #include "toys.h" |
| 2 | |
Rob Landley | 87897b2 | 2017-01-28 18:36:43 -0600 | [diff] [blame] | 3 | // A linestack is an array of struct ptr_len. |
| 4 | |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 5 | // Insert one stack into another before position in old stack. |
| 6 | // (Does not copy contents of strings, just shuffles index array contents.) |
| 7 | void linestack_addstack(struct linestack **lls, struct linestack *throw, |
| 8 | long pos) |
| 9 | { |
| 10 | struct linestack *catch = *lls; |
| 11 | |
| 12 | if (CFG_TOYBOX_DEBUG) |
| 13 | if (pos > catch->len) error_exit("linestack_addstack past end."); |
| 14 | |
| 15 | // Make a hole, allocating more space if necessary. |
| 16 | if (catch->len+throw->len >= catch->max) { |
| 17 | // New size rounded up to next multiple of 64, allocate and copy start. |
| 18 | catch->max = ((catch->len+throw->len)|63)+1; |
| 19 | *lls = xmalloc(sizeof(struct linestack)+catch->max*sizeof(struct ptr_len)); |
| 20 | memcpy(*lls, catch, sizeof(struct linestack)+pos*sizeof(struct ptr_len)); |
| 21 | } |
| 22 | |
| 23 | // Copy end (into new allocation if necessary) |
| 24 | if (pos != catch->len) |
| 25 | memmove((*lls)->idx+pos+throw->len, catch->idx+pos, |
| 26 | (catch->len-pos)*sizeof(struct ptr_len)); |
| 27 | |
| 28 | // Cleanup if we had to realloc. |
| 29 | if (catch != *lls) { |
| 30 | free(catch); |
| 31 | catch = *lls; |
| 32 | } |
| 33 | |
Rob Landley | 87897b2 | 2017-01-28 18:36:43 -0600 | [diff] [blame] | 34 | // Copy new chunk we made space for |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 35 | memcpy(catch->idx+pos, throw->idx, throw->len*sizeof(struct ptr_len)); |
| 36 | catch->len += throw->len; |
| 37 | } |
| 38 | |
Rob Landley | 87897b2 | 2017-01-28 18:36:43 -0600 | [diff] [blame] | 39 | // Insert one line/len into a linestack at pos |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 40 | void linestack_insert(struct linestack **lls, long pos, char *line, long len) |
| 41 | { |
| 42 | // alloca() was in 32V and Turbo C for DOS, but isn't in posix or c99. |
Rob Landley | 87897b2 | 2017-01-28 18:36:43 -0600 | [diff] [blame] | 43 | // This allocates enough memory for the linestack to have one ptr_len. |
| 44 | // (Even if a compiler adds gratuitous padidng that just makes it bigger.) |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 45 | struct { |
| 46 | struct linestack ls; |
| 47 | struct ptr_len pl; |
| 48 | } ls; |
| 49 | |
| 50 | ls.ls.len = ls.ls.max = 1; |
| 51 | ls.ls.idx[0].ptr = line; |
| 52 | ls.ls.idx[0].len = len; |
| 53 | linestack_addstack(lls, &ls.ls, pos); |
| 54 | } |
| 55 | |
| 56 | void linestack_append(struct linestack **lls, char *line) |
| 57 | { |
| 58 | linestack_insert(lls, (*lls)->len, line, strlen(line)); |
| 59 | } |
| 60 | |
| 61 | struct linestack *linestack_load(char *name) |
| 62 | { |
| 63 | FILE *fp = fopen(name, "r"); |
| 64 | struct linestack *ls; |
| 65 | |
| 66 | if (!fp) return 0; |
| 67 | |
| 68 | ls = xzalloc(sizeof(struct linestack)); |
| 69 | |
| 70 | for (;;) { |
| 71 | char *line = 0; |
| 72 | ssize_t len; |
| 73 | |
| 74 | if ((len = getline(&line, (void *)&len, fp))<1) break; |
| 75 | if (line[len-1]=='\n') len--; |
| 76 | linestack_insert(&ls, ls->len, line, len); |
| 77 | } |
| 78 | fclose(fp); |
| 79 | |
| 80 | return ls; |
| 81 | } |
| 82 | |
Rob Landley | 87897b2 | 2017-01-28 18:36:43 -0600 | [diff] [blame] | 83 | // Show width many columns, negative means from right edge, out=0 just measure |
| 84 | // if escout, send it unprintable chars, otherwise pass through raw data. |
Rob Landley | 544c1ec | 2016-01-17 14:08:10 -0600 | [diff] [blame] | 85 | // Returns width in columns, moves *str to end of data consumed. |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 86 | int crunch_str(char **str, int width, FILE *out, char *escmore, |
| 87 | int (*escout)(FILE *out, int cols, int wc)) |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 88 | { |
Rob Landley | 544c1ec | 2016-01-17 14:08:10 -0600 | [diff] [blame] | 89 | int columns = 0, col, bytes; |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 90 | char *start, *end; |
| 91 | |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 92 | for (end = start = *str; *end; columns += col, end += bytes) { |
| 93 | wchar_t wc; |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 94 | |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 95 | if ((bytes = mbrtowc(&wc, end, MB_CUR_MAX, 0))>0 && (col = wcwidth(wc))>=0) |
Rob Landley | 544c1ec | 2016-01-17 14:08:10 -0600 | [diff] [blame] | 96 | { |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 97 | if (!escmore || wc>255 || !strchr(escmore, wc)) { |
| 98 | if (width-columns<col) break; |
| 99 | if (out) fwrite(end, bytes, 1, out); |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 100 | |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 101 | continue; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (bytes<1) { |
| 106 | bytes = 1; |
| 107 | wc = *end; |
| 108 | } |
| 109 | col = width-columns; |
| 110 | if (col<1) break; |
Rob Landley | 817f059 | 2016-10-20 15:29:10 -0500 | [diff] [blame] | 111 | if (escout) col = escout(out, col, wc); |
| 112 | else if (out) fwrite(end, bytes, 1, out); |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 113 | } |
| 114 | *str = end; |
| 115 | |
| 116 | return columns; |
| 117 | } |
| 118 | |
Rob Landley | 87897b2 | 2017-01-28 18:36:43 -0600 | [diff] [blame] | 119 | |
| 120 | // standard escapes: ^X if <32, <XX> if invliad UTF8, U+XXXX if UTF8 !iswprint() |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 121 | int crunch_escape(FILE *out, int cols, int wc) |
| 122 | { |
| 123 | char buf[8]; |
| 124 | int rc; |
| 125 | |
| 126 | if (wc<' ') rc = sprintf(buf, "^%c", '@'+wc); |
| 127 | else if (wc<256) rc = sprintf(buf, "<%02X>", wc); |
| 128 | else rc = sprintf(buf, "U+%04X", wc); |
| 129 | |
| 130 | if (rc > cols) buf[rc = cols] = 0; |
| 131 | if (out) fputs(buf, out); |
| 132 | |
| 133 | return rc; |
| 134 | } |
| 135 | |
Rob Landley | 87897b2 | 2017-01-28 18:36:43 -0600 | [diff] [blame] | 136 | // Display "standard" escapes in reverse video. |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 137 | int crunch_rev_escape(FILE *out, int cols, int wc) |
| 138 | { |
| 139 | int rc; |
| 140 | |
| 141 | tty_esc("7m"); |
| 142 | rc = crunch_escape(out, cols, wc); |
| 143 | tty_esc("27m"); |
| 144 | |
| 145 | return rc; |
| 146 | } |
| 147 | |
Rob Landley | 544c1ec | 2016-01-17 14:08:10 -0600 | [diff] [blame] | 148 | // Write width chars at start of string to strdout with standard escapes |
| 149 | // Returns length in columns so caller can pad it out with spaces. |
| 150 | int draw_str(char *start, int width) |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 151 | { |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 152 | return crunch_str(&start, width, stdout, 0, crunch_rev_escape); |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 153 | } |
| 154 | |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 155 | // Return utf8 columns |
| 156 | int utf8len(char *str) |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 157 | { |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 158 | return crunch_str(&str, INT_MAX, 0, 0, crunch_rev_escape); |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 159 | } |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 160 | |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 161 | // Return bytes used by (up to) this many columns |
| 162 | int utf8skip(char *str, int width) |
| 163 | { |
| 164 | char *s = str; |
| 165 | |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 166 | crunch_str(&s, width, 0, 0, crunch_rev_escape); |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 167 | |
| 168 | return s-str; |
| 169 | } |
| 170 | |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 171 | // Print utf8 to stdout with standard escapes, trimmed to width and padded |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 172 | // out to padto. If padto<0 left justify. Returns columns printed |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 173 | int draw_trim_esc(char *str, int padto, int width, char *escmore, |
| 174 | int (*escout)(FILE *out, int cols, int wc)) |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 175 | { |
| 176 | int apad = abs(padto), len = utf8len(str); |
| 177 | |
Rob Landley | 87897b2 | 2017-01-28 18:36:43 -0600 | [diff] [blame] | 178 | if (padto>=0 && len>width) str += utf8skip(str, len-width); |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 179 | if (len>width) len = width; |
| 180 | |
| 181 | // Left pad if right justified |
| 182 | if (padto>0 && apad>len) printf("%*s", apad-len, ""); |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 183 | crunch_str(&str, len, stdout, 0, crunch_rev_escape); |
Rob Landley | ba86864 | 2016-01-17 17:16:03 -0600 | [diff] [blame] | 184 | if (padto<0 && apad>len) printf("%*s", apad-len, ""); |
| 185 | |
| 186 | return (apad > len) ? apad : len; |
Rob Landley | e32e802 | 2015-12-23 18:16:23 -0600 | [diff] [blame] | 187 | } |
Rob Landley | 2b999e6 | 2016-03-15 02:23:50 -0500 | [diff] [blame] | 188 | |
| 189 | // draw_trim_esc() with default escape |
| 190 | int draw_trim(char *str, int padto, int width) |
| 191 | { |
| 192 | return draw_trim_esc(str, padto, width, 0, 0); |
| 193 | } |