File: main.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (604 lines) | stat: -rw-r--r-- 16,314 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/* 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef __GLIBC__
#include <malloc.h>
#endif

#ifndef  WAIT_ANY
#define  WAIT_ANY -1
#endif

#include <locale.h>

#include <glib-object.h>

#include "libgimpbase/gimpbase.h"

#include "config/gimpconfig-dump.h"

#include "core/core-types.h"
#include "core/gimp.h"

#include "app_procs.h"
#include "errors.h"
#include "sanity.h"
#include "units.h"

#include "gimp-intl.h"


#ifdef G_OS_WIN32
#include <windows.h>
#else
static void  gimp_sigfatal_handler (gint         sig_num) G_GNUC_NORETURN;
static void  gimp_sigchld_handler  (gint         sig_num);
#endif

static void  gimp_show_version     (void);
static void  gimp_show_help        (const gchar *progname);


/*
 *  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. We do this because
 *      unparsed args are treated as images to load on
 *      startup.
 *
 *      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.
 */

int
main (int    argc,
      char **argv)
{
  const gchar        *abort_message           = NULL;
  const gchar        *full_prog_name          = NULL;
  const gchar        *alternate_system_gimprc = NULL;
  const gchar        *alternate_gimprc        = NULL;
  const gchar        *session_name            = NULL;
  const gchar        *batch_interpreter       = NULL;
  const gchar       **batch_commands          = NULL;
  gboolean            show_help               = FALSE;
  gboolean            no_interface            = FALSE;
  gboolean            no_data                 = FALSE;
  gboolean            no_fonts                = FALSE;
  gboolean            no_splash               = FALSE;
  gboolean            be_verbose              = FALSE;
  gboolean            use_shm                 = FALSE;
  gboolean            use_cpu_accel           = TRUE;
  gboolean            console_messages        = FALSE;
  gboolean            use_debug_handler       = FALSE;
#ifdef GIMP_UNSTABLE
  GimpStackTraceMode  stack_trace_mode        = GIMP_STACK_TRACE_QUERY;
  GimpPDBCompatMode   pdb_compat_mode         = GIMP_PDB_COMPAT_WARN;
#else
  GimpStackTraceMode  stack_trace_mode        = GIMP_STACK_TRACE_NEVER;
  GimpPDBCompatMode   pdb_compat_mode         = GIMP_PDB_COMPAT_ON;
#endif
  gint                i, j;

#if 0
  g_mem_set_vtable (glib_mem_profiler_table);
  g_atexit (g_mem_profile);
#endif

  /* Initialize variables */

  full_prog_name = argv[0];

  /* Initialize i18n support */

  setlocale (LC_ALL, "");

  bindtextdomain (GETTEXT_PACKAGE"-libgimp", gimp_locale_directory ());
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE"-libgimp", "UTF-8");
#endif

  bindtextdomain (GETTEXT_PACKAGE, gimp_locale_directory ());
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
#endif

  textdomain (GETTEXT_PACKAGE);

  /* Check argv[] for "--no-interface" before trying to initialize gtk+.
   * We also check for "--help" or "--version" here since those shouldn't
   * require gui libs either.
   */
  for (i = 1; i < argc; i++)
    {
      const gchar *arg = argv[i];

      if (arg[0] != '-')
        continue;

      if ((strcmp (arg, "--no-interface") == 0) ||
	  (strcmp (arg, "-i") == 0))
	{
	  no_interface = TRUE;
	}
      else if ((strcmp (arg, "--version") == 0) ||
               (strcmp (arg, "-v") == 0))
	{
	  gimp_show_version ();
	  app_exit (EXIT_SUCCESS);
	}
      else if ((strcmp (arg, "--help") == 0) ||
	       (strcmp (arg, "-h") == 0))
	{
	  gimp_show_help (full_prog_name);
	  app_exit (EXIT_SUCCESS);
	}
      else if (strncmp (arg, "--dump-gimprc", 13) == 0)
        {
          GimpConfigDumpFormat format = GIMP_CONFIG_DUMP_NONE;

          if (strcmp (arg, "--dump-gimprc") == 0)
            format = GIMP_CONFIG_DUMP_GIMPRC;
          if (strcmp (arg, "--dump-gimprc-system") == 0)
            format = GIMP_CONFIG_DUMP_GIMPRC_SYSTEM;
          else if (strcmp (arg, "--dump-gimprc-manpage") == 0)
            format = GIMP_CONFIG_DUMP_GIMPRC_MANPAGE;

          if (format)
            {
              Gimp     *gimp;
              gboolean  success;

              g_type_init ();

              gimp = g_object_new (GIMP_TYPE_GIMP, NULL);

              units_init (gimp);

              success = gimp_config_dump (format);

              g_object_unref (gimp);

              app_exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
            }
        }
    }

  if (! app_libs_init (&no_interface, &argc, &argv))
    {
      const gchar *msg;

      msg = _("GIMP could not initialize the graphical user interface.\n"
              "Make sure a proper setup for your display environment exists.");
      g_print ("%s\n\n", msg);

      app_exit (EXIT_FAILURE);
    }

  abort_message = sanity_check ();
  if (abort_message)
    app_abort (no_interface, abort_message);

  g_set_application_name (_("The GIMP"));

