staload UNSAFE = "prelude/SATS/unsafe.sats"
staload _ = "prelude/DATS/pointer.dats"
staload T = "libc/sys/SATS/types.sats"
typedef time_t = $T.time_t
typedef suseconds_t = $T.suseconds_t
staload "libc/SATS/errno.sats"
staload "libc/SATS/fcntl.sats"
staload "libc/SATS/signal.sats"
staload "libc/SATS/stdio.sats"
staload "libc/SATS/stdlib.sats"
staload "libc/SATS/unistd.sats"
staload "libc/sys/SATS/time.sats"
fun fprint_bytes_size
{n1,n2:nat | n2 <= n1}
(out: FILEref, buf: &(@[byte][n1]), n2: size_t n2): void = let
val p_buf = &buf
prval () = eqsize_byte_one () prval pfmul = mul_make {n2,sizeof(byte)} ()
prval () = mul_elim {n2,1} (pfmul)
prval (pf1, pf2) = array_v_split {byte} {n1,n2} (pfmul, view@ (buf))
prval pfu = unit_v ()
typedef env = FILEref
val () = array_ptr_foreach_fun_tsz__main {byte} {unit_v} {env}
(pfu | !p_buf, lam (pf | x, out) =<> $effmask_ref (fprint_byte (out, x)), n2, sizeof<byte>, out)
prval unit_v () = pfu
prval () = view@ (buf) := array_v_unsplit {byte} {n2,n1-n2} (pfmul, pf1, pf2)
in
end
fun print_bytes_size
{n1,n2:nat | n2 <= n1}
(buf: &(@[byte][n1]), n2: size_t n2): void = fprint_bytes_size (stdout_ref, buf, n2)
val theMsg = "Hello for the timer!\n"
val theMsgLen = string_length (theMsg)
fun test_timer (): void = let
var act: sigaction?
val () = ptr_zero<sigaction> (act)
fun handler (sgn: signum_t): void = let
val (pf_stdout | ()) = stdout_fildes_view_get ()
val _err = write_substring_err (pf_stdout | STDOUT_FILENO, theMsg, 0, theMsgLen)
val () = stdout_fildes_view_set (pf_stdout | )
in
end val () = act.sa_flags := SA_RESTART val () = act.sa_handler := sighandler(handler)
val err = sigaction_null (SIGALRM, act)
val () = assertloc (err = 0)
var itv: itimerval?
val _2 = $UNSAFE.cast{time_t} (2)
val () = ptr_zero<itimerval> (itv)
val () = itv.it_interval.tv_sec := _2; val () = itv.it_value.tv_sec := _2
val err = setitimer_null (ITIMER_REAL, itv)
val () = assertloc (err = 0)
#define BUFSZ 128
var !p_buf with pf_buf = @[byte][BUFSZ]()
prval () = pf_buf := bytes_v_of_b0ytes_v (pf_buf)
var nerr: int = 0
val () = while (true) let
val (pf_stdin | ()) = stdin_fildes_view_get ()
val nread = read_err (pf_stdin | STDIN_FILENO, !p_buf, BUFSZ)
val () = stdin_fildes_view_set (pf_stdin | )
in
if nread >= 0 then let
val nread = size1_of_ssize1 (nread)
in
if nread > 0 then let
val () = print_bytes_size (!p_buf, nread)
in
continue
end else (
printf ("EOF\n", @()); break
) end else let
val () = perror "read" in nerr := nerr + 1; break
end end val () = if (nerr > 0) then exit (EXIT_FAILURE)
in
end
implement
main () = () where {
val () = test_timer ()
}