ds3.0/bin/
ds3.0/extra/
ds3.0/extra/crat/
ds3.0/extra/creremote/
ds3.0/extra/mingw/
ds3.0/extra/wolfpaw/
ds3.0/fluffos-2.18-ds07/
ds3.0/fluffos-2.18-ds07/Win32/
ds3.0/fluffos-2.18-ds07/compat/
ds3.0/fluffos-2.18-ds07/compat/simuls/
ds3.0/fluffos-2.18-ds07/testsuite/
ds3.0/fluffos-2.18-ds07/testsuite/clone/
ds3.0/fluffos-2.18-ds07/testsuite/command/
ds3.0/fluffos-2.18-ds07/testsuite/data/
ds3.0/fluffos-2.18-ds07/testsuite/etc/
ds3.0/fluffos-2.18-ds07/testsuite/include/
ds3.0/fluffos-2.18-ds07/testsuite/inherit/
ds3.0/fluffos-2.18-ds07/testsuite/inherit/master/
ds3.0/fluffos-2.18-ds07/testsuite/log/
ds3.0/fluffos-2.18-ds07/testsuite/single/
ds3.0/fluffos-2.18-ds07/testsuite/single/tests/compiler/
ds3.0/fluffos-2.18-ds07/testsuite/single/tests/efuns/
ds3.0/fluffos-2.18-ds07/testsuite/single/tests/operators/
ds3.0/fluffos-2.18-ds07/testsuite/u/
ds3.0/fluffos-2.18-ds07/tmp/
ds3.0/lib/cmds/admins/
ds3.0/lib/cmds/common/
ds3.0/lib/cmds/creators/include/
ds3.0/lib/daemon/services/
ds3.0/lib/daemon/tmp/
ds3.0/lib/doc/
ds3.0/lib/doc/bguide/
ds3.0/lib/doc/efun/all/
ds3.0/lib/doc/efun/arrays/
ds3.0/lib/doc/efun/buffers/
ds3.0/lib/doc/efun/compile/
ds3.0/lib/doc/efun/floats/
ds3.0/lib/doc/efun/functions/
ds3.0/lib/doc/efun/mixed/
ds3.0/lib/doc/efun/numbers/
ds3.0/lib/doc/efun/parsing/
ds3.0/lib/doc/help/classes/
ds3.0/lib/doc/help/races/
ds3.0/lib/doc/lfun/
ds3.0/lib/doc/lfun/all/
ds3.0/lib/doc/lfun/lib/abilities/
ds3.0/lib/doc/lfun/lib/armor/
ds3.0/lib/doc/lfun/lib/bank/
ds3.0/lib/doc/lfun/lib/bot/
ds3.0/lib/doc/lfun/lib/clay/
ds3.0/lib/doc/lfun/lib/clean/
ds3.0/lib/doc/lfun/lib/clerk/
ds3.0/lib/doc/lfun/lib/client/
ds3.0/lib/doc/lfun/lib/combat/
ds3.0/lib/doc/lfun/lib/connect/
ds3.0/lib/doc/lfun/lib/container/
ds3.0/lib/doc/lfun/lib/corpse/
ds3.0/lib/doc/lfun/lib/creator/
ds3.0/lib/doc/lfun/lib/daemon/
ds3.0/lib/doc/lfun/lib/damage/
ds3.0/lib/doc/lfun/lib/deterioration/
ds3.0/lib/doc/lfun/lib/donate/
ds3.0/lib/doc/lfun/lib/door/
ds3.0/lib/doc/lfun/lib/equip/
ds3.0/lib/doc/lfun/lib/file/
ds3.0/lib/doc/lfun/lib/fish/
ds3.0/lib/doc/lfun/lib/fishing/
ds3.0/lib/doc/lfun/lib/flashlight/
ds3.0/lib/doc/lfun/lib/follow/
ds3.0/lib/doc/lfun/lib/ftp_client/
ds3.0/lib/doc/lfun/lib/ftp_data_connection/
ds3.0/lib/doc/lfun/lib/fuel/
ds3.0/lib/doc/lfun/lib/furnace/
ds3.0/lib/doc/lfun/lib/genetics/
ds3.0/lib/doc/lfun/lib/holder/
ds3.0/lib/doc/lfun/lib/id/
ds3.0/lib/doc/lfun/lib/interactive/
ds3.0/lib/doc/lfun/lib/lamp/
ds3.0/lib/doc/lfun/lib/leader/
ds3.0/lib/doc/lfun/lib/light/
ds3.0/lib/doc/lfun/lib/limb/
ds3.0/lib/doc/lfun/lib/living/
ds3.0/lib/doc/lfun/lib/load/
ds3.0/lib/doc/lfun/lib/look/
ds3.0/lib/doc/lfun/lib/manipulate/
ds3.0/lib/doc/lfun/lib/meal/
ds3.0/lib/doc/lfun/lib/messages/
ds3.0/lib/doc/lfun/lib/player/
ds3.0/lib/doc/lfun/lib/poison/
ds3.0/lib/doc/lfun/lib/position/
ds3.0/lib/doc/lfun/lib/post_office/
ds3.0/lib/doc/lfun/lib/potion/
ds3.0/lib/doc/lfun/lib/room/
ds3.0/lib/doc/lfun/lib/server/
ds3.0/lib/doc/lfun/lib/spell/
ds3.0/lib/doc/lfun/lib/torch/
ds3.0/lib/doc/lfun/lib/vendor/
ds3.0/lib/doc/lfun/lib/virt_sky/
ds3.0/lib/doc/lfun/lib/weapon/
ds3.0/lib/doc/lfun/lib/worn_storage/
ds3.0/lib/doc/lpc/constructs/
ds3.0/lib/doc/lpc/etc/
ds3.0/lib/doc/lpc/intermediate/
ds3.0/lib/doc/lpc/types/
ds3.0/lib/doc/misc/
ds3.0/lib/doc/old/
ds3.0/lib/doc/phints/
ds3.0/lib/domains/
ds3.0/lib/domains/Praxis/adm/
ds3.0/lib/domains/Praxis/attic/
ds3.0/lib/domains/Praxis/cemetery/mon/
ds3.0/lib/domains/Praxis/data/
ds3.0/lib/domains/Praxis/death/
ds3.0/lib/domains/Praxis/mountains/
ds3.0/lib/domains/Praxis/obj/armour/
ds3.0/lib/domains/Praxis/obj/magic/
ds3.0/lib/domains/Praxis/obj/weapon/
ds3.0/lib/domains/Praxis/orc_valley/
ds3.0/lib/domains/Ylsrim/
ds3.0/lib/domains/Ylsrim/adm/
ds3.0/lib/domains/Ylsrim/armor/
ds3.0/lib/domains/Ylsrim/broken/
ds3.0/lib/domains/Ylsrim/fish/
ds3.0/lib/domains/Ylsrim/meal/
ds3.0/lib/domains/Ylsrim/npc/
ds3.0/lib/domains/Ylsrim/obj/
ds3.0/lib/domains/Ylsrim/virtual/
ds3.0/lib/domains/Ylsrim/weapon/
ds3.0/lib/domains/campus/adm/
ds3.0/lib/domains/campus/chamber/
ds3.0/lib/domains/campus/etc/
ds3.0/lib/domains/campus/meals/
ds3.0/lib/domains/campus/txt/ai/charles/
ds3.0/lib/domains/campus/txt/ai/charles/bak2/
ds3.0/lib/domains/campus/txt/ai/charles/bak2/bak1/
ds3.0/lib/domains/campus/txt/ai/charly/
ds3.0/lib/domains/campus/txt/ai/charly/bak/
ds3.0/lib/domains/campus/txt/jenny/
ds3.0/lib/domains/cave/doors/
ds3.0/lib/domains/cave/etc/
ds3.0/lib/domains/cave/meals/
ds3.0/lib/domains/cave/weap/
ds3.0/lib/domains/default/chamber/
ds3.0/lib/domains/default/creator/
ds3.0/lib/domains/default/doors/
ds3.0/lib/domains/default/etc/
ds3.0/lib/domains/default/vehicle/
ds3.0/lib/domains/default/virtual/
ds3.0/lib/domains/town/save/
ds3.0/lib/domains/town/txt/shame/
ds3.0/lib/domains/town/virtual/
ds3.0/lib/domains/town/virtual/bottom/
ds3.0/lib/domains/town/virtual/space/
ds3.0/lib/estates/
ds3.0/lib/ftp/
ds3.0/lib/lib/comp/
ds3.0/lib/lib/daemons/
ds3.0/lib/lib/daemons/include/
ds3.0/lib/lib/lvs/
ds3.0/lib/lib/user/
ds3.0/lib/lib/virtual/
ds3.0/lib/log/
ds3.0/lib/log/adm/
ds3.0/lib/log/archive/
ds3.0/lib/log/chan/
ds3.0/lib/log/errors/
ds3.0/lib/log/law/adm/
ds3.0/lib/log/law/email/
ds3.0/lib/log/law/names/
ds3.0/lib/log/law/sites-misc/
ds3.0/lib/log/law/sites-register/
ds3.0/lib/log/law/sites-tempban/
ds3.0/lib/log/law/sites-watch/
ds3.0/lib/log/open/
ds3.0/lib/log/reports/
ds3.0/lib/log/router/
ds3.0/lib/log/secure/
ds3.0/lib/log/watch/
ds3.0/lib/obj/book_source/
ds3.0/lib/obj/include/
ds3.0/lib/powers/prayers/
ds3.0/lib/powers/spells/
ds3.0/lib/realms/template/
ds3.0/lib/realms/template/adm/
ds3.0/lib/realms/template/area/
ds3.0/lib/realms/template/area/armor/
ds3.0/lib/realms/template/area/npc/
ds3.0/lib/realms/template/area/obj/
ds3.0/lib/realms/template/area/room/
ds3.0/lib/realms/template/area/weap/
ds3.0/lib/realms/template/bak/
ds3.0/lib/realms/template/cmds/
ds3.0/lib/save/kills/o/
ds3.0/lib/secure/cfg/classes/
ds3.0/lib/secure/cmds/builders/
ds3.0/lib/secure/cmds/creators/include/
ds3.0/lib/secure/cmds/players/
ds3.0/lib/secure/cmds/players/include/
ds3.0/lib/secure/daemon/imc2server/
ds3.0/lib/secure/daemon/include/
ds3.0/lib/secure/lib/
ds3.0/lib/secure/lib/include/
ds3.0/lib/secure/lib/net/include/
ds3.0/lib/secure/lib/std/
ds3.0/lib/secure/log/adm/
ds3.0/lib/secure/log/bak/
ds3.0/lib/secure/log/intermud/
ds3.0/lib/secure/log/network/
ds3.0/lib/secure/modules/
ds3.0/lib/secure/npc/
ds3.0/lib/secure/obj/include/
ds3.0/lib/secure/room/
ds3.0/lib/secure/save/
ds3.0/lib/secure/save/backup/
ds3.0/lib/secure/save/boards/
ds3.0/lib/secure/save/players/g/
ds3.0/lib/secure/tmp/
ds3.0/lib/secure/upgrades/files/
ds3.0/lib/secure/verbs/creators/
ds3.0/lib/std/board/
ds3.0/lib/std/lib/
ds3.0/lib/verbs/admins/include/
ds3.0/lib/verbs/builders/
ds3.0/lib/verbs/common/
ds3.0/lib/verbs/common/include/
ds3.0/lib/verbs/creators/
ds3.0/lib/verbs/creators/include/
ds3.0/lib/verbs/rooms/
ds3.0/lib/verbs/rooms/include/
ds3.0/lib/www/client/
ds3.0/lib/www/errors/
ds3.0/lib/www/images/
ds3.0/win32/
/* Character Input/Output handler.
 * Character mode is meant to allow the user to issue special
 * requests to the mud using escape sequences. However, it is
 * not always as simple as "escape character X". 
 *  
 * This inherited object manages a get_char() loop that accumulates
 * input into CharStuff["charbuffer"], and watches for sets of
 * substrings that match known escape sequences, such as "up arrow"
 * and the like, which are not just one character. 
 * 
 * When an ESC is received (it is assumed that the driver translates
 * 0x1b to 0x1e with the ANSI_SUBSTITUTE define, making '30' the
 * escape char) then the subsequent input is not put into 
 * CharStuff["charbuffer"] but rather into CharStuff["escape"]
 * until a recognized escape sequence is completed in
 * CharStuff["escape"] or until some arbitrary number of characters
 * being input indicates that an unknown escape sequence has
 * been received. At that point the escape sequence is either
 * processed or discarded, and accumulation into CharStuff["charbuffer"]
 * resumes. 
 * -Crat 08FEB2009
 *
 *  Wow what's that even mean?
 * -Crat 22SEP2009
 */

