1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/*
* mc - columnate
*
* mc[-][-LINEWIDTH][-t][file...]
* - causes break on colon
* -LINEWIDTH sets width of line in which to columnate(default 80)
* -t suppresses expanding multiple blanks into tabs
*
*/
#include <u.h>
#include <sys/ioctl.h>
#include <termios.h>
#ifdef HAS_SYS_TERMIOS
#include <sys/termios.h>
#endif
#include <libc.h>
#include <draw.h>
#include <bio.h>
#include <fcall.h>
#include <9pclient.h>
#include <thread.h>
#define WIDTH 80
#define TAB 4
#define WORD_ALLOC_QUANTA 1024
#define ALLOC_QUANTA 4096
int wordsize(Rune*, int);
int nexttab(int);
int tabwid;
int mintab = 1;
int linewidth=WIDTH;
int colonflag=0;
int tabflag=0; /* -t flag turned off forever, except in acme */
Rune *cbuf, *cbufp;
Rune **word;
int maxwidth=0;
int nalloc=ALLOC_QUANTA;
int nwalloc=WORD_ALLOC_QUANTA;
int nchars=0;
int nwords=0;
Biobuf bin;
Biobuf bout;
void getwidth(void), readbuf(int), error(char *);
void scanwords(void), columnate(void), morechars(void);
void
threadmain(int argc, char *argv[])
{
int i;
int lineset;
int ifd;
lineset = 0;
Binit(&bout, 1, OWRITE);
while(argc > 1 && argv[1][0] == '-'){
--argc; argv++;
switch(argv[0][1]){
case '\0':
colonflag = 1;
break;
case 't':
tabflag = 0;
break;
default:
linewidth = atoi(&argv[0][1]);
if(linewidth <= 1)
linewidth = WIDTH;
lineset = 1;
break;
}
}
if(lineset == 0)
getwidth();
cbuf = cbufp = malloc(ALLOC_QUANTA*(sizeof *cbuf));
word = malloc(WORD_ALLOC_QUANTA*(sizeof *word));
if(word == 0 || cbuf == 0)
error("out of memory");
if(argc == 1)
readbuf(0);
else{
for(i = 1; i < argc; i++){
if((ifd = open(*++argv, OREAD)) == -1)
fprint(2, "mc: can't open %s (%r)\n", *argv);
else{
readbuf(ifd);
Bflush(&bin);
close(ifd);
}
}
}
columnate();
Bflush(&bout);
threadexitsall(0);
}
void
error(char *s)
{
fprint(2, "mc: %s\n", s);
threadexitsall(s);
}
void
readbuf(int fd)
{
int lastwascolon = 0;
long c;
int linesiz = 0;
Binit(&bin, fd, OREAD);
do{
if(nchars++ >= nalloc)
morechars();
*cbufp++ = c = Bgetrune(&bin);
linesiz++;
if(c == '\t') {
cbufp[-1] = L' ';
while(linesiz%TAB != 0) {
if(nchars++ >= nalloc)
morechars();
*cbufp++ = L' ';
linesiz++;
}
}
if(colonflag && c == ':')
lastwascolon++;
else if(lastwascolon){
if(c == '\n'){
--nchars; /* skip newline */
*cbufp = L'\0';
while(nchars > 0 && cbuf[--nchars] != '\n')
;
if(nchars)
nchars++;
columnate();
if (nchars)
Bputc(&bout, '\n');
Bprint(&bout, "%S", cbuf+nchars);
nchars = 0;
cbufp = cbuf;
}
lastwascolon = 0;
}
if(c == '\n')
linesiz = 0;
}while(c >= 0);
}
void
scanwords(void)
{
Rune *p, *q;
int i, w;
nwords=0;
maxwidth=0;
for(p = q = cbuf, i = 0; i < nchars; i++){
if(*p++ == L'\n'){
if(nwords >= nwalloc){
nwalloc += WORD_ALLOC_QUANTA;
if((word = realloc(word, nwalloc*sizeof(*word)))==0)
error("out of memory");
}
word[nwords++] = q;
p[-1] = L'\0';
w = wordsize(q, p-q-1);
if(w > maxwidth)
maxwidth = w;
q = p;
}
}
}
void
columnate(void)
{
int i, j;
int words_per_line;
int nlines;
int col;
int endcol;
scanwords();
if(nwords==0)
return;
maxwidth = nexttab(maxwidth+mintab-1);
words_per_line = linewidth/maxwidth;
if(words_per_line <= 0)
words_per_line = 1;
nlines=(nwords+words_per_line-1)/words_per_line;
for(i = 0; i < nlines; i++){
col = endcol = 0;
for(j = i; j < nwords; j += nlines){
endcol += maxwidth;
Bprint(&bout, "%S", word[j]);
col += wordsize(word[j], runestrlen(word[j]));
if(j+nlines < nwords){
if(tabflag) {
while(col < endcol){
Bputc(&bout, '\t');
col = nexttab(col);
}
}else{
while(col < endcol){
Bputc(&bout, ' ');
col++;
}
}
}
}
Bputc(&bout, '\n');
}
}
int
wordsize(Rune *w, int nw)
{
if(nw < 0)
abort();
if(font)
return runestringnwidth(font, w, nw);
return nw;
}
int
nexttab(int col)
{
if(tabwid){
col += tabwid;
col -= col%tabwid;
return col;
}
return col+1;
}
void
morechars(void)
{
nalloc += ALLOC_QUANTA;
if((cbuf = realloc(cbuf, nalloc*sizeof(*cbuf))) == 0)
error("out of memory");
cbufp = cbuf+nchars-1;
}
/*
* These routines discover the width of the display.
* It takes some work. If we do the easy calls to the
* draw library, the screen flashes due to repainting
* when mc exits.
*/
int
windowrect(struct winsize *ws)
{
int tty;
if((tty = open("/dev/tty", OWRITE)) < 0)
tty = 1;
if(ioctl(tty, TIOCGWINSZ, ws) < 0){
if(tty != 1)
close(tty);
return -1;
}
if(tty != 1)
close(tty);
return 0;
}
void
getwidth(void)
{
CFsys *fs;
char buf[500], *p, *q, *f[10], *fname;
int fd, n, nf, scale;
struct winsize ws;
Font *f1;
if((p = getenv("winid")) != nil){
fs = nsmount("acme", "");
if(fs == nil)
return;
snprint(buf, sizeof buf, "acme/%d/ctl", atoi(p));
if((fd = fsopenfd(fs, buf, OREAD)) < 0)
return;
if((n=readn(fd, buf, sizeof buf-1)) <= 0)
return;
buf[n] = 0;
if((nf=tokenize(buf, f, nelem(f))) < 7)
return;
// hidpi font in stringwidth(3) will call scalesubfont,
// which aborts in bytesperline, due to unknow depth,
// without initdraw. We scale by ourselves.
scale = parsefontscale(f[6], &fname);
tabwid = 0;
if(nf >= 8 && (tabwid = atoi(f[7])/scale) == 0)
return;
if((font = openfont(nil, fname)) == nil)
return;
mintab = stringwidth(font, "0");
if(tabwid == 0)
tabwid = mintab*4;
linewidth = atoi(f[5]) / scale;
tabflag = 1;
return;
}
if((p = getenv("termprog")) != nil && strcmp(p, "9term") == 0)
if((p = getenv("font")) != nil)
font = openfont(nil, p);
if(windowrect(&ws) < 0)
return;
if(ws.ws_xpixel == 0)
font = nil;
if(font){
// 9term leaves "is this a hidpi display" in the low bit of the ypixel height.
if(ws.ws_ypixel&1) {
// need hidpi font.
// loadhifpi creates a font that crashes in stringwidth,
// for reasons i don't understand.
// do it ourselves
p = getenv("font");
q = strchr(p, ',');
f1 = nil;
if(q != nil)
f1 = openfont(nil, q+1);
if(f1 != nil)
font = f1;
else
ws.ws_xpixel /= 2;
}
mintab = stringwidth(font, "0");
if((p = getenv("tabstop")) != nil)
tabwid = atoi(p)*mintab;
else
tabwid = 4*mintab;
tabflag = 1;
linewidth = ws.ws_xpixel;
}else
linewidth = ws.ws_col;
}
|