#if defined (USE_SYSV_SHM) || defined (USE_POSIX_SHM) || defined (G_OS_WIN32)
  use_shm = TRUE;
#endif

#ifdef __GLIBC__
  /* Tweak memory allocation so that memory allocated in chunks >= 4k
   * (64x64 pixel 1bpp tile) gets returned to the system when free'd ().
   */
  mallopt (M_MMAP_THRESHOLD, 64 * 64 - 1);
#endif

  batch_commands    = g_new (const gchar *, argc);
  batch_commands[0] = NULL;

  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "--g-fatal-warnings") == 0)
	{
          GLogLevelFlags fatal_mask;

          fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
          fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
          g_log_set_always_fatal (fatal_mask);
 	  argv[i] = NULL;
	}
      else if ((strcmp (argv[i], "--no-interface") == 0) ||
               (strcmp (argv[i], "-i") == 0))
	{
	  no_interface = TRUE;
 	  argv[i] = NULL;
	}
      else if (strcmp (argv[i], "--batch-interpreter") == 0)
	{
	  argv[i] = NULL;
	  if (argc <= ++i)
            {
	      show_help = TRUE;
	    }
          else
            {
	      batch_interpreter = argv[i];
	      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_commands[j] = argv[i];
	      argv[i] = NULL;
	    }
	  batch_commands[j] = NULL;

          /* We need at least one batch command */
	  if (batch_commands[0] == NULL)
	    show_help = TRUE;
	}
      else if (strcmp (argv[i], "--system-gimprc") == 0)
	{
 	  argv[i] = NULL;
	  if (argc <= ++i)
            {
	      show_help = TRUE;
	    }
          else
            {
	      alternate_system_gimprc = argv[i];
	      argv[i] = NULL;
            }
	}
      else if ((strcmp (argv[i], "--gimprc") == 0) ||
               (strcmp (argv[i], "-g") == 0))
	{
	  argv[i] = NULL;
	  if (argc <= ++i)
            {
	      show_help = TRUE;
	    }
          else
            {
	      alternate_gimprc = argv[i];
	      argv[i] = NULL;
            }
	}
      else if ((strcmp (argv[i], "--no-data") == 0) ||
	       (strcmp (argv[i], "-d") == 0))
	{
	  no_data = TRUE;
 	  argv[i] = NULL;
	}
      else if ((strcmp (argv[i], "--no-fonts") == 0) ||
	       (strcmp (argv[i], "-f") == 0))
	{
	  no_fonts = TRUE;
 	  argv[i] = NULL;
	}
      else if ((strcmp (argv[i], "--no-splash") == 0) ||
	       (strcmp (argv[i], "-s") == 0))
	{
	  no_splash = TRUE;
 	  argv[i] = NULL;
	}
      else if (strcmp (argv[i], "--verbose") == 0)
	{
	  be_verbose = TRUE;
 	  argv[i] = NULL;
	}
      else if (strcmp (argv[i], "--no-shm") == 0)
	{
	  use_shm = FALSE;
 	  argv[i] = NULL;
	}
      else if (strcmp (argv[i], "--no-cpu-accel") == 0)
	{
	  use_cpu_accel = FALSE;
 	  argv[i] = NULL;
	}
      else if (strcmp (argv[i], "--debug-handlers") == 0)
	{
	  use_debug_handler = TRUE;
 	  argv[i] = NULL;
	}
      else if ((strcmp (argv[i], "--console-messages") == 0) ||
	       (strcmp (argv[i], "-c") == 0))
	{
	  console_messages = TRUE;
 	  argv[i] = NULL;
	}
      else if (strcmp (argv[i], "--session") == 0)
	{
	  argv[i] = NULL;
	  if (argc <= ++i)
            {
	      show_help = TRUE;
	    }
          else
            {
	      session_name = argv[i];
	      argv[i] = NULL;
            }
	}
      else if (strcmp (argv[i], "--stack-trace-mode") == 0)
	{
 	  argv[i] = NULL;
	  if (argc <= ++i)
            {
	      show_help = TRUE;
	    }
          else
            {
	      if (! strcmp (argv[i], "never"))
		stack_trace_mode = GIMP_STACK_TRACE_NEVER;
	      else if (! strcmp (argv[i], "query"))
		stack_trace_mode = GIMP_STACK_TRACE_QUERY;
	      else if (! strcmp (argv[i], "always"))
		stack_trace_mode = GIMP_STACK_TRACE_ALWAYS;
	      else
		show_help = TRUE;

	      argv[i] = NULL;
            }
	}
      else if (strcmp (argv[i], "--pdb-compat-mode") == 0)
	{
 	  argv[i] = NULL;
	  if (argc <= ++i)
            {
	      show_help = TRUE;
	    }
          else
            {
	      if (! strcmp (argv[i], "off"))
		pdb_compat_mode = GIMP_PDB_COMPAT_OFF;
	      else if (! strcmp (argv[i], "on"))
		pdb_compat_mode = GIMP_PDB_COMPAT_ON;
	      else if (! strcmp (argv[i], "warn"))
		pdb_compat_mode = GIMP_PDB_COMPAT_WARN;
	      else
		show_help = TRUE;

	      argv[i] = NULL;
            }
	}
      else if (strcmp (argv[i], "--") == 0)
        {
          /*
           *  everything after "--" is a parameter (i.e. image to load)
           */
          argv[i] = NULL;
          break;
        }
      else if (argv[i][0] == '-')
	{
          /*
           *  anything else starting with a '-' is an error.
           */
	  g_print (_("\nInvalid option \"%s\"\n"), argv[i]);
	  show_help = TRUE;
	}
    }

  if (show_help)
    {
      gimp_show_help (full_prog_name);
      app_exit (EXIT_FAILURE);
    }

