
REM This program is free software; you can redistribute it and/or modify it
REM under the terms of the GNU General Public License as published by the Free
REM Software Foundation; either version 2 of the License, or (at your option)
REM any later version.
REM
REM This program is distributed in the hope that it will be useful, but WITHOUT
REM ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
REM FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more
REM details.
REM
REM You should have received a copy of the GNU General Public License along with
REM this program; if not, write to the Free Software Foundation, Inc., 59 Temple
REM Place - Suite 330, Boston, MA 02111-1307, USA


DIM block% 1023,syntax% 255,output% 255:app$="Director:TasksCmd"
quit%=FALSE:SYS "Wimp_Initialise",310,&4B534154,app$ TO ,task%
ON ERROR PROCerror(REPORT$+" at line "+STR$ ERL):A=GET:END

REM Get command line
SYS "OS_GetEnv" TO comm%
SYS "OS_ReadArgs","name,quit,app/ka,handle/ka,quitapp/s",comm%,block%,1023
appname$=FNstring(block%!8)
apphandle%=EVAL(FNstring(block%!12))

REM Quit application
quitapp%=block%!16
IF quitapp%<>0 THEN PROCquitapp

REM Quit
SYS "Wimp_CloseDown"
END

DEF PROCquitapp
block%!0=24
block%!16=8:REM Message_PreQuit
block%!20=-1:REM Flag - single task
SYS "Wimp_SendMessage",18,block%,apphandle%
PROCpoll
ENDPROC

DEF PROCpoll
REPEAT
  SYS "Wimp_Poll",,block% TO reason%
  CASE reason% OF
    WHEN 0: quit%=TRUE:PROCmessage_ack
    WHEN 18: PROCmessage_rec
    WHEN 0,19: PROCmessage_ack
  ENDCASE
UNTIL quit%=TRUE
ENDPROC

DEF PROCmessage_rec
IF quitapp% quit%=TRUE:REM Objected to being quit
ENDPROC

DEF PROCmessage_ack
IF quitapp% THEN
  block%!0=20
  block%!16=0:REM Message_Quit
  SYS "Wimp_SendMessage",17,block%,apphandle%
  quit%=TRUE
ENDIF
ENDPROC

DEF FNstring(ptr%):LOCAL a$:a$=""
WHILE ?ptr%>31
  a$+=CHR$(?ptr%):ptr%+=1
ENDWHILE:=a$

DEF FNreport(err$,flag%)
LOCAL name$,flag%
IF flag% AND 16 name$="Message from '"+app$+"'" ELSE name$=""
!block%=255
$(block%+4)=err$+CHR$0
SYS "Wimp_ReportError",block%,flag%,name$ TO ,errclick%
=errclick%

DEF PROCerror(err$)
ON ERROR REPORT:PRINT ERL:A=GET:END
ans%=FNreport(err$,1)
SYS "Wimp_CloseDown"
ENDPROC
