/* Katoob * Copyright (c) 2002,2003 Arabeyes, Mohammed Sameer. * * 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 2 of the License, 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. */ #ifdef HAVE_CONFIG_H #include #endif /* This should be included after config.h, to allow gettext stuff in config.h * to be effective, and because we removed G*_DISABLE_DEPRECATED from the * Makefile to katoob.h */ #include "katoob.h" #include #include /* realpath() */ #include /* realpath() */ #include /* strcmp() */ #include /* unlink() */ #include #include #include #include "katoobwindow.h" #include "recent.h" #include "init.h" #include "conf.h" #include "misc.h" #include "emulator.h" #include "mdi.h" #include "encodings.h" #include "misc.h" #include "prefs.h" #include "dnd.h" #include "crash.h" #ifdef ENABLE_PRINT #include "print.h" #endif /* ENABLE_PRINT */ #ifdef HAVE_SPELL #include "spelldlg.h" /* katoob_create_spelldlg() */ #endif /* HAVE_SPELL */ conf *config; /* our configuration struct */ UI *katoob; /* Initialised in katoob_init() */ #ifdef ENABLE_PRINT PConfig *PConf; #endif /* ENABLE_PRINT */ #ifdef EXPERIMENTAL char *_name; unsigned long _pid; /* Our signal handling function */ void signal_handler (const int sig); static void setup_signal_handler (); #endif int main (int argc, char *argv[]) { gchar c; gint fd; gchar *tmp; FILE *tmpfile; gchar *file; gchar file2[PATH_MAX + 1]; gint i = 0; gchar *_tmp; #ifdef ENABLE_NLS bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR); bind_textdomain_codeset (PACKAGE, "UTF-8"); textdomain (PACKAGE); #endif gtk_set_locale (); gtk_init (&argc, &argv); #ifdef EXPERIMENTAL _name = g_strdup (argv[0]); _pid = getpid (); /* Maybe we can catch anything ;-) */ setup_signal_handler (); #endif /* Initialise some variables */ katoob_init (&config, &katoob); /* Set the default configuration options */ config_init (config); /* Load the configuration file */ config_load (config); #ifdef ENABLE_PRINT print_config_init (); print_config_load (); #endif /* ENABLE_PRINT */ recent_init (); /* Parse the command line arguments */ parse_options (argc, argv); if (config->disable_xft) { setenv ("GDK_USE_XFT", "0", 1); } if (config->xkb) { xkb_init (katoob); } if (config->emulator) { katoob_emulator_init (); } if (!recent_load (config)) { katoob_debug ("Couldn't get recent documents file"); } encodings_init (); _tmp = g_strdup_printf ("Default encoding(%i): %s \"%s\"", config->defenc, katoob_encodings_get_by_number (config->defenc), katoob_encodings_get_name_by_number (config->defenc)); katoob_debug (_tmp); g_free (_tmp); #ifdef HAVE_SPELL /* This should be here first cause we'll need config->dicts when getting the default_dict */ config->dicts = katoob_spell_get_available_dicts (); if (!config->default_dict) { config->default_dict = g_strdup (katoob_spell_get_default_dict ()); } _tmp = g_strdup_printf ("The default dict. is: %s", config->default_dict); katoob_debug (_tmp); g_free (_tmp); create_mispelled_color (); #endif /* HAVE_SPELL */ #ifdef ENABLE_HIGHLIGHT katoob->hl_manager = gtk_source_languages_manager_new (); #endif /* ENABLE_HIGHLIGHT */ katoob->win = katoob_window_new (); katoob_update_main_interface (KATOOB_WINDOW (katoob->win)); gtk_widget_show_all (katoob->win); /* Setup the DnD stuff. */ katoob_dnd_init (); if (config->emulator) { g_signal_connect (G_OBJECT (katoob->win), "key-press-event", G_CALLBACK (katoob_text_view_emulator_insert), NULL); } g_signal_connect_after (G_OBJECT (KATOOB_WINDOW (katoob->win)->notebook), "switch-page", G_CALLBACK (katoob_switch_page_handler), NULL); if (config->files_toopen) { while (1) { file = g_slist_nth_data (config->files_toopen, i); if (!file) { break; } if (!strcmp (file, "-")) { katoob_debug ("Reading from stdin."); tmp = g_strdup_printf ("%s/%sXXXXXX", g_get_tmp_dir (), PACKAGE); katoob_debug (tmp); fd = g_mkstemp (tmp); if (fd == -1) { g_free (tmp); katoob_debug ("Could not create temporary file"); katoob_error (_("Could not create temporary file")); } else { katoob_debug (tmp); tmpfile = fdopen (fd, "w"); if (!tmpfile) { g_free (tmp); katoob_debug ("Could not open temporary file"); katoob_error (_("Could not open temporary file")); } else { printf ("%s: Reading from stdin...\n", PACKAGE); fflush (stdout); while ((c = getchar ()) != EOF) { fputc (c, tmpfile); } fclose (tmpfile); katoob_create_stdin_doc (tmp); unlink (tmp); g_free (tmp); close (fd); } /* katoob_create_stdin_doc (tmp); unlink (tmp); g_free (tmp); */ } } else { realpath (file, file2); if (file2) { katoob_create_doc_from_file (file2, TRUE, config->defenc); } } i++; } g_slist_foreach (config->files_toopen, (GFunc) g_free, NULL); g_slist_free (config->files_toopen); } else { katoob_create_doc (); } if ((config->xkb) && (!katoob->xkb_grps)) { xkb_error (); } /* We should grab the active document focus. */ katoob_document_grab_focus (katoob_get_active_doc ()); gtk_main (); katoob_debug ("main() Exiting."); return 0; } gboolean katoob_exit () { KatoobDocument *doc = NULL; gint x; katoob_debug (__FUNCTION__); while ((doc = katoob_get_active_doc ()) != NULL) { if (!katoob_close_doc (doc)) { return TRUE; } } config_save (); recent_save (); gtk_main_quit (); #ifdef ENABLE_PRINT /* NOTE: * This should be called before config_free() as we examine config->saveonexit */ print_config_save (); print_config_free (); #endif /* ENABLE_PRINT */ config_free (); g_datalist_clear (&katoob->recent_files); for (x = 0; x < XkbNumKbdGroups; x++) { if (katoob->xkb_grps[x]) { XFree (katoob->xkb_grps[x]); } } #ifdef EXPERIMENTAL g_free (_name); #endif return FALSE; } #ifdef EXPERIMENTAL static void setup_signal_handler () { katoob_debug (__FUNCTION__); /* Invalid memory reference */ signal (SIGSEGV, signal_handler); /* Interrupt from keyboard */ signal (SIGINT, signal_handler); /* Quit from keyboard */ signal (SIGQUIT, signal_handler); /* Floating point exception */ signal (SIGFPE, signal_handler); /* Bus error (bad memory access) */ signal (SIGBUS, signal_handler); /* Termination signal */ signal (SIGTERM, signal_handler); /* Hangup detected on controlling terminal */ signal (SIGHUP, signal_handler); /* Illegal Instruction */ signal (SIGILL, signal_handler); } void signal_handler (const int sig) { static volatile unsigned long _new = 0; if (_new) { return; } ++_new; katoob_debug (__FUNCTION__); switch (sig) { case SIGSEGV: case SIGFPE: case SIGBUS: case SIGILL: { katoob_save_open (); katoob_init_debug (sig); break; } case SIGTERM: { katoob_save_open (); config_save (); exit (255); break; } case SIGHUP: { katoob_reinit (); break; } case SIGQUIT: case SIGINT: { katoob_save_open (); exit (255); break; } } } #endif