main.c 7.3 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17 18
 */
#include "config.h"
19
#include <locale.h>
Elliot Lee's avatar
Elliot Lee committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#ifndef  WAIT_ANY
#define  WAIT_ANY -1
#endif   /*  WAIT_ANY  */

#include "appenv.h"
#include "app_procs.h"
#include "errors.h"
#include "install.h"
#include "tile.h"

static RETSIGTYPE on_signal (int);
static RETSIGTYPE on_sig_child (int);
static void       init (void);

/* GLOBAL data */
int no_interface;
int no_data;
45 46
int no_splash;
int no_splash_image;
47
int be_verbose;
Elliot Lee's avatar
Elliot Lee committed
48
int use_shm;
49
int use_debug_handler;
Elliot Lee's avatar
Elliot Lee committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
char *prog_name;		/* The path name we are invoked with */
char **batch_cmds;

/* LOCAL data */
static int gimp_argc;
static char **gimp_argv;

/*
 *  argv processing: 
 *      Arguments are either switches, their associated
 *      values, or image files.  As switches and their
 *      associated values are processed, those slots in
 *      the argv[] array are NULLed.
 *
 *      The GTK switches are processed first (X switches are
 *      processed here, not by any X routines).  Then the
 *      general GIMP switches are processed.  Any args
 *      left are assumed to be image files the GIMP should
 *      display.
 *
 *      The exception is the batch switch.  When this is
 *      encountered, all remaining args are treated as batch
 *      commands.
 */

Tim Janik's avatar
Tim Janik committed
75
int
Elliot Lee's avatar
Elliot Lee committed
76 77 78 79 80
main (int argc, char **argv)
{
  int show_version;
  int show_help;
  int i, j;
81
  gchar *display_name, *display_env;
Elliot Lee's avatar
Elliot Lee committed
82

83
  ATEXIT (g_mem_profile);
Elliot Lee's avatar
Elliot Lee committed
84 85 86 87 88

  /* Initialize variables */
  prog_name = argv[0];

  /* Initialize Gtk toolkit */
89 90
  gtk_set_locale ();
  setlocale(LC_NUMERIC, "C");  /* must use dot, not comma, as decimal separator */
Elliot Lee's avatar
Elliot Lee committed
91 92 93 94 95
  gtk_init (&argc, &argv);
  gtk_accelerator_table_set_mod_mask (NULL, GDK_SHIFT_MASK |
                                            GDK_CONTROL_MASK |
                                            GDK_MOD1_MASK);

96 97 98 99 100 101 102
  display_name = gdk_get_display ();
  display_env = g_new (gchar, strlen (display_name) + 9);
  *display_env = 0;
  strcat (display_env, "DISPLAY=");
  strcat (display_env, display_name);
  putenv (display_env);

Elliot Lee's avatar
Elliot Lee committed
103 104
  no_interface = FALSE;
  no_data = FALSE;
105 106
  no_splash = FALSE;
  no_splash_image = FALSE;
Elliot Lee's avatar
Elliot Lee committed
107
  use_shm = TRUE;
108
  use_debug_handler = FALSE;
Elliot Lee's avatar
Elliot Lee committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  batch_cmds = g_new (char*, argc);
  batch_cmds[0] = NULL;

  show_version = FALSE;
  show_help = FALSE;

  for (i = 1; i < argc; i++)
    {
      if ((strcmp (argv[i], "--no-interface") == 0) ||
	  (strcmp (argv[i], "-n") == 0))
	{
	  no_interface = TRUE;
	  argv[i] = NULL;
	}
      else if ((strcmp (argv[i], "--batch") == 0) ||
	       (strcmp (argv[i], "-b") == 0))
	{
	  argv[i] = NULL;
	  for (j = 0, i++ ; i < argc; j++, i++)
	    {
	      batch_cmds[j] = argv[i];
	      argv[i] = NULL;
	    }
	  batch_cmds[j] = NULL;
	}
      else if ((strcmp (argv[i], "--help") == 0) ||
	       (strcmp (argv[i], "-h") == 0))
	{
	  show_help = TRUE;
	}
      else if (strcmp (argv[i], "--version") == 0 ||
	       strcmp (argv[i], "-v") == 0)
	{
	  show_version = TRUE;
	}
      else if (strcmp (argv[i], "--no-data") == 0)
	{
	  no_data = TRUE;
	}
148 149 150 151 152 153 154 155
      else if (strcmp (argv[i], "--no-splash") == 0)
	{
	  no_splash = TRUE;
	}
      else if (strcmp (argv[i], "--no-splash-image") == 0)
	{
	  no_splash_image = TRUE;
	}
156 157 158 159
      else if (strcmp (argv[i], "--verbose") == 0)
	{
	  be_verbose = TRUE;
	}
Elliot Lee's avatar
Elliot Lee committed
160 161 162 163
      else if (strcmp (argv[i], "--no-shm") == 0)
	{
	  use_shm = FALSE;
	}
164 165 166 167
      else if (strcmp (argv[i], "--debug-handlers") == 0)
	{
	  use_debug_handler = TRUE;
	}
Elliot Lee's avatar
Elliot Lee committed
168 169 170 171 172 173 174 175 176 177
/*
 *    ANYTHING ELSE starting with a '-' is an error.
 */
      else if (argv[i][0] == '-')
	{
	  show_help = TRUE;
	}
    }

  if (show_version)
178
    g_print ("GIMP version " GIMP_VERSION "\n");
Elliot Lee's avatar
Elliot Lee committed
179 180 181 182 183 184 185 186 187 188

  if (show_help)
    {
      g_print ("\007Usage: %s [option ...] [files ...]\n", argv[0]);
      g_print ("Valid options are:\n");
      g_print ("  -h --help              Output this help.\n");
      g_print ("  -v --version           Output version info.\n");
      g_print ("  -b --batch <commands>  Run in batch mode.\n");
      g_print ("  -n --no-interface      Run without a user interface.\n");
      g_print ("  --no-data              Do not load patterns, gradients, palettes, brushes.\n");
189
      g_print ("  --verbose              Show startup messages.\n");
190 191
      g_print ("  --no-splash            Do not show the startup window.\n");
      g_print ("  --no-splash-image      Do not add an image to the startup window.\n");
Elliot Lee's avatar
Elliot Lee committed
192 193
      g_print ("  --no-shm               Do not use shared memory between GIMP and its plugins.\n");
      g_print ("  --no-xshm              Do not use the X Shared Memory extension.\n");
194
      g_print ("  --debug-handlers       Enable debugging signal handlers.\n");
Elliot Lee's avatar
Elliot Lee committed
195
      g_print ("  --display <display>    Use the designated X display.\n\n");
196

Elliot Lee's avatar
Elliot Lee committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    }

  if (show_version || show_help)
    exit (0);

  /* Handle some signals */
  signal (SIGHUP, on_signal);
  signal (SIGINT, on_signal);
  signal (SIGQUIT, on_signal);
  signal (SIGABRT, on_signal);
  signal (SIGBUS, on_signal);
  signal (SIGSEGV, on_signal);
  signal (SIGPIPE, on_signal);
  signal (SIGTERM, on_signal);
  signal (SIGFPE, on_signal);

  /* Handle child exits */
  signal (SIGCHLD, on_sig_child);

  /* Keep the command line arguments--for use in gimp_init */
  gimp_argc = argc - 1;
  gimp_argv = argv + 1;

  /* Check the installation */
  install_verify (init);

  /* Main application loop */
Tim Janik's avatar
Tim Janik committed
224 225 226 227
  if (!app_exit_finish_done ())
    gtk_main ();
  
  return 0;
Elliot Lee's avatar
Elliot Lee committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
}

