/* $Revision Header *** Header built automatically - do not edit! ***********
 *
 *      (C) Copyright 1992 by Metalworx
 *
 *      Name .....: template.c
 *      Created ..: Sat 19-Sep-92 12:46
 *      Revision .: 0
 *
 *      Date            Author          Comment
 *      =========       ========        ====================
 *           1990       Mtwx            Created this file!
 *
 * $Revision Header ********************************************************/
 #define REVISION 0

/*******************************************************************
* template.c : processes user desired templates into command line  *
*              parameters (argv) and gives them back to wfile to   *
*              be further processed. (Actually it simulates user   *
*              entered command line parameters)                    *
*******************************************************************/

/* ------------------------------- includes ----------------------------- */

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

/* ----------------------- forward references ------------------- */

void      amiga2intel(void);
void      intel2amiga(void);
void      check_malloc(int), print_templ(char *,int);

/* ----------------------- external references ------------------ */

extern char *templ_argv[];

/* -------------------------------------------------------------- */

int       unix2msdos(char *command)
{
  register int i;

  for (i = 0; i < 9; i++)
    templ_argv[i] = (char *) malloc(9);
  check_malloc(9);
  strcpy(templ_argv[0], "+CR");
  strcpy(templ_argv[1], "-204=132");   /* ae */
  strcpy(templ_argv[2], "-206=148");   /* oe */
  strcpy(templ_argv[3], "-207=129");   /* ue */
  strcpy(templ_argv[4], "-216=142");   /* AE */
  strcpy(templ_argv[5], "-218=153");   /* OE */
  strcpy(templ_argv[6], "-219=154");   /* UE */
  strcpy(templ_argv[7], "-222=225");   /* sz */
  strcpy(templ_argv[8], "+26");        /* EOF */
  print_templ(command, 9);
  return 9;
}

int       msdos2unix(char *command)
{
  register int i;

  for (i = 0; i < 8; i++)
    templ_argv[i] = (char *) malloc(9);
  check_malloc(9);
  strcpy(templ_argv[0], "-CR");
  strcpy(templ_argv[1], "-132=204");   /* ae */
  strcpy(templ_argv[2], "-148=206");   /* oe */
  strcpy(templ_argv[3], "-129=207");   /* ue */
  strcpy(templ_argv[4], "-142=216");   /* AE */
  strcpy(templ_argv[5], "-153=218");   /* OE */
  strcpy(templ_argv[6], "-154=219");   /* UE */
  strcpy(templ_argv[7], "-225=222");   /* sz */
  strcpy(templ_argv[8], "-26=0");      /* EOF */
  print_templ(command, 9);
  return 9;
}

int       msdos2amiga(char *command)
{
  register int i;

  for (i = 0; i < 9; i++)
    templ_argv[i] = (char *) malloc(9);
  check_malloc(9);
  intel2amiga();
  strcpy(templ_argv[8], "-26=0");      /* EOF */
  print_templ(command, 9);
  return 9;
}

int       amiga2msdos(char *command)
{
  register int i;

  for (i = 0; i < 9; i++)
    templ_argv[i] = (char *) malloc(9);
  check_malloc(9);
  amiga2intel();
  strcpy(templ_argv[8], "+26");
  print_templ(command, 9);
  return 9;
}

int       unix2amiga(char *command)
{
  register int i;

  for (i = 0; i < 7; i++)
    templ_argv[i] = (char *) malloc(9);
  check_malloc(7);
  strcpy(templ_argv[0], "-204=228");   /* ae */
  strcpy(templ_argv[1], "-206=246");   /* oe */
  strcpy(templ_argv[2], "-207=252");   /* ue */
  strcpy(templ_argv[3], "-216=196");   /* AE */
  strcpy(templ_argv[4], "-218=214");   /* OE */
  strcpy(templ_argv[5], "-219=220");   /* UE */
  strcpy(templ_argv[6], "-222=223");   /* sz */
  print_templ(command, 7);
  return 7;
}

