let handle_input =
  let cur_pos = ref 0 in
  fun fd ->
    (*prerr_endline "read";*)
    match Unix.read fd buf !cur_pos (buf_size - !cur_pos) with
      0 ->
        (*prerr_endline "done";*)
        cur_pos := 0;
        true
    | n ->
        let s = String.sub buf 0 (!cur_pos + n) in
        (*
           prerr_endline (Printf.sprintf "done: %s" s);
        *)

        let nl =
          try Some (String.index s '\n')
          with Not_found -> None
        in
        (
         match nl with
           None -> cur_pos := !cur_pos + n
         | Some p ->
             let com = String.sub s 0 p in
             String.blit s (p+1) buf 0 (!cur_pos + n - p - 1);
             cur_pos := (!cur_pos + n - p - 1);
             Cam_hooks.display_message (Printf.sprintf "server received command: %s" com);
             (
              try Cam_commands.eval_command com
              with
                Failure s -> prerr_endline s
              | e -> prerr_endline (Printexc.to_string e)
             )
        );
        if !cur_pos >= buf_size then
          cur_pos := 0;
        true