-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell.c
2012 lines (1837 loc) · 60.1 KB
/
shell.c
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Shell mode for QEmacs.
*
* Copyright (c) 2001-2002 Fabrice Bellard.
* Copyright (c) 2002-2014 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <termios.h>
#include "qe.h"
/* XXX: status line */
/* XXX: better tab handling */
/* XXX: bold & italic ? */
/* XXX: send real cursor position (CSI n) */
static ModeDef shell_mode, pager_mode;
#define MAX_ESC_PARAMS 3
enum TTYState {
TTY_STATE_NORM,
TTY_STATE_UTF8,
TTY_STATE_ESC,
TTY_STATE_ESC2,
TTY_STATE_CSI,
TTY_STATE_STRING,
};
static int shell_signature;
typedef struct ShellState {
void *signature;
/* buffer state */
int pty_fd;
int pid; /* -1 if not launched */
int color, attr, def_color;
int cur_offset; /* current offset at position x, y */
int esc_params[MAX_ESC_PARAMS];
int has_params[MAX_ESC_PARAMS];
int nb_esc_params;
int state;
int esc1, esc2;
int shifted;
int cset, charset[2];
int grab_keys;
unsigned char utf8_buf[8];
int utf8_len, utf8_pos;
EditBuffer *b;
EditBuffer *b_color; /* color buffer, one byte per char */
struct QEmacsState *qe_state;
const char *ka1, *ka3, *kb2, *kc1, *kc3, *kcbt, *kspd;
const char *kbeg, *kbs, *kent, *kdch1, *kich1;
const char *kcub1, *kcud1, *kcuf1, *kcuu1;
const char *kf1, *kf2, *kf3, *kf4, *kf5;
const char *kf6, *kf7, *kf8, *kf9, *kf10;
const char *kf11, *kf12, *kf13, *kf14, *kf15;
const char *kf16, *kf17, *kf18, *kf19, *kf20;
const char *khome, *kend, *kmous, *knp, *kpp;
const char *caption; /* process caption for exit message */
int shell_flags;
} ShellState;
/* CG: these variables should be encapsulated in a global structure */
static char error_buffer[MAX_BUFFERNAME_SIZE];
static int error_offset = -1;
static int error_line_num = -1;
static char error_filename[MAX_FILENAME_SIZE];
static void set_error_offset(EditBuffer *b, int offset)
{
pstrcpy(error_buffer, sizeof(error_buffer), b ? b->name : "");
error_offset = offset - 1;
error_line_num = -1;
*error_filename = '\0';
}
#define PTYCHAR1 "pqrstuvwxyzabcde"
#define PTYCHAR2 "0123456789abcdef"
/* allocate one pty/tty pair */
static int get_pty(char *tty_str, int size)
{
int fd;
char ptydev[] = "/dev/pty??";
char ttydev[] = "/dev/tty??";
int len = strlen(ttydev);
const char *c1, *c2;
#ifdef CONFIG_PTSNAME
/* First try Unix98 pseudo tty master */
/* CG: should check if posix_openpt is more appropriate than /dev/ptmx */
//fd = posix_openpt(O_RDWR | O_NOCTTY);
fd = open("/dev/ptmx", O_RDWR);
if (fd >= 0) {
#if 0
/* ptsname_r is a sensible renentrant version of ptsname, but
* it lacks portability, notably on OpenBSD and cygwin. So we
* have to use ill conceived ptsname.
*/
if (!ptsname_r(fd, tty_str, size) && !grantpt(fd) && !unlockpt(fd))
return fd;
#else
const char *name = ptsname(fd);
if (name) {
pstrcpy(tty_str, size, name);
if (!grantpt(fd) && !unlockpt(fd))
return fd;
}
#endif
close(fd);
}
#endif
/* then try BSD pseudo tty pre-created pairs */
for (c1 = PTYCHAR1; *c1; c1++) {
ptydev[len-2] = ttydev[len-2] = *c1;
for (c2 = PTYCHAR2; *c2; c2++) {
ptydev[len-1] = ttydev[len-1] = *c2;
if ((fd = open(ptydev, O_RDWR)) >= 0) {
if (access(ttydev, R_OK|W_OK) == 0) {
pstrcpy(tty_str, size, ttydev);
return fd;
}
close(fd);
}
}
}
return -1;
}
const char *get_shell(void)
{
const char *shell_path;
/* find shell name */
shell_path = getenv("SHELL");
if (!shell_path)
shell_path = "/bin/sh";
return shell_path;
}
#define TTY_XSIZE 80
#define TTY_YSIZE 25
#define TTY_YSIZE_INFINITE 10000
static int run_process(const char *cmd, int *fd_ptr, int *pid_ptr,
int cols, int rows, int shell_flags)
{
int pty_fd, pid, i, nb_fds;
char tty_name[1024];
struct winsize ws;
pty_fd = get_pty(tty_name, sizeof(tty_name));
if (pty_fd < 0) {
put_status(NULL, "run_process: cannot get tty: %s",
strerror(errno));
return -1;
}
fcntl(pty_fd, F_SETFL, O_NONBLOCK);
/* set dummy screen size */
ws.ws_col = cols;
ws.ws_row = rows;
ws.ws_xpixel = ws.ws_col;
ws.ws_ypixel = ws.ws_row;
ioctl(pty_fd, TIOCSWINSZ, &ws);
pid = fork();
if (pid < 0) {
put_status(NULL, "run_process: cannot fork");
return -1;
}
if (pid == 0) {
/* child process */
const char *argv[4];
int argc = 0;
argv[argc++] = get_shell();
if (cmd) {
argv[argc++] = "-c";
argv[argc++] = cmd;
}
argv[argc] = NULL;
/* detach controlling terminal */
#ifndef CONFIG_DARWIN
setsid();
#endif
/* close all files */
nb_fds = getdtablesize();
for (i = 0; i < nb_fds; i++)
close(i);
if (shell_flags & SF_INFINITE)
setenv("LINES", "10000", 1);
/* open pseudo tty for standard I/O */
if (shell_flags & SF_INTERACTIVE) {
/* interactive shell: input from / output to pseudo terminal */
open(tty_name, O_RDWR);
dup(0);
dup(0);
} else {
/* collect output from non interactive process: no input */
open("/dev/null", O_RDONLY);
open(tty_name, O_RDWR);
dup(1);
}
#ifdef CONFIG_DARWIN
setsid();
#endif
setenv("TERM", "xterm", 1);
unsetenv("PAGER");
//setenv("QELEVEL", "1", 1);
execv(argv[0], (char * const*)argv);
exit(1);
}
/* return file info */
*fd_ptr = pty_fd;
*pid_ptr = pid;
return 0;
}
/* VT100 emulation */
static void tty_init(ShellState *s)
{
char *term;
s->state = TTY_STATE_NORM;
/* Should compute def_color from shell default style at display
* time and force full redisplay upon style change.
*/
s->color = s->def_color = TTY_MAKE_COLOR(TTY_DEFFG, TTY_DEFBG);
s->attr = 0;
term = getenv("TERM");
/* vt100 terminfo definitions */
s->kbs = "\010";
s->ka1 = "\033Oq";
s->ka3 = "\033Os";
s->kb2 = "\033Or";
s->kc1 = "\033Op";
s->kc3 = "\033On";
s->kcub1 = "\033OD";
s->kcud1 = "\033OB";
s->kcuf1 = "\033OC";
s->kcuu1 = "\033OA";
s->kent = "\033OM";
s->kf1 = "\033OP";
s->kf2 = "\033OQ";
s->kf3 = "\033OR";
s->kf4 = "\033OS";
s->kf5 = "\033Ot";
s->kf6 = "\033Ou";
s->kf7 = "\033Ov";
s->kf8 = "\033Ol";
s->kf9 = "\033Ow";
s->kf10 = "\033Ox";
/* ansi terminfo definitions */
if (strstart(term, "ansi", NULL)) {
s->kbs = "\010";
s->kcbt = "\033[Z";
s->kcub1 = "\033[D";
s->kcud1 = "\033[B";
s->kcuf1 = "\033[C";
s->kcuu1 = "\033[A";
s->khome = "\033[H";
s->kich1 = "\033[L";
}
if (strstart(term, "vt220", NULL)) {
s->kcub1 = "\033[D";
s->kcud1 = "\033[B";
s->kcuf1 = "\033[C";
s->kcuu1 = "\033[A";
s->kdch1 = "\033[3~";
s->kend = "\033[4~";
s->khome = "\033[1~";
s->kich1 = "\033[2~";
s->knp = "\033[6~";
s->kpp = "\033[5~";
s->kf1 = "\033OP";
s->kf2 = "\033OQ";
s->kf3 = "\033OR";
s->kf4 = "\033OS";
s->kf5 = "\033[17~";
s->kf6 = "\033[18~";
s->kf7 = "\033[19~";
s->kf8 = "\033[20~";
s->kf9 = "\033[21~";
s->kf10 = "\033[29~";
}
if (strstart(term, "cygwin", NULL)) {
s->kbs = "\10";
goto linux_cygwin;
}
if (strstart(term, "linux", NULL)) {
s->kbs = "\177";
s->kb2 = "\033[G";
s->kcbt = "\033[Z";
s->kspd = "\032"; // ^Z
linux_cygwin:
s->kcub1 = "\033[D";
s->kcud1 = "\033[B";
s->kcuf1 = "\033[C";
s->kcuu1 = "\033[A";
s->kdch1 = "\033[3~";
s->kend = "\033[4~";
s->khome = "\033[1~";
s->kich1 = "\033[2~";
s->knp = "\033[6~";
s->kpp = "\033[5~";
s->kf1 = "\033[[A";
s->kf2 = "\033[[B";
s->kf3 = "\033[[C";
s->kf4 = "\033[[D";
s->kf5 = "\033[[E";
s->kf6 = "\033[17~";
s->kf7 = "\033[18~";
s->kf8 = "\033[19~";
s->kf9 = "\033[20~";
s->kf10 = "\033[21~";
s->kf11 = "\033[23~";
s->kf12 = "\033[24~";
s->kf13 = "\033[25~";
s->kf14 = "\033[26~";
s->kf15 = "\033[28~";
s->kf16 = "\033[29~";
s->kf17 = "\033[31~";
s->kf18 = "\033[32~";
s->kf19 = "\033[33~";
s->kf20 = "\033[34~";
}
if (strstart(term, "xterm", NULL)) {
s->ka1 = "\033Ow";
s->ka3 = "\033Ou";
s->kb2 = "\033Oy";
s->kbeg = "\033OE";
s->kbs = "\03377";
s->kc1 = "\033Oq";
s->kc3 = "\033Os";
s->kcub1 = "\033OD";
s->kcud1 = "\033OB";
s->kcuf1 = "\033OC";
s->kcuu1 = "\033OA";
s->kdch1 = "\033[3~";
s->kend = "\033[4~";
s->kent = "\033OM";
s->khome = "\033[1~";
s->kich1 = "\033[2~";
s->kmous = "\033[M";
s->knp = "\033[6~";
s->kpp = "\033[5~";
s->kf1 = "\033OP";
s->kf2 = "\033OQ";
s->kf3 = "\033OR";
s->kf4 = "\033OS";
s->kf5 = "\033[15~";
s->kf6 = "\033[17~";
s->kf7 = "\033[18~";
s->kf8 = "\033[19~";
s->kf9 = "\033[20~";
s->kf10 = "\033[21~";
s->kf11 = "\033[23~";
s->kf12 = "\033[24~";
s->kf13 = "\033[25~";
s->kf14 = "\033[26~";
s->kf15 = "\033[28~";
s->kf16 = "\033[29~";
s->kf17 = "\033[31~";
s->kf18 = "\033[32~";
s->kf19 = "\033[33~";
s->kf20 = "\033[34~";
}
}
static void tty_write(ShellState *s, const char *buf, int len)
{
int ret;
if (len < 0)
len = strlen(buf);
if (s->qe_state->trace_buffer)
eb_trace_bytes(buf, len, EB_TRACE_PTY);
while (len > 0) {
ret = write(s->pty_fd, buf, len);
if (ret == -1 && (errno == EAGAIN || errno == EINTR))
continue;
if (ret <= 0)
break;
buf += ret;
len -= ret;
}
}
/* Compute offset of the char at column x and row y (0 based).
* Can insert spaces or rows if needed.
* x and y may each be relative to the current position.
*/
/* XXX: optimize !!!!! */
static void tty_goto_xy(ShellState *s, int x, int y, int relative)
{
int total_lines, cur_line, line_num, col_num, offset, offset1, c;
/* compute offset */
eb_get_pos(s->b, &total_lines, &col_num, s->b->total_size);
if (s->cur_offset == s->b->total_size
|| eb_prevc(s->b, s->b->total_size, &offset1) != '\n')
total_lines++;
line_num = total_lines - TTY_YSIZE;
if (line_num < 0)
line_num = 0;
if (relative) {
eb_get_pos(s->b, &cur_line, &col_num, s->cur_offset);
cur_line -= line_num;
if (cur_line < 0)
cur_line = 0;
if (relative & 1)
x += col_num;
if (relative & 2)
y += cur_line;
}
if (y < 0 || y >= TTY_YSIZE_INFINITE - 1)
y = 0;
else
if (y >= TTY_YSIZE)
y = TTY_YSIZE - 1;
if (x < 0)
x = 0;
line_num += y;
/* add lines if necessary */
while (line_num >= total_lines) {
/* XXX: color may be wrong */
s->b->cur_style = QE_STYLE_TTY | s->color | s->attr;
eb_insert_uchar(s->b, s->b->total_size, '\n');
total_lines++;
}
offset = eb_goto_pos(s->b, line_num, 0);
for (; x > 0; x--) {
c = eb_nextc(s->b, offset, &offset1);
if (c == '\n') {
/* duplicate style of last char */
offset += eb_insert_spaces(s->b, offset, x);
break;
} else {
offset = offset1;
}
}
s->cur_offset = offset;
}
/* CG: XXX: tty_put_char purposely ignores charset when inserting chars */
static int tty_put_char(ShellState *s, int c)
{
char buf[1];
int c1, cur_len, offset, offset1;
offset = s->cur_offset;
buf[0] = c;
c1 = eb_nextc(s->b, offset, &offset1);
s->b->cur_style = QE_STYLE_TTY | s->color | s->attr;
if (c1 == '\n') {
/* insert */
eb_insert(s->b, offset, buf, 1);
} else {
/* check for (c1 != c) is not advisable optimisation because
* re-writing the same character may cause color changes.
*/
cur_len = offset1 - offset;
if (cur_len == 1) {
eb_write(s->b, offset, buf, 1);
} else {
eb_delete(s->b, offset, cur_len);
eb_insert(s->b, offset, buf, 1);
}
}
return s->cur_offset = offset + 1;
}
static void tty_csi_m(ShellState *s, int c, int has_param)
{
/* Comment from putty/terminal.c:
*
* A VT100 without the AVO only had one
* attribute, either underline or
* reverse video depending on the
* cursor type, this was selected by
* CSI 7m.
*
* case 2:
* This is sometimes DIM, eg on the
* GIGI and Linux
* case 8:
* This is sometimes INVIS various ANSI.
* case 21:
* This like 22 disables BOLD, DIM and INVIS
*
* The ANSI colours appear on any
* terminal that has colour (obviously)
* but the interaction between sgr0 and
* the colours varies but is usually
* related to the background colour
* erase item. The interaction between
* colour attributes and the mono ones
* is also very implementation
* dependent.
*
* The 39 and 49 attributes are likely
* to be unimplemented.
*/
switch (has_param ? c : 0) {
case 0: /* exit_attribute_mode */
s->color = s->def_color;
s->attr = 0;
break;
case 1: /* enter_bold_mode */
s->attr |= TTY_BOLD;
break;
case 22: /* exit_bold_mode */
s->attr &= ~TTY_BOLD;
break;
case 4: /* enter_underline_mode */
s->attr |= TTY_UNDERLINE;
break;
case 24: /* exit_underline_mode */
s->attr &= ~TTY_UNDERLINE;
break;
case 5: /* enter_blink_mode */
s->attr |= TTY_BLINK;
break;
case 25: /* exit_blink_mode */
s->attr &= ~TTY_BLINK;
break;
case 7: /* enter_reverse_mode, enter_standout_mode */
case 27: /* exit_reverse_mode, exit_standout_mode */
/* TODO */
break;
case 6: /* SCO light background */
case 8: /* enter_secure_mode */
case 9: /* cygwin dim mode */
case 10: /* SCO acs off */
case 11: /* SCO acs on (CP437) */
case 12: /* SCO acs on, |0x80 */
case 28: /* exit_secure_mode */
break;
case 39: /* orig_pair(1) default-foreground */
TTY_SET_FG_COLOR(s->color, TTY_DEFFG);
break;
case 49: /* orig_pair(2) default-background */
TTY_SET_BG_COLOR(s->color, TTY_DEFBG);
break;
case 38: /* set extended foreground color */
/* complete syntax is \033[38;5;Nm where N is in range 1..255 */
if (s->esc_params[1] == 5) {
/* set foreground color to third esc_param */
int color = s->esc_params[2];
/* simulate 256 colors */
color = get_tty_color(tty_fg_colors[color & 255],
tty_fg_colors, 16);
TTY_SET_FG_COLOR(s->color, color);
s->nb_esc_params = 1;
}
break;
case 48: /* set extended background color */
/* complete syntax is \033[48;5;Nm where N is in range 1..255 */
if (s->esc_params[1] == 5) {
/* set background color to third esc_param */
int color = s->esc_params[2];
/* simulate 256 colors */
color = get_tty_color(tty_fg_colors[color & 255],
tty_fg_colors, 16);
TTY_SET_BG_COLOR(s->color, color);
s->nb_esc_params = 1;
}
break;
default:
/* 0:black 1:red 2:green 3:yellow 4:blue 5:magenta 6:cyan 7:white */
if (c >= 30 && c <= 37) {
/* set foreground color */
TTY_SET_FG_COLOR(s->color, c - 30);
} else
if (c >= 40 && c <= 47) {
/* set background color */
TTY_SET_BG_COLOR(s->color, c - 40);
} else
if (c >= 90 && c <= 97) {
/* set bright foreground color */
TTY_SET_FG_COLOR(s->color, c - 90 + 8);
} else
if (c >= 100 && c <= 107) {
/* set bright background color */
TTY_SET_BG_COLOR(s->color, c - 100 + 8);
}
break;
}
}
/* Well, almost a hack to update cursor */
static void tty_update_cursor(__unused__ ShellState *s)
{
#if 0
QEmacsState *qs = s->qe_state;
EditState *e;
if (s->cur_offset == -1)
return;
for (e = qs->first_window; e != NULL; e = e->next_window) {
if (s->b == e->b && e->interactive) {
e->offset = s->cur_offset;
}
}
#endif
}
static ShellState *shell_get_state(EditState *e, int status)
{
ShellState *s = e->b->priv_data;
if (s && s->signature == &shell_signature)
return s;
if (status)
put_status(e, "Not a shell buffer");
return NULL;
}
/* CG: much cleaner way! */
/* would need a kill hook as well ? */
static void shell_display_hook(EditState *e)
{
ShellState *s;
if (e->interactive) {
if ((s = shell_get_state(e, 0)) != NULL)
e->offset = s->cur_offset;
}
}
static void shell_key(void *opaque, int key)
{
ShellState *s = opaque;
char buf[10];
const char *p;
int len;
if (!s || s->signature != &shell_signature)
return;
if (key == KEY_CTRL('o')) {
qe_ungrab_keys();
unget_key(key);
return;
}
p = buf;
len = -1;
switch (key) {
case KEY_UP: p = s->kcuu1; break;
case KEY_DOWN: p = s->kcud1; break;
case KEY_RIGHT: p = s->kcuf1; break;
case KEY_LEFT: p = s->kcub1; break;
//case KEY_CTRL_UP:
//case KEY_CTRL_DOWN:
//case KEY_CTRL_RIGHT:
//case KEY_CTRL_LEFT:
//case KEY_CTRL_END:
//case KEY_CTRL_HOME:
//case KEY_CTRL_PAGEUP:
//case KEY_CTRL_PAGEDOWN:
case KEY_SHIFT_TAB: p = s->kcbt; break;
case KEY_HOME: p = s->khome; break;
case KEY_INSERT: p = s->kich1; break;
case KEY_DELETE: p = s->kdch1; break;
case KEY_END: p = s->kend; break;
case KEY_PAGEUP: p = s->kpp; break;
case KEY_PAGEDOWN: p = s->knp; break;
case KEY_F1: p = s->kf1; break;
case KEY_F2: p = s->kf2; break;
case KEY_F3: p = s->kf3; break;
case KEY_F4: p = s->kf4; break;
case KEY_F5: p = s->kf5; break;
case KEY_F6: p = s->kf6; break;
case KEY_F7: p = s->kf7; break;
case KEY_F8: p = s->kf8; break;
case KEY_F9: p = s->kf9; break;
case KEY_F10: p = s->kf10; break;
case KEY_F11: p = s->kf11; break;
case KEY_F12: p = s->kf12; break;
case KEY_F13: p = s->kf13; break;
case KEY_F14: p = s->kf14; break;
case KEY_F15: p = s->kf15; break;
case KEY_F16: p = s->kf16; break;
case KEY_F17: p = s->kf17; break;
case KEY_F18: p = s->kf18; break;
case KEY_F19: p = s->kf19; break;
case KEY_F20: p = s->kf20; break;
default:
if (key < 256) {
buf[0] = key;
len = 1;
} else
if (key >= KEY_META(0) && key <= KEY_META(255)) {
buf[0] = '\033';
buf[1] = key;
len = 2;
} else {
p = NULL;
}
break;
}
if (p) {
tty_write(s, p, len);
}
}
static unsigned char const sco_color[16] = {
0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15,
};
static void tty_emulate(ShellState *s, int c)
{
int i, offset, offset1, offset2, n;
char buf1[10];
offset = s->cur_offset;
#define ESC2(c1,c2) (((c1)<<8)|((unsigned char)c2))
/* some bytes are state independent */
switch (c) {
case 0x18:
case 0x1A:
s->state = TTY_STATE_NORM;
return;
case 0x1B:
s->state = TTY_STATE_ESC;
return;
#if 0
case 0x9B:
goto csi_entry;
#endif
}
switch (s->state) {
case TTY_STATE_NORM:
switch (c) {
/* BEL Bell (Ctrl-G) */
/* FF Form Feed or New Page (NP) (Ctrl-L) same as LF */
/* VT Vertical Tab (Ctrl-K) same as LF */
case 8: /* ^H BS = backspace */
{
int c1;
c1 = eb_prevc(s->b, offset, &offset1);
if (c1 != '\n') {
s->cur_offset = offset1;
/* back_color_erase */
//tty_put_char(s, ' ');
}
}
break;
case 9: /* ^I HT = horizontal tab */
{
int col_num, cur_line;
eb_get_pos(s->b, &cur_line, &col_num, offset);
tty_goto_xy(s, (col_num + 8) & ~7, 0, 2);
break;
}
case 10: /* ^J NL = line feed */
/* go to next line */
/* CG: should check if column should be kept */
for (;;) {
if (offset == s->b->total_size) {
/* add a new line */
/* CG: XXX: ignoring charset */
s->b->cur_style = QE_STYLE_TTY | s->color | s->attr;
buf1[0] = '\n';
eb_insert(s->b, offset, buf1, 1);
offset = s->b->total_size;
break;
}
c = eb_nextc(s->b, offset, &offset);
if (c == '\n')
break;
}
s->b->last_log = 0; /* close undo record */
s->cur_offset = offset;
break;
case 13: /* ^M CR = carriage return */
/* move to bol */
s->cur_offset = eb_goto_bol(s->b, offset);
break;
case 14: /* ^N SO = shift out */
s->shifted = s->charset[s->cset = 1];
break;
case 15: /* ^O SI = shift in */
s->shifted = s->charset[s->cset = 0];
break;
default:
if (c >= 32) {
int c1, cur_len, len;
/* CG: assuming ISO-8859-1 characters */
/* CG: horrible kludge for alternate charset support */
if (s->shifted && c >= 96 && c < 128) {
#if 0
/* Should actually use these tables: */
static const wchar_t unitab_xterm_poorman[32] =
"*#****o~**+++++-----++++|****L. ";
#endif
if (s->b->charset == &charset_utf8) {
static const wchar_t unitab_xterm_std[32] = {
0x2666, 0x2592, 0x2409, 0x240c,
0x240d, 0x240a, 0x00b0, 0x00b1,
0x2424, 0x240b, 0x2518, 0x2510,
0x250c, 0x2514, 0x253c, 0x23ba,
0x23bb, 0x2500, 0x23bc, 0x23bd,
0x251c, 0x2524, 0x2534, 0x252c,
0x2502, 0x2264, 0x2265, 0x03c0,
0x2260, 0x00a3, 0x00b7, 0x0020
};
c = unitab_xterm_std[c - 96];
len = utf8_encode(buf1, c);
} else {
/* CG: quick 8 bit hack: store line drawing
* characters in [96..127] as meta control
* characters in [128..159].
* This hack is reversed in tty_term_flush().
*/
c += 32;
buf1[0] = c;
len = 1;
}
} else {
/* write char (should factorize with do_char() code */
/* CG: Charset support is inherently broken here because
* bytes are inserted one at a time and charset conversion
* should not be performed between shell output and buffer
* contents. UTF8 is special cased, other charsets need work.
*/
/* CG: further improvement direction includes automatic
* conversion from ISO-8859-1 to UTF-8 for invalid UTF-8
* byte sequences.
*/
if (s->b->charset == &charset_utf8) {
s->utf8_len = utf8_length[c];
if (s->utf8_len > 1) {
s->utf8_buf[0] = c;
s->utf8_pos = 1;
s->state = TTY_STATE_UTF8;
break;
}
}
//len = eb_encode_uchar(s->b, buf1, c);
buf1[0] = c;
len = 1;
}
c1 = eb_nextc(s->b, offset, &offset1);
s->b->cur_style = QE_STYLE_TTY | s->color | s->attr;
/* Should simplify with tty_put_char */
if (c1 == '\n') {
/* insert */
eb_insert(s->b, offset, buf1, len);
} else {
cur_len = offset1 - offset;
if (cur_len == len) {
eb_write(s->b, offset, buf1, len);
} else {
eb_delete(s->b, offset, cur_len);
eb_insert(s->b, offset, buf1, len);
}
}
s->cur_offset = offset + len;
}
break;
}
break;
case TTY_STATE_UTF8:
s->utf8_buf[s->utf8_pos++] = c;
if (s->utf8_pos >= s->utf8_len) {
int c1, cur_len, len;
len = s->utf8_len;
c1 = eb_nextc(s->b, offset, &offset1);
s->b->cur_style = QE_STYLE_TTY | s->color | s->attr;
if (c1 == '\n') {
/* insert */
eb_insert(s->b, offset, s->utf8_buf, len);
} else {
cur_len = offset1 - offset;
if (cur_len == len) {
eb_write(s->b, offset, s->utf8_buf, len);
} else {
eb_delete(s->b, offset, cur_len);
eb_insert(s->b, offset, s->utf8_buf, len);
}
}
s->cur_offset = offset + len;
s->state = TTY_STATE_NORM;
}
break;
case TTY_STATE_ESC:
if (c == '[') {
for (i = 0; i < MAX_ESC_PARAMS; i++) {
s->esc_params[i] = 1;
s->has_params[i] = 0;
}
s->nb_esc_params = 0;
s->esc1 = 0;
s->state = TTY_STATE_CSI;
} else {
/* CG: should deal with other sequences:
* ansi: hts=\EH, s0ds=\E(B, s1ds=\E)B, s2ds=\E*B, s3ds=\E+B,
* linux: hts=\EH, rc=\E8, ri=\EM, rs1=\Ec\E]R, sc=\E7,
* vt100: enacs=\E(B\E)0, hts=\EH, rc=\E8, ri=\EM$<5>,
* rmkx=\E[?1l\E>,
* rs2=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h, sc=\E7,
* smkx=\E[?1h\E=,
* xterm: enacs=\E(B\E)0, hts=\EH, is2=\E[!p\E[?3;4l\E[4l\E>,
* rc=\E8, ri=\EM, rmkx=\E[?1l\E>, rs1=\Ec,
* rs2=\E[!p\E[?3;4l\E[4l\E>, sc=\E7, smkx=\E[?1h\E=,
* set-window-title=\E]0;title text\007,
*/
switch (c) {
case '%':
case '(':
case ')':
case '*':
case '+':
case ']':
s->esc1 = c;
s->state = TTY_STATE_ESC2;
break;
case '7': // sc (save_cursor)
case '8': // rc (restore_cursor)
case '=': // smkx (DECKPAM: Keypad application mode)
case '>': // rmkx, is2, rs2 (DECKPNM: Keypad numeric mode)
case 'D': // IND: exactly equivalent to LF
case 'E': // NEL: exactly equivalent to CR-LF
case 'M': // ri (scroll_reverse, RI: reverse index - backwards LF)
case 'Z': /* DECID: terminal type query */
case 'c': // rs1 (reset_1string)
/* RIS: restore power-on settings */
case 'H': // hts (set_tab)
// XXX: do these
default:
s->state = TTY_STATE_NORM;
break;
}
}
break;
case TTY_STATE_ESC2:
s->state = TTY_STATE_NORM;
s->esc2 = c;
switch (ESC2(s->esc1, c)) {
case ESC2('%','G'): /* set utf mode */
case ESC2('%','8'): /* set utf mode */
case ESC2('%','@'): /* reset utf mode */
break;