{ list.a68 - godcc list 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 List =
    access JSON, Argp, CE, Globals
def
    pub proc godcc_list = (int pos) void:
    begin
          proc list_languages = void:
          begin
                ce_connect;
                JSON_Val languages := ce_get_json("languages");
                for i to LEN languages
                do JSON_Val lang = languages->i;
                   JSON_Val extensions = lang->"extensions";
                   puts(json_str (lang->"id"));
                   puts(" (");
                   for j to LEN extensions
                   do if j > 1 then  puts(", ") fi;
                      puts(json_str (extensions->j))
                   od;
                   puts(")'n")
                od;
                ce_disconnect
          end;

          proc list_compilers = (string language) void:
          begin string what := "compilers";
                (language /= "" | what +:= "/" + language);
                ce_connect;
                ce_get_text(what,
                            (int e, n, fd) void:
                               begin (n > 0 | puts(fgets(fd, n)));
                                     (e = 0 | puts("'n"))
                               end);
                ce_disconnect
          end;

          proc list_formats = void:
          begin
                proc do_format = (ref JSON_Val f) void:
                begin puts(json_str(f->"name"));
                      json_for_each_elem(f->"styles",
                                         (ref JSON_Val style) void:
                                            puts (" (" + json_str(style) + ")"));
                      puts("'n");
                      false
                end;

                ce_connect;
                JSON_Val formats = ce_get_json("formats");
                json_foreach_elem(formats, do_format);
                ce_disconnect
          end;

          string what;
          argp(pos, (),
               (int pos, string arg) bool:
                  begin if arg = "languages" then list_languages
                        elif arg = "formats" then list_formats
                        elif arg = "compilers"
                        then string language;
                             [1]ArgOpt opts;
                             opts[1] := ("l", "language", true,
                                         (string arg) bool: (language := arg;
                                                             true));
                             argp(pos, opts,
                                  (int pos, string no_opt_arg) bool:
                                     (error("invalid argument to list compilers"); ~),
                                  error);
                             list_compilers(language)
                        else error("unknown subcommand " + arg); skip
                        fi;
                        false
                  end,
               error)
    end;

    skip
fed
