File: dwmain.cpp

package info (click to toggle)
gs 5.10-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 14,960 kB
  • ctags: 25,299
  • sloc: ansic: 164,376; makefile: 3,020; cpp: 2,237; sh: 1,219; asm: 684; tcl: 434; perl: 56
file content (412 lines) | stat: -rw-r--r-- 10,389 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
/* Copyright (C) 1996, 1997, Russell Lang.  All rights reserved.
  
  This file is part of Aladdin Ghostscript.
  
  Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  License (the "License") for full details.
  
  Every copy of Aladdin Ghostscript must include a copy of the License,
  normally in a plain ASCII text file named PUBLIC.  The License grants you
  the right to copy, modify and redistribute Aladdin Ghostscript, but only
  under certain conditions described in the License.  Among other things, the
  License requires that the copyright notice and this notice be preserved on
  all copies.
*/


// dwmain.cpp
// Ghostscript DLL loader for Windows

#define STRICT
#include <windows.h>
#include <shellapi.h>
#ifndef __WIN32__
#include <toolhelp.h>
static BOOL dllfix(void);
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
extern "C" {
#include "gscdefs.h"
#define GSREVISION gs_revision
#include "gsdll.h"
}
#include "dwmain.h"
#include "dwdll.h"
#include "dwtext.h"
#include "dwimg.h"

#if defined(_MSC_VER) && defined(__WIN32__)
#define _export
#endif

/* public handles */
HINSTANCE ghInstance;

/* redirected stdio */
TextWindow tw;

const LPSTR szAppName = "Ghostscript";

#ifdef __WIN32__
const LPSTR szIniName = "GSWIN32.INI";
const char *szDllName = "GSDLL32.DLL";
#else
const LPSTR szIniName = "GSWIN.INI";
const char *szDllName = "GSDLL16.DLL";
#endif
const LPSTR szIniSection = "Text";

int dll_exit_status;

int FAR _export gsdll_callback(int message, char FAR *str, unsigned long count);

// the Ghostscript DLL class
gsdll_class gsdll;

char start_string[] = "systemdict /start get exec\n";

// program really starts at WinMain
int
new_main(int argc, char *argv[])
{
typedef char FAR * FARARGV_PTR;
FARARGV_PTR *far_argv;
int rc;

    // load DLL
    if (gsdll.load(ghInstance, szDllName, GSREVISION)) {
	char buf[256];
	gsdll.get_last_error(buf, sizeof(buf));
	tw.puts(buf);
	return 1;
    }

    
#ifdef __WIN32__
    far_argv = argv;
#else
    far_argv = new FARARGV_PTR[argc+1];
    if (!far_argv)
	return 1;
    for (int i = 0; i<argc; i++)
	far_argv[i] = argv[i];	// convert from near to far pointers
    far_argv[i+1] = NULL;
#endif

    // initialize the interpreter
    rc = gsdll.init(gsdll_callback, tw.get_handle(), argc, far_argv);
    if (rc == GSDLL_INIT_QUIT) {
        gsdll.unload();
	return 0;
    }
    if (rc) {
	char buf[256];
	gsdll.get_last_error(buf, sizeof(buf));
	tw.puts(buf);
        gsdll.unload();
#ifndef __WIN32__
	if (rc == GSDLL_INIT_IN_USE)
	    dllfix();   // offer option to unload NASTY!
#endif
	return rc;
    }

    // if (!batch)
    gsdll.execute(start_string, strlen(start_string));

#ifdef UNUSED
    int len;
    char line[256];
    do {
	len = tw.read_line(line, sizeof(line));
    } while ( len && !gsdll.execute(line, len) );
#endif
    
    gsdll.unload();

    return 0;
}


/* our exit handler */
/* also called from Text Window WM_CLOSE */
void win_exit(void)
{
    if (dll_exit_status) {
	/* display message box so error messages in text window can be read */
	char buf[80];
	if (IsIconic(tw.get_handle()))
	    ShowWindow(tw.get_handle(), SW_SHOWNORMAL);
	BringWindowToTop(tw.get_handle());  /* make text window visible */
	sprintf(buf, "Exit code %d\nSee text window for details",dll_exit_status);
	MessageBox((HWND)NULL, buf, szAppName, MB_OK | MB_ICONSTOP);
    }
}

