1 #include "toys.h"
2
3 // The design idea here is indexing a big blob of (potentially mmaped) data
4 // instead of copying the data into a zillion seperate malloc()s.
5
6 // A linestack is an array of struct ptr_len, with a currently used len
7 // and max tracking the memory allocation. This indexes existing string data,
8 // the lifetime of which is tracked externally.
9
10 // Insert one stack into another before position in old stack.
11 // (Does not copy contents of strings, just shuffles index array contents.)
linestack_addstack(struct linestack ** lls,struct linestack * throw,long pos)12 void 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 // Make a hole, allocating more space if necessary.
21 if (catch->len+throw->len >= catch->max) {
22 // New size rounded up to next multiple of 64, allocate and copy start.
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 // Copy end (into new allocation if necessary)
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 // Cleanup if we had to realloc.
34 if (catch != *lls) {
35 free(catch);
36 catch = *lls;
37 }
38
39 // Copy new chunk we made space for
40 memcpy(catch->idx+pos, throw->idx, throw->len*sizeof(struct ptr_len));
41 catch->len += throw->len;
42 }
43
44 // Insert one line/len into a linestack at pos
linestack_insert(struct linestack ** lls,long pos,char * line,long len)45 void linestack_insert(struct linestack **lls, long pos, char *line, long len)
46 {
47 // alloca() was in 32V and Turbo C for DOS, but isn't in posix or c99.
48 // This allocates enough memory for the linestack to have one ptr_len.
49 // (Even if a compiler adds gratuitous padidng that just makes it bigger.)
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
linestack_append(struct linestack ** lls,char * line)61 void linestack_append(struct linestack **lls, char *line)
62 {
63 linestack_insert(lls, (*lls)->len, line, strlen(line));
64 }
65
linestack_load(char * name)66 struct 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 // Show width many columns, negative means from right edge, out=0 just measure
89 // if escout, send it unprintable chars, otherwise pass through raw data.
90 // Returns width in columns, moves *str to end of data consumed.
crunch_str(char ** str,int width,FILE * out,char * escmore,int (* escout)(FILE * out,int cols,int wc))91 int 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 // standard escapes: ^X if <32, <XX> if invalid UTF8, U+XXXX if UTF8 !iswprint()
crunch_escape(FILE * out,int cols,int wc)126 int 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 // Display "standard" escapes in reverse video.
crunch_rev_escape(FILE * out,int cols,int wc)142 int 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 // Write width chars at start of string to strdout with standard escapes
154 // Returns length in columns so caller can pad it out with spaces.
draw_str(char * start,int width)155 int draw_str(char *start, int width)
156 {
157 return crunch_str(&start, width, stdout, 0, crunch_rev_escape);
158 }
159
160 // Return utf8 columns
utf8len(char * str)161 int utf8len(char *str)
162 {
163 return crunch_str(&str, INT_MAX, 0, 0, crunch_rev_escape);
164 }
165
166 // Return bytes used by (up to) this many columns
utf8skip(char * str,int width)167 int 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 // Print utf8 to stdout with standard escapes, trimmed to width and padded
177 // out to padto. If padto<0 left justify. Returns columns printed
draw_trim_esc(char * str,int padto,int width,char * escmore,int (* escout)(FILE * out,int cols,int wc))178 int 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 // Left pad if right justified
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 // draw_trim_esc() with default escape
draw_trim(char * str,int padto,int width)195 int draw_trim(char *str, int padto, int width)
196 {
197 return draw_trim_esc(str, padto, width, 0, 0);
198 }
199