/* FLSERV test routine. */

/*)BUILD
   $(PROGRAM)  =  FSTEST
   $(LIBS)     =  LB:[1,1]CS/LB
   $(TKBOPTIONS) = {
      TASK = ...TST
   }
*/

/*
 * Edits:
 * V01.00 19-Feb-82  TTC   Initial edit.
 * V02.00 21-Oct-82  TTC   added ISAM tests
 */

#include <cs.h>

#define VERSION "V02.00"

#define BUFSIZ    80

int $$narg = 1;

union
   {
   int dev;
   char name[2];
   } dnam;

extern long rmark();
extern long atol();
extern char ttice();

char *info  = "\nFSTEST (I) ";
char *fatal = "\nFSTEST (F) ";

char buf[BUFSIZ+1];

struct fsb fsblk;

long  fpb;
long recno;

main()
   {
   char op[4];

again:
   ttosf("FSTEST: O)pen T)erm G)et W)rite U)pdate E)rase C(reate D)elete\n");
   printf("        M)ark S)eek R)ewind P)osition Un)lock X)it [%s]", VERSION);
   ttise(op,sizeof(op));
   switch(tolower(op[0]))
      {
      case 'c':
               create();
               break;
      case 'd':
               delete();
               break;
      case 'e':
               erase();
               break;
      case 'g':
               get();
               break;
      case 'm':
               mark();
               break;
      case 'o':
               open();
               break;
      case 'p':
               posit();
               break;
      case 'r':
               rewind();
               break;
      case 's':
               seek();
               break;
      case 't':
               termin();
               break;
      case 'u':
               if(tolower(op[1]) == 'n')
                  unlock();
               else
                  update();
               break;
      case 'w':
               put();
               break;
      case 'x':
               quit();
               break;
      default:
               printf("\n");
               goto again;
               break;
      }
   goto again;
   }

/* Connect to a file. */
open()
   {
   char cmode[3], dprot[3], opt[5];
   int stream;

   ttosf("\nFile Name  : ");
   ttise(fsblk.fs_fsp, sizeof(fsblk.fs_fsp));
   ttosf("\nI(nput O(utput A(ppend : ");
   ttise(cmode, sizeof(cmode));
   ttosf("\nS(hared P(rotected E(xclusive : ");
   ttise(dprot, sizeof(dprot));
   ttosf("\nD(elete N(o-extend W(rite-thru-cache-disabled : ");
   *opt = '';
   ttise(opt, sizeof(opt));
   if((stream = ropen(&fsblk, sizeof(fsblk), cmode, dprot, opt)) == 0)
      message("%sProblems with open.  DSW = %06o\n", fatal, $dsw);
   else
      {
      message("%sOpen was successful\n", info);
      message("Stream Number = %d\n", stream);
      }
   iosb();
   }

