
/*************  main.c  **********************
 *                                           *
 *                                           *
 *  Exe:        Keti                         *
 *  Version:    1.0                          *
 *                                           *
 *  Why:        Print 3.5" Disklabel         *
 *                                           *
 *  Syntax:     Keti <inputfile>             *
 *                                           *
 *  Input:      Ascii-File                   *
 *               1 Headline  (max 25 chars)  *
 *              14 Textlines (max 44 chars)  *
 *                                           *
 *  Labelsize:  71.5 x 69.6 mm               *
 *              e.g. Zweckform No.3642       *
 *                                           *
 *  Hardware:   Nec P6 (compatible)          *
 *                                           *
 *  Software:   Kickstart 2.xx               *
 *                                           *
 *  Compiler:   DICE - thanx Matt            *
 *              dcc main.c -o Keti -2.0      *
 *              (only 2000 byte !!!)         *
 *                                           *
 *  Date:       14/08/92                     *
 *                                           *
 *  Author:     Kössi                        *
 *  Contact:    (0049) (0)2192 7630          *
 *                                           *
 *********************************************/

#include <exec/types.h>
#include <exec/exec.h>
#include <exec/libraries.h>
#include <dos/dos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>

#define MAXCHAR     45    /*  incl. '\n' */
#define MAXHEAD     26    /*  incl. '\n' */
#define MAXLINES    14

#define RESET       65
#define CONTLEN      7
#define HEADLEN      6
#define OS20STR     20

#define BUFSIZE    512

#define SI          15
#define DC2         18
#define CR          13
#define ESC         27
#define FS          28
#define CSI      '\x9B'
#define LF         '\n'
#define TAB        '\t'
#define SPACE       ' '
#define EOS        '\0'


const UBYTE version[]     = {'$','V','E','R',':',' '};
const UBYTE myname[]      = "Keti v1.0 © Koessi 92    \n\n";
const UBYTE usagestr[]    = "USAGE:  Keti <textfile>\n\n"
                            "textfile may have:   1 headline  (25 chars)\n"
                            "\t\tand 14 textlines (44 chars)\n";
const UBYTE os20str[]     = "No OS2.xx, sorry...\n";
const UBYTE parstr[]      = "par:";
const UBYTE prtstr[]      = "prt:";
const UBYTE fileisempty[] = ": File is empty !\n";
const UBYTE errorstr[]    =
{
  CSI, '3','2','m','*','*','*',' ','E','r','r','o','r',' ', EOS
};
const UBYTE normal[]      =
{
  CSI, '0','m', EOS
};

const UBYTE reset[RESET]  =
{
   FS, '@',       /*  RESET   */
  ESC, 'P',       /*  PICA    */
  ESC, 'x',  1,   /*  NLQ     */
  ESC, 'l',  2,   /*  LM  2   */
  ESC, 'Q', 28,   /*  RM 28   */
  ESC, 'R',  0,   /*  USA     */
  ESC, '0',       /*  88 LPP  */
   LF,  CR,       /*  CR LF   */
   SI,            /*  COND ON */
  ESC, 'a',  2,   /*  RIGHT   */

  'W', 'R','I','T','E',' ',' ','E','N','A','B','L','E',' ','-','>', LF, LF,
  'W', 'R','I','T','E',' ','P','R','O','T','E','C','T',' ','-','>', LF,

  ESC, 'a', 0,    /*  LEFT    */
  ESC, 'J', 15,   /*  15/180  */
};

const UBYTE headline[HEADLEN] =
{
  DC2,  LF, CR,   /*  COND OF */
  ESC, 'a',  1    /*  CENTER  */
};

const UBYTE contline[CONTLEN] =
{
   SI,            /*  COND ON */
  ESC, 'a',  0,   /*  LEFT    */
  ESC, 'Q', 48    /*  RM 48   */
};

BPTR input;     /* file         */
BPTR out;       /* par: | prt:  */

UBYTE buffer[BUFSIZE];

extern struct Library *SysBase;

int cleanup(int error);