void
set_font(void)
{
    int fontsize;
    char fontname[256];
    char buf[32];

    // read ini file
    GetPrivateProfileString(szIniSection, "FontName", "Courier New", fontname, sizeof(fontname), szIniName);
    fontsize = GetPrivateProfileInt(szIniSection, "FontSize", 10, szIniName);

    // set font
    tw.font(fontname, fontsize); 

    // write ini file
    WritePrivateProfileString(szIniSection, "FontName", fontname, szIniName);
    sprintf(buf, "%d", fontsize);
    WritePrivateProfileString(szIniSection, "FontSize", buf, szIniName);
}

int PASCAL 
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
{
#if defined(_MSC_VER)    /* MSC doesn't give us _argc and _argv[] so ...   */
#define MAXCMDTOKENS 128
	int	_argc=0;
	LPSTR	_argv[MAXCMDTOKENS];
	LPSTR	p, q;
#ifdef __WIN32__
	_argv[_argc] = "gswin32.exe";
#else
	_argv[_argc] = "gswin.exe";
#endif
	// Parse command line handling quotes.
	p = lpszCmdLine;
	while (*p) {
	    // for each argument
	    while ((*p) && (*p == ' '))
		p++;	// skip over leading spaces
	    if (*p == '\042') {
	       p++;		// skip "
	       q = p;
	       // scan to end of argument
	       // doesn't handle embedded quotes
	       while ((*p) && (*p != '\042'))
		    p++;
	       _argv[++_argc] = q;
	       if (*p)
		    *p++ = '\0';
	    }
	    else if (*p) {
	       // delimited by spaces
	       q = p;
	       while ((*p) && (*p != ' '))
		    p++;
	       _argv[++_argc] = q;
	       if (*p)
		    *p++ = '\0';
	    }
	}
	_argv[++_argc] = (LPSTR)NULL;
#endif

	if (hPrevInstance) {
	    MessageBox((HWND)NULL,"Can't run twice", szAppName, MB_ICONHAND | MB_OK);
	    return FALSE;
	}

	/* copy the hInstance into a variable so it can be used */
	ghInstance = hInstance;


	/* start up the text window */
	if (!hPrevInstance) {
	    HICON hicon = LoadIcon(hInstance, (LPSTR)MAKEINTRESOURCE(GSTEXT_ICON));
	    tw.register_class(hInstance, hicon);
	}
	set_font();
	tw.size(80, 80);
	tw.drag("(", ") run\r");

	// create the text window
	if (tw.create(szAppName, cmdShow))
	    exit(1);

	// initialize for image windows
	ImageWindow::hwndtext = tw.get_handle();
	ImageWindow::hInstance = hInstance;

	dll_exit_status = new_main(_argc, _argv);

 	win_exit();

	tw.destroy();

	return dll_exit_status;
}


int FAR _export
gsdll_callback(int message, char FAR *str, unsigned long count)
{
char buf[256];
ImageWindow *iw;
    switch (message) {
	case GSDLL_POLL:
	    {
		MSG msg;
		while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		    {
		    TranslateMessage(&msg);
		    DispatchMessage(&msg);
		    }
	    }
	    // If text window closed then abort Ghostscript
	    if (!IsWindow(tw.get_handle()))
		return -1;
	    break;
	case GSDLL_STDIN:
	    return tw.read_line(str, count);
	case GSDLL_STDOUT:
	    if (str != (char *)NULL)
		tw.write_buf(str, count);
	    return count;
	case GSDLL_DEVICE:
#ifdef DEBUG
	    sprintf(buf,"Callback: DEVICE %p %s\n", (char *)str,
		count ? "open" : "close");
	    tw.puts(buf);
#endif
	    if (count) {
		iw = new ImageWindow;
		iw->open(str);
	    }
	    else {
		iw = FindImageWindow(str);
		iw->close();
	    }
	    break;
	case GSDLL_SYNC:
#ifdef DEBUG
	    sprintf(buf,"Callback: SYNC %p\n", (char *)str);
	    tw.puts(buf);
#endif
	    iw = FindImageWindow(str);
	    iw->sync();
	    break;
	case GSDLL_PAGE:
#ifdef DEBUG
	    sprintf(buf,"Callback: PAGE %p\n", (char *)str);
	    tw.puts(buf);
#endif
	    iw = FindImageWindow(str);
	    iw->page();
	    break;
	case GSDLL_SIZE:
#ifdef DEBUG
	    sprintf(buf,"Callback: SIZE %p width=%d height=%d\n", (char *)str,
		(int)(count & 0xffff), (int)((count>>16) & 0xffff) );
	    tw.puts(buf);
#endif
	    iw = FindImageWindow(str);
	    iw->size( (int)(count & 0xffff), (int)((count>>16) & 0xffff) );
	    break;
	default:
	    sprintf(buf,"Callback: Unknown message=%d\n",message);
	    tw.puts(buf);
	    break;
    }
    return 0;
}


