Merge branch 'master' of git://localhost/acess2
[tpg/acess2.git] / Usermode / Libraries / libposix.so_src / termios.c
1 /*
2  * Acess2 POSIX Emulation
3  * - By John Hodge (thePowersGang)
4  *
5  * termios.c
6  * - Terminal Control
7  */
8 #include <termios.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <acess/sys.h>
12 #include <acess/devices/pty.h>
13
14 // === CODE ===
15 int tcgetattr(int fd, struct termios *termios_p)
16 {
17         if( fd == -1 ) {
18                 errno = EBADF;
19                 return -1;
20         }
21         
22         // Double-check `fd` describes a terminal
23         if( _SysIOCtl(fd, DRV_IOCTL_TYPE, NULL) != DRV_TYPE_TERMINAL ) {
24                 errno = EINVAL;
25                 return -1;
26         }
27         
28         // Fill defaults
29         memset(termios_p, 0, sizeof(struct termios));
30
31         // Query kernel for other params
32         struct ptymode  mode;
33         _SysIOCtl(fd, PTY_IOCTL_GETMODE, &mode);
34
35         if( (mode.OutputMode & PTYOMODE_BUFFMT) != PTYBUFFMT_TEXT ) {
36                 _SysDebug("Call to tcgetattr when terminal is not in text mode");
37                 return -1;
38         }
39
40         if(mode.InputMode & PTYIMODE_CANON)
41                 termios_p->c_lflag |= ICANON|ECHOE|ECHOK;
42         if(mode.InputMode & PTYIMODE_ECHO)
43                 termios_p->c_lflag |= ECHO;
44         // TODO: The more subtle flags
45
46         return 0;
47 }
48
49 int tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
50 {
51         _SysDebug("tcsetattr(%i, 0%o, %p)", fd, optional_actions, termios_p);
52
53         if( fd == -1 ) {
54                 errno = EBADF;
55                 return -1;
56         }
57
58         if( !termios_p || (optional_actions & ~(7)) ) {
59                 errno = EINVAL;
60                 return -1;
61         }
62         
63         // Double-check `fd` describes a terminal
64         if( _SysIOCtl(fd, DRV_IOCTL_TYPE, NULL) != DRV_TYPE_TERMINAL ) {
65                 errno = EINVAL;
66                 return -1;
67         }
68
69         struct ptymode  mode = {0,0};
70         
71         if(termios_p->c_lflag & ICANON)
72                 mode.InputMode |= PTYIMODE_CANON;
73         if(termios_p->c_lflag & ECHO)
74                 mode.InputMode |= PTYIMODE_ECHO;
75
76         _SysDebug("*termios_p = {%x,%x,%x,%x}",
77                 termios_p->c_iflag, termios_p->c_oflag, termios_p->c_cflag, termios_p->c_lflag);
78
79         return 0;
80 }
81

UCC git Repository :: git.ucc.asn.au