#include <daemons.h>
#include <lib.h>
#include "include/nmsh.h"

#define CHAR_DEBUG 0

private static mapping CharStuff;

int rEsc();

void create(){
    CharStuff = ([
            "charmode" : 1, /* are we in character mode */
            "noecho" : 1, /* do we echo input characters */
            "charbuffer" : "", /* what we've typed of this line so far */
            "tempbuffer" : "", /* the last line, before alias expansion */
            "charshell" : 0, /* hell if i remember */
            ]);
}

static int ReceiveChars(string str){
    string s;
    int c;

    if(!this_object()) return 0;

    if(str){
        c = str[0];
        s = str[0..0];
    }

    if(!CharStuff["charbuffer"]) CharStuff["charbuffer"] = "";

#if CHAR_DEBUG
    if(sizeof(str) > 1) debug("unexpected string: "+str);
    else debug("CHARIO received: "+s+", aka: "+c, "black");
#endif

    if(!sizeof(CharStuff["escape"]) && (c > 31 && c < 127)){ 
        /* Printable character, and we're not waiting on an ESC seq,
         * Let's add it to the character buffer, and then
         * "normalize" any Pinkfish sequences.
         */ 
        CharStuff["charbuffer"] += s;
        if(sizeof(CharStuff["charbuffer"]) > 1 && last(CharStuff["charbuffer"],2) == "%^"){
            /* replace pinkfish to avoid weirdness */
            CharStuff["charbuffer"] = truncate(CharStuff["charbuffer"],2);
            CharStuff["charbuffer"] += "%%^^";
        }
        this_object()->rAscii(s);
        get_char("ReceiveChars", CharStuff["noecho"]);
        return 1; 
    }

    if(sizeof(CharStuff["escape"]) && 
            member_array(30, CharStuff["escape"]) != -1){
        int cl;
        string esc;
        CharStuff["escape"] += s;
        esc = CharStuff["escape"];
        if(last(esc,1) == "H" || last(esc,1) == "R" || sizeof(esc) > 10){
            cl = 1;
            this_object()->rAnsi(esc);
        }
        else if(sizeof(esc) > 2 && esc[1] == 91){
            switch(esc[2]){
                case 65 : this_object()->rArrow("up"); cl = 1; break;
                case 66 : this_object()->rArrow("down"); cl = 1; break;
                case 68 : this_object()->rArrow("left"); cl = 1; break;
                case 67 : this_object()->rArrow("right"); cl = 1; break;
                case 51 : 
                          if(sizeof(esc) > 3 && esc[3] == 126){
                              this_object()->rDel();
                              cl = 1;
                          }
                          break;
            }
        }
        if(cl) CharStuff["escape"] = "";
        get_char("ReceiveChars", CharStuff["noecho"]);
        return 1;
    }

    switch(c){
        case 0 : case  8 : case 127 : this_object()->rBackspace(); break;
        case 1 : this_object()->rCtrl("a"); break;
        case 2 : this_object()->rCtrl("b"); break;
        case 3 : this_object()->rCtrl("c"); break;
        case 4 : this_object()->rCtrl("d"); break;
        case 5 : this_object()->rCtrl("e"); break;
        case 6 : this_object()->rCtrl("f"); break;
        case 7 : this_object()->rCtrl("g"); break;
        case 9 : this_object()->rCtrl("i"); break; /* aka Tab */
        case 10 : case 13: this_object()->rEnter(); break;
        case 11 : this_object()->rCtrl("k"); break;
        case 12 : this_object()->rCtrl("l"); break;
        case 14 : this_object()->rCtrl("n"); break;
        case 15 : this_object()->rCtrl("o"); break;
        case 16 : this_object()->rCtrl("p"); break;
        case 17 : this_object()->rCtrl("q"); break;
        case 18 : this_object()->rCtrl("r"); break;
        case 19 : this_object()->rCtrl("s"); break;
        case 20 : this_object()->rCtrl("t"); break;
        case 21 : this_object()->rCtrl("u"); break;
        case 22 : this_object()->rCtrl("v"); break;
        case 23 : this_object()->rCtrl("w"); break;
        case 24 : this_object()->rCtrl("x"); break;
        case 25 : this_object()->rCtrl("y"); break;
        case 26 : this_object()->rCtrl("z"); break;
        case 28 : this_object()->rCtrl("28"); break;
        case 30 : rEsc(); break;
        case 31 : this_object()->rCtrl("31"); break;
    }

    if(!this_object()){
        // Probably a warmboot or userload
        return 0;
    }
    if(in_edit(this_object())) CharStuff["noecho"] = 0;
    else CharStuff["noecho"] = 1;

#ifdef __GET_CHAR_IS_BUFFERED__
    if(CharStuff["charmode"]) get_char("ReceiveChars", CharStuff["noecho"]);
#endif
    this_object()->RedrawPrompt();
    return CharStuff["charmode"];
}

