ds2.10/bin/
ds2.10/extra/
ds2.10/extra/crat/
ds2.10/extra/creremote/
ds2.10/extra/mingw/
ds2.10/extra/wolfpaw/
ds2.10/fluffos-2.16-ds05/
ds2.10/fluffos-2.16-ds05/Win32/
ds2.10/fluffos-2.16-ds05/compat/
ds2.10/fluffos-2.16-ds05/compat/simuls/
ds2.10/fluffos-2.16-ds05/include/
ds2.10/fluffos-2.16-ds05/testsuite/
ds2.10/fluffos-2.16-ds05/testsuite/clone/
ds2.10/fluffos-2.16-ds05/testsuite/command/
ds2.10/fluffos-2.16-ds05/testsuite/data/
ds2.10/fluffos-2.16-ds05/testsuite/etc/
ds2.10/fluffos-2.16-ds05/testsuite/include/
ds2.10/fluffos-2.16-ds05/testsuite/inherit/
ds2.10/fluffos-2.16-ds05/testsuite/inherit/master/
ds2.10/fluffos-2.16-ds05/testsuite/log/
ds2.10/fluffos-2.16-ds05/testsuite/single/
ds2.10/fluffos-2.16-ds05/testsuite/single/tests/compiler/
ds2.10/fluffos-2.16-ds05/testsuite/single/tests/efuns/
ds2.10/fluffos-2.16-ds05/testsuite/single/tests/operators/
ds2.10/fluffos-2.16-ds05/testsuite/u/
ds2.10/lib/cmds/admins/
ds2.10/lib/cmds/common/
ds2.10/lib/cmds/creators/include/
ds2.10/lib/daemon/services/
ds2.10/lib/daemon/tmp/
ds2.10/lib/doc/
ds2.10/lib/doc/bguide/
ds2.10/lib/doc/efun/all/
ds2.10/lib/doc/efun/arrays/
ds2.10/lib/doc/efun/buffers/
ds2.10/lib/doc/efun/compile/
ds2.10/lib/doc/efun/floats/
ds2.10/lib/doc/efun/functions/
ds2.10/lib/doc/efun/general/
ds2.10/lib/doc/efun/mixed/
ds2.10/lib/doc/efun/numbers/
ds2.10/lib/doc/efun/parsing/
ds2.10/lib/doc/help/classes/
ds2.10/lib/doc/help/races/
ds2.10/lib/doc/lfun/
ds2.10/lib/doc/lfun/all/
ds2.10/lib/doc/lfun/lib/abilities/
ds2.10/lib/doc/lfun/lib/armor/
ds2.10/lib/doc/lfun/lib/bank/
ds2.10/lib/doc/lfun/lib/bot/
ds2.10/lib/doc/lfun/lib/clay/
ds2.10/lib/doc/lfun/lib/clean/
ds2.10/lib/doc/lfun/lib/clerk/
ds2.10/lib/doc/lfun/lib/client/
ds2.10/lib/doc/lfun/lib/combat/
ds2.10/lib/doc/lfun/lib/connect/
ds2.10/lib/doc/lfun/lib/container/
ds2.10/lib/doc/lfun/lib/corpse/
ds2.10/lib/doc/lfun/lib/creator/
ds2.10/lib/doc/lfun/lib/daemon/
ds2.10/lib/doc/lfun/lib/damage/
ds2.10/lib/doc/lfun/lib/deterioration/
ds2.10/lib/doc/lfun/lib/donate/
ds2.10/lib/doc/lfun/lib/door/
ds2.10/lib/doc/lfun/lib/equip/
ds2.10/lib/doc/lfun/lib/file/
ds2.10/lib/doc/lfun/lib/fish/
ds2.10/lib/doc/lfun/lib/fishing/
ds2.10/lib/doc/lfun/lib/flashlight/
ds2.10/lib/doc/lfun/lib/follow/
ds2.10/lib/doc/lfun/lib/ftp_client/
ds2.10/lib/doc/lfun/lib/ftp_data_connection/
ds2.10/lib/doc/lfun/lib/fuel/
ds2.10/lib/doc/lfun/lib/furnace/
ds2.10/lib/doc/lfun/lib/genetics/
ds2.10/lib/doc/lfun/lib/holder/
ds2.10/lib/doc/lfun/lib/id/
ds2.10/lib/doc/lfun/lib/interactive/
ds2.10/lib/doc/lfun/lib/lamp/
ds2.10/lib/doc/lfun/lib/leader/
ds2.10/lib/doc/lfun/lib/light/
ds2.10/lib/doc/lfun/lib/limb/
ds2.10/lib/doc/lfun/lib/living/
ds2.10/lib/doc/lfun/lib/load/
ds2.10/lib/doc/lfun/lib/look/
ds2.10/lib/doc/lfun/lib/manipulate/
ds2.10/lib/doc/lfun/lib/meal/
ds2.10/lib/doc/lfun/lib/messages/
ds2.10/lib/doc/lfun/lib/player/
ds2.10/lib/doc/lfun/lib/poison/
ds2.10/lib/doc/lfun/lib/position/
ds2.10/lib/doc/lfun/lib/post_office/
ds2.10/lib/doc/lfun/lib/potion/
ds2.10/lib/doc/lfun/lib/room/
ds2.10/lib/doc/lfun/lib/server/
ds2.10/lib/doc/lfun/lib/spell/
ds2.10/lib/doc/lfun/lib/torch/
ds2.10/lib/doc/lfun/lib/vendor/
ds2.10/lib/doc/lfun/lib/virt_sky/
ds2.10/lib/doc/lfun/lib/weapon/
ds2.10/lib/doc/lfun/lib/worn_storage/
ds2.10/lib/doc/lpc/constructs/
ds2.10/lib/doc/lpc/etc/
ds2.10/lib/doc/lpc/intermediate/
ds2.10/lib/doc/lpc/types/
ds2.10/lib/doc/misc/
ds2.10/lib/doc/old/
ds2.10/lib/doc/phints/
ds2.10/lib/domains/
ds2.10/lib/domains/Praxis/adm/
ds2.10/lib/domains/Praxis/attic/
ds2.10/lib/domains/Praxis/cemetery/mon/
ds2.10/lib/domains/Praxis/data/
ds2.10/lib/domains/Praxis/death/
ds2.10/lib/domains/Praxis/mountains/
ds2.10/lib/domains/Praxis/obj/armour/
ds2.10/lib/domains/Praxis/obj/magic/
ds2.10/lib/domains/Praxis/obj/weapon/
ds2.10/lib/domains/Praxis/orc_valley/
ds2.10/lib/domains/Ylsrim/
ds2.10/lib/domains/Ylsrim/adm/
ds2.10/lib/domains/Ylsrim/armor/
ds2.10/lib/domains/Ylsrim/broken/
ds2.10/lib/domains/Ylsrim/fish/
ds2.10/lib/domains/Ylsrim/meal/
ds2.10/lib/domains/Ylsrim/npc/
ds2.10/lib/domains/Ylsrim/obj/
ds2.10/lib/domains/Ylsrim/virtual/
ds2.10/lib/domains/Ylsrim/weapon/
ds2.10/lib/domains/alpha/room/
ds2.10/lib/domains/alpha/virtual/
ds2.10/lib/domains/campus/adm/
ds2.10/lib/domains/campus/etc/
ds2.10/lib/domains/campus/meals/
ds2.10/lib/domains/campus/txt/ai/charles/
ds2.10/lib/domains/campus/txt/ai/charles/bak2/
ds2.10/lib/domains/campus/txt/ai/charles/bak2/bak1/
ds2.10/lib/domains/campus/txt/ai/charly/
ds2.10/lib/domains/campus/txt/ai/charly/bak/
ds2.10/lib/domains/campus/txt/jenny/
ds2.10/lib/domains/cave/doors/
ds2.10/lib/domains/cave/etc/
ds2.10/lib/domains/cave/meals/
ds2.10/lib/domains/cave/weap/
ds2.10/lib/domains/default/chamber/
ds2.10/lib/domains/default/creator/
ds2.10/lib/domains/default/doors/
ds2.10/lib/domains/default/etc/
ds2.10/lib/domains/default/vehicle/
ds2.10/lib/domains/default/virtual/
ds2.10/lib/domains/town/save/
ds2.10/lib/domains/town/txt/shame/
ds2.10/lib/domains/town/virtual/
ds2.10/lib/domains/town/virtual/bottom/
ds2.10/lib/domains/town/virtual/space/
ds2.10/lib/estates/
ds2.10/lib/ftp/
ds2.10/lib/lib/comp/
ds2.10/lib/lib/daemons/
ds2.10/lib/lib/daemons/include/
ds2.10/lib/lib/lvs/
ds2.10/lib/lib/user/
ds2.10/lib/lib/virtual/
ds2.10/lib/log/
ds2.10/lib/log/adm/
ds2.10/lib/log/archive/
ds2.10/lib/log/chan/
ds2.10/lib/log/errors/
ds2.10/lib/log/law/adm/
ds2.10/lib/log/law/email/
ds2.10/lib/log/law/names/
ds2.10/lib/log/law/sites-misc/
ds2.10/lib/log/law/sites-register/
ds2.10/lib/log/law/sites-tempban/
ds2.10/lib/log/law/sites-watch/
ds2.10/lib/log/open/
ds2.10/lib/log/reports/
ds2.10/lib/log/router/
ds2.10/lib/log/secure/
ds2.10/lib/log/watch/
ds2.10/lib/obj/book_source/
ds2.10/lib/obj/include/
ds2.10/lib/powers/prayers/
ds2.10/lib/powers/spells/
ds2.10/lib/realms/template/
ds2.10/lib/realms/template/adm/
ds2.10/lib/realms/template/area/
ds2.10/lib/realms/template/area/armor/
ds2.10/lib/realms/template/area/npc/
ds2.10/lib/realms/template/area/obj/
ds2.10/lib/realms/template/area/room/
ds2.10/lib/realms/template/area/weap/
ds2.10/lib/realms/template/bak/
ds2.10/lib/realms/template/cmds/
ds2.10/lib/save/kills/o/
ds2.10/lib/secure/cfg/classes/
ds2.10/lib/secure/cmds/builders/
ds2.10/lib/secure/cmds/creators/include/
ds2.10/lib/secure/cmds/players/include/
ds2.10/lib/secure/daemon/imc2server/
ds2.10/lib/secure/daemon/include/
ds2.10/lib/secure/lib/
ds2.10/lib/secure/lib/include/
ds2.10/lib/secure/lib/net/include/
ds2.10/lib/secure/lib/std/
ds2.10/lib/secure/log/adm/
ds2.10/lib/secure/log/bak/
ds2.10/lib/secure/log/intermud/
ds2.10/lib/secure/log/network/
ds2.10/lib/secure/modules/
ds2.10/lib/secure/npc/
ds2.10/lib/secure/obj/include/
ds2.10/lib/secure/room/
ds2.10/lib/secure/save/
ds2.10/lib/secure/save/backup/
ds2.10/lib/secure/save/boards/
ds2.10/lib/secure/save/players/g/
ds2.10/lib/secure/tmp/
ds2.10/lib/secure/upgrades/files/
ds2.10/lib/secure/verbs/creators/
ds2.10/lib/std/board/
ds2.10/lib/std/lib/
ds2.10/lib/verbs/admins/include/
ds2.10/lib/verbs/builders/
ds2.10/lib/verbs/common/
ds2.10/lib/verbs/common/include/
ds2.10/lib/verbs/creators/
ds2.10/lib/verbs/creators/include/
ds2.10/lib/verbs/rooms/
ds2.10/lib/verbs/rooms/include/
ds2.10/lib/www/client/
ds2.10/lib/www/errors/
ds2.10/lib/www/images/
ds2.10/win32/
/*
 *  console.c -- implements a command-line for the driver.
 *               Separate file because this is likely to grow with time.
 *               Note that the intent is NOT to have a full scripting language,
 *               since that's, er, what LPC is.  This is to implement driver
 *               inspection that is difficult or impossible from inside the
 *               mud.  Because of this, explicit allocation of things is
 *               probably a bad idea: use automatic stack variables as much
 *               as is possible.
 *               Isaac Charles (Hamlet@Discworld, etc) -- Jul 2008
 */

