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
|
implement Gzip;
include "sys.m";
sys: Sys;
print, fprint: import sys;
include "draw.m";
include "string.m";
str: String;
include "daytime.m";
daytime: Daytime;
include "bufio.m";
bufio: Bufio;
Iobuf: import bufio;
include "filter.m";
deflate: Filter;
DEFLATEPATH: con "/dis/lib/deflate.dis";
Gzip: module
{
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
Arg: adt
{
argv: list of string;
c: int;
opts: string;
init: fn(argv: list of string): ref Arg;
opt: fn(arg: self ref Arg): int;
arg: fn(arg: self ref Arg): string;
};
argv0: con "gzip";
stderr: ref Sys->FD;
debug := 0;
verbose := 0;
level := 0;
usage()
{
fprint(stderr, "usage: %s [-vD1-9] [file ...]\n", argv0);
raise "fail:usage";
}
nomod(path: string)
{
sys->fprint(stderr, "%s: cannot load %s: %r\n", argv0, path);
raise "fail:bad module";
}
init(nil: ref Draw->Context, argv: list of string)
{
sys = load Sys Sys->PATH;
stderr = sys->fildes(2);
bufio = load Bufio Bufio->PATH;
if (bufio == nil)
nomod(Bufio->PATH);
str = load String String->PATH;
if (str == nil)
nomod(String->PATH);
daytime = load Daytime Daytime->PATH;
if (daytime == nil)
nomod(Daytime->PATH);
deflate = load Filter DEFLATEPATH;
if(deflate == nil)
nomod(DEFLATEPATH);
arg := Arg.init(argv);
level = 6;
while(c := arg.opt()){
case c{
'D' =>
debug++;
'v' =>
verbose++;
'1' to '9' =>
level = c - '0';
* =>
usage();
}
}
deflate->init();
argv = arg.argv;
ok := 1;
if(len argv == 0){
bin := bufio->fopen(sys->fildes(0), Bufio->OREAD);
bout := bufio->fopen(sys->fildes(1), Bufio->OWRITE);
ok = gzip(nil, daytime->now(), bin, bout, "stdin", "stdout");
bout.close();
bin.close();
}else{
for(; argv != nil; argv = tl argv)
ok &= gzipf(hd argv);
}
exit;
}
gzipf(file: string): int
{
bin := bufio->open(file, Bufio->OREAD);
if(bin == nil){
fprint(stderr, "%s: can't open %s: %r\n", argv0, file);
return 0;
}
(ok, dir) := sys->fstat(bin.fd);
if(ok >= 0)
mtime := dir.mtime;
else
mtime = daytime->now();
(nil, ofile) := str->splitr(file, "/");
ofile += ".gz";
bout := bufio->create(ofile, Bufio->OWRITE, 8r666);
if(bout == nil){
fprint(stderr, "%s: can't open %s: %r\n", argv0, ofile);
bin.close();
return 0;
}
ok = gzip(file, mtime, bin, bout, file, ofile);
bout.close();
bin.close();
if (ok)
sys->remove(file);
else
sys->remove(ofile);
return ok;
}
gzip(nil: string, nil: int, bin, bout: ref Iobuf, fin, fout: string): int
{
param := "h" + string level;
incount := outcount := 0;
if (debug)
param += "dv";
rq := deflate->start(param);
crc := 0;
for (;;) {
pick m := <-rq {
Fill =>
n := bin.read(m.buf, len m.buf);
m.reply <-= n;
if (n == -1) {
sys->fprint(stderr, "%s: error reading %s: %r\n", argv0, fin);
return 0;
}
incount += n;
Result =>
n := len m.buf;
if (bout.write(m.buf, n) != n) {
sys->fprint(stderr, "%s: error writing %s: %r\n", argv0, fout);
m.reply <-= -1;
return 0;
}
m.reply <-= 0;
outcount += n;
Info =>
sys->fprint(stderr, "%s\n", m.msg);
Finished =>
comp := 0.0;
if (incount > 0)
comp = 1.0 - real outcount / real incount;
if (verbose)
sys->fprint(stderr, "%s: %5.2f%%\n", fin, comp * 100.0);
return 1;
Error =>
sys->fprint(stderr, "%s: error compressing %s: %s\n", argv0, fin, m.e);
return 0;
}
}
}
fatal(msg: string)
{
fprint(stderr, "%s: %s\n", argv0, msg);
exit;
}
Arg.init(argv: list of string): ref Arg
{
if(argv != nil)
argv = tl argv;
return ref Arg(argv, 0, nil);
}
Arg.opt(arg: self ref Arg): int
{
if(arg.opts != ""){
arg.c = arg.opts[0];
arg.opts = arg.opts[1:];
return arg.c;
}
if(arg.argv == nil)
return arg.c = 0;
arg.opts = hd arg.argv;
if(len arg.opts < 2 || arg.opts[0] != '-')
return arg.c = 0;
arg.argv = tl arg.argv;
if(arg.opts == "--")
return arg.c = 0;
arg.c = arg.opts[1];
arg.opts = arg.opts[2:];
return arg.c;
}
Arg.arg(arg: self ref Arg): string
{
s := arg.opts;
arg.opts = "";
if(s != "")
return s;
if(arg.argv == nil)
return "";
s = hd arg.argv;
arg.argv = tl arg.argv;
return s;
}
|