-
Notifications
You must be signed in to change notification settings - Fork 27
/
ReadKey.xs
1947 lines (1638 loc) · 44.6 KB
/
ReadKey.xs
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
/* -*-C-*- */
#define PERL_NO_GET_CONTEXT /* we want efficiency */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#define InputStream PerlIO *
/*******************************************************************
Copyright (C) 1994,1995,1996,1997 Kenneth Albanowski. Unlimited
distribution and/or modification is allowed as long as this copyright
notice remains intact.
Written by Kenneth Albanowski on Thu Oct 6 11:42:20 EDT 1994
Contact at [email protected] or CIS:70705,126
Maintained by Jonathan Stowe <[email protected]>
The below captures the history prior to it being in full time version
control:
$Id: ReadKey.xs,v 2.22 2005/01/11 21:15:17 jonathan Exp $
Version 2.21, Sun Jul 28 12:57:56 BST 2002
Fix to improve the chances of automated testing succeeding
Version 2.20, Tue May 21 07:52:47 BST 2002
Patch from Autrijus Tang fixing Win32 Breakage with bleadperl
Version 2.19, Thu Mar 21 07:25:31 GMT 2002
Added check for definedness of $_[0] in comparisons in ReadKey, ReadLine
after reports of warnings.
Version 2.18, Sun Feb 10 13:06:57 GMT 2002
Altered prototyping style after reports of compile failures on
Windows.
Version 2.17, Fri Jan 25 06:58:47 GMT 2002
The '_' macro for non-ANSI compatibility was removed in 5.7.2
Version 2.16, Thu Nov 29 21:19:03 GMT 2001
It appears that the genchars.pl bit of the patch didnt apply
Applied the new ppport.h from Devel::PPPort
Version 2.15, Sun Nov 4 15:02:37 GMT 2001 (jns)
Applied the patch in
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-01/msg01588.html
for PerlIO compatibility.
Version 2.14, Sun Mar 28 23:26:13 EST 1999
ppport.h 1.007 fixed for 5.005_55.
Version 2.13, Wed Mar 24 20:46:06 EST 1999
Adapted to ppport.h 1.006.
Version 2.12, Wed Jan 7 10:33:11 EST 1998
Slightly modified test and error reporting for Win32.
Version 2.11, Sun Dec 14 00:39:12 EST 1997
First attempt at Win32 support.
Version 2.10, skipped
Version 2.09, Tue Oct 7 13:07:43 EDT 1997
Grr. Added explicit detection of sys/poll.h and poll.h.
Version 2.08, Mon Oct 6 16:07:44 EDT 1997
Changed poll.h to sys/poll.h.
Version 2.07, Sun Jan 26 19:11:56 EST 1997
Added $VERSION to .pm.
Version 2.06, Tue Nov 26 01:47:09 EST 1996
Added PERLIO support and removed duplicate declaration in .pm.
Version 2.05, Tue Mar 12 19:08:33 EST 1996
Changed poll support so it works. Cleaned up .pm a little.
Version 2.04, Tue Oct 10 05:35:48 EDT 1995
Whoops. Changed GetTermSize back so that GSIZE code won't be
compiled if GWINSZ is being used. Also took ts_xxx and ts_yyy
out of GSIZE.
Version 2.03, Thu Sep 21 21:53:16 EDT 1995
Fixed up debugging info in Readkey.pm, and changed TermSizeVIO
to use _scrsize(). Hopefully this is GO for both Solaris and OS/2.
Version 2.02, Mon Sep 18 22:17:57 EDT 1995
Workaround for Solaris bug wasn't sufficient. Modularlized
GetTermSize into perl code, and added support for the
`resize` executable. Hard coded path for Solaris machines.
Version 2.01, Wed Sep 13 22:22:23 EDT 1995
Change error reporting around in getscreensize so that if
an ioctl fails but getenv succeeds, no warning will be
printed. This is an attempt to work around a Solaris bug where
TIOCGWINSZ fails in telnet sessions.
Version 2.00, Mon Sep 4 06:37:24 EDT 1995
Added timeouts to select/poll, added USE_STDIO_PTR support
(required for recent perl revisions), and fixed up compilation
under OS/2.
Version 1.99, Fri Aug 11 20:18:11 EDT 1995
Add file handles to ReadMode.
Version 1.97, Mon Apr 10 21:41:52 EDT 1995
Changed mode 5 to disable UC & delays. Added more ECHO flags.
Tested termio[s] & sgtty.
Added termoptions so test.pl can give more info.
Version 1.96,
Mucked with filehandle selection in ReadKey.pm.
Version 1.95,
Cleaning up for distribution.
Version 1.94,
Dealt with get/settermsize sillyness.
Version 1.91, Sat Mar 11 23:47:04 EST 1995:
Andy's patches, and a bit of termsize finesse.
Version 1.9, Thu Mar 9 14:11:49 EST 1995:
Modifying for portability. Prototypes, singed chars, etc.
Version 1.8, Mon Jan 9 23:18:14 EST 1995:
Added use of Configure.pm. No changes to ReadKey.
Version 1.7, Fri Dec 16 13:48:14 EST 1994:
Getting closer to release. Added new readmode 2. Had to bump up other
modes, unfortunately. This is the _last_ time I do that. If I have to
bump up the modes again, I'm switching to a different scheme.
Version 1.6, Wed Dec 14 17:36:59 EST 1994:
Completly reorganized the control-char support (twice!) so that
it is automatically ported by the preproccessor for termio[s], or
by an included script for sgtty. Logical defaults for sgtty are included
too. Added Sun TermSize support. (Hope I got it right.)
Version 1.5, Fri Dec 9 16:07:49 EST 1994:
Added SetTermSize, GetSpeeds, Get/SetControlChars, PerlIO support.
Version 1.01, Thu Oct 20 23:32:39 EDT 1994:
Added Select_fd_set_t casts to select() call.
Version 1.0: First "real" release. Everything seems cool.
*******************************************************************/
/***
Things to do:
Make sure the GetSpeed function is doing it's best to separate ispeed
from ospeed.
Separate the stty stuff from ReadMode, so that stty -a can be easily
used, among other things.
***/
/* Using these defines, you can elide anything you know
won't work properly */
/* Methods of doing non-blocking reads */
/*#define DONT_USE_SELECT*/
/*#define DONT_USE_POLL*/
/*#define DONT_USE_NODELAY*/
/* Terminal I/O packages */
/*#define DONT_USE_TERMIOS*/
/*#define DONT_USE_TERMIO*/
/*#define DONT_USE_SGTTY*/
/* IOCTLs that can be used for GetTerminalSize */
/*#define DONT_USE_GWINSZ*/
/*#define DONT_USE_GSIZE*/
/* IOCTLs that can be used for SetTerminalSize */
/*#define DONT_USE_SWINSZ*/
/*#define DONT_USE_SSIZE*/
/* This bit is for OS/2 */
#ifdef OS2
# define I_FCNTL
# define HAS_FCNTL
# define O_NODELAY O_NDELAY
# define DONT_USE_SELECT
# define DONT_USE_POLL
# define DONT_USE_TERMIOS
# define DONT_USE_SGTTY
# define I_TERMIO
# define CC_TERMIO
/* This flag should be off in the lflags when we enable termio mode */
# define TRK_IDEFAULT IDEFAULT
# define INCL_SUB
# define INCL_DOS
# include <os2.h>
# include <stdlib.h>
# define VIOMODE
#else
/* no os2 */
#endif
/* This bit is for Windows 95/NT */
#ifdef WIN32
# define DONT_USE_TERMIO
# define DONT_USE_TERMIOS
# define DONT_USE_SGTTY
# define DONT_USE_POLL
# define DONT_USE_SELECT
# define DONT_USE_NODELAY
# define USE_WIN32
# include <io.h>
# if defined(_get_osfhandle) && (PERL_VERSION == 4) && (PERL_SUBVERSION < 5)
# undef _get_osfhandle
# if defined(_MSC_VER)
# define level _cnt
# endif
# endif
#endif
/* This bit for NeXT */
#ifdef _NEXT_SOURCE
/* fcntl with O_NDELAY (FNDELAY, actually) is broken on NeXT */
# define DONT_USE_NODELAY
#endif
#if !defined(DONT_USE_NODELAY)
# ifdef HAS_FCNTL
# define Have_nodelay
# ifdef I_FCNTL
# include <fcntl.h>
# endif
# ifdef I_SYS_FILE
# include <sys/file.h>
# endif
# ifdef I_UNISTD
# include <unistd.h>
# endif
/* If any other headers are needed for fcntl or O_NODELAY, they need to get
included right here */
# if !defined(O_NODELAY)
# if !defined(FNDELAY)
# undef Have_nodelay
# else
# define O_NODELAY FNDELAY
# endif
# else
# define O_NODELAY O_NDELAY
# endif
# endif
#endif
#if !defined(DONT_USE_SELECT)
# ifdef HAS_SELECT
# ifdef I_SYS_SELECT
# include <sys/select.h>
# endif
/* If any other headers are likely to be needed for select, they need to be
included right here */
# define Have_select
# endif
#endif
#if !defined(DONT_USE_POLL)
# ifdef HAS_POLL
# ifdef HAVE_POLL_H
# include <poll.h>
# define Have_poll
# endif
# ifdef HAVE_SYS_POLL_H
# include <sys/poll.h>
# define Have_poll
# endif
# endif
#endif
#ifdef DONT_USE_TERMIOS
# ifdef I_TERMIOS
# undef I_TERMIOS
# endif
#endif
#ifdef DONT_USE_TERMIO
# ifdef I_TERMIO
# undef I_TERMIO
# endif
#endif
#ifdef DONT_USE_SGTTY
# ifdef I_SGTTY
# undef I_SGTTY
# endif
#endif
/* Pre-POSIX SVR3 systems sometimes define struct winsize in
sys/ptem.h. However, sys/ptem.h needs a type mblk_t (?) which
is defined in <sys/stream.h>.
No, Configure (dist3.051) doesn't know how to check for this.
*/
#ifdef I_SYS_STREAM
# include <sys/stream.h>
#endif
#ifdef I_SYS_PTEM
# include <sys/ptem.h>
#endif
#ifdef I_TERMIOS
# include <termios.h>
#else
# ifdef I_TERMIO
# include <termio.h>
# else
# ifdef I_SGTTY
# include <sgtty.h>
# endif
# endif
#endif
#ifdef I_TERMIOS
# define CC_TERMIOS
#else
# ifdef I_TERMIO
# define CC_TERMIO
# else
# ifdef I_SGTTY
# define CC_SGTTY
# endif
# endif
#endif
#ifndef TRK_IDEFAULT
/* This flag should be off in the lflags when we enable termio mode */
# define TRK_IDEFAULT 0
#endif
/* Fix up the disappearance of the '_' macro in Perl 5.7.2 */
#ifndef _
# ifdef CAN_PROTOTYPE
# define _(args) args
# else
# define _(args) ()
# endif
#endif
#define DisableFlush (1) /* Should flushing mode changes be enabled?
I think not for now. */
#define STDIN PerlIO_stdin()
#include "cchars.h"
STATIC int GetTermSizeVIO _((pTHX_ PerlIO * file,
int * retwidth, int * retheight,
int * xpix, int * ypix));
STATIC int GetTermSizeGWINSZ _((pTHX_ PerlIO * file,
int * retwidth, int * retheight,
int * xpix, int * ypix));
STATIC int GetTermSizeGSIZE _((pTHX_ PerlIO * file,
int * retwidth, int * retheight,
int * xpix, int * ypix));
STATIC int GetTermSizeWin32 _((pTHX_ PerlIO * file,
int * retwidth, int * retheight,
int * xpix, int * ypix));
STATIC int SetTerminalSize _((pTHX_ PerlIO * file,
int width, int height,
int xpix, int ypix));
STATIC void ReadMode _((pTHX_ PerlIO * file,int mode));
STATIC int pollfile _((pTHX_ PerlIO * file, double delay));
STATIC int setnodelay _((pTHX_ PerlIO * file, int mode));
STATIC int selectfile _((pTHX_ PerlIO * file, double delay));
STATIC int Win32PeekChar _((pTHX_ PerlIO * file, double delay, char * key));
STATIC int getspeed _((pTHX_ PerlIO * file, I32 *in, I32 * out ));
#ifdef VIOMODE
int GetTermSizeVIO(pTHX_ PerlIO *file,int *retwidth,int *retheight,int *xpix,int *ypix)
{
/*int handle=PerlIO_fileno(file);
static VIOMODEINFO *modeinfo = NULL;
if (modeinfo == NULL)
modeinfo = (VIOMODEINFO *)malloc(sizeof(VIOMODEINFO));
VioGetMode(modeinfo,0);
*retheight = modeinfo->row ?: 25;
*retwidth = modeinfo->col ?: 80;*/
int buf[2];
_scrsize(&buf[0]);
*retwidth = buf[0]; *retheight = buf[1];
*xpix = *ypix = 0;
return 0;
}
#else
int GetTermSizeVIO(pTHX_ PerlIO *file,int * retwidth,int *retheight, int *xpix,int *ypix)
{
croak("TermSizeVIO is not implemented on this architecture");
return 0;
}
#endif
#if defined(TIOCGWINSZ) && !defined(DONT_USE_GWINSZ)
int GetTermSizeGWINSZ(pTHX_ PerlIO *file,int *retwidth,int *retheight,int *xpix,int *ypix)
{
int handle=PerlIO_fileno(file);
struct winsize w;
if (ioctl (handle, TIOCGWINSZ, &w) == 0) {
*retwidth=w.ws_col; *retheight=w.ws_row;
*xpix=w.ws_xpixel; *ypix=w.ws_ypixel; return 0;
}
else {
return -1; /* failure */
}
}
#else
int GetTermSizeGWINSZ(pTHX_ PerlIO *file,int *retwidth,int *retheight,int *xpix,int *ypix)
{
croak("TermSizeGWINSZ is not implemented on this architecture");
return 0;
}
#endif
#if (!defined(TIOCGWINSZ) || defined(DONT_USE_GWINSZ)) && (defined(TIOCGSIZE) && !defined(DONT_USE_GSIZE))
int GetTermSizeGSIZE(pTHX_ PerlIO *file,int *retwidth,int *retheight,int *xpix,int *ypix)
{
int handle=PerlIO_fileno(file);
struct ttysize w;
if (ioctl (handle, TIOCGSIZE, &w) == 0) {
*retwidth=w.ts_cols; *retheight=w.ts_lines;
*xpix=0/*w.ts_xxx*/; *ypix=0/*w.ts_yyy*/; return 0;
}
else {
return -1; /* failure */
}
}
#else
int GetTermSizeGSIZE(pTHX_ PerlIO *file,int *retwidth,int *retheight,int *xpix,int *ypix)
{
croak("TermSizeGSIZE is not implemented on this architecture");
return 0;
}
#endif
#ifdef USE_WIN32
int GetTermSizeWin32(pTHX_ PerlIO *file,int *retwidth,int *retheight,int *xpix,int *ypix)
{
int handle=PerlIO_fileno(file);
HANDLE whnd = (HANDLE)_get_osfhandle(handle);
CONSOLE_SCREEN_BUFFER_INFO info;
if (GetConsoleScreenBufferInfo(whnd, &info) ||
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info)) {
/* Logic: return maximum possible screen width, but return
only currently selected height */
if (retwidth)
*retwidth = info.dwMaximumWindowSize.X;
/*info.srWindow.Right - info.srWindow.Left;*/
if (retheight)
*retheight = info.srWindow.Bottom + 1 - info.srWindow.Top;
if (xpix)
*xpix = 0;
if (ypix)
*ypix = 0;
return 0;
} else
return -1;
}
#else
int GetTermSizeWin32(pTHX_ PerlIO *file,int *retwidth,int *retheight,int *xpix,int *ypix)
{
croak("TermSizeWin32 is not implemented on this architecture");
return 0;
}
#endif /* USE_WIN32 */
STATIC int termsizeoptions() {
return 0
#ifdef VIOMODE
| 1
#endif
#if defined(TIOCGWINSZ) && !defined(DONT_USE_GWINSZ)
| 2
#endif
#if defined(TIOCGSIZE) && !defined(DONT_USE_GSIZE)
| 4
#endif
#if defined(USE_WIN32)
| 8
#endif
;
}
int SetTerminalSize(pTHX_ PerlIO *file,int width,int height,int xpix,int ypix)
{
int handle=PerlIO_fileno(file);
#ifdef VIOMODE
return -1;
#else
#if defined(TIOCSWINSZ) && !defined(DONT_USE_SWINSZ)
char buffer[10];
struct winsize w;
w.ws_col=width;
w.ws_row=height;
w.ws_xpixel=xpix;
w.ws_ypixel=ypix;
if (ioctl (handle, TIOCSWINSZ, &w) == 0) {
sprintf(buffer,"%d",width); /* Be polite to our children */
my_setenv("COLUMNS",buffer);
sprintf(buffer,"%d",height);
my_setenv("LINES",buffer);
return 0;
}
else {
croak("TIOCSWINSZ ioctl call to set terminal size failed: %s",Strerror(errno));
return -1;
}
#else
# if defined(TIOCSSIZE) && !defined(DONT_USE_SSIZE)
char buffer[10];
struct ttysize w;
w.ts_lines=height;
w.ts_cols=width;
w.ts_xxx=xpix;
w.ts_yyy=ypix;
if (ioctl (handle, TIOCSSIZE, &w) == 0) {
sprintf(buffer,"%d",width);
my_setenv("COLUMNS",buffer);
sprintf(buffer,"%d",height);
my_setenv("LINES",buffer);
return 0;
}
else {
croak("TIOCSSIZE ioctl call to set terminal size failed: %s",Strerror(errno));
return -1;
}
# else
/*sprintf(buffer,"%d",width) * Should we could do this and then *
my_setenv("COLUMNS",buffer) * said we succeeded? *
sprintf(buffer,"%d",height);
my_setenv("LINES",buffer)*/
return -1; /* Fail */
# endif
#endif
#endif
}
STATIC const I32 terminal_speeds[] = {
#ifdef B50
50, B50,
#endif
#ifdef B75
75, B75,
#endif
#ifdef B110
110, B110,
#endif
#ifdef B134
134, B134,
#endif
#ifdef B150
150, B150,
#endif
#ifdef B200
200, B200,
#endif
#ifdef B300
300, B300,
#endif
#ifdef B600
600, B600,
#endif
#ifdef B1200
1200, B1200,
#endif
#ifdef B1800
1800, B1800,
#endif
#ifdef B2400
2400, B2400,
#endif
#ifdef B4800
4800, B4800,
#endif
#ifdef B9600
9600, B9600,
#endif
#ifdef B19200
19200, B19200,
#endif
#ifdef B38400
38400, B38400,
#endif
#ifdef B57600
57600, B57600,
#endif
#ifdef B115200
115200, B115200,
#endif
#ifdef EXTA
19200, EXTA,
#endif
#ifdef EXTB
38400, EXTB,
#endif
#ifdef B0
0, B0,
#endif
-1,-1
};
int getspeed(pTHX_ PerlIO *file,I32 *in, I32 *out)
{
int handle=PerlIO_fileno(file);
#if defined(I_TERMIOS) || defined(I_TERMIO) || defined(I_SGTTY)
int i;
#endif
# ifdef I_TERMIOS
/* Posixy stuff */
struct termios buf;
tcgetattr(handle,&buf);
*in = *out = -1;
*in = cfgetispeed(&buf);
*out = cfgetospeed(&buf);
for(i=0;terminal_speeds[i]!=-1;i+=2) {
if(*in == terminal_speeds[i+1])
{ *in = terminal_speeds[i]; break; }
}
for(i=0;terminal_speeds[i]!=-1;i+=2) {
if(*out == terminal_speeds[i+1])
{ *out = terminal_speeds[i]; break; }
}
return 0;
# else
# ifdef I_TERMIO
/* SysV stuff */
struct termio buf;
ioctl(handle,TCGETA,&buf);
*in=*out=-1;
for(i=0;terminal_speeds[i]!=-1;i+=2) {
if((buf.c_cflag & CBAUD) == terminal_speeds[i+1])
{ *in=*out=terminal_speeds[i]; break; }
}
return 0;
# else
# ifdef I_SGTTY
/* BSD stuff */
struct sgttyb buf;
ioctl(handle,TIOCGETP,&buf);
*in=*out=-1;
for(i=0;terminal_speeds[i]!=-1;i+=2)
if(buf.sg_ospeed == terminal_speeds[i+1])
{ *out = terminal_speeds[i]; break; }
for(i=0;terminal_speeds[i]!=-1;i+=2)
if(buf.sg_ispeed == terminal_speeds[i+1])
{ *in = terminal_speeds[i]; break; }
return 0;
# else
/* No termio, termios or sgtty. I suppose we can try stty,
but it would be nice if you could get a better OS */
return -1;
# endif
# endif
# endif
}
#ifdef WIN32
struct tbuffer { DWORD Mode; };
#else
#ifdef I_TERMIOS
#define USE_TERMIOS
#define tbuffer termios
#else
#ifdef I_TERMIO
#define USE_TERMIO
#define tbuffer termio
#else
#ifdef I_SGTTY
#define USE_SGTTY
struct tbuffer {
struct sgttyb buf;
#if defined(TIOCGETC)
struct tchars tchar;
#endif
#if defined(TIOCGLTC)
struct ltchars ltchar;
#endif
#if defined(TIOCLGET)
int local;
#endif
};
#else
#define USE_STTY
struct tbuffer {
int dummy;
};
#endif
#endif
#endif
#endif
static HV * filehash; /* Used to store the original terminal settings for each handle*/
static HV * modehash; /* Used to record the current terminal "mode" for each handle*/
void ReadMode(pTHX_ PerlIO *file,int mode)
{
dTHR;
int handle;
int firsttime;
int oldmode;
struct tbuffer work;
struct tbuffer savebuf;
handle=PerlIO_fileno(file);
firsttime=!hv_exists(filehash, (char*)&handle, sizeof(int));
# ifdef WIN32
if (!GetConsoleMode((HANDLE)_get_osfhandle(handle), &work.Mode))
croak("GetConsoleMode failed, LastError=|%d|",GetLastError());
# endif /* WIN32 */
# ifdef USE_TERMIOS
/* Posixy stuff */
tcgetattr(handle,&work);
#endif
#ifdef USE_TERMIO
/* SysV stuff */
ioctl(handle,TCGETA,&work);
#endif
#ifdef USE_SGTTY
/* BSD stuff */
ioctl(handle,TIOCGETP,&work.buf);
# if defined(TIOCGETC)
ioctl(handle,TIOCGETC,&work.tchar);
# endif
# if defined(TIOCLGET)
ioctl(handle,TIOCLGET,&work.local);
# endif
# if defined(TIOCGLTC)
ioctl(handle,TIOCGLTC,&work.ltchar);
# endif
#endif
if(firsttime) {
firsttime=0;
memcpy((void*)&savebuf,(void*)&work,sizeof(struct tbuffer));
if(!hv_store(filehash,(char*)&handle,sizeof(int),
newSVpv((char*)&savebuf,sizeof(struct tbuffer)),0))
croak("Unable to stash terminal settings.\n");
if(!hv_store(modehash,(char*)&handle,sizeof(int),newSViv(0),0))
croak("Unable to stash terminal settings.\n");
} else {
SV ** temp;
if(!(temp=hv_fetch(filehash,(char*)&handle,sizeof(int),0)))
croak("Unable to retrieve stashed terminal settings.\n");
memcpy(&savebuf,SvPV(*temp,PL_na),sizeof(struct tbuffer));
if(!(temp=hv_fetch(modehash,(char*)&handle,sizeof(int),0)))
croak("Unable to retrieve stashed terminal mode.\n");
oldmode=SvIV(*temp);
}
#ifdef WIN32
switch (mode) {
case 5:
/* Should 5 disable ENABLE_WRAP_AT_EOL_OUTPUT? */
case 4:
work.Mode &= ~(ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_OUTPUT);
work.Mode |= 0;
break;
case 3:
work.Mode &= ~(ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT);
work.Mode |= ENABLE_PROCESSED_INPUT|ENABLE_PROCESSED_OUTPUT;
break;
case 2:
work.Mode &= ~(ENABLE_ECHO_INPUT);
work.Mode |= ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT|ENABLE_PROCESSED_OUTPUT;
break;
case 1:
work.Mode &= ~(0);
work.Mode |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT|ENABLE_PROCESSED_OUTPUT;
break;
case 0:
work = savebuf;
firsttime = 1;
break;
}
if (!SetConsoleMode((HANDLE)_get_osfhandle(handle), work.Mode))
croak("SetConsoleMode failed, LastError=|%d|",GetLastError());
#endif /* WIN32 */
#ifdef USE_TERMIOS
/* What, me worry about standards? */
# if !defined (VMIN)
# define VMIN VEOF
# endif
# if !defined (VTIME)
# define VTIME VEOL
# endif
# if !defined (IXANY)
# define IXANY (0)
# endif
#ifndef IEXTEN
#ifdef IDEFAULT
#define IEXTEN IDEFAULT
#endif
#endif
/* XXX Is ONLCR in POSIX?. The value of '4' seems to be the same for
both SysV and Sun, so it's probably rather general, and I'm not
aware of a POSIX way to do this otherwise.
*/
#ifndef ONLCR
# define ONLCR 4
#endif
#ifndef IMAXBEL
#define IMAXBEL 0
#endif
#ifndef ECHOE
#define ECHOE 0
#endif
#ifndef ECHOK
#define ECHOK 0
#endif
#ifndef ECHONL
#define ECHONL 0
#endif
#ifndef ECHOPRT
#define ECHOPRT 0
#endif
#ifndef FLUSHO
#define FLUSHO 0
#endif
#ifndef PENDIN
#define PENDIN 0
#endif
#ifndef ECHOKE
#define ECHOKE 0
#endif
#ifndef ONLCR
#define ONLCR 0
#endif
#ifndef OCRNL
#define OCRNL 0
#endif
#ifndef ONLRET
#define ONLRET 0
#endif
#ifndef IUCLC
#define IUCLC 0
#endif
#ifndef OPOST
#define OPOST 0
#endif
#ifndef OLCUC
#define OLCUC 0
#endif
#ifndef ECHOCTL
#define ECHOCTL 0
#endif
#ifndef XCASE
#define XCASE 0
#endif
#ifndef BRKINT
#define BRKINT 0
#endif
if(mode==5) {
/*\
* Disable everything except parity if needed.
\*/
/* Hopefully, this should put the tty into unbuffered mode
with signals and control characters (both posixy and normal)
disabled, along with flow control. Echo should be off.
CR/LF is not translated, along with 8-bit/parity */
memcpy((void*)&work,(void*)&savebuf,sizeof(struct tbuffer));
work.c_lflag &= ~(ICANON|ISIG|IEXTEN );
work.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ECHOCTL);
work.c_lflag &= ~(ECHOPRT|ECHOKE|FLUSHO|PENDIN|XCASE);
work.c_lflag |= NOFLSH;
work.c_iflag &= ~(IXOFF|IXON|IXANY|ICRNL|IMAXBEL|BRKINT);
if(((work.c_iflag & INPCK) != INPCK) ||
((work.c_cflag & PARENB) != PARENB)) {
work.c_iflag &= ~ISTRIP;
work.c_iflag |= IGNPAR;
work.c_iflag &= ~PARMRK;
}
work.c_oflag &= ~(OPOST |ONLCR|OCRNL|ONLRET);
work.c_cc[VTIME] = 0;
work.c_cc[VMIN] = 1;
}
else if(mode==4) {
/* Hopefully, this should put the tty into unbuffered mode
with signals and control characters (both posixy and normal)
disabled, along with flow control. Echo should be off.
About the only thing left unchanged is 8-bit/parity */