#include "std.h"

#ifdef HAS_CONSOLE
#include "comm.h"
#include "object.h"
#include "dumpstat.h"

#define NAME_LEN 50

typedef struct {
    char name[NAME_LEN + 1];
    int cpy;
    int ref;
    long mem;
    long idle;
} ITEMS;


static int strcmpalpha(const char *aname, const char *bname, int depth);
static int objcmpalpha(const void *, const void *);
static int itemcmpsize(const void *, const void *);
static int objcmpsize(const void *, const void *);
static int objcmpidle(const void *, const void *);

static INLINE void print_obj(int refs, int cpy, const char *obname, long lastref,
        long sz) {
    printf("%4d %4d %-*s %5ld %10ld\n", refs, cpy, NAME_LEN,
            obname, ((long)time(0) - lastref), sz);
}

void console_command(char *s) {
    char verb[11];
    char args[3][160];
    int numargs, num[2];

    if(strlen(s) > 0) {
        if(strncmp(s, "quit", strlen(s)) == 0) {
            (void) puts("Console closing.  The mud remains.");
            //(void) fclose(stdin);
            has_console = 0;
            restore_sigttin();
        }
        else if(strncmp(s, "shutdown", ((strlen(s) > 4) ? strlen(s) : 4)) == 0) {
            (void) puts("Mud ending now.");
            shutdownMudOS(0);
            return;
        }
        else if(strncmp(s, "help", strlen(s)) == 0) {
            (void) puts(
                    "Console Help\n\
                    quit       Close the console (the MUD remains)\n\
                    shutdown   End the mud (rapidly)\n\
                    help       This.\n\
                    objects    Show a listing of all active objects.  Format:\n\
                    objects [qualifier] [val|range] [prefix=PREFIX]\n\
                    qualifiers are:\n\
                    alpha    sort objects alphabetically\n\
                    size     sort objects by (reverse) size\n\
                    idle     sort objects by (reverse) idle time\n\
                    totals   sum together usage of all clones of an obj and\n\
                    display in reverse order of size\n\
                    depth:<D>    sum together subdirectories past depth D.\n\
                    val is a number, specifying number of objects to show,\n\
                    from the beginning (positive) or from the end (negative).\n\
                    range is st..fin where st and fin can be number of objects\n\
                    from the beginning (positive) or from the end (negative.)\n\
                    if prefix=PREFIX is present, only items beginning with PREFIX\n\
                    will be displayed.  Note that ranges are applied and /then/ \n\
                    the prefixes are checked.  Specifying both may result in \n\
                    empty output\
                    ");
        }
        else { // commands that can be followed by args
            int DEPTH = 1000; /* allow summarization to dir-level */
            char *prefix = (char *) 0;
            int plen = 0;

            num[0] = 0;
            num[1] = INT_MAX;

            if((numargs = sscanf(s, "%10s %159s %159s %159s", verb, args[0], 
                            args[1], args[2]) - 1) 
                    >= 1) {
                int tmp;
                char *pos;

                if(numargs && (strncmp("prefix", args[numargs-1], 6) == 0) &&
                        (strlen(args[numargs-1]) > 6)) {
                    prefix = args[numargs-1] + 7;
                    plen = strlen(prefix);
                    numargs--;
                }

                if(numargs) {
                    pos = args[numargs-1];
                    while((pos = strchr(pos, '<')) != NULL)
                        *pos++ = '-';

                    if((tmp = sscanf(args[numargs-1], "%d..%d", &num[0], &num[1])) == 2){
                    }
                    else if(tmp == 1) {
                        if(args[numargs-1][strlen(args[numargs-1])-1] != '.') {
                            if(num[0] == 0) {
                                num[0] = num[1] = 0;
                            }
                            else if(num[0] > 0) {
                                num[1] = num[0] - 1;
                                num[0] = 0;
                            }
                        }
                    }

                    if(tmp)
                        numargs--;
                }
            }

            if(strncmp(verb, "objects", strlen(verb)) == 0) {
                object_t **list;
                int count;
                char *pos2;
                int i;
                long totsz = 0, sz;
                char TOTALS = (char) 0;

                /* allow 'objects depth:3 10' */
                if(numargs && ((pos2 = strpbrk(args[0], ":=")) != NULL)) {
                    sscanf(pos2+1, "%d", &DEPTH);
                    if(DEPTH < 1)
                        DEPTH = 1;
                    *pos2 = '\0';
                }

                // get_objects() allocates a string and pushes it onto the stack.
                // probably not a problem here as long as we remember to pop it.
                get_objects(&list, &count, 0, 0);

                if(numargs) {
                    if(strncmp("alpha", args[0], strlen(args[0])) == 0)
                        qsort(list, (size_t) count, sizeof(object_t *), objcmpalpha);
                    else if(strncmp("size", args[0], strlen(args[0])) == 0)
                        qsort(list, (size_t) count, sizeof(object_t *), objcmpsize);
                    else if(strncmp("idle", args[0], strlen(args[0])) == 0)
                        qsort(list, (size_t) count, sizeof(object_t *), objcmpidle);
                    else { /* This should be "totals" or "depth" */
                        TOTALS = (char) 1;
                        qsort(list, (size_t) count, sizeof(object_t *), objcmpalpha);
                    }
                }

                printf("%4s %4s %-*s %5s %10s\n", "Rf", "Cpy", NAME_LEN, "Obj",
                        "Idle", "Size");

                if(TOTALS && (count > 1)) {
                    ITEMS it[count+1];
                    int lst = 0;
                    char *pound;
                    //char tmpnam[NAME_LEN + 1];

                    memset(it, 0, (count+1) * sizeof(ITEMS));

                    for(i=0; i < count; i++) {
                        if(strcmpalpha(it[lst].name, list[i]->obname, DEPTH)
                                != 0) {
                            int sl;
                            const char *slash = list[i]->obname;
                            lst++;

                            for(sl = 0; sl < DEPTH; sl++)
                                if((slash = strchr(slash, '/')) == NULL) {
                                    slash = list[i]->obname + strlen(list[i]->obname);
                                    break;
                                }
                                else
                                    slash++;

                            strncpy(it[lst].name, list[i]->obname +
                                    (((slash - list[i]->obname) > NAME_LEN) ?
                                     ((slash - list[i]->obname) - NAME_LEN) 
                                     : 0)
                                    , NAME_LEN);
                            it[lst].name[slash - list[i]->obname] = '\0';

                            pound = strchr(it[lst].name, '#');
                            if(pound != NULL)
                                *pound = '\0';
                        }

                        it[lst].cpy++;
                        it[lst].ref += list[i]->ref;
                        it[lst].mem += list[i]->prog->total_size + data_size(list[i]) +
                            sizeof(object_t);
                        if(((long)list[i]->time_of_ref < it[lst].idle) || !it[lst].idle)
                            it[lst].idle = (long) list[i]->time_of_ref;
                    }

                    qsort(it+1, (size_t) lst, sizeof(ITEMS), itemcmpsize);

                    if(num[0] >= lst)
                        num[0] = lst - 1;
                    else if(num[0] < 0) {
                        num[0] = lst + num[0];
                        if(num[0] < 0)
                            num[0] = 0;
                    }
                    num[0]++;

                    if(num[1] >= lst)
                        num[1] = lst - 1;
                    else if(num[1] < 0) {
                        num[1] = lst + num[1];
                        if(num[1] < 0)
                            num[1] = 0;
                    }
                    num[1]++;

                    for(i=num[0]; i<=num[1]; i++) {
                        if(!prefix || (strncmp(it[i].name, prefix, plen) == 0)) {
                            print_obj(it[i].ref, it[i].cpy, it[i].name, it[i].idle,
                                    it[i].mem);
                            totsz += it[i].mem;
                        }
                    }
                }
                else { // no totals needed
                    if(num[0] >= count)
                        num[0] = count - 1;
                    else if(num[0] < 0) {
                        num[0] = count + num[0];
                        if(num[0] < 0)
                            num[0] = 0;
                    }
                    if(num[1] >= count)
                        num[1] = count - 1;
                    else if(num[1] < 0) {
                        num[1] = count + num[1];
                        if(num[1] < 0)
                            num[1] = 0;
                    }

                    for(i = num[0]; i <= num[1]; i++) {
                        if(!prefix || (strncmp(list[i]->obname, prefix, plen) == 0)) {
                            print_obj((int)list[i]->ref, 1, list[i]->obname,
                                    (long) list[i]->time_of_ref,
                                    sz = (long) list[i]->prog->total_size +
                                    (long)data_size(list[i]) +
                                    (long)sizeof(object_t));
                            totsz += sz;
                        }
                    }
                }

                printf("%4s %4s %-*s %5s %10s\n", "", "", NAME_LEN, "-----", "-----",
                        "----------");
                printf("%4s %4s %*d %-5s %10ld\n", "", "", NAME_LEN, 1+num[1]-num[0],
                        "objs", totsz);
                pop_stack();
            }
            else {
                (void) puts("Unknown command.");
            }
        }
    }

    if(has_console > 0) {
        (void) putchar('?'); (void) putchar(' ');
        (void) fflush(stdout);
    }
}