#ifndef __WIN32__
// This is a hack to cope with a GPF occuring in either Ghostscript
// or a driver loaded by Ghostscript.  If a GPF occurs, gswin16.exe
// is unloaded, but not gsdll16.dll.
// Attempts to reload gswin16.exe will fail because gsdll16.dll
// is already in use.  If this occurs, this code will unload
// gsdll16.dll, EVEN IF SOMEONE ELSE IS USING IT.

// The DLL module name as it appears in the module definition file
#define GSDLLNAME "GSDLL16"
// These are the names of EXE modules that are known users
// of the Ghostscript DLL.
// If one of these is running, it is not safe to unload the DLL.
#define GSEXE		"GSWIN16"

typedef HMODULE (WINAPI *TOOLMFN)(MODULEENTRY FAR *, LPCSTR);

static BOOL
dllfix(void)
{
HMODULE hdll;			// module handle of DLL
HMODULE hgswin16;		// module handle of Ghostscript
MODULEENTRY me_dll;		 // to contain details about DLL module
MODULEENTRY me_gswin16;  // details about Ghostscript
HINSTANCE htool;		// module handle of TOOLHELP.DLL
TOOLMFN lpfnModuleFindName = NULL;
char buf[256];
BOOL err = FALSE;

    // load TOOLHELP.DLL
    htool = LoadLibrary("TOOLHELP.DLL");
    if ( htool <= HINSTANCE_ERROR )
	err = TRUE;

    // get address of toolhelp function
    if (!err) {
	lpfnModuleFindName = (TOOLMFN)GetProcAddress(htool, "ModuleFindName");
	err = !lpfnModuleFindName;
    }

    // find handle for DLL
    if (!err) {
	memset(&me_dll, 0, sizeof(me_dll));
	me_dll.dwSize = sizeof(me_dll);
	hdll = lpfnModuleFindName(&me_dll, GSDLLNAME);
    }

    // look for Ghostscript EXE module
    // This should be found because we are it
    if (!err) {
	memset(&me_gswin16, 0, sizeof(me_gswin16));
	me_gswin16.dwSize = sizeof(me_gswin16);
	hgswin16 = lpfnModuleFindName(&me_gswin16, GSEXE);
    }

#ifdef DEBUG
    // for debugging, show what we have found
    if (hdll) {
	wsprintf(buf, "Found %s\nModule Handle=%d\nModule Usage=%d\nModule Path=%s",
	me_dll.szModule, me_dll.hModule, me_dll.wcUsage, me_dll.szExePath);
	MessageBox((HWND)NULL, buf, szAppName, MB_OK);
    }
    if (hgswin16) {
	wsprintf(buf, "Found %s\nModule Handle=%d\nModule Usage=%d\nModule Path=%s",
	me_gswin16.szModule, me_gswin16.hModule, me_gswin16.wcUsage,
	me_gswin16.szExePath);
	MessageBox((HWND)NULL, buf, szAppName, MB_OK);
    }
#endif

    // if DLL loaded and Ghostscript is not running...
    if (!err &&
	hdll && 		// Ghostscript DLL loaded
	(hgswin16 && me_gswin16.wcUsage == 1) // Only one copy of Ghostscript EXE
	) {
	wsprintf(buf, "%s is loaded but does not appear to be in use by Ghostscript. Unload it?", GSDLLNAME);
	if (MessageBox((HWND)NULL, buf, szAppName, MB_YESNO | MB_ICONHAND) == IDYES)
	    // If DLL is really in use, we about to cause a disaster
	    while (GetModuleUsage(hdll))
		FreeLibrary(hdll);
    }

    if (htool)
	FreeLibrary(htool);
    return err;
}
#endif