aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/fhandler.h
blob: 1fe6990d055284e94ef1e82e791d54b35b9af104 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
/* fhandler.h

   Copyright 1996, 1997, 1998, 1999, 2000, 2001 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#ifndef _FHANDLER_H_
#define _FHANDLER_H_

#include <sys/ioctl.h>

/* Classes

  Code is located in fhandler.cc unless another file name is given.

  fhandler_base		  normal I/O

     fhandler_disk_file
     fhandler_serial      Adds vmin and vtime.
     fhandler_dev_null    Not really I/O
     fhandler_dev_zero    Faked

     fhandler_dev_raw     (fhandler_raw.cc)
     fhandler_dev_floppy  (fhandler_floppy.cc)
     fhandler_dev_tape    (fhandler_tape.cc)

     fhandler_pipe
     fhandler_socket      (fhandler_socket.cc)

     fhandler_tty_slave   (tty.cc)
     fhandler_pty_master  (tty.cc)
     fhandler_tty_master  (tty.cc)

     fhandler_console     Out with ansi control. (console.cc)

     fhandler_windows	  Windows messages I/O (fhandler_windows.cc)

     fhandler_dev_random  /dev/[u]random implementation (fhandler_random.cc)

     fhandler_dev_mem     /dev/mem implementation (fhandler_mem.cc)

     fhandler_dev_clipboard	/dev/clipboard implementation (fhandler_clipboard.cc)

     fhandler_proc	  Interesting possibility, not implemented yet
*/

enum
{
  FH_RBINARY	= 0x00001000,	/* binary read mode */
  FH_WBINARY	= 0x00002000,	/* binary write mode */
  FH_CLOEXEC	= 0x00004000,	/* close-on-exec */
  FH_RBINSET	= 0x00008000,	/* binary read mode has been explicitly set */
  FH_WBINSET	= 0x00010000,	/* binary write mode has been explicitly set */
  FH_APPEND	= 0x00020000,	/* always append */
  FH_ASYNC	= 0x00040000,	/* async I/O */
  FH_SIGCLOSE	= 0x00080000,	/* signal handler should close fd on interrupt */

  FH_SYMLINK	= 0x00100000,	/* is a symlink */
  FH_EXECABL	= 0x00200000,	/* file looked like it would run:
				 * ends in .exe or .bat or begins with #! */
  FH_W95LSBUG	= 0x00400000,	/* set when lseek is called as a flag that
				 * _write should check if we've moved beyond
				 * EOF, zero filling if so. */
  FH_NOFRNAME	= 0x00800000,	/* Set if shouldn't free unix_path_name and
				   windows_path_name_ on destruction. */
  FH_NOEINTR	= 0x01000000,	/* Set if I/O should be uninterruptible. */
  FH_FFIXUP	= 0x02000000,	/* Set if need to fixup after fork. */
  FH_LOCAL	= 0x04000000,	/* File is unix domain socket */
  FH_SHUTRD	= 0x08000000,	/* Socket saw a SHUT_RD */
  FH_SHUTWR	= 0x10000000,	/* Socket saw a SHUT_WR */
  FH_ISREMOTE	= 0x10000000,	/* File is on a remote drive */
  FH_DCEXEC	= 0x20000000,	/* Don't care if this is executable */
  FH_HASACLS	= 0x40000000,	/* True if fs of file has ACLS */
  FH_QUERYOPEN	= 0x80000000,	/* open file without requesting either read
				   or write access */

  /* Device flags */

  /* Slow devices */
  FH_CONSOLE = 0x00000001,	/* is a console */
  FH_CONIN   = 0x00000002,	/* console input */
  FH_CONOUT  = 0x00000003,	/* console output */
  FH_TTYM    = 0x00000004,	/* is a tty master */
  FH_TTYS    = 0x00000005,	/* is a tty slave */
  FH_PTYM    = 0x00000006,	/* is a pty master */
  FH_SERIAL  = 0x00000007,	/* is a serial port */
  FH_PIPE    = 0x00000008,	/* is a pipe */
  FH_PIPER   = 0x00000009,	/* read end of a pipe */
  FH_PIPEW   = 0x0000000a,	/* write end of a pipe */
  FH_SOCKET  = 0x0000000b,	/* is a socket */
  FH_WINDOWS = 0x0000000c,	/* is a window */
  FH_SLOW    = 0x00000010,	/* "slow" device if below this */

  /* Fast devices */
  FH_DISK    = 0x00000010,	/* is a disk */
  FH_FLOPPY  = 0x00000011,	/* is a floppy */
  FH_TAPE    = 0x00000012,	/* is a tape */
  FH_NULL    = 0x00000013,	/* is the null device */
  FH_ZERO    = 0x00000014,	/* is the zero device */
  FH_RANDOM  = 0x00000015,	/* is a random device */
  FH_MEM     = 0x00000016,	/* is a mem device */
  FH_CLIPBOARD = 0x00000017, /* is a clipbaord device */
  FH_OSS_DSP = 0x00000018,	/* is a dsp audio device */

