
#INCLUDE "course.module"
#INCLUDE "dynproc.module"

#INCLUDE "crashproto.inc"

PROC decode.result (VAL INT r, CHAN BYTE out)
  CASE r
    DPROCESS.FINISHED
      out.string ("process finished happily.*n", 0, out)
    DPROCESS.FAULTED
      out.string ("process caused a fault and was killed.*n", 0, out)
:

PROC do.network (VAL INT libhandle, CHAN BYTE out)

  INT res:
  SEQ

    out.string ("starting process and communicating empty1..*n", 0, out)
    CHAN CRASHPROTO c:
    PAR
      c ! empty1
      [1]CHAN ANY x.in RETYPES [c]:
      [1]CHAN ANY x.out RETYPES [out]:
      ccsp.run ("testcrash", libhandle, x.in, x.out, res)
    decode.result (res, out)

    out.string ("starting process and communicating array.bounds..*n", 0, out)
    CHAN CRASHPROTO c:
    PAR
      c ! array.bounds
      [1]CHAN ANY x.in RETYPES [c]:
      [1]CHAN ANY x.out RETYPES [out]:
      ccsp.run ("testcrash", libhandle, x.in, x.out, res)
    decode.result (res, out)

    out.string ("starting process and communicating int.overflow..*n", 0, out)
    CHAN CRASHPROTO c:
    PAR
      c ! int.overflow
      [1]CHAN ANY x.in RETYPES [c]:
      [1]CHAN ANY x.out RETYPES [out]:
      ccsp.run ("testcrash", libhandle, x.in, x.out, res)
    decode.result (res, out)

    out.string ("starting process and communicating fpu.error..*n", 0, out)
    CHAN CRASHPROTO c:
    PAR
      c ! fpu.error
      [1]CHAN ANY x.in RETYPES [c]:
      [1]CHAN ANY x.out RETYPES [out]:
      ccsp.run ("testcrash", libhandle, x.in, x.out, res)
    decode.result (res, out)

    out.string ("starting process and communicating empty2. (should never succeed)*n", 0, out)
    CHAN CRASHPROTO c:
    PAR
      c ! empty2
      [1]CHAN ANY x.in RETYPES [c]:
      [1]CHAN ANY x.out RETYPES [out]:
      ccsp.run ("testcrash", libhandle, x.in, x.out, res)
    decode.result (res, out)

    out.string ("oh dear..  that failed.*n", 0, out)

:

PROC testcrash (CHAN BYTE kyb, scr, err)
  INT handle:
  SEQ
    out.string ("about to start (deadlock termination is deliberate)*n", 0, scr)
    ccsp.openlib ("./crashlib.so", handle)
    IF
      handle = 0
        out.string ("unable to open crashlib.so :(*n", 0, scr)
      TRUE
        SEQ
          do.network (handle, scr)
          ccsp.closelib (handle)
    out.string ("all done*n", 0, scr)
:


