blob: 5e7854156aab434ea6e8cfe8dc22ac8e84109d1d (
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
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
343
344
345
346
347
348
349
|
// **********************************************************************
//
// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
import Filesystem.*;
class Parser
{
Parser(DirectoryPrx root)
{
_dirs = new java.util.LinkedList<DirectoryPrx>();
_dirs.addFirst(root);
}
void
usage()
{
System.err.print(
"help Print this message.\n" +
"pwd Print current directory (/ = root).\n" +
"cd [DIR] Change directory (/ or empty = root).\n" +
"ls List current directory.\n" +
"lr Recursively list current directory.\n" +
"mkdir DIR [DIR...] Create directories DIR in current directory.\n" +
"mkfile FILE [FILE...] Create files FILE in current directory.\n" +
"rm NAME [NAME...] Delete directory or file NAME (rm * to delete all).\n" +
"cat FILE List the contents of FILE.\n" +
"write FILE [STRING...] Write STRING to FILE.\n" +
"exit, quit Exit this program.\n");
}
void
list(boolean recursive)
{
try
{
list(_dirs.get(0), recursive, 0);
}
catch(Ice.LocalException ex)
{
error(ex.toString());
}
}
void
list(Filesystem.DirectoryPrx dir, boolean recursive, int depth)
{
StringBuilder b = new StringBuilder();
for(int i = 0; i < depth; ++i)
{
b.append('\t');
}
String indent = b.toString();
NodeDesc[] contents = dir.list();
for(int i = 0; i < contents.length; ++i)
{
DirectoryPrx d
= contents[i].type == NodeType.DirType
? DirectoryPrxHelper.uncheckedCast(contents[i].proxy)
: null;
System.out.print(indent + contents[i].name + (d != null ? " (directory)" : " (file)"));
if(d != null && recursive)
{
System.out.println(":");
list(d, true, ++depth);
}
else
{
System.out.println();
}
}
}
void
createFile(java.util.List<String> names)
{
DirectoryPrx dir = _dirs.getFirst();
for(String name : names)
{
if(name.equals(".."))
{
System.out.println("Cannot create a file named `..'");
continue;
}
try
{
dir.createFile(name);
}
catch(NameInUse ex)
{
System.out.println("`" + name + "' exists already");
}
}
}
void
createDir(java.util.List<String> names)
{
DirectoryPrx dir = _dirs.getFirst();
for(String name : names)
{
if(name.equals(".."))
{
System.out.println("Cannot create a directory named `..'");
continue;
}
try
{
dir.createDirectory(name);
}
catch(NameInUse ex)
{
System.out.println("`" + name + "' exists already");
}
}
}
void
pwd()
{
if(_dirs.size() == 1)
{
System.out.print("/");
}
else
{
java.util.ListIterator<DirectoryPrx> i = _dirs.listIterator(_dirs.size());
i.previous();
while(i.hasPrevious())
{
System.out.print("/" + i.previous().name());
}
}
System.out.println();
}
void
cd(String name)
{
if(name.equals("/"))
{
while(_dirs.size() > 1)
{
_dirs.removeFirst();
}
return;
}
if(name.equals(".."))
{
if(_dirs.size() > 1)
{
_dirs.removeFirst();
}
return;
}
DirectoryPrx dir = _dirs.getFirst();
NodeDesc d;
try
{
d = dir.find(name);
}
catch(NoSuchName ex)
{
System.out.println("`" + name + "': no such directory");
return;
}
if(d.type == NodeType.FileType)
{
System.out.println("`" + name + "': not a directory");
return;
}
_dirs.addFirst(DirectoryPrxHelper.uncheckedCast(d.proxy));
}
void
cat(String name)
{
DirectoryPrx dir = _dirs.getFirst();
NodeDesc d;
try
{
d = dir.find(name);
}
catch(NoSuchName ex)
{
System.out.println("`" + name + "': no such file");
return;
}
if(d.type == NodeType.DirType)
{
System.out.println("`" + name + "': not a file");
return;
}
FilePrx f = FilePrxHelper.uncheckedCast(d.proxy);
String[] l = f.read();
for(int i = 0; i < l.length; ++i)
{
System.out.println(l[i]);
}
}
void
write(java.util.LinkedList<String> args)
{
DirectoryPrx dir = _dirs.getFirst();
String name = args.getFirst();
args.removeFirst();
NodeDesc d;
try
{
d = dir.find(name);
}
catch(NoSuchName ex)
{
System.out.println("`" + name + "': no such file");
return;
}
if(d.type == NodeType.DirType)
{
System.out.println("`" + name + "': not a file");
return;
}
FilePrx f = FilePrxHelper.uncheckedCast(d.proxy);
String[] l = args.toArray(new String[0]);
try
{
f.write(l);
}
catch(GenericError ex)
{
System.out.println("`" + name + "': cannot write to file: " + ex.reason);
}
}
void
destroy(java.util.List<String> names)
{
DirectoryPrx dir = _dirs.getFirst();
for(String name : names)
{
if(name.equals("*"))
{
NodeDesc[] nodes = dir.list();
for(NodeDesc node : nodes)
{
try
{
node.proxy.destroy();
}
catch(PermissionDenied ex)
{
System.out.println("cannot remove `" + node.name + "': " + ex.reason);
}
}
return;
}
else
{
NodeDesc d;
try
{
d = dir.find(name);
}
catch(NoSuchName ex)
{
System.out.println("`" + name + "': no such file or directory");
return;
}
try
{
d.proxy.destroy();
}
catch(PermissionDenied ex)
{
System.out.println("cannot remove `" + name + "': " + ex.reason);
}
}
}
}
void
error(String s)
{
System.err.println("error: " + s);
}
void
warning(String s)
{
System.err.println("warning: " + s);
}
String
getInput()
{
System.out.print("> ");
System.out.flush();
try
{
return _in.readLine();
}
catch(java.io.IOException e)
{
return null;
}
}
int
parse()
{
_in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
Grammar g = new Grammar(this);
g.parse();
return 0;
}
int
parse(java.io.BufferedReader in)
{
_in = in;
Grammar g = new Grammar(this);
g.parse();
return 0;
}
private java.util.LinkedList<DirectoryPrx> _dirs;
private java.io.BufferedReader _in;
}
|