  FH_NDEV    = 0x00000019,	/* Maximum number of devices */
  FH_DEVMASK = 0x00000fff,	/* devices live here */
  FH_BAD     = 0xffffffff
};

#define FHDEVN(n)	((n) & FH_DEVMASK)
#define FHISSETF(x)	__ISSETF (this, x, FH)
#define FHSETF(x)	__SETF (this, x, FH)
#define FHCLEARF(x)	__CLEARF (this, x, FH)
#define FHCONDSETF(n, x) __CONDSETF(n, this, x, FH)

#define FHSTATOFF	0

/* fcntl flags used only internaly. */
#define O_NOSYMLINK 0x080000
#define O_DIROPEN   0x100000

/* newlib used to define O_NDELAY differently from O_NONBLOCK.  Now it
   properly defines both to be the same.  Unfortunately, we have to
   behave properly the old version, too, to accomodate older executables. */
#define OLD_O_NDELAY	(CYGWIN_VERSION_CHECK_FOR_OLD_O_NONBLOCK ? 4 : 0)

/* Care for the old O_NDELAY flag. If one of the flags is set,
   both flags are set. */
#define O_NONBLOCK_MASK (O_NONBLOCK | OLD_O_NDELAY)

extern const char *windows_device_names[];
extern struct __cygwin_perfile *perfile_table;
#define __fmode (*(user_data->fmode_ptr))

class select_record;
class path_conv;
class fhandler_disk_file;

enum bg_check_types
{
  bg_error = -1,
  bg_eof = 0,
  bg_ok = 1,
  bg_signalled = 2
};

enum executable_states
{
  is_executable,
  not_executable,
  dont_care_if_executable,
  dont_know_if_executable
};

class fhandler_base
{
protected:
  DWORD status;
public:
  int cb;
private:
  int access;
  HANDLE io_handle;

  unsigned long namehash;	/* hashed filename, used as inode num */

protected:
  /* Full unix path name of this file */
  /* File open flags from open () and fcntl () calls */
  int openflags;

  char *rabuf;		/* used for crlf conversion in text files */
  size_t ralen;
  size_t raixget;
  size_t raixput;
  size_t rabuflen;

  char *unix_path_name;
  char *win32_path_name;
  DWORD open_status;

public:
  void set_name (const char * unix_path, const char * win32_path = NULL,
		 int unit = 0);

  virtual fhandler_base& operator =(fhandler_base &x);
  fhandler_base (DWORD dev, const char *name = 0, int unit = 0);
  virtual ~fhandler_base ();

  /* Non-virtual simple accessor functions. */
  void set_io_handle (HANDLE x) { io_handle = x; }

  void set_cb (size_t size) { cb = size; }
  DWORD get_device () { return status & FH_DEVMASK; }
  virtual int get_unit () { return 0; }
  virtual BOOL is_slow () { return get_device () < FH_SLOW; }

  int get_access () { return access; }
  void set_access (int x) { access = x; }

  int get_async () { return FHISSETF (ASYNC); }
  void set_async (int x) { FHCONDSETF (x, ASYNC); }

  int get_flags () { return openflags; }
  void set_flags (int x) { openflags = x; }

  int is_nonblocking ();
  void set_nonblocking (int yes);

  int get_w_binary () { return FHISSETF (WBINARY); }
  int get_r_binary () { return FHISSETF (RBINARY); }

  int get_w_binset () { return FHISSETF (WBINSET); }
  int get_r_binset () { return FHISSETF (RBINSET); }

  void set_w_binary (int b) { FHCONDSETF (b, WBINARY); FHSETF (WBINSET); }
  void set_r_binary (int b) { FHCONDSETF (b, RBINARY); FHSETF (RBINSET); }
  void clear_w_binary () {FHCLEARF (WBINARY); FHCLEARF (WBINSET); }
  void clear_r_binary () {FHCLEARF (RBINARY); FHCLEARF (RBINSET); }
  void set_open_status () {open_status = status;}
  DWORD get_open_status () {return open_status;}
  void reset_to_open_binmode ()
  {
    status = status & ~(FH_WBINARY | FH_WBINSET | FH_RBINARY | FH_RBINSET);
    status = status | ((FH_WBINARY | FH_WBINSET | FH_RBINARY | FH_RBINSET)
		       & open_status);
  }

  int get_default_fmode (int flags);

  int get_r_no_interrupt () { return FHISSETF (NOEINTR); }
  void set_r_no_interrupt (int b) { FHCONDSETF (b, NOEINTR); }

