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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
/*
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
All rights reserved.
Copyright (C) 2007-2010, Gabriel Dos Reis.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
- Neither the name of The Numerical Algorithms Group Ltd. nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <signal.h>
#include <locale.h>
#include "open-axiom.h"
#include "sockio.h"
#include "edible.h"
#include "com.h"
#include "bsdsignal.h"
#include "openpty.H1"
#include "prt.H1"
#include "edin.H1"
#include "wct.H1"
#include "fnct_key.H1"
#include "cfuns.h"
#ifdef AXIOM_UNLIKELY
#define log 1
#define logterm 1
#define siglog 1
#endif
#define Cursor_shape(x)
static void check_flip(void);
static void catch_signals(void);
static void init_parent(void);
static void set_function_chars(void);
static void flip_canonical(int );
static void flip_raw(int );
#ifdef siglog
int sigfile;
char sigbuff[256];
#endif
/* Here are the term structures I need for setting and resetting the
terminal characteristics. */
struct termios childbuf; /* the childs normal operating termio */
struct termios oldbuf; /* the initial settings */
struct termios rawbuf; /* the parents raw state, when it is doing nothing */
struct termios canonbuf; /* set it to be canonical */
/* the terminals mapping of the function keys */
unsigned char _INTR, _QUIT, _ERASE, _KILL, _EOF, _EOL, _RES1, _RES2;
int ppid; /* the parents's parent pid */
int child_pid; /* the childs process id */
short INS_MODE ; /* Flag for insert mode */
short ECHOIT = 1; /* Flag for echoing */
short PTY; /* Flag which tells me whether I should echo newlines */
int MODE; /* am I in cbreak, raw, or canonical */
char in_buff[1024]; /* buffer for storing characters read until they are processed */
char buff[MAXLINE]; /** Buffers for collecting input and **/
int buff_flag[MAXLINE]; /** flags for whether buff chars
are printing
or non-printing **/
char serverPath[20]; /* path name for opening the server side */
int contNum, serverNum; /* file descriptors for pty's */
int num_read; /* Number of chars read */
#ifdef log
int logfd;
char logpath[30];
#endif
int
main(int argc, char *argv[])
{
fd_set rfds; /* the structure for the select call */
int code; /* return code from system calls */
char out_buff[MAXLINE]; /* from child and stdin */
int out_flag[MAXLINE] ; /* initialize the output flags */
char *program; /* a string to hold the child program invocation */
char **pargs = 0; /* holds parts of the command line */
int not_command = 1; /* a flag while parsing the command line */
putenv((char*) "LC_ALL=C");
setlocale(LC_ALL, "");
/* try to get a pseudoterminal to play with */
if (ptyopen(&contNum, &serverNum, serverPath) == -1) {
perror("ptyopen failed");
exit(-1);
}
/* call the routine that handles signals */
catch_signals();
/* parse the command line arguments - as with the aixterm the command
argument -e should be last on the line. */
while(*++argv && not_command) {
if(!strcmp(*argv, "-f"))
load_wct_file(*++argv);
else if(!strcmp(*argv, "-e")) {
not_command = 0;
pargs = ++argv;
}
else {
fprintf(stderr, "usage: clef [-f fname] -e command\n");
exit(-1);
}
}
skim_wct();
#ifdef log
sprintf(logpath, "/tmp/cleflog%d", oa_getpid());
logfd = open(logpath, O_CREAT | O_RDWR, 0666);
#endif
/* get the original termio settings, so the child has them */
if(tcgetattr(0,&childbuf) == -1) {
perror("clef trying to get the initial terminal settings");
exit(-1);
}
/* start the child process */
child_pid = fork();
switch(child_pid) {
case -1 :
perror("clef can't create a new process");
exit(-1);
case 0:
/* CHILD */
/* Dissasociate form my parents group so all my child processes
look at my terminal as the controlling terminal for the group */
setsid();
serverNum = open(serverPath,O_RDWR);
if (serverNum == -1) perror("open serverPath failed");
/* since I am the child, I can close ptc, and dup pts for all it
standard descriptors */
if (dup2(serverNum, 0) == -1) perror("dup2 0 failed");
if (dup2(serverNum, 1) == -1) perror("dup2 1 failed");
if (dup2(serverNum, 2) == -1) perror("dup2 2 failed");
if( (dup2(serverNum, 0) == -1) ||
(dup2(serverNum, 1) == -1) ||
(dup2(serverNum, 2) == -1) ) {
perror("clef trying to dup2");
exit(-1);
}
/* since they have been duped, close them */
close(serverNum);
close(contNum);
/* To make sure everything is nice, set off enhedit */
/* childbuf.c_line = 0; */
/* reconfigure the child's terminal get echoing */
if(tcsetattr(0, TCSAFLUSH, &childbuf) == -1) {
perror("clef child trying to set child's terminal");
exit(-1);
}
/* fire up the child's process */
if(pargs){
execvp( pargs[0], pargs);
perror("clef trying to execvp its argument");
fprintf(stderr, "Process --> %s\n", pargs[0]);
}
else{
program = oa_getenv("SHELL");
if (!program)
program = strdup("/bin/sh");
else
program = strdup (program);
execlp( program,program, (char *) 0);
perror("clef trying to execlp the default child");
fprintf(stderr, "Process --> %s\n", program);
}
exit(-1);
break;
/* end switch */
}
/* PARENT */
/* Since I am the parent, I should start to initialize some stuff.
I have to close the pts side for it to work properly */
close(serverNum);
ppid = getppid();
/* Iinitialize some stuff for the reading and writing */
init_flag(out_flag, MAXLINE);
define_function_keys();
init_reader();
PTY = 1;
init_parent();
/* Here is the main loop, it simply starts reading characters from
the std input, and from the child. */
while(1) { /* loop forever */
/* use select to see who has stuff waiting for me to handle */
/* set file descriptors for ptc and stdin */
FD_ZERO(&rfds);
FD_SET(contNum,&rfds);
FD_SET(0,&rfds);
set_function_chars();
#ifdef log
{
char modepath[30];
sprintf(modepath, "\nMODE = %d\n", MODE);
write(logfd, modepath, strlen(modepath));
}
#endif
#ifdef logterm
{
struct termio ptermio;
char pbuff[1024];
tcgetattr(contNum, &ptermio);
sprintf(pbuff, "child's settings: Lflag = %d, Oflag = %d, Iflag = %d\n",
ptermio.c_lflag, ptermio.c_oflag, ptermio.c_iflag);
write(logfd, pbuff, strlen(pbuff));
}
#endif
code = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
for(; code < 0 ;) {
if(errno == EINTR) {
check_flip();
code = select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
}
else {
perror("clef select failure");
exit(-1);
}
}
/* reading from the child **/
if( FD_ISSET(contNum,&rfds)) {
if( (num_read = read(contNum, out_buff, MAXLINE)) == -1) {
num_read = 0;
}
#ifdef log
write(logfd, "OUT<<<<<", strlen("OUT<<<<<"));
write(logfd, out_buff, num_read);
#endif
if(num_read > 0) {
/* now do the printing to the screen */
if(MODE!= CLEFRAW) {
back_up(buff_pntr);
write(1,out_buff, num_read);
print_whole_buff(); /* reprint the input buffer */
}
else write(1,out_buff, num_read);
}
} /* done the child stuff */
/* I should read from std input */
else {
if(FD_ISSET(0,&rfds)) {
num_read = read(0, in_buff, MAXLINE);
#ifdef log
write(logfd, "IN<<<<<", strlen("IN<<<<<"));
write(logfd, in_buff, num_read);
#endif
check_flip();
if(MODE == CLEFRAW )
write(contNum, in_buff, num_read);
else
do_reading();
}
}
}
}
static void
init_parent(void)
{
/* get the original termio settings, so I never have to check again */
if(tcgetattr(0, &oldbuf) == -1) {
perror("clef trying to get terminal initial settings");
exit(-1);
}
/* get the settings for my different modes */
if ((tcgetattr(0, &canonbuf) == -1) ||
(tcgetattr(0, &rawbuf) == -1) ) {
perror("clef trying to get terminal settings");
exit(-1);
}
canonbuf.c_lflag &= ~(ICANON | ECHO | ISIG);
/* read before an eoln is typed */
canonbuf.c_lflag |= ISIG;
/* canonbuf.c_line = 0; turn off enhanced edit */
canonbuf.c_cc[VMIN] = 1; /* we want every character */
canonbuf.c_cc[VTIME] = 1; /* these may require tweaking */
/* also set up the parents raw setting for when needed **/
rawbuf.c_oflag = rawbuf.c_iflag = rawbuf.c_lflag /* = rawbuf.c_line */ = 0;
rawbuf.c_cc[VMIN] = 1;
rawbuf.c_cc[VTIME] = 1;
if(tcsetattr(0, TCSAFLUSH, &canonbuf) == -1) {
perror("clef setting parent terminal to canonical processing");
exit(0);
}
/* initialize some flags I will be using */
MODE = CLEFCANONICAL;
INS_MODE = 1;
Cursor_shape(2);
}
static void
hangup_handler(int sig)
{
#ifdef siglog
sigfile = open(sigbuff, O_RDWR | O_APPEND);
write(sigfile, "Hangup Handler\n", strlen("Hangup Handler\n"));
close(sigfile);
#endif
/* try to kill my child if it is around */
if(kill(child_pid, 0)) kill(child_pid, SIGTERM);
if(kill(ppid, 0) >= 0) {
/* fix the terminal and exit */
if(tcsetattr(0, TCSAFLUSH, &oldbuf) == -1) {
perror("clef restoring terminal in hangup handler");
}
printf("\n");
}
/* remove the temporary editor filename */
unlink(editorfilename);
exit(-1);
}
static void
terminate_handler(int sig)
{
#ifdef siglog
sigfile = open(sigbuff, O_RDWR | O_APPEND);
write(sigfile, "Terminate Handler\n", strlen("Terminate Handler\n") + 1);
close(sigfile);
openaxiom_sleep(1);
#endif
kill(child_pid, SIGTERM);
/* fix the terminal, and exit */
if(tcsetattr(0, TCSAFLUSH, &oldbuf) == -1) {
perror("clef restoring terminal in terminate handler");
}
printf("\n");
Cursor_shape(2);
fprintf(stderr, "\n");
/* remove the temporary editor filename */
unlink(editorfilename);
exit(0);
}
static void
interrupt_handler(int sig)
{
#ifdef siglog
sigfile = open(sigbuff, O_RDWR | O_APPEND);
write(sigfile, "Interrupt Handler\n", strlen("Interrupt Handler\n") + 1);
close(sigfile);
openaxiom_sleep(1);
#endif
kill(child_pid, SIGINT);
}
static void
child_handler(int sig)
{
#ifdef siglog
sigfile = open(sigbuff, O_RDWR | O_APPEND );
write(sigfile, "Child Handler\n", strlen("Child Handler\n") + 1);
close(sigfile);
#endif
Cursor_shape(2);
close(contNum);
if(kill(ppid, 0) >= 0) {
/* fix the terminal, and exit */
if(tcsetattr(0, TCSAFLUSH, &oldbuf) == -1) {
perror("clef restoring terminal in child handler");
}
printf("\n");
}
/* remove the temporary editor filename */
unlink(editorfilename);
exit(0);
}
static void
alarm_handler(int sig)
{
int newppid = getppid();
#ifdef siglog
sigfile = open(sigbuff, O_RDWR | O_APPEND);
write(sigfile, "Alarm Handler\n", strlen("Alarm Handler\n")+ 1 );
close(sigfile);
#endif
/* simply gets the parent process id, if different, it terminates ,
otherwise it resets the alarm */
if(ppid == newppid) {
alarm(60);
return;
}
else {
/* once that is done fix the terminal, and exit */
if(tcsetattr(0, TCSAFLUSH, &oldbuf) == -1) {
perror("clef restoring terminal in alarm handler");
}
Cursor_shape(2);
fprintf(stderr, "\n");
/* remove the temporary editor filename */
unlink(editorfilename);
exit(0);
}
}
/* a procedure which tells my parent how to catch signals from its children */
static void
catch_signals(void)
{
#ifdef siglog
sprintf(sigbuff, "/tmp/csig%d", oa_getpid());
sigfile = open(sigbuff, O_RDWR | O_TRUNC | O_CREAT);
write(sigfile, "Started \n", strlen("Started \n"));
close(sigfile);
#endif
bsdSignal(SIGHUP, hangup_handler,RestartSystemCalls);
bsdSignal(SIGCHLD,child_handler,RestartSystemCalls);
bsdSignal(SIGTERM, terminate_handler,RestartSystemCalls);
bsdSignal(SIGINT, interrupt_handler,RestartSystemCalls);
bsdSignal(SIGALRM, alarm_handler,RestartSystemCalls);
alarm(60);
}
/* Here is where I check the child's termio settings, and try to copy them.
I simply trace through the main modes (CLEFRAW, CLEFCANONICAL) and
try to simulate them */
static void
check_flip(void)
{
return;
#if 0
/*simply checks the termio structure of the child */
if(tcgetattr(contNum, &childbuf) == -1) {
perror("clef parent getting child's terminal in check_termio");
}
if(childbuf.c_lflag & ICANON) {
if(MODE != CLEFCANONICAL) {
flip_canonical(contNum);
MODE = CLEFCANONICAL;
}
}
else {
if(MODE != CLEFRAW) {
flip_raw(contNum);
MODE = CLEFRAW;
}
}
/* While I am here, lets set the echo */
if(childbuf.c_lflag & ECHO) ECHOIT = 1;
else ECHOIT = 0;
#endif
}
static void
flip_raw(int chann)
{
if(MODE == CLEFCANONICAL)
send_buff_to_child(chann);
if(tcsetattr(0, TCSAFLUSH, &rawbuf) == -1) {
perror("clef resetting parent to raw ");
exit(-1);
}
}
static void
flip_canonical(int chann)
{
if(tcsetattr(0, TCSAFLUSH, &canonbuf) == -1) {
perror("clef resetting parent to canonical ");
exit(-1);
}
if(INS_MODE)
Cursor_shape(5);
else
Cursor_shape(2);
}
#define etc_whitespace(c) ((c == ' ' || c == '\t')?(1):(0))
static void
set_function_chars(void)
{
/* get the special characters */
_INTR = childbuf.c_cc[VINTR];
_QUIT = childbuf.c_cc[VQUIT];
_ERASE = childbuf.c_cc[VERASE];
_KILL = childbuf.c_cc[VKILL];
_EOF = childbuf.c_cc[VEOF];
_EOL = childbuf.c_cc[VEOL];
return;
}
|