-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.c
153 lines (135 loc) · 3.62 KB
/
terminal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#define _XOPEN_SOURCE 600
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <stdint.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/select.h>
#include "list.h"
#include "client_include.h"
#include "sparkle.h"
struct termios orig;
void reset(){
tcsetattr(STDIN_FILENO, TCSANOW, &orig);
}
int main(){
// opening the pty
int fd = posix_openpt(O_RDWR | O_NOCTTY);
if(fd < 0){
perror("master open error\n");
exit(1);
}
if(grantpt(fd) < 0){
perror("grantpt error");
close(fd);
exit(1);
}
if(unlockpt(fd) < 0){
perror("unlockpt error");
close(fd);
exit(1);
}
char * name = ptsname(fd);
if(!name){
perror("ptsname error");
close(fd);
exit(1);
}
char buff[1024];
strcpy(buff, name);
struct winsize ws;
tcgetattr(STDIN_FILENO, &orig);
ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
int pid = fork();
if(pid < 0){
perror("fork error");
close(fd);
exit(1);
}
else if(pid != 0){
// master
// setting up the connection with the display manager
int handle = connect_to_server("127.0.0.1");
struct window * window = create_window(400, 400, handle);
if(window == NULL){
exit(1);
}
map_window(window, handle);
struct context * context = new_context(window->height, window->width, window->addr);
memset(window->addr, 255, window->size);
unsigned int * addr = (unsigned int *) window->addr;
char buffer[1024] = {0};
char command[1024];
sleep(1);
orig.c_lflag &= ~(ECHO|ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &orig);
atexit(reset);
fd_set fds;
FD_ZERO(&fds);
memset(buff, 0, 1024);
int pos = 0;
while(1){
FD_SET(fd, &fds);
FD_SET(STDIN_FILENO, &fds);
int ret = select(fd + 1, &fds, NULL, NULL, NULL);
if(FD_ISSET(fd, &fds)){
int rt = read(fd, buff + pos, 1024 - pos);
pos += rt;
//printf("%d\n", pos);
if(ret < 0){
exit(0);
}
//strcat(buffer, buff);
// for(int i=0;i<pos;i++){
// printf("%c", buff[i]);
// }
// fflush(stdout);
memset(window->addr, 255, window->size);
draw_text(context, buff, pos, 0, 0, 0x00000000);
//memset(buff, 0, 1024);
}
if(FD_ISSET(STDIN_FILENO, &fds)){
struct event * event = get_current_event(window, handle);
char c;
// if(event->event_bits & 1 << KEYBOARD_EVENT){
// c = to_char(event->key);
// if(c != 0){
// printf("got %d\n", c);
// write(fd, &c, 1);
//}
// }
ret = read(STDIN_FILENO, command, 1024);
write(fd, command, ret);
}
}
}
else{
// slave
if(setsid() < 0){
perror("setsid error");
close(fd);
exit(1);
}
close(fd);
int sfd = open(buff, O_RDWR);
if(sfd < 0){
perror("slave open error");
exit(1);
}
// orig.c_lflag &= ~ECHO;
tcsetattr(sfd, TCSANOW, &orig);
ioctl(sfd, TIOCSWINSZ, &ws);
dup2(sfd, 0);
dup2(sfd, 1);
dup2(sfd, 2);
execlp("/bin/sh", "/bin/sh", NULL);
printf("failed\n");
}
}