  int get_close_on_exec () { return FHISSETF (CLOEXEC); }
  int set_close_on_exec_flag (int b) { return FHCONDSETF (b, CLOEXEC); }

  LPSECURITY_ATTRIBUTES get_inheritance (bool all = 0)
  {
    if (all)
      return get_close_on_exec () ? &sec_all_nih : &sec_all;
    else
      return get_close_on_exec () ? &sec_none_nih : &sec_none;
  }

  void set_check_win95_lseek_bug (int b = 1) { FHCONDSETF (b, W95LSBUG); }
  int get_check_win95_lseek_bug () { return FHISSETF (W95LSBUG); }

  int get_need_fork_fixup () { return FHISSETF (FFIXUP); }
  void set_need_fork_fixup () { FHSETF (FFIXUP); }

  virtual void set_close_on_exec (int val);

  virtual void fixup_before_fork_exec (DWORD) {}
  virtual void fixup_after_fork (HANDLE);
  virtual void fixup_after_exec (HANDLE) {}

  int get_symlink_p () { return FHISSETF (SYMLINK); }
  void set_symlink_p (int val) { FHCONDSETF (val, SYMLINK); }
  void set_symlink_p () { FHSETF (SYMLINK); }

  int get_socket_p () { return FHISSETF (LOCAL); }
  void set_socket_p (int val) { FHCONDSETF (val, LOCAL); }
  void set_socket_p () { FHSETF (LOCAL); }

  int get_execable_p () { return FHISSETF (EXECABL); }
  void set_execable_p (executable_states val)
  {
    FHCONDSETF (val == is_executable, EXECABL);
    FHCONDSETF (val == dont_care_if_executable, DCEXEC);
  }
  void set_execable_p () { FHSETF (EXECABL); }
  int dont_care_if_execable () { return FHISSETF (DCEXEC); }

  int get_append_p () { return FHISSETF (APPEND); }
  void set_append_p (int val) { FHCONDSETF (val, APPEND); }
  void set_append_p () { FHSETF (APPEND); }

  int get_query_open () { return FHISSETF (QUERYOPEN); }
  void set_query_open (int val) { FHCONDSETF (val, QUERYOPEN); }

  int get_readahead_valid () { return raixget < ralen; }
  int puts_readahead (const char *s, size_t len = (size_t) -1);
  int put_readahead (char value);

  int get_readahead ();
  int peek_readahead (int queryput = 0);

  int eat_readahead (int n);

  void set_readahead_valid (int val, int ch = -1);

  int get_readahead_into_buffer (char *buf, size_t buflen);

  int has_acls () { return FHISSETF (HASACLS); }
  void set_has_acls (int val) { FHCONDSETF (val, HASACLS); }

  int isremote () { return FHISSETF (ISREMOTE); }
  void set_isremote (int val) { FHCONDSETF (val, ISREMOTE); }

  int no_free_names () { return FHISSETF (NOFRNAME); }
  void set_no_free_names (int val) { FHCONDSETF (val, NOFRNAME); }
  void set_no_free_names () { FHSETF (NOFRNAME); }

  const char *get_name () { return unix_path_name; }
  const char *get_win32_name () { return win32_path_name; }
  unsigned long get_namehash () { return namehash; }

  virtual void hclose (HANDLE h) {CloseHandle (h);}
  virtual void set_inheritance (HANDLE &h, int not_inheriting);

  /* fixup fd possibly non-inherited handles after fork */
  void fork_fixup (HANDLE parent, HANDLE &h, const char *name);

  /* Potentially overridden virtual functions. */
  virtual int open (const char *, int flags, mode_t mode = 0)
  {
    return open (flags, mode);
  }
  virtual int open (path_conv& real_path, int flags, mode_t mode);
  virtual int open (int flags, mode_t mode = 0);
  virtual int close ();
  virtual int fstat (struct stat *buf) { return stat_dev (get_device (), get_unit (), get_namehash (), buf); }
  virtual int ioctl (unsigned int cmd, void *);
  virtual int fcntl (int cmd, void *);
  virtual char const * ttyname () { return get_name(); }
  virtual int read (void *ptr, size_t len);
  virtual int write (const void *ptr, size_t len);
  virtual off_t lseek (off_t offset, int whence);
  virtual int lock (int, struct flock *);
  virtual void dump ();
  virtual int dup (fhandler_base *child);

