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
|
// **********************************************************************
//
// Copyright (c) 2003-2014 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.
//
// **********************************************************************
using System;
using System.Collections;
using System.IO;
using System.Text;
using Filesystem;
class Parser
{
virtual internal string Input
{
get
{
Console.Write("> ");
Console.Out.Flush();
try
{
return _in.ReadLine();
}
catch(IOException)
{
return null;
}
}
}
internal Parser(DirectoryPrx root)
{
_dirs = new ArrayList();
_dirs.Insert(0, root);
}
internal virtual void usage()
{
Console.Error.Write(
"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");
}
internal virtual void list(bool recursive)
{
try
{
list((DirectoryPrx)_dirs[0], recursive, 0);
}
catch(Ice.LocalException ex)
{
error(ex.ToString());
}
}
internal virtual void list(Filesystem.DirectoryPrx dir, bool 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;
Console.Write(indent + contents[i].name + (d != null ? " (directory)" : " (file)"));
if(d != null && recursive)
{
Console.WriteLine(":");
list(d, true, ++depth);
}
else
{
Console.WriteLine();
}
}
}
internal virtual void createFile(IList names)
{
DirectoryPrx dir = (DirectoryPrx)_dirs[0];
foreach(string name in names)
{
if(name.Equals(".."))
{
Console.WriteLine("Cannot create a file named `..'");
continue;
}
try
{
dir.createFile(name);
}
catch(NameInUse)
{
Console.WriteLine("`" + name + "' exists already");
}
}
}
internal virtual void createDir(IList names)
{
DirectoryPrx dir = (DirectoryPrx)_dirs[0];
foreach(string name in names)
{
if(name.Equals(".."))
{
Console.WriteLine("Cannot create a directory named `..'");
continue;
}
try
{
dir.createDirectory(name);
}
catch(NameInUse)
{
Console.WriteLine("`" + name + "' exists already");
}
}
}
internal virtual void pwd()
{
if(_dirs.Count == 1)
{
Console.Write("/");
}
else
{
for(int i = _dirs.Count - 2; i >= 0; --i)
{
Console.Write("/" + ((DirectoryPrx)_dirs[i]).name());
}
}
Console.WriteLine();
}
internal virtual void cd(string name)
{
if(name.Equals("/"))
{
while(_dirs.Count > 1)
{
_dirs.RemoveAt(0);
}
return;
}
if(name.Equals(".."))
{
if(_dirs.Count > 1)
{
_dirs.RemoveAt(0);
}
return;
}
DirectoryPrx dir = (DirectoryPrx)_dirs[0];
NodeDesc d;
try
{
d = dir.find(name);
}
catch(NoSuchName)
{
Console.WriteLine("`" + name + "': no such directory");
return;
}
if(d.type == NodeType.FileType)
{
Console.WriteLine("`" + name + "': not a directory");
return;
}
_dirs.Insert(0, DirectoryPrxHelper.uncheckedCast(d.proxy));
}
internal virtual void cat(string name)
{
DirectoryPrx dir = (DirectoryPrx)_dirs[0];
NodeDesc d;
try
{
d = dir.find(name);
}
catch(NoSuchName)
{
Console.WriteLine("`" + name + "': no such file");
return;
}
if(d.type == NodeType.DirType)
{
Console.WriteLine("`" + name + "': not a file");
return;
}
FilePrx f = FilePrxHelper.uncheckedCast(d.proxy);
string[] l = f.read();
for(int i = 0; i < l.Length; ++i)
{
Console.WriteLine(l[i]);
}
}
internal virtual void write(IList args)
{
DirectoryPrx dir = (DirectoryPrx)_dirs[0];
string name = (string)args[0];
args.RemoveAt(0);
NodeDesc d;
try
{
d = dir.find(name);
}
catch(NoSuchName)
{
Console.WriteLine("`" + name + "': no such file");
return;
}
if(d.type == NodeType.DirType)
{
Console.WriteLine("`" + name + "': not a file");
return;
}
FilePrx f = FilePrxHelper.uncheckedCast(d.proxy);
string[] l = new string[args.Count];
args.CopyTo(l, 0);
try
{
f.write(l);
}
catch(GenericError ex)
{
Console.WriteLine("`" + name + "': cannot write to file: " + ex.reason);
}
}
internal virtual void destroy(IList names)
{
DirectoryPrx dir = (DirectoryPrx)_dirs[0];
foreach(string name in names)
{
if(name.Equals("*"))
{
NodeDesc[] nodes = dir.list();
for(int j = 0; j < nodes.Length; ++j)
{
try
{
nodes[j].proxy.destroy();
}
catch(PermissionDenied ex)
{
Console.WriteLine("cannot remove `" + nodes[j].name + "': " + ex.reason);
}
}
return;
}
else
{
NodeDesc d;
try
{
d = dir.find(name);
}
catch(NoSuchName)
{
Console.WriteLine("`" + name + "': no such file or directory");
return;
}
try
{
d.proxy.destroy();
}
catch(PermissionDenied ex)
{
Console.WriteLine("cannot remove `" + name + "': " + ex.reason);
}
}
}
}
internal virtual void error(string s)
{
Console.Error.WriteLine("error: " + s);
}
internal virtual void warning(string s)
{
Console.Error.WriteLine("warning: " + s);
}
internal virtual int parse()
{
#if COMPACT
_in = Console.In;
#else
_in = new StreamReader(new StreamReader(Console.OpenStandardInput(), Encoding.Default).BaseStream,
new StreamReader(Console.OpenStandardInput(), Encoding.Default).CurrentEncoding);
#endif
Grammar g = new Grammar(this);
g.parse();
return 0;
}
private ArrayList _dirs;
private TextReader _in;
}
|