1 /* Copyright 2005 Renzo Davoli VDE-2
2 * Licensed under the GPLv2
4 * Minimal terminal emulator on a UNIX stream socket
15 #include <sys/socket.h>
18 #include <libvdehist.h>
21 static struct termios tiop
;
23 static void cleanup(void)
26 tcsetattr(STDIN_FILENO
,TCSAFLUSH
,&tiop
);
29 static void sig_handler(int sig
)
36 static void setsighandlers()
38 /* setting signal handlers.
39 * sets clean termination for SIGHUP, SIGINT and SIGTERM, and simply
40 * ignores all the others signals which could cause termination. */
41 struct { int sig
; const char *name
; int ignore
; } signals
[] = {
42 { SIGHUP
, "SIGHUP", 0 },
43 { SIGINT
, "SIGINT", 0 },
44 { SIGPIPE
, "SIGPIPE", 1 },
45 { SIGALRM
, "SIGALRM", 1 },
46 { SIGTERM
, "SIGTERM", 0 },
47 { SIGUSR1
, "SIGUSR1", 1 },
48 { SIGUSR2
, "SIGUSR2", 1 },
49 { SIGPROF
, "SIGPROF", 1 },
50 { SIGVTALRM
, "SIGVTALRM", 1 },
52 { SIGPOLL
, "SIGPOLL", 1 },
54 { SIGSTKFLT
, "SIGSTKFLT", 1 },
56 { SIGIO
, "SIGIO", 1 },
57 { SIGPWR
, "SIGPWR", 1 },
59 { SIGUNUSED
, "SIGUNUSED", 1 },
63 { SIGXCPU
, "SIGXCPU", 1 },
64 { SIGXFSZ
, "SIGXFSZ", 1 },
70 for(i
= 0; signals
[i
].sig
!= 0; i
++)
71 if(signal(signals
[i
].sig
,
72 signals
[i
].ignore
? SIG_IGN
: sig_handler
) < 0)
73 fprintf(stderr
,"Error setting handler for %s: %s\n", signals
[i
].name
,
78 static char *copy_header_prompt (int vdefd
,int termfd
,char *sock
)
84 struct pollfd wfd
={vdefd
,POLLIN
|POLLHUP
,0};
86 while ((n
=read(vdefd
,buf
,BUFSIZE
))>0) {
91 while (n
>0 && buf
[n
] !='\n')
93 write(termfd
,buf
,n
+1);
94 asprintf(&prompt
,"%s[%s]: ",buf
+n
+1,sock
);
102 int main(int argc
,char *argv
[])
104 struct sockaddr_un sun
;
108 struct termios newtiop
;
109 static struct pollfd pfd
[]={
110 {STDIN_FILENO
,POLLIN
| POLLHUP
,0},
111 {STDIN_FILENO
,POLLIN
| POLLHUP
,0}};
112 //static int fileout[]={STDOUT_FILENO,STDOUT_FILENO};
113 struct vdehiststat
*vdehst
;
116 sun
.sun_family
=PF_UNIX
;
117 snprintf(sun
.sun_path
,sizeof(sun
.sun_path
),"%s",argv
[1]);
118 //asprintf(&prompt,"vdterm[%s]: ",argv[1]);
119 if((fd
=socket(PF_UNIX
,SOCK_STREAM
,0))<0) {
120 perror("Socket opening error");
123 if ((rv
=connect(fd
,(struct sockaddr
*)(&sun
),sizeof(sun
))) < 0) {
124 perror("Socket connecting error");
127 tcgetattr(STDIN_FILENO
,&tiop
);
129 newtiop
.c_cc
[VMIN
]=1;
130 newtiop
.c_cc
[VTIME
]=0;
131 newtiop
.c_lflag
&= ~ICANON
;
132 newtiop
.c_lflag
&= ~ECHO
;
133 tcsetattr(STDIN_FILENO
,TCSAFLUSH
,&newtiop
);
134 flags
= fcntl(fd
, F_GETFL
);
136 fcntl(fd
, F_SETFL
, flags
);
138 prompt
=copy_header_prompt(fd
,STDOUT_FILENO
,argv
[1]);
139 vdehst
=vdehist_new(STDIN_FILENO
,fd
);
140 write(STDOUT_FILENO
,prompt
,strlen(prompt
)+1);
143 //printf("POLL %d %d\n",pfd[0].revents,pfd[1].revents);
144 if(pfd
[0].revents
& POLLHUP
||
145 pfd
[1].revents
& POLLHUP
)
147 if(pfd
[0].revents
& POLLIN
) {
148 if (vdehist_term_to_mgmt(vdehst
) != 0)
151 if(pfd
[1].revents
& POLLIN
)
152 vdehist_mgmt_to_term(vdehst
);
153 //printf("POLL RETURN!\n");