| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
#define CAML_INTERNALS |
| 27 |
#include <caml/io.h> |
| 28 |
#include <caml/mlvalues.h> |
| 29 |
#include <caml/alloc.h> |
| 30 |
#include <caml/memory.h> |
| 31 |
#include <caml/callback.h> |
| 32 |
#include <windows.h> |
| 33 |
|
| 34 |
|
| 35 |
extern long _get_osfhandle(int); |
| 36 |
#define HANDLE_OF_CHAN(vchan) ((HANDLE) _get_osfhandle(Channel(vchan)->fd)) |
| 37 |
|
| 38 |
static HANDLE hStdout; |
| 39 |
static CONSOLE_SCREEN_BUFFER_INFO *csbiInfo = NULL; |
| 40 |
|
| 41 |
void raise_error(char *fname, char *msg) |
| 42 |
{ |
| 43 |
CAMLparam0(); |
| 44 |
CAMLlocal2(vfname, vmsg); |
| 45 |
static value *exn = NULL; |
| 46 |
value args[2]; |
| 47 |
|
| 48 |
if (exn == NULL) { |
| 49 |
|
| 50 |
exn = caml_named_value("ANSITerminal.Error"); |
| 51 |
} |
| 52 |
vfname = caml_copy_string(fname); |
| 53 |
vmsg = caml_copy_string(msg); |
| 54 |
args[0] = vfname; |
| 55 |
args[1] = vmsg; |
| 56 |
caml_raise_with_args(*exn, 2, args); |
| 57 |
CAMLnoreturn; |
| 58 |
} |
| 59 |
|
| 60 |
void exn_of_error(char *fname, BOOL cond) |
| 61 |
{ |
| 62 |
CAMLparam0(); |
| 63 |
CAMLlocal2(vfname, vmsg); |
| 64 |
char *msg, *p; |
| 65 |
LPVOID lpMsgBuf; |
| 66 |
static value *exn = NULL; |
| 67 |
value args[2]; |
| 68 |
|
| 69 |
if (cond) { |
| 70 |
if (exn == NULL) { |
| 71 |
|
| 72 |
exn = caml_named_value("ANSITerminal.Error"); |
| 73 |
} |
| 74 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 75 |
FORMAT_MESSAGE_FROM_SYSTEM | |
| 76 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 77 |
NULL, |
| 78 |
GetLastError(), |
| 79 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 80 |
(LPTSTR) &lpMsgBuf, |
| 81 |
0, NULL); |
| 82 |
vfname = caml_copy_string(fname); |
| 83 |
msg = (char *) lpMsgBuf; |
| 84 |
p = msg + strlen(msg) - 1; |
| 85 |
while(*p == '\n' || *p == '\r') { |
| 86 |
*p = '\0'; |
| 87 |
p--; |
| 88 |
} |
| 89 |
vmsg = caml_copy_string(msg); |
| 90 |
LocalFree(lpMsgBuf); |
| 91 |
args[0] = vfname; |
| 92 |
args[1] = vmsg; |
| 93 |
caml_raise_with_args(*exn, 2, args); |
| 94 |
} |
| 95 |
CAMLreturn0; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#define SET_CSBI(fname) \ |
| 101 |
if (! csbiInfo) { \ |
| 102 |
hStdout = GetStdHandle(STD_OUTPUT_HANDLE); \ |
| 103 |
if (hStdout == INVALID_HANDLE_VALUE) { \ |
| 104 |
raise_error(fname, "Invalid stdout handle"); \ |
| 105 |
} \ |
| 106 |
exn_of_error(fname, \ |
| 107 |
! GetConsoleScreenBufferInfo(hStdout, csbiInfo)); \ |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
#define SET_STYLE(fname) \ |
| 112 |
CAMLexport \ |
| 113 |
value ANSITerminal_ ## fname(value vchan, value vcode) \ |
| 114 |
{ \ |
| 115 |
HANDLE h = HANDLE_OF_CHAN(vchan); \ |
| 116 |
int code = Int_val(vcode); \ |
| 117 |
\ |
| 118 |
exn_of_error("ANSITerminal." #fname, \ |
| 119 |
! SetConsoleTextAttribute(h, code)); \ |
| 120 |
return Val_unit; \ |
| 121 |
} |
| 122 |
|
| 123 |
SET_STYLE(set_style) |
| 124 |
SET_STYLE(unset_style) |
| 125 |
|
| 126 |
CAMLexport |
| 127 |
value ANSITerminal_get_style(value vchan) |
| 128 |
{ |
| 129 |
HANDLE h = HANDLE_OF_CHAN(vchan); |
| 130 |
CONSOLE_SCREEN_BUFFER_INFO info; |
| 131 |
|
| 132 |
|
| 133 |
exn_of_error("ANSITerminal.set_style", |
| 134 |
! GetConsoleScreenBufferInfo(h, &info)); |
| 135 |
|
| 136 |
return(Val_int(info.wAttributes)); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
CAMLexport |
| 141 |
value ANSITerminal_pos(value vunit) |
| 142 |
{ |
| 143 |
CAMLparam1(vunit); |
| 144 |
CAMLlocal1(vpos); |
| 145 |
SMALL_RECT w; |
| 146 |
SHORT x, y; |
| 147 |
|
| 148 |
exn_of_error("ANSITerminal.pos_cursor", |
| 149 |
! GetConsoleScreenBufferInfo(hStdout, csbiInfo)); |
| 150 |
w = csbiInfo->srWindow; |
| 151 |
|
| 152 |
x = csbiInfo->dwCursorPosition.X - w.Left + 1; |
| 153 |
y = csbiInfo->dwCursorPosition.Y - w.Top + 1; |
| 154 |
|
| 155 |
vpos = caml_alloc_tuple(2); |
| 156 |
Store_field(vpos, 0, Val_int(x)); |
| 157 |
Store_field(vpos, 1, Val_int(y)); |
| 158 |
CAMLreturn(vpos); |
| 159 |
} |
| 160 |
|
| 161 |
CAMLexport |
| 162 |
value ANSITerminal_size(value vunit) |
| 163 |
{ |
| 164 |
CAMLparam1(vunit); |
| 165 |
CAMLlocal1(vsize); |
| 166 |
SMALL_RECT w; |
| 167 |
|
| 168 |
|
| 169 |
SET_CSBI("ANSITerminal.size"); |
| 170 |
exn_of_error("ANSITerminal.size", |
| 171 |
! GetConsoleScreenBufferInfo(hStdout, csbiInfo)); |
| 172 |
w = csbiInfo->srWindow; |
| 173 |
|
| 174 |
vsize = caml_alloc_tuple(2); |
| 175 |
Store_field(vsize, 0, Val_int(w.Right - w.Left + 1)); |
| 176 |
Store_field(vsize, 1, Val_int(w.Bottom - w.Top + 1)); |
| 177 |
|
| 178 |
CAMLreturn(vsize); |
| 179 |
} |
| 180 |
|
| 181 |
CAMLexport |
| 182 |
value ANSITerminal_resize(value vx, value vy) |
| 183 |
{ |
| 184 |
|
| 185 |
COORD dwSize; |
| 186 |
dwSize.X = Int_val(vx); |
| 187 |
dwSize.Y = Int_val(vy); |
| 188 |
exn_of_error("ANSITerminal.resize", |
| 189 |
! SetConsoleScreenBufferSize(hStdout, dwSize)); |
| 190 |
return Val_unit; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
CAMLexport |
| 195 |
value ANSITerminal_SetCursorPosition(value vx, value vy) |
| 196 |
{ |
| 197 |
COORD c; |
| 198 |
SMALL_RECT w; |
| 199 |
|
| 200 |
SET_CSBI("ANSITerminal.set_cursor"); |
| 201 |
exn_of_error("ANSITerminal.set_cursor", |
| 202 |
! GetConsoleScreenBufferInfo(hStdout, csbiInfo)); |
| 203 |
|
| 204 |
|
| 205 |
w = csbiInfo->srWindow; |
| 206 |
c.X = Int_val(vx) - 1 + w.Left; |
| 207 |
c.Y = Int_val(vy) - 1 + w.Top; |
| 208 |
|
| 209 |
|
| 210 |
fprintf(stderr,"vx,vy = %d,%d --> [c.X,Y = %d,%d ; L %d R %d T %d B %d]\n", |
| 211 |
Int_val(vx),Int_val(vy), c.X, c.Y, w.Left, w.Right, w.Top, w.Bottom); |
| 212 |
|
| 213 |
if (c.X > w.Right) c.X = w.Right; |
| 214 |
if (c.Y > w.Bottom) c.Y = w.Bottom; |
| 215 |
exn_of_error("ANSITerminal.set_cursor", |
| 216 |
! SetConsoleCursorPosition(hStdout, c)); |
| 217 |
return Val_unit; |
| 218 |
} |
| 219 |
|
| 220 |
CAMLexport |
| 221 |
value ANSITerminal_FillConsoleOutputCharacter( |
| 222 |
value vchan, value vc, value vlen, value vx, value vy) |
| 223 |
{ |
| 224 |
CAMLparam1(vchan); |
| 225 |
HANDLE h = HANDLE_OF_CHAN(vchan); |
| 226 |
int NumberOfCharsWritten; |
| 227 |
COORD dwWriteCoord; |
| 228 |
|
| 229 |
SET_CSBI("ANSITerminal.erase"); |
| 230 |
exn_of_error("ANSITerminal.erase", |
| 231 |
! GetConsoleScreenBufferInfo(hStdout, csbiInfo)); |
| 232 |
dwWriteCoord.X = Int_val(vx) - 1 + csbiInfo->srWindow.Left; |
| 233 |
dwWriteCoord.Y = Int_val(vy) - 1 + csbiInfo->srWindow.Top; |
| 234 |
exn_of_error("ANSITerminal.erase", |
| 235 |
!FillConsoleOutputCharacter(h, Int_val(vc), Int_val(vlen), |
| 236 |
dwWriteCoord, |
| 237 |
&NumberOfCharsWritten)); |
| 238 |
CAMLreturn(Val_int(NumberOfCharsWritten)); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
CAMLexport |
| 243 |
value ANSITerminal_Scroll(value vx) |
| 244 |
{ |
| 245 |
|
| 246 |
INT x = Int_val(vx); |
| 247 |
SMALL_RECT srctScrollRect, srctClipRect; |
| 248 |
CHAR_INFO chiFill; |
| 249 |
COORD coordDest; |
| 250 |
|
| 251 |
SET_CSBI("ANSITerminal.scroll"); |
| 252 |
srctScrollRect.Left = 0; |
| 253 |
srctScrollRect.Top = 1; |
| 254 |
srctScrollRect.Right = csbiInfo->dwSize.X - x; |
| 255 |
srctScrollRect.Bottom = csbiInfo->dwSize.Y - x; |
| 256 |
|
| 257 |
|
| 258 |
coordDest.X = 0; |
| 259 |
coordDest.Y = 0; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
srctClipRect = srctScrollRect; |
| 264 |
|
| 265 |
|
| 266 |
chiFill.Attributes = FOREGROUND_RED|FOREGROUND_INTENSITY; |
| 267 |
chiFill.Char.AsciiChar = (char) ' '; |
| 268 |
|
| 269 |
exn_of_error("ANSITerminal.scroll", |
| 270 |
! ScrollConsoleScreenBuffer( |
| 271 |
hStdout, |
| 272 |
&srctScrollRect, |
| 273 |
&srctClipRect, |
| 274 |
coordDest, |
| 275 |
&chiFill)); |
| 276 |
return Val_unit; |
| 277 |
} |