123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219(* File: ANSITerminal.ml
Allow colors, cursor movements, erasing,... under Unix and DOS shells.
*********************************************************************
Copyright 2004 by Troestler Christophe
Christophe.Troestler(at)umh.ac.be
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation, with the
special exception on linking described in file LICENSE.
This library 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 file
LICENSE for more details.
*)(** See the file ctlseqs.html (unix)
and (for DOS) http://www.ka.net/jmenees/Dos/Ansi.htm
*)openPrintf(* Erasing *)typeloc=Above|Below|Screenleterase=function|Above->print_string"\027[1J"|Below->print_string"\027[0J"|Screen->print_string"\027[2J"(* Cursor *)letset_cursorxy=ifx<=0then(ify>0thenprintf"\027[%id"y)else(* x > 0 *)ify<=0thenprintf"\027[%iG"xelseprintf"\027[%i;%iH"yxletmove_cursorxy=ifx>0thenprintf"\027[%iC"xelseifx<0thenprintf"\027[%iD"(-x);ify>0thenprintf"\027[%iB"yelseify<0thenprintf"\027[%iA"(-y)letsave_cursor()=print_string"\027[s"letrestore_cursor()=print_string"\027[u"(* Scrolling *)letscrolllines=iflines>0thenprintf"\027[%iS"lineselseiflines<0thenprintf"\027[%iT"(-lines)(* Colors *)letautoreset=reftrueletset_autoresetb=autoreset:=btypecolor=Black|Red|Green|Yellow|Blue|Magenta|Cyan|White|Defaulttypestyle=|Reset|Bold|Underlined|Blink|Inverse|Hidden|Foregroundofcolor|Backgroundofcolorletblack=ForegroundBlackletred=ForegroundRedletgreen=ForegroundGreenletyellow=ForegroundYellowletblue=ForegroundBlueletmagenta=ForegroundMagentaletcyan=ForegroundCyanletwhite=ForegroundWhiteletdefault=ForegroundDefaultleton_black=BackgroundBlackleton_red=BackgroundRedleton_green=BackgroundGreenleton_yellow=BackgroundYellowleton_blue=BackgroundBlueleton_magenta=BackgroundMagentaleton_cyan=BackgroundCyanleton_white=BackgroundWhiteleton_default=BackgroundDefaultletstyle_to_string=function|Reset->"0"|Bold->"1"|Underlined->"4"|Blink->"5"|Inverse->"7"|Hidden->"8"|ForegroundBlack->"30"|ForegroundRed->"31"|ForegroundGreen->"32"|ForegroundYellow->"33"|ForegroundBlue->"34"|ForegroundMagenta->"35"|ForegroundCyan->"36"|ForegroundWhite->"37"|ForegroundDefault->"39"|BackgroundBlack->"40"|BackgroundRed->"41"|BackgroundGreen->"42"|BackgroundYellow->"43"|BackgroundBlue->"44"|BackgroundMagenta->"45"|BackgroundCyan->"46"|BackgroundWhite->"47"|BackgroundDefault->"49"letprint_stringstyletxt=print_string"\027[";lets=String.concat";"(List.mapstyle_to_stringstyle)inprint_strings;print_string"m";print_stringtxt;if!autoresetthenprint_string"\027[0m"letprintfstyle=kprintf(print_stringstyle)(* On DOS & windows, to enable the ANSI sequences, ANSI.SYS should be
loaded in C:\CONFIG.SYS with a line of the type
DEVICE = C:\DOS\ANSI.SYS
DEVICEHIGH=C:\WINDOWS\COMMAND\ANSI.SYS
This routine checks whether the line is present and, if not, it
inserts it and tells the user to reboot.
On WINNT, one will create a ANSI.NT in the user dir and a
command.com link on the desktop (with Configfilename = our ANSI.NT)
and tell the user to use it.
REM: that does NOT work under winxp because OCaml programs are not
considered to run in DOS mode only...
http://support.microsoft.com/default.aspx?scid=kb;en-us;816179
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_functions.asp
*)(* let is_readable file = *)(* try close_in(open_in file); true *)(* with Sys_error _ -> false *)(* let config_sys = "C:\\CONFIG.SYS" *)(* exception OK *)(* let win9x () = *)(* (\* Locate ANSI.SYS *\) *)(* let ansi_sys = List.find is_readable [ *)(* "C:\\DOS\\ANSI.SYS"; *)(* "C:\\WINDOWS\\COMMAND\\ANSI.SYS"; ] in *)(* (\* Parse CONFIG.SYS to see wether it has the right line *\) *)(* try *)(* let re = Str.regexp_case_fold *)(* ("^DEVICE\\(HIGH\\)?[ \t]*=[ \t]*" ^ ansi_sys ^ "[ \t]*$") in *)(* let fh = open_in config_sys in *)(* begin try *)(* while true do *)(* if Str.string_match re (input_line fh) 0 then raise OK *)(* done *)(* with *)(* | End_of_file -> *)(* (\* Correct line not found: add it *\) *)(* close_in fh; *)(* raise(Sys_error "win9x") *)(* | OK -> close_in fh (\* Correct line found, keep going *\) *)(* end *)(* with Sys_error _ -> *)(* (\* config_sys not does not exists or does not contain the right line. *\) *)(* let fh = open_out_gen [Open_wronly; Open_append; Open_creat; Open_text] *)(* 0x777 config_sys in *)(* output_string fh ("DEVICEHIGH=" ^ ansi_sys ^ "\n"); *)(* close_out fh; *)(* prerr_endline "Please restart your computer and rerun the program."; *)(* exit 1 *)(* let winnt home = *)(* (\* Locate ANSI.SYS *\) *)(* let system = *)(* try Sys.getenv "SystemRoot" *)(* with Not_found -> "C:\\WINDOWS" in *)(* let ansi_sys = *)(* List.find is_readable (List.map (fun s -> Filename.concat system s) *)(* [ "SYSTEM32\\ANSI.SYS"; ]) in *)(* (\* Create an ANSI.SYS file in the user dir *\) *)(* let ansi_nt = Filename.concat home "ANSI.NT" in *)(* let fh = open_out ansi_nt in *)(* output_string fh "dosonly\ndevice="; *)(* output_string fh ansi_sys; *)(* output_string fh "\ndevice=%SystemRoot%\\system32\\himem.sys *)
(* files=40 *)
(* dos=high, umb *)
(* " ; *)(* close_out fh; *)(* (\* Make a command.com link on the desktop *\) *)(* let fh = open_out (Filename.concat home "command.lnk") in *)(* close_out fh *)(* let () = *)(* if Sys.os_type = "Win32" then begin *)(* try winnt(Sys.getenv "USERPROFILE") (\* WinNT, Win2000, WinXP *\) *)(* with Not_found -> win9x() (\* Win9x *\) *)(* end *)