  virtual HANDLE mmap (caddr_t *addr, size_t len, DWORD access,
		       int flags, off_t off);
  virtual int munmap (HANDLE h, caddr_t addr, size_t len);
  virtual int msync (HANDLE h, caddr_t addr, size_t len, int flags);
  virtual BOOL fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset,
				      DWORD size, void *address);

  void *operator new (size_t, void *p) {return p;}

  virtual void init (HANDLE, DWORD, mode_t);

  virtual int tcflush (int);
  virtual int tcsendbreak (int);
  virtual int tcdrain ();
  virtual int tcflow (int);
  virtual int tcsetattr (int a, const struct termios *t);
  virtual int tcgetattr (struct termios *t);
  virtual int tcsetpgrp (const pid_t pid);
  virtual int tcgetpgrp ();
  virtual int is_tty () { return 0; }
  virtual BOOL is_device () { return TRUE; }
  virtual char *ptsname () { return NULL;}
  virtual class fhandler_socket *is_socket () { return 0; }
  virtual class fhandler_console *is_console () { return 0; }
  virtual int is_windows () {return 0; }

  virtual int raw_read (void *ptr, size_t ulen);
  virtual int raw_write (const void *ptr, size_t ulen);

  /* Virtual accessor functions to hide the fact
     that some fd's have two handles. */
  virtual HANDLE& get_handle () { return io_handle; }
  virtual HANDLE& get_io_handle () { return io_handle; }
  virtual HANDLE& get_output_handle () { return io_handle; }
  virtual BOOL hit_eof () {return FALSE;}
  virtual select_record *select_read (select_record *s);
  virtual select_record *select_write (select_record *s);
  virtual select_record *select_except (select_record *s);
  virtual int ready_for_read (int fd, DWORD howlong, int ignra);
  virtual const char * get_native_name ()
  {
    return windows_device_names[FHDEVN (status)];
  }
  virtual bg_check_types bg_check (int) {return bg_ok;}
  void clear_readahead ()
  {
    raixput = raixget = ralen = rabuflen = 0;
    rabuf = NULL;
  }
  void operator delete (void *);
};

class fhandler_socket: public fhandler_base
{
private:
  int addr_family;
  int connect_secret [4];
  HANDLE secret_event;
  struct _WSAPROTOCOL_INFOA *prot_info_ptr;

public:
  fhandler_socket (const char *name = 0);
  ~fhandler_socket ();
  int get_socket () { return (int) get_handle(); }
  fhandler_socket * is_socket () { return this; }

  bool saw_shutdown_read () const {return FHISSETF (SHUTRD);}
  bool saw_shutdown_write () const {return FHISSETF (SHUTWR);}

  void set_shutdown_read () {FHSETF (SHUTRD);}
  void set_shutdown_write () {FHSETF (SHUTWR);}

  int write (const void *ptr, size_t len);
  int read (void *ptr, size_t len);
  int ioctl (unsigned int cmd, void *);
  int fcntl (int cmd, void *);
  off_t lseek (off_t, int) { return 0; }
  int close ();
  void hclose (HANDLE) {close ();}
  int dup (fhandler_base *child);

  void set_close_on_exec (int val);
  virtual void fixup_before_fork_exec (DWORD);
  void fixup_after_fork (HANDLE);
  void fixup_after_exec (HANDLE);

  select_record *select_read (select_record *s);
  select_record *select_write (select_record *s);
  select_record *select_except (select_record *s);
  int ready_for_read (int fd, DWORD howlong, int ignra);
  int get_addr_family () {return addr_family;}
  void set_addr_family (int af) {addr_family = af;}
  void set_connect_secret ();
  void get_connect_secret (char*);
  HANDLE create_secret_event (int *secret = NULL);
  int check_peer_secret_event (struct sockaddr_in *peer, int *secret = NULL);
  void signal_secret_event ();
  void close_secret_event ();
};

class fhandler_pipe: public fhandler_base
{
  HANDLE guard;
public:
  fhandler_pipe (const char *name = 0, DWORD devtype = FH_PIPE);
  off_t lseek (off_t offset, int whence);
  BOOL is_slow () {return !wincap.has_unreliable_pipes ();}
  select_record *select_read (select_record *s);
  select_record *select_write (select_record *s);
  select_record *select_except (select_record *s);
  int ready_for_read (int fd, DWORD howlong, int ignra);
  void set_close_on_exec (int val);
  int read (void *ptr, size_t len);
  int close ();
  void create_guard (SECURITY_ATTRIBUTES *sa) {guard = CreateMutex (sa, FALSE, NULL);}
  int dup (fhandler_base *child);
};

class fhandler_dev_raw: public fhandler_base
{
protected:
  char *devbuf;
  size_t devbufsiz;
  size_t devbufstart;
  size_t devbufend;
  int eom_detected    : 1;
  int eof_detected    : 1;
  int lastblk_to_read : 1;
  int is_writing      : 1;
  int has_written     : 1;
  int varblkop	      : 1;
  int unit;

  virtual void clear (void);
  virtual int writebuf (void);

