preamble
  normally mode is undefined
 
  define .TRUE          to mean 1
  define .FALSE         to mean 0 
  define .OSOK          to mean 0
  define .OSERROR       to mean -1

    ''---- the helper functions
  define LISTDIR        as routine given 1
  define WRITETS        as routine given 2
  define WRITETRC       as routine given 3


    ''-- define all as normal functions

    ''------ test 1 -------------
  define OS.GET.OSTYPE.F        as text function given 0
  define OS.GET.WINDOWSYSTYPE.F as text function given 0
  define OS.GET.COMPUTERTYPE.F  as text function given 0
  define OS.MAX.FNAMELEN.F      as integer function given 0
  define OS.GET.HOSTNAME.F      as text function given 0
  define OS.GET.ENVVAR.F        as text function given 1
  define OS.GET.CWD.F           as text function given 0
  define OS.GET.CURDRIVE.F      as text function given 0
  define OS.GET.PROGDIR.F       as text function given 1
  define OS.SYSTEMTIME.F        as integer function given 0
  define OS.TIME2STRING.F       as text function given 1

  define OS.VIEWHELP.F          as integer function given 2

    ''-------- test 2 ---------
  define OS.CHANGE.DIR.F        as integer function given 1
  define OS.MAKE.DIR.F          as integer function given 1
  define OS.REMOVE.DIR.F        as integer function given 1
  define OS.FILE.EXISTS.F       as integer function given 1
  define OS.DELETE.FILE.F       as integer function given 1
  define OS.RENAME.FILE.F       as integer function given 2
  define OS.GET.DIRSEPCHAR.F    as alpha function given 0

  define OS.OPEN.DIR.F          as pointer function given 1
  define OS.NEXT.DIRENTRY.F     as text function given 1
  define OS.CLOSE.DIR.F         as integer function given 1

  define OS.FILE.TYPE.F         as integer function given 1
  define OS.TEST.ACCESS.F       as integer function given 2
  define OS.FILE.MODTIME.F       as integer function given 1
  define OS.FILE.ACCESSTIME.F    as integer function given 1
  define OS.FILE.SIZE.F          as integer function given 1

  define OS.MAKE.EMPTY.FILE.F   as integer function given 1
  define OS.MAKE.TMP.FILE.F     as text function given 1
  define OS.COPY.FILE.F          as integer function given 2
  define OS.MATCHES.FILEPATTERN.F as integer function given 2
  define OS.IS.LEGAL.FNAMECHAR.F as integer function given 1
  define OS.INCR.FNAME.IFEX.F   as text function given 1
  define OS.MAP2LEGALFNAME.F    as text function given 1
  define OS.APPEND.SLASH.F      as text function given 1
  define OS.REMOVE.FINAL.SLASH.F as text function given 1
  define OS.DIRNAME.F           as text function given 1
  define OS.BASENAME.F          as text function given 1

    ''------ test 3
  define OS.SYSTEMCALL.F        as integer function given 2
  define OS.START.BGTASK.F      as integer function given 2
  define OS.CHECK.BGTASK.R      as routine yielding 3
  define OS.KILL.BGTASK.F       as integer function given 1
  define OS.GET.PID.F           as integer function given 0

    ''------ test 4
  define OS.QUOTE.STRING.F      as text function given 1


    ''---- system stuff
  define UNIX.STRING.F          as text function given 1

end
main
  ''***************************************************************************
  ''** SIMSCRIPT OS Interface test program 
  ''**
  ''***************************************************************************

define TESTN as integer variable

TESTN = 1

while TESTN <> 0
do
  write as "------- SIMSCRIPT OS INTERFACE TEST ----------------",/
  write as /
  write as "Select set of test calls:",/
  write as /
  write as "  1. OS queries",/
  write as "  2. Files and Directories",/
  write as "  3. Processes",/
  write as "  4. Utilities",/
  write as "  5. Direct calls",/,/
  write as "  9. All automatic tests",/,/
  write as "---> Choose number (0=exit) ",+
  read TESTN
  write as /

  select case TESTN
    case 0
        ''-- just ignore
    case 1  ''------------ OS queries ---------------------
        call TEST1
    case 2  ''------------ Files and directories ----------
        call TEST2
    case 3  ''------------ Processes ----------------------
        call TEST3
    case 4  ''------------ Utilities ----------------------
        call TEST4
    case 5  ''------------ Direct calls -------------------
        call TEST5
    case 9  ''------------ All automatic ------------------
        call TEST1
        call TEST2
        call TEST3
        call TEST4
    default
        write as "**** Improper test number ***",/
  endselect