#ifndef G_OS_WIN32

  /* No use catching these on Win32, the user won't get any
   * stack trace from glib anyhow. It's better to let Windows inform
   * about the program error, and offer debugging (if the user
   * has installed MSVC or some other compiler that knows how to
   * install itself as a handler for program errors).
   */

  /* Handle fatal signals */

  /* these are handled by gimp_terminate() */
  gimp_signal_private (SIGHUP,  gimp_sigfatal_handler, 0);
  gimp_signal_private (SIGINT,  gimp_sigfatal_handler, 0);
  gimp_signal_private (SIGQUIT, gimp_sigfatal_handler, 0);
  gimp_signal_private (SIGABRT, gimp_sigfatal_handler, 0);
  gimp_signal_private (SIGTERM, gimp_sigfatal_handler, 0);

  if (stack_trace_mode != GIMP_STACK_TRACE_NEVER)
    {
      /* these are handled by gimp_fatal_error() */
      gimp_signal_private (SIGBUS,  gimp_sigfatal_handler, 0);
      gimp_signal_private (SIGSEGV, gimp_sigfatal_handler, 0);
      gimp_signal_private (SIGFPE,  gimp_sigfatal_handler, 0);
    }

  /* Ignore SIGPIPE because plug_in.c handles broken pipes */

  gimp_signal_private (SIGPIPE, SIG_IGN, 0);

  /* Collect dead children */

  gimp_signal_private (SIGCHLD, gimp_sigchld_handler, SA_RESTART);