  /* returns not null, if `win_error' determines an end of media condition */
  virtual int is_eom(int win_error) = 0;
  /* returns not null, if `win_error' determines an end of file condition */
  virtual int is_eof(int win_error) = 0;

  fhandler_dev_raw (DWORD dev, const char *name, int unit);

public:
  ~fhandler_dev_raw (void);

  int open (const char *path, int flags, mode_t mode = 0);
  int close (void);

  int raw_read (void *ptr, size_t ulen);
  int raw_write (const void *ptr, size_t ulen);

  int fstat (struct stat *buf);

  int dup (fhandler_base *child);

  int ioctl (unsigned int cmd, void *buf);

  void fixup_after_fork (HANDLE);
  void fixup_after_exec (HANDLE);
};

class fhandler_dev_floppy: public fhandler_dev_raw
{
protected:
  virtual int is_eom (int win_error);
  virtual int is_eof (int win_error);

public:
  fhandler_dev_floppy (const char *name, int unit);

  virtual int open (const char *path, int flags, mode_t mode = 0);
  virtual int close (void);

  virtual off_t lseek (off_t offset, int whence);

  virtual int ioctl (unsigned int cmd, void *buf);
};

class fhandler_dev_tape: public fhandler_dev_raw
{
  int norewind;
  int lasterr;

protected:
  virtual void clear (void);

  virtual int is_eom (int win_error);
  virtual int is_eof (int win_error);

public:
  fhandler_dev_tape (const char *name, int unit);

  virtual int open (const char *path, int flags, mode_t mode = 0);
  virtual int close (void);

  virtual off_t lseek (off_t offset, int whence);

  virtual int fstat (struct stat *buf);

  virtual int dup (fhandler_base *child);

  virtual int ioctl (unsigned int cmd, void *buf);

private:
  int tape_write_marks (int marktype, DWORD len);
  int tape_get_pos (unsigned long *ret);
  int tape_set_pos (int mode, long count, BOOLEAN sfm_func = FALSE);
  int tape_erase (int mode);
  int tape_prepare (int action);
  BOOLEAN tape_get_feature (DWORD parm);
  int tape_get_blocksize (long *min, long *def, long *max, long *cur);
  int tape_set_blocksize (long count);
  int tape_status (struct mtget *get);
  int tape_compression (long count);
};

/* Standard disk file */

class fhandler_disk_file: public fhandler_base
{
public:
  fhandler_disk_file (const char *name);

  int open (const char *path, int flags, mode_t mode = 0);
  int open (path_conv& real_path, int flags, mode_t mode);
  int close ();
  int lock (int, struct flock *);
  BOOL is_device () { return FALSE; }
  int fstat (struct stat *buf);

  HANDLE mmap (caddr_t *addr, size_t len, DWORD access, int flags, off_t off);
  int munmap (HANDLE h, caddr_t addr, size_t len);
  int msync (HANDLE h, caddr_t addr, size_t len, int flags);
  BOOL fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset,
			      DWORD size, void *address);
};

class fhandler_serial: public fhandler_base
{
private:
  unsigned int vmin_;			/* from termios */
  unsigned int vtime_;			/* from termios */
  pid_t pgrp_;

public:
  int overlapped_armed;
  OVERLAPPED io_status;

  /* Constructor */
  fhandler_serial (const char *name, DWORD devtype = FH_SERIAL, int unit = 0);

  int open (const char *path, int flags, mode_t mode);
  int close ();
  void init (HANDLE h, DWORD a, mode_t flags);
  void overlapped_setup ();
  int dup (fhandler_base *child);
  int raw_read (void *ptr, size_t ulen);
  int raw_write (const void *ptr, size_t ulen);
  int tcsendbreak (int);
  int tcdrain ();
  int tcflow (int);
  int tcsetattr (int a, const struct termios *t);
  int tcgetattr (struct termios *t);
  off_t lseek (off_t, int) { return 0; }
  int tcflush (int);
  void dump ();
  int is_tty () { return 1; }
  void fixup_after_fork (HANDLE parent);
  void fixup_after_exec (HANDLE);

  /* We maintain a pgrp so that tcsetpgrp and tcgetpgrp work, but we
     don't use it for permissions checking.  fhandler_tty_slave does
     permission checking on pgrps.  */
  virtual int tcgetpgrp () { return pgrp_; }
  virtual int tcsetpgrp (const pid_t pid) { pgrp_ = pid; return 0; }
  select_record *select_read (select_record *s);
  select_record *select_write (select_record *s);
  select_record *select_except (select_record *s);
  int ready_for_read (int fd, DWORD howlong, int ignra);
};

#define acquire_output_mutex(ms) \
  __acquire_output_mutex (__PRETTY_FUNCTION__, __LINE__, ms);

