1#include "toys.h"
2
3
4
5
6
7
8
9
10
11
12void linestack_addstack(struct linestack **lls, struct linestack *throw,
13 long pos)
14{
15 struct linestack *catch = *lls;
16
17 if (CFG_TOYBOX_DEBUG)
18 if (pos > catch->len) error_exit("linestack_addstack past end.");
19
20
21 if (catch->len+throw->len >= catch->max) {
22
23 catch->max = ((catch->len+throw->len)|63)+1;
24 *lls = xmalloc(sizeof(struct linestack)+catch->max*sizeof(struct ptr_len));
25 memcpy(*lls, catch, sizeof(struct linestack)+pos*sizeof(struct ptr_len));
26 }
27
28
29 if (pos != catch->len)
30 memmove((*lls)->idx+pos+throw->len, catch->idx+pos,
31 (catch->len-pos)*sizeof(struct ptr_len));
32
33
34 if (catch != *lls) {
35 free(catch);
36 catch = *lls;
37 }
38
39
40 memcpy(catch->idx+pos, throw->idx, throw->len*sizeof(struct ptr_len));
41 catch->len += throw->len;
42}
43
44
45void linestack_insert(struct linestack **lls, long pos, char *line, long len)
46{
47
48
49
50 struct {
51 struct ptr_len pl;
52 struct linestack ls;
53 } ls;
54
55 ls.ls.len = ls.ls.max = 1;
56 ls.ls.idx[0].ptr = line;
57 ls.ls.idx[0].len = len;
58 linestack_addstack(lls, &ls.ls, pos);
59}
60
61void linestack_append(struct linestack **lls, char *line)
62{
63 linestack_insert(lls, (*lls)->len, line, strlen(line));
64}
65
66struct linestack *linestack_load(char *name)
67{
68 FILE *fp = fopen(name, "r");
69 struct linestack *ls;
70
71 if (!fp) return 0;
72
73 ls = xzalloc(sizeof(struct linestack));
74
75 for (;;) {
76 char *line = 0;
77 ssize_t len;
78
79 if ((len = getline(&line, (void *)&len, fp))<1) break;
80 if (line[len-1]=='\n') len--;
81 linestack_insert(&ls, ls->len, line, len);
82 }
83 fclose(fp);
84
85 return ls;
86}
87
88
89
90
91int crunch_str(char **str, int width, FILE *out, char *escmore,
92 int (*escout)(FILE *out, int cols, int wc))
93{
94 int columns = 0, col, bytes;
95 char *start, *end;
96 unsigned wc;
97
98 for (end = start = *str; *end; columns += col, end += bytes) {
99 if ((bytes = utf8towc(&wc, end, 4))>0 && (col = wcwidth(wc))>=0) {
100 if (!escmore || wc>255 || !strchr(escmore, wc)) {
101 if (width-columns<col) break;
102 if (out) fwrite(end, bytes, 1, out);
103
104 continue;
105 }
106 }
107
108 if (bytes<1) {
109 bytes = 1;
110 wc = *end;
111 }
112 col = width-columns;
113 if (col<1) break;
114 if (escout) {
115 if ((col = escout(out, col, wc))<0) break;
116 } else if (out) fwrite(end, 1, bytes, out);
117 }
118 *str = end;
119
120 return columns;
121}
122
123
124
125int crunch_escape(FILE *out, int cols, int wc)
126{
127 char buf[11];
128 int rc;
129
130 if (wc<' ') rc = sprintf(buf, "^%c", '@'+wc);
131 else if (wc<256) rc = sprintf(buf, "<%02X>", wc);
132 else rc = sprintf(buf, "U+%04X", wc);
133
134 if (rc > cols) buf[rc = cols] = 0;
135 if (out) fputs(buf, out);
136
137 return rc;
138}
139
140
141int crunch_rev_escape(FILE *out, int cols, int wc)
142{
143 int rc;
144
145 tty_esc("7m");
146 rc = crunch_escape(out, cols, wc);
147 tty_esc("27m");
148
149 return rc;
150}
151
152
153
154int draw_str(char *start, int width)
155{
156 return crunch_str(&start, width, stdout, 0, crunch_rev_escape);
157}
158
159
160int utf8len(char *str)
161{
162 return crunch_str(&str, INT_MAX, 0, 0, crunch_rev_escape);
163}
164
165
166int utf8skip(char *str, int width)
167{
168 char *s = str;
169
170 crunch_str(&s, width, 0, 0, crunch_rev_escape);
171
172 return s-str;
173}
174
175
176
177int draw_trim_esc(char *str, int padto, int width, char *escmore,
178 int (*escout)(FILE *out, int cols, int wc))
179{
180 int apad = abs(padto), len = utf8len(str);
181
182 if (padto>=0 && len>width) str += utf8skip(str, len-width);
183 if (len>width) len = width;
184
185
186 if (padto>0 && apad>len) printf("%*s", apad-len, "");
187 crunch_str(&str, len, stdout, 0, crunch_rev_escape);
188 if (padto<0 && apad>len) printf("%*s", apad-len, "");
189
190 return (apad > len) ? apad : len;
191}
192
193
194int draw_trim(char *str, int padto, int width)
195{
196 return draw_trim_esc(str, padto, width, 0, 0);
197}
198