create()
   {
   char cmode[3], dprot[3], opt[4];
   int stream;

   ttosf("\nFile Name : ");
   ttise(fsblk.fs_fsp, sizeof(fsblk.fs_fsp));
   ttosf("\nS(equential R(elative : ");
   ttise(buf, 10);
   if(tolower(buf[0]) == 's')
      {
      fsblk.fs_fmt = FF_SEQ;
      ttosf("\n   Initial file size, blocks (0 = volume default) : ");
      ttise(buf, 20);
      fsblk.fs_bsz = atol(buf);
      }
   else
      {
      fsblk.fs_fmt = FF_REL;
/**********
      ttosf("\n   Record length, bytes : ");
      ttise(buf, 20);
      fsblk.fs_rsz = atoi(buf);
***********/
      ttosf("\nFSTEST has an 80 byte record buffer for relative files");
      fsblk.fs_rsz = 80;

      ttosf("\n   Record capacity : ");
      ttise(buf, 20);
      fsblk.fs_nrc = atol(buf);
      }
   ttosf("\nN(ormal S(tream F(ortran B(inary : ");
   ttise(buf, 10);
   switch(tolower(buf[0]))
      {
      case 's':   fsblk.fs_rec = RM_ANO; break;
      case 'f':   fsblk.fs_rec = RM_AFT; break;
      case 'n':   fsblk.fs_rec = RM_ACR; break;
      case 'b':   fsblk.fs_rec = RM_BIN;
      default: ;
      }
   ttosf("\nI(nput O(utput A(ppend : ");
   ttise(cmode, sizeof(cmode));
   ttosf("\nS(hared P(rotected E(xclusive : ");
   ttise(dprot, sizeof(dprot));
   ttosf("\nD(elete N(o-extend W(rite-thru-cache-disabled : ");
   *opt = '';
   ttise(opt, sizeof(opt));
   ttosf("\n** Using volume default static protection **");
   if((stream = rcreat(&fsblk, sizeof(fsblk), cmode, dprot, opt)) == 0)
      message("%sProblems with create.  DSW = %06o\n", fatal, $dsw);
   else
      {
      message("%sCreate was successful\n", info);
      message("Stream Number = %d\n", stream);
      }
   iosb();
   }

/* Delete a file. */
delete()
   {
   char opt[4];

   ttosf("\nFile Name  : ");
   ttise(fsblk.fs_fsp, sizeof(fsblk.fs_fsp));
   ttosf("\nW(ait M(ark-for-delete : ");
   *opt = '';
   ttise(opt, sizeof(opt));
   if(rdelet(&fsblk, sizeof(fsblk), opt) == 0)
      message("%sProblems with delete.  DSW = %06o\n", fatal, $dsw);
   else
      message("%sDelete was successful\n", info);
   iosb();
   }

/* Get an exising record */
get()
   {
   char opt[3];
   char str[10];
   int len;

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   ttosf("\nRecord Number (0 for sequential) : ");
   ttise(buf, 20);
   recno = atol(buf);
   ttosf("\nL(ock record  W(ait-on-lock : ");
   *opt = '';
   ttise(opt, sizeof(opt));
   if((len = rget(atoi(str),buf,BUFSIZ,&recno,4,opt)) == -1)
      message("%sProblems with get or EOF.  DSW = %06o\n", fatal, $dsw);
   else
      {
      buf[len] = '\0';
      message("%sGet was successful\n%s\n", info, buf);
      message("Length of record = %d\n", len);
      }
   iosb();
   }

put()
   {
   char str[10];
   char buf2[10];
   int i;

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   ttosf("\nRecord Number (0 for sequential) : ");
   ttise(buf, 20);
   recno = atol(buf);
   ttosf("\nRecord to put :\n");
   ttise(buf,sizeof(buf));
   ttosf("\nS)equential R)elative : ");
   ttise(buf2, 20);
   if(tolower(buf2[0]) == 'r')
      {
      for(i=strlen(buf); i < BUFSIZ; i++)
         buf[i] = ' ';
      buf[BUFSIZ] = '\0';
      if(rput(atoi(str),buf,BUFSIZ,&recno,4) == 0)
         message("%sProblems with put.  DSW = %06o\n", fatal, $dsw);
      else
         message("%sPut was successful\n", info);
      }
   else
      {
      if(rput(atoi(str),buf,strlen(buf),&recno,4) == 0)
         message("%sProblems with put.  DSW = %06o\n", fatal, $dsw);
      else
         message("%sPut was successful\n", info);
      }
   iosb();
   }

termin()
   {
   char str[10];
   char opt[5];

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   ttosf("\nM(ark-for-delete  S(pool  T(runcate : ");
   *opt = '';
   ttise(opt, sizeof(opt));
   if(rclose(atoi(str), opt) == 0)
      message("%sProblems with close.  DSW = %06o\n", fatal, $dsw);
   else
      message("%sClose was successful\n", info);
   iosb();
   }

