OSDN Git Service

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