loop

end
routine TEST4
  write as "---- Test 4: ",/


end
routine TEST1
  define TSECS as integer variable

  write as "---- Test 1: ",/
  write OS.GET.OSTYPE.F as              "OS Type        = ", T *, /
  write OS.GET.WINDOWSYSTYPE.F as       "WS Type        = ", T *, /
  write OS.GET.COMPUTERTYPE.F as        "Comp. Type     = ", T *, /
  write OS.MAX.FNAMELEN.F as            "MaxFnameLen    = ", I 4, /
  write OS.GET.HOSTNAME.F as            "hostname       = ", T *, /
  write OS.GET.ENVVAR.F("PATH") as      "envvar(PATH)   = ", T *, /
  write OS.GET.ENVVAR.F("SIMHOME") as   "envvar(SIMHOME)= ", T *, /
  write OS.GET.ENVVAR.F("BLABLA") as    "envvar(BLABLA) = ", T *, /
  write OS.GET.CWD.F as                 "cwd            = ", T *, /
  write OS.GET.CURDRIVE.F as            "current drive  = ", T *, /
  write OS.GET.PROGDIR.F("testosi") as  "progdir(testosi)= ", T *, /
  write OS.GET.PROGDIR.F("cc") as       "progdir(cc)  = ", T *, /

  TSECS = OS.SYSTEMTIME.F 
  write TSECS as                        "system time    = ", I 12, /
  write OS.TIME2STRING.F(TSECS) as      "string time    = ", T *,/

end
routine WRITETRC(TEXT, RC, OKRC)
  define RC, OKRC as integer variable
  define TEXT as text variable

  write TEXT, RC as T 24, " = ", I 4,/
  if RC <> OKRC
    write as "    **** ERROR ****",/
  endif
end
routine TEST2
''***  define A1 as alpha variable
  define S1, S2, S3 as text variable

  write as "---- Test 2: ",/,/
