{ compile.a68 - godcc compile sub-command.

  Copyright (C) 2026 Jose E. Marchesi

  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 3 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, see
  <https://www.gnu.org/licenses/>. }

module Compile =
    access Utils, HTTP, Argp, CE, Globals
def
    pub proc godcc_compile = (int pos) void:
    begin
          { Try to infer the default for the compiler from the
            progname. }
          string compiler;
          begin string progname = argv(1);
                int dash = char_in_string(progname, "-");
                if dash > 0 AND dash <= UPB progname
                then compiler := progname[dash+1:]
                fi
          end;

          { Parse command-line options for the sub-command. }
          string src_file := "-", out_file := "-", comp_opts;
          argp (pos, []ArgOpt (("c", "compiler", true,
                                (string arg) bool: (compiler := arg; true)),
                               ("o", "", true,
                                (string arg) bool: (out_file := arg; true))),
                (int pos, string no_opt_arg) bool:
                   begin if no_opt_arg[1] = "-"
                         then comp_opts +:= " " + no_opt_arg
                         elif src_file /= "-"
                         then error("please specify exactly one source file to compile")
                         else src_file := no_opt_arg
                         fi;
                         true
                   end,
                error);
          (compiler = "" | error("please specify a compiler with -c|--compiler"));

          { Form the URI appending options if necessary. }
          string uri := "compiler/" + compiler + "/compile";
          if comp_opts /= ""
          then uri +:= "?options=" + http_escape_url(comp_opts) fi;

          { Open the input and output files.  }
          int in_fd := (src_file = "-" | 0 | fopen_checked(src_file, file_o_rdonly));
          int out_fd := (out_file = "-" | 1 | fcreate_checked(out_file, 8r644));

          { Query the server, printing the response. }
          ce_connect;
          ce_post_text(uri,
                       { contents }
                       (ref bool done) string:
                          begin ref string line = fgets (in_fd, 0);
                                done := UPB line = 0;
                                line
                          end,
                       { response }
                       (int e, n, fd) void:
                          (n > 0 | puts(fgets (fd, n))));
          ce_disconnect;

          { Close files. }
          (in_fd /= 1 | fclose_checked(src_file, in_fd));
          (out_fd /= 1 | fclose_checked(out_file, out_fd))
    end;

    skip
fed