varargs int CancelCharmode(int extra){
    int ret = 1;
#if CHAR_DEBUG
    debug("cancelling! stack: "+get_stack());
#endif
    if(!CharStuff) CharStuff = ([]);
    flush_messages();
    CharStuff["charbuffer"] = "";
    if(!CharStuff["charmode"] && !query_charmode(this_object())) return 0;
    CharStuff["charmode"] = 0;
#ifdef __DSLIB__
    remove_get_char(this_object());
    remove_charmode(this_object());
#else
    ReceiveChars(sprintf("%c",13));
#endif
    if(extra) ReceiveChars(sprintf("%c",13));
    this_object()->SetProperty("was_charmode", 0);
    return ret;
}

int SetCharmode(int x){
    if(!x) CharStuff["charmode"] = 0;
#ifdef __GET_CHAR_IS_BUFFERED__
    else CharStuff["charmode"] = 1;
    remove_get_char(this_object());
    if(!(this_object()->GetCedmode())){
        get_char("ReceiveChars", CharStuff["noecho"]);
    }
    else get_char("ReceiveChar", CharStuff["noecho"]);
#endif
    return CharStuff["charmode"];
}

int GetCharmode(){
    if(!CharStuff) CharStuff = ([]);
    return CharStuff["charmode"];
}