#define release_output_mutex() \
  __release_output_mutex (__PRETTY_FUNCTION__, __LINE__);

class tty;
class tty_min;
class fhandler_termios: public fhandler_base
{
protected:
  HANDLE output_handle;
  virtual void doecho (const void *, DWORD) {};
  virtual int accept_input () {return 1;};
public:
  tty_min *tc;
  fhandler_termios (DWORD dev, const char *name = 0, int unit = 0) :
  fhandler_base (dev, name, unit)
  {
    set_need_fork_fixup ();
  }
  HANDLE& get_output_handle () { return output_handle; }
  int line_edit (const char *rptr, int nread, int always_accept = 0);
  void set_output_handle (HANDLE h) { output_handle = h; }
  void tcinit (tty_min *this_tc, int force = FALSE);
  virtual int is_tty () { return 1; }
  int tcgetpgrp ();
  int tcsetpgrp (int pid);
  bg_check_types bg_check (int sig);
  virtual DWORD __acquire_output_mutex (const char *fn, int ln, DWORD ms) {return 1;}
  virtual void __release_output_mutex (const char *fn, int ln) {}
  void fixup_after_fork (HANDLE);
  void fixup_after_exec (HANDLE parent) { fixup_after_fork (parent); }
  void echo_erase (int force = 0);
};

enum ansi_intensity
{
  INTENSITY_INVISIBLE,
  INTENSITY_DIM,
  INTENSITY_NORMAL,
  INTENSITY_BOLD
};

#define normal 1
#define gotesc 2
#define gotsquare 3
#define gotarg1 4
#define gotrsquare 5
#define gotcommand 6
#define gettitle 7
#define eattitle 8
#define MAXARGS 10

/* This is a input and output console handle */
class fhandler_console: public fhandler_termios
{
private:

  WORD default_color, underline_color, dim_color;

  /* Used to determine if an input keystroke should be modified with META. */
  int meta_mask;

/* Output state */
  int state_;
  int args_[MAXARGS];
  int nargs_;
  unsigned rarg;
  BOOL saw_question_mark;

  char my_title_buf [TITLESIZE + 1];

  WORD current_win32_attr;
  ansi_intensity intensity;
  BOOL underline, blink, reverse;
  WORD fg, bg;

  /* saved cursor coordinates */
  int savex, savey;

  /* saved screen */
  COORD savebufsiz;
  PCHAR_INFO savebuf;

  struct
    {
      short Top, Bottom;
    } scroll_region;
  struct
    {
      SHORT winTop;
      SHORT winBottom;
      COORD dwWinSize;
      COORD dwBufferSize;
      COORD dwCursorPosition;
      WORD wAttributes;
    } info;

  COORD dwLastCursorPosition;
  DWORD dwLastButtonState;
  int nModifiers;

  BOOL insert_mode;
  BOOL use_mouse;
  BOOL raw_win32_keyboard_mode;

/* Output calls */
  void set_default_attr ();
  WORD get_win32_attr ();

  BOOL fillin_info ();
  void clear_screen (int, int, int, int);
  void scroll_screen (int, int, int, int, int, int);
  void cursor_set (BOOL, int, int);
  void cursor_get (int *, int *);
  void cursor_rel (int, int);
  const unsigned char * write_normal (unsigned const char*, unsigned const char *);
  void char_command (char);
  BOOL set_raw_win32_keyboard_mode (BOOL);
  int output_tcsetattr (int a, const struct termios *t);

/* Input calls */
  int igncr_enabled ();
  int input_tcsetattr (int a, const struct termios *t);
  void set_cursor_maybe ();

public:

  fhandler_console (const char *name);

  fhandler_console* is_console () { return this; }

  int open (const char *path, int flags, mode_t mode = 0);

  int write (const void *ptr, size_t len);
  void doecho (const void *str, DWORD len) { (void) write (str, len); }
  int read (void *ptr, size_t len);
  int close ();

  int tcflush (int);
  int tcsetattr (int a, const struct termios *t);
  int tcgetattr (struct termios *t);

  /* Special dup as we must dup two handles */
  int dup (fhandler_base *child);

  int ioctl (unsigned int cmd, void *);
  void init (HANDLE, DWORD, mode_t);
  bool mouse_aware () {return use_mouse;}

  select_record *select_read (select_record *s);
  select_record *select_write (select_record *s);
  select_record *select_except (select_record *s);
  int ready_for_read (int fd, DWORD howlong, int ignra);
  void fixup_after_exec (HANDLE);
  void set_close_on_exec (int val);
  void fixup_after_fork (HANDLE parent);
  void set_input_state ();
};

