#include <debugconsole.h>
#include <kb.h>
#include <limits.h>
#include <process.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <terminal.h>

#define RECV_MAX (1024 * 16)

static int ce_pgid;

void receiver (void* arg) {
  (void)arg;

  for (;;) {
    char recv[RECV_MAX];
    memset (recv, 0, sizeof (recv));
    mail_receive (&recv, sizeof (recv) - 1);

    terminal_print (recv, strlen (recv));
  }
}

void app_main (void) {
  debug_printf ("Init process is running. Starting user shell...\n");

  int ce_pid = exec ("RD", "ce");
  ce_pgid = get_procgroup (ce_pid);

  process_spawn (&receiver, NULL);

  for (;;) {
    int ch = kb_read_key ();

    if (ch == 0)
      continue;

    mail_send (ce_pgid, (uint8_t*)&ch, 1);
  }
}
