Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

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