klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

ksystem.win32.c (1895B)


      1 /*
      2 ** ksystem.win32.c
      3 ** Platform dependent functionality - version for Windows.
      4 ** See Copyright Notice in klisp.h
      5 */
      6 
      7 #include <windows.h>
      8 #include <stdio.h>
      9 #include <io.h>
     10 #include "kobject.h"
     11 #include "kstate.h"
     12 #include "kinteger.h"
     13 #include "kport.h"
     14 #include "ksystem.h"
     15 
     16 /* declare implemented functionality */
     17 
     18 #define HAVE_PLATFORM_JIFFIES
     19 #define HAVE_PLATFORM_ISATTY
     20 
     21 /* jiffies */
     22 
     23 TValue ksystem_current_jiffy(klisp_State *K)
     24 {
     25     LARGE_INTEGER li;
     26     QueryPerformanceCounter(&li);
     27     return kinteger_new_uint64(K, li.QuadPart);
     28 }
     29 
     30 TValue ksystem_jiffies_per_second(klisp_State *K)
     31 {
     32     LARGE_INTEGER li;
     33     QueryPerformanceFrequency(&li);
     34     return kinteger_new_uint64(K, li.QuadPart);
     35 }
     36 
     37 bool ksystem_isatty(klisp_State *K, TValue port)
     38 {
     39     if (!ttisfport(port) || kport_is_closed(port))
     40         return false;
     41 
     42     /* get the underlying Windows File HANDLE */
     43 
     44     int fd = _fileno(kfport_file(port));
     45     if (fd == -1 || fd == -2)
     46         return false;
     47 
     48     HANDLE h = (HANDLE) _get_osfhandle(fd);
     49     if (h == INVALID_HANDLE_VALUE)
     50         return false;
     51 
     52     /* Googling gives two unreliable ways to emulate isatty():
     53      *
     54      *  1) test if GetFileType() returns FILE_TYPE_CHAR
     55      *    - reports NUL special file as a terminal
     56      *
     57      *  2) test if GetConsoleMode() succeeds
     58      *    - does not work on output handles
     59      *    - does not work in plain wine (works in wineconsole)
     60      *    - probably won't work if Windows Console is replaced
     61      *         a terminal emulator
     62 	 *
     63      *  3) _isatty()
     64      *    - calls GetFileType() internally (including MinGW version)
     65      *
     66      * TEMP: use GetConsoleMode()
     67      */
     68 /* 
     69 ** Lua uses _isatty in Windows, shouldn't that work?
     70 ** e.g. _isatty(_fileno(kport_file(port)))
     71 ** I'll try to test it when I have access to a Windows box
     72 ** Andres Navarro
     73 */
     74 
     75     DWORD mode;
     76     return GetConsoleMode(h, &mode);
     77 }