# include "basic.h"

int config          PROTO((int, char **));
int exec_sleep      PROTO((int, char **));
int exec_usleep     PROTO((int, char **));
int cd              PROTO((int, char **));
int date            PROTO((int, char **));
int dirname_opihi   PROTO((int, char **));
int echo            PROTO((int, char **));
int exec_last       PROTO((int, char **));
int exec_next       PROTO((int, char **));
int exec_break      PROTO((int, char **));
int file            PROTO((int, char **));
int getchr_func     PROTO((int, char **));
int help       	    PROTO((int, char **));
int input      	    PROTO((int, char **));
int list       	    PROTO((int, char **));
int list_help  	    PROTO((int, char **));
int list_vars  	    PROTO((int, char **));
int local           PROTO((int, char **)); /* data? */
int macro      	    PROTO((int, char **));
int memory          PROTO((int, char **));
int mkdir_opihi	    PROTO((int, char **));
int module    	    PROTO((int, char **));
int output     	    PROTO((int, char **));
int pwd        	    PROTO((int, char **));
int quit       	    PROTO((int, char **));
int run_for    	    PROTO((int, char **));
int run_if     	    PROTO((int, char **));
int run_while  	    PROTO((int, char **));
int scan       	    PROTO((int, char **));
int shell      	    PROTO((int, char **));
int sprintf_opihi   PROTO((int, char **));
int fprintf_opihi   PROTO((int, char **));
int strlen_func     PROTO((int, char **));
int substr_func     PROTO((int, char **));
int strpop          PROTO((int, char **));
int strsub          PROTO((int, char **));
int wait_func  	    PROTO((int, char **));
int which      	    PROTO((int, char **));

/** mapping of the command names to command functions **/
static Command cmds[] = {  
  {"config",        config,             "(re)load config file?"},
  {"sleep",         exec_sleep,         "sleep for N seconds"},
  {"usleep",        exec_usleep,        "sleep for N microseconds"},
  {"cd",      	    cd,                 "change directory"},
  {"date",    	    date,               "get current date"},
  {"dirname",       dirname_opihi,      "built-in dirname function"},
  {"echo",    	    echo,               "type this line *"},
  {"break",   	    exec_break,         "escape from function *"},
  {"continue",	    exec_next,          "next loop iteration"},
  {"next",	    exec_next,          "next loop iteration"},
  {"last",	    exec_last,          "last loop iteration"},
  {"return",	    exec_last,          "exit from macro"},
  {"file",          file,               "test file existence"},
  {"getchr",        getchr_func,        "find character in string"},
  {"help",    	    help,               "get help on a function *"},
  {"input",   	    input,              "read command lines from a file *"},
  {"list",    	    list,               "get variable list"},
  {"?",       	    list_help,          "list commands *"},
  {"??",      	    list_vars,          "list variables *"},
  {"local",  	    local,              "define local variables"},
  {"macro",   	    macro,              "deal with the macros *"}, 
  {"memory",        memory,             "long listing of the allocated memory"},
  {"mkdir",         mkdir_opihi,        "built-in mkdir command"},
  {"module",        module,             "load script file from the modules directories"},
  {"output",  	    output,             "redirect output to file"},
  {"pwd",     	    pwd,                "print current working directory"},
  {"exit",    	    quit,               "exit program *"}, 
  {"quit",    	    quit,               "exit program *"},
  {"for",     	    run_for,            "for loop"}, 
  {"if",      	    run_if,             "logical cases *"}, 
  {"while",   	    run_while,          "while loop"}, 
  {"scan",    	    scan,               "scan line from keyboard or file to variable *"},
  {"!",       	    shell,              "system call"},
  {"exec",    	    shell,              "system call"},
  {"sprintf",       sprintf_opihi,      "formatted print to a variable"},
  {"fprintf",       fprintf_opihi,      "formatted print to standard output"},
  {"strlen",        strlen_func,        "string length"},
  {"substr",        substr_func,        "substring"},
  {"strpop",        strpop,             "pop a string"},
  {"strsub",        strsub,             "replace instances of a key in a string"},
  {"wait",    	    wait_func,          "wait until return is typed"},
  {"which",   	    which,              "show command *"}
};

void InitBasic () {
  
  int i;

  InitCommands ();
  InitMacros ();
  InitBuffers ();
  InitVectors ();
  InitVariables ();
  InitLists ();

  for (i = 0; i < sizeof (cmds) / sizeof (Command); i++) {
    AddCommand (&cmds[i]);
  }
  
}

void InitBasic_PantasksClient () {
  
  int i;

  InitCommands ();
  InitMacros ();
  InitBuffers ();
  InitVectors ();
  InitVariables ();
  InitLists ();

  for (i = 0; i < sizeof (cmds) / sizeof (Command); i++) {
    if (!strcmp (cmds[i].name, "quit")) goto valid;
    if (!strcmp (cmds[i].name, "exit")) goto valid;
    if (!strcmp (cmds[i].name, "exec")) goto valid;
    if (!strcmp (cmds[i].name, "!")) goto valid;
    continue;

  valid:
    AddCommand (&cmds[i]);
  }
}
