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