From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA19654; Thu, 21 Aug 2003 18:50:13 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id SAA13931 for ; Thu, 21 Aug 2003 18:50:12 +0200 (MET DST) Received: from jamlikhet.polytechnique.org (b.mx.polytechnique.org [129.104.30.35]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h7LGoBT18323 for ; Thu, 21 Aug 2003 18:50:11 +0200 (MET DST) Received: from alan-schm1p.inria.fr (DHCP12-10.CIS.UPENN.EDU [158.130.13.30]) by ssl.polytechnique.org (Postfix) with ESMTP id F2F2AC539 for ; Thu, 21 Aug 2003 18:50:10 +0200 (CEST) Received: by alan-schm1p.inria.fr (Postfix, from userid 501) id 6223F467B; Thu, 21 Aug 2003 12:49:47 -0400 (EDT) Date: Thu, 21 Aug 2003 12:49:47 -0400 From: Alan Schmitt To: caml-list@inria.fr Subject: Re: [Caml-list] unbuffered input: a solution Message-ID: <20030821164947.GK18587@alan-schm1p> Mail-Followup-To: caml-list@inria.fr References: <20030821161157.GH18587@alan-schm1p> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030821161157.GH18587@alan-schm1p> X-Editor: Vim http://vim.sf.net/ X-Info: http://pauillac.inria.fr/~aschmitt/ X-Operating-System: Linux/2.4.21-6mdk (i686) X-Uptime: 12:43:40 up 1 day, 8:29, 8 users, load average: 0.02, 0.07, 0.07 User-Agent: Mutt/1.5.4i X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 char:01 char:01 buffering:01 owen:99 unison:01 tcgetattr:01 stdin:01 tcsetattr:01 stdin:01 icanon:01 printf:01 printf:01 stdout:01 tcsetattr:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk * Alan Schmitt (alan.schmitt@polytechnique.org) wrote: > Hi, > > I've been looking at this for about one hour, and I seem to get stuck: > how can I do unbuffered input in caml ? I want a program to block until > a char is entered. I tried input_char and Unix.read (without > and with Unix.select), to no avail. I do not know if it is caml, the > libc, or the terminal that is buffering. > > Any hint on how to do this ? Thanks to Owen Gunden and the unison code, I found the following solution: let term = Unix.tcgetattr Unix.stdin in try Unix.tcsetattr Unix.stdin Unix.TCSANOW {term with Unix.c_icanon = false; Unix.c_echo = false; Unix.c_vmin = 1;}; while true do let c = input_char stdin in Printf.printf "read %c\n" c; flush stdout done; (* restore the terminal in its initial state *) Unix.tcsetattr Unix.stdin Unix.TCSANOW term with | e -> Unix.tcsetattr Unix.stdin Unix.TCSANOW term; raise e Is there no simpler solution ? And by the way, I see that unison does a Sys.catch_break true to transform the break signal into an exception, but I did not need it here (doing ctrl-c does restore the terminal in a good state). I'm curious as to why this is the case. Alan Schmitt -- The hacker: someone who figured things out and made something cool happen. ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners