summaryrefslogtreecommitdiff
path: root/gs/psi/gs.c
blob: f97bfd8987b6fb7ba7b2acba2c7dda5ade8f2108 (plain)
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
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id$ */
/* 'main' program for Ghostscript */
#include "ghost.h"
#include "imain.h"
#include "imainarg.h"
#include "iapi.h"
#include "iminst.h"
#include "ierrors.h"
#include "gsmalloc.h"
#include "locale_.h"

#ifdef __GNUC__
#  if (__GNUC__ == 2 && __GNUC_MINOR__ == 96)
#    define NEED_COMMIT_STACK 1
#  endif
#endif

/* Define an optional array of strings for testing. */
/*#define RUN_STRINGS */
#ifdef RUN_STRINGS
static const char *run_strings[] =
{
    "2 vmreclaim /SAVE save def 2 vmreclaim",
    "(saved\n) print flush",
    "SAVE restore (restored\n) print flush 2 vmreclaim",
    "(done\n) print flush quit",
    0
};

#endif

#ifdef NEED_COMMIT_STACK
/*
 * It is well known that GCC 2.96 for x86 sometimes forgets to adjust $esp
 * and leaves automatic variables at small distance below the stack pointer.
 * Apparently, when the access to the automatic variable causes a page fault
 * Linux sends a SEGV signal if the access happens below the stack pointer.
 * Pre-loading the stack pages resolves the problem.
 */
static void
commit_stack_pages( void )
{
    char buf[65536]; /* In most cases GS lives in 64K stack */
    int i;
    for ( i = 0; i < sizeof(buf) - 1; i += 1024)
        buf[i] = 0;
}
#endif

int
main(int argc, char *argv[])
{
    int exit_status, code;
    gs_main_instance *minst;
    gs_memory_t *mem;

#ifdef NEED_COMMIT_STACK   /* hack for bug in gcc 2.96 */
    commit_stack_pages();
#endif
    exit_status = 0;

    /*
     * Call setlocale(LC_CTYPE), so that we can convert PDF passwords
     * from the locale character set to UTF-8 if necessary.  Note that
     * we only do this when running as a standalone application -- we
     * can't use setlocale at all if ghostscript is built as a library,
     * because it would affect the rest of the program.  Applications
     * that use ghostscript as a library are responsible for setting
     * the locale themselves.
     *
     * For now, we ignore the return value of setlocale, since there's
     * not much we can do here if it fails.  It might be nice to set
     * a flag instead, so we could warn the user if they later enter
     * a non-ASCII PDF password that doesn't work.
     */
    (void)setlocale(LC_CTYPE, "");
    mem = gs_malloc_init();
    minst = gs_main_alloc_instance(mem);
    code = (minst == NULL ? e_Fatal : 0);
    if (code >= 0)
        code = gs_main_init_with_args(minst, argc, argv);

#ifdef RUN_STRINGS
    {				/* Run a list of strings (for testing). */
        const char **pstr = run_strings;

        for (; *pstr; ++pstr) {
            int exit_code;
            ref error_object;
            int code;

            fprintf(stdout, "{%s} =>\n", *pstr);
            fflush(stdout);
            code = gs_main_run_string(minst, *pstr, 0,
                                      &exit_code, &error_object);
            zflush(osp);
            fprintf(stdout, " => code = %d\n", code);
            fflush(stdout);
            if (code < 0) {
                gs_to_exit(1);
                return 1;
            }
        }
    }
#endif

    if (code >= 0)
        code = gs_main_run_start(minst);

    exit_status = 0;
    switch (code) {
        case 0:
        case e_Info:
        case e_Quit:
            break;
        case e_Fatal:
            exit_status = 1;
            break;
        default:
            exit_status = 255;
    }

    gs_to_exit_with_code(minst->heap, exit_status, code);
    gs_malloc_release(mem);

    switch (exit_status) {
        case 0:
            exit_status =  exit_OK;
            break;
        case 1:
            exit_status =  exit_FAILED;
            break;
    }
    return exit_status;
}