class fhandler_tty_common: public fhandler_termios
{
public:
  fhandler_tty_common (DWORD dev, const char *name = 0, int unit = 0) :
    fhandler_termios (dev, name, unit),
    ttynum (unit)
  {
    // nothing to do
  }
  HANDLE output_done_event;	// Raised by master when tty's output buffer
				// written. Write status in tty::write_retval.
  HANDLE ioctl_request_event;	// Raised by slave to perform ioctl() request.
				// Ioctl() request in tty::cmd/arg.
  HANDLE ioctl_done_event;	// Raised by master on ioctl() completion.
				// Ioctl() status in tty::ioctl_retval.
  HANDLE output_mutex, input_mutex;
  HANDLE input_available_event;
  HANDLE inuse;			// used to indicate that a tty is in use


  DWORD __acquire_output_mutex (const char *fn, int ln, DWORD ms);
  void __release_output_mutex (const char *fn, int ln);

  int ttynum;			// Master tty num.
  virtual int dup (fhandler_base *child);

  tty *get_ttyp () { return (tty *)tc; }
  int get_unit () { return ttynum; }

  int close ();
  void set_close_on_exec (int val);
  void fixup_after_fork (HANDLE parent);
  select_record *select_read (select_record *s);
  select_record *select_write (select_record *s);
  select_record *select_except (select_record *s);
  int ready_for_read (int fd, DWORD howlong, int ignra);
};

class fhandler_tty_slave: public fhandler_tty_common
{
public:
  /* Constructor */
  fhandler_tty_slave (const char *name);
  fhandler_tty_slave (int, const char *name);

  int open (const char *path, int flags, mode_t mode = 0);
  int write (const void *ptr, size_t len);
  int read (void *ptr, size_t len);
  void init (HANDLE, DWORD, mode_t);

  int tcsetattr (int a, const struct termios *t);
  int tcgetattr (struct termios *t);
  int tcflush (int);
  int ioctl (unsigned int cmd, void *);

  off_t lseek (off_t, int) { return 0; }
  select_record *select_read (select_record *s);
  int ready_for_read (int fd, DWORD howlong, int ignra);
};

class fhandler_pty_master: public fhandler_tty_common
{
  int pktmode;			// non-zero if pty in a packet mode.
public:
  int need_nl;			// Next read should start with \n

  /* Constructor */
  fhandler_pty_master (const char *name, DWORD devtype = FH_PTYM, int unit = -1);

  int process_slave_output (char *buf, size_t len, int pktmode_on);
  void doecho (const void *str, DWORD len);
  int accept_input ();
  int open (const char *path, int flags, mode_t mode = 0);
  int write (const void *ptr, size_t len);
  int read (void *ptr, size_t len);
  int close ();

  int tcsetattr (int a, const struct termios *t);
  int tcgetattr (struct termios *t);
  int tcflush (int);
  int ioctl (unsigned int cmd, void *);

  off_t lseek (off_t, int) { return 0; }
  char *ptsname ();

  void set_close_on_exec (int val);
  BOOL hit_eof ();
};

class fhandler_tty_master: public fhandler_pty_master
{
public:
  /* Constructor */
  fhandler_tty_master (const char *name, int unit);
  fhandler_console *console;	// device handler to perform real i/o.
  HANDLE hThread;		// process_output thread handle.

  int init (int);
  int init_console ();
  void fixup_after_fork (HANDLE parent);
  void fixup_after_exec (HANDLE);
};

class fhandler_dev_null: public fhandler_base
{
public:
  fhandler_dev_null (const char *name);

  void dump ();
  select_record *select_read (select_record *s);
  select_record *select_write (select_record *s);
  select_record *select_except (select_record *s);
};

class fhandler_dev_zero: public fhandler_base
{
public:
  fhandler_dev_zero (const char *name);
  int open (const char *path, int flags, mode_t mode = 0);
  int write (const void *ptr, size_t len);
  int read (void *ptr, size_t len);
  off_t lseek (off_t offset, int whence);
  int close (void);

  void dump ();
};

class fhandler_dev_random: public fhandler_base
{
protected:
  int unit;
  HCRYPTPROV crypt_prov;
  long pseudo;

  BOOL crypt_gen_random (void *ptr, size_t len);
  int pseudo_write (const void *ptr, size_t len);
  int pseudo_read (void *ptr, size_t len);

public:
  fhandler_dev_random (const char *name, int unit);
  int get_unit () { return unit; }
  int open (const char *path, int flags, mode_t mode = 0);
  int write (const void *ptr, size_t len);
  int read (void *ptr, size_t len);
  off_t lseek (off_t offset, int whence);
  int close (void);
  int dup (fhandler_base *child);

  void dump ();
};

class fhandler_dev_mem: public fhandler_base
{
protected:
  int unit;
  DWORD mem_size;
  DWORD pos;

public:
  fhandler_dev_mem (const char *name, int unit);
  ~fhandler_dev_mem (void);