''***  A1 = OS.GET.DIRSEPCHAR.F
''***  S1 = atot.f(A1)
''***  call WRITETS("dirsepchar", S1)
  call WRITETS("cwd", OS.GET.CWD.F)
  call WRITETRC("mkdir(testdir1)", OS.MAKE.DIR.F("testdir1"), .OSOK)
  call WRITETRC("chdir(testdir1)", OS.CHANGE.DIR.F("testdir1"), .OSOK)
  call WRITETS("cwd", OS.GET.CWD.F)
  call WRITETRC("fileexists(junk)", OS.FILE.EXISTS.F("junk"), .FALSE) 
  call WRITETRC("mkemptyfile(junk)", OS.MAKE.EMPTY.FILE.F("junk"), .OSOK)
  call WRITETRC("fileexists(junk)", OS.FILE.EXISTS.F("junk"), .TRUE)  

    ''- make some temp files
  S1 = OS.MAKE.TMP.FILE.F("")
  S2 = OS.MAKE.TMP.FILE.F("")
  S3 = OS.MAKE.TMP.FILE.F("")
  call WRITETS("mktmpfile()", S1 )
  call WRITETS("mktmpfile()", S2 )
  call WRITETS("mktmpfile()", S3 )

  call LISTDIR("")
  call WRITETRC("removefile(TF1)", OS.DELETE.FILE.F(S1), .OSOK)
  call WRITETRC("removefile(TF2)", OS.DELETE.FILE.F(S2), .OSOK)
  call WRITETRC("removefile(TF3)", OS.DELETE.FILE.F(S3), .OSOK)

  call WRITETRC("testaccess(junk,0)", OS.TEST.ACCESS.F("junk", 0), .TRUE)
  call WRITETRC("testaccess(junk,1)", OS.TEST.ACCESS.F("junk", 1), .TRUE)
  call WRITETRC("testaccess(junk,2)", OS.TEST.ACCESS.F("junk", 2), .TRUE)
  call WRITETRC("testaccess(junk,3)", OS.TEST.ACCESS.F("junk", 3), .TRUE)
  call WRITETRC("testaccess(junk,4)", OS.TEST.ACCESS.F("junk", 4), .TRUE)
  call WRITETRC("testaccess(junk1,0)", OS.TEST.ACCESS.F("junk1", 0), .FALSE)

  call WRITETRC("removefile(junk)", OS.DELETE.FILE.F("junk"), .OSOK)
  call WRITETRC("chdir(..)", OS.CHANGE.DIR.F(".."), .OSOK)
  call WRITETRC("rmdir(testdir1)", OS.REMOVE.DIR.F("testdir1"), .OSOK)
  call WRITETS("cwd", OS.GET.CWD.F)

  ''*** call LISTDIR("")

  call WRITETS("appendslash(f:\osi)", OS.APPEND.SLASH.F("f:\osi") )
  call WRITETS("appendslash(f:\osi\)", OS.APPEND.SLASH.F("f:\osi\") )
  call WRITETS("removeslash(f:\osi)", OS.REMOVE.FINAL.SLASH.F("f:\osi") )
  call WRITETS("removeslash(f:\osi\)", OS.REMOVE.FINAL.SLASH.F("f:\osi\") )

  ''call WRITETS("dirname(f:\osi\objss)", OS.DIRNAME.F("f:\osi\objss") )
  ''call WRITETS("basename(f:\osi\objss)", OS.BASENAME.F("f:\osi\objss") )

  call WRITETS("dirname(/simc/simc.test)", OS.DIRNAME.F("/simc/simc.test") )
  call WRITETS("basename(/simc/simc.test)", OS.BASENAME.F("/simc/simc.test") )


end
routine WRITETS(TEXT, S1)
  define S1, TEXT as text variables
  write TEXT, S1 as T 25, " = ", T *,/
end
routine TEST5
  define S1 as text variable
  define TESTN as integer variable

  write as "---- Test 5: ",/,/

  TESTN = 1
  while TESTN <> 0 
  do
    write as "0  - Exit",/
    write as "1  - List directory",/
    write as "2  - Viewhelp",/
    write as /,"Enter test number: ", +
    read TESTN
    select case TESTN
      case 1
        write as "Enter directory to be listed: ",+
        read S1
        call LISTDIR(S1)
      case 2
	write as "Not implemented in UNIX ...",/

        ''write as "Enter DB path : ", +
        ''read S1
        ''call WRITETRC("viewhelp", OS.VIEWHELP.F(S1, 0), .OSOK)
      default
    endselect  
  loop


end
routine LISTDIR(DIRNAME)
  define DIRNAME as text variable
  define P1 as pointer variable
  define S1 as text variable
  define F as text variable

  P1 = OS.OPEN.DIR.F(DIRNAME)        ''-- current directory
  write DIRNAME, P1 as "----- Reading directory: ", T *, " (P1 = ", C 8, ")",/
  if P1 = 0
    write as "**** ERROR ****",/
    return
  endif
  S1 = OS.NEXT.DIRENTRY.F(P1)
  while S1 <> ""
  do
    F = concat.f(OS.APPEND.SLASH.F(DIRNAME), S1)
    write S1 ,OS.FILE.TYPE.F(F), OS.FILE.SIZE.F(F),
          OS.FILE.MODTIME.F(F), OS.FILE.ACCESSTIME.F(F) 
      as T 15, I 4, I 10, I 12, I 12, /
    S1 = OS.NEXT.DIRENTRY.F(P1)
  loop
  write as /
  call WRITETRC("closedir(P1)", OS.CLOSE.DIR.F(P1), .OSOK) 

end
routine TEST3
  define rc as integer variable
  define exitcode, status as integer variables

  write as "---- Test 3: ",/

  rc = OS.SYSTEMCALL.F("retrc 3",0)
  write rc as "system(retrc 3) = ", I 4, /

  rc = OS.SYSTEMCALL.F("retrc 2",0)
  write rc as "system(retrc 2) = ", I 4, /


  write as "now in parallel ...",/

  rc = OS.START.BGTASK.F("retrc 2 5",0)
  write rc as "start(retrc 2 5). pid  = ", I 6, /
  rc = OS.START.BGTASK.F("retrc 3 7",0)
  write rc as "start(retrc 3 7). pid  = ", I 6, /
  rc = OS.START.BGTASK.F("retrc 4 4",0)
  write rc as "start(retrc 4 4). pid  = ", I 6, /

  rc = 0
  while rc <> -1
  do 
    call OS.CHECK.BGTASK.R yielding rc, exitcode, status    
        ''-- returns -1 when last child exited
    if rc > 0          ''-- did a child exit ?
      write rc, exitcode, status
        as I 5, " terminated with retcode = ", I 5, ", and status = ", I 5,/
    endif
  loop

end
