/*
 * The Towers Of Hanoi
 * Flux OS kit Implementation
 * Copyright (C) 1999 Amit Singh. All Rights Reserved.
 * http://hanoi.kernelthread.com
 *
 * Tested with oskit-990402 on Linux 2.2.6
 */

/*
 * Creation directions (cwd = oskit-990402/examples/x86):
 *
 * gcc -c -o hanoi.o -DOSKIT -MD -DHAVE_CONFIG_H -I. -I../../examples/x86 \
 * -I../../examples/x86/more -I../../examples/x86/shared  -I- -I../../oskit/c \
 * -I../../examples/x86/shared -I../.. -I../.. -nostdinc -O2 -Wall hanoi.c
 *
 * ld -Ttext 100000 -L../../lib -o hanoi ../../lib/multiboot.o hanoi.o \
 * -loskit_kern -loskit_c -loskit_lmm ../../lib/crtn.o
 *
 * mklinuximage hanoi (cwd = oskit-990402/boot/linux)
 *
 * The final step (mklinuximage) will give a kernel image (zImage) bootable
 * using LILO.
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define FROM    1
#define TO      3
#define USING   2

#define MAXCMDLEN 1023

char *banner    = "Welcome to the Hanoi Operating System!\n";
char *copyright = "Copyright (C) 1999 Amit Singh. All Rights Reserved";
char *authorurl = "http://www.kernelthread.com";
char *fluxurl   = "http://www.cs.utah.edu/oskit";

void
dohanoi(int N, int from, int to, int using)
{
  if (N > 0) {
    dohanoi(N-1, from, using, to);
    printf ("move %d --> %d\n", from, to);
    dohanoi(N-1, using, to, from);
  }
}

int
main (int argc, char **argv)
{
  int i = 0;
  long int N;
  char cmd[MAXCMDLEN + 1];

  printf("\t%s\n", banner);
  printf("\t%s\n", copyright);
  printf("\t%s\n", authorurl);
  printf("\t%s\n\n", fluxurl);

  for (;;) {

    printf("Hanoi[%d]> ", ++i);
    cmd[0] = '\0';
    fgets(cmd, 1023, stdin);
    if (*cmd == '\0')
      continue;

    N = strtol(cmd, (char **)NULL, 10);
    /* a bit of error checking, LONG_XXX should be there in limits.h */
    if (N == LONG_MIN || N == LONG_MAX || N <= 0) {
      fprintf(stderr, "illegal value for number of disks\n");
      continue;
    }

    dohanoi(N, FROM, TO, USING);
  }

  exit(0);
}