/***********************  main  **********************************/
int
_main(short len, char *arg)
{
  char  *buf    = buffer;
  char  *ptr    = buf;
  int    chars  = 0;

  if (SysBase->lib_Version < 36)
  {
    BPTR out0;
    if (out0 = Output())
    {
      Write(out0, os20str, OS20STR);
      Close(out0);
    }
    return(RETURN_FAIL);
  }

  PutStr(myname);

  if (*arg == LF)
  {
    PutStr(usagestr);
    return(cleanup(RETURN_OK));
  }

  arg[len - 1] = EOS;

  if (((input = Open(arg, MODE_OLDFILE))   == DOSFALSE) ||
      ((chars = Read(input, buf, BUFSIZE)) == -1))
    return(cleanup(RETURN_WARN));

  if (chars < BUFSIZE)                /*  everything red */
  {
    Close(input);
    input = NULL;
  }

  while (chars)                       /*  skip empty space */
  {
    char p = *ptr;
    if (!(p == SPACE || p == LF || p == TAB))
      break;

    ++ptr; --chars;
  }

  if (chars == 0)                     /*  file empty  */
  {
    PutStr(errorstr);
    PutStr(fileisempty);
    return(cleanup(RETURN_OK));
  }

  if ((out = Open(prtstr, MODE_NEWFILE)) == DOSFALSE)
    return(cleanup(RETURN_WARN));
  else
    Close(out);

  if ((out = Open(parstr, MODE_NEWFILE)) == DOSFALSE)
    return(cleanup(RETURN_WARN));

  buf = ptr;
  len = 0;
  while (chars)     /*  read headline */
  {
    ++len; --chars;
    if (*ptr++ == LF)
      break;
  }

  short cnt = MAXCHAR;
  if (len > cnt)
  {
    len = cnt;
    buf[MAXCHAR - 1] = LF;
  }

  if (!((Write(out,    reset,   RESET) ==   RESET) &&
        (Write(out,      buf,     len) ==     len) &&
        (Write(out, headline, HEADLEN) == HEADLEN)))
    return(cleanup(RETURN_WARN));

  cnt = MAXHEAD;
  if (len > cnt)
  {
    len = cnt;
    buf[MAXHEAD - 1] = LF;
  }

  if (!((Write(out,      buf,     len) ==     len) &&
        (Write(out, contline, CONTLEN) == CONTLEN)))
    return(cleanup(RETURN_WARN));

  Close(out);

  if ((out = Open(prtstr, MODE_NEWFILE)) == DOSFALSE)
    return(cleanup(RETURN_WARN));

  len = 0;
  buf = buffer;
  cnt = 1;          /*  cnt lin */
  short xtr = 0;    /*  cnt esc */


  while (chars)
  {
    while (chars)
    {
      if (*ptr == (BYTE)CSI)   /*  esc codes */
        xtr += 3;

      *buf = *ptr++;

      ++len; --chars;

      if (*buf++ == LF || chars == 0)   /*  eol */
      {
        buf = buffer;
        if ((len - xtr) > MAXCHAR)
        {
          len = MAXCHAR;
          buf[MAXCHAR - 1] = LF;
        }

        if (Write(out, buf, len) != len)
          return(cleanup(RETURN_WARN));

        len = 0;
        if (chars)
        {
          if (cnt < MAXLINES)
          {
            cnt++;
            xtr = 0;
          }
          else
            chars = 0;
        }
        else
        {
          if (xtr)
          {
            short mx = 3;
            while (xtr > mx)
              xtr -= mx;
            xtr = mx - xtr;
          }
        }
      }
    }
    if (input && (cnt < MAXLINES))
    {
      ptr = buffer;
      if ((chars = Read(input, ptr, BUFSIZE)) == -1)
        return(cleanup(RETURN_WARN));
    }
  }                         /*  chars > 0 */
  for (*buf = LF; cnt <= MAXLINES; cnt++)
  {
    if (Write(out, buf, 1) != 1)
      return(cleanup(RETURN_WARN));
  }
  return(cleanup(RETURN_OK));
}


/***********************  cleanup() ********************************/
int
cleanup(int error)
{
  if (error)
    PrintFault(IoErr(), errorstr);

  PutStr(normal);

  if (input)
    Close(input);
  if (out)
    Close(out);

  return(error);
}
