

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "MLog.h"
#include "server_lib.h"
#include "commands.h"
#include "tasks.h"
#include "time_funct.h"
#include "parameters.h"


/* Common string used in lots of places */
static const char    LocMsg[] = "        %s:%d <%s>\n";

/********************************************************************/
/*                                                                  */
/*  cmd_set                                                         */
/*                                                                  */
/*                                                                  */
/*  PURPOSE:    Set a global state variable.                        */
/*              Any asignment that can be made in the mk4d.conf     */
/*              file may be made here too.  The arguments to this   */
/*              command are two strings that when concatenated      */
/*              would look like a non-comment line in the mk4d.conf */
/*              file.                                               */
/*                                                                  */
/*  ARGUMENTS:  Two strings with the following fields               */
/*                1) Variable name, case insensitive                */
/*                2) Value to be assigned in string format          */
/*                                                                  */
/********************************************************************/
int cmd_set(int sd, char args[])
{
    /* These are the argument expected in the args[] string */
    /* they must appear in the order shown.                 */
    char    VarName[80];
    char    VarValue[80];
    
    /* Used internally */
    char    FaiureMsg1[] =
        "WARNING invalid argument passed to cmd_set\n";
    int     nargs;
    char    line[80];
    char    FailureStr[80];
    int     res;
    
    /* check for valid arguments */
    if ( !args )
    {
        /* null pointer */
        ml_print (ML_MANDATORY, ML_ERROR, FaiureMsg1);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, "(null pointer)");
            
        strcpy(line, "Missing agument\n"); 
        res = sl_Send2Client(sd, line);   
        if (res < 0)
        {
            /* error sending command.  Client died? */
            ml_printf(ML_MANDATORY, ML_ERROR,
                "ERROR: Send to client failed on socket %d\n", sd);
            ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
                __FILE__, __LINE__, line);
            return CMD_FAIL;
        }
        return CMD_FAIL;
        
    }
    if ( strlen(args) < 3 )
    {
        /* to short to be valid */
        ml_print (ML_MANDATORY, ML_ERROR, FaiureMsg1);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
            
        strcpy(line, "Argument to short to be valid\n");
        res = sl_Send2Client(sd, line);
        if (res < 0)
        {
            /* error sending command.  Client died? */
            ml_printf(ML_MANDATORY, ML_ERROR,
                "ERROR: Send to client failed on socket %d\n", sd);
            ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
                __FILE__, __LINE__, line);
            return CMD_FAIL;
        }
        return CMD_FAIL;
        
    }

    /* convert string and return count of arguments */
    nargs = sscanf(args, "%s %s", VarName, VarValue);
  
    if (nargs != 2)
    {
        /* Some error in args string detected */
        ml_print (ML_MANDATORY, ML_ERROR, FaiureMsg1);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
            
        strcpy(line, "Invalid agument\n");
        res = sl_Send2Client(sd, line);
        if (res < 0)
        {
            /* error sending command.  Client died? */
            ml_printf(ML_MANDATORY, ML_ERROR,
                "ERROR: Send to client failed on socket %d\n", sd);
            ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
                __FILE__, __LINE__, line);
            return CMD_FAIL;
        }
        return CMD_FAIL;
    }

    /* If we get to this point we know the arguments are      */
    /* converted with no errors.  We know we have good input  */
    /*  and can proceed                                       */

    strcpy(FailureStr, "");
    res = Keyword_Assignment(VarName, VarValue, FailureStr);
    if (res == RTN_ERR)
    {
        /* there was an error.  Send FAIL and the reason for the    */
        /* failure to the client.                                   */
        
        snprintf(line, sizeof(line), "SET not performed: %s\n", FailureStr);
        res = sl_Send2Client(sd, line);
        if (res < 0)
        {
            /* error sending command.  Client died? */
            ml_printf(ML_MANDATORY, ML_ERROR,
                "ERROR: Send to client failed on socket %d\n", sd);
            ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
                __FILE__, __LINE__, line);
            return CMD_FAIL;
        }
        return CMD_FAIL;
    }

    /* We are done.  Send positive return code. */
    return CMD_OK;
}    