static int strcmpalpha(const char *aname, const char *bname, int depth) {
    int slash = 0;

    for(;(*aname == *bname) && (*aname != '#') && (*bname != '#') &&
            (*aname != '\0') && (*bname != '\0'); aname++, bname++) {
        if(*aname == '/')
            slash++;

        if(slash == depth)
            break;
    }

    if((*aname == '#') || ((slash == depth) && (*aname == '/'))) {
        if((*bname != '#') && (*bname != '\0') && 
                ((slash != depth) || (*bname != '/')))
            return 1;
        return 0;
    }
    if((*bname == '#') || ((slash == depth) && (*bname == '/'))) {
        if(*aname == '\0')
            return 0;
        return -1;
    }

    return (int) (*aname - *bname);
}

static int objcmpalpha(const void *a, const void *b) {
    const char *aname = (*((object_t **) a))->obname;
    const char *bname = (*((object_t **) b))->obname;

    return strcmpalpha(aname, bname, 1000);
}

static int itemcmpsize(const void *a, const void *b) {
    long asz = (*((ITEMS *) a)).mem;
    long bsz = (*((ITEMS *) b)).mem;

    if(asz == bsz)
        return strcmpalpha((*((ITEMS *) a)).name, (*((ITEMS *) b)).name, 1000);

    return (int) (bsz - asz);
}

static int objcmpsize(const void *a, const void *b) {
    object_t *aobj = (*((object_t **) a));
    object_t *bobj = (*((object_t **) b));
    int asz = aobj->prog->total_size + data_size(aobj);
    int bsz = bobj->prog->total_size + data_size(bobj);

    if(asz == bsz)
        return objcmpalpha(a, b);

    return bsz - asz;
}

static int objcmpidle(const void *a, const void *b) {
    int arft = (*((object_t **) a))->time_of_ref;
    int brft = (*((object_t **) b))->time_of_ref;

    if(arft == brft)
        return objcmpalpha(a, b);

    return arft - brft;
}
#endif