mark()
   {
   char str[10];

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   if((fpb = rmark(atoi(str))) == -1)
      message("%sProblems with mark.  DSW = %06o\n", fatal, $dsw);
   else
      {
      message("%sMark was successful\n", info);
      message("File Position = %ld\n", fpb);
      }
   iosb();
   }

rewind()
   {
   char str[10];

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   if(rrwd(atoi(str), "") == 0)
      message("%sProblems with rewind.  DSW = %06o\n", fatal, $dsw);
   else
      message("%sRewind was successful\n", info);
   iosb();
   }

seek()
   {
   char str[10];
   char fpbstrg[10];

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   ttosf("\nFile Position : ");
   ttise(fpbstrg, sizeof(fpbstrg));
   fpb = atol(fpbstrg);
   if(rseek(atoi(str), &fpb) == 0)
      message("%sProblems with seek.  DSW = %06o\n", fatal, $dsw);
   else
      message("%sSeek was successful\n", info);
   iosb();
   }

update()
   {
   char opt[3];
   char str[10];
   char buf2[10];
   int i;

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   ttosf("\nRecord Number to update (0 for sequential) : ");
   ttise(buf, 20);
   recno = atol(buf);
   ttosf("\nW(ait-on-lock : ");
   *opt = '';
   ttise(opt, sizeof(opt));
   ttosf("\nNew record contents :\n");
   ttise(buf,sizeof(buf));
   ttosf("\nS)equential R)elative : ");
   ttise(buf2, 20);
   if(tolower(buf2[0]) == 'r')
      {
      for(i=strlen(buf); i < BUFSIZ; i++)
         buf[i] = ' ';
      buf[BUFSIZ] = '\0';
      if(rewrit(atoi(str),buf,BUFSIZ,&recno,4,opt ) == -1)
         message("%sProblems with update.  DSW = %06o\n", fatal, $dsw);
      else
         message("%sUpdate was successful\n", info);
      }
   else
      {
      if(rewrit(atoi(str),buf,strlen(buf),&recno,4,"") == -1)
         message("%sProblems with update.  DSW = %06o\n", fatal, $dsw);
      else
         message("%sUpdate was successful\n", info);
      }
   iosb();
   }

unlock()
   {
   char str[10];

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   if(runlck(atoi(str)) == 0)
      message("%sProblems with unlock. DSW = %06o\n", fatal, $dsw);
   else
      message("%sUnlock was successful\n", info);
   iosb();
   }

posit()
   {
   char opt[5];
   char str[10];

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   ttosf("\nPosition to record number : ");
   ttise(buf,sizeof(buf));
   recno = atol(buf);
   ttosf("\nG(reater N(-greater or equal : ");
   *opt = '';
   ttise(opt, sizeof(opt));
   if(rpos(atoi(str),&recno,4,opt) == 0)
      message("%sProblems with position.  DSW = %06o\n", fatal, $dsw);
   else
      message("%sPosition was successful. Record no. %ld.\n", info, recno);
   iosb();
   }

erase()
   {
   char opt[5];
   char str[10];

   ttosf("\nStream Number : ");
   ttise(str, sizeof(str));
   ttosf("\nRecord Number to erase (0 for sequential) : ");
   ttise(buf, 20);
   recno = atol(buf);
   ttosf("\nW(ait on locked record : ");
   *opt = '';
   ttise(opt, sizeof(opt));
   if(rerase(atoi(str),buf,BUFSIZ,&recno,4,opt) == -1)
      message("%sProblems with erase.  DSW = %06o\n", fatal, $dsw);
   else
      message("%sErase was successful\n", info);
   iosb();
   }

iosb()
   {
   printf("IOSB = [%06o %06o]\n", f_iosb[0], f_iosb[1]);
   }

message(msg)
   {
   printf("%r", &msg);
   }

quit()
   {
   printf("\n\n%sExiting\n", info);
   exit();
   }
