
/* loadtemp.c
 *
 * Wimp function library
 *  Joseph Heenan, 1998.
 *
 *
 *
 *
 * $Log: loadtemp,v $
 * Revision 1.3  1998/05/28 17:17:59  joseph
 * Changed to use a 2k buffer on the stack rather than a temporary
 * malloc()'ed buffer if possible
 *
 * Revision 1.2  1998/05/10 20:13:10  jogu
 * Added names to file headers
 * Added implicit cast for frontend_taskname to wimp_report_error
 *   (it defines 'name' without 'const')
 *
 * Revision 1.1.1.1  1998/05/10 19:56:03  jogu
 * WimpCLib V1.00 (created from newshound c.wimpc rev 1.2)
 *
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "swis.h"
#include "wimplib.h"

#include "wimpclib.h"


int wimpc_loadtemplate(const char *name)
{
  char *buffer,*workspace,*workend, nameb[12];
  _kernel_swi_regs regs;
  int handle;
  char stack[2048];


  memset( nameb, 0, sizeof nameb ); /* not needed ? */
  *nameb=0;
  strncat( nameb,name, sizeof(nameb) - 1 );

  regs.r[1] =       0;
  regs.r[2] =       0;
  regs.r[3] =       0;
  regs.r[4] =       -1;
  regs.r[5] = (int) nameb; /* 12 bytes / word aligned */
  regs.r[6] =       0;
  E_CHECK_RETURN( -1, wimp_load_template(&regs) );

  if ( regs.r[6] == 0 )
    E_RETURN( -1, "Template not in template file" );

  if ( regs.r[1] < sizeof stack )
    buffer = stack;
  else
    buffer = malloc( regs.r[1] );

  workspace = malloc( regs.r[2] );
  if ( !buffer || !workspace)
  {
    if ( buffer && buffer!=stack   ) free( buffer );
    if ( workspace ) free( workspace );
    E_RETURN( -1, "Could not claim memory for template" );
  }

  workend=workspace+regs.r[2];

  regs.r[1]= (int) buffer;
  regs.r[2]= (int) workspace;
  regs.r[3]= (int) workend;
  regs.r[4]=       -1;
  regs.r[5]= (int) nameb; /* 12 bytes / word aligned */
  regs.r[6]=       0;

  E_CHECK_RETURN( -1, wimp_load_template(&regs) );

  E_CHECK_RETURN( -1, wimp_create_window((WimpCreateWindowBlock *)buffer, &handle) );

  if ( buffer != stack ) free(buffer);

  return handle;
}

