//
// Author: Hongwei Xi (hwxi AT cs DOT bu DOT edu)
// Time: May 20, 2009
//

//
// list all the words with a given prefix; see the command [look]
//

(* ****** ****** *)

#include "BUCASCS320.hats"

(* ****** ****** *)

fun mylook (
  inp: FILEref, prfx: string, nprfx: size_t
) : void = let
  val word = input_line (inp)
in
//
if stropt_is_some word then let
  val word = stropt_unsome (word)
  val sgn = strncasecmp (word, prfx, nprfx)
  val () = if sgn = 0 then printf ("%s\n", @(word))
in
  if strncmp (prfx, word, nprfx) >= 0 then mylook (inp, prfx, nprfx)
end else (
  // nothing
) (* end of [if] *)
//
end // end of [mylook]

(* ****** ****** *)

#define WORDS_FILE "/usr/share/dict/words"

implement
main (argc, argv) = let
  val () = assert (argc >= 2)
  val prfx = argv.[1]
  val nprfx = string_length (prfx)
  val inp = open_file_exn (WORDS_FILE, file_mode_r)
  val () = mylook (inp, prfx, nprfx)
  val () = close_file_exn (inp)
in
  // empty
end (* end of [main] *)

(* ****** ****** *)

(* end of [mylook.dats] *)