static string GetCharbuffer(){
    if(!CharStuff) CharStuff = ([]);
    return (CharStuff["charbuffer"] || "" );
}

static string SetCharbuffer(string str){
    if(!CharStuff) CharStuff = ([]);
    return CharStuff["charbuffer"] = ( str || "" );
}

static string GetTempbuffer(){
    if(!CharStuff) CharStuff = ([]);
    return (CharStuff["tempbuffer"] || "" );
}

static string SetTempbuffer(string str){
    if(!CharStuff) CharStuff = ([]);
    return CharStuff["tempbuffer"] = ( str || "" );
}

void CheckCharmode(){
    if(!in_edit() && !in_input() && this_object()->GetProperty("was_charmode")){
        CharStuff["charmode"] = 1;
    }
    if(CharStuff["charmode"] && !query_charmode(this_object())){
        if(!in_edit() && !in_input()) SetCharmode(CharStuff["charmode"]);
        if(!this_player() || this_player() != this_object()){
            SetCharmode(1);
            this_object()->RedrawPrompt();
        }
    }
    if(!CharStuff["charmode"] && query_charmode(this_object()) > 0){
        CancelCharmode();
    }
}

void heart_beat(){
    CheckCharmode();
}

int rEsc(){
    CharStuff["escape"] = sprintf("%c", 30);
    if(in_edit(this_object())) CharStuff["noecho"] = 0;
    else CharStuff["noecho"] = 1;
    return 1;
}

int SetNoEcho(int x){
    if(x) CharStuff["noecho"] = 1;
    else CharStuff["noecho"] = 0;
    return CharStuff["noecho"];
}

int GetNoEcho(){
    return CharStuff["noecho"];
}