/* * Get terminal size * * ---------------------------------------------------------------- * * Copyright (C) 2000-2006 Mojave Group, Caltech * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Authors: Jason Hickey * Aleksey Nogin */ #include #ifdef __CYGWIN__ # include #endif #ifdef WIN32 # include # pragma warning (disable: 4127 4189 4702) #else # include #endif #include #include #include #include value caml_term_size(value arg) { CAMLparam1(arg); CAMLlocal1(buf); /* Return a pair of numbers */ buf = alloc_small(2, 0); /* Get the terminal size, return None on failure */ #ifdef WIN32 { HANDLE fd = *(HANDLE *)Data_custom_val(arg); CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo; if (! GetConsoleScreenBufferInfo(fd, &ConsoleInfo)) failwith("lm_termsize.c: caml_term_size: GetConsoleScreenBufferInfo failed"); Field(buf, 0) = Val_int(ConsoleInfo.dwSize.Y); Field(buf, 1) = Val_int(ConsoleInfo.dwSize.X); } #else { int fd = Int_val(arg); struct winsize ws; if(ioctl(fd, TIOCGWINSZ, &ws) < 0) failwith("lm_termsize.c: caml_term_size: not a terminal"); /* Return the pair of numbers */ Field(buf, 0) = Val_int(ws.ws_row); Field(buf, 1) = Val_int(ws.ws_col); } #endif CAMLreturn(buf); }