/* main() and termios functions.

   Copyright (C) 1993 Sebastiano Vigna

    This file is part of ne, the nice editor.

    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 1, 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, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them.   Help stamp out software-hoarding!  */


#include "ne.h"
#include "version.h"
#include "regex.h"
#include <signal.h>


#ifdef _AMIGA
unsigned char version[] = "\0$VER:"ABOUT_MSG;
#else
unsigned char version[] = "@(#)"ABOUT_MSG;
#endif


/* These lists contains the existing buffers, clips and macros. cur_buffer
denotes the currently displayed buffer. */

list buffers = { (node *)&buffers.tail, NULL, (node *)&buffers.head };
list clips = { (node *)&clips.tail, NULL, (node *)&clips.head };
list macros = { (node *)&macros.tail, NULL, (node *)&macros.head };

buffer *cur_buffer;


/* These function live here because they access cur_buffer. new_buffer()
creates a new buffer, adds it to the buffer list, and assign it to
cur_buffer. delete_buffer() destroys cur_buffer, and makes the previous or
next buffer the current buffer, if any of the two exists. */

buffer *new_buffer(void) {
	buffer *b = alloc_buffer(cur_buffer);

	if (b) {
		clear_buffer(b);
		if (cur_buffer) add(&b->b_node, &cur_buffer->b_node);
		else add_head(&buffers, &b->b_node);
		cur_buffer = b;
	}

	return(b);
}

int delete_buffer(void) {

	buffer *nb = (buffer *)cur_buffer->b_node.next, *pb = (buffer *)cur_buffer->b_node.prev;

	rem(&cur_buffer->b_node);
	free_buffer(cur_buffer);

	if (pb->b_node.prev) {
		cur_buffer = pb;
      return(TRUE);
	}

	if (nb->b_node.next) {
		cur_buffer = nb;
		return(TRUE);
	}

	return(FALSE);
}



/* The main() function. It is responsible for argument parsing, calling
some terminal and signal initialization functions, and entering the
event loop. */

int main(int argc, unsigned char **argv) {

	input_class ic;
	int c, no_config = FALSE;
	unsigned char *macro_name = NULL;

	for(c=1; c<argc; c++) {

		/* Special arguments start with two dashes. If we find one, we
		cancel its entry in argv[], so that it will be skipped when opening
		the specified files. */

		if (argv[c][0] == '-' && argv[c][1] == '-') {
			if (!strcmp(&argv[c][2], "noconfig")) {
				no_config = TRUE;
				argv[c] = NULL;
			}
			else if (!strcmp(&argv[c][2], "macro")) {
				if (c<argc-1) {
					macro_name = argv[c+1];
					argv[c] = argv[c+1] = NULL;
				}
			}
		}
	}

	/* Unless --noconfig was specified, we try to configure the
	menus and the keyboard. Note that these functions can exit() on error. */

	if (!no_config) {
		get_menu_configuration();
		get_key_bindings();
	}

	/* If we cannot even create a buffer, better go... */

	if (!new_buffer()) exit(1);
	clear_buffer(cur_buffer);

	/* General terminfo and cursor motion initalization. From here onwards,
	we cannot exit() lightly. */

	term_init();

	/* This is necessary so that buffer.c can ignore terminfo. */

	cur_buffer->turbo = lines*2;

	/* We will be always using the last line for the status bar. */

	set_terminal_window(lines-1);

	/* We read in all the key capabilities. */

	read_key_capabilities();

	/* Some initializations of other modules... */

	re_set_syntax(RE_SYNTAX_EGREP);

#ifndef _AMIGA

	/* This function sets fatal_code() as signal interrupt handler
	for all the dangerous signals (SIGILL, SIGSEGV etc.). */

	set_fatal_code();

	/* This is the alarm interrupt handler, which just set itself as
	the SIGALRM handler. */

	set_alarm_code(0);

	/* Both SIGINT and SIGQUIT are used for the interrupt character---we
	get whatever arrives. */

	signal(SIGINT, set_stop);
	signal(SIGQUIT, set_stop);

	/* We do not want to be stopped. */

	signal(SIGTSTP, SIG_IGN);

#endif

	/* The terminal is prepared for interactive I/O. */

	set_interactive_mode();

	clear_entire_screen();

	load_auto_prefs(cur_buffer, DEF_PREFS_NAME);

	if (argc > 1) {

		/* The first file opened does not need a NEW_BUFFER call. Note that
		file loading can be interrupted (wildcarding can sometimes produce
		unwanted results). */

		int first_file = TRUE;

		stop = FALSE;

		for(c=1; c<argc && !stop; c++) {
			if (argv[c]) {
				if (!first_file) do_action(cur_buffer, NEW_BUFFER, -1, NULL);
				else first_file = FALSE;

				do_action(cur_buffer, OPEN, -1, strdup(argv[c]));
			}
		}

		/* This call makes current the first specified file. It is called
		only if more than one buffer exist. */

		if (get_nth_buffer(1)) do_action(cur_buffer, NEXT_BUFFER, -1, NULL);
	}

	/* Note that we do not need to update the display. clear_entire_screen() is
	ok if no file was loaded; otherwise, the OPEN will call update_window(). */

	refresh_window(cur_buffer);

	if (macro_name) do_action(cur_buffer, MACRO, -1, strdup(macro_name));

	while(TRUE) {

		refresh_window(cur_buffer);
		draw_status_bar();

		move_cursor(cur_buffer->cur_y, cur_buffer->cur_x);

		while( (ic = class[c = get_key_code()]) == IGNORE);

		switch(ic) {
			case ALPHA:
				print_error(do_action(cur_buffer, INSERT_CHAR, c, NULL));
				break;

			case RETURN:
				print_error(do_action(cur_buffer, INSERT_LINE, -1, NULL));
				break;

			case COMMAND:
				print_error(execute_command_line(cur_buffer, key_binding[c]));
				break;

			default:
				break;
		}
	}
}
