summaryrefslogtreecommitdiff
path: root/libraries/base/cbits/IOutils.c
blob: b5098d6540d01f21d6ab3d0a07cb8f805af36a8f (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
/*
 * (c) The GHC Team 2017-2018.
 *
 * I/O Utility functions for Windows.
 */

#include <stdbool.h>
#include <stdint.h>
#include <winsock2.h>
#include <windows.h>
#include <io.h>
#include <math.h>

/* Import some functions defined in base.  */
extern void maperrno(void);

/* Enum of Handle type.  */
typedef
enum HandleType
  {
    TYPE_CHAR,   // 0
    TYPE_DISK,   // 1
    TYPE_PIPE,   // 2
    TYPE_SOCKET, // 3
    TYPE_REMOTE, // 4
    TYPE_RAW,    // 5
    TYPE_UNKNOWN // 6
  } HANDLE_TYPE;

/*
 * handleReady(hwnd) checks to see whether input is available on the file
 * handle 'hwnd'.  Input meaning 'can I safely read at least a
 * *character* from this file object without blocking?'
 */
int
__handle_ready(HANDLE hFile, bool write, int msecs)
{
    DWORD handleType = GetFileType (hFile);

    DWORD rc;
        DWORD avail;

    switch (handleType)
      {
        case FILE_TYPE_CHAR:
        {
            INPUT_RECORD buf[1];
            DWORD count;

            /* A Console Handle will appear to be ready
             (WaitForSingleObject() returned WAIT_OBJECT_0) when
             it has events in its input buffer, but these events might
             not be keyboard events, so when we read from the Handle the
             read() will block.  So here we try to discard non-keyboard
             events from a console handle's input buffer and then try
             the WaitForSingleObject() again.
             Phyx: I'm worried that we're discarding events someone else may need.  */
            while (true) // keep trying until we find a real key event
            {
                rc = WaitForSingleObject( hFile, msecs );
                switch (rc)
                  {
                    case WAIT_TIMEOUT:
                        return false;
                    case WAIT_OBJECT_0:
                        break;
                    default:
                        /* WAIT_FAILED */
                        maperrno();
                        return -1;
                  }

                while (true) // discard non-key events
                {
                    /* I wonder if we can do better by grabbing a list of
                       input records at a time by using PeekConsoleInput.  */
                    rc = PeekConsoleInput(hFile, buf, 1, &count);
                    if (rc == 0) {
                        rc = GetLastError();
                        if (rc == ERROR_INVALID_HANDLE || rc == ERROR_INVALID_FUNCTION)
                            return true;
                        else {
                            maperrno();
                            return -1;
                        }
                    }

                    if (count == 0)
                        break; /* no more events => wait again.  */

                    /* discard console events that are not "key down", because
                       these will also be discarded by ReadFile().  */
                    if (buf[0].EventType == KEY_EVENT &&
                        buf[0].Event.KeyEvent.bKeyDown &&
                        buf[0].Event.KeyEvent.uChar.AsciiChar != '\0')
                          return true; /* it's a proper keypress.  */
                    else
                    {
                        /* it's a non-key event, a key up event, or a
                           non-character key (e.g. shift).  discard it.  */
                        rc = ReadConsoleInput(hFile, buf, 1, &count);
                        if (rc == 0) {
                            rc = GetLastError();
                            if (rc == ERROR_INVALID_HANDLE || rc == ERROR_INVALID_FUNCTION)
                                return true;
                            else {
                                maperrno();
                                return -1;
                            }
                        }
                    }
                }
            }
        }
        case FILE_TYPE_DISK:
            /* assume that disk files are always ready.  */
            return true;

        case FILE_TYPE_PIPE:
        {
            // Try to see if this is a socket
            //-------------------------
            // Create new event
            WSAEVENT newEvent = WSACreateEvent();

            //-------------------------
            // Associate event types FD_WRITE or FD_READ
            // with the listening socket and NewEvent
            rc = WSAEventSelect((SOCKET)hFile, newEvent, write ? FD_WRITE : FD_READ);

            if (rc == WSAENOTSOCK)
            {
                CloseHandle (newEvent);

                // WaitForMultipleObjects() doesn't work for pipes (it
                // always returns WAIT_OBJECT_0 even when no data is
                // available).  If the HANDLE is a pipe, therefore, we try
                // PeekNamedPipe:
                //
                rc = PeekNamedPipe( hFile, NULL, 0, NULL, &avail, NULL );
                if (rc != 0)
                    return avail != 0;
                else {
                    rc = GetLastError();
                    if (rc == ERROR_BROKEN_PIPE)
                        return true; // this is probably what we want

                    if (rc != ERROR_INVALID_HANDLE && rc != ERROR_INVALID_FUNCTION) {
                        maperrno();
                        return -1;
                    }
                }
                /* PeekNamedPipe didn't work - fall through to the general case */
            }
            else if (rc != 0)
            {
                CloseHandle (newEvent);
                // It seems to be a socket but can't determine the state.
                // Maybe not initialized. Either way, we know enough.
                return false;
            }

            // Wait for the socket event to trigger.
            rc = WaitForSingleObject( newEvent, msecs );
            CloseHandle (newEvent);

            /* 1 => Input ready, 0 => not ready, -1 => error */
            switch (rc)
              {
                case WAIT_TIMEOUT:
                    return false;
                case WAIT_OBJECT_0:
                    return true;
                default:
                {
                    /* WAIT_FAILED */
                    maperrno();
                    return -1;
                }
              }
        }
        default:
            rc = WaitForSingleObject( hFile, msecs );

            /* 1 => Input ready, 0 => not ready, -1 => error */
            switch (rc)
              {
                case WAIT_TIMEOUT:
                    return false;
                case WAIT_OBJECT_0:
                    return true;
                default:
                {
                    /* WAIT_FAILED */
                    maperrno();
                    return -1;
                }
              }
      }
}

bool
__is_console(HANDLE hFile)
{
    /* Broken handle can't be terminal */
    if (hFile == INVALID_HANDLE_VALUE)
        return false;

    DWORD handleType = GetFileType (hFile);

    /* TTY must be a character device */
    if (handleType != FILE_TYPE_CHAR)
        return false;

    DWORD st;
    /* GetConsoleMode appears to fail when it's not a TTY.  In
       particular, it's what most of our terminal functions
       assume works, so if it doesn't work for all intents
       and purposes we're not dealing with a terminal. */
    if (!GetConsoleMode(hFile, &st)) {
        /* Clear the error buffer before returning.  */
        SetLastError (ERROR_SUCCESS);
        return false;
    }

    return true;
}

#if !defined(ENABLE_VIRTUAL_TERMINAL_INPUT)
#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
#endif

bool
__set_console_buffering(HANDLE hFile, bool cooked)
{
    if (hFile == INVALID_HANDLE_VALUE) {
        return false;
    }

    DWORD  st;
    if (!GetConsoleMode(hFile, &st)) {
        return false;
    }

    /* According to GetConsoleMode() docs, it is not possible to
       leave ECHO_INPUT enabled without also having LINE_INPUT,
       so we have to turn both off here.
       We toggle ENABLE_VIRTUAL_TERMINAL_INPUT to enable us to receive
       virtual keyboard keys in ReadConsole.  */
    DWORD flgs = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
    DWORD enabled = (st & ~flgs) | ENABLE_VIRTUAL_TERMINAL_INPUT;
    DWORD disabled = (st | ENABLE_LINE_INPUT) & ~ENABLE_VIRTUAL_TERMINAL_INPUT;


        return SetConsoleMode(hFile, cooked ? enabled : disabled);
}

bool
__set_console_echo(HANDLE hFile, bool on)
{
    DWORD  st;
    DWORD flgs = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;

    if (hFile == INVALID_HANDLE_VALUE) {
        return false;
    }

        return GetConsoleMode(hFile, &st) &&
               SetConsoleMode(hFile, ( on ? (st | flgs) : (st & ~flgs)));
}

bool
__get_console_echo(HANDLE hFile)
{
    DWORD  st;

    if (hFile == INVALID_HANDLE_VALUE) {
        return false;
    }

        return GetConsoleMode(hFile, &st) &&
               (st & ENABLE_ECHO_INPUT) == ENABLE_ECHO_INPUT;
}

bool
__flush_input_console(HANDLE hFile)
{
    if ( hFile == INVALID_HANDLE_VALUE )
      return false;

        /* If the 'handle' isn't connected to a console; treat the flush
         * operation as a NOP.
         */
        DWORD unused;
        if ( !GetConsoleMode(hFile, &unused) &&
             GetLastError() == ERROR_INVALID_HANDLE ) {
            return false;
        }

        if ( FlushConsoleInputBuffer(hFile) )
            return true;

    maperrno();
    return false;
}

HANDLE_TYPE
__handle_type (HANDLE hFile)
{
    DWORD handleType = GetFileType (hFile);
    switch (handleType)
      {
        case FILE_TYPE_PIPE:
          {
            WSAEVENT newEvent = WSACreateEvent();
            DWORD rc = WSAEventSelect((SOCKET)hFile, newEvent, FD_CLOSE);
            CloseHandle (newEvent);
            if (rc == WSAENOTSOCK)
              return TYPE_SOCKET;
            else
              return TYPE_PIPE;
          }
        case FILE_TYPE_CHAR:
          return TYPE_CHAR;
        case FILE_TYPE_DISK:
          return TYPE_DISK;
        case FILE_TYPE_REMOTE:
          return TYPE_REMOTE;
        case FILE_TYPE_UNKNOWN:
        default:
          return TYPE_UNKNOWN;
      }
}

bool
__close_handle (HANDLE hFile)
{
    switch (__handle_type (hFile))
      {
        case TYPE_SOCKET:
            return closesocket ((SOCKET)hFile) == 0;
        default:
            return CloseHandle (hFile);
      }
}

bool __set_file_pointer (HANDLE hFile, int64_t pos, DWORD moveMethod,
                         int64_t* outPos)
{
    LARGE_INTEGER ret;
    LARGE_INTEGER li;
    li.QuadPart = pos;
    bool success = SetFilePointerEx (hFile, li, &ret, moveMethod)
                    != INVALID_SET_FILE_POINTER;
    *outPos = ret.QuadPart;
    return success;
}

int64_t __get_file_pointer (HANDLE hFile)
{
    LARGE_INTEGER ret;
    LARGE_INTEGER pos;
    pos.QuadPart = 0;
    if (SetFilePointerEx(hFile, pos, &ret, FILE_CURRENT)
        == INVALID_SET_FILE_POINTER)
      return -1;

    return ret.QuadPart;
}

int64_t __get_file_size (HANDLE hFile)
{
    /* Broken handle can't do stat.  */
    if (hFile == INVALID_HANDLE_VALUE)
        return false;

    switch (GetFileType (hFile))
    {
      case FILE_TYPE_CHAR:
      case FILE_TYPE_DISK:
        break;
      default:
        return -1;
    }

    LARGE_INTEGER ret;
    if (!GetFileSizeEx(hFile, &ret))
      return -1;

    return ret.QuadPart;
}

bool __set_file_size (HANDLE hFile, int64_t size)
{
    LARGE_INTEGER li;
    li.QuadPart = size;
    if(!SetFilePointerEx (hFile, li, NULL, FILE_BEGIN))
        return false;

    return SetEndOfFile (hFile);
}

bool __duplicate_handle (HANDLE hFile, HANDLE* hFileDup)
{
    switch (__handle_type (hFile))
    {
        case TYPE_SOCKET:
            // should use WSADuplicateSocket
            return false;
        default:
            return DuplicateHandle(GetCurrentProcess(),
                                hFile,
                                GetCurrentProcess(),
                                hFileDup,
                                0,
                                FALSE,
                                DUPLICATE_SAME_ACCESS);
    }
}

bool __set_console_pointer (HANDLE hFile, int64_t pos, DWORD moveMethod,
                            int64_t* outPos)
{
    CONSOLE_SCREEN_BUFFER_INFO info;
    if(!GetConsoleScreenBufferInfo (hFile, &info))
        return false;

    COORD point;
    switch (moveMethod)
    {
        case FILE_END:
          {
              int64_t end = info.dwSize.X * info.dwSize.Y;
              pos = end + pos;
              point = (COORD) { pos % info.dwSize.X, pos / info.dwSize.X };
              break;
          }
        case FILE_CURRENT:
          {
              int64_t current = (info.dwCursorPosition.Y * info.dwSize.X)
                              + info.dwCursorPosition.X;
              pos = current + pos;
              point = (COORD) { pos % info.dwSize.X, pos / info.dwSize.X };
              break;
          }
        case FILE_BEGIN:
        default:
          point = (COORD) { pos % info.dwSize.X, pos / info.dwSize.X };
          break;
    }

    *outPos = pos;
    return SetConsoleCursorPosition (hFile, point);
}

int64_t __get_console_pointer (HANDLE hFile)
{
    CONSOLE_SCREEN_BUFFER_INFO info;
    if(!GetConsoleScreenBufferInfo (hFile, &info))
        return -1;

    return (info.dwCursorPosition.Y * info.dwSize.X) + info.dwCursorPosition.X;
}

int64_t __get_console_buffer_size (HANDLE hFile)
{
    CONSOLE_SCREEN_BUFFER_INFO ret;
    if (!GetConsoleScreenBufferInfo(hFile, &ret))
      return -1;

    return ret.dwSize.X * ret.dwSize.Y;
}

bool __set_console_buffer_size (HANDLE hFile, int64_t size)
{
    CONSOLE_SCREEN_BUFFER_INFO ret;
    if (!GetConsoleScreenBufferInfo(hFile, &ret))
      return false;

    COORD sz = {ret.dwSize.X, (int)ceil(size / ret.dwSize.X)};
    return SetConsoleScreenBufferSize (hFile, sz);
}