/****************************************************************************/
/*                                                                          */
/*  FILE: cmd_image2cdl.c                                                   */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Execute a client command to display the image buffer  content on    */
/*      an IRAF style display server such as ximtool or SAOtng.  This       */
/*      is done be use of the CDL (Client Display Library.)                 */
/*                                                                          */
/*  HISTORY:                                                                */   
/*      June 1998 -- Chris Albertson Started work                           */
/*                                                                          */
/****************************************************************************/
/*                                                                          */
/*                  Copyright (C) 1998 Chris Albertson.                     */
/*                                                                          */
/*   This program is free software; you can redistribute it and/or          */
/*   modify it under the terms of the GNU General Public License as         */ 
/*   published by the Free Software Foundation; either version 2, or        */ 
/*   (at your option) any later version.                                    */
/****************************************************************************/


/* Used by RCS and ident */
char cmd_image2cdl_rcsid[] = 
    "$Id: cmd_image2cdl.c,v 1.2 1998/09/29 06:44:33 chris Exp $";


#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 "buffer_mgr.h"


/* Prototypes of local functions */
static int validate_args(char args[]);

/* Arguments.  Decoded by validate_args() */
#define SERVER_NAMESZ 512
static char Server[SERVER_NAMESZ];



/****************************************************************************/
/*                                                                          */
/*  cmd_image2cdl                                                              */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Execute the "image2cdl" command.                                    */
/*                                                                          */
/*  ARGUMENTS:                                                              */
/*      cmd_point_at like all command functions accepts one string          */
/*      argument.  Inside this are the "real" arguments.  Theses are:       */
/*                                                                          */
/*        1) The server address.  This is a string of the form:             */
/*           <domain>: <address>  Examples are:                             */
/*           unix:/tmp/.IMT%d                                               */
/*           fifo:/dev/imt1i:/dev/imt1o                                     */
/*           inet:5137:foo.bar.edu                                          */
/*           See the documentation that comes with the CDL source code.     */
/*                                                                          */
/*        2) We may want to add more arguments so a user can specify        */
/*           if/how the image is to be z-scaled if subsampled/compressed    */
/*                                                                          */
/****************************************************************************/
int cmd_image2cdl(int sd, char args[])
{
    int res;
    
    
    if (CMD_FAIL == validate_args(args))
    {
        return CMD_FAIL;
    } 
    
    ml_printf(ML_DEBUG, ML_INFORMATION,
        "cmd_image2cdl called with valid argument: Server=<%s>\n",
        Server);  
            
    /* Display the image(s) */
    if(-1 == IA2CDL(Server))
    {
        /* ERROR */
        ml_print (ML_MANDATORY, ML_ERROR,
            "ERROR: cmd_image2cdl failed.\n");
            
        res = sl_Send2Client(sd, 
            "Image could not be displayed.\n");
        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, 
                "       File: %s  Line: %d\n", __FILE__, __LINE__);
            return CMD_FAIL;
        }

        return CMD_FAIL;
    }

        

        
    return CMD_OK;
}



/****************************************************************************/
/*                                                                          */
/*  validate_args                                                           */
/*                                                                          */
/*  PURPOSE:                                                                */
/*      Validate and decode command argument string.                        */
/*                                                                          */
/*  OUTPUT:                                                                 */
/*                                                                          */
/****************************************************************************/      
static int validate_args(char args[])
{ 
    int nargs;
    
    /* Common string used in lots of places */
    static const char    LocMsg[] = "        %s:%d <%s>\n";
    
    

    /* convert the args[] string to values.  First check for sanity */
    if ( !args )
    {
        /* null pointer */
        ml_print (ML_MANDATORY, ML_ERROR, 
            "ERROR no arguments passed\n");
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, "(null pointer)");
        return CMD_FAIL;
    }
    
    /* convert string and return count of arguments */
    nargs = sscanf(args, "%s", Server);
  
    if (nargs != 1)
    {
        /* Some error in args string detected */
        ml_printf(ML_MANDATORY, ML_ERROR, 
            "ERROR Could not parse argment list >%s<\n", args);
        ml_printf(ML_MANDATORY, ML_ERROR, LocMsg,
            __FILE__, __LINE__, args);
        return CMD_FAIL;
    }
           
    return CMD_OK;
}    
