summaryrefslogtreecommitdiff
path: root/appl/cmd/lc.b
blob: de5ec57924ad8ad7cd6492fcfe6ea57672bc76dc (plain)
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
implement Lc;

include "sys.m";
	sys: Sys;
include "draw.m";
include "readdir.m";
	readdir: Readdir;
include "bufio.m";
	bufio: Bufio;
	Iobuf: import bufio;

Lc: module {
	init: fn(nil: ref Draw->Context, argv: list of string);
};

t_FILE, t_DIR, t_NUMTYPES: con iota;
columns := 65;
stderr: ref Sys->FD;
stdout: ref Iobuf;

usage()
{
	sys->fprint(stderr, "usage: lc [-df] [-c columns] [file ...]\n");
	raise "fail:usage";
}

init(nil: ref Draw->Context, argv: list of string)
{
	sys = load Sys Sys->PATH;
	stderr = sys->fildes(2);
	readdir = load Readdir Readdir->PATH;
	if (readdir == nil) {
		sys->fprint(stderr, "lc: cannot load %s: %r\n", Readdir->PATH);
		raise "fail:bad module";
	}
	bufio = load Bufio Bufio->PATH;
	stdout = bufio->fopen(sys->fildes(1), Sys->OWRITE);
	if (bufio == nil) {
		sys->fprint(stderr, "lc: cannot load %s: %r\n", Bufio->PATH);
		raise "fail:bad module";
	}
	if (argv == nil)
		return;
	argv = tl argv;
	flags := 0;
loop:	while (argv != nil && hd argv != nil && (hd argv)[0] == '-') {
		s := (hd argv)[1:];
		argv = tl argv;
	flagloop: for (; s != nil; s = s[1:]) {
			case s[0] {
			'-' =>
				break loop;
			'd' =>
				flags |= 1 << t_DIR;
			'f' =>
				flags |= 1 << t_FILE;
			'c' =>
				if (len s > 1) {
					columns = int s[1:];
					break flagloop;
				}
				if (argv == nil)
					usage();
				columns = int hd argv;
				argv = tl argv;
			* =>
				usage();
			}
		}
	}
					
	headings := 0;
	if (flags == 0) {
		flags = (1<<t_DIR)|(1<<t_FILE);
		headings = 1;
	}
	if (argv == nil)
		argv = "." :: nil;
	multi := tl argv != nil;
	nondir: list of string;
	for (; argv != nil; argv = tl argv) {
		dname := hd argv;
		(ok, dir) := sys->stat(dname);
		if(ok < 0) {
			sys->fprint(stderr, "lc: can't stat %s: %r\n", hd argv);
			continue;
		}
		if (dir.mode & Sys->DMDIR) {
			(d, n) := readdir->init(hd argv, Readdir->NAME | Readdir->COMPACT);
			if (n < 0)
				sys->fprint(stderr, "lc: cannot read %s: %r\n", hd argv);
			else {
				indent := 0;
				if (multi && headings) {
					stdout.puts(hd argv + "/\n");
					indent = 2;
				}
				l: list of string = nil;
				for (i := 0; i < n; i++) {
					s := d[i].name;
					if (!headings && dname != ".")
						s = dname + "/" + s;
					if (d[i].mode & Sys->DMDIR) {
						if (flags & (1<<t_DIR))
							l = s + "/" :: l;
					} else if (flags & (1<<t_FILE))
						l = s :: l;
				}
				d = nil;
				lc(l, indent);
			}
		} else if (flags & (1 << t_FILE))
			nondir = dname :: nondir;
	}
	lc(nondir, 0);
	stdout.close();
}

lc(dl: list of string, indent: int)
{
	a := array[len dl] of string;
	j := len a - 1;
	maxwidth := 0;
	for (; dl != nil; dl = tl dl) {
		s := hd dl;
		a[j--] = s;
		if (len s > maxwidth)
			maxwidth = len s;
	}
	outcols(a, maxwidth, indent);
}
		
outcols(stuff: array of string, maxwidth, indent: int)
{
	num := len stuff;
	cols := columns - indent;
	numcols := cols / (maxwidth + 1);
	colwidth: int;
	if (numcols == 0) {
		numcols = 1;
		colwidth = maxwidth;
	} else
		colwidth = cols / numcols;
	numrows := (num + numcols - 1) / numcols;
	
	for (i := 0; i < numrows; i++) {
		if (indent)
			stdout.puts(sys->sprint("%*s", indent, ""));
		for (j := i; j < num; j += numrows) {
			if (j + numrows < num)
				stdout.puts(sys->sprint("%*.*s", -colwidth, colwidth, stuff[j]));
			else
				stdout.puts(sys->sprint("%.*s\n", colwidth, stuff[j]));
		}
	}
}