/*
   <p>Machine-dependent code to handle paging terminal output.</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 <errno.h>
#include "environ.h"

/*
   This is machine-dependent code to handle paging terminal output.
   
   In the OS-9 environment, where the terminal drivers will handle
   display pagination if so requested, this function sets the terminal
   to the pagination method requested in <u>action</u> for the requested
   <u>pathno</u>. It returns -1 if it failed to retrieve or modify 
   the i/o manager options, E_BTYP if the path is not connected to an SCF
   (terminal) device and E_ILLARG if the value of <u>action</u> is invalid.
   Zero is returned if the request was successfully actioned. Valid
   values for <u>action</u> are:
   <ul>
   <li>P_RECORD - save the current pagination state</li>
   <li>P_PAUSE - set the pagination state to pause after each screenful</li>
   <li>P_NOPAUSE - turn off screen pagination</li>
   <li>P_RESTORE - restore the pagination state recorded by P_RECORD.
       No checks are made that P_RECORD was in fact called</li>
   </ul>

   The function does nothing in a Linux or MS-DOS environment because
   neither operating system's terminal device drivers support output
   pagination. These versions only exist to ensure program portability.
*/

#ifdef OS9

#define _SCF_CLASS    0
#include <sgstat.h>

int _stored_state;

int setpause(action,pathno)
int action;
int pathno;
{
    struct sgbuf buffer;

    if (_gs_opt(pathno, &buffer) == -1)
        return(-1);     /* return with error if _gs_opt fails    */

    if (buffer.sg_class != _SCF_CLASS)
        return(E_BTYP); /* exit if not SCF device */
        
    switch(action)
    {
        case P_RECORD:    _stored_state = buffer.sg_pause;
                        break;

        case P_PAUSE:    buffer.sg_pause = 1;
                        break;

        case P_NOPAUSE:    buffer.sg_pause = 0;
                        break;

        case P_RESTORE:    buffer.sg_pause = _stored_state;
                        break;

        default:        return(E_ILLARG);
    }
    
    if (_ss_opt(pathno, &buffer) == -1)
        return(-1);                /* return with error if _gs_opt fails    */
    else
        return(0);                /* success                                */
}
#endif

#ifdef MSDOS

int setpause(int action, int pathno)
{
    return(0);
}

#endif

#ifdef LINUX

int setpause(int action, int pathno)
{
    return(0);
}

#endif

