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
97 for (end = start = *str; *end; columns += col, end += bytes) {
98 wchar_t wc;
99
100 if ((bytes = utf8towc(&wc, end, 4))>0 && (col = wcwidth(wc))>=0) {
101 if (!escmore || wc>255 || !strchr(escmore, wc)) {
102 if (width-columns<col) break;
103 if (out) fwrite(end, bytes, 1, out);
104
105 continue;
106 }
107 }
108
109 if (bytes<1) {
110 bytes = 1;
111 wc = *end;
112 }
113 col = width-columns;
114 if (col<1) break;
115 if (escout) {
116 if ((col = escout(out, col, wc))<0) break;
117 } else if (out) fwrite(end, 1, bytes, out);
118 }
119 *str = end;
120
121 return columns;
122}
123
124
125
126int crunch_escape(FILE *out, int cols, int wc)
127{
128 char buf[11];
129 int rc;
130
131 if (wc<' ') rc = sprintf(buf, "^%c", '@'+wc);
132 else if (wc<256) rc = sprintf(buf, "<%02X>", wc);
133 else rc = sprintf(buf, "U+%04X", wc);
134
135 if (rc > cols) buf[rc = cols] = 0;
136 if (out) fputs(buf, out);
137
138 return rc;
139}
140
141
142int crunch_rev_escape(FILE *out, int cols, int wc)
143{
144 int rc;
145
146 tty_esc("7m");
147 rc = crunch_escape(out, cols, wc);
148 tty_esc("27m");
149
150 return rc;
151}
152
153
154
155int draw_str(char *start, int width)
156{
157 return crunch_str(&start, width, stdout, 0, crunch_rev_escape);
158}
159
160
161int utf8len(char *str)
162{
163 return crunch_str(&str, INT_MAX, 0, 0, crunch_rev_escape);
164}
165
166
167int utf8skip(char *str, int width)
168{
169 char *s = str;
170
171 crunch_str(&s, width, 0, 0, crunch_rev_escape);
172
173 return s-str;
174}
175
176
177
178int draw_trim_esc(char *str, int padto, int width, char *escmore,
179 int (*escout)(FILE *out, int cols, int wc))
180{
181 int apad = abs(padto), len = utf8len(str);
182
183 if (padto>=0 && len>width) str += utf8skip(str, len-width);
184 if (len>width) len = width;
185
186
187 if (padto>0 && apad>len) printf("%*s", apad-len, "");
188 crunch_str(&str, len, stdout, 0, crunch_rev_escape);
189 if (padto<0 && apad>len) printf("%*s", apad-len, "");
190
191 return (apad > len) ? apad : len;
192}
193
194
195int draw_trim(char *str, int padto, int width)
196{
197 return draw_trim_esc(str, padto, width, 0, 0);
198}
199