
/*
 * Copyright (c) 2001 BBNT Solutions LLC
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice and this permission
 * appear in all copies and in supporting documentation, and that the
 * name of BBN Technologies not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  BBN makes no representations about the
 * suitability of this software for any purposes.  It is provided "AS
 * IS" without express or implied warranties.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stats.h"

struct cmd_func
{
    char *cmd;
    unsigned short args;
    void (*func) ();
};

struct cmd_func cmd_list [] = { 
#ifdef IMPL_A2
                                { "a2",        1, cmd_a2 },
#endif /* IMPL_A2 */
                                { "base",      1, cmd_base },
                                { "cdf",       1, cmd_cdf },
                                { "ccdf",      1, cmd_ccdf },
                                { "clear",     0, cmd_clear },
                                { "close",     0, cmd_close },
                                { "dump",      1, cmd_dump },
                                { "exit",      0, cmd_exit },
                                { "help",      0, cmd_help },
                                { "info",      0, print_info },
                                { "input",     1, cmd_input },
                                { "ks-fit",    1, cmd_ks_dist_fit },
                                { "open",      1, cmd_open },
                                { "perc",      1, cmd_perc },
#ifdef TRANSFORM
                                { "transform", 1, cmd_transform },
#endif /* TRANSFORM */
                                { "trim",      1, cmd_trim },
                                { "set",       1, cmd_set } };


void cmd_exit ()
{
    exit (0);
}


void cmd_usage (cmd)
char *cmd;
{
    fprintf (stderr,"Invalid command: %s\n", cmd);
    cmd_help ();
    if (script_file)
	exit (1);
}


void do_cmd (input_line)
char *input_line;
{
    unsigned short list_size;
    unsigned short i;
    char *cmd;
    char *input;

    list_size = sizeof (cmd_list) / sizeof (struct cmd_func);
    cmd = strtok (input_line," ");
    for (i = 0; i < list_size; i++)
    {
        input = cmd_list [i].args ? cmd : input_line;
        if (!strcmp (input,cmd_list [i].cmd))
        {
            cmd_list [i].func ();            
            return;
        }
    }
    if (!strcmp (cmd,"basic"))
        cmd_basic (NULL," ");
    else if (!strcmp (cmd,"freqtab"))
        cmd_freqtab (0.0,0,1);
    else if (!strcmp (cmd,"sort"))
        quick_sort (sample,0,num_samples - 1);
    else
        cmd_usage (input_line);
}
