/************************************************************
**
** Application: TaskLib
**
** Title:       c.message
**
*************************************************************/

/*
*
* Copyright (c) 2023, David Pilling and Chris Johnson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the copyright holder nor the names of their
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/


/* Include files */
/* from standard clib */
#include <stdlib.h>
#include <stdio.h>

/* from TaskLib */
#include "os.h"
#include "swis.h"
#include "alloc.h"

/* from application */
#include "message.h"



/************************************************************/

/* Local defines */
#define MSG_BUF_LEN 256


typedef struct message_descriptor
{
  int reserved1;
  int reserved2;
  int reserved3;
  int reserved4;
  int open;
} message_descriptor;



/************************************************************/
/* global variables */
static message_descriptor msg_desc;
static char * msg_file_buffer;

char msg_buffer[MSG_BUF_LEN];

/************************************************************/


/************************************************************/

static os_error * message_fileinfo (char * filename, int * size)

{
  os_error * err;
  os_regset  rx;

  rx.r[0] = 0;
  rx.r[1] = (int) filename;
  rx.r[2] = 0;

  err = os_swix (MessageTrans_FileInfo, &rx);

  if ((rx.r[0] & 1) == 1)
  {
    *size = 0;
  }
  else
  {
    *size = rx.r[2];
  }

  return (err);
}




/************************************************************/

static os_error * message_openfile (char * filename, char * msg_file_buffer)

{
  os_error * err;
  os_regset  rx;

  rx.r[0] = (int) &msg_desc;
  rx.r[1] = (int) filename;
  rx.r[2] = (int) msg_file_buffer;

  err = os_swix (MessageTrans_OpenFile, &rx);

  return (err);
}




/************************************************************/

static os_error * message_closefile (void)

{
  os_error * err;
  os_regset  rx;

  rx.r[0] = (int) &msg_desc;

  err = os_swix (MessageTrans_CloseFile, &rx);

  return (NULL);
}





/************************************************************/

char * message_lookup (char * token)

{
  os_error * err;
  os_regset  rx;

  rx.r[0] = (int) &msg_desc;
  rx.r[1] = (int) token;
  rx.r[2] = (int) msg_buffer;
  rx.r[3] = MSG_BUF_LEN;

  rx.r[4] = 0;     // no parameter substitution
  rx.r[5] = 0;     // no parameter substitution
  rx.r[6] = 0;     // no parameter substitution
  rx.r[7] = 0;     // no parameter substitution

  err = os_swix (MessageTrans_Lookup, &rx);

  msg_buffer[rx.r[3]] = 0x00;

  if (err)
    return (NULL);
  else
    return (msg_buffer);

}



/************************************************************/

os_error * message_initialise (char * filename)

{
  os_error * err;
  int size;

  err = message_fileinfo (filename, &size);

  if (size != 0)
  {
    if (!err) err = salloc ( (void **)&msg_file_buffer, size + 4);
    if (!err) err = message_openfile (filename, msg_file_buffer);
  }

  return (err);
}




/************************************************************/

os_error * message_finit (void)

{
  os_error * err = NULL;

  message_closefile ();
  sfree ((void **)&msg_file_buffer);

  return (err);
}




/*************  End of c.messages  ***********************/

