/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2020 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/extend.internal.h"
#include "libc/intrin/pushpop.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/thread/thread.h"

#ifdef __x86_64__
STATIC_YOINK("_init_g_fds");
#endif

struct Fds g_fds;
static struct Fd g_fds_static[8];

static textwindows dontinline void SetupWinStd(struct Fds *fds, int i, int x) {
  int64_t h;
  h = GetStdHandle(x);
  if (!h || h == -1) return;
  fds->p[i].kind = pushpop(kFdFile);
  fds->p[i].handle = h;
  atomic_store_explicit(&fds->f, i + 1, memory_order_relaxed);
}

textstartup void InitializeFileDescriptors(void) {
  struct Fds *fds;
  __fds_lock_obj._type = PTHREAD_MUTEX_RECURSIVE;
  fds = VEIL("r", &g_fds);
  fds->n = 4;
  atomic_store_explicit(&fds->f, 3, memory_order_relaxed);
  if (_weaken(_extend)) {
    fds->p = fds->e = (void *)kMemtrackFdsStart;
    fds->e =
        _weaken(_extend)(fds->p, fds->n * sizeof(*fds->p), fds->e, MAP_PRIVATE,
                         kMemtrackFdsStart + kMemtrackFdsSize);
  } else {
    fds->p = g_fds_static;
    fds->e = g_fds_static + ARRAYLEN(g_fds_static);
  }
  if (IsMetal()) {
    extern const char vga_console[];
    fds->f = 3;
    if (_weaken(vga_console)) {
      fds->p[0].kind = pushpop(kFdConsole);
      fds->p[1].kind = pushpop(kFdConsole);
      fds->p[2].kind = pushpop(kFdConsole);
    } else {
      fds->p[0].kind = pushpop(kFdSerial);
      fds->p[1].kind = pushpop(kFdSerial);
      fds->p[2].kind = pushpop(kFdSerial);
    }
    fds->p[0].handle = VEIL("r", 0x3F8ull);
    fds->p[1].handle = VEIL("r", 0x3F8ull);
    fds->p[2].handle = VEIL("r", 0x3F8ull);
  } else if (IsWindows()) {
    SetupWinStd(fds, 0, kNtStdInputHandle);
    SetupWinStd(fds, 1, kNtStdOutputHandle);
    SetupWinStd(fds, 2, kNtStdErrorHandle);
  }
  fds->p[1].flags = O_WRONLY | O_APPEND;
  fds->p[2].flags = O_WRONLY | O_APPEND;
}
