/*
   <p>Test harness for <i>setpause()</i>/</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include "environ.h"

#ifdef LINUX
#include <stdlib.h>
#endif

char *op[] = { "P_RECORD", "P_PAUSE", "P_NOPAUSE", "P_RESTORE", "bad op" };

void checkreply(int r, int a, int p)
{
    int    i;

    i = a-1;
    if (i>3 || i<0)
        i = 4;
        
    printf("Reply for operation %d (%s) on pathno %d is %d\n",
            a, op[i], p, r);
    if (r<0)
    {
        printf("Abandoned\n");
        exit(1);
    }
}

int main(int argc, char **argv)
{
    int     i, r, p;
    char    buf[80];

    setprog(argv[0]);
    
    if (argc<2)
    {
        printf("Piss OFF!\n");
        exit(1);
    }
    r = setpause(P_RECORD,1);
    checkreply(r,P_RECORD,1);

    if (strcmp(argv[1],"pause")==0)
    {
        r = setpause(P_PAUSE,1);
        checkreply(r,P_PAUSE,1);
        for (i=1; i<30; i++)
            printf("Paused line %d\n",i);
        r = setpause(P_RESTORE,1);
        checkreply(r,P_RESTORE,1);
    }
    else
    if (strcmp(argv[1],"nopause")==0)
    {
        r = setpause(P_NOPAUSE,1);
        checkreply(r,P_NOPAUSE,1);
        for (i=1; i<30; i++)
            printf("Un-paused line %d\n",i);
        r = setpause(P_RESTORE,1);
        checkreply(r,P_RESTORE,1);
    }
    else
    if (strcmp(argv[1],"bad")==0)
    {
        i = P_RESTORE*10;
        r = setpause(i,1);
        checkreply(r,i,1);
        if (r > 0)
            exit(0);
        r = setpause(P_RESTORE,1);
        checkreply(r,P_RESTORE,1);
    }
    else
    if (strcmp(argv[1],"file")==0)
    {
        p = creat("testpause.txt",(S_IREAD|S_IWRITE));
        if (p < -1)
            error(argv[0],"can't open","testpause.txt");
        r = setpause(P_NOPAUSE,p);
        checkreply(r,P_NOPAUSE,p);
        for (i=1; i<30; i++)
        {
            sprintf(buf,"Record no %d\n",i);
            write(p,buf,strlen(buf));
        }
        r = setpause(P_RESTORE,p);
        checkreply(r,P_RESTORE,p);
        close(p);
    }
    else
        printf("Bad arg; use pause,nopause,bad,file\n");

    exit(0);
}

