Repository navigation
Expand file tree
/
Copy pathtty.c
More file actions
1293 lines (1124 loc) · 36 KB
/
Copy pathtty.c
File metadata and controls
1293 lines (1124 loc) · 36 KB
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
/* TTY part of PTY. */
#include <assert.h>
#include <minix/drivers.h>
#include <minix/driver.h>
#include <termios.h>
#include <sys/ttycom.h>
#include <sys/ttydefaults.h>
#include <sys/fcntl.h>
#include <signal.h>
#include "tty.h"
#include <sys/time.h>
#include <sys/select.h>
/* Address of a tty structure. */
#define tty_addr(line) (&tty_table[line])
/* Macros for magic tty structure pointers. */
#define FIRST_TTY tty_addr(0)
#define END_TTY tty_addr(sizeof(tty_table) / sizeof(tty_table[0]))
/* A device exists if at least its 'devread' function is defined. */
#define tty_active(tp) ((tp)->tty_devread != NULL)
struct kmessages kmess;
static void tty_timed_out(minix_timer_t *tp);
static void settimer(tty_t *tty_ptr, int enable);
static void in_transfer(tty_t *tp);
static int tty_echo(tty_t *tp, int ch);
static void rawecho(tty_t *tp, int ch);
static int back_over(tty_t *tp);
static void reprint(tty_t *tp);
static void dev_ioctl(tty_t *tp);
static void setattr(tty_t *tp);
static void tty_icancel(tty_t *tp);
static int do_open(devminor_t minor, int access, endpoint_t user_endpt);
static int do_close(devminor_t minor);
static ssize_t do_read(devminor_t minor, u64_t position, endpoint_t endpt,
cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
static ssize_t do_write(devminor_t minor, u64_t position, endpoint_t endpt,
cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
static int do_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id);
static int do_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id);
static int do_select(devminor_t minor, unsigned int ops, endpoint_t endpt);
static struct chardriver tty_tab = {
.cdr_open = do_open,
.cdr_close = do_close,
.cdr_read = do_read,
.cdr_write = do_write,
.cdr_ioctl = do_ioctl,
.cdr_cancel = do_cancel,
.cdr_select = do_select
};
/* Default attributes. */
static struct termios termios_defaults = {
.c_iflag = TTYDEF_IFLAG,
.c_oflag = TTYDEF_OFLAG,
.c_cflag = TTYDEF_CFLAG,
.c_lflag = TTYDEF_LFLAG,
.c_ispeed = TTYDEF_SPEED,
.c_ospeed = TTYDEF_SPEED,
.c_cc = {
[VEOF] = CEOF,
[VEOL] = CEOL,
[VERASE] = CERASE,
[VINTR] = CINTR,
[VKILL] = CKILL,
[VMIN] = CMIN,
[VQUIT] = CQUIT,
[VTIME] = CTIME,
[VSUSP] = CSUSP,
[VSTART] = CSTART,
[VSTOP] = CSTOP,
[VREPRINT] = CREPRINT,
[VLNEXT] = CLNEXT,
[VDISCARD] = CDISCARD,
[VSTATUS] = CSTATUS
}
};
static struct winsize winsize_defaults; /* = all zeroes */
static const char lined[TTLINEDNAMELEN] = "termios"; /* line discipline */
/* Global variables for the TTY task (declared extern in tty.h). */
tty_t tty_table[NR_PTYS];
u32_t system_hz;
static void tty_startup(void);
static int tty_init(int, sef_init_info_t *);
/*===========================================================================*
* tty_task *
*===========================================================================*/
int main(void)
{
/* Main routine of the terminal task. */
message tty_mess; /* buffer for all incoming messages */
int ipc_status;
int line;
int r;
register tty_t *tp;
tty_startup();
while (TRUE) {
/* Check for and handle any events on any of the ttys. */
for (tp = FIRST_TTY; tp < END_TTY; tp++) {
if (tp->tty_events) handle_events(tp);
}
/* Get a request message. */
r= driver_receive(ANY, &tty_mess, &ipc_status);
if (r != 0)
panic("driver_receive failed with: %d", r);
if (is_ipc_notify(ipc_status)) {
switch (_ENDPOINT_P(tty_mess.m_source)) {
case CLOCK:
/* run watchdogs of expired timers */
expire_timers(tty_mess.m_notify.timestamp);
break;
default:
/* do nothing */
break;
}
/* done, get new message */
continue;
}
if (!IS_CDEV_RQ(tty_mess.m_type)) {
chardriver_process(&tty_tab, &tty_mess, ipc_status);
continue;
}
/* Only device requests should get to this point.
* All requests have a minor device number.
*/
if (OK != chardriver_get_minor(&tty_mess, &line))
continue;
if ((line >= PTYPX_MINOR) && (line < (PTYPX_MINOR + NR_PTYS)) &&
tty_mess.m_type != CDEV_IOCTL) {
/* Terminals and pseudo terminals belong together. We can only
* make a distinction between the two based on position in the
* tty_table and not on minor number. Hence this special case.
*/
do_pty(&tty_mess, ipc_status);
continue;
}
/* Execute the requested device driver function. */
chardriver_process(&tty_tab, &tty_mess, ipc_status);
}
return 0;
}
tty_t *
line2tty(devminor_t line)
{
/* Convert a terminal line to tty_table pointer */
tty_t* tp;
if ((line - TTYPX_MINOR) < NR_PTYS) {
tp = tty_addr(line - TTYPX_MINOR);
} else if ((line - PTYPX_MINOR) < NR_PTYS) {
tp = tty_addr(line - PTYPX_MINOR);
} else {
tp = NULL;
}
return(tp);
}
/*===========================================================================*
* tty_startup *
*===========================================================================*/
static void tty_startup(void)
{
/* Register init callbacks. */
sef_setcb_init_fresh(tty_init);
sef_setcb_init_restart(tty_init);
/* No live update support for now. */
/* No signal support for now. */
/* Let SEF perform startup. */
sef_startup();
}
/*===========================================================================*
* do_read *
*===========================================================================*/
static ssize_t do_read(devminor_t minor, u64_t UNUSED(position),
endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
cdev_id_t id)
{
/* A process wants to read from a terminal. */
tty_t *tp;
int r;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
/* Check if there is already a process hanging in a read, check if the
* parameters are correct, do I/O.
*/
if (tp->tty_incaller != NONE || tp->tty_inleft > 0)
return EIO;
if (size <= 0)
return EINVAL;
/* Copy information from the message to the tty struct. */
tp->tty_incaller = endpt;
tp->tty_inid = id;
tp->tty_ingrant = grant;
assert(tp->tty_incum == 0);
tp->tty_inleft = size;
if (!(tp->tty_termios.c_lflag & ICANON) && tp->tty_termios.c_cc[VTIME] > 0) {
if (tp->tty_termios.c_cc[VMIN] == 0) {
/* MIN & TIME specify a read timer that finishes the
* read in TIME/10 seconds if no bytes are available.
*/
settimer(tp, TRUE);
tp->tty_min = 1;
} else {
/* MIN & TIME specify an inter-byte timer that may
* have to be cancelled if there are no bytes yet.
*/
if (tp->tty_eotct == 0) {
settimer(tp, FALSE);
tp->tty_min = tp->tty_termios.c_cc[VMIN];
}
}
}
/* Anything waiting in the input buffer? Clear it out... */
in_transfer(tp);
/* ...then go back for more. */
handle_events(tp);
if (tp->tty_inleft == 0)
return EDONTREPLY; /* already done */
/* There were no bytes in the input queue available. */
if (flags & CDEV_NONBLOCK) {
tty_icancel(tp);
r = tp->tty_incum > 0 ? tp->tty_incum : EAGAIN;
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_incaller = NONE;
return r;
}
if (tp->tty_select_ops)
select_retry(tp);
return EDONTREPLY; /* suspend the caller */
}
/*===========================================================================*
* do_write *
*===========================================================================*/
static ssize_t do_write(devminor_t minor, u64_t UNUSED(position),
endpoint_t endpt, cp_grant_id_t grant, size_t size, int flags,
cdev_id_t id)
{
/* A process wants to write on a terminal. */
tty_t *tp;
int r;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
/* Check if there is already a process hanging in a write, check if the
* parameters are correct, do I/O.
*/
if (tp->tty_outcaller != NONE || tp->tty_outleft > 0)
return EIO;
if (size <= 0)
return EINVAL;
/* Copy message parameters to the tty structure. */
tp->tty_outcaller = endpt;
tp->tty_outid = id;
tp->tty_outgrant = grant;
assert(tp->tty_outcum == 0);
tp->tty_outleft = size;
/* Try to write. */
handle_events(tp);
if (tp->tty_outleft == 0)
return EDONTREPLY; /* already done */
/* None or not all the bytes could be written. */
if (flags & CDEV_NONBLOCK) {
r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
tp->tty_outleft = tp->tty_outcum = 0;
tp->tty_outcaller = NONE;
return r;
}
if (tp->tty_select_ops)
select_retry(tp);
return EDONTREPLY; /* suspend the caller */
}
/*===========================================================================*
* do_ioctl *
*===========================================================================*/
static int do_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id)
{
/* Perform an IOCTL on this terminal. POSIX termios calls are handled
* by the IOCTL system call.
*/
tty_t *tp;
int i, r;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
r = OK;
switch (request) {
case TIOCGETA:
/* Get the termios attributes. */
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &tp->tty_termios,
sizeof(struct termios));
break;
case TIOCSETAW:
case TIOCSETAF:
case TIOCDRAIN:
if (tp->tty_outleft > 0) {
if (flags & CDEV_NONBLOCK)
return EAGAIN;
/* Wait for all ongoing output processing to finish. */
tp->tty_iocaller = endpt;
tp->tty_ioid = id;
tp->tty_ioreq = request;
tp->tty_iogrant = grant;
return EDONTREPLY; /* suspend the caller */
}
if (request == TIOCDRAIN) break;
if (request == TIOCSETAF) tty_icancel(tp);
/*FALL THROUGH*/
case TIOCSETA:
/* Set the termios attributes. */
r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &tp->tty_termios,
sizeof(struct termios));
if (r != OK) break;
setattr(tp);
break;
case TIOCFLUSH:
r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &i, sizeof(i));
if (r != OK) break;
if(i & FREAD) { tty_icancel(tp); }
if(i & FWRITE) { (*tp->tty_ocancel)(tp, 0); }
break;
case TIOCSTART:
tp->tty_inhibited = 0;
tp->tty_events = 1;
break;
case TIOCSTOP:
tp->tty_inhibited = 1;
tp->tty_events = 1;
break;
case TIOCSBRK: /* tcsendbreak - turn break on */
if (tp->tty_break_on != NULL) (*tp->tty_break_on)(tp,0);
break;
case TIOCCBRK: /* tcsendbreak - turn break off */
if (tp->tty_break_off != NULL) (*tp->tty_break_off)(tp,0);
break;
case TIOCGWINSZ:
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &tp->tty_winsize,
sizeof(struct winsize));
break;
case TIOCSWINSZ:
r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &tp->tty_winsize,
sizeof(struct winsize));
sigchar(tp, SIGWINCH, 0);
break;
case TIOCGETD: /* get line discipline */
i = TTYDISC;
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &i, sizeof(i));
break;
case TIOCSETD: /* set line discipline */
printf("TTY: TIOCSETD: can't set any other line discipline.\n");
r = ENOTTY;
break;
case TIOCGLINED: /* get line discipline as string */
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) lined, sizeof(lined));
break;
case TIOCGQSIZE: /* get input/output queue sizes */
/* Not sure what to report here. We are using input and output queues
* of different sizes, and the IOCTL allows for one single value for
* both. So far this seems to be just for stty(1) so let's just report
* the larger of the two. TODO: revisit queue sizes altogether.
*/
i = MAX(TTY_IN_BYTES, TTY_OUT_BYTES);
r = sys_safecopyto(endpt, grant, 0, (vir_bytes) &i, sizeof(i));
break;
case TIOCSCTTY:
/* Process sets this tty as its controlling tty */
tp->tty_pgrp = user_endpt;
break;
/* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
* not defined.
*/
case TIOCGPGRP:
case TIOCSPGRP:
default:
r = ENOTTY;
}
return r;
}
/*===========================================================================*
* do_open *
*===========================================================================*/
static int do_open(devminor_t minor, int access, endpoint_t user_endpt)
{
/* A tty line has been opened. Make it the callers controlling tty if
* CDEV_NOCTTY is *not* set and it is not the log device. CDEV_CTTY is returned
* if the tty is made the controlling tty, otherwise OK or an error code.
*/
tty_t *tp;
int r = OK;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
if (!(access & CDEV_NOCTTY)) {
tp->tty_pgrp = user_endpt;
r = CDEV_CTTY;
}
tp->tty_openct++;
if (tp->tty_openct == 1) {
/* Tell the device that the tty is opened */
(*tp->tty_open)(tp, 0);
}
return r;
}
/*===========================================================================*
* do_close *
*===========================================================================*/
static int do_close(devminor_t minor)
{
/* A tty line has been closed. Clean up the line if it is the last close. */
tty_t *tp;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
if (--tp->tty_openct == 0) {
tp->tty_pgrp = 0;
tty_icancel(tp);
(*tp->tty_ocancel)(tp, 0);
(*tp->tty_close)(tp, 0);
tp->tty_termios = termios_defaults;
tp->tty_winsize = winsize_defaults;
setattr(tp);
}
return OK;
}
/*===========================================================================*
* do_cancel *
*===========================================================================*/
static int do_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id)
{
/* A signal has been sent to a process that is hanging trying to read or write.
* The pending read or write must be finished off immediately.
*/
tty_t *tp;
int r;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
/* Check the parameters carefully, to avoid cancelling twice. */
r = EDONTREPLY;
if (tp->tty_inleft != 0 && endpt == tp->tty_incaller && id == tp->tty_inid) {
/* Process was reading when killed. Clean up input. */
tty_icancel(tp);
r = tp->tty_incum > 0 ? tp->tty_incum : EAGAIN;
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_incaller = NONE;
} else if (tp->tty_outleft != 0 && endpt == tp->tty_outcaller &&
id == tp->tty_outid) {
/* Process was writing when killed. Clean up output. */
r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
tp->tty_outleft = tp->tty_outcum = 0;
tp->tty_outcaller = NONE;
} else if (tp->tty_ioreq != 0 && endpt == tp->tty_iocaller &&
id == tp->tty_ioid) {
/* Process was waiting for output to drain. */
r = EINTR;
tp->tty_ioreq = 0;
tp->tty_iocaller = NONE;
}
if (r != EDONTREPLY)
tp->tty_events = 1;
/* Only reply if we found a matching request. */
return r;
}
int select_try(struct tty *tp, int ops)
{
int ready_ops = 0;
/* Special case. If line is hung up, no operations will block.
* (and it can be seen as an exceptional condition.)
*/
if (tp->tty_termios.c_ospeed == B0) {
ready_ops |= ops;
}
if (ops & CDEV_OP_RD) {
/* will i/o not block on read? */
if (tp->tty_inleft > 0) {
ready_ops |= CDEV_OP_RD; /* EIO - no blocking */
} else if (tp->tty_incount > 0) {
/* Is a regular read possible? tty_incount
* says there is data. But a read will only succeed
* in canonical mode if a newline has been seen.
*/
if (!(tp->tty_termios.c_lflag & ICANON) ||
tp->tty_eotct > 0) {
ready_ops |= CDEV_OP_RD;
}
}
}
if (ops & CDEV_OP_WR) {
if (tp->tty_outleft > 0) ready_ops |= CDEV_OP_WR;
else if ((*tp->tty_devwrite)(tp, 1)) ready_ops |= CDEV_OP_WR;
}
return ready_ops;
}
int select_retry(struct tty *tp)
{
int ops;
if (tp->tty_select_ops && (ops = select_try(tp, tp->tty_select_ops))) {
chardriver_reply_select(tp->tty_select_proc,
tp->tty_select_minor, ops);
tp->tty_select_ops &= ~ops;
}
return OK;
}
/*===========================================================================*
* do_select *
*===========================================================================*/
static int do_select(devminor_t minor, unsigned int ops, endpoint_t endpt)
{
tty_t *tp;
int ready_ops, watch;
if ((tp = line2tty(minor)) == NULL)
return ENXIO;
watch = (ops & CDEV_NOTIFY);
ops &= (CDEV_OP_RD | CDEV_OP_WR | CDEV_OP_ERR);
ready_ops = select_try(tp, ops);
ops &= ~ready_ops;
if (ops && watch) {
/* Translated minor numbers are a problem with late select replies. We
* have to save the minor number used to do the select, since otherwise
* VFS won't be able to make sense of those late replies. We do not
* support selecting on two different minors for the same object.
*/
if (tp->tty_select_ops != 0 && tp->tty_select_minor != minor) {
printf("TTY: select on one object with two minors (%d, %d)\n",
tp->tty_select_minor, minor);
return EBADF;
}
tp->tty_select_ops |= ops;
tp->tty_select_proc = endpt;
tp->tty_select_minor = minor;
}
return ready_ops;
}
/*===========================================================================*
* handle_events *
*===========================================================================*/
void handle_events(tp)
tty_t *tp; /* TTY to check for events. */
{
/* Handle any events pending on a TTY. */
do {
tp->tty_events = 0;
/* Read input and perform input processing. */
(*tp->tty_devread)(tp, 0);
/* Perform output processing and write output. */
(*tp->tty_devwrite)(tp, 0);
/* Ioctl waiting for some event? */
if (tp->tty_ioreq != 0) dev_ioctl(tp);
} while (tp->tty_events);
/* Transfer characters from the input queue to a waiting process. */
in_transfer(tp);
/* Reply if enough bytes are available. */
if (tp->tty_incum >= tp->tty_min && tp->tty_inleft > 0) {
chardriver_reply_task(tp->tty_incaller, tp->tty_inid, tp->tty_incum);
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_incaller = NONE;
}
if (tp->tty_select_ops)
{
select_retry(tp);
}
select_retry_pty(tp);
}
/*===========================================================================*
* in_transfer *
*===========================================================================*/
static void in_transfer(tp)
register tty_t *tp; /* pointer to terminal to read from */
{
/* Transfer bytes from the input queue to a process reading from a terminal. */
int ch;
int count;
char buf[64], *bp;
/* Force read to succeed if the line is hung up, looks like EOF to reader. */
if (tp->tty_termios.c_ospeed == B0) tp->tty_min = 0;
/* Anything to do? */
if (tp->tty_inleft == 0 || tp->tty_eotct < tp->tty_min) return;
bp = buf;
while (tp->tty_inleft > 0 && tp->tty_eotct > 0) {
ch = *tp->tty_intail;
if (!(ch & IN_EOF)) {
/* One character to be delivered to the user. */
*bp = ch & IN_CHAR;
tp->tty_inleft--;
if (++bp == bufend(buf)) {
/* Temp buffer full, copy to user space. */
sys_safecopyto(tp->tty_incaller,
tp->tty_ingrant, tp->tty_incum,
(vir_bytes) buf, (vir_bytes) buflen(buf));
tp->tty_incum += buflen(buf);
bp = buf;
}
}
/* Remove the character from the input queue. */
if (++tp->tty_intail == bufend(tp->tty_inbuf))
tp->tty_intail = tp->tty_inbuf;
tp->tty_incount--;
if (ch & IN_EOT) {
tp->tty_eotct--;
/* Don't read past a line break in canonical mode. */
if (tp->tty_termios.c_lflag & ICANON) tp->tty_inleft = 0;
}
}
if (bp > buf) {
/* Leftover characters in the buffer. */
count = bp - buf;
sys_safecopyto(tp->tty_incaller, tp->tty_ingrant, tp->tty_incum,
(vir_bytes) buf, (vir_bytes) count);
tp->tty_incum += count;
}
/* Usually reply to the reader, possibly even if incum == 0 (EOF). */
if (tp->tty_inleft == 0) {
chardriver_reply_task(tp->tty_incaller, tp->tty_inid, tp->tty_incum);
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_incaller = NONE;
}
}
/*===========================================================================*
* in_process *
*===========================================================================*/
int in_process(tp, buf, count)
register tty_t *tp; /* terminal on which character has arrived */
char *buf; /* buffer with input characters */
int count; /* number of input characters */
{
/* Characters have just been typed in. Process, save, and echo them. Return
* the number of characters processed.
*/
int ch, sig, ct;
int timeset = FALSE;
for (ct = 0; ct < count; ct++) {
/* Take one character. */
ch = *buf++ & BYTE;
/* Strip to seven bits? */
if (tp->tty_termios.c_iflag & ISTRIP) ch &= 0x7F;
/* Input extensions? */
if (tp->tty_termios.c_lflag & IEXTEN) {
/* Previous character was a character escape? */
if (tp->tty_escaped) {
tp->tty_escaped = NOT_ESCAPED;
ch |= IN_ESC; /* protect character */
}
/* LNEXT (^V) to escape the next character? */
if (ch == tp->tty_termios.c_cc[VLNEXT]) {
tp->tty_escaped = ESCAPED;
rawecho(tp, '^');
rawecho(tp, '\b');
continue; /* do not store the escape */
}
/* REPRINT (^R) to reprint echoed characters? */
if (ch == tp->tty_termios.c_cc[VREPRINT]) {
reprint(tp);
continue;
}
}
/* _POSIX_VDISABLE is a normal character value, so better escape it. */
if (ch == _POSIX_VDISABLE) ch |= IN_ESC;
/* Map CR to LF, ignore CR, or map LF to CR. */
if (ch == '\r') {
if (tp->tty_termios.c_iflag & IGNCR) continue;
if (tp->tty_termios.c_iflag & ICRNL) ch = '\n';
} else
if (ch == '\n') {
if (tp->tty_termios.c_iflag & INLCR) ch = '\r';
}
/* Canonical mode? */
if (tp->tty_termios.c_lflag & ICANON) {
/* Erase processing (rub out of last character). */
if (ch == tp->tty_termios.c_cc[VERASE]) {
(void) back_over(tp);
if (!(tp->tty_termios.c_lflag & ECHOE)) {
(void) tty_echo(tp, ch);
}
continue;
}
/* Kill processing (remove current line). */
if (ch == tp->tty_termios.c_cc[VKILL]) {
while (back_over(tp)) {}
if (!(tp->tty_termios.c_lflag & ECHOE)) {
(void) tty_echo(tp, ch);
if (tp->tty_termios.c_lflag & ECHOK)
rawecho(tp, '\n');
}
continue;
}
/* EOF (^D) means end-of-file, an invisible "line break". */
if (ch == tp->tty_termios.c_cc[VEOF]) ch |= IN_EOT | IN_EOF;
/* The line may be returned to the user after an LF. */
if (ch == '\n') ch |= IN_EOT;
/* Same thing with EOL, whatever it may be. */
if (ch == tp->tty_termios.c_cc[VEOL]) ch |= IN_EOT;
}
/* Start/stop input control? */
if (tp->tty_termios.c_iflag & IXON) {
/* Output stops on STOP (^S). */
if (ch == tp->tty_termios.c_cc[VSTOP]) {
tp->tty_inhibited = STOPPED;
tp->tty_events = 1;
continue;
}
/* Output restarts on START (^Q) or any character if IXANY. */
if (tp->tty_inhibited) {
if (ch == tp->tty_termios.c_cc[VSTART]
|| (tp->tty_termios.c_iflag & IXANY)) {
tp->tty_inhibited = RUNNING;
tp->tty_events = 1;
if (ch == tp->tty_termios.c_cc[VSTART])
continue;
}
}
}
if (tp->tty_termios.c_lflag & ISIG) {
/* Check for INTR, QUIT and STATUS characters. */
int sig = -1;
if (ch == tp->tty_termios.c_cc[VINTR])
sig = SIGINT;
else if(ch == tp->tty_termios.c_cc[VQUIT])
sig = SIGQUIT;
else if(ch == tp->tty_termios.c_cc[VSTATUS])
sig = SIGINFO;
if(sig >= 0) {
sigchar(tp, sig, 1);
(void) tty_echo(tp, ch);
continue;
}
}
/* Is there space in the input buffer? */
if (tp->tty_incount == buflen(tp->tty_inbuf)) {
/* No space; discard in canonical mode, keep in raw mode. */
if (tp->tty_termios.c_lflag & ICANON) continue;
break;
}
if (!(tp->tty_termios.c_lflag & ICANON)) {
/* In raw mode all characters are "line breaks". */
ch |= IN_EOT;
/* Start an inter-byte timer? */
if (!timeset && tp->tty_termios.c_cc[VMIN] > 0
&& tp->tty_termios.c_cc[VTIME] > 0) {
settimer(tp, TRUE);
timeset = TRUE;
}
}
/* Perform the intricate function of echoing. */
if (tp->tty_termios.c_lflag & (ECHO|ECHONL)) ch = tty_echo(tp, ch);
/* Save the character in the input queue. */
*tp->tty_inhead++ = ch;
if (tp->tty_inhead == bufend(tp->tty_inbuf))
tp->tty_inhead = tp->tty_inbuf;
tp->tty_incount++;
if (ch & IN_EOT) tp->tty_eotct++;
/* Try to finish input if the queue threatens to overflow. */
if (tp->tty_incount == buflen(tp->tty_inbuf)) in_transfer(tp);
}
return ct;
}
/*===========================================================================*
* echo *
*===========================================================================*/
static int tty_echo(tp, ch)
register tty_t *tp; /* terminal on which to echo */
register int ch; /* pointer to character to echo */
{
/* Echo the character if echoing is on. Some control characters are echoed
* with their normal effect, other control characters are echoed as "^X",
A594
* normal characters are echoed normally. EOF (^D) is echoed, but immediately
* backspaced over. Return the character with the echoed length added to its
* attributes.
*/
int len, rp;
ch &= ~IN_LEN;
if (!(tp->tty_termios.c_lflag & ECHO)) {
if (ch == ('\n' | IN_EOT) && (tp->tty_termios.c_lflag
& (ICANON|ECHONL)) == (ICANON|ECHONL))
(*tp->tty_echo)(tp, '\n');
return(ch);
}
/* "Reprint" tells if the echo output has been messed up by other output. */
rp = tp->tty_incount == 0 ? FALSE : tp->tty_reprint;
if ((ch & IN_CHAR) < ' ') {
switch (ch & (IN_ESC|IN_EOF|IN_EOT|IN_CHAR)) {
case '\t':
len = 0;
do {
(*tp->tty_echo)(tp, ' ');
len++;
} while (len < TAB_SIZE && (tp->tty_position & TAB_MASK) != 0);
break;
case '\r' | IN_EOT:
case '\n' | IN_EOT:
(*tp->tty_echo)(tp, ch & IN_CHAR);
len = 0;
break;
default:
(*tp->tty_echo)(tp, '^');
(*tp->tty_echo)(tp, '@' + (ch & IN_CHAR));
len = 2;
}
} else
if ((ch & IN_CHAR) == '\177') {
/* A DEL prints as "^?". */
(*tp->tty_echo)(tp, '^');
(*tp->tty_echo)(tp, '?');
len = 2;
} else {
(*tp->tty_echo)(tp, ch & IN_CHAR);
len = 1;
}
if (ch & IN_EOF) while (len > 0) { (*tp->tty_echo)(tp, '\b'); len--; }
tp->tty_reprint = rp;
return(ch | (len << IN_LSHIFT));
}
/*===========================================================================*
* rawecho *
*===========================================================================*/
static void rawecho(tp, ch)
register tty_t *tp;
int ch;
{
/* Echo without interpretation if ECHO is set. */
int rp = tp->tty_reprint;
if (tp->tty_termios.c_lflag & ECHO) (*tp->tty_echo)(tp, ch);
tp->tty_reprint = rp;
}
/*===========================================================================*
* back_over *
*===========================================================================*/
static int back_over(tp)
register tty_t *tp;
{
/* Backspace to previous character on screen and erase it. */
u16_t *head;
int len;
if (tp->tty_incount == 0) return(0); /* queue empty */
head = tp->tty_inhead;
if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
if (*--head & IN_EOT) return(0); /* can't erase "line breaks" */
if (tp->tty_reprint) reprint(tp); /* reprint if messed up */
tp->tty_inhead = head;
tp->tty_incount--;
if (tp->tty_termios.c_lflag & ECHOE) {
len = (*head & IN_LEN) >> IN_LSHIFT;
while (len > 0) {
rawecho(tp, '\b');
rawecho(tp, ' ');
rawecho(tp, '\b');
len--;
}
}
return(1); /* one character erased */
}
/*===========================================================================*
* reprint *
*===========================================================================*/
static void reprint(tp)
register tty_t *tp; /* pointer to tty struct */
{
/* Restore what has been echoed to screen before if the user input has been
* messed up by output, or if REPRINT (^R) is typed.
*/
int count;
u16_t *head;
tp->tty_reprint = FALSE;
/* Find the last line break in the input. */
head = tp->tty_inhead;
count = tp->tty_incount;
while (count > 0) {
if (head == tp->tty_inbuf) head = bufend(tp->tty_inbuf);
if (head[-1] & IN_EOT) break;
head--;
count--;
}
if (count == tp->tty_incount) return; /* no reason to reprint */
/* Show REPRINT (^R) and move to a new line. */