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

PROC runprimes (CHAN OF BYTE out, VAL INT libhandle)

  PROC convert (CHAN OF INT in, CHAN OF BYTE byte.out)
    INT tmp:
    WHILE TRUE
      SEQ
        in ? tmp
        out.int (tmp, 0, byte.out)
        byte.out ! '*n'
  :

  CHAN OF INT x:
  PAR
    convert (x, out)
    [1]CHAN OF ANY tmp.in:
    CHAN OF ANY tmp.x RETYPES x:
    INT res:
    ccsp.run ("sieve", libhandle, tmp.in, [tmp.x], res)
:

PROC primes (CHAN OF BYTE kyb, scr, err)
  INT libhandle:
  SEQ
    ccsp.openlib ("./sieve.so", libhandle)
    IF
      libhandle = 0
        out.string ("unable to open ./sieve.so :(*n", 0, scr)
      TRUE
        SEQ
          runprimes (scr, libhandle)
          ccsp.closelib (libhandle)
:


