-- Copyright (C) 1986  by Xerox Corporation. All rights reserved. 
-- CWindowLibImpl.mesa
-- NFS   31-Jan-86 10:05:14

DIRECTORY
  CEnvUtil USING[IsItDefaultStdin, SetOutputMode, StdinHandle, SystemCall, zone],
  CIOLib USING[FilePtr],
  CRuntime USING [ConfigHandle,GetConfigHandle,  GetStdout, SetConfigHandle,
    ProcessNotRegistered, StopIfUserAborted],
  CString USING [CString, CStringToLongString],
  CWindowLib USING [sgttyb, TIOCGETP, TIOCSETP];
CWindowLibImpl:PROGRAM
  IMPORTS CEnvUtil, CRuntime, CString
  EXPORTS CWindowLib = {

  failure:INTEGER = -1;
  success:INTEGER = 0;
  
  ioctl:PUBLIC PROCEDURE[
    stream:CIOLib.FilePtr, request:INTEGER,argp:LONG POINTER TO CWindowLib.sgttyb]
    RETURNS[INTEGER ← success] = {
    IF ~CEnvUtil.IsItDefaultStdin[stream↑] THEN RETURN[failure];
    SELECT request FROM
      CWindowLib.TIOCGETP => argp↑ ← LOOPHOLE[stream↑,CEnvUtil.StdinHandle].mode;
      CWindowLib.TIOCSETP => LOOPHOLE[stream↑,CEnvUtil.StdinHandle].mode ← argp↑;
      ENDCASE => RETURN[failure];
    IF stream = CRuntime.GetStdout[] THEN 
      CEnvUtil.SetOutputMode[stream↑.clientData, argp↑];
   };
   
   system:PUBLIC PROCEDURE[string:CString.CString] RETURNS[INTEGER] = {
     mString:LONG STRING ← CString.CStringToLongString[string,CEnvUtil.zone];
     exitCode:INTEGER;
     cH:CRuntime.ConfigHandle ← NIL;
     CRuntime.StopIfUserAborted[];  -- since user abort will be reset.
     cH ← CRuntime.GetConfigHandle[!CRuntime.ProcessNotRegistered => CONTINUE];
     exitCode ← SIGNAL CEnvUtil.SystemCall[mString];
     IF cH # NIL THEN CRuntime.SetConfigHandle[cH];
     CEnvUtil.zone.FREE[@mString];
     RETURN[exitCode];
   };

}.