/* poke.pk - Application infrastructure.  */

/* Copyright (C) 2020, 2021, 2022, 2023, 2024, 2025, 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 <http://www.gnu.org/licenses/>.
 */

/**** Globals. ****/

/* Many of the following variables are modified by poke.c when poke
   initializes, after loading this file.  The initialization values
   below are therefore the defaults.  */

/* poke can be run either interactively (from a tty) or in batch mode.
   The following predicate records this.  */

var pk_interactive_p = 0;

/* Whether poke should be quiet.  */
var pk_quiet_p = 0;

/* The endianness of the running host.  */
var pk_host_endian = ENDIAN_BIG;

/* The endianness of the network to which the running host is
   connected.  */
var pk_network_endian = ENDIAN_BIG;

/* Whether the terminal hyperlinks server is running or not.  */
var pk_hserver_p = 0;

/* The following global contains the name of the program to use to
   display documentation.  Valid values are `info' and `less'.  */
var pk_doc_viewer = "info";

/* Whether poke was built with support for libNBD.  */
var pk_have_libnbd_p = 0;

/* Whether to user the terminal pager.  */
var pk_pager_p = 0;

/* Default exception handler.  Do not do anything here that could
   raise an exception, because it will be silently ignored :P */

fun pk_exception_handler = (Exception exception) void:
  {
   if (exception.code != EC_exit && exception.code != EC_signal)
     {
       printf ("unhandled %<warning:%s%> exception\n",
               exception.name == "" ? "unknown" : exception.name);

       if (exception.location != "" || exception.msg != "")
         {
           if (exception.location != "")
             print (exception.location + " ");
           print (exception.msg + "\n");
         }
     }
  }

/* Bounder for constructing unbounded Poke array types from C.  */

fun pk_bounder_unbounded = any:
{
  /* XXX use `push null' whenever supported.  */
  return asm any: ("push 7");
}

/* Print current dispatching strategy of PVM.  */

fun pk_print_vm_dispatch = void:
{
  printf ("%s\n", vm_dispatch);
}

/* Load some poke subsystems.  */

load "pk-table.pk";
load "pk-cmd-ios.pk";
load "pk-cmd-info.pk";
load "pk-cmd-help.pk";
load "pk-cmd-set.pk";
load "pk-cmd-misc.pk";
load "pk-tracer.pk";

/* Default machinery for the poke prompt.  */

var pk_prompt_comment = 0;
var pk_prompt_show_ios = 1;
var pk_prompt_show_endian = 1;
var pk_prompt_show_pp = 1;
var pk_prompt_show_obase = 1;

fun pk_prompt = string:
{
  if (pk_quiet_p)
    return pk_prompt_comment ? "#!poke!# " : "(poke) ";

  var prompt = pk_prompt_comment ? "#!" : "(";
  var flags = "";

  if (pk_prompt_show_pp && vm_opprint)
    flags += "p";

  if (pk_prompt_show_obase)
    flags += (vm_obase == 10 ? "0"
              : vm_obase == 16 ? "x"
              : vm_obase == 8 ? "o"
              : vm_obase == 2 ? "2"
              : "");

  if (flags != "")
    prompt += flags + ":";

  if (pk_prompt_show_endian)
    prompt += get_endian == ENDIAN_BIG ? "big:" : "little:";

  if (pk_prompt_show_ios)
    {
      try prompt += iohandler (get_ios);
      catch if E_no_ios { prompt += "poke"; };
   }
  else
    prompt += "poke";

  return prompt + (pk_prompt_comment ? "!# " : ") ");
}

/* Add the current working directory to the load path.  */
load_path = ".:" + load_path;
