// $Id: cmd_loop.cpp,v 1.15 2013/03/22 21:46:57 lpoulsen Exp $
//
// Copyright (c) 2008 by Afar Communications Inc, Santa Barbara, CA 93117 USA
//
// Command loop for ECON program
//
// $Log: cmd_loop.cpp,v $
// Revision 1.15  2013/03/22 21:46:57  lpoulsen
// Make -i filename work together with -r name
//
// Revision 1.14  2013/03/22 17:37:17  lpoulsen
// Allow "econ -r name" to make one connection with the named radio
//     without ever entering the Econsole> command prompt.
//
// Revision 1.13  2012/01/13 00:55:05  lpoulsen
// Update to current platform SDK
// Print WinPcap version and OS 32/64 bit type from version command
//
// Revision 1.12  2011/03/22 00:26:10  lpoulsen
// Resolve sequence errors on very weak links
// Resolve hangs when Ctrl-C between "dir" and "download"
// Resolve conflicting build date information
//
// Revision 1.11  2011/03/10 00:54:07  lpoulsen
// Allow shell escapes with !
//
// Revision 1.10  2011/02/08 22:07:02  lpoulsen
// evived the LINUX build of ECON - now ECON-PLUS
//
// Revision 1.9  2010/12/28 20:05:22  lpoulsen
// Make sure AOK is defined.
//
// Revision 1.8  2008/08/06 17:06:43  lpoulsen
// Add build date to version command output
//
// Revision 1.7  2008/07/25 00:43:27  lpoulsen
// Added "select all" and "edit" commands.
// Optional OEM_COMPANY personalization
//
// Revision 1.6  2008/07/17 00:21:52  lpoulsen
// Resolve type of global cvs_tag[];
//
// Revision 1.5  2008/07/17 00:14:37  lpoulsen
// Exit gracefully on EOF on stdin.
// CVS ----------------------------------------------------------------------
//
// Revision 1.4  2008/07/16 00:23:53  lpoulsen
// Prepare for release CON 04.00
//
// Revision 1.3  2008/07/11 16:28:46  lpoulsen
// Econ 4.00 release candidate supports UPDATE and SELECT commands.
//
// Revision 1.2  2008/06/10 00:06:10  lpoulsen
// New -X option is now working
// Updated -I and -O implementation
//
// Revision 1.1  2008/05/31 00:35:09  lpoulsen
// Econ 4.x preview - now with command shell
//
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "afartype.h"
#include "parser.h"
#include "ucon.h"

#define AOK          0
#define EXIT_SUCCESS 0

char         input[82];
ucio_session basic_session;

extern "C" {
    void exit(int i);
    void print_system_version(void);
}

extern_cmd_func version_cmd;

void
cmd_loop(char *prompt)
{
    char        *command, *cmd_tail;
    int          pos;
    char        *option_I = get_option_I();   // local copy
    char        *option_R = get_option_R();   // local copy
    int          cmd_index;
    cmd_ret_t    cmd_status = CMD_SUCCESS;

    if (prompt == NULL) prompt = (char *)"Afar";

    basic_session.session_outfile = stdout;

    while(1)
    {
        if (option_R && (get_econ_phase() == ECON_STARTING)) {
            sprintf (input, "CONNECT %s %s", option_R,
                             (option_I ? option_I : "") );
        } else {
            printf("%s> ", prompt);
            fgets(input, 80, stdin);
            if (feof(stdin))
            {
                printf("\n");
                exit(EXIT_SUCCESS);
            }
#ifdef LINUX
            if (input[0] == '!')
            {
                system(&input[1]);
                continue;
            }
#endif
        }
        command = input;

        while (command)
        {
            cmd_tail = strchr(command, ';');
            if (cmd_tail) {
                *cmd_tail = 0;
                cmd_tail++;
            }
            pos = 0;
            cmd_index = parse_command(command, pos, current_command_table,
                                                    &basic_session);
            if (cmd_index != ~AOK)
            {
                Argument_List *a = parse_arguments(cmd_index, command, pos,
                                                   current_command_table,
                                                   &basic_session);
                if (a != 0)
                {
                    cmd_status = ((current_command_table[cmd_index].func))(a,
                                                   &basic_session);
                    delete a;
                }
            }
            if (cmd_status != CMD_SUCCESS)
                break;
            if (option_R)
                exit(EXIT_SUCCESS);
            command = cmd_tail;
        }
    }
    exit(EXIT_SUCCESS);
}

cmd_ret_t
version_cmd(Argument_List * a, ucio_session *session)
{
    char *cvs_tag = get_cvs_tag();

    if (strlen(cvs_tag) > 9)
    {
        size_t i;

        for (i = 8; i < strlen(cvs_tag); i++)
        {
            switch (cvs_tag[i])
            {
            case '_':
                cvs_tag[i] = ' ';
                break;
            case '-':
                cvs_tag[i] = '.';
                break;
            case '$':
                cvs_tag[i] = 0;
                break;
            default:
                ;
            }
        }
    }
    printf("EConsole %s built %s %s\n",
                ((strlen(cvs_tag) > 9) ? &cvs_tag[7] : "engineering version" ),
                __DATE__, __TIME__);
#ifdef WIN32
    print_system_version();
#endif
    return CMD_SUCCESS;
}

cmd_ret_t
session_logout(Argument_List * a, ucio_session *session)
{
    exit(0);
    return CMD_SUCCESS;
}