  int open (const char *path, int flags, mode_t mode = 0);
  int write (const void *ptr, size_t ulen);
  int read (void *ptr, size_t ulen);
  off_t lseek (off_t offset, int whence);
  int close (void);
  int fstat (struct stat *buf);
  int dup (fhandler_base *child);

  HANDLE mmap (caddr_t *addr, size_t len, DWORD access, int flags, off_t off);
  int munmap (HANDLE h, caddr_t addr, size_t len);
  int msync (HANDLE h, caddr_t addr, size_t len, int flags);
  BOOL fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset,
			      DWORD size, void *address);

  void dump ();
} ;

class fhandler_dev_clipboard: public fhandler_base
{
public:
  fhandler_dev_clipboard (const char *name);
  int is_windows (void) { return 1; }
  int open (const char *path, int flags, mode_t mode = 0);
  int write (const void *ptr, size_t len);
  int read (void *ptr, size_t len);
  off_t lseek (off_t offset, int whence);
  int close (void);

  int dup (fhandler_base *child);

  void dump ();

private:
  off_t pos;
  void *membuffer;
  size_t msize;
  BOOL eof;
};

class fhandler_windows: public fhandler_base
{
private:
  HWND hWnd_;	// the window whose messages are to be retrieved by read() call
  int method_;  // write method (Post or Send)
public:
  fhandler_windows (const char *name = 0);
  int is_windows (void) { return 1; }
  int open (const char *path, int flags, mode_t mode = 0);
  int write (const void *ptr, size_t len);
  int read (void *ptr, size_t len);
  int ioctl (unsigned int cmd, void *);
  off_t lseek (off_t, int) { return 0; }
  int close (void) { return 0; }

  void set_close_on_exec (int val);
  void fixup_after_fork (HANDLE parent);
  select_record *select_read (select_record *s);
  select_record *select_write (select_record *s);
  select_record *select_except (select_record *s);
  int ready_for_read (int fd, DWORD howlong, int ignra);
};

class fhandler_dev_dsp : public fhandler_base
{
private:
  int audioformat_;
  int audiofreq_;
  int audiobits_;
  int audiochannels_;
  bool setupwav(const char *pData, int nBytes);
public:
  fhandler_dev_dsp (const char *name = 0);
  ~fhandler_dev_dsp();

  int open (const char *path, int flags, mode_t mode = 0);
  int write (const void *ptr, size_t len);
  int read (void *ptr, size_t len);
  int ioctl (unsigned int cmd, void *);
  off_t lseek (off_t, int);
  int close (void);
  int dup (fhandler_base * child);
  void dump (void);
  void fixup_after_exec (HANDLE);
};

#if 0
/* You can't do this */
typedef union
{
  fhandler_normal normal;
  fhandler_dev_null dev_null;
  fhandler bare;
  fhandler_serial tty;
} fhandler_union;
#else
#define fhandler_union fhandler_console
#endif
struct select_record
{
  int fd;
  HANDLE h;
  fhandler_base *fh;
  BOOL saw_error;
  BOOL windows_handle;
  BOOL read_ready, write_ready, except_ready;
  BOOL read_selected, write_selected, except_selected;
  int (*startup) (select_record *me, class select_stuff *stuff);
  int (*poll) (select_record *me, fd_set *readfds, fd_set *writefds,
	       fd_set *exceptfds);
  int (*verify) (select_record *me, fd_set *readfds, fd_set *writefds,
		 fd_set *exceptfds);
  void (*cleanup) (select_record *me, class select_stuff *stuff);
  struct select_record *next;

  select_record (fhandler_base *in_fh = NULL) : fd (0), h (NULL),
		 fh (in_fh), saw_error (0), windows_handle (0),
		 read_ready (0), write_ready (0), except_ready (0),
		 read_selected (0), write_selected (0), except_selected (0),
		 startup (NULL), poll (NULL), verify (NULL), cleanup (NULL),
		 next (NULL) {}
};

class select_stuff
{
public:
  ~select_stuff ();
  select_stuff (): always_ready (0), windows_used (0), start (0)
  {
    memset (device_specific, 0, sizeof (device_specific));
  }
  BOOL always_ready, windows_used;
  select_record start;
  void *device_specific[FH_NDEV];

  int test_and_set (int i, fd_set *readfds, fd_set *writefds,
		     fd_set *exceptfds);
  int poll (fd_set *readfds, fd_set *writefds, fd_set *exceptfds);
  int wait (fd_set *readfds, fd_set *writefds, fd_set *exceptfds, DWORD ms);
  void cleanup ();
};

int __stdcall set_console_state_for_spawn ();

#endif /* _FHANDLER_H_ */