Debian Codegraph @

fltk/test/tty.py

raw symbols SCIP
1 #
2 # "$Id: adjuster.py 35 2003-09-29 21:39:48Z andreasheld $"
3 #
4 # Adjuster test program for pyFLTK the Python bindings
5 # for the Fast Light Tool Kit (FLTK).
6 #
7 # FLTK copyright 1998-1999 by Bill Spitzak and others.
8 # pyFLTK copyright 2003 by Andreas Held and others.
9 #
10 # This library is free software you can redistribute it and/or
11 # modify it under the terms of the GNU Library General Public
12 # License, version 2.0 as published by the Free Software Foundation.
13 #
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Library General Public License for more details.
18 #
19 # You should have received a copy of the GNU Library General Public
20 # License along with this library if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 # USA.
23 #
24 # Please report all bugs and problems to "[email protected]".
25 #
26
27 from fltk import *
28 import sys, os, platform
29
30 #
31 # fltk-ttys - Open several processes, display their output in fltk widgets
32 # Greg Ercolano 02/21/2005 1.00
33 #
34
35 # Globals
36 G_disp = [0,0,0]; # one display per child
37 G_buff = [0,0,0]; # one buffer per child
38 G_outfd = [0,0,0]; # read pipe for childs stderr, one per child
39 G_pids = [0,0,0] # pid for each child
40
41 # Start child process, makes a read pipe to its stderr
42 def start_child(t):
43 global G_pids
44 #int out[2]; pipe(out);
45 out = os.pipe()
46 G_pids[t] = os.fork()
47 print("pid = ", G_pids[t])
48 #G_pids[t] = -1
49 if G_pids[t] == -1:
50 # error
51 os.close(out[0])
52 os.close(out[1])
53 elif G_pids[t] == 0:
54 # child
55 os.close(out[0])
56 os.dup2(out[1],2)
57 os.close(out[1])
58 if t == 0:
59 os.execlp("/bin/sh", "sh", "-c", "ps auxww 1>&2")
60 print("execlp(ps)")
61 #perror("execlp(ps)")
62 sys.exit(1)
63 elif t == 1:
64 os.execlp("/bin/sh", "sh", "-c", "perl -e 'for($t=0; sleep(1); $t++) {print STDERR rand().\"\\n\"; if ($t>5) {kill(9,$$);}}' 1>&2")
65 print("execlp(perl)")
66 #perror("excelp(perl)")
67 sys.exit(1)
68 elif t == 2:
69 os.execlp("/bin/sh", "sh", "-c", "(ls -la; ping -c 8 localhost) 1>&2")
70 #perror("execlp(ls/ping)")
71 print("execlp(ls/ping)")
72 sys.exit(1)
73 else:
74 # parent
75 G_outfd[t] = out[0]
76 os.close(out[1])
77
78
79 # Data ready interrupt
80 def data_ready(fd, data):
81 t = data
82 s = ""
83 bytes = os.read(fd, 4096-1);
84 if len(bytes) == 0: # EOF
85 G_buff[t].append("\n\n*** EOF ***\n")
86 try:
87 (pid,status) = os.waitpid(G_pids[t], os.WNOHANG)
88 except:
89 bytes = "waitpid(): no child process\n"
90 else:
91 if os.WIFEXITED(status):
92 bytes = f"Exit={os.WEXITSTATUS(status)}\n"
93 os.close(fd)
94 Fl.remove_fd(fd)
95 G_pids[t] = -1
96 elif os.WIFSIGNALED(status):
97 bytes = f"Killed with {os.WTERMSIG(status)}\n"
98 os.close(fd)
99 Fl.remove_fd(fd)
100 G_pids[t] = -1
101 elif os.WIFSTOPPED(status):
102 bytes = f"Stopped with {os.WSTOPSIG(status)}\n"
103
104 G_buff[t].append(bytes)
105 else: # DATA
106 if sys.version >= '3':
107 G_buff[t].append(bytes.decode('utf-8'))
108 else:
109 G_buff[t].append(bytes)
110
111
112 # Clean up if someone closes the window
113 def close_cb(widget):
114 print("Killing child processes..\n")
115 for t in range(3):
116 if G_pids[t] == -1:
117 continue
118 os.kill(G_pids[t], 9)
119 sys.exit(0)
120
121
122 if __name__=='__main__':
123 if platform.platform().find("Windows") >= 0:
124 fl_message("Not supported on Windows platform!")
125 else:
126
127 win = Fl_Double_Window(620,520,"fltk-tty");
128 win.callback(close_cb) # kill children if window closed
129
130 # Start children, one tty for each
131 for t in range(3):
132 start_child(t)
133 G_buff[t] = Fl_Text_Buffer()
134 G_disp[t] = Fl_Text_Display(10+t*200, 10, 200, 500)
135 G_disp[t].buffer(G_buff[t])
136 G_disp[t].textfont(FL_COURIER)
137 G_disp[t].textsize(12)
138 Fl.add_fd(G_outfd[t], data_ready, t)
139
140 win.resizable(win)
141 win.show()
142 Fl.run()
143