Terminal emulator issue

Linux specific questions.
Post Reply
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Terminal emulator issue

Post by nimdays »

I read some tuts and then i tried this small one.
The problem is i must press enter after ctrl-z,c
Maybe you have a solution for this.

Code: Select all

#include "crt/unistd.bi"
#include "crt/sys/select.bi"
#inclib "util"

#define SIGINT 2
#define SIGQUIT 3
#define SIGTSTP 20

type sighandler as sub(byval as integer)

declare function forkpty alias"forkpty"(byval as integer ptr,byval as zstring ptr,_
        byval as any ptr,byval as any ptr)as integer

declare function signal alias"signal"(byval as integer,byval as sighandler)as sighandler

dim shared as integer m
static as pid_t pid

sub mysig(byval sig as integer) 
   ?"signal " & sig & " caught"
   dim as byte c
   if sig = 2 then 
     c = &h3
   elseif sig = 3 then
     c = &h1c
   else
     c = &h1a
   end if
   write_(m, @c, 1)
end sub

pid = forkpty(@m,0,0,0)
if pid=0 then
   execlp("/bin/sh","/bin/sh",0)
   end
end if

signal(SIGINT,@mysig) 'ctr-c
signal(SIGQUIT,@mysig) 'ctr-\
signal(SIGTSTP,@mysig) 'ctr-z

dim as fd_set fd_in
dim as integer rc
dim as zstring * 1024 buf

?"parent : " &getpid() & " child : " &pid

while (1)
   
   FD_ZERO(@fd_in)
   FD_SET_(0,@fd_in)
   FD_SET_(m, @fd_in)
   
   rc = select_(m + 1, @fd_in,0,0, 0)
   if (FD_ISSET(0, @fd_in)) then
      rc = read_(0, @buf, sizeof(buf))
      write_(m, @buf, rc)
   elseif (FD_ISSET(m, @fd_in)) then
      rc = read_(m, @buf, sizeof(buf))
      write_(1, @buf, rc)
   end if
   
wend
This is the output :

Code: Select all

tc@box:/tmp$ ./term1
parent : 6552 child : 6553
tc@box:/tmp$ cat
a
a
signal 20 caught
^Z[1]+  Stopped                    cat
tc@box:/tmp$ 
tc@box:/tmp$ fg
cat
signal 2 caught
^C
tc@box:/tmp$ 
tc@box:/tmp$ Terminated
killed from another window
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Terminal emulator issue

Post by nimdays »

I found a sample from qb64 forum.
http://www.qb64.net/forum/index.php?topic=12159.0

Maybe can fix this.
Post Reply