#endif /* G_OS_WIN32 */

  gimp_errors_init (full_prog_name,
                    use_debug_handler,
                    stack_trace_mode);

  app_run (full_prog_name,
           argc - 1,
           argv + 1,
           alternate_system_gimprc,
           alternate_gimprc,
           session_name,
           batch_interpreter,
           batch_commands,
           no_interface,
           no_data,
           no_fonts,
           no_splash,
           be_verbose,
           use_shm,
           use_cpu_accel,
           console_messages,
           stack_trace_mode,
           pdb_compat_mode);

  g_free (batch_commands);

  return EXIT_SUCCESS;
}


static void
gimp_show_version (void)
{
  g_print ("%s %s\n", _("GIMP version"), GIMP_VERSION);
}

static void
gimp_show_help (const gchar *progname)
{
  gimp_show_version ();

  g_print (_("\nUsage: %s [option ... ] [file ... ]\n\n"),
           gimp_filename_to_utf8 (progname));
  g_print (_("Options:\n"));
  g_print (_("  -h, --help               Output this help.\n"));
  g_print (_("  -v, --version            Output version information.\n"));
  g_print (_("  --verbose                Show startup messages.\n"));
  g_print (_("  --no-shm                 Do not use shared memory between GIMP and plugins.\n"));
  g_print (_("  --no-cpu-accel           Do not use special CPU accelerations.\n"));
  g_print (_("  -d, --no-data            Do not load brushes, gradients, palettes, patterns.\n"));
  g_print (_("  -f, --no-fonts           Do not load any fonts.\n"));
  g_print (_("  -i, --no-interface       Run without a user interface.\n"));
  g_print (_("  --display <display>      Use the designated X display.\n"));
  g_print (_("  -s, --no-splash          Do not show the startup window.\n"));
  g_print (_("  --session <name>         Use an alternate sessionrc file.\n"));
  g_print (_("  -g, --gimprc <gimprc>    Use an alternate gimprc file.\n"));
  g_print (_("  --system-gimprc <gimprc> Use an alternate system gimprc file.\n"));
  g_print (_("  --dump-gimprc            Output a gimprc file with default settings.\n"));
  g_print (_("  -c, --console-messages   Display warnings to console instead of a dialog box.\n"));
  g_print (_("  --debug-handlers         Enable non-fatal debugging signal handlers.\n"));
  g_print (_("  --stack-trace-mode <never | query | always>\n"
             "                           Debugging mode for fatal signals.\n"));
  g_print (_("  --pdb-compat-mode <off | on | warn>\n"
             "                           Procedural Database compatibility mode.\n"));
  g_print (_("  --batch-interpreter <procedure>\n"
             "                           The procedure to process batch commands with.\n"));
  g_print (_("  -b, --batch <commands>   Process commands in batch mode.\n"));
  g_print ("\n");
}


#ifdef G_OS_WIN32

/* In case we build this as a windowed application */

#ifdef __GNUC__
#  ifndef _stdcall
#    define _stdcall  __attribute__((stdcall))
#  endif
#endif

int _stdcall
WinMain (struct HINSTANCE__ *hInstance,
	 struct HINSTANCE__ *hPrevInstance,
	 char               *lpszCmdLine,
	 int                 nCmdShow)
{
  return main (__argc, __argv);
}

#endif /* G_OS_WIN32 */


#ifndef G_OS_WIN32

/* gimp core signal handler for fatal signals */

static void
gimp_sigfatal_handler (gint sig_num)
{
  switch (sig_num)
    {
    case SIGHUP:
    case SIGINT:
    case SIGQUIT:
    case SIGABRT:
    case SIGTERM:
      gimp_terminate (g_strsignal (sig_num));
      break;

    case SIGBUS:
    case SIGSEGV:
    case SIGFPE:
    default:
      gimp_fatal_error (g_strsignal (sig_num));
      break;
    }
}

/* gimp core signal handler for death-of-child signals */

static void
gimp_sigchld_handler (gint sig_num)
{
  gint pid;
  gint status;

  while (TRUE)
    {
      pid = waitpid (WAIT_ANY, &status, WNOHANG);

      if (pid <= 0)
	break;
    }
}

#endif /* ! G_OS_WIN32 */