#include <driver_hook.h>
mapping fixed_commands;
string cwd;
void write(string s) {
this_object()->catch_tell(s);
}
void parse_command(string s) {
string fixed_command;
int end;
write("parsing your command:");
write(s);
write("\n");
fixed_command = s;
if ((end = member(s, ' ')) >= 0) {
fixed_command = s[0..end-1];
}
fixed_command = fixed_commands[fixed_command];
if (fixed_command)
call_other(this_object(), fixed_command, end >= 0 ? s[end+1..] : "", s);
else
call_other(this_object(), s);
}
void logon() {
catch_tell("Hello World\n");
set_interactive_hook(IH_INPUT, "parse_command");
fixed_commands = ([
"ls":"list_files",
"cd":"change_dir",
]);
cwd = "/";
}
void catch_tell(string s) {
text_message(s);
}
void s() {
write("south\n");
}
void add() {
string s;
s = "add";
s[0]++;
write(s);
}
void n() {
write(sprintf("%d", 42));
}
string make_path_absolute(string path) {
int back, scan;
if (! strlen(path) || path[0] != '/')
path = cwd + path;
while ((back = strstr(path, "/../")) >= 0) {
for (scan = back; scan && path[--scan] != '/'; );
path = path[0..scan] + path[back+4..];
}
if (path[<3..] == "/..") {
for (scan = strlen(path) - 3; scan && path[--scan] != '/'; );
path = path[0..scan];
}
return path;
}
void list_files(string path) {
int max, i, len, tmp;
status trunc_flag;
mixed *dir;
path = make_path_absolute(path);
dir = get_dir (path,3, 0);
if (!dir) {
#if 1
write(sprintf("%s: No such directory.\n", path));
#else
write(path);
write(": No such directory.\n");
#endif
return;
}
if (sizeof(dir) > 999)
{
dir = dir[0..998];
trunc_flag = 1;
}
for(i = sizeof(dir); i--; ) {
if(dir[i--] == -2)
dir[i]+="/";
len = strlen(dir[i]);
if (len > max)
max = len;
}
++max;
if (max > 79)
max = 79;
for (i=0; i < sizeof(dir); i+=2) {
string name;
name = dir[i];
tmp = strlen(name);
if (len + tmp > 79) {
len = 0;
write("\n");
}
write(name);
if (len + max > 79) {
write("\n");
len = 0;
} else {
write(
" "
[<max-tmp..]);
len += max;
}
}
write("\n");
if (trunc_flag) write("***TRUNCATED***\n");
}
void change_dir(string path) {
path = make_path_absolute(path);
if (strlen(path) > 1)
path += "/";
cwd = path;
}