int       amiga2unix(char *command)
{
  register int i;

  for (i = 0; i < 7; i++)
    templ_argv[i] = (char *) malloc(9);
  check_malloc(7);
  strcpy(templ_argv[0], "-132=204");   /* ae */
  strcpy(templ_argv[1], "-148=206");   /* oe */
  strcpy(templ_argv[2], "-129=207");   /* ue */
  strcpy(templ_argv[3], "-142=216");   /* AE */
  strcpy(templ_argv[4], "-153=218");   /* OE */
  strcpy(templ_argv[5], "-154=219");   /* UE */
  strcpy(templ_argv[6], "-225=222");   /* sz */
  print_templ(command, 7);
  return 7;
}


/*##########################################################################
# amiga2os2():  template for the Amiga -> OS/2 conversion                  #
#                                                                          #
#   inputs: char *command - pointer to the template option                 #
#   return: int - count of template commands                               #
##########################################################################*/
int       amiga2os2(char *command)
{
  register int i;

  for (i = 0; i < 8; i++)
    templ_argv[i] = (char *) malloc(9);
  check_malloc(8);
  amiga2intel();
  print_templ(command, 8);
  return 8;
}


/*##########################################################################
# os22amiga():  template for the OS/2 -> Amiga conversion                  #
#                                                                          #
#   inputs: char *commqnd - pointer to the template option                 #
#   return: int - count of template commands                               #
##########################################################################*/
int       os22amiga(char *command)
{
  register int i;

  for (i = 0; i < 8; i++)
    templ_argv[i] = (char *) malloc(9);
  check_malloc(8);
  intel2amiga();
  print_templ(command, 8);
  return 8;
}


/*##########################################################################
# amiga2intel():  will be called by both the amiga2msdos() and the         #
#                 amiga2os2() routine, because most of the options are     #
#                 identical                                                #
#   inputs: none (void)                                                    #
#   return: none (void)                                                    #
##########################################################################*/
void      amiga2intel(void)
{
  strcpy(templ_argv[0], "+CR");
  strcpy(templ_argv[1], "-228=132");
  strcpy(templ_argv[2], "-246=148");
  strcpy(templ_argv[3], "-252=129");
  strcpy(templ_argv[4], "-196=142");
  strcpy(templ_argv[5], "-214=153");
  strcpy(templ_argv[6], "-220=154");
  strcpy(templ_argv[7], "-223=225");
}


/*##########################################################################
# intel2amiga():  will be called by both the msdos2amiga() and the         #
#                 amiga2os2() routine, because most of the options are     #
#                 identical                                                #
#                                                                          #
#   inputs: none (void)                                                    #
#   return: none (void)                                                    #
##########################################################################*/
void      intel2amiga(void)
{
  strcpy(templ_argv[0], "-CR");
  strcpy(templ_argv[1], "-132=228");   /* ae */
  strcpy(templ_argv[2], "-148=246");   /* oe */
  strcpy(templ_argv[3], "-129=252");   /* ue */
  strcpy(templ_argv[4], "-142=196");   /* AE */
  strcpy(templ_argv[5], "-153=214");   /* OE */
  strcpy(templ_argv[6], "-154=220");   /* UE */
  strcpy(templ_argv[7], "-225=223");   /* sz */
}


void      check_malloc(int number)
{
  int       i;

  for (i = 0; i < number; i++)
  {
    if (templ_argv[i] == NULL)
    {
      puts("Couldn't get memory for building template!");
      exit(1);
    }
  }
}

void      print_templ(char *command, int number)
{
  int       i, length = 0;

  printf("Option: %s\n", command);
  for (i = 0; i < number; i++)
  {
    if (length + strlen(templ_argv[i]) + 1 > 79)
    {
      printf("\n");
      length = strlen(templ_argv[i]) + 1;
    }
    else
      length += strlen(templ_argv[i]) + 1;
    printf("%s ", templ_argv[i]);
  }
  printf("\n");
}
