Fortran · 2065 bytes Raw Blame History
1 module pty_bindings
2 use, intrinsic :: iso_c_binding
3 implicit none
4 private
5
6 public :: c_pty_fork, c_pty_set_size, c_pty_read, c_pty_write
7 public :: c_pty_close, c_pty_child_alive, c_pty_get_child_pid
8
9 interface
10
11 ! Fork shell and return master fd (-1 on error)
12 integer(c_int) function c_pty_fork(shell, rows, cols) &
13 bind(C, name="fortty_pty_fork")
14 import :: c_int, c_char
15 character(kind=c_char), intent(in) :: shell(*)
16 integer(c_int), value :: rows, cols
17 end function c_pty_fork
18
19 ! Set PTY window size (0 on success, -1 on error)
20 integer(c_int) function c_pty_set_size(master_fd, rows, cols) &
21 bind(C, name="fortty_pty_set_size")
22 import :: c_int
23 integer(c_int), value :: master_fd, rows, cols
24 end function c_pty_set_size
25
26 ! Read from PTY (returns bytes read, 0 if would block, -1 on error/EOF)
27 integer(c_int) function c_pty_read(fd, buf, count) &
28 bind(C, name="fortty_pty_read")
29 import :: c_int, c_char
30 integer(c_int), value :: fd, count
31 character(kind=c_char), intent(out) :: buf(*)
32 end function c_pty_read
33
34 ! Write to PTY (returns bytes written, -1 on error)
35 integer(c_int) function c_pty_write(fd, buf, count) &
36 bind(C, name="fortty_pty_write")
37 import :: c_int, c_char
38 integer(c_int), value :: fd, count
39 character(kind=c_char), intent(in) :: buf(*)
40 end function c_pty_write
41
42 ! Close PTY and wait for child
43 subroutine c_pty_close(master_fd) bind(C, name="fortty_pty_close")
44 import :: c_int
45 integer(c_int), value :: master_fd
46 end subroutine c_pty_close
47
48 ! Check if child is alive (1 = alive, 0 = dead)
49 integer(c_int) function c_pty_child_alive() &
50 bind(C, name="fortty_pty_child_alive")
51 import :: c_int
52 end function c_pty_child_alive
53
54 ! Get child PID
55 integer(c_int) function c_pty_get_child_pid() &
56 bind(C, name="fortty_pty_get_child_pid")
57 import :: c_int
58 end function c_pty_get_child_pid
59
60 end interface
61
62 end module pty_bindings
63