/*
 * (C) Copyright 1992, ..., 2014 the "DOSEMU-Development-Team".
 *
 * for details see file COPYING in the DOSEMU distribution
 */

/************************************************
 * unix.c
 * Run unix commands from inside Dosemu
 *
 * Written by ???
 *
 ************************************************/


#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dos2linux.h"
#include "utilities.h"
#include "system.h"
#include "msetenv.h"
#include "emu.h"
#include "unix.h"

static int usage(void);

int unix_main(int argc, char **argv)
{
  char s[256];
  int c;
  int secure = 0, bg = 0;
  const char *getopt_string = "+bercsd:w";

  if (argc == 1 ||
      (argc == 2 && !strcmp (argv[1], "/?"))) {
    return usage();
  }

  GETOPT_RESET();		// glibc wants this to reser parser state
  while ((c = getopt(argc, argv, getopt_string)) != EOF) {
    /* Got a switch */
    switch (c) {
    case 'b':
	bg++;
	break;
    case 's':
	secure++;
	break;
    case 'd':
      if (chdir(argv[2]) != 0) {
	com_printf("Chdir failed\n");
	return 1;
      }
      break;
    case 'w':
      if (getcwd(s, sizeof(s)) == NULL) {
	com_printf("Getcwd failed\n");
	return 1;
      }
      com_printf("%s\n", s);
      break;
    default:
      return usage();
    }
  }
  if (optind < argc) {
    const char *term = mgetenv("TERM");
    if (term)
      emu_setenv("TERM", term, 1, dosemu_envp);

    if (secure) {
      if (argc - optind > 1) {
        com_printf("unix: arguments not allowed when -s is specified\n");
        return 1;
      }
      return run_unix_secure(argv[optind]);
    }
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
    return run_unix_command(argc - optind, argv + optind, bg);
#pragma GCC diagnostic pop
  }

  return 0;
}


static int usage(void)
{
  com_printf ("Usage: UNIX [FLAG COMMAND]\n\n");
  com_printf ("UNIX -d dir\n");
  com_printf ("  Set unix work dir to \"dir\".\n\n");
  com_printf ("UNIX -w\n");
  com_printf ("  Get current unix work dir.\n\n");
  com_printf ("UNIX [-b] command [arg1 ...]\n");
  com_printf ("  Execute the Linux command with the arguments given.\n");
  com_printf ("  -b means run in background.\n\n");
  com_printf ("UNIX\n");
  com_printf ("UNIX /?\n");
  com_printf ("  show this help screen\n");

  return 0;
}
