
/****************************************************************
 *                                                              *
 *    TICON                                                     *
 *                                                              *
 *   Types (displays) Text Files from an Icon                   *
 *                                                              *
 *   copyright 1986 by Pete Goodeve -- All Rights Reserved      *
 *                                                              *
 *    This is a quick hack, sparsely commented -- Sorry.        *
 *                                                              *
 *    Compile with pass 2 -v option (Lattice)                   *
 *    Link with lstartup.obj, amiga.lib, lc.lib                 *
 *                                                              *
 * vers 86:7:15                                                 *
 ****************************************************************/

#include "exec/types.h"
#include "workbench/startup.h"


extern struct WBStartup *WBenchMsg;

struct WBArg *argptr;
int nargs;

LONG infile, outfile;

_main()
{

   if (!WBenchMsg)
      return 0;

   if (!(outfile = Open("CON:0/10/640/185/TICON 1.00", MODE_OLDFILE)))
      return 0;

   argptr = WBenchMsg->sm_ArgList;
   nargs = WBenchMsg->sm_NumArgs;
   for(argptr++, nargs--; nargs; argptr++, nargs--)
      doito(argptr);

   display("\n\n<<type ctrl-C to close window>>");
   Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);
   Close(outfile);
}
/*** end main ***/


doito(argp) struct WBArg *argp;
{
   LONG oldir;

   oldir = CurrentDir(argp->wa_Lock);
   infile = Open(argp->wa_Name, MODE_OLDFILE);
   dispfile();
   Close(infile);
   CurrentDir(oldir);
}

display(msg) char *msg;
{
   Write(outfile, msg, strlen(msg));
}

#define BUFSIZE 200

dispfile()
{
   char inbuf[BUFSIZE], outbuf[BUFSIZE], ansbuf[2];
   int inpos = 0, lcount = 20;
   while (copyline(inbuf, &inpos, outbuf)) {
      if (!lcount-- || *outbuf == '\014' /*Formfeed*/) {
         display("<<Type return to continue>>");
         Read(outfile,ansbuf,1);
         display("\x1b\x9bF\x1b\x9bM"); /* backup one line & delete */
         display(outbuf);
         lcount = 15; /* leave some overlap with previous page */
      }
      else display(outbuf);
      if (SetSignal(0,SIGBREAKF_CTRL_C)) break;
   }
}

copyline(in, pos, out) char *in, *out; int *pos;
{
   char ch, copych();
   int trunc=BUFSIZE-1;
   while ((*out++ = ch = copych(in, pos)) && ch != '\n' && --trunc)
       /*loop*/;
   *out = '\0';
   return (int) ch /* FALSE if EOF */;
}

 static int curmax=0;

char copych(in, pos) char *in; int *pos;
{
   if (*pos >= curmax) {
      curmax = Read(infile,in,BUFSIZE);
      *pos = 0;
      if (!curmax) return '\0';
   }
   return in[(*pos)++];
}

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