static void
init ()
{
  /*  Continue initializing  */
  gimp_init (gimp_argc, gimp_argv);
}

static int caught_fatal_sig = 0;

static RETSIGTYPE
on_signal (int sig_num)
{
  if (caught_fatal_sig)
/*    raise (sig_num);*/
    kill (getpid (), sig_num);
  caught_fatal_sig = 1;

  switch (sig_num)
    {
    case SIGHUP:
250
      terminate ("sighup caught");
Elliot Lee's avatar
Elliot Lee committed
251 252
      break;
    case SIGINT:
253
      terminate ("sigint caught");
Elliot Lee's avatar
Elliot Lee committed
254 255
      break;
    case SIGQUIT:
256
      terminate ("sigquit caught");
Elliot Lee's avatar
Elliot Lee committed
257 258
      break;
    case SIGABRT:
259
      terminate ("sigabrt caught");
Elliot Lee's avatar
Elliot Lee committed
260 261
      break;
    case SIGBUS:
262
      fatal_error ("sigbus caught");
Elliot Lee's avatar
Elliot Lee committed
263 264
      break;
    case SIGSEGV:
265
      fatal_error ("sigsegv caught");
Elliot Lee's avatar
Elliot Lee committed
266 267
      break;
    case SIGPIPE:
268
      terminate ("sigpipe caught");
Elliot Lee's avatar
Elliot Lee committed
269 270
      break;
    case SIGTERM:
271
      terminate ("sigterm caught");
Elliot Lee's avatar
Elliot Lee committed
272 273
      break;
    case SIGFPE:
274
      fatal_error ("sigfpe caught");
Elliot Lee's avatar
Elliot Lee committed
275 276
      break;
    default:
277
      fatal_error ("unknown signal");
Elliot Lee's avatar
Elliot Lee committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
      break;
    }
}

static RETSIGTYPE
on_sig_child (int sig_num)
{
  int pid;
  int status;

  while (1)
    {
      pid = waitpid (WAIT_ANY, &status, WNOHANG);
      if (pid <= 0)
	break;
    }
}