1 /* Copyright (C) 2001-2006 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied, modified
8    or distributed except as expressly authorized under the terms of that
9    license.  Refer to licensing information at http://www.artifex.com/
10    or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11    San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 /* $Id: dxmainc.c 9085 2008-09-13 20:22:25Z giles $ */
14 
15 /* dxmainc.c */
16 /*
17  * Ghostscript frontend which provides a console to the Ghostscript
18  * shared library.  Load time linking to libgs.so
19  * This does not support the display device.  Use dxmain.c/gsx for that,
20  * or modify this program to use bare Xlib calls.
21  * Compile using
22  *    gcc -o gsc dxmainc.c -lgs
23  *
24  * The ghostscript library needs to be compiled with
25  *  gcc -fPIC -g -c -Wall file.c
26  *  gcc -shared -Wl,-soname,libgs.so.7 -o libgs.so.7.00 file.o -lc
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #define __PROTOTYPES__
36 #include "ierrors.h"
37 #include "iapi.h"
38 
39 const char start_string[] = "systemdict /start get exec\n";
40 
41 static int gsdll_stdin(void *instance, char *buf, int len);
42 static int gsdll_stdout(void *instance, const char *str, int len);
43 static int gsdll_stdout(void *instance, const char *str, int len);
44 
45 /*********************************************************************/
46 /* stdio functions */
47 
48 /* callback for reading stdin */
49 /* Use async input */
50 static int
gsdll_stdin(void * instance,char * buf,int len)51 gsdll_stdin(void *instance, char *buf, int len)
52 {
53     return read(fileno(stdin), buf, len);
54 }
55 
56 static int
gsdll_stdout(void * instance,const char * str,int len)57 gsdll_stdout(void *instance, const char *str, int len)
58 {
59     fwrite(str, 1, len, stdout);
60     fflush(stdout);
61     return len;
62 }
63 
64 static int
gsdll_stderr(void * instance,const char * str,int len)65 gsdll_stderr(void *instance, const char *str, int len)
66 {
67     fwrite(str, 1, len, stderr);
68     fflush(stderr);
69     return len;
70 }
71 
72 /*********************************************************************/
73 
main(int argc,char * argv[])74 int main(int argc, char *argv[])
75 {
76     int exit_status;
77     int code = 1, code1;
78     void *instance;
79     int exit_code;
80 
81     /* run Ghostscript */
82     if ((code = gsapi_new_instance(&instance, NULL)) == 0) {
83         gsapi_set_stdio(instance, gsdll_stdin, gsdll_stdout, gsdll_stderr);
84 	code = gsapi_init_with_args(instance, argc, argv);
85 
86 	if (code == 0)
87 	    code = gsapi_run_string(instance, start_string, 0, &exit_code);
88         code1 = gsapi_exit(instance);
89 	if (code == 0 || code == e_Quit)
90 	    code = code1;
91 	if (code == e_Quit)
92 	    code = 0;	/* user executed 'quit' */
93 
94 	gsapi_delete_instance(instance);
95     }
96 
97     exit_status = 0;
98     switch (code) {
99 	case 0:
100 	case e_Info:
101 	case e_Quit:
102 	    break;
103 	case e_Fatal:
104 	    exit_status = 1;
105 	    break;
106 	default:
107 	    exit_status = 255;
108     }
109 
110     return exit_status;
111 }
112 
113